3 #define RRD_TYPE_TC "tc"
4 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
6 // ----------------------------------------------------------------------------
8 // this requires the script plugins.d/tc-qos-helper.sh
10 #define TC_LINE_MAX 1024
29 unsigned long long bytes;
30 unsigned long long packets;
31 unsigned long long dropped;
32 unsigned long long overlimits;
33 unsigned long long requeues;
34 unsigned long long lended;
35 unsigned long long borrowed;
36 unsigned long long giants;
37 unsigned long long tokens;
38 unsigned long long ctokens;
47 char updated; // updated bytes
48 int unupdated; // the number of times, this has been found un-updated
50 struct tc_class *next;
51 struct tc_class *prev;
72 char enabled_all_classes_qdiscs;
80 avl_tree classes_index;
82 struct tc_class *classes;
84 struct tc_device *next;
85 struct tc_device *prev;
89 struct tc_device *tc_device_root = NULL;
91 // ----------------------------------------------------------------------------
94 static int tc_device_compare(void* a, void* b) {
95 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
96 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
97 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
100 avl_tree tc_device_root_index = {
105 #define tc_device_index_add(st) (struct tc_device *)avl_insert(&tc_device_root_index, (avl *)(st))
106 #define tc_device_index_del(st) (struct tc_device *)avl_remove(&tc_device_root_index, (avl *)(st))
108 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
109 struct tc_device tmp;
111 tmp.hash = (hash)?hash:simple_hash(tmp.id);
113 return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
117 // ----------------------------------------------------------------------------
120 static int tc_class_compare(void* a, void* b) {
121 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
122 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
123 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
126 #define tc_class_index_add(st, rd) (struct tc_class *)avl_insert(&((st)->classes_index), (avl *)(rd))
127 #define tc_class_index_del(st, rd) (struct tc_class *)avl_remove(&((st)->classes_index), (avl *)(rd))
129 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
132 tmp.hash = (hash)?hash:simple_hash(tmp.id);
134 return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
137 // ----------------------------------------------------------------------------
139 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
140 if(c == n->classes) {
142 n->classes = c->next;
144 n->classes = c->prev;
146 if(c->next) c->next->prev = c->prev;
147 if(c->prev) c->prev->next = c->next;
149 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);
151 if(unlikely(tc_class_index_del(n, c) != c))
152 error("plugin_tc: INTERNAL ERROR: attempt remove class '%s' from device '%s': removed a different calls", c->id, n->id);
161 static inline void tc_device_classes_cleanup(struct tc_device *d) {
162 static int cleanup_every = 999;
164 if(unlikely(cleanup_every > 0)) {
165 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 120);
166 if(cleanup_every < 0) cleanup_every = -cleanup_every;
170 d->family_updated = 0;
172 struct tc_class *c = d->classes;
174 if(unlikely(cleanup_every && c->unupdated >= cleanup_every)) {
175 struct tc_class *nc = c->next;
188 static inline void tc_device_commit(struct tc_device *d) {
189 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;
191 if(unlikely(enable_new_interfaces == -1)) {
192 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
193 enable_bytes = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
194 enable_packets = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
195 enable_dropped = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
196 enable_tokens = config_get_boolean_ondemand("plugin:tc", "enable tokens charts for all interfaces", CONFIG_ONDEMAND_NO);
197 enable_ctokens = config_get_boolean_ondemand("plugin:tc", "enable ctokens charts for all interfaces", CONFIG_ONDEMAND_NO);
198 enabled_all_classes_qdiscs = config_get_boolean_ondemand("plugin:tc", "enable show all classes and qdiscs for all interfaces", CONFIG_ONDEMAND_NO);
201 // we only need to add leaf classes
202 struct tc_class *c, *x;
203 unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0, tokens_sum = 0, ctokens_sum = 0;
207 for(c = d->classes ; c ; c = c->next) {
211 if(unlikely(!c->updated))
217 // mark the classes as leafs and parents
218 for(c = d->classes ; c ; c = c->next) {
219 if(unlikely(!c->updated)) continue;
220 if(unlikely(c->isqdisc)) continue;
222 for(x = d->classes ; x ; x = x->next) {
223 if(unlikely(!x->updated)) continue;
224 if(unlikely(c == x)) continue;
225 if(unlikely(x->isqdisc)) continue;
228 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
229 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
230 // 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);
239 if(unlikely(debug_flags & D_TC_LOOP)) {
240 for(c = d->classes ; c ; c = c->next) {
241 if((c->isleaf && c->hasparent) || d->enabled_all_classes_qdiscs) debug(D_TC_LOOP, "TC: Device '%s', class %s, OK", d->name, c->id);
242 else debug(D_TC_LOOP, "TC: Device '%s', class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name?d->name:d->id, c->id, c->isleaf, c->hasparent, c->parentid?c->parentid:"(unset)");
247 // we need the root qdisc
248 for(c = d->classes ; c ; c = c->next) {
249 // 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);
250 debug(D_TC_LOOP, "TC: Device '%s', class '%s', isleaf=%s, isqdisc=%s, hasparent=%s bytes=%d, packtes=%d, dropped=%d, tokens=%d, ctokens=%d", d->name?d->name:d->id, c->name?c->name:c->id, c->isleaf?"true":"false", c->isqdisc?"true":"false", c->hasparent?"true":"false", c->bytes, c->packets, c->dropped, c->tokens, c->ctokens);
251 if(unlikely(c->updated && c->isqdisc && !c->hasparent)) {
253 bytes_sum = c->bytes;
254 packets_sum = c->packets;
255 dropped_sum = c->dropped;
256 tokens_sum = c->tokens;
257 ctokens_sum = c->ctokens;
258 debug(D_TC_LOOP, "TC: found root qdisc. active_qos=%d, bytes_sum=%llu", active_qos, bytes_sum);
263 if(unlikely(!active_qos)) {
264 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
265 tc_device_classes_cleanup(d);
269 if(unlikely(d->enabled == (char)-1)) {
270 char var_name[CONFIG_MAX_NAME + 1];
271 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
272 d->enabled = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
274 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
275 d->enabled_bytes = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
277 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
278 d->enabled_packets = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
280 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
281 d->enabled_dropped = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
283 snprintfz(var_name, CONFIG_MAX_NAME, "tokens chart for %s", d->id);
284 d->enabled_tokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_tokens);
286 snprintfz(var_name, CONFIG_MAX_NAME, "ctokens chart for %s", d->id);
287 d->enabled_ctokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_ctokens);
289 snprintfz(var_name, CONFIG_MAX_NAME, "show all classes for %s", d->id);
290 d->enabled_all_classes_qdiscs = (char)config_get_boolean_ondemand("plugin:tc", var_name, enabled_all_classes_qdiscs);
293 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).",
294 d->name?d->name:d->id,
295 d->enabled, enable_new_interfaces,
296 d->enabled_bytes, enable_bytes,
297 d->enabled_packets, enable_packets,
298 d->enabled_dropped, enable_dropped,
299 d->enabled_tokens, enable_tokens,
300 d->enabled_ctokens, enable_ctokens,
301 d->enabled_all_classes_qdiscs, enabled_all_classes_qdiscs,
309 if(likely(d->enabled)) {
310 // --------------------------------------------------------------------
313 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
314 d->enabled_bytes = CONFIG_ONDEMAND_YES;
316 if(unlikely(!d->st_bytes)) {
317 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
318 if(unlikely(!d->st_bytes)) {
319 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
320 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, RRDSET_TYPE_STACKED);
324 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
325 rrdset_next(d->st_bytes);
327 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
328 rrdset_set_name(d->st_bytes, d->name);
336 for(c = d->classes ; c ; c = c->next) {
337 if(unlikely(!c->updated)) continue;
339 if((c->isleaf && c->hasparent && !c->isqdisc) || d->enabled_all_classes_qdiscs) {
340 if(unlikely(!c->rd_bytes)) {
341 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
342 if(unlikely(!c->rd_bytes)) {
343 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
345 // new class, we have to add it
346 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
348 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
351 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
353 // if it has a name, different to the id
354 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
355 // update the rrd dimension with the new name
356 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
357 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
361 rrdset_done(d->st_bytes);
364 // --------------------------------------------------------------------
367 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
368 d->enabled_packets = CONFIG_ONDEMAND_YES;
370 if(unlikely(!d->st_packets)) {
371 char id[RRD_ID_LENGTH_MAX + 1];
372 char name[RRD_ID_LENGTH_MAX + 1];
373 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
374 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
376 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
377 if(unlikely(!d->st_packets)) {
378 debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
379 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, RRDSET_TYPE_STACKED);
383 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
384 rrdset_next(d->st_packets);
390 for(c = d->classes ; c ; c = c->next) {
391 if(unlikely(!c->updated)) continue;
393 if((c->isleaf && c->hasparent && !c->isqdisc) || d->enabled_all_classes_qdiscs) {
394 if(unlikely(!c->rd_packets)) {
395 c->rd_packets = rrddim_find(d->st_packets, c->id);
396 if(unlikely(!c->rd_packets)) {
397 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
399 // new class, we have to add it
400 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
402 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
405 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
407 // if it has a name, different to the id
408 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
409 // update the rrd dimension with the new name
410 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
411 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
415 rrdset_done(d->st_packets);
418 // --------------------------------------------------------------------
421 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
422 d->enabled_dropped = CONFIG_ONDEMAND_YES;
424 if(unlikely(!d->st_dropped)) {
425 char id[RRD_ID_LENGTH_MAX + 1];
426 char name[RRD_ID_LENGTH_MAX + 1];
427 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
428 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
430 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
431 if(unlikely(!d->st_dropped)) {
432 debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
433 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, RRDSET_TYPE_STACKED);
437 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
438 rrdset_next(d->st_dropped);
444 for(c = d->classes ; c ; c = c->next) {
445 if(unlikely(!c->updated)) continue;
447 if((c->isleaf && c->hasparent && !c->isqdisc) || d->enabled_all_classes_qdiscs) {
448 if(unlikely(!c->rd_dropped)) {
449 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
450 if(unlikely(!c->rd_dropped)) {
451 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
453 // new class, we have to add it
454 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
456 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
459 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
461 // if it has a name, different to the id
462 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
463 // update the rrd dimension with the new name
464 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
465 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
469 rrdset_done(d->st_dropped);
472 // --------------------------------------------------------------------
475 if(d->enabled_tokens == CONFIG_ONDEMAND_YES || (d->enabled_tokens == CONFIG_ONDEMAND_ONDEMAND && tokens_sum)) {
476 d->enabled_tokens = CONFIG_ONDEMAND_YES;
478 if(unlikely(!d->st_tokens)) {
479 char id[RRD_ID_LENGTH_MAX + 1];
480 char name[RRD_ID_LENGTH_MAX + 1];
481 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_tokens", d->id);
482 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
484 d->st_tokens = rrdset_find_bytype(RRD_TYPE_TC, id);
485 if(unlikely(!d->st_tokens)) {
486 debug(D_TC_LOOP, "TC: Creating new _tokens chart for device '%s'", d->name?d->name:d->id);
487 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);
491 debug(D_TC_LOOP, "TC: Updating _tokens chart for device '%s'", d->name?d->name:d->id);
492 rrdset_next(d->st_tokens);
498 for(c = d->classes ; c ; c = c->next) {
499 if(unlikely(!c->updated)) continue;
501 if((c->isleaf && c->hasparent && !c->isqdisc) || d->enabled_all_classes_qdiscs) {
502 if(unlikely(!c->rd_tokens)) {
503 c->rd_tokens = rrddim_find(d->st_tokens, c->id);
504 if(unlikely(!c->rd_tokens)) {
505 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_tokens->id, c->id, c->name);
507 // new class, we have to add it
508 c->rd_tokens = rrddim_add(d->st_tokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
510 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_tokens->id, c->id);
513 rrddim_set_by_pointer(d->st_tokens, c->rd_tokens, c->tokens);
515 // if it has a name, different to the id
516 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
517 // update the rrd dimension with the new name
518 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_tokens->id, c->rd_tokens->id, c->name);
519 rrddim_set_name(d->st_tokens, c->rd_tokens, c->name);
523 rrdset_done(d->st_tokens);
526 // --------------------------------------------------------------------
529 if(d->enabled_ctokens == CONFIG_ONDEMAND_YES || (d->enabled_ctokens == CONFIG_ONDEMAND_ONDEMAND && ctokens_sum)) {
530 d->enabled_ctokens = CONFIG_ONDEMAND_YES;
532 if(unlikely(!d->st_ctokens)) {
533 char id[RRD_ID_LENGTH_MAX + 1];
534 char name[RRD_ID_LENGTH_MAX + 1];
535 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_ctokens", d->id);
536 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
538 d->st_ctokens = rrdset_find_bytype(RRD_TYPE_TC, id);
539 if(unlikely(!d->st_ctokens)) {
540 debug(D_TC_LOOP, "TC: Creating new _ctokens chart for device '%s'", d->name?d->name:d->id);
541 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);
545 debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
546 rrdset_next(d->st_ctokens);
552 for(c = d->classes ; c ; c = c->next) {
553 if(unlikely(!c->updated)) continue;
555 if((c->isleaf && c->hasparent && !c->isqdisc) || d->enabled_all_classes_qdiscs) {
556 if(unlikely(!c->rd_ctokens)) {
557 c->rd_ctokens = rrddim_find(d->st_ctokens, c->id);
558 if(unlikely(!c->rd_ctokens)) {
559 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_ctokens->id, c->id, c->name);
561 // new class, we have to add it
562 c->rd_ctokens = rrddim_add(d->st_ctokens, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_ABSOLUTE);
564 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_ctokens->id, c->id);
567 rrddim_set_by_pointer(d->st_ctokens, c->rd_ctokens, c->ctokens);
569 // if it has a name, different to the id
570 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
571 // update the rrd dimension with the new name
572 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_ctokens->id, c->rd_ctokens->id, c->name);
573 rrddim_set_name(d->st_ctokens, c->rd_ctokens, c->name);
577 rrdset_done(d->st_ctokens);
581 tc_device_classes_cleanup(d);
584 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
586 struct tc_class *c = tc_class_index_find(d, id, 0);
591 if(likely(name && *name && strcmp(c->id, name) != 0)) {
592 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
593 c->name = strdupz(name);
599 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
603 if(likely(name && *name && strcmp(d->id, name) != 0)) {
604 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
605 d->name = strdupz(name);
610 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
614 if(likely(family && *family && strcmp(d->id, family) != 0)) {
615 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
616 d->family = strdupz(family);
617 d->family_updated = 1;
619 // no need for null termination - it is already null
622 static inline struct tc_device *tc_device_create(char *id)
624 struct tc_device *d = tc_device_index_find(id, 0);
627 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
629 d = callocz(1, sizeof(struct tc_device));
632 d->hash = simple_hash(d->id);
633 d->enabled = (char)-1;
635 avl_init(&d->classes_index, tc_class_compare);
636 if(unlikely(tc_device_index_add(d) != d))
637 error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
639 if(!tc_device_root) {
643 d->next = tc_device_root;
644 tc_device_root->prev = d;
652 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char qdisc, char *parentid, char *leafid)
654 struct tc_class *c = tc_class_index_find(n, id, 0);
657 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
659 c = callocz(1, sizeof(struct tc_class));
661 if(n->classes) n->classes->prev = c;
662 c->next = n->classes;
666 c->hash = simple_hash(c->id);
669 if(parentid && *parentid) {
670 c->parentid = strdupz(parentid);
671 c->parent_hash = simple_hash(c->parentid);
674 if(leafid && *leafid) {
675 c->leafid = strdupz(leafid);
676 c->leaf_hash = simple_hash(c->leafid);
679 if(unlikely(tc_class_index_add(n, c) != c))
680 error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
685 static inline void tc_device_free(struct tc_device *n)
687 if(n->next) n->next->prev = n->prev;
688 if(n->prev) n->prev->next = n->next;
689 if(tc_device_root == n) {
690 if(n->next) tc_device_root = n->next;
691 else tc_device_root = n->prev;
694 if(unlikely(tc_device_index_del(n) != n))
695 error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
697 while(n->classes) tc_class_free(n, n->classes);
705 static inline void tc_device_free_all()
707 while(tc_device_root)
708 tc_device_free(tc_device_root);
713 static inline int tc_space(char c) {
726 static inline void tc_split_words(char *str, char **words, int max_words) {
730 // skip all white space
731 while(tc_space(*s)) s++;
733 // store the first word
736 // while we have something
739 if(unlikely(tc_space(*s))) {
741 // terminate the word
744 // skip all white space
745 while(tc_space(*s)) s++;
747 // if we reached the end, stop
750 // store the next word
751 if(i < max_words) words[i++] = s;
757 // terminate the words
758 while(i < max_words) words[i++] = NULL;
761 volatile pid_t tc_child_pid = 0;
762 void *tc_main(void *ptr) {
763 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
765 info("TC thread created with task id %d", gettid());
767 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
768 error("Cannot set pthread cancel type to DEFERRED.");
770 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
771 error("Cannot set pthread cancel state to ENABLE.");
773 struct rusage thread;
774 RRDSET *stcpu = NULL, *sttime = NULL;
776 char buffer[TC_LINE_MAX+1] = "";
777 char *words[MAX_WORDS] = { NULL };
779 uint32_t BEGIN_HASH = simple_hash("BEGIN");
780 uint32_t END_HASH = simple_hash("END");
781 uint32_t QDISC_HASH = simple_hash("qdisc");
782 uint32_t CLASS_HASH = simple_hash("class");
783 uint32_t SENT_HASH = simple_hash("Sent");
784 uint32_t LENDED_HASH = simple_hash("lended:");
785 uint32_t TOKENS_HASH = simple_hash("tokens:");
786 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
787 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
788 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
789 uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
790 #ifdef DETACH_PLUGINS_FROM_NETDATA
791 uint32_t MYPID_HASH = simple_hash("MYPID");
795 snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
796 char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
799 if(unlikely(netdata_exit)) break;
802 struct tc_device *device = NULL;
803 struct tc_class *class = NULL;
805 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
806 debug(D_TC_LOOP, "executing '%s'", buffer);
808 fp = mypopen(buffer, (pid_t *)&tc_child_pid);
810 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
814 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
815 if(unlikely(netdata_exit)) break;
817 buffer[TC_LINE_MAX] = '\0';
818 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
820 tc_split_words(buffer, words, MAX_WORDS);
822 if(unlikely(!words[0] || !*words[0])) {
823 // debug(D_TC_LOOP, "empty line");
826 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
828 first_hash = simple_hash(words[0]);
830 if(unlikely(device && ((first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) || (first_hash == QDISC_HASH && strcmp(words[0], "qdisc") == 0)))) {
831 // 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]);
833 // words[1] : class type
835 // words[3] : parent or root
836 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
837 //char *type = words[1]; // the class: htb, fq_codel, etc
839 // we are only interested for HTB classes
840 //if(strcmp(type, "htb") != 0) continue;
842 char *id = words[2]; // the class major:minor
843 char *parent = words[3]; // 'parent' or 'root'
844 char *parentid = words[4]; // the parent's id
845 char *leaf = words[5]; // 'leaf'
846 char *leafid = words[6]; // leafid
849 if(strcmp(words[0], "qdisc") == 0) {
852 if(strcmp(parent, "root") == 0) {
856 else if(!leaf || strcmp(leaf, "leaf") != 0)
859 char leafbuf[20 + 1] = "";
860 if(leafid && leafid[strlen(leafid) - 1] == ':') {
861 strncpyz(leafbuf, leafid, 20 - 1);
862 strcat(leafbuf, "1");
866 class = tc_class_add(device, id, qdisc, parentid, leafid);
869 // clear the last class
873 else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
874 // debug(D_TC_LOOP, "END line");
877 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
878 error("Cannot set pthread cancel state to DISABLE.");
880 tc_device_commit(device);
881 // tc_device_free(device);
883 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
884 error("Cannot set pthread cancel state to ENABLE.");
890 else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
891 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
893 if(likely(words[1] && *words[1])) {
894 device = tc_device_create(words[1]);
897 // tc_device_free(device);
903 else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
904 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
905 if(likely(words[1] && *words[1])) {
906 class->bytes = str2ull(words[1]);
913 if(likely(words[3] && *words[3]))
914 class->packets = str2ull(words[3]);
916 if(likely(words[6] && *words[6]))
917 class->dropped = str2ull(words[6]);
919 if(likely(words[8] && *words[8]))
920 class->overlimits = str2ull(words[8]);
922 if(likely(words[10] && *words[10]))
923 class->requeues = str2ull(words[8]);
925 else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
926 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
927 if(likely(words[1] && *words[1]))
928 class->lended = str2ull(words[1]);
930 if(likely(words[3] && *words[3]))
931 class->borrowed = str2ull(words[3]);
933 if(likely(words[5] && *words[5]))
934 class->giants = str2ull(words[5]);
936 else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
937 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
938 if(likely(words[1] && *words[1]))
939 class->tokens = str2ull(words[1]);
941 if(likely(words[3] && *words[3]))
942 class->ctokens = str2ull(words[3]);
944 else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
945 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
946 if(likely(words[1] && *words[1]))
947 tc_device_set_device_name(device, words[1]);
949 else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
950 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
951 if(likely(words[1] && *words[1]))
952 tc_device_set_device_family(device, words[1]);
954 else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
955 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
957 char *path = words[2];
958 if(likely(id && *id && path && *path))
959 tc_device_set_class_name(device, id, path);
961 else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
962 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
963 getrusage(RUSAGE_THREAD, &thread);
965 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
966 if(unlikely(!stcpu)) {
967 stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
968 rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
969 rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
971 else rrdset_next(stcpu);
973 rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
974 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
977 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
978 if(unlikely(!sttime)) {
979 sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
980 rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
982 else rrdset_next(sttime);
984 rrddim_set(sttime, "run_time", atoll(words[1]));
988 #ifdef DETACH_PLUGINS_FROM_NETDATA
989 else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
990 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
992 pid_t pid = atol(id);
994 if(likely(pid)) tc_child_pid = pid;
996 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
1000 // debug(D_TC_LOOP, "IGNORED line");
1004 // fgets() failed or loop broke
1005 int code = mypclose(fp, (pid_t)tc_child_pid);
1008 if(unlikely(device)) {
1009 // tc_device_free(device);
1014 if(unlikely(netdata_exit)) {
1015 tc_device_free_all();
1019 if(code == 1 || code == 127) {
1021 // 127 = cannot even run it
1022 error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
1024 tc_device_free_all();
1028 sleep((unsigned int) rrd_update_every);
1032 info("TC thread exiting");
1034 static_thread->enabled = 0;