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 PTHREAD_RWLOCK_INITIALIZER
90 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
91 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
93 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
94 struct tc_device *result = NULL, tmp;
96 tmp.hash = (hash)?hash:simple_hash(tmp.id);
98 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
103 // ----------------------------------------------------------------------------
106 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
108 static int tc_class_compare(void* a, void* b) {
109 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
110 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
111 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
114 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
115 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
117 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
118 struct tc_class *result = NULL, tmp;
120 tmp.hash = (hash)?hash:simple_hash(tmp.id);
122 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
126 // ----------------------------------------------------------------------------
128 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
129 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);
131 if(c->next) c->next->prev = c->prev;
132 if(c->prev) c->prev->next = c->next;
133 if(n->classes == c) {
134 if(c->next) n->classes = c->next;
135 else n->classes = c->prev;
138 tc_class_index_del(n, c);
140 if(c->id) free(c->id);
141 if(c->name) free(c->name);
142 if(c->leafid) free(c->leafid);
143 if(c->parentid) free(c->parentid);
148 static void tc_device_classes_cleanup(struct tc_device *d) {
149 static int cleanup_every = 999;
151 if(cleanup_every > 0) {
152 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
153 if(cleanup_every > 0) cleanup_every = -cleanup_every;
154 if(cleanup_every == 0) cleanup_every = -1;
157 struct tc_class *c = d->classes;
159 if(c->seen < cleanup_every) {
160 struct tc_class *nc = c->next;
173 static void tc_device_commit(struct tc_device *d)
175 static int enable_new_interfaces = -1;
177 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
179 // we only need to add leaf classes
180 struct tc_class *c, *x;
183 for(c = d->classes ; c ; c = c->next) {
188 // mark the classes as leafs and parents
189 for(c = d->classes ; c ; c = c->next) {
190 if(!c->updated) continue;
192 for(x = d->classes ; x ; x = x->next) {
193 if(!x->updated) continue;
198 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
199 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
200 // 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);
209 for ( c = d->classes ; c ; c = c->next) {
210 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
211 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);
215 // we need at least a class
216 for(c = d->classes ; c ; c = c->next) {
217 // 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);
218 if(!c->updated) continue;
219 if(c->isleaf && c->hasparent) break;
222 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
223 tc_device_classes_cleanup(d);
227 char var_name[CONFIG_MAX_NAME + 1];
228 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
229 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
230 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
232 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
234 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);
236 for(c = d->classes ; c ; c = c->next) {
237 if(!c->updated) continue;
239 if(c->isleaf && c->hasparent)
240 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
244 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
245 rrdset_next_plugins(st);
247 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
250 for(c = d->classes ; c ; c = c->next) {
251 if(!c->updated) continue;
253 if(c->isleaf && c->hasparent) {
254 RRDDIM *rd = rrddim_find(st, c->id);
257 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
259 // new class, we have to add it
260 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
262 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
264 rrddim_set_by_pointer(st, rd, c->bytes);
266 // if it has a name, different to the id
268 // update the rrd dimension with the new name
269 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
270 rrddim_set_name(st, rd, c->name);
280 tc_device_classes_cleanup(d);
283 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
285 struct tc_class *c = tc_class_index_find(d, id, 0);
287 if(c->name) free(c->name);
290 if(name && *name && strcmp(c->id, name) != 0) {
291 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
292 c->name = strdup(name);
297 static void tc_device_set_device_name(struct tc_device *d, char *name) {
298 if(d->name) free(d->name);
301 if(name && *name && strcmp(d->id, name) != 0) {
302 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
303 d->name = strdup(name);
307 static void tc_device_set_device_family(struct tc_device *d, char *family) {
308 if(d->family) free(d->family);
311 if(family && *family && strcmp(d->id, family) != 0) {
312 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
313 d->family = strdup(family);
315 // no need for null termination - it is already null
318 static struct tc_device *tc_device_create(char *id)
320 struct tc_device *d = tc_device_index_find(id, 0);
323 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
325 d = calloc(1, sizeof(struct tc_device));
327 fatal("Cannot allocate memory for tc_device %s", id);
332 d->hash = simple_hash(d->id);
334 d->classes_index.root = NULL;
335 d->classes_index.compar = tc_class_compare;
336 pthread_rwlock_init(&d->classes_index.rwlock, NULL);
338 tc_device_index_add(d);
340 if(!tc_device_root) {
344 d->next = tc_device_root;
345 tc_device_root->prev = d;
353 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
355 struct tc_class *c = tc_class_index_find(n, id, 0);
358 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
360 c = calloc(1, sizeof(struct tc_class));
362 fatal("Cannot allocate memory for tc class");
366 if(n->classes) n->classes->prev = c;
367 c->next = n->classes;
375 c->hash = simple_hash(c->id);
377 if(parentid && *parentid) {
378 c->parentid = strdup(parentid);
379 c->parent_hash = simple_hash(c->parentid);
382 if(leafid && *leafid) {
383 c->leafid = strdup(leafid);
384 c->leaf_hash = simple_hash(c->leafid);
387 tc_class_index_add(n, c);
395 static void tc_device_free(struct tc_device *n)
397 if(n->next) n->next->prev = n->prev;
398 if(n->prev) n->prev->next = n->next;
399 if(tc_device_root == n) {
400 if(n->next) tc_device_root = n->next;
401 else tc_device_root = n->prev;
404 tc_device_index_del(n);
406 while(n->classes) tc_class_free(n, n->classes);
408 if(n->id) free(n->id);
409 if(n->name) free(n->name);
410 if(n->family) free(n->family);
415 static void tc_device_free_all()
417 while(tc_device_root)
418 tc_device_free(tc_device_root);
423 static inline int tc_space(char c) {
436 static void tc_split_words(char *str, char **words, int max_words) {
440 // skip all white space
441 while(tc_space(*s)) s++;
443 // store the first word
446 // while we have something
451 // terminate the word
454 // skip all white space
455 while(tc_space(*s)) s++;
457 // if we reached the end, stop
460 // store the next word
461 if(i < max_words) words[i++] = s;
467 // terminate the words
468 while(i < max_words) words[i++] = NULL;
471 pid_t tc_child_pid = 0;
472 void *tc_main(void *ptr)
476 info("TC thread created with task id %d", gettid());
478 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
479 error("Cannot set pthread cancel type to DEFERRED.");
481 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
482 error("Cannot set pthread cancel state to ENABLE.");
484 struct rusage thread;
485 RRDSET *stcpu = NULL, *sttime = NULL;
487 char buffer[TC_LINE_MAX+1] = "";
488 char *words[MAX_WORDS] = { NULL };
490 uint32_t BEGIN_HASH = simple_hash("BEGIN");
491 uint32_t END_HASH = simple_hash("END");
492 uint32_t CLASS_HASH = simple_hash("class");
493 uint32_t SENT_HASH = simple_hash("Sent");
494 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
495 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
496 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
497 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
498 #ifdef DETACH_PLUGINS_FROM_NETDATA
499 uint32_t MYPID_HASH = simple_hash("MYPID");
505 struct tc_device *device = NULL;
506 struct tc_class *class = NULL;
508 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);
509 debug(D_TC_LOOP, "executing '%s'", buffer);
510 // fp = popen(buffer, "r");
511 fp = mypopen(buffer, &tc_child_pid);
513 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
517 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
518 if(netdata_exit) break;
520 buffer[TC_LINE_MAX] = '\0';
521 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
523 tc_split_words(buffer, words, MAX_WORDS);
524 if(!words[0] || !*words[0]) {
525 // debug(D_TC_LOOP, "empty line");
528 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
530 first_hash = simple_hash(words[0]);
532 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
533 // 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]);
535 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
536 // char *type = words[1]; // the class: htb, fq_codel, etc
537 char *id = words[2]; // the class major:minor
538 char *parent = words[3]; // 'parent' or 'root'
539 char *parentid = words[4]; // the parent's id
540 char *leaf = words[5]; // 'leaf'
541 char *leafid = words[6]; // leafid
543 if(strcmp(parent, "root") == 0) {
547 else if(!leaf || strcmp(leaf, "leaf") != 0)
550 char leafbuf[20 + 1] = "";
551 if(leafid && leafid[strlen(leafid) - 1] == ':') {
552 strncpy(leafbuf, leafid, 20 - 1);
553 strcat(leafbuf, "1");
557 class = tc_class_add(device, id, parentid, leafid);
560 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
561 // debug(D_TC_LOOP, "END line");
564 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
565 error("Cannot set pthread cancel state to DISABLE.");
567 tc_device_commit(device);
568 // tc_device_free(device);
572 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
573 error("Cannot set pthread cancel state to ENABLE.");
576 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
577 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
580 // tc_device_free(device);
585 if(words[1] && *words[1]) {
586 device = tc_device_create(words[1]);
590 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
591 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
592 if(words[1] && *words[1]) {
593 class->bytes = atoll(words[1]);
596 else class->bytes = 0;
598 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
599 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
600 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
602 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
603 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
604 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
606 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
607 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
609 char *path = words[2];
610 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
612 else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
613 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
614 getrusage(RUSAGE_THREAD, &thread);
616 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
618 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
619 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
620 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
622 else rrdset_next(stcpu);
624 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
625 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
628 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
630 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
631 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
633 else rrdset_next(sttime);
635 rrddim_set(sttime, "run_time", atoll(words[1]));
639 #ifdef DETACH_PLUGINS_FROM_NETDATA
640 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
641 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
643 pid_t pid = atol(id);
645 if(pid) tc_child_pid = pid;
647 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
651 // debug(D_TC_LOOP, "IGNORED line");
654 mypclose(fp, tc_child_pid);
658 // tc_device_free(device);
664 tc_device_free_all();
668 sleep(rrd_update_every);