8 #include <sys/resource.h>
13 #include "appconfig.h"
16 #include "plugin_tc.h"
18 #include "../config.h"
20 #define RRD_TYPE_TC "tc"
21 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
23 // ----------------------------------------------------------------------------
25 // this requires the script plugins.d/tc-qos-helper.sh
27 #define TC_LINE_MAX 1024
45 unsigned long long bytes;
46 unsigned long long packets;
47 unsigned long long dropped;
48 unsigned long long overlimits;
49 unsigned long long requeues;
50 unsigned long long lended;
51 unsigned long long borrowed;
52 unsigned long long giants;
53 unsigned long long tokens;
54 unsigned long long ctokens;
61 char updated; // updated bytes
62 int seen; // seen in the tc list (even without bytes)
64 struct tc_class *next;
65 struct tc_class *prev;
85 avl_tree classes_index;
87 struct tc_class *classes;
89 struct tc_device *next;
90 struct tc_device *prev;
94 struct tc_device *tc_device_root = NULL;
96 // ----------------------------------------------------------------------------
99 static int tc_device_compare(void* a, void* b) {
100 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
101 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
102 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
105 avl_tree tc_device_root_index = {
110 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
111 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
113 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
114 struct tc_device tmp;
116 tmp.hash = (hash)?hash:simple_hash(tmp.id);
118 return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
122 // ----------------------------------------------------------------------------
125 static int tc_class_compare(void* a, void* b) {
126 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
127 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
128 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
131 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
132 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
134 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
137 tmp.hash = (hash)?hash:simple_hash(tmp.id);
139 return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
142 // ----------------------------------------------------------------------------
144 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
145 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);
147 if(c->next) c->next->prev = c->prev;
148 if(c->prev) c->prev->next = c->next;
149 if(n->classes == c) {
150 if(c->next) n->classes = c->next;
151 else n->classes = c->prev;
154 tc_class_index_del(n, c);
156 if(c->id) free(c->id);
157 if(c->name) free(c->name);
158 if(c->leafid) free(c->leafid);
159 if(c->parentid) free(c->parentid);
164 static inline void tc_device_classes_cleanup(struct tc_device *d) {
165 static int cleanup_every = 999;
167 if(unlikely(cleanup_every > 0)) {
168 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
169 if(cleanup_every < 0) cleanup_every = -cleanup_every;
173 d->family_updated = 0;
175 struct tc_class *c = d->classes;
177 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
178 struct tc_class *nc = c->next;
191 static inline void tc_device_commit(struct tc_device *d) {
192 static int enable_new_interfaces = -1;
194 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
196 // we only need to add leaf classes
197 struct tc_class *c, *x;
200 for(c = d->classes ; c ; c = c->next) {
205 // mark the classes as leafs and parents
206 for(c = d->classes ; c ; c = c->next) {
207 if(!c->updated) continue;
209 for(x = d->classes ; x ; x = x->next) {
210 if(!x->updated) continue;
215 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
216 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
217 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has as leaf class '%s' (parentid: '%s').", d->name?d->name:d->id, c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->name?x->name:x->id, x->parentid?x->parentid:x->id);
226 for ( c = d->classes ; c ; c = c->next) {
227 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
228 else debug(D_TC_LOOP, "TC: Device %s, class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name, c->id, c->isleaf, c->hasparent, c->parentid);
232 // we need at least a class
233 for(c = d->classes ; c ; c = c->next) {
234 // debug(D_TC_LOOP, "TC: Device '%s', class '%s', isLeaf=%d, HasParent=%d, Seen=%d", d->name?d->name:d->id, c->name?c->name:c->id, c->isleaf, c->hasparent, c->seen);
235 if(!c->updated) continue;
236 if(c->isleaf && c->hasparent) break;
239 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
240 tc_device_classes_cleanup(d);
244 if(unlikely(d->enabled == -1)) {
245 char var_name[CONFIG_MAX_NAME + 1];
246 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
247 d-> enabled = config_get_boolean("plugin:tc", var_name, enable_new_interfaces);
250 if(likely(d->enabled)) {
251 // --------------------------------------------------------------------
254 if(unlikely(!d->st_bytes)) {
255 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
256 if(unlikely(!d->st_bytes)) {
257 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
258 d->st_bytes = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, RRD_TYPE_TC ".qos", "Class Usage", "kilobits/s", 7000, rrd_update_every, RRDSET_TYPE_STACKED);
262 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
263 rrdset_next_plugins(d->st_bytes);
265 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
266 rrdset_set_name(d->st_bytes, d->name);
274 for(c = d->classes ; c ; c = c->next) {
275 if(unlikely(!c->updated)) continue;
279 if(c->isleaf && c->hasparent) {
280 if(unlikely(!c->rd_bytes)) {
281 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
282 if(unlikely(!c->rd_bytes)) {
283 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
285 // new class, we have to add it
286 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
288 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
291 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
293 // if it has a name, different to the id
294 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
295 // update the rrd dimension with the new name
296 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
297 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
301 rrdset_done(d->st_bytes);
303 // --------------------------------------------------------------------
306 if(unlikely(!d->st_packets)) {
307 char id[RRD_ID_LENGTH_MAX + 1];
308 char name[RRD_ID_LENGTH_MAX + 1];
309 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
310 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
312 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
313 if(unlikely(!d->st_packets)) {
314 debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
315 d->st_packets = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_packets", "Class Packets", "packets/s", 7010, rrd_update_every, RRDSET_TYPE_STACKED);
319 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
320 rrdset_next_plugins(d->st_packets);
326 for(c = d->classes ; c ; c = c->next) {
327 if(unlikely(!c->updated)) continue;
331 if(c->isleaf && c->hasparent) {
332 if(unlikely(!c->rd_packets)) {
333 c->rd_packets = rrddim_find(d->st_packets, c->id);
334 if(unlikely(!c->rd_packets)) {
335 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
337 // new class, we have to add it
338 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
340 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
343 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
345 // if it has a name, different to the id
346 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
347 // update the rrd dimension with the new name
348 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
349 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
353 rrdset_done(d->st_packets);
355 // --------------------------------------------------------------------
358 if(unlikely(!d->st_dropped)) {
359 char id[RRD_ID_LENGTH_MAX + 1];
360 char name[RRD_ID_LENGTH_MAX + 1];
361 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
362 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
364 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
365 if(unlikely(!d->st_dropped)) {
366 debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
367 d->st_dropped = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_dropped", "Class Dropped Packets", "packets/s", 7020, rrd_update_every, RRDSET_TYPE_STACKED);
371 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
372 rrdset_next_plugins(d->st_dropped);
378 for(c = d->classes ; c ; c = c->next) {
379 if(unlikely(!c->updated)) continue;
383 if(c->isleaf && c->hasparent) {
384 if(unlikely(!c->rd_dropped)) {
385 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
386 if(unlikely(!c->rd_dropped)) {
387 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
389 // new class, we have to add it
390 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
392 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
395 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
397 // if it has a name, different to the id
398 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
399 // update the rrd dimension with the new name
400 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
401 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
405 rrdset_done(d->st_dropped);
408 tc_device_classes_cleanup(d);
411 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
413 struct tc_class *c = tc_class_index_find(d, id, 0);
420 if(likely(name && *name && strcmp(c->id, name) != 0)) {
421 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
422 c->name = strdup(name);
429 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
435 if(likely(name && *name && strcmp(d->id, name) != 0)) {
436 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
437 d->name = strdup(name);
443 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
444 if(likely(d->family))
449 if(likely(family && *family && strcmp(d->id, family) != 0)) {
450 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
451 d->family = strdup(family);
452 if(likely(d->family))
453 d->family_updated = 1;
455 // no need for null termination - it is already null
458 static inline struct tc_device *tc_device_create(char *id)
460 struct tc_device *d = tc_device_index_find(id, 0);
463 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
465 d = calloc(1, sizeof(struct tc_device));
467 fatal("Cannot allocate memory for tc_device %s", id);
472 d->hash = simple_hash(d->id);
475 avl_init(&d->classes_index, tc_class_compare);
476 tc_device_index_add(d);
478 if(!tc_device_root) {
482 d->next = tc_device_root;
483 tc_device_root->prev = d;
491 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
493 struct tc_class *c = tc_class_index_find(n, id, 0);
496 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
498 c = calloc(1, sizeof(struct tc_class));
500 fatal("Cannot allocate memory for tc class");
504 if(n->classes) n->classes->prev = c;
505 c->next = n->classes;
513 c->hash = simple_hash(c->id);
515 if(parentid && *parentid) {
516 c->parentid = strdup(parentid);
517 c->parent_hash = simple_hash(c->parentid);
520 if(leafid && *leafid) {
521 c->leafid = strdup(leafid);
522 c->leaf_hash = simple_hash(c->leafid);
525 tc_class_index_add(n, c);
533 static inline void tc_device_free(struct tc_device *n)
535 if(n->next) n->next->prev = n->prev;
536 if(n->prev) n->prev->next = n->next;
537 if(tc_device_root == n) {
538 if(n->next) tc_device_root = n->next;
539 else tc_device_root = n->prev;
542 tc_device_index_del(n);
544 while(n->classes) tc_class_free(n, n->classes);
546 if(n->id) free(n->id);
547 if(n->name) free(n->name);
548 if(n->family) free(n->family);
553 static inline void tc_device_free_all()
555 while(tc_device_root)
556 tc_device_free(tc_device_root);
561 static inline int tc_space(char c) {
574 static inline void tc_split_words(char *str, char **words, int max_words) {
578 // skip all white space
579 while(tc_space(*s)) s++;
581 // store the first word
584 // while we have something
587 if(unlikely(tc_space(*s))) {
589 // terminate the word
592 // skip all white space
593 while(tc_space(*s)) s++;
595 // if we reached the end, stop
598 // store the next word
599 if(i < max_words) words[i++] = s;
605 // terminate the words
606 while(i < max_words) words[i++] = NULL;
609 pid_t tc_child_pid = 0;
610 void *tc_main(void *ptr)
614 info("TC thread created with task id %d", gettid());
616 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
617 error("Cannot set pthread cancel type to DEFERRED.");
619 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
620 error("Cannot set pthread cancel state to ENABLE.");
622 struct rusage thread;
623 RRDSET *stcpu = NULL, *sttime = NULL;
625 char buffer[TC_LINE_MAX+1] = "";
626 char *words[MAX_WORDS] = { NULL };
628 uint32_t BEGIN_HASH = simple_hash("BEGIN");
629 uint32_t END_HASH = simple_hash("END");
630 uint32_t CLASS_HASH = simple_hash("class");
631 uint32_t SENT_HASH = simple_hash("Sent");
632 uint32_t LENDED_HASH = simple_hash("lended:");
633 uint32_t TOKENS_HASH = simple_hash("tokens:");
634 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
635 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
636 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
637 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
638 #ifdef DETACH_PLUGINS_FROM_NETDATA
639 uint32_t MYPID_HASH = simple_hash("MYPID");
644 if(unlikely(netdata_exit)) break;
647 struct tc_device *device = NULL;
648 struct tc_class *class = NULL;
650 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), rrd_update_every);
651 debug(D_TC_LOOP, "executing '%s'", buffer);
652 // fp = popen(buffer, "r");
653 fp = mypopen(buffer, &tc_child_pid);
655 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
660 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
661 if(unlikely(netdata_exit)) break;
663 buffer[TC_LINE_MAX] = '\0';
664 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
666 tc_split_words(buffer, words, MAX_WORDS);
667 if(unlikely(!words[0] || !*words[0])) {
668 // debug(D_TC_LOOP, "empty line");
671 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
673 first_hash = simple_hash(words[0]);
675 if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
676 // debug(D_TC_LOOP, "CLASS line on class id='%s', parent='%s', parentid='%s', leaf='%s', leafid='%s'", words[2], words[3], words[4], words[5], words[6]);
678 // clear the last class
681 // words[1] : class type
683 // words[3] : parent or root
684 if(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
685 //char *type = words[1]; // the class: htb, fq_codel, etc
687 // we are only interested for HTB classes
688 //if(strcmp(type, "htb") != 0) continue;
690 char *id = words[2]; // the class major:minor
691 char *parent = words[3]; // 'parent' or 'root'
692 char *parentid = words[4]; // the parent's id
693 char *leaf = words[5]; // 'leaf'
694 char *leafid = words[6]; // leafid
696 if(strcmp(parent, "root") == 0) {
700 else if(!leaf || strcmp(leaf, "leaf") != 0)
703 char leafbuf[20 + 1] = "";
704 if(leafid && leafid[strlen(leafid) - 1] == ':') {
705 strncpyz(leafbuf, leafid, 20 - 1);
706 strcat(leafbuf, "1");
710 class = tc_class_add(device, id, parentid, leafid);
713 else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
714 // debug(D_TC_LOOP, "END line");
717 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
718 error("Cannot set pthread cancel state to DISABLE.");
720 tc_device_commit(device);
721 // tc_device_free(device);
725 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
726 error("Cannot set pthread cancel state to ENABLE.");
729 else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
730 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
733 // tc_device_free(device);
738 if(words[1] && *words[1]) {
739 device = tc_device_create(words[1]);
743 else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
744 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
745 if(words[1] && *words[1]) {
746 class->bytes = strtoull(words[1], NULL, 10);
750 if(words[3] && *words[3])
751 class->packets = strtoull(words[3], NULL, 10);
753 if(words[6] && *words[6])
754 class->dropped = strtoull(words[6], NULL, 10);
756 if(words[8] && *words[8])
757 class->overlimits = strtoull(words[8], NULL, 10);
759 if(words[10] && *words[10])
760 class->requeues = strtoull(words[8], NULL, 10);
762 else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
763 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
764 if(words[1] && *words[1])
765 class->lended = strtoull(words[1], NULL, 10);
767 if(words[3] && *words[3])
768 class->borrowed = strtoull(words[3], NULL, 10);
770 if(words[5] && *words[5])
771 class->giants = strtoull(words[5], NULL, 10);
773 else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
774 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
775 if(words[1] && *words[1])
776 class->tokens = strtoull(words[1], NULL, 10);
778 if(words[3] && *words[3])
779 class->ctokens = strtoull(words[3], NULL, 10);
781 else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
782 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
783 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
785 else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
786 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
787 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
789 else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
790 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
792 char *path = words[2];
793 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
795 else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
796 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
797 getrusage(RUSAGE_THREAD, &thread);
799 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
801 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
802 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
803 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
805 else rrdset_next(stcpu);
807 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
808 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
811 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
813 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
814 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
816 else rrdset_next(sttime);
818 rrddim_set(sttime, "run_time", atoll(words[1]));
822 #ifdef DETACH_PLUGINS_FROM_NETDATA
823 else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
824 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
826 pid_t pid = atol(id);
828 if(pid) tc_child_pid = pid;
830 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
834 // debug(D_TC_LOOP, "IGNORED line");
837 // fgets() failed or loop broke
838 int code = mypclose(fp, tc_child_pid);
842 // tc_device_free(device);
848 tc_device_free_all();
853 if(code == 1 || code == 127) {
855 // 127 = cannot even run it
856 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
858 tc_device_free_all();
863 sleep((unsigned int) rrd_update_every);