@cryptotaxi247 / netdata-1 / commits / e2d46de1e

TC plugin now cleanups properly; added TC packets and dropped packets charts

Costa Tsaousis committed Jul 17, 2016 at 15:07 UTC e2d46de1ecb414f526657181bb3322e34e71aab1
1 file changed +198 -60
src/plugin_tc.c
+198 -60
@@ -53,8 +53,13 @@ struct tc_class {
53 unsigned long long tokens;
54 unsigned long long ctokens;
55
56 + RRDDIM *rd_bytes;
57 + RRDDIM *rd_packets;
58 + RRDDIM *rd_dropped;
59 +
60 + char name_updated;
61 char updated; // updated bytes
57 - char seen; // seen in the tc list (even without bytes)
62 + int seen; // seen in the tc list (even without bytes)
63
64 struct tc_class *next;
65 struct tc_class *prev;
@@ -69,6 +74,14 @@ struct tc_device {
74 char *name;
75 char *family;
76
77 + char name_updated;
78 + char family_updated;
79 +
80 + char enabled;
81 + RRDSET *st_bytes;
82 + RRDSET *st_packets;
83 + RRDSET *st_dropped;
84 +
85 avl_tree classes_index;
86
87 struct tc_class *classes;
@@ -151,30 +164,31 @@ static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
164 static inline void tc_device_classes_cleanup(struct tc_device *d) {
165 static int cleanup_every = 999;
166
154 - if(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 - if(cleanup_every == 0) cleanup_every = -1;
167 + if(unlikely(cleanup_every > 0)) {
168 + cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
169 + if(cleanup_every < 0) cleanup_every = -cleanup_every;
170 }
171
172 + d->name_updated = 0;
173 + d->family_updated = 0;
174 +
175 struct tc_class *c = d->classes;
176 while(c) {
162 - if(c->seen < cleanup_every) {
177 + if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
178 struct tc_class *nc = c->next;
179 tc_class_free(d, c);
180 c = nc;
181 }
167 - else c = c->next;
168 -
169 - if(c) {
182 + else {
183 c->updated = 0;
171 - c->seen--;
184 + c->name_updated = 0;
185 +
186 + c = c->next;
187 }
188 }
189 }
190
176 -static inline void tc_device_commit(struct tc_device *d)
177 -{
191 +static inline void tc_device_commit(struct tc_device *d) {
192 static int enable_new_interfaces = -1;
193
194 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
@@ -227,57 +241,168 @@ static inline void tc_device_commit(struct tc_device *d)
241 return;
242 }
243
230 - char var_name[CONFIG_MAX_NAME + 1];
231 - snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
232 - if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
233 - RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
234 - if(!st) {
235 - debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
236 -
237 - st = 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);
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("plugin:tc", var_name, enable_new_interfaces);
248 + }
249
239 - for(c = d->classes ; c ; c = c->next) {
240 - if(!c->updated) continue;
250 + if(likely(d->enabled)) {
251 + // --------------------------------------------------------------------
252 + // bytes
253
242 - if(c->isleaf && c->hasparent)
243 - rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
254 + if(unlikely(!d->st_bytes)) {
255 + d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
256 + if(unlikely(!d->st_bytes)) {
257 + debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
258 + 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);
259 }
260 }
261 else {
262 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
248 - rrdset_next_plugins(st);
263 + rrdset_next_plugins(d->st_bytes);
264
250 - if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
265 + if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
266 + rrdset_set_name(d->st_bytes, d->name);
267 + d->name_updated = 0;
268 + }
269 +
270 + // FIXME
271 + // update the family
272 }
273
274 for(c = d->classes ; c ; c = c->next) {
254 - if(!c->updated) continue;
275 + if(unlikely(!c->updated)) continue;
276 +
277 + c->seen++;
278
279 if(c->isleaf && c->hasparent) {
257 - RRDDIM *rd = rrddim_find(st, c->id);
280 + if(unlikely(!c->rd_bytes)) {
281 + c->rd_bytes = rrddim_find(d->st_bytes, c->id);
282 + if(unlikely(!c->rd_bytes)) {
283 + debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
284 +
285 + // new class, we have to add it
286 + c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
287 + }
288 + else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
289 + }
290
259 - if(!rd) {
260 - debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", st->id, c->id, c->name);
291 + rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
292
262 - // new class, we have to add it
263 - rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
293 + // if it has a name, different to the id
294 + if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
295 + // update the rrd dimension with the new name
296 + debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
297 + rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
298 }
265 - else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
299 + }
300 + }
301 + rrdset_done(d->st_bytes);
302 +
303 + // --------------------------------------------------------------------
304 + // packets
305 +
306 + if(unlikely(!d->st_packets)) {
307 + char id[RRD_ID_LENGTH_MAX + 1];
308 + char name[RRD_ID_LENGTH_MAX + 1];
309 + snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
310 + snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
311 +
312 + d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
313 + if(unlikely(!d->st_packets)) {
314 + debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
315 + 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);
316 + }
317 + }
318 + else {
319 + debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
320 + rrdset_next_plugins(d->st_packets);
321
267 - rrddim_set_by_pointer(st, rd, c->bytes);
322 + // FIXME
323 + // update the family
324 + }
325 +
326 + for(c = d->classes ; c ; c = c->next) {
327 + if(unlikely(!c->updated)) continue;
328 +
329 + c->seen++;
330 +
331 + if(c->isleaf && c->hasparent) {
332 + if(unlikely(!c->rd_packets)) {
333 + c->rd_packets = rrddim_find(d->st_packets, c->id);
334 + if(unlikely(!c->rd_packets)) {
335 + debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
336 +
337 + // new class, we have to add it
338 + c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
339 + }
340 + else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
341 + }
342 +
343 + rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
344
345 // if it has a name, different to the id
270 - if(c->name) {
346 + if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
347 // update the rrd dimension with the new name
272 - debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
273 - rrddim_set_name(st, rd, c->name);
348 + debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
349 + rrddim_set_name(d->st_packets, c->rd_packets, c->name);
350 + }
351 + }
352 + }
353 + rrdset_done(d->st_packets);
354 +
355 + // --------------------------------------------------------------------
356 + // dropped
357 +
358 + if(unlikely(!d->st_dropped)) {
359 + char id[RRD_ID_LENGTH_MAX + 1];
360 + char name[RRD_ID_LENGTH_MAX + 1];
361 + snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
362 + snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
363 +
364 + d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
365 + if(unlikely(!d->st_dropped)) {
366 + debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
367 + 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);
368 + }
369 + }
370 + else {
371 + debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
372 + rrdset_next_plugins(d->st_dropped);
373
275 - free(c->name);
276 - c->name = NULL;
374 + // FIXME
375 + // update the family
376 + }
377 +
378 + for(c = d->classes ; c ; c = c->next) {
379 + if(unlikely(!c->updated)) continue;
380 +
381 + c->seen++;
382 +
383 + if(c->isleaf && c->hasparent) {
384 + if(unlikely(!c->rd_dropped)) {
385 + c->rd_dropped = rrddim_find(d->st_dropped, c->id);
386 + if(unlikely(!c->rd_dropped)) {
387 + debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
388 +
389 + // new class, we have to add it
390 + c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
391 + }
392 + else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
393 + }
394 +
395 + rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
396 +
397 + // if it has a name, different to the id
398 + if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
399 + // update the rrd dimension with the new name
400 + debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
401 + rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
402 }
403 }
404 }
280 - rrdset_done(st);
405 + rrdset_done(d->st_dropped);
406 }
407
408 tc_device_classes_cleanup(d);
@@ -286,34 +411,46 @@ static inline void tc_device_commit(struct tc_device *d)
411 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
412 {
413 struct tc_class *c = tc_class_index_find(d, id, 0);
289 - if(c) {
290 - if(c->name) free(c->name);
414 + if(likely(c)) {
415 + if(likely(c->name))
416 + free(c->name);
417 +
418 c->name = NULL;
419
293 - if(name && *name && strcmp(c->id, name) != 0) {
420 + if(likely(name && *name && strcmp(c->id, name) != 0)) {
421 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
422 c->name = strdup(name);
423 + if(likely(c->name))
424 + c->name_updated = 1;
425 }
426 }
427 }
428
429 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
301 - if(d->name) free(d->name);
430 + if(likely(d->name))
431 + free(d->name);
432 +
433 d->name = NULL;
434
304 - if(name && *name && strcmp(d->id, name) != 0) {
435 + if(likely(name && *name && strcmp(d->id, name) != 0)) {
436 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
437 d->name = strdup(name);
438 + if(likely(d->name))
439 + d->name_updated = 1;
440 }
441 }
442
443 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
311 - if(d->family) free(d->family);
444 + if(likely(d->family))
445 + free(d->family);
446 +
447 d->family = NULL;
448
314 - if(family && *family && strcmp(d->id, family) != 0) {
449 + if(likely(family && *family && strcmp(d->id, family) != 0)) {
450 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
451 d->family = strdup(family);
452 + if(likely(d->family))
453 + d->family_updated = 1;
454 }
455 // no need for null termination - it is already null
456 }
@@ -333,6 +470,7 @@ static inline struct tc_device *tc_device_create(char *id)
470
471 d->id = strdup(id);
472 d->hash = simple_hash(d->id);
473 + d->enabled = -1;
474
475 avl_init(&d->classes_index, tc_class_compare);
476 tc_device_index_add(d);
@@ -446,7 +584,7 @@ static inline void tc_split_words(char *str, char **words, int max_words) {
584 // while we have something
585 while(*s) {
586 // if it is a space
449 - if(tc_space(*s)) {
587 + if(unlikely(tc_space(*s))) {
588
589 // terminate the word
590 *s++ = '\0';
@@ -526,7 +664,7 @@ void *tc_main(void *ptr)
664 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
665
666 tc_split_words(buffer, words, MAX_WORDS);
529 - if(!words[0] || !*words[0]) {
667 + if(unlikely(!words[0] || !*words[0])) {
668 // debug(D_TC_LOOP, "empty line");
669 continue;
670 }
@@ -534,7 +672,7 @@ void *tc_main(void *ptr)
672
673 first_hash = simple_hash(words[0]);
674
537 - if(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) {
675 + if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
676 // 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]);
677
678 // clear the last class
@@ -572,7 +710,7 @@ void *tc_main(void *ptr)
710 class = tc_class_add(device, id, parentid, leafid);
711 }
712 }
575 - else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
713 + else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
714 // debug(D_TC_LOOP, "END line");
715
716 if(device) {
@@ -588,7 +726,7 @@ void *tc_main(void *ptr)
726 error("Cannot set pthread cancel state to ENABLE.");
727 }
728 }
591 - else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
729 + else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
730 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
731
732 if(device) {
@@ -602,7 +740,7 @@ void *tc_main(void *ptr)
740 class = NULL;
741 }
742 }
605 - else if(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0) {
743 + else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
744 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
745 if(words[1] && *words[1]) {
746 class->bytes = strtoull(words[1], NULL, 10);
@@ -621,7 +759,7 @@ void *tc_main(void *ptr)
759 if(words[10] && *words[10])
760 class->requeues = strtoull(words[8], NULL, 10);
761 }
624 - else if(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0) {
762 + else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
763 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
764 if(words[1] && *words[1])
765 class->lended = strtoull(words[1], NULL, 10);
@@ -632,7 +770,7 @@ void *tc_main(void *ptr)
770 if(words[5] && *words[5])
771 class->giants = strtoull(words[5], NULL, 10);
772 }
635 - else if(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0) {
773 + else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
774 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
775 if(words[1] && *words[1])
776 class->tokens = strtoull(words[1], NULL, 10);
@@ -640,21 +778,21 @@ void *tc_main(void *ptr)
778 if(words[3] && *words[3])
779 class->ctokens = strtoull(words[3], NULL, 10);
780 }
643 - else if(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0) {
781 + else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
782 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
783 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
784 }
647 - else if(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0) {
785 + else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
786 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
787 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
788 }
651 - else if(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0) {
789 + else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
790 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
791 char *id = words[1];
792 char *path = words[2];
793 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
794 }
657 - else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
795 + else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
796 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
797 getrusage(RUSAGE_THREAD, &thread);
798
@@ -682,7 +820,7 @@ void *tc_main(void *ptr)
820
821 }
822 #ifdef DETACH_PLUGINS_FROM_NETDATA
685 - else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
823 + else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
824 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
825 char *id = words[1];
826 pid_t pid = atol(id);