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;
46 char updated; // updated bytes
47 char seen; // seen in the tc list (even without bytes)
49 struct tc_class *next;
50 struct tc_class *prev;
62 avl_tree classes_index;
64 struct tc_class *classes;
66 struct tc_device *next;
67 struct tc_device *prev;
71 struct tc_device *tc_device_root = NULL;
73 // ----------------------------------------------------------------------------
76 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
78 static int tc_device_compare(void* a, void* b) {
79 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
80 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
81 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
84 avl_tree tc_device_root_index = {
87 #ifdef AVL_LOCK_WITH_MUTEX
88 PTHREAD_MUTEX_INITIALIZER
90 PTHREAD_RWLOCK_INITIALIZER
94 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
95 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
97 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
98 struct tc_device *result = NULL, tmp;
100 tmp.hash = (hash)?hash:simple_hash(tmp.id);
102 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
107 // ----------------------------------------------------------------------------
110 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
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 struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
122 struct tc_class *result = NULL, tmp;
124 tmp.hash = (hash)?hash:simple_hash(tmp.id);
126 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
130 // ----------------------------------------------------------------------------
132 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
133 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);
135 if(c->next) c->next->prev = c->prev;
136 if(c->prev) c->prev->next = c->next;
137 if(n->classes == c) {
138 if(c->next) n->classes = c->next;
139 else n->classes = c->prev;
142 tc_class_index_del(n, c);
144 if(c->id) free(c->id);
145 if(c->name) free(c->name);
146 if(c->leafid) free(c->leafid);
147 if(c->parentid) free(c->parentid);
152 static void tc_device_classes_cleanup(struct tc_device *d) {
153 static int cleanup_every = 999;
155 if(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;
158 if(cleanup_every == 0) cleanup_every = -1;
161 struct tc_class *c = d->classes;
163 if(c->seen < cleanup_every) {
164 struct tc_class *nc = c->next;
177 static void tc_device_commit(struct tc_device *d)
179 static int enable_new_interfaces = -1;
181 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
183 // we only need to add leaf classes
184 struct tc_class *c, *x;
187 for(c = d->classes ; c ; c = c->next) {
192 // mark the classes as leafs and parents
193 for(c = d->classes ; c ; c = c->next) {
194 if(!c->updated) continue;
196 for(x = d->classes ; x ; x = x->next) {
197 if(!x->updated) continue;
202 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
203 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
204 // 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);
213 for ( c = d->classes ; c ; c = c->next) {
214 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
215 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);
219 // we need at least a class
220 for(c = d->classes ; c ; c = c->next) {
221 // 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);
222 if(!c->updated) continue;
223 if(c->isleaf && c->hasparent) break;
226 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
227 tc_device_classes_cleanup(d);
231 char var_name[CONFIG_MAX_NAME + 1];
232 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
233 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
234 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
236 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
238 st = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, "Class Usage", "kilobits/s", 1000, rrd_update_every, RRDSET_TYPE_STACKED);
240 for(c = d->classes ; c ; c = c->next) {
241 if(!c->updated) continue;
243 if(c->isleaf && c->hasparent)
244 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
248 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
249 rrdset_next_plugins(st);
251 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
254 for(c = d->classes ; c ; c = c->next) {
255 if(!c->updated) continue;
257 if(c->isleaf && c->hasparent) {
258 RRDDIM *rd = rrddim_find(st, c->id);
261 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
263 // new class, we have to add it
264 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
266 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
268 rrddim_set_by_pointer(st, rd, c->bytes);
270 // if it has a name, different to the id
272 // update the rrd dimension with the new name
273 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
274 rrddim_set_name(st, rd, c->name);
284 tc_device_classes_cleanup(d);
287 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
289 struct tc_class *c = tc_class_index_find(d, id, 0);
291 if(c->name) free(c->name);
294 if(name && *name && strcmp(c->id, name) != 0) {
295 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
296 c->name = strdup(name);
301 static void tc_device_set_device_name(struct tc_device *d, char *name) {
302 if(d->name) free(d->name);
305 if(name && *name && strcmp(d->id, name) != 0) {
306 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
307 d->name = strdup(name);
311 static void tc_device_set_device_family(struct tc_device *d, char *family) {
312 if(d->family) free(d->family);
315 if(family && *family && strcmp(d->id, family) != 0) {
316 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
317 d->family = strdup(family);
319 // no need for null termination - it is already null
322 static struct tc_device *tc_device_create(char *id)
324 struct tc_device *d = tc_device_index_find(id, 0);
327 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
329 d = calloc(1, sizeof(struct tc_device));
331 fatal("Cannot allocate memory for tc_device %s", id);
336 d->hash = simple_hash(d->id);
338 d->classes_index.root = NULL;
339 d->classes_index.compar = tc_class_compare;
340 #ifdef AVL_LOCK_WITH_MUTEX
341 pthread_mutex_init(&d->classes_index.mutex, NULL);
343 pthread_rwlock_init(&d->classes_index.rwlock, NULL);
346 tc_device_index_add(d);
348 if(!tc_device_root) {
352 d->next = tc_device_root;
353 tc_device_root->prev = d;
361 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
363 struct tc_class *c = tc_class_index_find(n, id, 0);
366 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
368 c = calloc(1, sizeof(struct tc_class));
370 fatal("Cannot allocate memory for tc class");
374 if(n->classes) n->classes->prev = c;
375 c->next = n->classes;
383 c->hash = simple_hash(c->id);
385 if(parentid && *parentid) {
386 c->parentid = strdup(parentid);
387 c->parent_hash = simple_hash(c->parentid);
390 if(leafid && *leafid) {
391 c->leafid = strdup(leafid);
392 c->leaf_hash = simple_hash(c->leafid);
395 tc_class_index_add(n, c);
403 static void tc_device_free(struct tc_device *n)
405 if(n->next) n->next->prev = n->prev;
406 if(n->prev) n->prev->next = n->next;
407 if(tc_device_root == n) {
408 if(n->next) tc_device_root = n->next;
409 else tc_device_root = n->prev;
412 tc_device_index_del(n);
414 while(n->classes) tc_class_free(n, n->classes);
416 if(n->id) free(n->id);
417 if(n->name) free(n->name);
418 if(n->family) free(n->family);
423 static void tc_device_free_all()
425 while(tc_device_root)
426 tc_device_free(tc_device_root);
431 static inline int tc_space(char c) {
444 static void tc_split_words(char *str, char **words, int max_words) {
448 // skip all white space
449 while(tc_space(*s)) s++;
451 // store the first word
454 // while we have something
459 // terminate the word
462 // skip all white space
463 while(tc_space(*s)) s++;
465 // if we reached the end, stop
468 // store the next word
469 if(i < max_words) words[i++] = s;
475 // terminate the words
476 while(i < max_words) words[i++] = NULL;
479 pid_t tc_child_pid = 0;
480 void *tc_main(void *ptr)
484 info("TC thread created with task id %d", gettid());
486 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
487 error("Cannot set pthread cancel type to DEFERRED.");
489 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
490 error("Cannot set pthread cancel state to ENABLE.");
492 struct rusage thread;
493 RRDSET *stcpu = NULL, *sttime = NULL;
495 char buffer[TC_LINE_MAX+1] = "";
496 char *words[MAX_WORDS] = { NULL };
498 uint32_t BEGIN_HASH = simple_hash("BEGIN");
499 uint32_t END_HASH = simple_hash("END");
500 uint32_t CLASS_HASH = simple_hash("class");
501 uint32_t SENT_HASH = simple_hash("Sent");
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);
527 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
528 if(unlikely(netdata_exit)) break;
530 buffer[TC_LINE_MAX] = '\0';
531 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
533 tc_split_words(buffer, words, MAX_WORDS);
534 if(!words[0] || !*words[0]) {
535 // debug(D_TC_LOOP, "empty line");
538 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
540 first_hash = simple_hash(words[0]);
542 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
543 // 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]);
545 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
546 // char *type = words[1]; // the class: htb, fq_codel, etc
547 char *id = words[2]; // the class major:minor
548 char *parent = words[3]; // 'parent' or 'root'
549 char *parentid = words[4]; // the parent's id
550 char *leaf = words[5]; // 'leaf'
551 char *leafid = words[6]; // leafid
553 if(strcmp(parent, "root") == 0) {
557 else if(!leaf || strcmp(leaf, "leaf") != 0)
560 char leafbuf[20 + 1] = "";
561 if(leafid && leafid[strlen(leafid) - 1] == ':') {
562 strncpy(leafbuf, leafid, 20 - 1);
563 strcat(leafbuf, "1");
567 class = tc_class_add(device, id, parentid, leafid);
570 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
571 // debug(D_TC_LOOP, "END line");
574 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
575 error("Cannot set pthread cancel state to DISABLE.");
577 tc_device_commit(device);
578 // tc_device_free(device);
582 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
583 error("Cannot set pthread cancel state to ENABLE.");
586 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
587 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
590 // tc_device_free(device);
595 if(words[1] && *words[1]) {
596 device = tc_device_create(words[1]);
600 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
601 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
602 if(words[1] && *words[1]) {
603 class->bytes = strtoull(words[1], NULL, 10);
606 else class->bytes = 0;
608 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
609 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
610 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
612 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
613 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
614 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
616 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
617 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
619 char *path = words[2];
620 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
622 else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
623 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
624 getrusage(RUSAGE_THREAD, &thread);
626 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
628 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
629 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
630 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
632 else rrdset_next(stcpu);
634 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
635 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
638 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
640 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
641 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
643 else rrdset_next(sttime);
645 rrddim_set(sttime, "run_time", atoll(words[1]));
649 #ifdef DETACH_PLUGINS_FROM_NETDATA
650 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
651 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
653 pid_t pid = atol(id);
655 if(pid) tc_child_pid = pid;
657 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
661 // debug(D_TC_LOOP, "IGNORED line");
664 mypclose(fp, tc_child_pid);
668 // tc_device_free(device);
674 tc_device_free_all();
678 sleep((unsigned int) rrd_update_every);