]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
all required system headers in common.h; some progress on health variables
[netdata.git] / src / plugin_tc.c
1 #include "common.h"
2
3 #define RRD_TYPE_TC                                     "tc"
4 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
5
6 // ----------------------------------------------------------------------------
7 // /sbin/tc processor
8 // this requires the script plugins.d/tc-qos-helper.sh
9
10 #define TC_LINE_MAX 1024
11
12 struct tc_class {
13         avl avl;
14
15         char *id;
16         uint32_t hash;
17
18         char *name;
19
20         char *leafid;
21         uint32_t leaf_hash;
22
23         char *parentid;
24         uint32_t parent_hash;
25
26         char hasparent;
27         char isleaf;
28         unsigned long long bytes;
29         unsigned long long packets;
30         unsigned long long dropped;
31         unsigned long long overlimits;
32         unsigned long long requeues;
33         unsigned long long lended;
34         unsigned long long borrowed;
35         unsigned long long giants;
36         unsigned long long tokens;
37         unsigned long long ctokens;
38
39         RRDDIM *rd_bytes;
40         RRDDIM *rd_packets;
41         RRDDIM *rd_dropped;
42
43         char name_updated;
44         char updated;   // updated bytes
45         int seen;               // seen in the tc list (even without bytes)
46
47         struct tc_class *next;
48         struct tc_class *prev;
49 };
50
51 struct tc_device {
52         avl avl;
53
54         char *id;
55         uint32_t hash;
56
57         char *name;
58         char *family;
59
60         char name_updated;
61         char family_updated;
62
63         char enabled;
64         char enabled_bytes;
65         char enabled_packets;
66         char enabled_dropped;
67
68         RRDSET *st_bytes;
69         RRDSET *st_packets;
70         RRDSET *st_dropped;
71
72         avl_tree classes_index;
73
74         struct tc_class *classes;
75
76         struct tc_device *next;
77         struct tc_device *prev;
78 };
79
80
81 struct tc_device *tc_device_root = NULL;
82
83 // ----------------------------------------------------------------------------
84 // tc_device index
85
86 static int tc_device_compare(void* a, void* b) {
87         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
88         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
89         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
90 }
91
92 avl_tree tc_device_root_index = {
93                 NULL,
94                 tc_device_compare
95 };
96
97 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
98 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
99
100 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
101         struct tc_device tmp;
102         tmp.id = (char *)id;
103         tmp.hash = (hash)?hash:simple_hash(tmp.id);
104
105         return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
106 }
107
108
109 // ----------------------------------------------------------------------------
110 // tc_class index
111
112 static int tc_class_compare(void* a, void* b) {
113         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
114         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
115         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
116 }
117
118 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
119 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
120
121 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
122         struct tc_class tmp;
123         tmp.id = (char *)id;
124         tmp.hash = (hash)?hash:simple_hash(tmp.id);
125
126         return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
127 }
128
129 // ----------------------------------------------------------------------------
130
131 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
132         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);
133
134         if(c->next) c->next->prev = c->prev;
135         if(c->prev) c->prev->next = c->next;
136         if(n->classes == c) {
137                 if(c->next) n->classes = c->next;
138                 else n->classes = c->prev;
139         }
140
141         tc_class_index_del(n, c);
142
143         if(c->id) free(c->id);
144         if(c->name) free(c->name);
145         if(c->leafid) free(c->leafid);
146         if(c->parentid) free(c->parentid);
147
148         free(c);
149 }
150
151 static inline void tc_device_classes_cleanup(struct tc_device *d) {
152         static int cleanup_every = 999;
153
154         if(unlikely(cleanup_every > 0)) {
155                 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
156                 if(cleanup_every < 0) cleanup_every = -cleanup_every;
157         }
158
159         d->name_updated = 0;
160         d->family_updated = 0;
161
162         struct tc_class *c = d->classes;
163         while(c) {
164                 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
165                         struct tc_class *nc = c->next;
166                         tc_class_free(d, c);
167                         c = nc;
168                 }
169                 else {
170                         c->updated = 0;
171                         c->name_updated = 0;
172
173                         c = c->next;
174                 }
175         }
176 }
177
178 static inline void tc_device_commit(struct tc_device *d) {
179         static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1;
180
181         if(unlikely(enable_new_interfaces == -1)) {
182                 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
183                 enable_bytes          = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
184                 enable_packets        = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
185                 enable_dropped        = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
186         }
187
188         // we only need to add leaf classes
189         struct tc_class *c, *x;
190         unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0;
191         int active_classes = 0;
192
193         // set all classes
194         for(c = d->classes ; c ; c = c->next) {
195                 c->isleaf = 1;
196                 c->hasparent = 0;
197         }
198
199         // mark the classes as leafs and parents
200         for(c = d->classes ; c ; c = c->next) {
201                 if(unlikely(!c->updated)) continue;
202
203                 for(x = d->classes ; x ; x = x->next) {
204                         if(unlikely(!x->updated)) continue;
205
206                         if(unlikely(c == x)) continue;
207
208                         if(x->parentid && (
209                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
210                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
211                                 // 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);
212                                 c->isleaf = 0;
213                                 x->hasparent = 1;
214                         }
215                 }
216         }
217
218         // debugging:
219         /*
220         for ( c = d->classes ; c ; c = c->next) {
221                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
222                 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);
223         }
224         */
225
226         // we need at least a class
227         for(c = d->classes ; c ; c = c->next) {
228                 // 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);
229                 if(!c->updated) continue;
230                 if(c->isleaf && c->hasparent) {
231                         active_classes++;
232                         bytes_sum += c->bytes;
233                         packets_sum += c->packets;
234                         dropped_sum += c->dropped;
235                 }
236         }
237
238         if(unlikely(!active_classes)) {
239                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
240                 tc_device_classes_cleanup(d);
241                 return;
242         }
243
244         if(unlikely(d->enabled == -1)) {
245                 char var_name[CONFIG_MAX_NAME + 1];
246                 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
247                 d->enabled         = config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
248
249                 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
250                 d->enabled_bytes   = config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
251
252                 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
253                 d->enabled_packets = config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
254
255                 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
256                 d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
257         }
258
259         if(likely(d->enabled)) {
260                 // --------------------------------------------------------------------
261                 // bytes
262
263                 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
264                         d->enabled_bytes = CONFIG_ONDEMAND_YES;
265
266                         if(unlikely(!d->st_bytes)) {
267                                 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
268                                 if(unlikely(!d->st_bytes)) {
269                                         debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
270                                         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);
271                                 }
272                         }
273                         else {
274                                 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
275                                 rrdset_next_plugins(d->st_bytes);
276
277                                 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
278                                         rrdset_set_name(d->st_bytes, d->name);
279                                         d->name_updated = 0;
280                                 }
281
282                                 // FIXME
283                                 // update the family
284                         }
285
286                         for(c = d->classes ; c ; c = c->next) {
287                                 if(unlikely(!c->updated)) continue;
288
289                                 if(c->isleaf && c->hasparent) {
290                                         c->seen++;
291
292                                         if(unlikely(!c->rd_bytes)) {
293                                                 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
294                                                 if(unlikely(!c->rd_bytes)) {
295                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
296
297                                                         // new class, we have to add it
298                                                         c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
299                                                 }
300                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
301                                         }
302
303                                         rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
304
305                                         // if it has a name, different to the id
306                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
307                                                 // update the rrd dimension with the new name
308                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
309                                                 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
310                                         }
311                                 }
312                         }
313                         rrdset_done(d->st_bytes);
314                 }
315
316                 // --------------------------------------------------------------------
317                 // packets
318                 
319                 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
320                         d->enabled_packets = CONFIG_ONDEMAND_YES;
321
322                         if(unlikely(!d->st_packets)) {
323                                 char id[RRD_ID_LENGTH_MAX + 1];
324                                 char name[RRD_ID_LENGTH_MAX + 1];
325                                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
326                                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
327
328                                 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
329                                 if(unlikely(!d->st_packets)) {
330                                         debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
331                                         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);
332                                 }
333                         }
334                         else {
335                                 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
336                                 rrdset_next_plugins(d->st_packets);
337
338                                 // FIXME
339                                 // update the family
340                         }
341
342                         for(c = d->classes ; c ; c = c->next) {
343                                 if(unlikely(!c->updated)) continue;
344
345                                 if(c->isleaf && c->hasparent) {
346                                         if(unlikely(!c->rd_packets)) {
347                                                 c->rd_packets = rrddim_find(d->st_packets, c->id);
348                                                 if(unlikely(!c->rd_packets)) {
349                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
350
351                                                         // new class, we have to add it
352                                                         c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
353                                                 }
354                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
355                                         }
356
357                                         rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
358
359                                         // if it has a name, different to the id
360                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
361                                                 // update the rrd dimension with the new name
362                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
363                                                 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
364                                         }
365                                 }
366                         }
367                         rrdset_done(d->st_packets);
368                 }
369
370                 // --------------------------------------------------------------------
371                 // dropped
372                 
373                 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
374                         d->enabled_dropped = CONFIG_ONDEMAND_YES;
375                         
376                         if(unlikely(!d->st_dropped)) {
377                                 char id[RRD_ID_LENGTH_MAX + 1];
378                                 char name[RRD_ID_LENGTH_MAX + 1];
379                                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
380                                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
381
382                                 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
383                                 if(unlikely(!d->st_dropped)) {
384                                         debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
385                                         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);
386                                 }
387                         }
388                         else {
389                                 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
390                                 rrdset_next_plugins(d->st_dropped);
391
392                                 // FIXME
393                                 // update the family
394                         }
395
396                         for(c = d->classes ; c ; c = c->next) {
397                                 if(unlikely(!c->updated)) continue;
398
399                                 if(c->isleaf && c->hasparent) {
400                                         if(unlikely(!c->rd_dropped)) {
401                                                 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
402                                                 if(unlikely(!c->rd_dropped)) {
403                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
404
405                                                         // new class, we have to add it
406                                                         c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
407                                                 }
408                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
409                                         }
410
411                                         rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
412
413                                         // if it has a name, different to the id
414                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
415                                                 // update the rrd dimension with the new name
416                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
417                                                 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
418                                         }
419                                 }
420                         }
421                         rrdset_done(d->st_dropped);
422                 }
423         }
424
425         tc_device_classes_cleanup(d);
426 }
427
428 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
429 {
430         struct tc_class *c = tc_class_index_find(d, id, 0);
431         if(likely(c)) {
432                 if(likely(c->name))
433                         free(c->name);
434
435                 c->name = NULL;
436
437                 if(likely(name && *name && strcmp(c->id, name) != 0)) {
438                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
439                         c->name = strdup(name);
440                         if(likely(c->name))
441                                 c->name_updated = 1;
442                 }
443         }
444 }
445
446 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
447         if(likely(d->name))
448                 free(d->name);
449
450         d->name = NULL;
451
452         if(likely(name && *name && strcmp(d->id, name) != 0)) {
453                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
454                 d->name = strdup(name);
455                 if(likely(d->name))
456                         d->name_updated = 1;
457         }
458 }
459
460 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
461         if(likely(d->family))
462                 free(d->family);
463
464         d->family = NULL;
465
466         if(likely(family && *family && strcmp(d->id, family) != 0)) {
467                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
468                 d->family = strdup(family);
469                 if(likely(d->family))
470                         d->family_updated = 1;
471         }
472         // no need for null termination - it is already null
473 }
474
475 static inline struct tc_device *tc_device_create(char *id)
476 {
477         struct tc_device *d = tc_device_index_find(id, 0);
478
479         if(!d) {
480                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
481
482                 d = calloc(1, sizeof(struct tc_device));
483                 if(!d) {
484                         fatal("Cannot allocate memory for tc_device %s", id);
485                         return NULL;
486                 }
487
488                 d->id = strdup(id);
489                 d->hash = simple_hash(d->id);
490                 d->enabled = -1;
491
492                 avl_init(&d->classes_index, tc_class_compare);
493                 tc_device_index_add(d);
494
495                 if(!tc_device_root) {
496                         tc_device_root = d;
497                 }
498                 else {
499                         d->next = tc_device_root;
500                         tc_device_root->prev = d;
501                         tc_device_root = d;
502                 }
503         }
504
505         return(d);
506 }
507
508 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
509 {
510         struct tc_class *c = tc_class_index_find(n, id, 0);
511
512         if(!c) {
513                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
514
515                 c = calloc(1, sizeof(struct tc_class));
516                 if(!c) {
517                         fatal("Cannot allocate memory for tc class");
518                         return NULL;
519                 }
520
521                 if(n->classes) n->classes->prev = c;
522                 c->next = n->classes;
523                 n->classes = c;
524
525                 c->id = strdup(id);
526                 if(!c->id) {
527                         free(c);
528                         return NULL;
529                 }
530                 c->hash = simple_hash(c->id);
531
532                 if(parentid && *parentid) {
533                         c->parentid = strdup(parentid);
534                         c->parent_hash = simple_hash(c->parentid);
535                 }
536
537                 if(leafid && *leafid) {
538                         c->leafid = strdup(leafid);
539                         c->leaf_hash = simple_hash(c->leafid);
540                 }
541
542                 tc_class_index_add(n, c);
543         }
544
545         c->seen = 1;
546
547         return(c);
548 }
549
550 static inline void tc_device_free(struct tc_device *n)
551 {
552         if(n->next) n->next->prev = n->prev;
553         if(n->prev) n->prev->next = n->next;
554         if(tc_device_root == n) {
555                 if(n->next) tc_device_root = n->next;
556                 else tc_device_root = n->prev;
557         }
558
559         tc_device_index_del(n);
560
561         while(n->classes) tc_class_free(n, n->classes);
562
563         if(n->id) free(n->id);
564         if(n->name) free(n->name);
565         if(n->family) free(n->family);
566
567         free(n);
568 }
569
570 static inline void tc_device_free_all()
571 {
572         while(tc_device_root)
573                 tc_device_free(tc_device_root);
574 }
575
576 #define MAX_WORDS 20
577
578 static inline int tc_space(char c) {
579         switch(c) {
580         case ' ':
581         case '\t':
582         case '\r':
583         case '\n':
584                 return 1;
585
586         default:
587                 return 0;
588         }
589 }
590
591 static inline void tc_split_words(char *str, char **words, int max_words) {
592         char *s = str;
593         int i = 0;
594
595         // skip all white space
596         while(tc_space(*s)) s++;
597
598         // store the first word
599         words[i++] = s;
600
601         // while we have something
602         while(*s) {
603                 // if it is a space
604                 if(unlikely(tc_space(*s))) {
605
606                         // terminate the word
607                         *s++ = '\0';
608
609                         // skip all white space
610                         while(tc_space(*s)) s++;
611
612                         // if we reached the end, stop
613                         if(!*s) break;
614
615                         // store the next word
616                         if(i < max_words) words[i++] = s;
617                         else break;
618                 }
619                 else s++;
620         }
621
622         // terminate the words
623         while(i < max_words) words[i++] = NULL;
624 }
625
626 pid_t tc_child_pid = 0;
627 void *tc_main(void *ptr) {
628         (void)ptr;
629
630         info("TC thread created with task id %d", gettid());
631
632         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
633                 error("Cannot set pthread cancel type to DEFERRED.");
634
635         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
636                 error("Cannot set pthread cancel state to ENABLE.");
637
638         struct rusage thread;
639         RRDSET *stcpu = NULL, *sttime = NULL;
640
641         char buffer[TC_LINE_MAX+1] = "";
642         char *words[MAX_WORDS] = { NULL };
643
644         uint32_t BEGIN_HASH = simple_hash("BEGIN");
645         uint32_t END_HASH = simple_hash("END");
646         uint32_t CLASS_HASH = simple_hash("class");
647         uint32_t SENT_HASH = simple_hash("Sent");
648         uint32_t LENDED_HASH = simple_hash("lended:");
649         uint32_t TOKENS_HASH = simple_hash("tokens:");
650         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
651         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
652         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
653         uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
654 #ifdef DETACH_PLUGINS_FROM_NETDATA
655         uint32_t MYPID_HASH = simple_hash("MYPID");
656 #endif
657         uint32_t first_hash;
658
659         snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", config_get("plugins", "plugins directory", PLUGINS_DIR));
660         char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
661         
662         for(;1;) {
663                 if(unlikely(netdata_exit)) break;
664
665                 FILE *fp;
666                 struct tc_device *device = NULL;
667                 struct tc_class *class = NULL;
668
669                 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", tc_script, rrd_update_every);
670                 debug(D_TC_LOOP, "executing '%s'", buffer);
671
672                 fp = mypopen(buffer, &tc_child_pid);
673                 if(unlikely(!fp)) {
674                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
675                         pthread_exit(NULL);
676                         return NULL;
677                 }
678
679                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
680                         if(unlikely(netdata_exit)) break;
681
682                         buffer[TC_LINE_MAX] = '\0';
683                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
684
685                         tc_split_words(buffer, words, MAX_WORDS);
686
687                         if(unlikely(!words[0] || !*words[0])) {
688                                 // debug(D_TC_LOOP, "empty line");
689                                 continue;
690                         }
691                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
692
693                         first_hash = simple_hash(words[0]);
694
695                         if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
696                                 // 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]);
697
698                                 // words[1] : class type
699                                 // words[2] : N:XX
700                                 // words[3] : parent or root
701                                 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
702                                         //char *type     = words[1];  // the class: htb, fq_codel, etc
703
704                                         // we are only interested for HTB classes
705                                         //if(strcmp(type, "htb") != 0) continue;
706
707                                         char *id       = words[2];      // the class major:minor
708                                         char *parent   = words[3];      // 'parent' or 'root'
709                                         char *parentid = words[4];      // the parent's id
710                                         char *leaf     = words[5];      // 'leaf'
711                                         char *leafid   = words[6];      // leafid
712
713                                         if(strcmp(parent, "root") == 0) {
714                                                 parentid = NULL;
715                                                 leafid = NULL;
716                                         }
717                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
718                                                 leafid = NULL;
719
720                                         char leafbuf[20 + 1] = "";
721                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
722                                                 strncpyz(leafbuf, leafid, 20 - 1);
723                                                 strcat(leafbuf, "1");
724                                                 leafid = leafbuf;
725                                         }
726
727                                         class = tc_class_add(device, id, parentid, leafid);
728                                 }
729                                 else {
730                                         // clear the last class
731                                         class = NULL;
732                                 }
733                         }
734                         else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
735                                 // debug(D_TC_LOOP, "END line");
736
737                                 if(likely(device)) {
738                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
739                                                 error("Cannot set pthread cancel state to DISABLE.");
740
741                                         tc_device_commit(device);
742                                         // tc_device_free(device);
743
744                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
745                                                 error("Cannot set pthread cancel state to ENABLE.");
746                                 }
747
748                                 device = NULL;
749                                 class = NULL;
750                         }
751                         else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
752                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
753
754                                 if(likely(words[1] && *words[1])) {
755                                         device = tc_device_create(words[1]);
756                                 }
757                                 else {
758                                         // tc_device_free(device);
759                                         device = NULL;
760                                 }
761
762                                 class = NULL;
763                         }
764                         else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
765                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
766                                 if(likely(words[1] && *words[1])) {
767                                         class->bytes = strtoull(words[1], NULL, 10);
768                                         class->updated = 1;
769                                 }
770                                 else {
771                                         class->updated = 0;
772                                 }
773
774                                 if(likely(words[3] && *words[3]))
775                                         class->packets = strtoull(words[3], NULL, 10);
776
777                                 if(likely(words[6] && *words[6]))
778                                         class->dropped = strtoull(words[6], NULL, 10);
779
780                                 if(likely(words[8] && *words[8]))
781                                         class->overlimits = strtoull(words[8], NULL, 10);
782
783                                 if(likely(words[10] && *words[10]))
784                                         class->requeues = strtoull(words[8], NULL, 10);
785                         }
786                         else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
787                                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
788                                 if(likely(words[1] && *words[1]))
789                                         class->lended = strtoull(words[1], NULL, 10);
790
791                                 if(likely(words[3] && *words[3]))
792                                         class->borrowed = strtoull(words[3], NULL, 10);
793
794                                 if(likely(words[5] && *words[5]))
795                                         class->giants = strtoull(words[5], NULL, 10);
796                         }
797                         else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
798                                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
799                                 if(likely(words[1] && *words[1]))
800                                         class->tokens = strtoull(words[1], NULL, 10);
801
802                                 if(likely(words[3] && *words[3]))
803                                         class->ctokens = strtoull(words[3], NULL, 10);
804                         }
805                         else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
806                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
807                                 if(likely(words[1] && *words[1]))
808                                         tc_device_set_device_name(device, words[1]);
809                         }
810                         else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
811                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
812                                 if(likely(words[1] && *words[1]))
813                                         tc_device_set_device_family(device, words[1]);
814                         }
815                         else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
816                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
817                                 char *id    = words[1];
818                                 char *path  = words[2];
819                                 if(likely(id && *id && path && *path))
820                                         tc_device_set_class_name(device, id, path);
821                         }
822                         else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
823                                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
824                                 getrusage(RUSAGE_THREAD, &thread);
825
826                                 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
827                                 if(unlikely(!stcpu)) {
828                                         stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
829                                         rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
830                                         rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
831                                 }
832                                 else rrdset_next(stcpu);
833
834                                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
835                                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
836                                 rrdset_done(stcpu);
837
838                                 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
839                                 if(unlikely(!sttime)) {
840                                         sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
841                                         rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
842                                 }
843                                 else rrdset_next(sttime);
844
845                                 rrddim_set(sttime, "run_time", atoll(words[1]));
846                                 rrdset_done(sttime);
847
848                         }
849 #ifdef DETACH_PLUGINS_FROM_NETDATA
850                         else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
851                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
852                                 char *id = words[1];
853                                 pid_t pid = atol(id);
854
855                                 if(likely(pid)) tc_child_pid = pid;
856
857                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
858                         }
859 #endif
860                         //else {
861                         //      debug(D_TC_LOOP, "IGNORED line");
862                         //}
863                 }
864
865                 // fgets() failed or loop broke
866                 int code = mypclose(fp, tc_child_pid);
867                 tc_child_pid = 0;
868
869                 if(unlikely(device)) {
870                         // tc_device_free(device);
871                         device = NULL;
872                         class = NULL;
873                 }
874
875                 if(unlikely(netdata_exit)) {
876                         tc_device_free_all();
877                         pthread_exit(NULL);
878                         return NULL;
879                 }
880
881                 if(code == 1 || code == 127) {
882                         // 1 = DISABLE
883                         // 127 = cannot even run it
884                         error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
885
886                         tc_device_free_all();
887                         pthread_exit(NULL);
888                         return NULL;
889                 }
890
891                 sleep((unsigned int) rrd_update_every);
892         }
893
894         pthread_exit(NULL);
895         return NULL;
896 }