]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
log: now is logging program name too; tc: huge optimizations; procfile: optimized...
[netdata.git] / src / plugin_tc.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "avl.h"
7 #include "log.h"
8 #include "common.h"
9 #include "config.h"
10 #include "rrd.h"
11 #include "popen.h"
12 #include "plugin_tc.h"
13
14 #define RRD_TYPE_TC                                     "tc"
15 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
16
17 // ----------------------------------------------------------------------------
18 // /sbin/tc processor
19 // this requires the script plugins.d/tc-qos-helper.sh
20
21 #define TC_LINE_MAX 1024
22
23 struct tc_class {
24         avl avl;
25
26         char *id;
27         uint32_t hash;
28
29         char *name;
30
31         char *leafid;
32         uint32_t leaf_hash;
33
34         char *parentid;
35         uint32_t parent_hash;
36
37         char hasparent;
38         char isleaf;
39         unsigned long long bytes;
40
41         char updated;
42
43         struct tc_class *next;
44 };
45
46 struct tc_device {
47         avl avl;
48
49         char *id;
50         uint32_t hash;
51
52         char *name;
53         char *family;
54
55         avl_tree classes_index;
56
57         struct tc_class *classes;
58 };
59
60
61 // ----------------------------------------------------------------------------
62 // tc_device index
63
64 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
65
66 static int tc_device_compare(void* a, void* b) {
67         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
68         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
69         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
70 }
71
72 avl_tree tc_device_root_index = {
73                 NULL,
74                 tc_device_compare
75 };
76
77 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
78 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
79
80 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
81         struct tc_device *result = NULL, tmp;
82         tmp.id = (char *)id;
83         tmp.hash = (hash)?hash:simple_hash(tmp.id);
84
85         avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
86         return result;
87 }
88
89
90 // ----------------------------------------------------------------------------
91 // tc_class index
92
93 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
94
95 static int tc_class_compare(void* a, void* b) {
96         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
97         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
98         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
99 }
100
101 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
102 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
103
104 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
105         struct tc_class *result = NULL, tmp;
106         tmp.id = (char *)id;
107         tmp.hash = (hash)?hash:simple_hash(tmp.id);
108
109         avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
110         return result;
111 }
112
113 // ----------------------------------------------------------------------------
114
115 static void tc_device_commit(struct tc_device *d)
116 {
117         static int enable_new_interfaces = -1;
118
119         if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
120         
121         // we only need to add leaf classes
122         struct tc_class *c, *x;
123
124         // set them all as leafs without parent
125         for(c = d->classes ; c ; c = c->next) {
126                 c->isleaf = 1;
127                 c->hasparent = 0;
128         }
129
130         // mark the classes as leafs and parents
131         for(c = d->classes ; c ; c = c->next) {
132                 if(!c->updated) continue;
133
134                 for(x = d->classes ; x ; x = x->next) {
135                         if(!x->updated) continue;
136
137                         if(x->parentid && (
138                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
139                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
140                                 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has leaf the class '%s' (parentid: '%s').", d->name, c->name, c->leafid, x->name, x->parentid);
141                                 c->isleaf = 0;
142                                 x->hasparent = 1;
143                         }
144                 }
145         }
146         
147         // debugging:
148         /*
149         for ( c = d->classes ; c ; c = c->next) {
150                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
151                 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);
152         }
153         */
154
155         // we need a device
156         for(c = d->classes ; c ; c = c->next) {
157                 if(!c->updated) continue;
158                 if(c->isleaf && c->hasparent) break;
159         }
160         if(!c) {
161                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
162                 return;
163         }
164
165         char var_name[CONFIG_MAX_NAME + 1];
166         snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
167         if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
168                 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
169                 if(!st) {
170                         debug(D_TC_LOOP, "TC: Committing new TC device '%s'", d->name?d->name:d->id);
171
172                         st = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, "Class Usage", "kilobits/s", 1000, rrd_update_every, RRDSET_TYPE_STACKED);
173
174                         for(c = d->classes ; c ; c = c->next) {
175                                 if(!c->updated) continue;
176
177                                 if(c->isleaf && c->hasparent)
178                                         rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
179                         }
180                 }
181                 else {
182                         rrdset_next_plugins(st);
183
184                         if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
185                 }
186
187                 for(c = d->classes ; c ; c = c->next) {
188                         if(!c->updated) continue;
189
190                         if(c->isleaf && c->hasparent) {
191                                 if(rrddim_set(st, c->id, c->bytes) != 0) {
192                                         
193                                         // new class, we have to add it
194                                         rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
195                                         rrddim_set(st, c->id, c->bytes);
196                                 }
197
198                                 // if it has a name, different to the id
199                                 if(c->name) {
200                                         // update the rrd dimension with the new name
201                                         RRDDIM *rd = rrddim_find(st, c->id);
202                                         if(rd) rrddim_set_name(st, rd, c->name);
203
204                                         free(c->name);
205                                         c->name = NULL;
206                                 }
207                         }
208
209                         c->updated = 0;
210                 }
211                 rrdset_done(st);
212         }
213 }
214
215 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
216 {
217         struct tc_class *c = tc_class_index_find(d, id, 0);
218         if(c) {
219                 if(c->name) free(c->name);
220                 c->name = NULL;
221
222                 if(name && *name && strcmp(c->id, name) != 0) c->name = strdup(name);
223         }
224 }
225
226 static void tc_device_set_device_name(struct tc_device *d, char *name) {
227         if(d->name) free(d->name);
228         d->name = NULL;
229
230         if(name && *name && strcmp(d->id, name) != 0) d->name = strdup(name);
231 }
232
233 static void tc_device_set_device_family(struct tc_device *d, char *family) {
234         if(d->family) free(d->family);
235         d->family = NULL;
236
237         if(family && *family && strcmp(d->id, family) != 0) d->family = strdup(family);
238         // no need for null termination - it is already null
239 }
240
241 static struct tc_device *tc_device_create(char *id)
242 {
243         struct tc_device *d = tc_device_index_find(id, 0);
244
245         if(!d) {
246
247                 d = calloc(1, sizeof(struct tc_device));
248                 if(!d) {
249                         fatal("Cannot allocate memory for tc_device %s", id);
250                         return NULL;
251                 }
252
253                 d->id = strdup(id);
254                 d->hash = simple_hash(d->id);
255
256                 d->classes_index.root = NULL;
257                 d->classes_index.compar = tc_class_compare;
258
259                 tc_device_index_add(d);
260         }
261
262         return(d);
263 }
264
265 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
266 {
267         // fprintf(stderr, "Adding class id '%s', parentid '%s', leafid '%s'\n", id, parentid, leafid);
268
269         struct tc_class *c = tc_class_index_find(n, id, 0);
270         if(!c) {
271                 c = calloc(1, sizeof(struct tc_class));
272                 if(!c) {
273                         fatal("Cannot allocate memory for tc class");
274                         return NULL;
275                 }
276
277                 c->next = n->classes;
278                 n->classes = c;
279
280                 c->id = strdup(id);
281                 if(!c->id) {
282                         free(c);
283                         return NULL;
284                 }
285                 c->hash = simple_hash(c->id);
286
287                 if(parentid && *parentid) {
288                         c->parentid = strdup(parentid);
289                         c->parent_hash = simple_hash(c->parentid);
290                 }
291
292                 if(leafid && *leafid) {
293                         c->leafid = strdup(leafid);
294                         c->leaf_hash = simple_hash(c->leafid);
295                 }
296
297                 tc_class_index_add(n, c);
298         }
299
300         return(c);
301 }
302 /*
303 static void tc_class_free_all(struct tc_device *n, struct tc_class *c)
304 {
305         if(c->next) tc_class_free_all(n, c->next);
306         tc_class_index_del(n, c);
307
308         if(c->id) free(c->id);
309         if(c->name) free(c->name);
310         if(c->leafid) free(c->leafid);
311         if(c->parentid) free(c->parentid);
312
313         free(c);
314 }
315
316 static void tc_device_free(struct tc_device *n)
317 {
318         tc_device_index_del(n);
319         if(n->classes) tc_class_free_all(n, n->classes);
320
321         if(n->id) free(n->id);
322         if(n->name) free(n->name);
323         if(n->family) free(n->family);
324
325         free(n);
326 }
327 */
328
329 #define MAX_WORDS 20
330
331 int tc_space(char c) {
332         switch(c) {
333         case ' ':
334         case '\t':
335         case '\r':
336         case '\n':
337                 return 1;
338
339         default:
340                 return 0;
341         }
342 }
343
344 void tc_split_words(char *str, char **words, int max_words) {
345         char *s = str;
346         int i = 0;
347
348         // skip all white space
349         while(tc_space(*s)) s++;
350
351         // store the first word
352         words[i++] = s;
353
354         // while we have something
355         while(*s) {
356                 // if it is a space
357                 if(tc_space(*s)) {
358
359                         // terminate the word
360                         *s++ = '\0';
361
362                         // skip all white space
363                         while(tc_space(*s)) s++;
364
365                         // if we reached the end, stop
366                         if(!*s) break;
367
368                         // store the next word
369                         if(i < max_words) words[i++] = s;
370                         else break;
371                 }
372                 else s++;
373         }
374
375         // terminate the words
376         while(i < max_words) words[i++] = NULL;
377 }
378
379 pid_t tc_child_pid = 0;
380 void *tc_main(void *ptr)
381 {
382         if(ptr) { ; }
383
384         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
385                 error("Cannot set pthread cancel type to DEFERRED.");
386
387         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
388                 error("Cannot set pthread cancel state to ENABLE.");
389
390         char buffer[TC_LINE_MAX+1] = "";
391         char *words[MAX_WORDS] = { NULL };
392
393         uint32_t BEGIN_HASH = simple_hash("BEGIN");
394         uint32_t END_HASH = simple_hash("END");
395         uint32_t CLASS_HASH = simple_hash("class");
396         uint32_t SENT_HASH = simple_hash("Sent");
397         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
398         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
399         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
400 #ifdef DETACH_PLUGINS_FROM_NETDATA
401         uint32_t MYPID_HASH = simple_hash("MYPID");
402 #endif
403         uint32_t first_hash;
404
405         for(;1;) {
406                 FILE *fp;
407                 struct tc_device *device = NULL;
408                 struct tc_class *class = NULL;
409
410                 snprintf(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), rrd_update_every);
411                 debug(D_TC_LOOP, "executing '%s'", buffer);
412                 // fp = popen(buffer, "r");
413                 fp = mypopen(buffer, &tc_child_pid);
414                 if(!fp) {
415                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
416                         return NULL;
417                 }
418
419                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
420                         buffer[TC_LINE_MAX] = '\0';
421                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
422
423                         tc_split_words(buffer, words, MAX_WORDS);
424                         if(!words[0] || !*words[0]) {
425                                 // debug(D_TC_LOOP, "empty line");
426                                 continue;
427                         }
428                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
429
430                         first_hash = simple_hash(words[0]);
431
432                         if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
433                                 // 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]);
434
435                                 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
436                                         // char *type     = words[1];  // the class: htb, fq_codel, etc
437                                         char *id       = words[2];      // the class major:minor
438                                         char *parent   = words[3];      // 'parent' or 'root'
439                                         char *parentid = words[4];      // the parent's id
440                                         char *leaf     = words[5];      // 'leaf'
441                                         char *leafid   = words[6];      // leafid
442
443                                         if(strcmp(parent, "root") == 0) {
444                                                 parentid = NULL;
445                                                 leafid = NULL;
446                                         }
447                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
448                                                 leafid = NULL;
449
450                                         char leafbuf[20 + 1] = "";
451                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
452                                                 strncpy(leafbuf, leafid, 20 - 1);
453                                                 strcat(leafbuf, "1");
454                                                 leafid = leafbuf;
455                                         }
456
457                                         class = tc_class_add(device, id, parentid, leafid);
458                                 }
459                         }
460                         else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
461                                 // debug(D_TC_LOOP, "END line");
462
463                                 if(device) {
464                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
465                                                 error("Cannot set pthread cancel state to DISABLE.");
466
467                                         tc_device_commit(device);
468                                         // tc_device_free(device);
469                                         device = NULL;
470                                         class = NULL;
471
472                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
473                                                 error("Cannot set pthread cancel state to ENABLE.");
474                                 }
475                         }
476                         else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
477                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
478
479                                 if(device) {
480                                         // tc_device_free(device);
481                                         device = NULL;
482                                         class = NULL;
483                                 }
484
485                                 if(words[1] && *words[1]) {
486                                         device = tc_device_create(words[1]);
487                                         class = NULL;
488                                 }
489                         }
490                         else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
491                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
492                                 if(words[1] && *words[1]) {
493                                         class->bytes = atoll(words[1]);
494                                         class->updated = 1;
495                                 }
496                                 else class->bytes = 0;
497                         }
498                         else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
499                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
500                                 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
501                         }
502                         else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
503                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
504                                 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
505                         }
506                         else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
507                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
508                                 char *id    = words[1];
509                                 char *path  = words[2];
510                                 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
511                         }
512 #ifdef DETACH_PLUGINS_FROM_NETDATA
513                         else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
514                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
515                                 char *id = words[1];
516                                 pid_t pid = atol(id);
517
518                                 if(pid) tc_child_pid = pid;
519
520                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
521                         }
522 #endif
523                         //else {
524                         //      debug(D_TC_LOOP, "IGNORED line");
525                         //}
526                 }
527                 mypclose(fp);
528
529                 if(device) {
530                         // tc_device_free(device);
531                         device = NULL;
532                         class = NULL;
533                 }
534
535                 sleep(rrd_update_every);
536         }
537
538         return NULL;
539 }
540