8 #include <sys/resource.h>
13 #include "appconfig.h"
16 #include "plugin_tc.h"
18 #define RRD_TYPE_TC "tc"
19 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
21 // ----------------------------------------------------------------------------
23 // this requires the script plugins.d/tc-qos-helper.sh
25 #define TC_LINE_MAX 1024
43 unsigned long long bytes;
45 char updated; // updated bytes
46 char seen; // seen in the tc list (even without bytes)
48 struct tc_class *next;
49 struct tc_class *prev;
61 avl_tree classes_index;
63 struct tc_class *classes;
67 // ----------------------------------------------------------------------------
70 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
72 static int tc_device_compare(void* a, void* b) {
73 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
74 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
75 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
78 avl_tree tc_device_root_index = {
81 PTHREAD_RWLOCK_INITIALIZER
84 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
85 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
87 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
88 struct tc_device *result = NULL, tmp;
90 tmp.hash = (hash)?hash:simple_hash(tmp.id);
92 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
97 // ----------------------------------------------------------------------------
100 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
102 static int tc_class_compare(void* a, void* b) {
103 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
104 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
105 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
108 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
109 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
111 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
112 struct tc_class *result = NULL, tmp;
114 tmp.hash = (hash)?hash:simple_hash(tmp.id);
116 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
120 // ----------------------------------------------------------------------------
122 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
123 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);
125 if(c->next) c->next->prev = c->prev;
126 if(c->prev) c->prev->next = c->next;
127 if(n->classes == c) n->classes = c->next;
129 tc_class_index_del(n, c);
131 if(c->id) free(c->id);
132 if(c->name) free(c->name);
133 if(c->leafid) free(c->leafid);
134 if(c->parentid) free(c->parentid);
139 static void tc_device_classes_cleanup(struct tc_device *d) {
140 static int cleanup_every = 999;
142 if(cleanup_every > 0) {
143 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
144 if(cleanup_every > 0) cleanup_every = -cleanup_every;
145 if(cleanup_every == 0) cleanup_every = -1;
148 struct tc_class *c = d->classes;
150 if(c->seen < cleanup_every) {
151 struct tc_class *nc = c->next;
164 static void tc_device_commit(struct tc_device *d)
166 static int enable_new_interfaces = -1;
168 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
170 // we only need to add leaf classes
171 struct tc_class *c, *x;
174 for(c = d->classes ; c ; c = c->next) {
179 // mark the classes as leafs and parents
180 for(c = d->classes ; c ; c = c->next) {
181 if(!c->updated) continue;
183 for(x = d->classes ; x ; x = x->next) {
184 if(!x->updated) continue;
189 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
190 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
191 // 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);
200 for ( c = d->classes ; c ; c = c->next) {
201 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
202 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);
206 // we need at least a class
207 for(c = d->classes ; c ; c = c->next) {
208 // 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);
209 if(!c->updated) continue;
210 if(c->isleaf && c->hasparent) break;
213 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
214 tc_device_classes_cleanup(d);
218 char var_name[CONFIG_MAX_NAME + 1];
219 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
220 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
221 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
223 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
225 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);
227 for(c = d->classes ; c ; c = c->next) {
228 if(!c->updated) continue;
230 if(c->isleaf && c->hasparent)
231 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
235 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
236 rrdset_next_plugins(st);
238 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
241 for(c = d->classes ; c ; c = c->next) {
242 if(!c->updated) continue;
244 if(c->isleaf && c->hasparent) {
245 RRDDIM *rd = rrddim_find(st, c->id);
248 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
250 // new class, we have to add it
251 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
253 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
255 rrddim_set_by_pointer(st, rd, c->bytes);
257 // if it has a name, different to the id
259 // update the rrd dimension with the new name
260 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
261 rrddim_set_name(st, rd, c->name);
271 tc_device_classes_cleanup(d);
274 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
276 struct tc_class *c = tc_class_index_find(d, id, 0);
278 if(c->name) free(c->name);
281 if(name && *name && strcmp(c->id, name) != 0) {
282 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
283 c->name = strdup(name);
288 static void tc_device_set_device_name(struct tc_device *d, char *name) {
289 if(d->name) free(d->name);
292 if(name && *name && strcmp(d->id, name) != 0) {
293 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
294 d->name = strdup(name);
298 static void tc_device_set_device_family(struct tc_device *d, char *family) {
299 if(d->family) free(d->family);
302 if(family && *family && strcmp(d->id, family) != 0) {
303 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
304 d->family = strdup(family);
306 // no need for null termination - it is already null
309 static struct tc_device *tc_device_create(char *id)
311 struct tc_device *d = tc_device_index_find(id, 0);
314 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
316 d = calloc(1, sizeof(struct tc_device));
318 fatal("Cannot allocate memory for tc_device %s", id);
323 d->hash = simple_hash(d->id);
325 d->classes_index.root = NULL;
326 d->classes_index.compar = tc_class_compare;
327 pthread_rwlock_init(&d->classes_index.rwlock, NULL);
329 tc_device_index_add(d);
335 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
337 struct tc_class *c = tc_class_index_find(n, id, 0);
340 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
342 c = calloc(1, sizeof(struct tc_class));
344 fatal("Cannot allocate memory for tc class");
348 if(n->classes) n->classes->prev = c;
349 c->next = n->classes;
357 c->hash = simple_hash(c->id);
359 if(parentid && *parentid) {
360 c->parentid = strdup(parentid);
361 c->parent_hash = simple_hash(c->parentid);
364 if(leafid && *leafid) {
365 c->leafid = strdup(leafid);
366 c->leaf_hash = simple_hash(c->leafid);
369 tc_class_index_add(n, c);
377 static void tc_device_free(struct tc_device *n)
379 tc_device_index_del(n);
381 while(n->classes) tc_class_free(n, n->classes);
383 if(n->id) free(n->id);
384 if(n->name) free(n->name);
385 if(n->family) free(n->family);
393 static inline int tc_space(char c) {
406 static void tc_split_words(char *str, char **words, int max_words) {
410 // skip all white space
411 while(tc_space(*s)) s++;
413 // store the first word
416 // while we have something
421 // terminate the word
424 // skip all white space
425 while(tc_space(*s)) s++;
427 // if we reached the end, stop
430 // store the next word
431 if(i < max_words) words[i++] = s;
437 // terminate the words
438 while(i < max_words) words[i++] = NULL;
441 pid_t tc_child_pid = 0;
442 void *tc_main(void *ptr)
446 info("TC thread created with task id %d", gettid());
448 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
449 error("Cannot set pthread cancel type to DEFERRED.");
451 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
452 error("Cannot set pthread cancel state to ENABLE.");
454 struct rusage thread;
455 RRDSET *stcpu = NULL, *sttime = NULL;
457 char buffer[TC_LINE_MAX+1] = "";
458 char *words[MAX_WORDS] = { NULL };
460 uint32_t BEGIN_HASH = simple_hash("BEGIN");
461 uint32_t END_HASH = simple_hash("END");
462 uint32_t CLASS_HASH = simple_hash("class");
463 uint32_t SENT_HASH = simple_hash("Sent");
464 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
465 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
466 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
467 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
468 #ifdef DETACH_PLUGINS_FROM_NETDATA
469 uint32_t MYPID_HASH = simple_hash("MYPID");
475 struct tc_device *device = NULL;
476 struct tc_class *class = NULL;
478 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);
479 debug(D_TC_LOOP, "executing '%s'", buffer);
480 // fp = popen(buffer, "r");
481 fp = mypopen(buffer, &tc_child_pid);
483 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
487 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
488 buffer[TC_LINE_MAX] = '\0';
489 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
491 tc_split_words(buffer, words, MAX_WORDS);
492 if(!words[0] || !*words[0]) {
493 // debug(D_TC_LOOP, "empty line");
496 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
498 first_hash = simple_hash(words[0]);
500 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
501 // 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]);
503 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
504 // char *type = words[1]; // the class: htb, fq_codel, etc
505 char *id = words[2]; // the class major:minor
506 char *parent = words[3]; // 'parent' or 'root'
507 char *parentid = words[4]; // the parent's id
508 char *leaf = words[5]; // 'leaf'
509 char *leafid = words[6]; // leafid
511 if(strcmp(parent, "root") == 0) {
515 else if(!leaf || strcmp(leaf, "leaf") != 0)
518 char leafbuf[20 + 1] = "";
519 if(leafid && leafid[strlen(leafid) - 1] == ':') {
520 strncpy(leafbuf, leafid, 20 - 1);
521 strcat(leafbuf, "1");
525 class = tc_class_add(device, id, parentid, leafid);
528 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
529 // debug(D_TC_LOOP, "END line");
532 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
533 error("Cannot set pthread cancel state to DISABLE.");
535 tc_device_commit(device);
536 // tc_device_free(device);
540 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
541 error("Cannot set pthread cancel state to ENABLE.");
544 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
545 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
548 // tc_device_free(device);
553 if(words[1] && *words[1]) {
554 device = tc_device_create(words[1]);
558 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
559 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
560 if(words[1] && *words[1]) {
561 class->bytes = atoll(words[1]);
564 else class->bytes = 0;
566 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
567 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
568 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
570 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
571 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
572 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
574 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
575 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
577 char *path = words[2];
578 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
580 else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
581 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
582 getrusage(RUSAGE_THREAD, &thread);
584 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
586 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
587 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
588 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
590 else rrdset_next(stcpu);
592 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
593 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
596 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
598 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
599 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
601 else rrdset_next(sttime);
603 rrddim_set(sttime, "run_time", atoll(words[1]));
607 #ifdef DETACH_PLUGINS_FROM_NETDATA
608 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
609 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
611 pid_t pid = atol(id);
613 if(pid) tc_child_pid = pid;
615 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
619 // debug(D_TC_LOOP, "IGNORED line");
622 mypclose(fp, tc_child_pid);
626 // tc_device_free(device);
631 sleep(rrd_update_every);