plugin_tc.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #define RRD_TYPE_TC "tc"
  4. #define PLUGIN_TC_NAME "tc.plugin"
  5. // ----------------------------------------------------------------------------
  6. // /sbin/tc processor
  7. // this requires the script plugins.d/tc-qos-helper.sh
  8. #define TC_LINE_MAX 1024
  9. struct tc_class {
  10. avl_t avl;
  11. char *id;
  12. uint32_t hash;
  13. char *name;
  14. char *leafid;
  15. uint32_t leaf_hash;
  16. char *parentid;
  17. uint32_t parent_hash;
  18. char hasparent;
  19. char isleaf;
  20. char isqdisc;
  21. char render;
  22. unsigned long long bytes;
  23. unsigned long long packets;
  24. unsigned long long dropped;
  25. unsigned long long overlimits;
  26. unsigned long long requeues;
  27. unsigned long long lended;
  28. unsigned long long borrowed;
  29. unsigned long long giants;
  30. unsigned long long tokens;
  31. unsigned long long ctokens;
  32. RRDDIM *rd_bytes;
  33. RRDDIM *rd_packets;
  34. RRDDIM *rd_dropped;
  35. RRDDIM *rd_tokens;
  36. RRDDIM *rd_ctokens;
  37. char name_updated;
  38. char updated; // updated bytes
  39. int unupdated; // the number of times, this has been found un-updated
  40. struct tc_class *next;
  41. struct tc_class *prev;
  42. };
  43. struct tc_device {
  44. avl_t avl;
  45. char *id;
  46. uint32_t hash;
  47. char *name;
  48. char *family;
  49. char name_updated;
  50. char family_updated;
  51. char enabled;
  52. char enabled_bytes;
  53. char enabled_packets;
  54. char enabled_dropped;
  55. char enabled_tokens;
  56. char enabled_ctokens;
  57. char enabled_all_classes_qdiscs;
  58. RRDSET *st_bytes;
  59. RRDSET *st_packets;
  60. RRDSET *st_dropped;
  61. RRDSET *st_tokens;
  62. RRDSET *st_ctokens;
  63. avl_tree_type classes_index;
  64. struct tc_class *classes;
  65. struct tc_class *last_class;
  66. struct tc_device *next;
  67. struct tc_device *prev;
  68. };
  69. struct tc_device *tc_device_root = NULL;
  70. // ----------------------------------------------------------------------------
  71. // tc_device index
  72. static int tc_device_compare(void* a, void* b) {
  73. if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
  74. else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
  75. else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
  76. }
  77. avl_tree_type tc_device_root_index = {
  78. NULL,
  79. tc_device_compare
  80. };
  81. #define tc_device_index_add(st) (struct tc_device *)avl_insert(&tc_device_root_index, (avl_t *)(st))
  82. #define tc_device_index_del(st) (struct tc_device *)avl_remove(&tc_device_root_index, (avl_t *)(st))
  83. static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
  84. struct tc_device tmp;
  85. tmp.id = (char *)id;
  86. tmp.hash = (hash)?hash:simple_hash(tmp.id);
  87. return (struct tc_device *)avl_search(&(tc_device_root_index), (avl_t *)&tmp);
  88. }
  89. // ----------------------------------------------------------------------------
  90. // tc_class index
  91. static int tc_class_compare(void* a, void* b) {
  92. if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
  93. else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
  94. else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
  95. }
  96. #define tc_class_index_add(st, rd) (struct tc_class *)avl_insert(&((st)->classes_index), (avl_t *)(rd))
  97. #define tc_class_index_del(st, rd) (struct tc_class *)avl_remove(&((st)->classes_index), (avl_t *)(rd))
  98. static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
  99. struct tc_class tmp;
  100. tmp.id = (char *)id;
  101. tmp.hash = (hash)?hash:simple_hash(tmp.id);
  102. return (struct tc_class *)avl_search(&(st->classes_index), (avl_t *) &tmp);
  103. }
  104. // ----------------------------------------------------------------------------
  105. static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
  106. if(c == n->classes) {
  107. if(likely(c->next))
  108. n->classes = c->next;
  109. else
  110. n->classes = c->prev;
  111. }
  112. if(c == n->last_class) {
  113. if(unlikely(c->next))
  114. n->last_class = c->next;
  115. else
  116. n->last_class = c->prev;
  117. }
  118. if(c->next) c->next->prev = c->prev;
  119. if(c->prev) c->prev->next = c->next;
  120. 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);
  121. if(unlikely(tc_class_index_del(n, c) != c))
  122. error("plugin_tc: INTERNAL ERROR: attempt remove class '%s' from device '%s': removed a different calls", c->id, n->id);
  123. freez(c->id);
  124. freez(c->name);
  125. freez(c->leafid);
  126. freez(c->parentid);
  127. freez(c);
  128. }
  129. static inline void tc_device_classes_cleanup(struct tc_device *d) {
  130. static int cleanup_every = 999;
  131. if(unlikely(cleanup_every > 0)) {
  132. cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 120);
  133. if(cleanup_every < 0) cleanup_every = -cleanup_every;
  134. }
  135. d->name_updated = 0;
  136. d->family_updated = 0;
  137. struct tc_class *c = d->classes;
  138. while(c) {
  139. if(unlikely(cleanup_every && c->unupdated >= cleanup_every)) {
  140. struct tc_class *nc = c->next;
  141. tc_class_free(d, c);
  142. c = nc;
  143. }
  144. else {
  145. c->updated = 0;
  146. c->name_updated = 0;
  147. c = c->next;
  148. }
  149. }
  150. }
  151. static inline void tc_device_commit(struct tc_device *d) {
  152. 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;
  153. if(unlikely(enable_new_interfaces == -1)) {
  154. enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_BOOLEAN_YES);
  155. enable_bytes = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_BOOLEAN_AUTO);
  156. enable_packets = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_BOOLEAN_AUTO);
  157. enable_dropped = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_BOOLEAN_AUTO);
  158. enable_tokens = config_get_boolean_ondemand("plugin:tc", "enable tokens charts for all interfaces", CONFIG_BOOLEAN_NO);
  159. enable_ctokens = config_get_boolean_ondemand("plugin:tc", "enable ctokens charts for all interfaces", CONFIG_BOOLEAN_NO);
  160. enabled_all_classes_qdiscs = config_get_boolean_ondemand("plugin:tc", "enable show all classes and qdiscs for all interfaces", CONFIG_BOOLEAN_NO);
  161. }
  162. if(unlikely(d->enabled == (char)-1)) {
  163. char var_name[CONFIG_MAX_NAME + 1];
  164. snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
  165. d->enabled = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
  166. snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
  167. d->enabled_bytes = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
  168. snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
  169. d->enabled_packets = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
  170. snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
  171. d->enabled_dropped = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
  172. snprintfz(var_name, CONFIG_MAX_NAME, "tokens chart for %s", d->id);
  173. d->enabled_tokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_tokens);
  174. snprintfz(var_name, CONFIG_MAX_NAME, "ctokens chart for %s", d->id);
  175. d->enabled_ctokens = (char)config_get_boolean_ondemand("plugin:tc", var_name, enable_ctokens);
  176. snprintfz(var_name, CONFIG_MAX_NAME, "show all classes for %s", d->id);
  177. d->enabled_all_classes_qdiscs = (char)config_get_boolean_ondemand("plugin:tc", var_name, enabled_all_classes_qdiscs);
  178. }
  179. // we only need to add leaf classes
  180. struct tc_class *c, *x /*, *root = NULL */;
  181. unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0, tokens_sum = 0, ctokens_sum = 0;
  182. int active_nodes = 0, updated_classes = 0, updated_qdiscs = 0;
  183. // prepare all classes
  184. // we set reasonable defaults for the rest of the code below
  185. for(c = d->classes ; c ; c = c->next) {
  186. c->render = 0; // do not render this class
  187. c->isleaf = 1; // this is a leaf class
  188. c->hasparent = 0; // without a parent
  189. if(unlikely(!c->updated))
  190. c->unupdated++; // increase its unupdated counter
  191. else {
  192. c->unupdated = 0; // reset its unupdated counter
  193. // count how many of each kind
  194. if(c->isqdisc)
  195. updated_qdiscs++;
  196. else
  197. updated_classes++;
  198. }
  199. }
  200. if(unlikely(!d->enabled || (!updated_classes && !updated_qdiscs))) {
  201. debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. It is not enabled/updated.", d->name?d->name:d->id);
  202. tc_device_classes_cleanup(d);
  203. return;
  204. }
  205. if(unlikely(updated_classes && updated_qdiscs)) {
  206. error("TC: device '%s' has active both classes (%d) and qdiscs (%d). Will render only qdiscs.", d->id, updated_classes, updated_qdiscs);
  207. // set all classes to !updated
  208. for(c = d->classes ; c ; c = c->next)
  209. if(unlikely(!c->isqdisc && c->updated))
  210. c->updated = 0;
  211. updated_classes = 0;
  212. }
  213. // mark the classes as leafs and parents
  214. //
  215. // TC is hierarchical:
  216. // - classes can have other classes in them
  217. // - the same is true for qdiscs (i.e. qdiscs have classes, that have other qdiscs)
  218. //
  219. // we need to present a chart with leaf nodes only, so that the sum
  220. // of all dimensions of the chart, will be the total utilization
  221. // of the interface.
  222. //
  223. // here we try to find the ones we need to report
  224. // by default all nodes are marked with: isleaf = 1 (see above)
  225. //
  226. // so, here we remove the isleaf flag from nodes in the middle
  227. // and we add the hasparent flag to leaf nodes we found their parent
  228. if(likely(!d->enabled_all_classes_qdiscs)) {
  229. for(c = d->classes; c; c = c->next) {
  230. if(unlikely(!c->updated)) continue;
  231. //debug(D_TC_LOOP, "TC: In device '%s', %s '%s' has leafid: '%s' and parentid '%s'.",
  232. // d->id,
  233. // c->isqdisc?"qdisc":"class",
  234. // c->id,
  235. // c->leafid?c->leafid:"NULL",
  236. // c->parentid?c->parentid:"NULL");
  237. // find if c is leaf or not
  238. for(x = d->classes; x; x = x->next) {
  239. if(unlikely(!x->updated || c == x || !x->parentid)) continue;
  240. // classes have both parentid and leafid
  241. // qdiscs have only parentid
  242. // the following works for both (it is an OR)
  243. if((c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
  244. (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0)) {
  245. // debug(D_TC_LOOP, "TC: In device '%s', %s '%s' (leafid: '%s') has as leaf %s '%s' (parentid: '%s').", d->name?d->name:d->id, c->isqdisc?"qdisc":"class", c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->isqdisc?"qdisc":"class", x->name?x->name:x->id, x->parentid?x->parentid:x->id);
  246. c->isleaf = 0;
  247. x->hasparent = 1;
  248. }
  249. }
  250. }
  251. }
  252. for(c = d->classes ; c ; c = c->next) {
  253. if(unlikely(!c->updated)) continue;
  254. // debug(D_TC_LOOP, "TC: device '%s', %s '%s' isleaf=%d, hasparent=%d", d->id, (c->isqdisc)?"qdisc":"class", c->id, c->isleaf, c->hasparent);
  255. if(unlikely((c->isleaf && c->hasparent) || d->enabled_all_classes_qdiscs)) {
  256. c->render = 1;
  257. active_nodes++;
  258. bytes_sum += c->bytes;
  259. packets_sum += c->packets;
  260. dropped_sum += c->dropped;
  261. tokens_sum += c->tokens;
  262. ctokens_sum += c->ctokens;
  263. }
  264. //if(unlikely(!c->hasparent)) {
  265. // if(root) error("TC: multiple root class/qdisc for device '%s' (old: '%s', new: '%s')", d->id, root->id, c->id);
  266. // root = c;
  267. // debug(D_TC_LOOP, "TC: found root class/qdisc '%s'", root->id);
  268. //}
  269. }
  270. #ifdef NETDATA_INTERNAL_CHECKS
  271. // dump all the list to see what we know
  272. if(unlikely(debug_flags & D_TC_LOOP)) {
  273. for(c = d->classes ; c ; c = c->next) {
  274. if(c->render) debug(D_TC_LOOP, "TC: final nodes dump for '%s': class %s, OK", d->name, c->id);
  275. else debug(D_TC_LOOP, "TC: final nodes dump for '%s': class %s, IGNORE (updated: %d, isleaf: %d, hasparent: %d, parent: %s)", d->name?d->name:d->id, c->id, c->updated, c->isleaf, c->hasparent, c->parentid?c->parentid:"(unset)");
  276. }
  277. }
  278. #endif
  279. if(unlikely(!active_nodes)) {
  280. debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No useful classes/qdiscs.", d->name?d->name:d->id);
  281. tc_device_classes_cleanup(d);
  282. return;
  283. }
  284. 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).",
  285. d->name?d->name:d->id,
  286. d->enabled, enable_new_interfaces,
  287. d->enabled_bytes, enable_bytes,
  288. d->enabled_packets, enable_packets,
  289. d->enabled_dropped, enable_dropped,
  290. d->enabled_tokens, enable_tokens,
  291. d->enabled_ctokens, enable_ctokens,
  292. d->enabled_all_classes_qdiscs, enabled_all_classes_qdiscs,
  293. bytes_sum,
  294. packets_sum,
  295. dropped_sum,
  296. tokens_sum,
  297. ctokens_sum
  298. );
  299. // --------------------------------------------------------------------
  300. // bytes
  301. if(d->enabled_bytes == CONFIG_BOOLEAN_YES || (d->enabled_bytes == CONFIG_BOOLEAN_AUTO &&
  302. (bytes_sum ||
  303. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  304. d->enabled_bytes = CONFIG_BOOLEAN_YES;
  305. if(unlikely(!d->st_bytes))
  306. d->st_bytes = rrdset_create_localhost(
  307. RRD_TYPE_TC
  308. , d->id
  309. , d->name ? d->name : d->id
  310. , d->family ? d->family : d->id
  311. , RRD_TYPE_TC ".qos"
  312. , "Class Usage"
  313. , "kilobits/s"
  314. , PLUGIN_TC_NAME
  315. , NULL
  316. , NETDATA_CHART_PRIO_TC_QOS
  317. , localhost->rrd_update_every
  318. , d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED
  319. );
  320. else {
  321. rrdset_next(d->st_bytes);
  322. if(unlikely(d->name_updated)) rrdset_set_name(d->st_bytes, d->name);
  323. // TODO
  324. // update the family
  325. }
  326. for(c = d->classes ; c ; c = c->next) {
  327. if(unlikely(!c->render)) continue;
  328. if(unlikely(!c->rd_bytes))
  329. c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
  330. else if(unlikely(c->name_updated))
  331. rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
  332. rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
  333. }
  334. rrdset_done(d->st_bytes);
  335. }
  336. // --------------------------------------------------------------------
  337. // packets
  338. if(d->enabled_packets == CONFIG_BOOLEAN_YES || (d->enabled_packets == CONFIG_BOOLEAN_AUTO &&
  339. (packets_sum ||
  340. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  341. d->enabled_packets = CONFIG_BOOLEAN_YES;
  342. if(unlikely(!d->st_packets)) {
  343. char id[RRD_ID_LENGTH_MAX + 1];
  344. char name[RRD_ID_LENGTH_MAX + 1];
  345. snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
  346. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
  347. d->st_packets = rrdset_create_localhost(
  348. RRD_TYPE_TC
  349. , id
  350. , name
  351. , d->family ? d->family : d->id
  352. , RRD_TYPE_TC ".qos_packets"
  353. , "Class Packets"
  354. , "packets/s"
  355. , PLUGIN_TC_NAME
  356. , NULL
  357. , NETDATA_CHART_PRIO_TC_QOS_PACKETS
  358. , localhost->rrd_update_every
  359. , d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED
  360. );
  361. }
  362. else {
  363. rrdset_next(d->st_packets);
  364. if(unlikely(d->name_updated)) {
  365. char name[RRD_ID_LENGTH_MAX + 1];
  366. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
  367. rrdset_set_name(d->st_packets, name);
  368. }
  369. // TODO
  370. // update the family
  371. }
  372. for(c = d->classes ; c ; c = c->next) {
  373. if(unlikely(!c->render)) continue;
  374. if(unlikely(!c->rd_packets))
  375. c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  376. else if(unlikely(c->name_updated))
  377. rrddim_set_name(d->st_packets, c->rd_packets, c->name);
  378. rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
  379. }
  380. rrdset_done(d->st_packets);
  381. }
  382. // --------------------------------------------------------------------
  383. // dropped
  384. if(d->enabled_dropped == CONFIG_BOOLEAN_YES || (d->enabled_dropped == CONFIG_BOOLEAN_AUTO &&
  385. (dropped_sum ||
  386. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  387. d->enabled_dropped = CONFIG_BOOLEAN_YES;
  388. if(unlikely(!d->st_dropped)) {
  389. char id[RRD_ID_LENGTH_MAX + 1];
  390. char name[RRD_ID_LENGTH_MAX + 1];
  391. snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
  392. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
  393. d->st_dropped = rrdset_create_localhost(
  394. RRD_TYPE_TC
  395. , id
  396. , name
  397. , d->family ? d->family : d->id
  398. , RRD_TYPE_TC ".qos_dropped"
  399. , "Class Dropped Packets"
  400. , "packets/s"
  401. , PLUGIN_TC_NAME
  402. , NULL
  403. , NETDATA_CHART_PRIO_TC_QOS_DROPPED
  404. , localhost->rrd_update_every
  405. , d->enabled_all_classes_qdiscs ? RRDSET_TYPE_LINE : RRDSET_TYPE_STACKED
  406. );
  407. }
  408. else {
  409. rrdset_next(d->st_dropped);
  410. if(unlikely(d->name_updated)) {
  411. char name[RRD_ID_LENGTH_MAX + 1];
  412. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
  413. rrdset_set_name(d->st_dropped, name);
  414. }
  415. // TODO
  416. // update the family
  417. }
  418. for(c = d->classes ; c ; c = c->next) {
  419. if(unlikely(!c->render)) continue;
  420. if(unlikely(!c->rd_dropped))
  421. c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  422. else if(unlikely(c->name_updated))
  423. rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
  424. rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
  425. }
  426. rrdset_done(d->st_dropped);
  427. }
  428. // --------------------------------------------------------------------
  429. // tokens
  430. if(d->enabled_tokens == CONFIG_BOOLEAN_YES || (d->enabled_tokens == CONFIG_BOOLEAN_AUTO &&
  431. (tokens_sum ||
  432. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  433. d->enabled_tokens = CONFIG_BOOLEAN_YES;
  434. if(unlikely(!d->st_tokens)) {
  435. char id[RRD_ID_LENGTH_MAX + 1];
  436. char name[RRD_ID_LENGTH_MAX + 1];
  437. snprintfz(id, RRD_ID_LENGTH_MAX, "%s_tokens", d->id);
  438. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
  439. d->st_tokens = rrdset_create_localhost(
  440. RRD_TYPE_TC
  441. , id
  442. , name
  443. , d->family ? d->family : d->id
  444. , RRD_TYPE_TC ".qos_tokens"
  445. , "Class Tokens"
  446. , "tokens"
  447. , PLUGIN_TC_NAME
  448. , NULL
  449. , NETDATA_CHART_PRIO_TC_QOS_TOKENS
  450. , localhost->rrd_update_every
  451. , RRDSET_TYPE_LINE
  452. );
  453. }
  454. else {
  455. rrdset_next(d->st_tokens);
  456. if(unlikely(d->name_updated)) {
  457. char name[RRD_ID_LENGTH_MAX + 1];
  458. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_tokens", d->name?d->name:d->id);
  459. rrdset_set_name(d->st_tokens, name);
  460. }
  461. // TODO
  462. // update the family
  463. }
  464. for(c = d->classes ; c ; c = c->next) {
  465. if(unlikely(!c->render)) continue;
  466. if(unlikely(!c->rd_tokens)) {
  467. c->rd_tokens = rrddim_add(d->st_tokens, c->id, c->name?c->name:c->id, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  468. }
  469. else if(unlikely(c->name_updated))
  470. rrddim_set_name(d->st_tokens, c->rd_tokens, c->name);
  471. rrddim_set_by_pointer(d->st_tokens, c->rd_tokens, c->tokens);
  472. }
  473. rrdset_done(d->st_tokens);
  474. }
  475. // --------------------------------------------------------------------
  476. // ctokens
  477. if(d->enabled_ctokens == CONFIG_BOOLEAN_YES || (d->enabled_ctokens == CONFIG_BOOLEAN_AUTO &&
  478. (ctokens_sum ||
  479. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  480. d->enabled_ctokens = CONFIG_BOOLEAN_YES;
  481. if(unlikely(!d->st_ctokens)) {
  482. char id[RRD_ID_LENGTH_MAX + 1];
  483. char name[RRD_ID_LENGTH_MAX + 1];
  484. snprintfz(id, RRD_ID_LENGTH_MAX, "%s_ctokens", d->id);
  485. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
  486. d->st_ctokens = rrdset_create_localhost(
  487. RRD_TYPE_TC
  488. , id
  489. , name
  490. , d->family ? d->family : d->id
  491. , RRD_TYPE_TC ".qos_ctokens"
  492. , "Class cTokens"
  493. , "ctokens"
  494. , PLUGIN_TC_NAME
  495. , NULL
  496. , NETDATA_CHART_PRIO_TC_QOS_CTOKENS
  497. , localhost->rrd_update_every
  498. , RRDSET_TYPE_LINE
  499. );
  500. }
  501. else {
  502. debug(D_TC_LOOP, "TC: Updating _ctokens chart for device '%s'", d->name?d->name:d->id);
  503. rrdset_next(d->st_ctokens);
  504. if(unlikely(d->name_updated)) {
  505. char name[RRD_ID_LENGTH_MAX + 1];
  506. snprintfz(name, RRD_ID_LENGTH_MAX, "%s_ctokens", d->name?d->name:d->id);
  507. rrdset_set_name(d->st_ctokens, name);
  508. }
  509. // TODO
  510. // update the family
  511. }
  512. for(c = d->classes ; c ; c = c->next) {
  513. if(unlikely(!c->render)) continue;
  514. if(unlikely(!c->rd_ctokens))
  515. c->rd_ctokens = rrddim_add(d->st_ctokens, c->id, c->name?c->name:c->id, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  516. else if(unlikely(c->name_updated))
  517. rrddim_set_name(d->st_ctokens, c->rd_ctokens, c->name);
  518. rrddim_set_by_pointer(d->st_ctokens, c->rd_ctokens, c->ctokens);
  519. }
  520. rrdset_done(d->st_ctokens);
  521. }
  522. tc_device_classes_cleanup(d);
  523. }
  524. static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name) {
  525. if(unlikely(!name || !*name)) return;
  526. struct tc_class *c = tc_class_index_find(d, id, 0);
  527. if(likely(c)) {
  528. if(likely(c->name)) {
  529. if(!strcmp(c->name, name)) return;
  530. freez(c->name);
  531. c->name = NULL;
  532. }
  533. if(likely(name && *name && strcmp(c->id, name) != 0)) {
  534. debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
  535. c->name = strdupz(name);
  536. c->name_updated = 1;
  537. }
  538. }
  539. }
  540. static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
  541. if(unlikely(!name || !*name)) return;
  542. if(d->name) {
  543. if(!strcmp(d->name, name)) return;
  544. freez(d->name);
  545. d->name = NULL;
  546. }
  547. if(likely(name && *name && strcmp(d->id, name) != 0)) {
  548. debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
  549. d->name = strdupz(name);
  550. d->name_updated = 1;
  551. }
  552. }
  553. static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
  554. freez(d->family);
  555. d->family = NULL;
  556. if(likely(family && *family && strcmp(d->id, family) != 0)) {
  557. debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
  558. d->family = strdupz(family);
  559. d->family_updated = 1;
  560. }
  561. // no need for null termination - it is already null
  562. }
  563. static inline struct tc_device *tc_device_create(char *id)
  564. {
  565. struct tc_device *d = tc_device_index_find(id, 0);
  566. if(!d) {
  567. debug(D_TC_LOOP, "TC: Creating device '%s'", id);
  568. d = callocz(1, sizeof(struct tc_device));
  569. d->id = strdupz(id);
  570. d->hash = simple_hash(d->id);
  571. d->enabled = (char)-1;
  572. avl_init(&d->classes_index, tc_class_compare);
  573. if(unlikely(tc_device_index_add(d) != d))
  574. error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", d->id);
  575. if(!tc_device_root) {
  576. tc_device_root = d;
  577. }
  578. else {
  579. d->next = tc_device_root;
  580. tc_device_root->prev = d;
  581. tc_device_root = d;
  582. }
  583. }
  584. return(d);
  585. }
  586. static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char qdisc, char *parentid, char *leafid)
  587. {
  588. struct tc_class *c = tc_class_index_find(n, id, 0);
  589. if(!c) {
  590. debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
  591. c = callocz(1, sizeof(struct tc_class));
  592. if(unlikely(!n->classes))
  593. n->classes = c;
  594. else if(likely(n->last_class)) {
  595. n->last_class->next = c;
  596. c->prev = n->last_class;
  597. }
  598. n->last_class = c;
  599. c->id = strdupz(id);
  600. c->hash = simple_hash(c->id);
  601. c->isqdisc = qdisc;
  602. if(parentid && *parentid) {
  603. c->parentid = strdupz(parentid);
  604. c->parent_hash = simple_hash(c->parentid);
  605. }
  606. if(leafid && *leafid) {
  607. c->leafid = strdupz(leafid);
  608. c->leaf_hash = simple_hash(c->leafid);
  609. }
  610. if(unlikely(tc_class_index_add(n, c) != c))
  611. error("plugin_tc: INTERNAL ERROR: attempt index class '%s' on device '%s': already exists", c->id, n->id);
  612. }
  613. return(c);
  614. }
  615. static inline void tc_device_free(struct tc_device *n)
  616. {
  617. if(n->next) n->next->prev = n->prev;
  618. if(n->prev) n->prev->next = n->next;
  619. if(tc_device_root == n) {
  620. if(n->next) tc_device_root = n->next;
  621. else tc_device_root = n->prev;
  622. }
  623. if(unlikely(tc_device_index_del(n) != n))
  624. error("plugin_tc: INTERNAL ERROR: removing device '%s' removed a different device.", n->id);
  625. while(n->classes) tc_class_free(n, n->classes);
  626. freez(n->id);
  627. freez(n->name);
  628. freez(n->family);
  629. freez(n);
  630. }
  631. static inline void tc_device_free_all()
  632. {
  633. while(tc_device_root)
  634. tc_device_free(tc_device_root);
  635. }
  636. #define PLUGINSD_MAX_WORDS 20
  637. static inline int tc_space(char c) {
  638. switch(c) {
  639. case ' ':
  640. case '\t':
  641. case '\r':
  642. case '\n':
  643. return 1;
  644. default:
  645. return 0;
  646. }
  647. }
  648. static inline void tc_split_words(char *str, char **words, int max_words) {
  649. char *s = str;
  650. int i = 0;
  651. // skip all white space
  652. while(tc_space(*s)) s++;
  653. // store the first word
  654. words[i++] = s;
  655. // while we have something
  656. while(*s) {
  657. // if it is a space
  658. if(unlikely(tc_space(*s))) {
  659. // terminate the word
  660. *s++ = '\0';
  661. // skip all white space
  662. while(tc_space(*s)) s++;
  663. // if we reached the end, stop
  664. if(!*s) break;
  665. // store the next word
  666. if(i < max_words) words[i++] = s;
  667. else break;
  668. }
  669. else s++;
  670. }
  671. // terminate the words
  672. while(i < max_words) words[i++] = NULL;
  673. }
  674. static pid_t tc_child_pid = 0;
  675. static void tc_main_cleanup(void *ptr) {
  676. worker_unregister();
  677. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  678. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  679. info("cleaning up...");
  680. if(tc_child_pid) {
  681. info("TC: killing with SIGTERM tc-qos-helper process %d", tc_child_pid);
  682. if(killpid(tc_child_pid) != -1) {
  683. siginfo_t info;
  684. info("TC: waiting for tc plugin child process pid %d to exit...", tc_child_pid);
  685. waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
  686. }
  687. tc_child_pid = 0;
  688. }
  689. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  690. }
  691. #define WORKER_TC_CLASS 0
  692. #define WORKER_TC_BEGIN 1
  693. #define WORKER_TC_END 2
  694. #define WORKER_TC_SENT 3
  695. #define WORKER_TC_LENDED 4
  696. #define WORKER_TC_TOKENS 5
  697. #define WORKER_TC_SETDEVICENAME 6
  698. #define WORKER_TC_SETDEVICEGROUP 7
  699. #define WORKER_TC_SETCLASSNAME 8
  700. #define WORKER_TC_WORKTIME 9
  701. #if WORKER_UTILIZATION_MAX_JOB_TYPES < 10
  702. #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 10
  703. #endif
  704. void *tc_main(void *ptr) {
  705. worker_register("TC");
  706. worker_register_job_name(WORKER_TC_CLASS, "class");
  707. worker_register_job_name(WORKER_TC_BEGIN, "begin");
  708. worker_register_job_name(WORKER_TC_END, "end");
  709. worker_register_job_name(WORKER_TC_SENT, "sent");
  710. worker_register_job_name(WORKER_TC_LENDED, "lended");
  711. worker_register_job_name(WORKER_TC_TOKENS, "tokens");
  712. worker_register_job_name(WORKER_TC_SETDEVICENAME, "devicename");
  713. worker_register_job_name(WORKER_TC_SETDEVICEGROUP, "devicegroup");
  714. worker_register_job_name(WORKER_TC_SETCLASSNAME, "classname");
  715. worker_register_job_name(WORKER_TC_WORKTIME, "worktime");
  716. netdata_thread_cleanup_push(tc_main_cleanup, ptr);
  717. char command[FILENAME_MAX + 1];
  718. char *words[PLUGINSD_MAX_WORDS] = { NULL };
  719. uint32_t BEGIN_HASH = simple_hash("BEGIN");
  720. uint32_t END_HASH = simple_hash("END");
  721. uint32_t QDISC_HASH = simple_hash("qdisc");
  722. uint32_t CLASS_HASH = simple_hash("class");
  723. uint32_t SENT_HASH = simple_hash("Sent");
  724. uint32_t LENDED_HASH = simple_hash("lended:");
  725. uint32_t TOKENS_HASH = simple_hash("tokens:");
  726. uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
  727. uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
  728. uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
  729. uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
  730. uint32_t first_hash;
  731. snprintfz(command, TC_LINE_MAX, "%s/tc-qos-helper.sh", netdata_configured_primary_plugins_dir);
  732. char *tc_script = config_get("plugin:tc", "script to run to get tc values", command);
  733. while(!netdata_exit) {
  734. FILE *fp;
  735. struct tc_device *device = NULL;
  736. struct tc_class *class = NULL;
  737. snprintfz(command, TC_LINE_MAX, "exec %s %d", tc_script, localhost->rrd_update_every);
  738. debug(D_TC_LOOP, "executing '%s'", command);
  739. fp = mypopen(command, (pid_t *)&tc_child_pid);
  740. if(unlikely(!fp)) {
  741. error("TC: Cannot popen(\"%s\", \"r\").", command);
  742. goto cleanup;
  743. }
  744. char buffer[TC_LINE_MAX+1] = "";
  745. while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
  746. if(unlikely(netdata_exit)) break;
  747. buffer[TC_LINE_MAX] = '\0';
  748. // debug(D_TC_LOOP, "TC: read '%s'", buffer);
  749. tc_split_words(buffer, words, PLUGINSD_MAX_WORDS);
  750. if(unlikely(!words[0] || !*words[0])) {
  751. // debug(D_TC_LOOP, "empty line");
  752. worker_is_idle();
  753. continue;
  754. }
  755. // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
  756. first_hash = simple_hash(words[0]);
  757. if(unlikely(device && ((first_hash == CLASS_HASH && strcmp(words[0], "class") == 0) || (first_hash == QDISC_HASH && strcmp(words[0], "qdisc") == 0)))) {
  758. worker_is_busy(WORKER_TC_CLASS);
  759. // 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]);
  760. char *type = words[1]; // the class/qdisc type: htb, fq_codel, etc
  761. char *id = words[2]; // the class/qdisc major:minor
  762. char *parent = words[3]; // the word 'parent' or 'root'
  763. char *parentid = words[4]; // parentid
  764. char *leaf = words[5]; // the word 'leaf'
  765. char *leafid = words[6]; // leafid
  766. int parent_is_root = 0;
  767. int parent_is_parent = 0;
  768. if(likely(parent)) {
  769. parent_is_parent = !strcmp(parent, "parent");
  770. if(!parent_is_parent)
  771. parent_is_root = !strcmp(parent, "root");
  772. }
  773. if(likely(type && id && (parent_is_root || parent_is_parent))) {
  774. char qdisc = 0;
  775. if(first_hash == QDISC_HASH) {
  776. qdisc = 1;
  777. if(!strcmp(type, "ingress")) {
  778. // we don't want to get the ingress qdisc
  779. // there should be an IFB interface for this
  780. class = NULL;
  781. worker_is_idle();
  782. continue;
  783. }
  784. if(parent_is_parent && parentid) {
  785. // eliminate the minor number from parentid
  786. // why: parentid is the id of the parent class
  787. // but major: is also the id of the parent qdisc
  788. char *s = parentid;
  789. while(*s && *s != ':') s++;
  790. if(*s == ':') s[1] = '\0';
  791. }
  792. }
  793. if(parent_is_root) {
  794. parentid = NULL;
  795. leafid = NULL;
  796. }
  797. else if(!leaf || strcmp(leaf, "leaf") != 0)
  798. leafid = NULL;
  799. char leafbuf[20 + 1] = "";
  800. if(leafid && leafid[strlen(leafid) - 1] == ':') {
  801. strncpyz(leafbuf, leafid, 20 - 1);
  802. strcat(leafbuf, "1");
  803. leafid = leafbuf;
  804. }
  805. class = tc_class_add(device, id, qdisc, parentid, leafid);
  806. }
  807. else {
  808. // clear the last class
  809. class = NULL;
  810. }
  811. }
  812. else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
  813. worker_is_busy(WORKER_TC_END);
  814. // debug(D_TC_LOOP, "END line");
  815. if(likely(device)) {
  816. netdata_thread_disable_cancelability();
  817. tc_device_commit(device);
  818. // tc_device_free(device);
  819. netdata_thread_enable_cancelability();
  820. }
  821. device = NULL;
  822. class = NULL;
  823. }
  824. else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
  825. worker_is_busy(WORKER_TC_BEGIN);
  826. // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
  827. if(likely(words[1] && *words[1])) {
  828. device = tc_device_create(words[1]);
  829. }
  830. else {
  831. // tc_device_free(device);
  832. device = NULL;
  833. }
  834. class = NULL;
  835. }
  836. else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
  837. worker_is_busy(WORKER_TC_SENT);
  838. // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
  839. if(likely(words[1] && *words[1])) {
  840. class->bytes = str2ull(words[1]);
  841. class->updated = 1;
  842. }
  843. else {
  844. class->updated = 0;
  845. }
  846. if(likely(words[3] && *words[3]))
  847. class->packets = str2ull(words[3]);
  848. if(likely(words[6] && *words[6]))
  849. class->dropped = str2ull(words[6]);
  850. if(likely(words[8] && *words[8]))
  851. class->overlimits = str2ull(words[8]);
  852. if(likely(words[10] && *words[10]))
  853. class->requeues = str2ull(words[8]);
  854. }
  855. else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
  856. worker_is_busy(WORKER_TC_LENDED);
  857. // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
  858. if(likely(words[1] && *words[1]))
  859. class->lended = str2ull(words[1]);
  860. if(likely(words[3] && *words[3]))
  861. class->borrowed = str2ull(words[3]);
  862. if(likely(words[5] && *words[5]))
  863. class->giants = str2ull(words[5]);
  864. }
  865. else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
  866. worker_is_busy(WORKER_TC_TOKENS);
  867. // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
  868. if(likely(words[1] && *words[1]))
  869. class->tokens = str2ull(words[1]);
  870. if(likely(words[3] && *words[3]))
  871. class->ctokens = str2ull(words[3]);
  872. }
  873. else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
  874. worker_is_busy(WORKER_TC_SETDEVICENAME);
  875. // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
  876. if(likely(words[1] && *words[1]))
  877. tc_device_set_device_name(device, words[1]);
  878. }
  879. else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
  880. worker_is_busy(WORKER_TC_SETDEVICEGROUP);
  881. // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
  882. if(likely(words[1] && *words[1]))
  883. tc_device_set_device_family(device, words[1]);
  884. }
  885. else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
  886. worker_is_busy(WORKER_TC_SETCLASSNAME);
  887. // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
  888. char *id = words[1];
  889. char *path = words[2];
  890. if(likely(id && *id && path && *path))
  891. tc_device_set_class_name(device, id, path);
  892. }
  893. else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
  894. worker_is_busy(WORKER_TC_WORKTIME);
  895. // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
  896. static RRDSET *sttime = NULL;
  897. static RRDDIM *rd_run_time = NULL;
  898. if(unlikely(!sttime)) {
  899. sttime = rrdset_create_localhost(
  900. "netdata"
  901. , "plugin_tc_time"
  902. , NULL
  903. , "workers plugin tc"
  904. , "netdata.workers.tc.script_time"
  905. , "Netdata TC script execution"
  906. , "milliseconds/run"
  907. , PLUGIN_TC_NAME
  908. , NULL
  909. , NETDATA_CHART_PRIO_NETDATA_TC_TIME
  910. , localhost->rrd_update_every
  911. , RRDSET_TYPE_AREA
  912. );
  913. rd_run_time = rrddim_add(sttime, "run_time", "run time", 1, 1, RRD_ALGORITHM_ABSOLUTE);
  914. }
  915. else rrdset_next(sttime);
  916. rrddim_set_by_pointer(sttime, rd_run_time, str2ll(words[1], NULL));
  917. rrdset_done(sttime);
  918. }
  919. //else {
  920. // debug(D_TC_LOOP, "IGNORED line");
  921. //}
  922. worker_is_idle();
  923. }
  924. // fgets() failed or loop broke
  925. int code = mypclose(fp, (pid_t)tc_child_pid);
  926. tc_child_pid = 0;
  927. if(unlikely(device)) {
  928. // tc_device_free(device);
  929. device = NULL;
  930. class = NULL;
  931. }
  932. if(unlikely(netdata_exit)) {
  933. tc_device_free_all();
  934. goto cleanup;
  935. }
  936. if(code == 1 || code == 127) {
  937. // 1 = DISABLE
  938. // 127 = cannot even run it
  939. error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
  940. tc_device_free_all();
  941. goto cleanup;
  942. }
  943. sleep((unsigned int) localhost->rrd_update_every);
  944. }
  945. cleanup: ; // added semi-colon to prevent older gcc error: label at end of compound statement
  946. worker_unregister();
  947. netdata_thread_cleanup_pop(1);
  948. return NULL;
  949. }