rrdset.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
  5. debug(D_RRD_CALLS, "Checking read lock on chart '%s'", st->id);
  6. int ret = netdata_rwlock_trywrlock(&st->rrdset_rwlock);
  7. if(ret == 0)
  8. fatal("RRDSET '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
  9. }
  10. void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
  11. debug(D_RRD_CALLS, "Checking write lock on chart '%s'", st->id);
  12. int ret = netdata_rwlock_tryrdlock(&st->rrdset_rwlock);
  13. if(ret == 0)
  14. fatal("RRDSET '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
  15. }
  16. // ----------------------------------------------------------------------------
  17. // RRDSET index
  18. int rrdset_compare(void* a, void* b) {
  19. if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
  20. else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
  21. else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
  22. }
  23. static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
  24. RRDSET tmp;
  25. strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
  26. tmp.hash = (hash)?hash:simple_hash(tmp.id);
  27. return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
  28. }
  29. // ----------------------------------------------------------------------------
  30. // RRDSET name index
  31. #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
  32. int rrdset_compare_name(void* a, void* b) {
  33. RRDSET *A = rrdset_from_avlname(a);
  34. RRDSET *B = rrdset_from_avlname(b);
  35. // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
  36. if(A->hash_name < B->hash_name) return -1;
  37. else if(A->hash_name > B->hash_name) return 1;
  38. else return strcmp(A->name, B->name);
  39. }
  40. RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
  41. void *result;
  42. // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
  43. result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
  44. if(result) return rrdset_from_avlname(result);
  45. return NULL;
  46. }
  47. RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
  48. void *result;
  49. // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
  50. result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
  51. if(result) return rrdset_from_avlname(result);
  52. return NULL;
  53. }
  54. // ----------------------------------------------------------------------------
  55. // RRDSET - find charts
  56. static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
  57. void *result = NULL;
  58. RRDSET tmp;
  59. tmp.name = name;
  60. tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
  61. // fprintf(stderr, "SEARCHING: %s\n", name);
  62. result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
  63. if(result) {
  64. RRDSET *st = rrdset_from_avlname(result);
  65. if(strcmp(st->magic, RRDSET_MAGIC) != 0)
  66. error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
  67. // fprintf(stderr, "FOUND: %s\n", name);
  68. return rrdset_from_avlname(result);
  69. }
  70. // fprintf(stderr, "NOT FOUND: %s\n", name);
  71. return NULL;
  72. }
  73. inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
  74. debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, host->hostname);
  75. RRDSET *st = rrdset_index_find(host, id, 0);
  76. return(st);
  77. }
  78. inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
  79. debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, host->hostname);
  80. char buf[RRD_ID_LENGTH_MAX + 1];
  81. strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
  82. strcat(buf, ".");
  83. int len = (int) strlen(buf);
  84. strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
  85. return(rrdset_find(host, buf));
  86. }
  87. inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
  88. debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, host->hostname);
  89. RRDSET *st = rrdset_index_find_name(host, name, 0);
  90. return(st);
  91. }
  92. // ----------------------------------------------------------------------------
  93. // RRDSET - rename charts
  94. char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
  95. char c, *p = to;
  96. while (length-- && (c = *from++)) {
  97. if(c != '.' && !isalnum(c))
  98. c = '_';
  99. *p++ = c;
  100. }
  101. *p = '\0';
  102. return to;
  103. }
  104. int rrdset_set_name(RRDSET *st, const char *name) {
  105. if(unlikely(st->name && !strcmp(st->name, name)))
  106. return 1;
  107. RRDHOST *host = st->rrdhost;
  108. debug(D_RRD_CALLS, "rrdset_set_name() old: '%s', new: '%s'", st->name?st->name:"", name);
  109. char b[CONFIG_MAX_VALUE + 1];
  110. char n[RRD_ID_LENGTH_MAX + 1];
  111. snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
  112. rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
  113. if(rrdset_index_find_name(host, b, 0)) {
  114. error("RRDSET: chart name '%s' on host '%s' already exists.", b, host->hostname);
  115. return 0;
  116. }
  117. if(st->name) {
  118. rrdset_index_del_name(host, st);
  119. st->name = config_set_default(st->config_section, "name", b);
  120. st->hash_name = simple_hash(st->name);
  121. rrdsetvar_rename_all(st);
  122. }
  123. else {
  124. st->name = config_get(st->config_section, "name", b);
  125. st->hash_name = simple_hash(st->name);
  126. }
  127. rrdset_wrlock(st);
  128. RRDDIM *rd;
  129. rrddim_foreach_write(rd, st)
  130. rrddimvar_rename_all(rd);
  131. rrdset_unlock(st);
  132. if(unlikely(rrdset_index_add_name(host, st) != st))
  133. error("RRDSET: INTERNAL ERROR: attempted to index duplicate chart name '%s'", st->name);
  134. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_SEND);
  135. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_IGNORE);
  136. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  137. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  138. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  139. return 1;
  140. }
  141. inline void rrdset_is_obsolete(RRDSET *st) {
  142. if(unlikely(!(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  143. rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE);
  144. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  145. // the chart will not get more updates (data collection)
  146. // so, we have to push its definition now
  147. rrdset_push_chart_definition_now(st);
  148. }
  149. }
  150. inline void rrdset_isnot_obsolete(RRDSET *st) {
  151. if(unlikely((rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  152. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  153. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  154. // the chart will be pushed upstream automatically
  155. // due to data collection
  156. }
  157. }
  158. inline void rrdset_update_heterogeneous_flag(RRDSET *st) {
  159. RRDHOST *host = st->rrdhost;
  160. (void)host;
  161. RRDDIM *rd;
  162. rrdset_flag_clear(st, RRDSET_FLAG_HOMEGENEOUS_CHECK);
  163. RRD_ALGORITHM algorithm = st->dimensions->algorithm;
  164. collected_number multiplier = abs(st->dimensions->multiplier);
  165. collected_number divisor = abs(st->dimensions->divisor);
  166. rrddim_foreach_read(rd, st) {
  167. if(algorithm != rd->algorithm || multiplier != abs(rd->multiplier) || divisor != abs(rd->divisor)) {
  168. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  169. #ifdef NETDATA_INTERNAL_CHECKS
  170. info("Dimension '%s' added on chart '%s' of host '%s' is not homogeneous to other dimensions already present (algorithm is '%s' vs '%s', multiplier is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ", divisor is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ").",
  171. rd->name,
  172. st->name,
  173. host->hostname,
  174. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm),
  175. rd->multiplier, multiplier,
  176. rd->divisor, divisor
  177. );
  178. #endif
  179. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  180. }
  181. return;
  182. }
  183. }
  184. rrdset_flag_clear(st, RRDSET_FLAG_HETEROGENEOUS);
  185. }
  186. // ----------------------------------------------------------------------------
  187. // RRDSET - reset a chart
  188. void rrdset_reset(RRDSET *st) {
  189. debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
  190. st->last_collected_time.tv_sec = 0;
  191. st->last_collected_time.tv_usec = 0;
  192. st->last_updated.tv_sec = 0;
  193. st->last_updated.tv_usec = 0;
  194. st->current_entry = 0;
  195. st->counter = 0;
  196. st->counter_done = 0;
  197. RRDDIM *rd;
  198. rrddim_foreach_read(rd, st) {
  199. rd->last_collected_time.tv_sec = 0;
  200. rd->last_collected_time.tv_usec = 0;
  201. rd->collections_counter = 0;
  202. // memset(rd->values, 0, rd->entries * sizeof(storage_number));
  203. }
  204. }
  205. // ----------------------------------------------------------------------------
  206. // RRDSET - helpers for rrdset_create()
  207. inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
  208. if(unlikely(entries < 5)) entries = 5;
  209. if(unlikely(entries > RRD_HISTORY_ENTRIES_MAX)) entries = RRD_HISTORY_ENTRIES_MAX;
  210. if(unlikely(mode == RRD_MEMORY_MODE_NONE || mode == RRD_MEMORY_MODE_ALLOC))
  211. return entries;
  212. long page = (size_t)sysconf(_SC_PAGESIZE);
  213. long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
  214. if(unlikely(size % page)) {
  215. size -= (size % page);
  216. size += page;
  217. long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
  218. return n;
  219. }
  220. return entries;
  221. }
  222. static inline void last_collected_time_align(RRDSET *st) {
  223. st->last_collected_time.tv_sec -= st->last_collected_time.tv_sec % st->update_every;
  224. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)))
  225. st->last_collected_time.tv_usec = 0;
  226. else
  227. st->last_collected_time.tv_usec = 500000;
  228. }
  229. static inline void last_updated_time_align(RRDSET *st) {
  230. st->last_updated.tv_sec -= st->last_updated.tv_sec % st->update_every;
  231. st->last_updated.tv_usec = 0;
  232. }
  233. // ----------------------------------------------------------------------------
  234. // RRDSET - free a chart
  235. void rrdset_free(RRDSET *st) {
  236. if(unlikely(!st)) return;
  237. RRDHOST *host = st->rrdhost;
  238. rrdhost_check_wrlock(host); // make sure we have a write lock on the host
  239. rrdset_wrlock(st); // lock this RRDSET
  240. // info("Removing chart '%s' ('%s')", st->id, st->name);
  241. // ------------------------------------------------------------------------
  242. // remove it from the indexes
  243. if(unlikely(rrdset_index_del(host, st) != st))
  244. error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
  245. rrdset_index_del_name(host, st);
  246. // ------------------------------------------------------------------------
  247. // free its children structures
  248. while(st->variables) rrdsetvar_free(st->variables);
  249. while(st->alarms) rrdsetcalc_unlink(st->alarms);
  250. while(st->dimensions) rrddim_free(st, st->dimensions);
  251. rrdfamily_free(host, st->rrdfamily);
  252. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  253. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  254. // ------------------------------------------------------------------------
  255. // unlink it from the host
  256. if(st == host->rrdset_root) {
  257. host->rrdset_root = st->next;
  258. }
  259. else {
  260. // find the previous one
  261. RRDSET *s;
  262. for(s = host->rrdset_root; s && s->next != st ; s = s->next) ;
  263. // bypass it
  264. if(s) s->next = st->next;
  265. else error("Request to free RRDSET '%s': cannot find it under host '%s'", st->id, host->hostname);
  266. }
  267. rrdset_unlock(st);
  268. // ------------------------------------------------------------------------
  269. // free it
  270. netdata_rwlock_destroy(&st->rrdset_rwlock);
  271. // free directly allocated members
  272. freez(st->config_section);
  273. freez(st->plugin_name);
  274. freez(st->module_name);
  275. switch(st->rrd_memory_mode) {
  276. case RRD_MEMORY_MODE_SAVE:
  277. case RRD_MEMORY_MODE_MAP:
  278. case RRD_MEMORY_MODE_RAM:
  279. debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
  280. munmap(st, st->memsize);
  281. break;
  282. case RRD_MEMORY_MODE_ALLOC:
  283. case RRD_MEMORY_MODE_NONE:
  284. freez(st);
  285. break;
  286. }
  287. }
  288. void rrdset_save(RRDSET *st) {
  289. rrdset_check_rdlock(st);
  290. // info("Saving chart '%s' ('%s')", st->id, st->name);
  291. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
  292. debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
  293. memory_file_save(st->cache_filename, st, st->memsize);
  294. }
  295. RRDDIM *rd;
  296. rrddim_foreach_read(rd, st) {
  297. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
  298. debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
  299. memory_file_save(rd->cache_filename, rd, rd->memsize);
  300. }
  301. }
  302. }
  303. void rrdset_delete(RRDSET *st) {
  304. RRDDIM *rd;
  305. rrdset_check_rdlock(st);
  306. info("Deleting chart '%s' ('%s') from disk...", st->id, st->name);
  307. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
  308. info("Deleting chart header file '%s'.", st->cache_filename);
  309. if(unlikely(unlink(st->cache_filename) == -1))
  310. error("Cannot delete chart header file '%s'", st->cache_filename);
  311. }
  312. rrddim_foreach_read(rd, st) {
  313. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  314. info("Deleting dimension file '%s'.", rd->cache_filename);
  315. if(unlikely(unlink(rd->cache_filename) == -1))
  316. error("Cannot delete dimension file '%s'", rd->cache_filename);
  317. }
  318. }
  319. recursively_delete_dir(st->cache_dir, "left-over chart");
  320. }
  321. // ----------------------------------------------------------------------------
  322. // RRDSET - create a chart
  323. static inline RRDSET *rrdset_find_on_create(RRDHOST *host, const char *fullid) {
  324. RRDSET *st = rrdset_find(host, fullid);
  325. if(unlikely(st)) {
  326. rrdset_isnot_obsolete(st);
  327. debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
  328. return st;
  329. }
  330. return NULL;
  331. }
  332. RRDSET *rrdset_create_custom(
  333. RRDHOST *host
  334. , const char *type
  335. , const char *id
  336. , const char *name
  337. , const char *family
  338. , const char *context
  339. , const char *title
  340. , const char *units
  341. , const char *plugin
  342. , const char *module
  343. , long priority
  344. , int update_every
  345. , RRDSET_TYPE chart_type
  346. , RRD_MEMORY_MODE memory_mode
  347. , long history_entries
  348. ) {
  349. if(!type || !type[0]) {
  350. fatal("Cannot create rrd stats without a type: id '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  351. , (id && *id)?id:"<unset>"
  352. , (name && *name)?name:"<unset>"
  353. , (family && *family)?family:"<unset>"
  354. , (context && *context)?context:"<unset>"
  355. , (title && *title)?title:"<unset>"
  356. , (units && *units)?units:"<unset>"
  357. , (plugin && *plugin)?plugin:"<unset>"
  358. , (module && *module)?module:"<unset>"
  359. );
  360. return NULL;
  361. }
  362. if(!id || !id[0]) {
  363. fatal("Cannot create rrd stats without an id: type '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  364. , type
  365. , (name && *name)?name:"<unset>"
  366. , (family && *family)?family:"<unset>"
  367. , (context && *context)?context:"<unset>"
  368. , (title && *title)?title:"<unset>"
  369. , (units && *units)?units:"<unset>"
  370. , (plugin && *plugin)?plugin:"<unset>"
  371. , (module && *module)?module:"<unset>"
  372. );
  373. return NULL;
  374. }
  375. // ------------------------------------------------------------------------
  376. // check if it already exists
  377. char fullid[RRD_ID_LENGTH_MAX + 1];
  378. snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
  379. RRDSET *st = rrdset_find_on_create(host, fullid);
  380. if(st) {
  381. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  382. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  383. return st;
  384. }
  385. rrdhost_wrlock(host);
  386. st = rrdset_find_on_create(host, fullid);
  387. if(st) {
  388. rrdhost_unlock(host);
  389. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  390. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  391. return st;
  392. }
  393. char fullfilename[FILENAME_MAX + 1];
  394. // ------------------------------------------------------------------------
  395. // compose the config_section for this chart
  396. char config_section[RRD_ID_LENGTH_MAX + 1];
  397. if(host == localhost)
  398. strcpy(config_section, fullid);
  399. else
  400. snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
  401. // ------------------------------------------------------------------------
  402. // get the options from the config, we need to create it
  403. long rentries = config_get_number(config_section, "history", history_entries);
  404. long entries = align_entries_to_pagesize(memory_mode, rentries);
  405. if(entries != rentries) entries = config_set_number(config_section, "history", entries);
  406. if(memory_mode == RRD_MEMORY_MODE_NONE && entries != rentries)
  407. entries = config_set_number(config_section, "history", 10);
  408. int enabled = config_get_boolean(config_section, "enabled", 1);
  409. if(!enabled) entries = 5;
  410. unsigned long size = sizeof(RRDSET);
  411. char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
  412. time_t now = now_realtime_sec();
  413. // ------------------------------------------------------------------------
  414. // load it or allocate it
  415. debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
  416. snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
  417. if(memory_mode == RRD_MEMORY_MODE_SAVE || memory_mode == RRD_MEMORY_MODE_MAP || memory_mode == RRD_MEMORY_MODE_RAM) {
  418. st = (RRDSET *) mymmap(
  419. (memory_mode == RRD_MEMORY_MODE_RAM)?NULL:fullfilename
  420. , size
  421. , ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE)
  422. , 0
  423. );
  424. if(st) {
  425. memset(&st->avl, 0, sizeof(avl));
  426. memset(&st->avlname, 0, sizeof(avl));
  427. memset(&st->rrdvar_root_index, 0, sizeof(avl_tree_lock));
  428. memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
  429. memset(&st->rrdset_rwlock, 0, sizeof(netdata_rwlock_t));
  430. st->name = NULL;
  431. st->config_section = NULL;
  432. st->type = NULL;
  433. st->family = NULL;
  434. st->title = NULL;
  435. st->units = NULL;
  436. st->context = NULL;
  437. st->cache_dir = NULL;
  438. st->plugin_name = NULL;
  439. st->module_name = NULL;
  440. st->dimensions = NULL;
  441. st->rrdfamily = NULL;
  442. st->rrdhost = NULL;
  443. st->next = NULL;
  444. st->variables = NULL;
  445. st->alarms = NULL;
  446. st->flags = 0x00000000;
  447. if(memory_mode == RRD_MEMORY_MODE_RAM) {
  448. memset(st, 0, size);
  449. }
  450. else {
  451. if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
  452. info("Initializing file %s.", fullfilename);
  453. memset(st, 0, size);
  454. }
  455. else if(strcmp(st->id, fullid) != 0) {
  456. error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
  457. // munmap(st, size);
  458. // st = NULL;
  459. memset(st, 0, size);
  460. }
  461. else if(st->memsize != size || st->entries != entries) {
  462. error("File %s does not have the desired size. Clearing it.", fullfilename);
  463. memset(st, 0, size);
  464. }
  465. else if(st->update_every != update_every) {
  466. error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
  467. memset(st, 0, size);
  468. }
  469. else if((now - st->last_updated.tv_sec) > update_every * entries) {
  470. error("File %s is too old. Clearing it.", fullfilename);
  471. memset(st, 0, size);
  472. }
  473. else if(st->last_updated.tv_sec > now + update_every) {
  474. error("File %s refers to the future by %zd secs. Resetting it to now.", fullfilename, (ssize_t)(st->last_updated.tv_sec - now));
  475. st->last_updated.tv_sec = now;
  476. }
  477. // make sure the database is aligned
  478. if(st->last_updated.tv_sec) {
  479. st->update_every = update_every;
  480. last_updated_time_align(st);
  481. }
  482. }
  483. // make sure we have the right memory mode
  484. // even if we cleared the memory
  485. st->rrd_memory_mode = memory_mode;
  486. }
  487. }
  488. if(unlikely(!st)) {
  489. st = callocz(1, size);
  490. st->rrd_memory_mode = (memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_ALLOC;
  491. }
  492. st->plugin_name = plugin?strdup(plugin):NULL;
  493. st->module_name = module?strdup(module):NULL;
  494. st->config_section = strdup(config_section);
  495. st->rrdhost = host;
  496. st->memsize = size;
  497. st->entries = entries;
  498. st->update_every = update_every;
  499. if(st->current_entry >= st->entries) st->current_entry = 0;
  500. strcpy(st->cache_filename, fullfilename);
  501. strcpy(st->magic, RRDSET_MAGIC);
  502. strcpy(st->id, fullid);
  503. st->hash = simple_hash(st->id);
  504. st->cache_dir = cache_dir;
  505. st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
  506. st->type = config_get(st->config_section, "type", type);
  507. st->family = config_get(st->config_section, "family", family?family:st->type);
  508. json_fix_string(st->family);
  509. st->units = config_get(st->config_section, "units", units?units:"");
  510. json_fix_string(st->units);
  511. st->context = config_get(st->config_section, "context", context?context:st->id);
  512. json_fix_string(st->context);
  513. st->hash_context = simple_hash(st->context);
  514. st->priority = config_get_number(st->config_section, "priority", priority);
  515. if(enabled)
  516. rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
  517. else
  518. rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
  519. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  520. rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
  521. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  522. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_SEND);
  523. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_IGNORE);
  524. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  525. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  526. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  527. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  528. // if(!strcmp(st->id, "disk_util.dm-0")) {
  529. // st->debug = 1;
  530. // error("enabled debugging for '%s'", st->id);
  531. // }
  532. // else error("not enabled debugging for '%s'", st->id);
  533. st->green = NAN;
  534. st->red = NAN;
  535. st->last_collected_time.tv_sec = 0;
  536. st->last_collected_time.tv_usec = 0;
  537. st->counter_done = 0;
  538. st->gap_when_lost_iterations_above = (int) (gap_when_lost_iterations_above + 2);
  539. st->last_accessed_time = 0;
  540. st->upstream_resync_time = 0;
  541. avl_init_lock(&st->dimensions_index, rrddim_compare);
  542. avl_init_lock(&st->rrdvar_root_index, rrdvar_compare);
  543. netdata_rwlock_init(&st->rrdset_rwlock);
  544. if(name && *name && rrdset_set_name(st, name))
  545. // we did set the name
  546. ;
  547. else
  548. // could not use the name, use the id
  549. rrdset_set_name(st, id);
  550. st->title = config_get(st->config_section, "title", title);
  551. json_fix_string(st->title);
  552. st->rrdfamily = rrdfamily_create(host, st->family);
  553. st->next = host->rrdset_root;
  554. host->rrdset_root = st;
  555. if(host->health_enabled) {
  556. rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  557. rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, RRDVAR_OPTION_DEFAULT);
  558. rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, RRDVAR_OPTION_DEFAULT);
  559. rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, RRDVAR_OPTION_DEFAULT);
  560. rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, RRDVAR_OPTION_DEFAULT);
  561. }
  562. if(unlikely(rrdset_index_add(host, st) != st))
  563. error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
  564. rrdsetcalc_link_matching(st);
  565. rrdcalctemplate_link_matching(st);
  566. rrdhost_cleanup_obsolete_charts(host);
  567. rrdhost_unlock(host);
  568. return(st);
  569. }
  570. // ----------------------------------------------------------------------------
  571. // RRDSET - data collection iteration control
  572. inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
  573. if(unlikely(!st->last_collected_time.tv_sec || !microseconds || (rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK)))) {
  574. // call the full next_usec() function
  575. rrdset_next_usec(st, microseconds);
  576. return;
  577. }
  578. st->usec_since_last_update = microseconds;
  579. }
  580. inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
  581. struct timeval now;
  582. now_realtime_timeval(&now);
  583. #ifdef NETDATA_INTERNAL_CHECKS
  584. char *discard_reason = NULL;
  585. usec_t discarded = microseconds;
  586. #endif
  587. if(unlikely(rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK))) {
  588. // the chart needs to be re-synced to current time
  589. rrdset_flag_clear(st, RRDSET_FLAG_SYNC_CLOCK);
  590. // discard the microseconds supplied
  591. microseconds = 0;
  592. #ifdef NETDATA_INTERNAL_CHECKS
  593. if(!discard_reason) discard_reason = "SYNC CLOCK FLAG";
  594. #endif
  595. }
  596. if(unlikely(!st->last_collected_time.tv_sec)) {
  597. // the first entry
  598. microseconds = st->update_every * USEC_PER_SEC;
  599. #ifdef NETDATA_INTERNAL_CHECKS
  600. if(!discard_reason) discard_reason = "FIRST DATA COLLECTION";
  601. #endif
  602. }
  603. else if(unlikely(!microseconds)) {
  604. // no dt given by the plugin
  605. microseconds = dt_usec(&now, &st->last_collected_time);
  606. #ifdef NETDATA_INTERNAL_CHECKS
  607. if(!discard_reason) discard_reason = "NO USEC GIVEN BY COLLECTOR";
  608. #endif
  609. }
  610. else {
  611. // microseconds has the time since the last collection
  612. susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
  613. if(unlikely(since_last_usec < 0)) {
  614. // oops! the database is in the future
  615. info("RRD database for chart '%s' on host '%s' is %0.5" LONG_DOUBLE_MODIFIER " secs in the future (counter #%zu, update #%zu). Adjusting it to current time.", st->id, st->rrdhost->hostname, (LONG_DOUBLE)-since_last_usec / USEC_PER_SEC, st->counter, st->counter_done);
  616. st->last_collected_time.tv_sec = now.tv_sec - st->update_every;
  617. st->last_collected_time.tv_usec = now.tv_usec;
  618. last_collected_time_align(st);
  619. st->last_updated.tv_sec = now.tv_sec - st->update_every;
  620. st->last_updated.tv_usec = now.tv_usec;
  621. last_updated_time_align(st);
  622. microseconds = st->update_every * USEC_PER_SEC;
  623. #ifdef NETDATA_INTERNAL_CHECKS
  624. if(!discard_reason) discard_reason = "COLLECTION TIME IN FUTURE";
  625. #endif
  626. }
  627. else if(unlikely((usec_t)since_last_usec > (usec_t)(st->update_every * 5 * USEC_PER_SEC))) {
  628. // oops! the database is too far behind
  629. info("RRD database for chart '%s' on host '%s' is %0.5" LONG_DOUBLE_MODIFIER " secs in the past (counter #%zu, update #%zu). Adjusting it to current time.", st->id, st->rrdhost->hostname, (LONG_DOUBLE)since_last_usec / USEC_PER_SEC, st->counter, st->counter_done);
  630. microseconds = (usec_t)since_last_usec;
  631. #ifdef NETDATA_INTERNAL_CHECKS
  632. if(!discard_reason) discard_reason = "COLLECTION TIME TOO FAR IN THE PAST";
  633. #endif
  634. }
  635. #ifdef NETDATA_INTERNAL_CHECKS
  636. if(since_last_usec > 0 && (susec_t)microseconds < since_last_usec) {
  637. static __thread susec_t min_delta = USEC_PER_SEC * 3600, permanent_min_delta = 0;
  638. static __thread time_t last_t = 0;
  639. // the first time initialize it so that it will make the check later
  640. if(last_t == 0) last_t = now.tv_sec + 60;
  641. susec_t delta = since_last_usec - (susec_t)microseconds;
  642. if(delta < min_delta) min_delta = delta;
  643. if(now.tv_sec >= last_t + 60) {
  644. last_t = now.tv_sec;
  645. if(min_delta > permanent_min_delta) {
  646. info("MINIMUM MICROSECONDS DELTA of thread %d increased from %lld to %lld (+%lld)", gettid(), permanent_min_delta, min_delta, min_delta - permanent_min_delta);
  647. permanent_min_delta = min_delta;
  648. }
  649. min_delta = USEC_PER_SEC * 3600;
  650. }
  651. }
  652. #endif
  653. }
  654. #ifdef NETDATA_INTERNAL_CHECKS
  655. debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
  656. rrdset_debug(st, "NEXT: %llu microseconds", microseconds);
  657. if(discarded && discarded != microseconds)
  658. info("host '%s', chart '%s': discarded data collection time of %llu usec, replaced with %llu usec, reason: '%s'", st->rrdhost->hostname, st->id, discarded, microseconds, discard_reason?discard_reason:"UNDEFINED");
  659. #endif
  660. st->usec_since_last_update = microseconds;
  661. }
  662. // ----------------------------------------------------------------------------
  663. // RRDSET - process the collected values for all dimensions of a chart
  664. static inline usec_t rrdset_init_last_collected_time(RRDSET *st) {
  665. now_realtime_timeval(&st->last_collected_time);
  666. last_collected_time_align(st);
  667. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  668. #ifdef NETDATA_INTERNAL_CHECKS
  669. rrdset_debug(st, "initialized last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  670. #endif
  671. return last_collect_ut;
  672. }
  673. static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
  674. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  675. usec_t ut = last_collect_ut + st->usec_since_last_update;
  676. st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
  677. st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
  678. #ifdef NETDATA_INTERNAL_CHECKS
  679. rrdset_debug(st, "updated last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  680. #endif
  681. return last_collect_ut;
  682. }
  683. static inline usec_t rrdset_init_last_updated_time(RRDSET *st) {
  684. // copy the last collected time to last updated time
  685. st->last_updated.tv_sec = st->last_collected_time.tv_sec;
  686. st->last_updated.tv_usec = st->last_collected_time.tv_usec;
  687. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  688. st->last_updated.tv_sec -= st->update_every;
  689. last_updated_time_align(st);
  690. usec_t last_updated_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  691. #ifdef NETDATA_INTERNAL_CHECKS
  692. rrdset_debug(st, "initialized last updated time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_updated_ut / USEC_PER_SEC);
  693. #endif
  694. return last_updated_ut;
  695. }
  696. static inline void rrdset_done_push_exclusive(RRDSET *st) {
  697. // usec_t update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  698. //
  699. // if(unlikely(st->usec_since_last_update > update_every_ut * remote_clock_resync_iterations)) {
  700. // error("Chart '%s' was last collected %llu usec before. Resetting it.", st->id, st->usec_since_last_update);
  701. // rrdset_reset(st);
  702. // st->usec_since_last_update = update_every_ut;
  703. // }
  704. if(unlikely(!st->last_collected_time.tv_sec)) {
  705. // it is the first entry
  706. // set the last_collected_time to now
  707. rrdset_init_last_collected_time(st);
  708. }
  709. else {
  710. // it is not the first entry
  711. // calculate the proper last_collected_time, using usec_since_last_update
  712. rrdset_update_last_collected_time(st);
  713. }
  714. st->counter_done++;
  715. rrdset_rdlock(st);
  716. rrdset_done_push(st);
  717. rrdset_unlock(st);
  718. }
  719. static inline size_t rrdset_done_interpolate(
  720. RRDSET *st
  721. , usec_t update_every_ut
  722. , usec_t last_stored_ut
  723. , usec_t next_store_ut
  724. , usec_t last_collect_ut
  725. , usec_t now_collect_ut
  726. , char store_this_entry
  727. , uint32_t storage_flags
  728. ) {
  729. RRDDIM *rd;
  730. size_t stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
  731. usec_t first_ut = last_stored_ut, last_ut = 0;
  732. (void)first_ut;
  733. ssize_t iterations = (ssize_t)((now_collect_ut - last_stored_ut) / (update_every_ut));
  734. if((now_collect_ut % (update_every_ut)) == 0) iterations++;
  735. size_t counter = st->counter;
  736. long current_entry = st->current_entry;
  737. for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
  738. #ifdef NETDATA_INTERNAL_CHECKS
  739. if(iterations < 0) { error("INTERNAL CHECK: %s: iterations calculation wrapped! first_ut = %llu, last_stored_ut = %llu, next_store_ut = %llu, now_collect_ut = %llu", st->name, first_ut, last_stored_ut, next_store_ut, now_collect_ut); }
  740. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  741. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  742. #endif
  743. last_ut = next_store_ut;
  744. rrddim_foreach_read(rd, st) {
  745. calculated_number new_value;
  746. switch(rd->algorithm) {
  747. case RRD_ALGORITHM_INCREMENTAL:
  748. new_value = (calculated_number)
  749. ( rd->calculated_value
  750. * (calculated_number)(next_store_ut - last_collect_ut)
  751. / (calculated_number)(now_collect_ut - last_collect_ut)
  752. );
  753. #ifdef NETDATA_INTERNAL_CHECKS
  754. rrdset_debug(st, "%s: CALC2 INC "
  755. CALCULATED_NUMBER_FORMAT " = "
  756. CALCULATED_NUMBER_FORMAT
  757. " * (%llu - %llu)"
  758. " / (%llu - %llu)"
  759. , rd->name
  760. , new_value
  761. , rd->calculated_value
  762. , next_store_ut, last_collect_ut
  763. , now_collect_ut, last_collect_ut
  764. );
  765. #endif
  766. rd->calculated_value -= new_value;
  767. new_value += rd->last_calculated_value;
  768. rd->last_calculated_value = 0;
  769. new_value /= (calculated_number)st->update_every;
  770. if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
  771. #ifdef NETDATA_INTERNAL_CHECKS
  772. rrdset_debug(st, "%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
  773. rd->name
  774. , (calculated_number)(next_store_ut - last_stored_ut)
  775. );
  776. #endif
  777. new_value = new_value * (calculated_number)(st->update_every * USEC_PER_SEC) / (calculated_number)(next_store_ut - last_stored_ut);
  778. }
  779. break;
  780. case RRD_ALGORITHM_ABSOLUTE:
  781. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  782. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  783. default:
  784. if(iterations == 1) {
  785. // this is the last iteration
  786. // do not interpolate
  787. // just show the calculated value
  788. new_value = rd->calculated_value;
  789. }
  790. else {
  791. // we have missed an update
  792. // interpolate in the middle values
  793. new_value = (calculated_number)
  794. ( ( (rd->calculated_value - rd->last_calculated_value)
  795. * (calculated_number)(next_store_ut - last_collect_ut)
  796. / (calculated_number)(now_collect_ut - last_collect_ut)
  797. )
  798. + rd->last_calculated_value
  799. );
  800. #ifdef NETDATA_INTERNAL_CHECKS
  801. rrdset_debug(st, "%s: CALC2 DEF "
  802. CALCULATED_NUMBER_FORMAT " = ((("
  803. "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
  804. " * %llu"
  805. " / %llu) + " CALCULATED_NUMBER_FORMAT
  806. , rd->name
  807. , new_value
  808. , rd->calculated_value, rd->last_calculated_value
  809. , (next_store_ut - first_ut)
  810. , (now_collect_ut - first_ut), rd->last_calculated_value
  811. );
  812. #endif
  813. }
  814. break;
  815. }
  816. if(unlikely(!store_this_entry)) {
  817. rd->values[current_entry] = SN_EMPTY_SLOT; //pack_storage_number(0, SN_NOT_EXISTS);
  818. continue;
  819. }
  820. if(likely(rd->updated && rd->collections_counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
  821. rd->values[current_entry] = pack_storage_number(new_value, storage_flags );
  822. rd->last_stored_value = new_value;
  823. #ifdef NETDATA_INTERNAL_CHECKS
  824. rrdset_debug(st, "%s: STORE[%ld] "
  825. CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
  826. , rd->name
  827. , current_entry
  828. , unpack_storage_number(rd->values[current_entry]), new_value
  829. );
  830. #endif
  831. }
  832. else {
  833. #ifdef NETDATA_INTERNAL_CHECKS
  834. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING "
  835. , rd->name
  836. , current_entry
  837. );
  838. #endif
  839. rd->values[current_entry] = SN_EMPTY_SLOT; // pack_storage_number(0, SN_NOT_EXISTS);
  840. rd->last_stored_value = NAN;
  841. }
  842. stored_entries++;
  843. #ifdef NETDATA_INTERNAL_CHECKS
  844. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
  845. calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
  846. calculated_number t2 = unpack_storage_number(rd->values[current_entry]);
  847. calculated_number accuracy = accuracy_loss(t1, t2);
  848. debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
  849. , st->id, rd->name
  850. , current_entry
  851. , t2
  852. , get_storage_number_flags(rd->values[current_entry])
  853. , t1
  854. , accuracy
  855. , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
  856. );
  857. rd->collected_volume += t1;
  858. rd->stored_volume += t2;
  859. accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
  860. debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
  861. , st->id, rd->name
  862. , current_entry
  863. , rd->stored_volume
  864. , rd->collected_volume
  865. , accuracy
  866. , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
  867. );
  868. }
  869. #endif
  870. }
  871. // reset the storage flags for the next point, if any;
  872. storage_flags = SN_EXISTS;
  873. counter++;
  874. current_entry = ((current_entry + 1) >= st->entries) ? 0 : current_entry + 1;
  875. last_stored_ut = next_store_ut;
  876. }
  877. st->counter = counter;
  878. st->current_entry = current_entry;
  879. if(likely(last_ut)) {
  880. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  881. st->last_updated.tv_usec = 0;
  882. }
  883. return stored_entries;
  884. }
  885. static inline void rrdset_done_fill_the_gap(RRDSET *st) {
  886. usec_t update_every_ut = st->update_every * USEC_PER_SEC;
  887. usec_t now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  888. long c = 0, entries = st->entries;
  889. RRDDIM *rd;
  890. rrddim_foreach_read(rd, st) {
  891. usec_t next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  892. long current_entry = st->current_entry;
  893. for(c = 0; c < entries && next_store_ut <= now_collect_ut ; next_store_ut += update_every_ut, c++) {
  894. rd->values[current_entry] = SN_EMPTY_SLOT;
  895. current_entry = ((current_entry + 1) >= entries) ? 0 : current_entry + 1;
  896. #ifdef NETDATA_INTERNAL_CHECKS
  897. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING (FILLED THE GAP)", rd->name, current_entry);
  898. #endif
  899. }
  900. }
  901. if(c > 0) {
  902. c--;
  903. st->last_updated.tv_sec += c * st->update_every;
  904. st->current_entry += c;
  905. if(st->current_entry >= st->entries)
  906. st->current_entry -= st->entries;
  907. }
  908. }
  909. void rrdset_done(RRDSET *st) {
  910. if(unlikely(netdata_exit)) return;
  911. if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
  912. if(unlikely(st->rrdhost->rrdpush_send_enabled))
  913. rrdset_done_push_exclusive(st);
  914. return;
  915. }
  916. debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
  917. RRDDIM *rd;
  918. char
  919. store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
  920. first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
  921. usec_t
  922. last_collect_ut, // the timestamp in microseconds, of the last collected value
  923. now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
  924. last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
  925. next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
  926. update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  927. netdata_thread_disable_cancelability();
  928. // a read lock is OK here
  929. rrdset_rdlock(st);
  930. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))) {
  931. error("Chart '%s' has the OBSOLETE flag set, but it is collected.", st->id);
  932. rrdset_isnot_obsolete(st);
  933. }
  934. // check if the chart has a long time to be updated
  935. if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
  936. info("host '%s', chart %s: took too long to be updated (counter #%zu, update #%zu, %0.3" LONG_DOUBLE_MODIFIER " secs). Resetting it.", st->rrdhost->hostname, st->name, st->counter, st->counter_done, (LONG_DOUBLE)st->usec_since_last_update / USEC_PER_SEC);
  937. rrdset_reset(st);
  938. st->usec_since_last_update = update_every_ut;
  939. store_this_entry = 0;
  940. first_entry = 1;
  941. }
  942. #ifdef NETDATA_INTERNAL_CHECKS
  943. rrdset_debug(st, "microseconds since last update: %llu", st->usec_since_last_update);
  944. #endif
  945. // set last_collected_time
  946. if(unlikely(!st->last_collected_time.tv_sec)) {
  947. // it is the first entry
  948. // set the last_collected_time to now
  949. last_collect_ut = rrdset_init_last_collected_time(st) - update_every_ut;
  950. // the first entry should not be stored
  951. store_this_entry = 0;
  952. first_entry = 1;
  953. }
  954. else {
  955. // it is not the first entry
  956. // calculate the proper last_collected_time, using usec_since_last_update
  957. last_collect_ut = rrdset_update_last_collected_time(st);
  958. }
  959. // if this set has not been updated in the past
  960. // we fake the last_update time to be = now - usec_since_last_update
  961. if(unlikely(!st->last_updated.tv_sec)) {
  962. // it has never been updated before
  963. // set a fake last_updated, in the past using usec_since_last_update
  964. rrdset_init_last_updated_time(st);
  965. // the first entry should not be stored
  966. store_this_entry = 0;
  967. first_entry = 1;
  968. }
  969. // check if we will re-write the entire data set
  970. if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
  971. info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Resetting it. Will not store the next entry.", st->name, st->last_updated.tv_sec, st->last_updated.tv_usec, st->last_collected_time.tv_sec, st->last_collected_time.tv_usec);
  972. rrdset_reset(st);
  973. rrdset_init_last_updated_time(st);
  974. st->usec_since_last_update = update_every_ut;
  975. // the first entry should not be stored
  976. store_this_entry = 0;
  977. first_entry = 1;
  978. }
  979. // these are the 3 variables that will help us in interpolation
  980. // last_stored_ut = the last time we added a value to the storage
  981. // now_collect_ut = the time the current value has been collected
  982. // next_store_ut = the time of the next interpolation point
  983. now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  984. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  985. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  986. if(unlikely(!st->counter_done)) {
  987. // if we have not collected metrics this session (st->counter_done == 0)
  988. // and we have collected metrics for this chart in the past (st->counter != 0)
  989. // fill the gap (the chart has been just loaded from disk)
  990. if(unlikely(st->counter)) {
  991. rrdset_done_fill_the_gap(st);
  992. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  993. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  994. }
  995. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))) {
  996. store_this_entry = 1;
  997. last_collect_ut = next_store_ut - update_every_ut;
  998. #ifdef NETDATA_INTERNAL_CHECKS
  999. rrdset_debug(st, "Fixed first entry.");
  1000. #endif
  1001. }
  1002. else {
  1003. store_this_entry = 0;
  1004. #ifdef NETDATA_INTERNAL_CHECKS
  1005. rrdset_debug(st, "Will not store the next entry.");
  1006. #endif
  1007. }
  1008. }
  1009. st->counter_done++;
  1010. if(unlikely(st->rrdhost->rrdpush_send_enabled))
  1011. rrdset_done_push(st);
  1012. #ifdef NETDATA_INTERNAL_CHECKS
  1013. rrdset_debug(st, "last_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (last collection time)", (LONG_DOUBLE)last_collect_ut/USEC_PER_SEC);
  1014. rrdset_debug(st, "now_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (current collection time)", (LONG_DOUBLE)now_collect_ut/USEC_PER_SEC);
  1015. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  1016. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  1017. #endif
  1018. // calculate totals and count the dimensions
  1019. int dimensions = 0;
  1020. st->collected_total = 0;
  1021. rrddim_foreach_read(rd, st) {
  1022. dimensions++;
  1023. if(likely(rd->updated))
  1024. st->collected_total += rd->collected_value;
  1025. }
  1026. uint32_t storage_flags = SN_EXISTS;
  1027. // process all dimensions to calculate their values
  1028. // based on the collected figures only
  1029. // at this stage we do not interpolate anything
  1030. rrddim_foreach_read(rd, st) {
  1031. if(unlikely(!rd->updated)) {
  1032. rd->calculated_value = 0;
  1033. continue;
  1034. }
  1035. #ifdef NETDATA_INTERNAL_CHECKS
  1036. rrdset_debug(st, "%s: START "
  1037. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1038. " collected_value = " COLLECTED_NUMBER_FORMAT
  1039. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1040. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1041. , rd->name
  1042. , rd->last_collected_value
  1043. , rd->collected_value
  1044. , rd->last_calculated_value
  1045. , rd->calculated_value
  1046. );
  1047. #endif
  1048. switch(rd->algorithm) {
  1049. case RRD_ALGORITHM_ABSOLUTE:
  1050. rd->calculated_value = (calculated_number)rd->collected_value
  1051. * (calculated_number)rd->multiplier
  1052. / (calculated_number)rd->divisor;
  1053. #ifdef NETDATA_INTERNAL_CHECKS
  1054. rrdset_debug(st, "%s: CALC ABS/ABS-NO-IN "
  1055. CALCULATED_NUMBER_FORMAT " = "
  1056. COLLECTED_NUMBER_FORMAT
  1057. " * " CALCULATED_NUMBER_FORMAT
  1058. " / " CALCULATED_NUMBER_FORMAT
  1059. , rd->name
  1060. , rd->calculated_value
  1061. , rd->collected_value
  1062. , (calculated_number)rd->multiplier
  1063. , (calculated_number)rd->divisor
  1064. );
  1065. #endif
  1066. break;
  1067. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1068. if(unlikely(!st->collected_total))
  1069. rd->calculated_value = 0;
  1070. else
  1071. // the percentage of the current value
  1072. // over the total of all dimensions
  1073. rd->calculated_value =
  1074. (calculated_number)100
  1075. * (calculated_number)rd->collected_value
  1076. / (calculated_number)st->collected_total;
  1077. #ifdef NETDATA_INTERNAL_CHECKS
  1078. rrdset_debug(st, "%s: CALC PCENT-ROW "
  1079. CALCULATED_NUMBER_FORMAT " = 100"
  1080. " * " COLLECTED_NUMBER_FORMAT
  1081. " / " COLLECTED_NUMBER_FORMAT
  1082. , rd->name
  1083. , rd->calculated_value
  1084. , rd->collected_value
  1085. , st->collected_total
  1086. );
  1087. #endif
  1088. break;
  1089. case RRD_ALGORITHM_INCREMENTAL:
  1090. if(unlikely(rd->collections_counter <= 1)) {
  1091. rd->calculated_value = 0;
  1092. continue;
  1093. }
  1094. // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1095. // to reset the calculation (it will give zero as the calculation for this second)
  1096. if(unlikely(rd->last_collected_value > rd->collected_value)) {
  1097. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1098. , st->name, rd->name
  1099. , rd->last_collected_value
  1100. , rd->collected_value);
  1101. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1102. storage_flags = SN_EXISTS_RESET;
  1103. rd->last_collected_value = rd->collected_value;
  1104. }
  1105. rd->calculated_value +=
  1106. (calculated_number)(rd->collected_value - rd->last_collected_value)
  1107. * (calculated_number)rd->multiplier
  1108. / (calculated_number)rd->divisor;
  1109. #ifdef NETDATA_INTERNAL_CHECKS
  1110. rrdset_debug(st, "%s: CALC INC PRE "
  1111. CALCULATED_NUMBER_FORMAT " = ("
  1112. COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
  1113. ")"
  1114. " * " CALCULATED_NUMBER_FORMAT
  1115. " / " CALCULATED_NUMBER_FORMAT
  1116. , rd->name
  1117. , rd->calculated_value
  1118. , rd->collected_value, rd->last_collected_value
  1119. , (calculated_number)rd->multiplier
  1120. , (calculated_number)rd->divisor
  1121. );
  1122. #endif
  1123. break;
  1124. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1125. if(unlikely(rd->collections_counter <= 1)) {
  1126. rd->calculated_value = 0;
  1127. continue;
  1128. }
  1129. // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1130. // to reset the calculation (it will give zero as the calculation for this second)
  1131. if(unlikely(rd->last_collected_value > rd->collected_value)) {
  1132. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1133. , st->name, rd->name
  1134. , rd->last_collected_value
  1135. , rd->collected_value
  1136. );
  1137. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1138. storage_flags = SN_EXISTS_RESET;
  1139. rd->last_collected_value = rd->collected_value;
  1140. }
  1141. // the percentage of the current increment
  1142. // over the increment of all dimensions together
  1143. if(unlikely(st->collected_total == st->last_collected_total))
  1144. rd->calculated_value = 0;
  1145. else
  1146. rd->calculated_value =
  1147. (calculated_number)100
  1148. * (calculated_number)(rd->collected_value - rd->last_collected_value)
  1149. / (calculated_number)(st->collected_total - st->last_collected_total);
  1150. #ifdef NETDATA_INTERNAL_CHECKS
  1151. rrdset_debug(st, "%s: CALC PCENT-DIFF "
  1152. CALCULATED_NUMBER_FORMAT " = 100"
  1153. " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1154. " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1155. , rd->name
  1156. , rd->calculated_value
  1157. , rd->collected_value, rd->last_collected_value
  1158. , st->collected_total, st->last_collected_total
  1159. );
  1160. #endif
  1161. break;
  1162. default:
  1163. // make the default zero, to make sure
  1164. // it gets noticed when we add new types
  1165. rd->calculated_value = 0;
  1166. #ifdef NETDATA_INTERNAL_CHECKS
  1167. rrdset_debug(st, "%s: CALC "
  1168. CALCULATED_NUMBER_FORMAT " = 0"
  1169. , rd->name
  1170. , rd->calculated_value
  1171. );
  1172. #endif
  1173. break;
  1174. }
  1175. #ifdef NETDATA_INTERNAL_CHECKS
  1176. rrdset_debug(st, "%s: PHASE2 "
  1177. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1178. " collected_value = " COLLECTED_NUMBER_FORMAT
  1179. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1180. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1181. , rd->name
  1182. , rd->last_collected_value
  1183. , rd->collected_value
  1184. , rd->last_calculated_value
  1185. , rd->calculated_value
  1186. );
  1187. #endif
  1188. }
  1189. // at this point we have all the calculated values ready
  1190. // it is now time to interpolate values on a second boundary
  1191. #ifdef NETDATA_INTERNAL_CHECKS
  1192. if(unlikely(now_collect_ut < next_store_ut)) {
  1193. // this is collected in the same interpolation point
  1194. rrdset_debug(st, "THIS IS IN THE SAME INTERPOLATION POINT");
  1195. info("INTERNAL CHECK: host '%s', chart '%s' is collected in the same interpolation point: short by %llu microseconds", st->rrdhost->hostname, st->name, next_store_ut - now_collect_ut);
  1196. }
  1197. #endif
  1198. rrdset_done_interpolate(st
  1199. , update_every_ut
  1200. , last_stored_ut
  1201. , next_store_ut
  1202. , last_collect_ut
  1203. , now_collect_ut
  1204. , store_this_entry
  1205. , storage_flags
  1206. );
  1207. st->last_collected_total = st->collected_total;
  1208. rrddim_foreach_read(rd, st) {
  1209. if(unlikely(!rd->updated))
  1210. continue;
  1211. #ifdef NETDATA_INTERNAL_CHECKS
  1212. rrdset_debug(st, "%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", rd->name, rd->last_collected_value, rd->collected_value);
  1213. #endif
  1214. rd->last_collected_value = rd->collected_value;
  1215. switch(rd->algorithm) {
  1216. case RRD_ALGORITHM_INCREMENTAL:
  1217. if(unlikely(!first_entry)) {
  1218. #ifdef NETDATA_INTERNAL_CHECKS
  1219. rrdset_debug(st, "%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", rd->name, rd->last_calculated_value + rd->calculated_value, rd->calculated_value);
  1220. #endif
  1221. rd->last_calculated_value += rd->calculated_value;
  1222. }
  1223. else {
  1224. #ifdef NETDATA_INTERNAL_CHECKS
  1225. rrdset_debug(st, "THIS IS THE FIRST POINT");
  1226. #endif
  1227. }
  1228. break;
  1229. case RRD_ALGORITHM_ABSOLUTE:
  1230. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1231. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1232. #ifdef NETDATA_INTERNAL_CHECKS
  1233. rrdset_debug(st, "%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", rd->name, rd->last_calculated_value, rd->calculated_value);
  1234. #endif
  1235. rd->last_calculated_value = rd->calculated_value;
  1236. break;
  1237. }
  1238. rd->calculated_value = 0;
  1239. rd->collected_value = 0;
  1240. rd->updated = 0;
  1241. #ifdef NETDATA_INTERNAL_CHECKS
  1242. rrdset_debug(st, "%s: END "
  1243. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1244. " collected_value = " COLLECTED_NUMBER_FORMAT
  1245. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1246. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1247. , rd->name
  1248. , rd->last_collected_value
  1249. , rd->collected_value
  1250. , rd->last_calculated_value
  1251. , rd->calculated_value
  1252. );
  1253. #endif
  1254. }
  1255. // ALL DONE ABOUT THE DATA UPDATE
  1256. // --------------------------------------------------------------------
  1257. /*
  1258. // find if there are any obsolete dimensions (not updated recently)
  1259. if(unlikely(rrd_delete_unupdated_dimensions)) {
  1260. for( rd = st->dimensions; likely(rd) ; rd = rd->next )
  1261. if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
  1262. break;
  1263. if(unlikely(rd)) {
  1264. RRDDIM *last;
  1265. // there is dimension to free
  1266. // upgrade our read lock to a write lock
  1267. rrdset_unlock(st);
  1268. rrdset_wrlock(st);
  1269. for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
  1270. // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
  1271. if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
  1272. info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
  1273. if(unlikely(!last)) {
  1274. st->dimensions = rd->next;
  1275. rd->next = NULL;
  1276. rrddim_free(st, rd);
  1277. rd = st->dimensions;
  1278. continue;
  1279. }
  1280. else {
  1281. last->next = rd->next;
  1282. rd->next = NULL;
  1283. rrddim_free(st, rd);
  1284. rd = last->next;
  1285. continue;
  1286. }
  1287. }
  1288. last = rd;
  1289. rd = rd->next;
  1290. }
  1291. if(unlikely(!st->dimensions)) {
  1292. info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
  1293. st->enabled = 0;
  1294. }
  1295. }
  1296. }
  1297. */
  1298. rrdset_unlock(st);
  1299. netdata_thread_enable_cancelability();
  1300. }