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 = {
83 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
84 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
86 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
87 struct tc_device *result = NULL, tmp;
89 tmp.hash = (hash)?hash:simple_hash(tmp.id);
91 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
96 // ----------------------------------------------------------------------------
99 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
101 static int tc_class_compare(void* a, void* b) {
102 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
103 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
104 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
107 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
108 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
110 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
111 struct tc_class *result = NULL, tmp;
113 tmp.hash = (hash)?hash:simple_hash(tmp.id);
115 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
119 // ----------------------------------------------------------------------------
121 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
122 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);
124 if(c->next) c->next->prev = c->prev;
125 if(c->prev) c->prev->next = c->next;
126 if(n->classes == c) n->classes = c->next;
128 tc_class_index_del(n, c);
130 if(c->id) free(c->id);
131 if(c->name) free(c->name);
132 if(c->leafid) free(c->leafid);
133 if(c->parentid) free(c->parentid);
138 static void tc_device_classes_cleanup(struct tc_device *d) {
139 static int cleanup_every = 999;
141 if(cleanup_every > 0) {
142 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
143 if(cleanup_every > 0) cleanup_every = -cleanup_every;
144 if(cleanup_every == 0) cleanup_every = -1;
147 struct tc_class *c = d->classes;
149 if(c->seen < cleanup_every) {
150 struct tc_class *nc = c->next;
163 static void tc_device_commit(struct tc_device *d)
165 static int enable_new_interfaces = -1;
167 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
169 // we only need to add leaf classes
170 struct tc_class *c, *x;
173 for(c = d->classes ; c ; c = c->next) {
178 // mark the classes as leafs and parents
179 for(c = d->classes ; c ; c = c->next) {
180 if(!c->updated) continue;
182 for(x = d->classes ; x ; x = x->next) {
183 if(!x->updated) continue;
188 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
189 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
190 // 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);
199 for ( c = d->classes ; c ; c = c->next) {
200 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
201 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);
205 // we need at least a class
206 for(c = d->classes ; c ; c = c->next) {
207 // 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);
208 if(!c->updated) continue;
209 if(c->isleaf && c->hasparent) break;
212 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
213 tc_device_classes_cleanup(d);
217 char var_name[CONFIG_MAX_NAME + 1];
218 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
219 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
220 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
222 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
224 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);
226 for(c = d->classes ; c ; c = c->next) {
227 if(!c->updated) continue;
229 if(c->isleaf && c->hasparent)
230 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
234 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
235 rrdset_next_plugins(st);
237 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
240 for(c = d->classes ; c ; c = c->next) {
241 if(!c->updated) continue;
243 if(c->isleaf && c->hasparent) {
244 RRDDIM *rd = rrddim_find(st, c->id);
247 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
249 // new class, we have to add it
250 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
252 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
254 rrddim_set_by_pointer(st, rd, c->bytes);
256 // if it has a name, different to the id
258 // update the rrd dimension with the new name
259 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
260 rrddim_set_name(st, rd, c->name);
270 tc_device_classes_cleanup(d);
273 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
275 struct tc_class *c = tc_class_index_find(d, id, 0);
277 if(c->name) free(c->name);
280 if(name && *name && strcmp(c->id, name) != 0) {
281 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
282 c->name = strdup(name);
287 static void tc_device_set_device_name(struct tc_device *d, char *name) {
288 if(d->name) free(d->name);
291 if(name && *name && strcmp(d->id, name) != 0) {
292 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
293 d->name = strdup(name);
297 static void tc_device_set_device_family(struct tc_device *d, char *family) {
298 if(d->family) free(d->family);
301 if(family && *family && strcmp(d->id, family) != 0) {
302 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
303 d->family = strdup(family);
305 // no need for null termination - it is already null
308 static struct tc_device *tc_device_create(char *id)
310 struct tc_device *d = tc_device_index_find(id, 0);
313 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
315 d = calloc(1, sizeof(struct tc_device));
317 fatal("Cannot allocate memory for tc_device %s", id);
322 d->hash = simple_hash(d->id);
324 d->classes_index.root = NULL;
325 d->classes_index.compar = tc_class_compare;
327 tc_device_index_add(d);
333 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
335 struct tc_class *c = tc_class_index_find(n, id, 0);
338 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
340 c = calloc(1, sizeof(struct tc_class));
342 fatal("Cannot allocate memory for tc class");
346 if(n->classes) n->classes->prev = c;
347 c->next = n->classes;
355 c->hash = simple_hash(c->id);
357 if(parentid && *parentid) {
358 c->parentid = strdup(parentid);
359 c->parent_hash = simple_hash(c->parentid);
362 if(leafid && *leafid) {
363 c->leafid = strdup(leafid);
364 c->leaf_hash = simple_hash(c->leafid);
367 tc_class_index_add(n, c);
375 static void tc_device_free(struct tc_device *n)
377 tc_device_index_del(n);
379 while(n->classes) tc_class_free(n, n->classes);
381 if(n->id) free(n->id);
382 if(n->name) free(n->name);
383 if(n->family) free(n->family);
391 static inline int tc_space(char c) {
404 static void tc_split_words(char *str, char **words, int max_words) {
408 // skip all white space
409 while(tc_space(*s)) s++;
411 // store the first word
414 // while we have something
419 // terminate the word
422 // skip all white space
423 while(tc_space(*s)) s++;
425 // if we reached the end, stop
428 // store the next word
429 if(i < max_words) words[i++] = s;
435 // terminate the words
436 while(i < max_words) words[i++] = NULL;
439 pid_t tc_child_pid = 0;
440 void *tc_main(void *ptr)
444 info("TC thread created with task id %d", gettid());
446 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
447 error("Cannot set pthread cancel type to DEFERRED.");
449 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
450 error("Cannot set pthread cancel state to ENABLE.");
452 struct rusage thread;
453 RRDSET *stcpu = NULL, *sttime = NULL;
455 char buffer[TC_LINE_MAX+1] = "";
456 char *words[MAX_WORDS] = { NULL };
458 uint32_t BEGIN_HASH = simple_hash("BEGIN");
459 uint32_t END_HASH = simple_hash("END");
460 uint32_t CLASS_HASH = simple_hash("class");
461 uint32_t SENT_HASH = simple_hash("Sent");
462 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
463 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
464 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
465 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
466 #ifdef DETACH_PLUGINS_FROM_NETDATA
467 uint32_t MYPID_HASH = simple_hash("MYPID");
473 struct tc_device *device = NULL;
474 struct tc_class *class = NULL;
476 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);
477 debug(D_TC_LOOP, "executing '%s'", buffer);
478 // fp = popen(buffer, "r");
479 fp = mypopen(buffer, &tc_child_pid);
481 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
485 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
486 buffer[TC_LINE_MAX] = '\0';
487 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
489 tc_split_words(buffer, words, MAX_WORDS);
490 if(!words[0] || !*words[0]) {
491 // debug(D_TC_LOOP, "empty line");
494 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
496 first_hash = simple_hash(words[0]);
498 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
499 // 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]);
501 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
502 // char *type = words[1]; // the class: htb, fq_codel, etc
503 char *id = words[2]; // the class major:minor
504 char *parent = words[3]; // 'parent' or 'root'
505 char *parentid = words[4]; // the parent's id
506 char *leaf = words[5]; // 'leaf'
507 char *leafid = words[6]; // leafid
509 if(strcmp(parent, "root") == 0) {
513 else if(!leaf || strcmp(leaf, "leaf") != 0)
516 char leafbuf[20 + 1] = "";
517 if(leafid && leafid[strlen(leafid) - 1] == ':') {
518 strncpy(leafbuf, leafid, 20 - 1);
519 strcat(leafbuf, "1");
523 class = tc_class_add(device, id, parentid, leafid);
526 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
527 // debug(D_TC_LOOP, "END line");
530 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
531 error("Cannot set pthread cancel state to DISABLE.");
533 tc_device_commit(device);
534 // tc_device_free(device);
538 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
539 error("Cannot set pthread cancel state to ENABLE.");
542 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
543 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
546 // tc_device_free(device);
551 if(words[1] && *words[1]) {
552 device = tc_device_create(words[1]);
556 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
557 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
558 if(words[1] && *words[1]) {
559 class->bytes = atoll(words[1]);
562 else class->bytes = 0;
564 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
565 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
566 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
568 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
569 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
570 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
572 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
573 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
575 char *path = words[2];
576 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
578 else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
579 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
580 getrusage(RUSAGE_THREAD, &thread);
582 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
584 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
585 rrddim_add(stcpu, "user", NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
586 rrddim_add(stcpu, "system", NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
588 else rrdset_next(stcpu);
590 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
591 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
594 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
596 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
597 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
599 else rrdset_next(sttime);
601 rrddim_set(sttime, "run_time", atoll(words[1]));
605 #ifdef DETACH_PLUGINS_FROM_NETDATA
606 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
607 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
609 pid_t pid = atol(id);
611 if(pid) tc_child_pid = pid;
613 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
617 // debug(D_TC_LOOP, "IGNORED line");
620 mypclose(fp, tc_child_pid);
624 // tc_device_free(device);
629 sleep(rrd_update_every);