]> arthur.barton.de Git - netdata.git/blobdiff - src/plugin_tc.c
improved netdata cleanup/exit procedure
[netdata.git] / src / plugin_tc.c
index ae967c16d8415ec99a9aa06b398cf8535399a63a..d5fd86aac77ddb4b5ffc4800595e60e633c069de 100644 (file)
@@ -100,8 +100,8 @@ avl_tree tc_device_root_index = {
         tc_device_compare
 };
 
-#define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
-#define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
+#define tc_device_index_add(st) (struct tc_device *)avl_insert(&tc_device_root_index, (avl *)(st))
+#define tc_device_index_del(st) (struct tc_device *)avl_remove(&tc_device_root_index, (avl *)(st))
 
 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
     struct tc_device tmp;
@@ -121,8 +121,8 @@ static int tc_class_compare(void* a, void* b) {
     else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
 }
 
-#define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
-#define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
+#define tc_class_index_add(st, rd) (struct tc_class *)avl_insert(&((st)->classes_index), (avl *)(rd))
+#define tc_class_index_del(st, rd) (struct tc_class *)avl_remove(&((st)->classes_index), (avl *)(rd))
 
 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
     struct tc_class tmp;
@@ -146,7 +146,8 @@ static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
 
     debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', seen=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->seen);
 
-    tc_class_index_del(n, c);
+    if(unlikely(tc_class_index_del(n, c) != c))
+        error("plugin_tc: INTERNAL ERROR: attempt remove class '%s' from device '%s': removed a different calls", c->id, n->id);
 
     freez(c->id);
     freez(c->name);
@@ -267,10 +268,10 @@ static inline void tc_device_commit(struct tc_device *d) {
         snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
         d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
 
-        snprintfz(var_name, CONFIG_MAX_NAME, "dropped tokens chart for %s", d->id);
+        snprintfz(var_name, CONFIG_MAX_NAME, "tokens chart for %s", d->id);
         d->enabled_tokens = config_get_boolean_ondemand("plugin:tc", var_name, enable_tokens);
 
-        snprintfz(var_name, CONFIG_MAX_NAME, "dropped ctokens chart for %s", d->id);
+        snprintfz(var_name, CONFIG_MAX_NAME, "ctokens chart for %s", d->id);
         d->enabled_ctokens = config_get_boolean_ondemand("plugin:tc", var_name, enable_ctokens);
     }
 
@@ -306,7 +307,7 @@ static inline void tc_device_commit(struct tc_device *d) {
             }
             else {
                 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
-                rrdset_next_plugins(d->st_bytes);
+                rrdset_next(d->st_bytes);
 
                 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
                     rrdset_set_name(d->st_bytes, d->name);
@@ -367,7 +368,7 @@ static inline void tc_device_commit(struct tc_device *d) {
             }
             else {
                 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
-                rrdset_next_plugins(d->st_packets);
+                rrdset_next(d->st_packets);
 
                 // FIXME
                 // update the family
@@ -421,7 +422,7 @@ static inline void tc_device_commit(struct tc_device *d) {
             }
             else {
                 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
-                rrdset_next_plugins(d->st_dropped);
+                rrdset_next(d->st_dropped);
 
                 // FIXME
                 // update the family
@@ -475,7 +476,7 @@ static inline void tc_device_commit(struct tc_device *d) {
             }
             else {
                 debug(D_TC_LOOP, "TC: Updating _tokens chart for device '%s'", d->name?d->name:d->id);
-                rrdset_next_plugins(d->st_tokens);
+                rrdset_next(d->st_tokens);
 
                 // FIXME
                 // update the family
@@ -529,7 +530,7 @@ static inline void tc_device_commit(struct tc_device *d) {
             }
             else {
                 debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
-                rrdset_next_plugins(d->st_ctokens);
+                rrdset_next(d->st_ctokens);
 
                 // FIXME
                 // update the family
@@ -619,7 +620,8 @@ static inline struct tc_device *tc_device_create(char *id)
         d->enabled = (char)-1;
 
         avl_init(&d->classes_index, tc_class_compare);
-        tc_device_index_add(d);
+        if(unlikely(tc_device_index_add(d) != d))
+            error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
 
         if(!tc_device_root) {
             tc_device_root = d;
@@ -660,7 +662,8 @@ static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char
             c->leaf_hash = simple_hash(c->leafid);
         }
 
-        tc_class_index_add(n, c);
+        if(unlikely(tc_class_index_add(n, c) != c))
+            error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
     }
 
     c->seen = 1;
@@ -677,7 +680,8 @@ static inline void tc_device_free(struct tc_device *n)
         else tc_device_root = n->prev;
     }
 
-    tc_device_index_del(n);
+    if(unlikely(tc_device_index_del(n) != n))
+        error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
 
     while(n->classes) tc_class_free(n, n->classes);
 
@@ -745,7 +749,7 @@ static inline void tc_split_words(char *str, char **words, int max_words) {
 
 pid_t tc_child_pid = 0;
 void *tc_main(void *ptr) {
-    (void)ptr;
+    struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
 
     info("TC thread created with task id %d", gettid());
 
@@ -792,8 +796,7 @@ void *tc_main(void *ptr) {
         fp = mypopen(buffer, &tc_child_pid);
         if(unlikely(!fp)) {
             error("TC: Cannot popen(\"%s\", \"r\").", buffer);
-            pthread_exit(NULL);
-            return NULL;
+            goto cleanup;
         }
 
         while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
@@ -994,8 +997,7 @@ void *tc_main(void *ptr) {
 
         if(unlikely(netdata_exit)) {
             tc_device_free_all();
-            pthread_exit(NULL);
-            return NULL;
+            goto cleanup;
         }
 
         if(code == 1 || code == 127) {
@@ -1004,13 +1006,17 @@ void *tc_main(void *ptr) {
             error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
 
             tc_device_free_all();
-            pthread_exit(NULL);
-            return NULL;
+            goto cleanup;
         }
 
         sleep((unsigned int) rrd_update_every);
     }
 
+cleanup:
+    info("TC thread exiting");
+
+    static_thread->enabled = 0;
+    static_thread->thread = NULL;
     pthread_exit(NULL);
     return NULL;
 }