3 #define RRD_TYPE_TC "tc"
4 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
6 // ----------------------------------------------------------------------------
8 // this requires the script plugins.d/tc-qos-helper.sh
10 #define TC_LINE_MAX 1024
28 unsigned long long bytes;
29 unsigned long long packets;
30 unsigned long long dropped;
31 unsigned long long overlimits;
32 unsigned long long requeues;
33 unsigned long long lended;
34 unsigned long long borrowed;
35 unsigned long long giants;
36 unsigned long long tokens;
37 unsigned long long ctokens;
44 char updated; // updated bytes
45 int seen; // seen in the tc list (even without bytes)
47 struct tc_class *next;
48 struct tc_class *prev;
72 avl_tree classes_index;
74 struct tc_class *classes;
76 struct tc_device *next;
77 struct tc_device *prev;
81 struct tc_device *tc_device_root = NULL;
83 // ----------------------------------------------------------------------------
86 static int tc_device_compare(void* a, void* b) {
87 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
88 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
89 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
92 avl_tree tc_device_root_index = {
97 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
98 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
100 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
101 struct tc_device tmp;
103 tmp.hash = (hash)?hash:simple_hash(tmp.id);
105 return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
109 // ----------------------------------------------------------------------------
112 static int tc_class_compare(void* a, void* b) {
113 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
114 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
115 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
118 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
119 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
121 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
124 tmp.hash = (hash)?hash:simple_hash(tmp.id);
126 return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
129 // ----------------------------------------------------------------------------
131 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
132 if(c == n->classes) {
134 n->classes = c->next;
136 n->classes = c->prev;
138 if(c->next) c->next->prev = c->prev;
139 if(c->prev) c->prev->next = c->next;
141 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);
143 tc_class_index_del(n, c);
152 static inline void tc_device_classes_cleanup(struct tc_device *d) {
153 static int cleanup_every = 999;
155 if(unlikely(cleanup_every > 0)) {
156 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
157 if(cleanup_every < 0) cleanup_every = -cleanup_every;
161 d->family_updated = 0;
163 struct tc_class *c = d->classes;
165 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
166 struct tc_class *nc = c->next;
179 static inline void tc_device_commit(struct tc_device *d) {
180 static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1;
182 if(unlikely(enable_new_interfaces == -1)) {
183 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
184 enable_bytes = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
185 enable_packets = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
186 enable_dropped = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
189 // we only need to add leaf classes
190 struct tc_class *c, *x;
191 unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0;
192 int active_classes = 0;
195 for(c = d->classes ; c ; c = c->next) {
200 // mark the classes as leafs and parents
201 for(c = d->classes ; c ; c = c->next) {
202 if(unlikely(!c->updated)) continue;
204 for(x = d->classes ; x ; x = x->next) {
205 if(unlikely(!x->updated)) continue;
207 if(unlikely(c == x)) continue;
210 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
211 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
212 // 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);
221 for ( c = d->classes ; c ; c = c->next) {
222 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
223 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);
227 // we need at least a class
228 for(c = d->classes ; c ; c = c->next) {
229 // 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);
230 if(!c->updated) continue;
231 if(c->isleaf && c->hasparent) {
233 bytes_sum += c->bytes;
234 packets_sum += c->packets;
235 dropped_sum += c->dropped;
239 if(unlikely(!active_classes)) {
240 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
241 tc_device_classes_cleanup(d);
245 if(unlikely(d->enabled == -1)) {
246 char var_name[CONFIG_MAX_NAME + 1];
247 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
248 d->enabled = config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
250 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
251 d->enabled_bytes = config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
253 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
254 d->enabled_packets = config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
256 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
257 d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
260 if(likely(d->enabled)) {
261 // --------------------------------------------------------------------
264 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
265 d->enabled_bytes = CONFIG_ONDEMAND_YES;
267 if(unlikely(!d->st_bytes)) {
268 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
269 if(unlikely(!d->st_bytes)) {
270 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
271 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);
275 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
276 rrdset_next_plugins(d->st_bytes);
278 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
279 rrdset_set_name(d->st_bytes, d->name);
287 for(c = d->classes ; c ; c = c->next) {
288 if(unlikely(!c->updated)) continue;
290 if(c->isleaf && c->hasparent) {
293 if(unlikely(!c->rd_bytes)) {
294 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
295 if(unlikely(!c->rd_bytes)) {
296 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
298 // new class, we have to add it
299 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
301 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
304 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
306 // if it has a name, different to the id
307 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
308 // update the rrd dimension with the new name
309 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
310 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
314 rrdset_done(d->st_bytes);
317 // --------------------------------------------------------------------
320 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
321 d->enabled_packets = CONFIG_ONDEMAND_YES;
323 if(unlikely(!d->st_packets)) {
324 char id[RRD_ID_LENGTH_MAX + 1];
325 char name[RRD_ID_LENGTH_MAX + 1];
326 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
327 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
329 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
330 if(unlikely(!d->st_packets)) {
331 debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
332 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);
336 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
337 rrdset_next_plugins(d->st_packets);
343 for(c = d->classes ; c ; c = c->next) {
344 if(unlikely(!c->updated)) continue;
346 if(c->isleaf && c->hasparent) {
347 if(unlikely(!c->rd_packets)) {
348 c->rd_packets = rrddim_find(d->st_packets, c->id);
349 if(unlikely(!c->rd_packets)) {
350 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
352 // new class, we have to add it
353 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
355 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
358 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
360 // if it has a name, different to the id
361 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
362 // update the rrd dimension with the new name
363 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
364 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
368 rrdset_done(d->st_packets);
371 // --------------------------------------------------------------------
374 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
375 d->enabled_dropped = CONFIG_ONDEMAND_YES;
377 if(unlikely(!d->st_dropped)) {
378 char id[RRD_ID_LENGTH_MAX + 1];
379 char name[RRD_ID_LENGTH_MAX + 1];
380 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
381 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
383 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
384 if(unlikely(!d->st_dropped)) {
385 debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
386 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);
390 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
391 rrdset_next_plugins(d->st_dropped);
397 for(c = d->classes ; c ; c = c->next) {
398 if(unlikely(!c->updated)) continue;
400 if(c->isleaf && c->hasparent) {
401 if(unlikely(!c->rd_dropped)) {
402 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
403 if(unlikely(!c->rd_dropped)) {
404 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
406 // new class, we have to add it
407 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
409 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
412 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
414 // if it has a name, different to the id
415 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
416 // update the rrd dimension with the new name
417 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
418 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
422 rrdset_done(d->st_dropped);
426 tc_device_classes_cleanup(d);
429 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
431 struct tc_class *c = tc_class_index_find(d, id, 0);
436 if(likely(name && *name && strcmp(c->id, name) != 0)) {
437 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
438 c->name = strdupz(name);
444 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
448 if(likely(name && *name && strcmp(d->id, name) != 0)) {
449 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
450 d->name = strdupz(name);
455 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
459 if(likely(family && *family && strcmp(d->id, family) != 0)) {
460 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
461 d->family = strdupz(family);
462 d->family_updated = 1;
464 // no need for null termination - it is already null
467 static inline struct tc_device *tc_device_create(char *id)
469 struct tc_device *d = tc_device_index_find(id, 0);
472 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
474 d = callocz(1, sizeof(struct tc_device));
477 d->hash = simple_hash(d->id);
480 avl_init(&d->classes_index, tc_class_compare);
481 tc_device_index_add(d);
483 if(!tc_device_root) {
487 d->next = tc_device_root;
488 tc_device_root->prev = d;
496 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
498 struct tc_class *c = tc_class_index_find(n, id, 0);
501 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
503 c = callocz(1, sizeof(struct tc_class));
505 if(n->classes) n->classes->prev = c;
506 c->next = n->classes;
510 c->hash = simple_hash(c->id);
512 if(parentid && *parentid) {
513 c->parentid = strdupz(parentid);
514 c->parent_hash = simple_hash(c->parentid);
517 if(leafid && *leafid) {
518 c->leafid = strdupz(leafid);
519 c->leaf_hash = simple_hash(c->leafid);
522 tc_class_index_add(n, c);
530 static inline void tc_device_free(struct tc_device *n)
532 if(n->next) n->next->prev = n->prev;
533 if(n->prev) n->prev->next = n->next;
534 if(tc_device_root == n) {
535 if(n->next) tc_device_root = n->next;
536 else tc_device_root = n->prev;
539 tc_device_index_del(n);
541 while(n->classes) tc_class_free(n, n->classes);
549 static inline void tc_device_free_all()
551 while(tc_device_root)
552 tc_device_free(tc_device_root);
557 static inline int tc_space(char c) {
570 static inline void tc_split_words(char *str, char **words, int max_words) {
574 // skip all white space
575 while(tc_space(*s)) s++;
577 // store the first word
580 // while we have something
583 if(unlikely(tc_space(*s))) {
585 // terminate the word
588 // skip all white space
589 while(tc_space(*s)) s++;
591 // if we reached the end, stop
594 // store the next word
595 if(i < max_words) words[i++] = s;
601 // terminate the words
602 while(i < max_words) words[i++] = NULL;
605 pid_t tc_child_pid = 0;
606 void *tc_main(void *ptr) {
609 info("TC thread created with task id %d", gettid());
611 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
612 error("Cannot set pthread cancel type to DEFERRED.");
614 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
615 error("Cannot set pthread cancel state to ENABLE.");
617 struct rusage thread;
618 RRDSET *stcpu = NULL, *sttime = NULL;
620 char buffer[TC_LINE_MAX+1] = "";
621 char *words[MAX_WORDS] = { NULL };
623 uint32_t BEGIN_HASH = simple_hash("BEGIN");
624 uint32_t END_HASH = simple_hash("END");
625 uint32_t CLASS_HASH = simple_hash("class");
626 uint32_t SENT_HASH = simple_hash("Sent");
627 uint32_t LENDED_HASH = simple_hash("lended:");
628 uint32_t TOKENS_HASH = simple_hash("tokens:");
629 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
630 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
631 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
632 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
633 #ifdef DETACH_PLUGINS_FROM_NETDATA
634 uint32_t MYPID_HASH = simple_hash("MYPID");
638 snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
639 char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
642 if(unlikely(netdata_exit)) break;
645 struct tc_device *device = NULL;
646 struct tc_class *class = NULL;
648 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
649 debug(D_TC_LOOP, "executing '%s'", buffer);
651 fp = mypopen(buffer, &tc_child_pid);
653 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
658 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
659 if(unlikely(netdata_exit)) break;
661 buffer[TC_LINE_MAX] = '\0';
662 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
664 tc_split_words(buffer, words, MAX_WORDS);
666 if(unlikely(!words[0] || !*words[0])) {
667 // debug(D_TC_LOOP, "empty line");
670 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
672 first_hash = simple_hash(words[0]);
674 if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
675 // 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]);
677 // words[1] : class type
679 // words[3] : parent or root
680 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
681 //char *type = words[1]; // the class: htb, fq_codel, etc
683 // we are only interested for HTB classes
684 //if(strcmp(type, "htb") != 0) continue;
686 char *id = words[2]; // the class major:minor
687 char *parent = words[3]; // 'parent' or 'root'
688 char *parentid = words[4]; // the parent's id
689 char *leaf = words[5]; // 'leaf'
690 char *leafid = words[6]; // leafid
692 if(strcmp(parent, "root") == 0) {
696 else if(!leaf || strcmp(leaf, "leaf") != 0)
699 char leafbuf[20 + 1] = "";
700 if(leafid && leafid[strlen(leafid) - 1] == ':') {
701 strncpyz(leafbuf, leafid, 20 - 1);
702 strcat(leafbuf, "1");
706 class = tc_class_add(device, id, parentid, leafid);
709 // clear the last class
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);
723 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
724 error("Cannot set pthread cancel state to ENABLE.");
730 else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
731 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
733 if(likely(words[1] && *words[1])) {
734 device = tc_device_create(words[1]);
737 // tc_device_free(device);
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(likely(words[1] && *words[1])) {
746 class->bytes = strtoull(words[1], NULL, 10);
753 if(likely(words[3] && *words[3]))
754 class->packets = strtoull(words[3], NULL, 10);
756 if(likely(words[6] && *words[6]))
757 class->dropped = strtoull(words[6], NULL, 10);
759 if(likely(words[8] && *words[8]))
760 class->overlimits = strtoull(words[8], NULL, 10);
762 if(likely(words[10] && *words[10]))
763 class->requeues = strtoull(words[8], NULL, 10);
765 else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
766 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
767 if(likely(words[1] && *words[1]))
768 class->lended = strtoull(words[1], NULL, 10);
770 if(likely(words[3] && *words[3]))
771 class->borrowed = strtoull(words[3], NULL, 10);
773 if(likely(words[5] && *words[5]))
774 class->giants = strtoull(words[5], NULL, 10);
776 else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
777 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
778 if(likely(words[1] && *words[1]))
779 class->tokens = strtoull(words[1], NULL, 10);
781 if(likely(words[3] && *words[3]))
782 class->ctokens = strtoull(words[3], NULL, 10);
784 else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
785 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
786 if(likely(words[1] && *words[1]))
787 tc_device_set_device_name(device, words[1]);
789 else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
790 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
791 if(likely(words[1] && *words[1]))
792 tc_device_set_device_family(device, words[1]);
794 else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
795 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
797 char *path = words[2];
798 if(likely(id && *id && path && *path))
799 tc_device_set_class_name(device, id, path);
801 else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
802 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
803 getrusage(RUSAGE_THREAD, &thread);
805 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
806 if(unlikely(!stcpu)) {
807 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
808 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
809 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
811 else rrdset_next(stcpu);
813 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
814 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
817 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
818 if(unlikely(!sttime)) {
819 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
820 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
822 else rrdset_next(sttime);
824 rrddim_set(sttime, "run_time", atoll(words[1]));
828 #ifdef DETACH_PLUGINS_FROM_NETDATA
829 else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
830 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
832 pid_t pid = atol(id);
834 if(likely(pid)) tc_child_pid = pid;
836 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
840 // debug(D_TC_LOOP, "IGNORED line");
844 // fgets() failed or loop broke
845 int code = mypclose(fp, tc_child_pid);
848 if(unlikely(device)) {
849 // tc_device_free(device);
854 if(unlikely(netdata_exit)) {
855 tc_device_free_all();
860 if(code == 1 || code == 127) {
862 // 127 = cannot even run it
863 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
865 tc_device_free_all();
870 sleep((unsigned int) rrd_update_every);