3 #define RRD_TYPE_TC "tc"
5 // ----------------------------------------------------------------------------
7 // this requires the script plugins.d/tc-qos-helper.sh
9 #define TC_LINE_MAX 1024
30 unsigned long long bytes;
31 unsigned long long packets;
32 unsigned long long dropped;
33 unsigned long long overlimits;
34 unsigned long long requeues;
35 unsigned long long lended;
36 unsigned long long borrowed;
37 unsigned long long giants;
38 unsigned long long tokens;
39 unsigned long long ctokens;
48 char updated; // updated bytes
49 int unupdated; // the number of times, this has been found un-updated
51 struct tc_class *next;
52 struct tc_class *prev;
73 char enabled_all_classes_qdiscs;
81 avl_tree classes_index;
83 struct tc_class *classes;
85 struct tc_device *next;
86 struct tc_device *prev;
90 struct tc_device *tc_device_root = NULL;
92 // ----------------------------------------------------------------------------
95 static int tc_device_compare(void* a, void* b) {
96 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
97 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
98 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
101 avl_tree tc_device_root_index = {
106 #define tc_device_index_add(st) (struct tc_device *)avl_insert(&tc_device_root_index, (avl *)(st))
107 #define tc_device_index_del(st) (struct tc_device *)avl_remove(&tc_device_root_index, (avl *)(st))
109 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
110 struct tc_device tmp;
112 tmp.hash = (hash)?hash:simple_hash(tmp.id);
114 return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
118 // ----------------------------------------------------------------------------
121 static int tc_class_compare(void* a, void* b) {
122 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
123 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
124 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
127 #define tc_class_index_add(st, rd) (struct tc_class *)avl_insert(&((st)->classes_index), (avl *)(rd))
128 #define tc_class_index_del(st, rd) (struct tc_class *)avl_remove(&((st)->classes_index), (avl *)(rd))
130 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
133 tmp.hash = (hash)?hash:simple_hash(tmp.id);
135 return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
138 // ----------------------------------------------------------------------------
140 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
141 if(c == n->classes) {
143 n->classes = c->next;
145 n->classes = c->prev;
147 if(c->next) c->next->prev = c->prev;
148 if(c->prev) c->prev->next = c->next;
150 debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', unused=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->unupdated);
152 if(unlikely(tc_class_index_del(n, c) != c))
153 error("plugin_tc: INTERNAL ERROR: attempt remove class '%s' from device '%s': removed a different calls", c->id, n->id);
162 static inline void tc_device_classes_cleanup(struct tc_device *d) {
163 static int cleanup_every = 999;
165 if(unlikely(cleanup_every > 0)) {
166 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 120);
167 if(cleanup_every < 0) cleanup_every = -cleanup_every;
171 d->family_updated = 0;
173 struct tc_class *c = d->classes;
175 if(unlikely(cleanup_every && c->unupdated >= cleanup_every)) {
176 struct tc_class *nc = c->next;
189 static inline void tc_device_commit(struct tc_device *d) {
190 static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1, enable_tokens = -1, enable_ctokens = -1, enabled_all_classes_qdiscs = -1;
192 if(unlikely(enable_new_interfaces == -1)) {
193 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
194 enable_bytes = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
195 enable_packets = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
196 enable_dropped = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
197 enable_tokens = config_get_boolean_ondemand("plugin:tc", "enable tokens charts for all interfaces", CONFIG_ONDEMAND_NO);
198 enable_ctokens = config_get_boolean_ondemand("plugin:tc", "enable ctokens charts for all interfaces", CONFIG_ONDEMAND_NO);
199 enabled_all_classes_qdiscs = config_get_boolean_ondemand("plugin:tc", "enable show all classes and qdiscs for all interfaces", CONFIG_ONDEMAND_NO);
202 if(unlikely(d->enabled == (char)-1)) {
203 char var_name[CONFIG_MAX_NAME + 1];
204 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
206 d->enabled = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
208 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
209 d->enabled_bytes = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
211 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
212 d->enabled_packets = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
214 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
215 d->enabled_dropped = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
217 snprintfz(var_name, CONFIG_MAX_NAME, "tokens chart for %s", d->id);
218 d->enabled_tokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_tokens);
220 snprintfz(var_name, CONFIG_MAX_NAME, "ctokens chart for %s", d->id);
221 d->enabled_ctokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_ctokens);
223 snprintfz(var_name, CONFIG_MAX_NAME, "show all classes for %s", d->id);
224 d->enabled_all_classes_qdiscs = (char)config_get_boolean_ondemand("plugin:tc", var_name, enabled_all_classes_qdiscs);
227 // we only need to add leaf classes
228 struct tc_class *c, *x /*, *root = NULL */;
229 unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0, tokens_sum = 0, ctokens_sum = 0;
230 int active_nodes = 0, updated_classes = 0, updated_qdiscs = 0;
232 // prepare all classes
233 // we set reasonable defaults for the rest of the code below
235 for(c = d->classes ; c ; c = c->next) {
236 c->render = 0; // do not render this class
238 c->isleaf = 1; // this is a leaf class
239 c->hasparent = 0; // without a parent
241 if(unlikely(!c->updated))
242 c->unupdated++; // increase its unupdated counter
244 c->unupdated = 0; // reset its unupdated counter
246 // count how many of each kind
254 if(unlikely(!d->enabled || (!updated_classes && !updated_qdiscs))) {
255 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. It is not enabled/updated.", d->name?d->name:d->id);
256 tc_device_classes_cleanup(d);
260 if(unlikely(updated_classes && updated_qdiscs)) {
261 error("TC: device '%s' has active both classes (%d) and qdiscs (%d). Will render only qdiscs.", d->id, updated_classes, updated_qdiscs);
263 // set all classes to !updated
264 for(c = d->classes ; c ; c = c->next)
265 if(unlikely(!c->isqdisc && c->updated))
271 // mark the classes as leafs and parents
273 // TC is hierarchical:
274 // - classes can have other classes in them
275 // - the same is true for qdiscs (i.e. qdiscs have classes, that have other qdiscs)
277 // we need to present a chart with leaf nodes only, so that the sum
278 // of all dimensions of the chart, will be the total utilization
281 // here we try to find the ones we need to report
282 // by default all nodes are marked with: isleaf = 1 (see above)
284 // so, here we remove the isleaf flag from nodes in the middle
285 // and we add the hasparent flag to leaf nodes we found their parent
286 for(c = d->classes; c; c = c->next) {
287 if(unlikely(!c->updated)) continue;
289 //debug(D_TC_LOOP, "TC: In device '%s', %s '%s' has leafid: '%s' and parentid '%s'.",
291 // c->isqdisc?"qdisc":"class",
293 // c->leafid?c->leafid:"NULL",
294 // c->parentid?c->parentid:"NULL");
296 // find if c is leaf or not
297 for(x = d->classes; x; x = x->next) {
298 if(unlikely(!x->updated || c == x || !x->parentid)) continue;
300 // classes have both parentid and leafid
301 // qdiscs have only parentid
302 // the following works for both (it is an OR)
304 if( (c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
305 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0)) {
306 // debug(D_TC_LOOP, "TC: In device '%s', %s '%s' (leafid: '%s') has as leaf %s '%s' (parentid: '%s').", d->name?d->name:d->id, c->isqdisc?"qdisc":"class", c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->isqdisc?"qdisc":"class", x->name?x->name:x->id, x->parentid?x->parentid:x->id);
313 for(c = d->classes ; c ; c = c->next) {
314 if(unlikely(!c->updated)) continue;
316 // debug(D_TC_LOOP, "TC: device '%s', %s '%s' isleaf=%d, hasparent=%d", d->id, (c->isqdisc)?"qdisc":"class", c->id, c->isleaf, c->hasparent);
318 if(unlikely((c->isleaf && c->hasparent) || d->enabled_all_classes_qdiscs)) {
321 bytes_sum += c->bytes;
322 packets_sum += c->packets;
323 dropped_sum += c->dropped;
324 tokens_sum += c->tokens;
325 ctokens_sum += c->ctokens;
328 //if(unlikely(!c->hasparent)) {
329 // if(root) error("TC: multiple root class/qdisc for device '%s' (old: '%s', new: '%s')", d->id, root->id, c->id);
331 // debug(D_TC_LOOP, "TC: found root class/qdisc '%s'", root->id);
335 #ifdef NETDATA_INTERNAL_CHECKS
336 // dump all the list to see what we know
338 if(unlikely(debug_flags & D_TC_LOOP)) {
339 for(c = d->classes ; c ; c = c->next) {
340 if(c->render) debug(D_TC_LOOP, "TC: final nodes dump for '%s': class %s, OK", d->name, c->id);
341 else debug(D_TC_LOOP, "TC: final nodes dump for '%s': class %s, IGNORE (updated: %d, isleaf: %d, hasparent: %d, parent: %s)", d->name?d->name:d->id, c->id, c->updated, c->isleaf, c->hasparent, c->parentid?c->parentid:"(unset)");
346 if(unlikely(!active_nodes)) {
347 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No useful classes/qdiscs.", d->name?d->name:d->id);
348 tc_device_classes_cleanup(d);
352 debug(D_TC_LOOP, "TC: evaluating TC device '%s'. enabled = %d/%d (bytes: %d/%d, packets: %d/%d, dropped: %d/%d, tokens: %d/%d, ctokens: %d/%d, all_classes_qdiscs: %d/%d), classes: (bytes = %llu, packets = %llu, dropped = %llu, tokens = %llu, ctokens = %llu).",
353 d->name?d->name:d->id,
354 d->enabled, enable_new_interfaces,
355 d->enabled_bytes, enable_bytes,
356 d->enabled_packets, enable_packets,
357 d->enabled_dropped, enable_dropped,
358 d->enabled_tokens, enable_tokens,
359 d->enabled_ctokens, enable_ctokens,
360 d->enabled_all_classes_qdiscs, enabled_all_classes_qdiscs,
368 // --------------------------------------------------------------------
371 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
372 d->enabled_bytes = CONFIG_ONDEMAND_YES;
374 if(unlikely(!d->st_bytes))
375 d->st_bytes = 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, d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED);
378 rrdset_next(d->st_bytes);
379 if(unlikely(d->name_updated)) rrdset_set_name(d->st_bytes, d->name);
385 for(c = d->classes ; c ; c = c->next) {
386 if(unlikely(!c->render)) continue;
388 if(unlikely(!c->rd_bytes))
389 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
390 else if(unlikely(c->name_updated))
391 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
393 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
395 rrdset_done(d->st_bytes);
398 // --------------------------------------------------------------------
401 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
402 d->enabled_packets = CONFIG_ONDEMAND_YES;
404 if(unlikely(!d->st_packets)) {
405 char id[RRD_ID_LENGTH_MAX + 1];
406 char name[RRD_ID_LENGTH_MAX + 1];
407 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
408 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
410 d->st_packets = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_packets", "Class Packets", "packets/s", 7010, rrd_update_every, d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED);
413 rrdset_next(d->st_packets);
415 if(unlikely(d->name_updated)) {
416 char name[RRD_ID_LENGTH_MAX + 1];
417 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
418 rrdset_set_name(d->st_packets, name);
425 for(c = d->classes ; c ; c = c->next) {
426 if(unlikely(!c->render)) continue;
428 if(unlikely(!c->rd_packets))
429 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
430 else if(unlikely(c->name_updated))
431 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
433 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
435 rrdset_done(d->st_packets);
438 // --------------------------------------------------------------------
441 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
442 d->enabled_dropped = CONFIG_ONDEMAND_YES;
444 if(unlikely(!d->st_dropped)) {
445 char id[RRD_ID_LENGTH_MAX + 1];
446 char name[RRD_ID_LENGTH_MAX + 1];
447 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
448 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
450 d->st_dropped = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_dropped", "Class Dropped Packets", "packets/s", 7020, rrd_update_every, d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED);
453 rrdset_next(d->st_dropped);
455 if(unlikely(d->name_updated)) {
456 char name[RRD_ID_LENGTH_MAX + 1];
457 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
458 rrdset_set_name(d->st_dropped, name);
465 for(c = d->classes ; c ; c = c->next) {
466 if(unlikely(!c->render)) continue;
468 if(unlikely(!c->rd_dropped))
469 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
470 else if(unlikely(c->name_updated))
471 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
473 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
475 rrdset_done(d->st_dropped);
478 // --------------------------------------------------------------------
481 if(d->enabled_tokens == CONFIG_ONDEMAND_YES || (d->enabled_tokens == CONFIG_ONDEMAND_ONDEMAND && tokens_sum)) {
482 d->enabled_tokens = CONFIG_ONDEMAND_YES;
484 if(unlikely(!d->st_tokens)) {
485 char id[RRD_ID_LENGTH_MAX + 1];
486 char name[RRD_ID_LENGTH_MAX + 1];
487 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_tokens", d->id);
488 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
490 d->st_tokens = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_tokens", "Class Tokens", "tokens", 7030, rrd_update_every, RRDSET_TYPE_LINE);
493 rrdset_next(d->st_tokens);
495 if(unlikely(d->name_updated)) {
496 char name[RRD_ID_LENGTH_MAX + 1];
497 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
498 rrdset_set_name(d->st_tokens, name);
505 for(c = d->classes ; c ; c = c->next) {
506 if(unlikely(!c->render)) continue;
508 if(unlikely(!c->rd_tokens)) {
509 c->rd_tokens = rrddim_add(d->st_tokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
511 else if(unlikely(c->name_updated))
512 rrddim_set_name(d->st_tokens, c->rd_tokens, c->name);
514 rrddim_set_by_pointer(d->st_tokens, c->rd_tokens, c->tokens);
516 rrdset_done(d->st_tokens);
519 // --------------------------------------------------------------------
522 if(d->enabled_ctokens == CONFIG_ONDEMAND_YES || (d->enabled_ctokens == CONFIG_ONDEMAND_ONDEMAND && ctokens_sum)) {
523 d->enabled_ctokens = CONFIG_ONDEMAND_YES;
525 if(unlikely(!d->st_ctokens)) {
526 char id[RRD_ID_LENGTH_MAX + 1];
527 char name[RRD_ID_LENGTH_MAX + 1];
528 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_ctokens", d->id);
529 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
531 d->st_ctokens = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_ctokens", "Class cTokens", "ctokens", 7040, rrd_update_every, RRDSET_TYPE_LINE);
534 debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
535 rrdset_next(d->st_ctokens);
537 if(unlikely(d->name_updated)) {
538 char name[RRD_ID_LENGTH_MAX + 1];
539 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
540 rrdset_set_name(d->st_ctokens, name);
547 for(c = d->classes ; c ; c = c->next) {
548 if(unlikely(!c->render)) continue;
550 if(unlikely(!c->rd_ctokens))
551 c->rd_ctokens = rrddim_add(d->st_ctokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
552 else if(unlikely(c->name_updated))
553 rrddim_set_name(d->st_ctokens, c->rd_ctokens, c->name);
555 rrddim_set_by_pointer(d->st_ctokens, c->rd_ctokens, c->ctokens);
557 rrdset_done(d->st_ctokens);
560 tc_device_classes_cleanup(d);
563 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name) {
564 if(unlikely(!name || !*name)) return;
566 struct tc_class *c = tc_class_index_find(d, id, 0);
568 if(likely(c->name)) {
569 if(!strcmp(c->name, name)) return;
574 if(likely(name && *name && strcmp(c->id, name) != 0)) {
575 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
576 c->name = strdupz(name);
582 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
583 if(unlikely(!name || !*name)) return;
586 if(!strcmp(d->name, name)) return;
591 if(likely(name && *name && strcmp(d->id, name))) {
592 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
593 d->name = strdupz(name);
598 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
602 if(likely(family && *family && strcmp(d->id, family) != 0)) {
603 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
604 d->family = strdupz(family);
605 d->family_updated = 1;
607 // no need for null termination - it is already null
610 static inline struct tc_device *tc_device_create(char *id)
612 struct tc_device *d = tc_device_index_find(id, 0);
615 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
617 d = callocz(1, sizeof(struct tc_device));
620 d->hash = simple_hash(d->id);
621 d->enabled = (char)-1;
623 avl_init(&d->classes_index, tc_class_compare);
624 if(unlikely(tc_device_index_add(d) != d))
625 error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
627 if(!tc_device_root) {
631 d->next = tc_device_root;
632 tc_device_root->prev = d;
640 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char qdisc, char *parentid, char *leafid)
642 struct tc_class *c = tc_class_index_find(n, id, 0);
645 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
647 c = callocz(1, sizeof(struct tc_class));
649 if(n->classes) n->classes->prev = c;
650 c->next = n->classes;
654 c->hash = simple_hash(c->id);
657 if(parentid && *parentid) {
658 c->parentid = strdupz(parentid);
659 c->parent_hash = simple_hash(c->parentid);
662 if(leafid && *leafid) {
663 c->leafid = strdupz(leafid);
664 c->leaf_hash = simple_hash(c->leafid);
667 if(unlikely(tc_class_index_add(n, c) != c))
668 error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
673 static inline void tc_device_free(struct tc_device *n)
675 if(n->next) n->next->prev = n->prev;
676 if(n->prev) n->prev->next = n->next;
677 if(tc_device_root == n) {
678 if(n->next) tc_device_root = n->next;
679 else tc_device_root = n->prev;
682 if(unlikely(tc_device_index_del(n) != n))
683 error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
685 while(n->classes) tc_class_free(n, n->classes);
693 static inline void tc_device_free_all()
695 while(tc_device_root)
696 tc_device_free(tc_device_root);
701 static inline int tc_space(char c) {
714 static inline void tc_split_words(char *str, char **words, int max_words) {
718 // skip all white space
719 while(tc_space(*s)) s++;
721 // store the first word
724 // while we have something
727 if(unlikely(tc_space(*s))) {
729 // terminate the word
732 // skip all white space
733 while(tc_space(*s)) s++;
735 // if we reached the end, stop
738 // store the next word
739 if(i < max_words) words[i++] = s;
745 // terminate the words
746 while(i < max_words) words[i++] = NULL;
749 volatile pid_t tc_child_pid = 0;
750 void *tc_main(void *ptr) {
751 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
753 info("TC thread created with task id %d", gettid());
755 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
756 error("Cannot set pthread cancel type to DEFERRED.");
758 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
759 error("Cannot set pthread cancel state to ENABLE.");
761 struct rusage thread;
762 RRDSET *stcpu = NULL, *sttime = NULL;
764 char buffer[TC_LINE_MAX+1] = "";
765 char *words[MAX_WORDS] = { NULL };
767 uint32_t BEGIN_HASH = simple_hash("BEGIN");
768 uint32_t END_HASH = simple_hash("END");
769 uint32_t QDISC_HASH = simple_hash("qdisc");
770 uint32_t CLASS_HASH = simple_hash("class");
771 uint32_t SENT_HASH = simple_hash("Sent");
772 uint32_t LENDED_HASH = simple_hash("lended:");
773 uint32_t TOKENS_HASH = simple_hash("tokens:");
774 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
775 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
776 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
777 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
778 #ifdef DETACH_PLUGINS_FROM_NETDATA
779 uint32_t MYPID_HASH = simple_hash("MYPID");
783 snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
784 char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
787 if(unlikely(netdata_exit)) break;
790 struct tc_device *device = NULL;
791 struct tc_class *class = NULL;
793 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
794 debug(D_TC_LOOP, "executing '%s'", buffer);
796 fp = mypopen(buffer, (pid_t *)&tc_child_pid);
798 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
802 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
803 if(unlikely(netdata_exit)) break;
805 buffer[TC_LINE_MAX] = '\0';
806 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
808 tc_split_words(buffer, words, MAX_WORDS);
810 if(unlikely(!words[0] || !*words[0])) {
811 // debug(D_TC_LOOP, "empty line");
814 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
816 first_hash = simple_hash(words[0]);
818 if(unlikely(device && ((first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) || (first_hash == QDISC_HASH && strcmp(words[0], "qdisc") == 0)))) {
819 // 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]);
821 char *type = words[1]; // the class/qdisc type: htb, fq_codel, etc
822 char *id = words[2]; // the class/qdisc major:minor
823 char *parent = words[3]; // the word 'parent' or 'root'
824 char *parentid = words[4]; // parentid
825 char *leaf = words[5]; // the word 'leaf'
826 char *leafid = words[6]; // leafid
828 int parent_is_root = 0;
829 int parent_is_parent = 0;
831 parent_is_parent = !strcmp(parent, "parent");
833 if(!parent_is_parent)
834 parent_is_root = !strcmp(parent, "root");
837 if(likely(type && id && (parent_is_root || parent_is_parent))) {
840 if(first_hash == QDISC_HASH) {
843 if(!strcmp(type, "ingress")) {
844 // we don't want to get the ingress qdisc
845 // there should be an IFB interface for this
851 if(parent_is_parent && parentid) {
852 // eliminate the minor number from parentid
853 // why: parentid is the id of the parent class
854 // but major: is also the id of the parent qdisc
857 while(*s && *s != ':') s++;
858 if(*s == ':') s[1] = '\0';
866 else if(!leaf || strcmp(leaf, "leaf") != 0)
869 char leafbuf[20 + 1] = "";
870 if(leafid && leafid[strlen(leafid) - 1] == ':') {
871 strncpyz(leafbuf, leafid, 20 - 1);
872 strcat(leafbuf, "1");
876 class = tc_class_add(device, id, qdisc, parentid, leafid);
879 // clear the last class
883 else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
884 // debug(D_TC_LOOP, "END line");
887 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
888 error("Cannot set pthread cancel state to DISABLE.");
890 tc_device_commit(device);
891 // tc_device_free(device);
893 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
894 error("Cannot set pthread cancel state to ENABLE.");
900 else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
901 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
903 if(likely(words[1] && *words[1])) {
904 device = tc_device_create(words[1]);
907 // tc_device_free(device);
913 else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
914 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
915 if(likely(words[1] && *words[1])) {
916 class->bytes = str2ull(words[1]);
923 if(likely(words[3] && *words[3]))
924 class->packets = str2ull(words[3]);
926 if(likely(words[6] && *words[6]))
927 class->dropped = str2ull(words[6]);
929 if(likely(words[8] && *words[8]))
930 class->overlimits = str2ull(words[8]);
932 if(likely(words[10] && *words[10]))
933 class->requeues = str2ull(words[8]);
935 else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
936 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
937 if(likely(words[1] && *words[1]))
938 class->lended = str2ull(words[1]);
940 if(likely(words[3] && *words[3]))
941 class->borrowed = str2ull(words[3]);
943 if(likely(words[5] && *words[5]))
944 class->giants = str2ull(words[5]);
946 else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
947 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
948 if(likely(words[1] && *words[1]))
949 class->tokens = str2ull(words[1]);
951 if(likely(words[3] && *words[3]))
952 class->ctokens = str2ull(words[3]);
954 else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
955 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
956 if(likely(words[1] && *words[1]))
957 tc_device_set_device_name(device, words[1]);
959 else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
960 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
961 if(likely(words[1] && *words[1]))
962 tc_device_set_device_family(device, words[1]);
964 else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
965 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
967 char *path = words[2];
968 if(likely(id && *id && path && *path))
969 tc_device_set_class_name(device, id, path);
971 else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
972 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
973 getrusage(RUSAGE_THREAD, &thread);
975 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
976 if(unlikely(!stcpu)) {
977 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
978 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
979 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
981 else rrdset_next(stcpu);
983 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
984 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
987 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
988 if(unlikely(!sttime)) {
989 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
990 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
992 else rrdset_next(sttime);
994 rrddim_set(sttime, "run_time", atoll(words[1]));
998 #ifdef DETACH_PLUGINS_FROM_NETDATA
999 else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
1000 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
1001 char *id = words[1];
1002 pid_t pid = atol(id);
1004 if(likely(pid)) tc_child_pid = pid;
1006 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
1010 // debug(D_TC_LOOP, "IGNORED line");
1014 // fgets() failed or loop broke
1015 int code = mypclose(fp, (pid_t)tc_child_pid);
1018 if(unlikely(device)) {
1019 // tc_device_free(device);
1024 if(unlikely(netdata_exit)) {
1025 tc_device_free_all();
1029 if(code == 1 || code == 127) {
1031 // 127 = cannot even run it
1032 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
1034 tc_device_free_all();
1038 sleep((unsigned int) rrd_update_every);
1042 info("TC thread exiting");
1044 static_thread->enabled = 0;