lidbdlog limiter: allow updating the ruleset 39/186139/4
authorMichal Bloch <m.bloch@samsung.com>
Tue, 7 Aug 2018 11:42:45 +0000 (13:42 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 21 Aug 2018 10:13:04 +0000 (12:13 +0200)
Do not cleanup the ruleset after turning it into a hashmap to allow reuse.

Change-Id: Id45171ba8c60f4f4e39789309276a06208deaef9
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
include/loglimiter.h
src/libdlog/loglimiter.c

index eac9c7f..cef665a 100644 (file)
@@ -34,6 +34,8 @@ int __log_limiter_pass_log(const char* tag, int prio);
 
 int __log_limiter_create(struct log_config *config);
 
+void __log_limiter_update(struct log_config *config);
+
 #ifdef __cplusplus
 }
 #endif
index a75499d..4424d60 100644 (file)
@@ -64,7 +64,8 @@ struct hashmap {
 struct hashmap* rules_hashmap = NULL;
 
 /* Keep rules table as single-linked list */
-struct rule* rules_table = NULL;
+struct rule* current_rules_table = NULL;
+struct rule* original_rules_table = NULL;
 
 #define HASHMAP_MASK(hm)          ((int)(hm->size - 1))
 #define HASHMAP_LINEAR_PROBE_LEAP 1
@@ -251,7 +252,7 @@ static struct rule* hashmap_search(struct hashmap* hm,
 }
 
 /* Must be always executed after __log_config_read() */
-int __log_limiter_initialize(void)
+int __log_limiter_initialize(struct rule *rules_table)
 {
        int hm_size;
        struct rule* rlist = NULL;
@@ -272,8 +273,10 @@ int __log_limiter_initialize(void)
        rules_hashmap = (struct hashmap*) hashmap_create(hm_size,
                                                        &rule_compare,
                                                        &rule_match);
-       if (NULL == rules_hashmap || !rules_hashmap->size)
-               goto bailout;
+       if (NULL == rules_hashmap || !rules_hashmap->size) {
+               hashmap_destroy(&rules_hashmap);
+               return -1;
+       }
 
        /* Add rule to hashmap */
        rlist = rules_table;
@@ -283,30 +286,24 @@ int __log_limiter_initialize(void)
        }
 
        return 0;
-
-bailout:
-       hashmap_destroy(&rules_hashmap);
-       rules_destroy(&rules_table);
-
-       return (-1);
 }
 // LCOV_EXCL_STOP
 
 void __log_limiter_destroy(void)
 {
        hashmap_destroy(&rules_hashmap);
-       rules_destroy(&rules_table);
+       rules_destroy(&original_rules_table);
 }
 
 // LCOV_EXCL_START
-int __log_limiter_add_rule(const char* tag, int prio, int limit)
+int __log_limiter_add_rule(struct rule** rules_table, const char* tag, int prio, int limit)
 {
        struct rule* r;
 
        if (!tag)
                return (-1);
 
-       for (struct rule *i = rules_table; i; i = i->prev) {
+       for (struct rule *i = *rules_table; i; i = i->prev) {
                if (strcmp(i->tag, tag) || i->prio != util_prio_to_char(prio))
                        continue;
 
@@ -327,8 +324,8 @@ int __log_limiter_add_rule(const char* tag, int prio, int limit)
        r->start = time(NULL);
        r->hit = 0;
 
-       r->prev = rules_table;
-       rules_table = r;
+       r->prev = *rules_table;
+       *rules_table = r;
 
        return 0;
 }
@@ -394,10 +391,12 @@ int __log_limiter_pass_log(const char* tag, int prio)
  * @param[in,out] u Userdata (unused)
  */
 // LCOV_EXCL_START
-static void __config_iteration(const char* key, const char* value, void *u)
+static void __config_iteration(const char* key, const char* value, void *userdata)
 {
        assert(key);
        assert(value);
+       struct rule **rules_table = (struct rule **)userdata;
+       assert(rules_table);
 
        static const int prefix_len = sizeof("limiter|") - 1;
        char * delimiter_pos;
@@ -420,17 +419,50 @@ static void __config_iteration(const char* key, const char* value, void *u)
        else
                limit = atoi(value);
 
-       __log_limiter_add_rule(limiter_tag, *(delimiter_pos + 1), limit);
+       __log_limiter_add_rule(rules_table, limiter_tag, *(delimiter_pos + 1), limit);
 }
 // LCOV_EXCL_STOP
 
 int __log_limiter_create(struct log_config *config)
 {
        assert(config);
-       assert(!rules_table);
+       assert(!original_rules_table);
        assert(!rules_hashmap);
 
-       log_config_foreach(config, __config_iteration, NULL); // LCOV_EXCL_LINE
+       log_config_foreach(config, __config_iteration, &original_rules_table); // LCOV_EXCL_LINE
+
+       const int r = __log_limiter_initialize(original_rules_table); // LCOV_EXCL_LINE
+       if (r)
+               rules_destroy(&original_rules_table);
+       return !r;
+}
+
+void __log_limiter_update(struct log_config *config)
+{
+       assert(config);
+       assert(!original_rules_table || rules_hashmap);
+
+       struct rule *rules_table = NULL;
+       for (struct rule *i = original_rules_table; i; i = i->prev)
+               __log_limiter_add_rule(&rules_table, i->tag, i->prio, i->limit);
+
+       log_config_foreach(config, __config_iteration, &rules_table);
+
+       if (rules_hashmap) {
+               for (struct rule *i = rules_table; i; i = i->prev) {
+                       const char prio_c = util_prio_to_char(i->prio);
+                       const struct rule *const prev_rule = hashmap_search(rules_hashmap, i->tag, prio_c);
+                       if (!prev_rule)
+                               continue;
+
+                       i->hit = prev_rule->hit;
+                       i->start = prev_rule->start;
+               }
+       }
+
+       hashmap_destroy(&rules_hashmap);
+       __log_limiter_initialize(rules_table);
 
-       return (__log_limiter_initialize() == 0); // LCOV_EXCL_LINE
+       rules_destroy(&current_rules_table);
+       current_rules_table = rules_table;
 }