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;
56 char updated; // updated bytes
57 char seen; // seen in the tc list (even without bytes)
59 struct tc_class *next;
60 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_iterator(avl *a) { if(a) {}; return 0; }
88 static int tc_device_compare(void* a, void* b) {
89 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
90 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
91 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
94 avl_tree tc_device_root_index = {
99 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
100 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
102 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
103 struct tc_device *result = NULL, tmp;
105 tmp.hash = (hash)?hash:simple_hash(tmp.id);
107 avl_search(&(tc_device_root_index), (avl *) &tmp, tc_device_iterator, (avl **) &result);
112 // ----------------------------------------------------------------------------
115 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
117 static int tc_class_compare(void* a, void* b) {
118 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
119 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
120 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
123 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
124 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
126 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
127 struct tc_class *result = NULL, tmp;
129 tmp.hash = (hash)?hash:simple_hash(tmp.id);
131 avl_search(&(st->classes_index), (avl *) &tmp, tc_class_iterator, (avl **) &result);
135 // ----------------------------------------------------------------------------
137 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
138 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);
140 if(c->next) c->next->prev = c->prev;
141 if(c->prev) c->prev->next = c->next;
142 if(n->classes == c) {
143 if(c->next) n->classes = c->next;
144 else n->classes = c->prev;
147 tc_class_index_del(n, c);
149 if(c->id) free(c->id);
150 if(c->name) free(c->name);
151 if(c->leafid) free(c->leafid);
152 if(c->parentid) free(c->parentid);
157 static inline void tc_device_classes_cleanup(struct tc_device *d) {
158 static int cleanup_every = 999;
160 if(cleanup_every > 0) {
161 cleanup_every = (int) -config_get_number("plugin:tc", "cleanup unused classes every", 60);
162 if(cleanup_every > 0) cleanup_every = -cleanup_every;
163 if(cleanup_every == 0) cleanup_every = -1;
166 struct tc_class *c = d->classes;
168 if(c->seen < cleanup_every) {
169 struct tc_class *nc = c->next;
182 static inline void tc_device_commit(struct tc_device *d)
184 static int enable_new_interfaces = -1;
186 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
188 // we only need to add leaf classes
189 struct tc_class *c, *x;
192 for(c = d->classes ; c ; c = c->next) {
197 // mark the classes as leafs and parents
198 for(c = d->classes ; c ; c = c->next) {
199 if(!c->updated) continue;
201 for(x = d->classes ; x ; x = x->next) {
202 if(!x->updated) continue;
207 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
208 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
209 // 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);
218 for ( c = d->classes ; c ; c = c->next) {
219 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
220 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);
224 // we need at least a class
225 for(c = d->classes ; c ; c = c->next) {
226 // 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);
227 if(!c->updated) continue;
228 if(c->isleaf && c->hasparent) break;
231 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
232 tc_device_classes_cleanup(d);
236 char var_name[CONFIG_MAX_NAME + 1];
237 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
238 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
239 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
241 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
243 st = 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);
245 for(c = d->classes ; c ; c = c->next) {
246 if(!c->updated) continue;
248 if(c->isleaf && c->hasparent)
249 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
253 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
254 rrdset_next_plugins(st);
256 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
259 for(c = d->classes ; c ; c = c->next) {
260 if(!c->updated) continue;
262 if(c->isleaf && c->hasparent) {
263 RRDDIM *rd = rrddim_find(st, c->id);
266 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
268 // new class, we have to add it
269 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
271 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
273 rrddim_set_by_pointer(st, rd, c->bytes);
275 // if it has a name, different to the id
277 // update the rrd dimension with the new name
278 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
279 rrddim_set_name(st, rd, c->name);
289 tc_device_classes_cleanup(d);
292 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
294 struct tc_class *c = tc_class_index_find(d, id, 0);
296 if(c->name) free(c->name);
299 if(name && *name && strcmp(c->id, name) != 0) {
300 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
301 c->name = strdup(name);
306 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
307 if(d->name) free(d->name);
310 if(name && *name && strcmp(d->id, name) != 0) {
311 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
312 d->name = strdup(name);
316 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
317 if(d->family) free(d->family);
320 if(family && *family && strcmp(d->id, family) != 0) {
321 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
322 d->family = strdup(family);
324 // no need for null termination - it is already null
327 static inline struct tc_device *tc_device_create(char *id)
329 struct tc_device *d = tc_device_index_find(id, 0);
332 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
334 d = calloc(1, sizeof(struct tc_device));
336 fatal("Cannot allocate memory for tc_device %s", id);
341 d->hash = simple_hash(d->id);
343 avl_init(&d->classes_index, tc_class_compare);
344 tc_device_index_add(d);
346 if(!tc_device_root) {
350 d->next = tc_device_root;
351 tc_device_root->prev = d;
359 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
361 struct tc_class *c = tc_class_index_find(n, id, 0);
364 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
366 c = calloc(1, sizeof(struct tc_class));
368 fatal("Cannot allocate memory for tc class");
372 if(n->classes) n->classes->prev = c;
373 c->next = n->classes;
381 c->hash = simple_hash(c->id);
383 if(parentid && *parentid) {
384 c->parentid = strdup(parentid);
385 c->parent_hash = simple_hash(c->parentid);
388 if(leafid && *leafid) {
389 c->leafid = strdup(leafid);
390 c->leaf_hash = simple_hash(c->leafid);
393 tc_class_index_add(n, c);
401 static inline void tc_device_free(struct tc_device *n)
403 if(n->next) n->next->prev = n->prev;
404 if(n->prev) n->prev->next = n->next;
405 if(tc_device_root == n) {
406 if(n->next) tc_device_root = n->next;
407 else tc_device_root = n->prev;
410 tc_device_index_del(n);
412 while(n->classes) tc_class_free(n, n->classes);
414 if(n->id) free(n->id);
415 if(n->name) free(n->name);
416 if(n->family) free(n->family);
421 static inline void tc_device_free_all()
423 while(tc_device_root)
424 tc_device_free(tc_device_root);
429 static inline int tc_space(char c) {
442 static inline void tc_split_words(char *str, char **words, int max_words) {
446 // skip all white space
447 while(tc_space(*s)) s++;
449 // store the first word
452 // while we have something
457 // terminate the word
460 // skip all white space
461 while(tc_space(*s)) s++;
463 // if we reached the end, stop
466 // store the next word
467 if(i < max_words) words[i++] = s;
473 // terminate the words
474 while(i < max_words) words[i++] = NULL;
477 pid_t tc_child_pid = 0;
478 void *tc_main(void *ptr)
482 info("TC thread created with task id %d", gettid());
484 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
485 error("Cannot set pthread cancel type to DEFERRED.");
487 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
488 error("Cannot set pthread cancel state to ENABLE.");
490 struct rusage thread;
491 RRDSET *stcpu = NULL, *sttime = NULL;
493 char buffer[TC_LINE_MAX+1] = "";
494 char *words[MAX_WORDS] = { NULL };
496 uint32_t BEGIN_HASH = simple_hash("BEGIN");
497 uint32_t END_HASH = simple_hash("END");
498 uint32_t CLASS_HASH = simple_hash("class");
499 uint32_t SENT_HASH = simple_hash("Sent");
500 uint32_t LENDED_HASH = simple_hash("lended:");
501 uint32_t TOKENS_HASH = simple_hash("tokens:");
502 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
503 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
504 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
505 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
506 #ifdef DETACH_PLUGINS_FROM_NETDATA
507 uint32_t MYPID_HASH = simple_hash("MYPID");
512 if(unlikely(netdata_exit)) break;
515 struct tc_device *device = NULL;
516 struct tc_class *class = NULL;
518 snprintf(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);
519 debug(D_TC_LOOP, "executing '%s'", buffer);
520 // fp = popen(buffer, "r");
521 fp = mypopen(buffer, &tc_child_pid);
523 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
528 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
529 if(unlikely(netdata_exit)) break;
531 buffer[TC_LINE_MAX] = '\0';
532 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
534 tc_split_words(buffer, words, MAX_WORDS);
535 if(!words[0] || !*words[0]) {
536 // debug(D_TC_LOOP, "empty line");
539 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
541 first_hash = simple_hash(words[0]);
543 if(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) {
544 // 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]);
546 // clear the last class
549 // words[1] : class type
551 // words[3] : parent or root
552 if(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
553 //char *type = words[1]; // the class: htb, fq_codel, etc
555 // we are only interested for HTB classes
556 //if(strcmp(type, "htb") != 0) continue;
558 char *id = words[2]; // the class major:minor
559 char *parent = words[3]; // 'parent' or 'root'
560 char *parentid = words[4]; // the parent's id
561 char *leaf = words[5]; // 'leaf'
562 char *leafid = words[6]; // leafid
564 if(strcmp(parent, "root") == 0) {
568 else if(!leaf || strcmp(leaf, "leaf") != 0)
571 char leafbuf[20 + 1] = "";
572 if(leafid && leafid[strlen(leafid) - 1] == ':') {
573 strncpy(leafbuf, leafid, 20 - 1);
574 strcat(leafbuf, "1");
578 class = tc_class_add(device, id, parentid, leafid);
581 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
582 // debug(D_TC_LOOP, "END line");
585 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
586 error("Cannot set pthread cancel state to DISABLE.");
588 tc_device_commit(device);
589 // tc_device_free(device);
593 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
594 error("Cannot set pthread cancel state to ENABLE.");
597 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
598 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
601 // tc_device_free(device);
606 if(words[1] && *words[1]) {
607 device = tc_device_create(words[1]);
611 else if(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0) {
612 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
613 if(words[1] && *words[1]) {
614 class->bytes = strtoull(words[1], NULL, 10);
618 if(words[3] && *words[3])
619 class->packets = strtoull(words[3], NULL, 10);
621 if(words[6] && *words[6])
622 class->dropped = strtoull(words[6], NULL, 10);
624 if(words[8] && *words[8])
625 class->overlimits = strtoull(words[8], NULL, 10);
627 if(words[10] && *words[10])
628 class->requeues = strtoull(words[8], NULL, 10);
630 else if(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0) {
631 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
632 if(words[1] && *words[1])
633 class->lended = strtoull(words[1], NULL, 10);
635 if(words[3] && *words[3])
636 class->borrowed = strtoull(words[3], NULL, 10);
638 if(words[5] && *words[5])
639 class->giants = strtoull(words[5], NULL, 10);
641 else if(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0) {
642 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
643 if(words[1] && *words[1])
644 class->tokens = strtoull(words[1], NULL, 10);
646 if(words[3] && *words[3])
647 class->ctokens = strtoull(words[3], NULL, 10);
649 else if(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0) {
650 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
651 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
653 else if(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0) {
654 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
655 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
657 else if(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0) {
658 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
660 char *path = words[2];
661 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
663 else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
664 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
665 getrusage(RUSAGE_THREAD, &thread);
667 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
669 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
670 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
671 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
673 else rrdset_next(stcpu);
675 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
676 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
679 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
681 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
682 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
684 else rrdset_next(sttime);
686 rrddim_set(sttime, "run_time", atoll(words[1]));
690 #ifdef DETACH_PLUGINS_FROM_NETDATA
691 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
692 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
694 pid_t pid = atol(id);
696 if(pid) tc_child_pid = pid;
698 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
702 // debug(D_TC_LOOP, "IGNORED line");
705 // fgets() failed or loop broke
706 int code = mypclose(fp, tc_child_pid);
710 // tc_device_free(device);
716 tc_device_free_all();
721 if(code == 1 || code == 127) {
723 // 127 = cannot even run it
724 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
726 tc_device_free_all();
731 sleep((unsigned int) rrd_update_every);