8 #include <sys/resource.h>
13 #include "appconfig.h"
16 #include "plugin_tc.h"
19 #define RRD_TYPE_TC "tc"
20 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
22 // ----------------------------------------------------------------------------
24 // this requires the script plugins.d/tc-qos-helper.sh
26 #define TC_LINE_MAX 1024
44 unsigned long long bytes;
45 unsigned long long packets;
46 unsigned long long dropped;
47 unsigned long long overlimits;
48 unsigned long long requeues;
49 unsigned long long lended;
50 unsigned long long borrowed;
51 unsigned long long giants;
52 unsigned long long tokens;
53 unsigned long long ctokens;
60 char updated; // updated bytes
61 int seen; // seen in the tc list (even without bytes)
63 struct tc_class *next;
64 struct tc_class *prev;
88 avl_tree classes_index;
90 struct tc_class *classes;
92 struct tc_device *next;
93 struct tc_device *prev;
97 struct tc_device *tc_device_root = NULL;
99 // ----------------------------------------------------------------------------
102 static int tc_device_compare(void* a, void* b) {
103 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
104 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
105 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
108 avl_tree tc_device_root_index = {
113 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
114 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
116 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
117 struct tc_device tmp;
119 tmp.hash = (hash)?hash:simple_hash(tmp.id);
121 return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
125 // ----------------------------------------------------------------------------
128 static int tc_class_compare(void* a, void* b) {
129 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
130 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
131 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
134 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
135 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
137 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
140 tmp.hash = (hash)?hash:simple_hash(tmp.id);
142 return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
145 // ----------------------------------------------------------------------------
147 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
148 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);
150 if(c->next) c->next->prev = c->prev;
151 if(c->prev) c->prev->next = c->next;
152 if(n->classes == c) {
153 if(c->next) n->classes = c->next;
154 else n->classes = c->prev;
157 tc_class_index_del(n, c);
159 if(c->id) free(c->id);
160 if(c->name) free(c->name);
161 if(c->leafid) free(c->leafid);
162 if(c->parentid) free(c->parentid);
167 static inline void tc_device_classes_cleanup(struct tc_device *d) {
168 static int cleanup_every = 999;
170 if(unlikely(cleanup_every > 0)) {
171 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
172 if(cleanup_every < 0) cleanup_every = -cleanup_every;
176 d->family_updated = 0;
178 struct tc_class *c = d->classes;
180 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
181 struct tc_class *nc = c->next;
194 static inline void tc_device_commit(struct tc_device *d) {
195 static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1;
197 if(unlikely(enable_new_interfaces == -1)) {
198 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
199 enable_bytes = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
200 enable_packets = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
201 enable_dropped = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
204 // we only need to add leaf classes
205 struct tc_class *c, *x;
206 unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0;
207 int active_classes = 0;
210 for(c = d->classes ; c ; c = c->next) {
215 // mark the classes as leafs and parents
216 for(c = d->classes ; c ; c = c->next) {
217 if(unlikely(!c->updated)) continue;
219 for(x = d->classes ; x ; x = x->next) {
220 if(unlikely(!x->updated)) continue;
222 if(unlikely(c == x)) continue;
225 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
226 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
227 // 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);
236 for ( c = d->classes ; c ; c = c->next) {
237 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
238 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);
242 // we need at least a class
243 for(c = d->classes ; c ; c = c->next) {
244 // 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);
245 if(!c->updated) continue;
246 if(c->isleaf && c->hasparent) {
248 bytes_sum += c->bytes;
249 packets_sum += c->packets;
250 dropped_sum += c->dropped;
254 if(unlikely(!active_classes)) {
255 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
256 tc_device_classes_cleanup(d);
260 if(unlikely(d->enabled == -1)) {
261 char var_name[CONFIG_MAX_NAME + 1];
262 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
263 d->enabled = config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
265 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
266 d->enabled_bytes = config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
268 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
269 d->enabled_packets = config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
271 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
272 d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
275 if(likely(d->enabled)) {
276 // --------------------------------------------------------------------
279 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
280 d->enabled_bytes = CONFIG_ONDEMAND_YES;
282 if(unlikely(!d->st_bytes)) {
283 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
284 if(unlikely(!d->st_bytes)) {
285 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
286 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);
290 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
291 rrdset_next_plugins(d->st_bytes);
293 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
294 rrdset_set_name(d->st_bytes, d->name);
302 for(c = d->classes ; c ; c = c->next) {
303 if(unlikely(!c->updated)) continue;
305 if(c->isleaf && c->hasparent) {
308 if(unlikely(!c->rd_bytes)) {
309 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
310 if(unlikely(!c->rd_bytes)) {
311 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
313 // new class, we have to add it
314 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
316 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
319 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
321 // if it has a name, different to the id
322 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
323 // update the rrd dimension with the new name
324 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
325 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
329 rrdset_done(d->st_bytes);
332 // --------------------------------------------------------------------
335 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
336 d->enabled_packets = CONFIG_ONDEMAND_YES;
338 if(unlikely(!d->st_packets)) {
339 char id[RRD_ID_LENGTH_MAX + 1];
340 char name[RRD_ID_LENGTH_MAX + 1];
341 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
342 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
344 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
345 if(unlikely(!d->st_packets)) {
346 debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
347 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);
351 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
352 rrdset_next_plugins(d->st_packets);
358 for(c = d->classes ; c ; c = c->next) {
359 if(unlikely(!c->updated)) continue;
361 if(c->isleaf && c->hasparent) {
362 if(unlikely(!c->rd_packets)) {
363 c->rd_packets = rrddim_find(d->st_packets, c->id);
364 if(unlikely(!c->rd_packets)) {
365 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
367 // new class, we have to add it
368 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
370 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
373 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
375 // if it has a name, different to the id
376 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
377 // update the rrd dimension with the new name
378 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
379 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
383 rrdset_done(d->st_packets);
386 // --------------------------------------------------------------------
389 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
390 d->enabled_dropped = CONFIG_ONDEMAND_YES;
392 if(unlikely(!d->st_dropped)) {
393 char id[RRD_ID_LENGTH_MAX + 1];
394 char name[RRD_ID_LENGTH_MAX + 1];
395 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
396 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
398 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
399 if(unlikely(!d->st_dropped)) {
400 debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
401 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);
405 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
406 rrdset_next_plugins(d->st_dropped);
412 for(c = d->classes ; c ; c = c->next) {
413 if(unlikely(!c->updated)) continue;
415 if(c->isleaf && c->hasparent) {
416 if(unlikely(!c->rd_dropped)) {
417 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
418 if(unlikely(!c->rd_dropped)) {
419 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
421 // new class, we have to add it
422 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
424 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
427 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
429 // if it has a name, different to the id
430 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
431 // update the rrd dimension with the new name
432 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
433 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
437 rrdset_done(d->st_dropped);
441 tc_device_classes_cleanup(d);
444 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
446 struct tc_class *c = tc_class_index_find(d, id, 0);
453 if(likely(name && *name && strcmp(c->id, name) != 0)) {
454 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
455 c->name = strdup(name);
462 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
468 if(likely(name && *name && strcmp(d->id, name) != 0)) {
469 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
470 d->name = strdup(name);
476 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
477 if(likely(d->family))
482 if(likely(family && *family && strcmp(d->id, family) != 0)) {
483 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
484 d->family = strdup(family);
485 if(likely(d->family))
486 d->family_updated = 1;
488 // no need for null termination - it is already null
491 static inline struct tc_device *tc_device_create(char *id)
493 struct tc_device *d = tc_device_index_find(id, 0);
496 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
498 d = calloc(1, sizeof(struct tc_device));
500 fatal("Cannot allocate memory for tc_device %s", id);
505 d->hash = simple_hash(d->id);
508 avl_init(&d->classes_index, tc_class_compare);
509 tc_device_index_add(d);
511 if(!tc_device_root) {
515 d->next = tc_device_root;
516 tc_device_root->prev = d;
524 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
526 struct tc_class *c = tc_class_index_find(n, id, 0);
529 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
531 c = calloc(1, sizeof(struct tc_class));
533 fatal("Cannot allocate memory for tc class");
537 if(n->classes) n->classes->prev = c;
538 c->next = n->classes;
546 c->hash = simple_hash(c->id);
548 if(parentid && *parentid) {
549 c->parentid = strdup(parentid);
550 c->parent_hash = simple_hash(c->parentid);
553 if(leafid && *leafid) {
554 c->leafid = strdup(leafid);
555 c->leaf_hash = simple_hash(c->leafid);
558 tc_class_index_add(n, c);
566 static inline void tc_device_free(struct tc_device *n)
568 if(n->next) n->next->prev = n->prev;
569 if(n->prev) n->prev->next = n->next;
570 if(tc_device_root == n) {
571 if(n->next) tc_device_root = n->next;
572 else tc_device_root = n->prev;
575 tc_device_index_del(n);
577 while(n->classes) tc_class_free(n, n->classes);
579 if(n->id) free(n->id);
580 if(n->name) free(n->name);
581 if(n->family) free(n->family);
586 static inline void tc_device_free_all()
588 while(tc_device_root)
589 tc_device_free(tc_device_root);
594 static inline int tc_space(char c) {
607 static inline void tc_split_words(char *str, char **words, int max_words) {
611 // skip all white space
612 while(tc_space(*s)) s++;
614 // store the first word
617 // while we have something
620 if(unlikely(tc_space(*s))) {
622 // terminate the word
625 // skip all white space
626 while(tc_space(*s)) s++;
628 // if we reached the end, stop
631 // store the next word
632 if(i < max_words) words[i++] = s;
638 // terminate the words
639 while(i < max_words) words[i++] = NULL;
642 pid_t tc_child_pid = 0;
643 void *tc_main(void *ptr) {
646 info("TC thread created with task id %d", gettid());
648 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
649 error("Cannot set pthread cancel type to DEFERRED.");
651 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
652 error("Cannot set pthread cancel state to ENABLE.");
654 struct rusage thread;
655 RRDSET *stcpu = NULL, *sttime = NULL;
657 char buffer[TC_LINE_MAX+1] = "";
658 char *words[MAX_WORDS] = { NULL };
660 uint32_t BEGIN_HASH = simple_hash("BEGIN");
661 uint32_t END_HASH = simple_hash("END");
662 uint32_t CLASS_HASH = simple_hash("class");
663 uint32_t SENT_HASH = simple_hash("Sent");
664 uint32_t LENDED_HASH = simple_hash("lended:");
665 uint32_t TOKENS_HASH = simple_hash("tokens:");
666 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
667 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
668 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
669 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
670 #ifdef DETACH_PLUGINS_FROM_NETDATA
671 uint32_t MYPID_HASH = simple_hash("MYPID");
676 if(unlikely(netdata_exit)) break;
679 struct tc_device *device = NULL;
680 struct tc_class *class = NULL;
682 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);
683 debug(D_TC_LOOP, "executing '%s'", buffer);
685 fp = mypopen(buffer, &tc_child_pid);
687 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
692 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
693 if(unlikely(netdata_exit)) break;
695 buffer[TC_LINE_MAX] = '\0';
696 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
698 tc_split_words(buffer, words, MAX_WORDS);
700 if(unlikely(!words[0] || !*words[0])) {
701 // debug(D_TC_LOOP, "empty line");
704 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
706 first_hash = simple_hash(words[0]);
708 if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
709 // 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]);
711 // words[1] : class type
713 // words[3] : parent or root
714 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
715 //char *type = words[1]; // the class: htb, fq_codel, etc
717 // we are only interested for HTB classes
718 //if(strcmp(type, "htb") != 0) continue;
720 char *id = words[2]; // the class major:minor
721 char *parent = words[3]; // 'parent' or 'root'
722 char *parentid = words[4]; // the parent's id
723 char *leaf = words[5]; // 'leaf'
724 char *leafid = words[6]; // leafid
726 if(strcmp(parent, "root") == 0) {
730 else if(!leaf || strcmp(leaf, "leaf") != 0)
733 char leafbuf[20 + 1] = "";
734 if(leafid && leafid[strlen(leafid) - 1] == ':') {
735 strncpyz(leafbuf, leafid, 20 - 1);
736 strcat(leafbuf, "1");
740 class = tc_class_add(device, id, parentid, leafid);
743 // clear the last class
747 else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
748 // debug(D_TC_LOOP, "END line");
751 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
752 error("Cannot set pthread cancel state to DISABLE.");
754 tc_device_commit(device);
755 // tc_device_free(device);
757 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
758 error("Cannot set pthread cancel state to ENABLE.");
764 else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
765 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
767 if(likely(words[1] && *words[1])) {
768 device = tc_device_create(words[1]);
771 // tc_device_free(device);
777 else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
778 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
779 if(likely(words[1] && *words[1])) {
780 class->bytes = strtoull(words[1], NULL, 10);
787 if(likely(words[3] && *words[3]))
788 class->packets = strtoull(words[3], NULL, 10);
790 if(likely(words[6] && *words[6]))
791 class->dropped = strtoull(words[6], NULL, 10);
793 if(likely(words[8] && *words[8]))
794 class->overlimits = strtoull(words[8], NULL, 10);
796 if(likely(words[10] && *words[10]))
797 class->requeues = strtoull(words[8], NULL, 10);
799 else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
800 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
801 if(likely(words[1] && *words[1]))
802 class->lended = strtoull(words[1], NULL, 10);
804 if(likely(words[3] && *words[3]))
805 class->borrowed = strtoull(words[3], NULL, 10);
807 if(likely(words[5] && *words[5]))
808 class->giants = strtoull(words[5], NULL, 10);
810 else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
811 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
812 if(likely(words[1] && *words[1]))
813 class->tokens = strtoull(words[1], NULL, 10);
815 if(likely(words[3] && *words[3]))
816 class->ctokens = strtoull(words[3], NULL, 10);
818 else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
819 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
820 if(likely(words[1] && *words[1]))
821 tc_device_set_device_name(device, words[1]);
823 else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
824 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
825 if(likely(words[1] && *words[1]))
826 tc_device_set_device_family(device, words[1]);
828 else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
829 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
831 char *path = words[2];
832 if(likely(id && *id && path && *path))
833 tc_device_set_class_name(device, id, path);
835 else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
836 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
837 getrusage(RUSAGE_THREAD, &thread);
839 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
840 if(unlikely(!stcpu)) {
841 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
842 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
843 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
845 else rrdset_next(stcpu);
847 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
848 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
851 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
852 if(unlikely(!sttime)) {
853 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
854 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
856 else rrdset_next(sttime);
858 rrddim_set(sttime, "run_time", atoll(words[1]));
862 #ifdef DETACH_PLUGINS_FROM_NETDATA
863 else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
864 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
866 pid_t pid = atol(id);
868 if(likely(pid)) tc_child_pid = pid;
870 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
874 // debug(D_TC_LOOP, "IGNORED line");
878 // fgets() failed or loop broke
879 int code = mypclose(fp, tc_child_pid);
882 if(unlikely(device)) {
883 // tc_device_free(device);
888 if(unlikely(netdata_exit)) {
889 tc_device_free_all();
894 if(code == 1 || code == 127) {
896 // 127 = cannot even run it
897 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
899 tc_device_free_all();
904 sleep((unsigned int) rrd_update_every);