rrdset.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. info("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_HOMOGENEOUS_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. st->rrddim_page_alignment = 0;
  198. RRDDIM *rd;
  199. rrddim_foreach_read(rd, st) {
  200. rd->last_collected_time.tv_sec = 0;
  201. rd->last_collected_time.tv_usec = 0;
  202. rd->collections_counter = 0;
  203. // memset(rd->values, 0, rd->entries * sizeof(storage_number));
  204. #ifdef ENABLE_DBENGINE
  205. if (RRD_MEMORY_MODE_DBENGINE == st->rrd_memory_mode) {
  206. rrdeng_store_metric_flush_current_page(rd);
  207. }
  208. #endif
  209. }
  210. }
  211. // ----------------------------------------------------------------------------
  212. // RRDSET - helpers for rrdset_create()
  213. inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
  214. if(unlikely(entries < 5)) entries = 5;
  215. if(unlikely(entries > RRD_HISTORY_ENTRIES_MAX)) entries = RRD_HISTORY_ENTRIES_MAX;
  216. if(unlikely(mode == RRD_MEMORY_MODE_NONE || mode == RRD_MEMORY_MODE_ALLOC))
  217. return entries;
  218. long page = (size_t)sysconf(_SC_PAGESIZE);
  219. long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
  220. if(unlikely(size % page)) {
  221. size -= (size % page);
  222. size += page;
  223. long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
  224. return n;
  225. }
  226. return entries;
  227. }
  228. static inline void last_collected_time_align(RRDSET *st) {
  229. st->last_collected_time.tv_sec -= st->last_collected_time.tv_sec % st->update_every;
  230. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)))
  231. st->last_collected_time.tv_usec = 0;
  232. else
  233. st->last_collected_time.tv_usec = 500000;
  234. }
  235. static inline void last_updated_time_align(RRDSET *st) {
  236. st->last_updated.tv_sec -= st->last_updated.tv_sec % st->update_every;
  237. st->last_updated.tv_usec = 0;
  238. }
  239. // ----------------------------------------------------------------------------
  240. // RRDSET - free a chart
  241. void rrdset_free(RRDSET *st) {
  242. if(unlikely(!st)) return;
  243. RRDHOST *host = st->rrdhost;
  244. rrdhost_check_wrlock(host); // make sure we have a write lock on the host
  245. rrdset_wrlock(st); // lock this RRDSET
  246. // info("Removing chart '%s' ('%s')", st->id, st->name);
  247. // ------------------------------------------------------------------------
  248. // remove it from the indexes
  249. if(unlikely(rrdset_index_del(host, st) != st))
  250. error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
  251. rrdset_index_del_name(host, st);
  252. // ------------------------------------------------------------------------
  253. // free its children structures
  254. freez(st->exporting_flags);
  255. while(st->variables) rrdsetvar_free(st->variables);
  256. while(st->alarms) rrdsetcalc_unlink(st->alarms);
  257. while(st->dimensions) rrddim_free(st, st->dimensions);
  258. rrdfamily_free(host, st->rrdfamily);
  259. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  260. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  261. // ------------------------------------------------------------------------
  262. // unlink it from the host
  263. if(st == host->rrdset_root) {
  264. host->rrdset_root = st->next;
  265. }
  266. else {
  267. // find the previous one
  268. RRDSET *s;
  269. for(s = host->rrdset_root; s && s->next != st ; s = s->next) ;
  270. // bypass it
  271. if(s) s->next = st->next;
  272. else error("Request to free RRDSET '%s': cannot find it under host '%s'", st->id, host->hostname);
  273. }
  274. rrdset_unlock(st);
  275. // ------------------------------------------------------------------------
  276. // free it
  277. netdata_rwlock_destroy(&st->rrdset_rwlock);
  278. // free directly allocated members
  279. freez(st->config_section);
  280. freez(st->plugin_name);
  281. freez(st->module_name);
  282. switch(st->rrd_memory_mode) {
  283. case RRD_MEMORY_MODE_SAVE:
  284. case RRD_MEMORY_MODE_MAP:
  285. case RRD_MEMORY_MODE_RAM:
  286. debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
  287. munmap(st, st->memsize);
  288. break;
  289. case RRD_MEMORY_MODE_ALLOC:
  290. case RRD_MEMORY_MODE_NONE:
  291. case RRD_MEMORY_MODE_DBENGINE:
  292. freez(st);
  293. break;
  294. }
  295. }
  296. void rrdset_save(RRDSET *st) {
  297. rrdset_check_rdlock(st);
  298. // info("Saving chart '%s' ('%s')", st->id, st->name);
  299. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
  300. debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
  301. memory_file_save(st->cache_filename, st, st->memsize);
  302. }
  303. RRDDIM *rd;
  304. rrddim_foreach_read(rd, st) {
  305. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
  306. debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
  307. memory_file_save(rd->cache_filename, rd, rd->memsize);
  308. }
  309. }
  310. }
  311. void rrdset_delete(RRDSET *st) {
  312. RRDDIM *rd;
  313. rrdset_check_rdlock(st);
  314. info("Deleting chart '%s' ('%s') from disk...", st->id, st->name);
  315. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
  316. info("Deleting chart header file '%s'.", st->cache_filename);
  317. if(unlikely(unlink(st->cache_filename) == -1))
  318. error("Cannot delete chart header file '%s'", st->cache_filename);
  319. }
  320. rrddim_foreach_read(rd, st) {
  321. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  322. info("Deleting dimension file '%s'.", rd->cache_filename);
  323. if(unlikely(unlink(rd->cache_filename) == -1))
  324. error("Cannot delete dimension file '%s'", rd->cache_filename);
  325. }
  326. }
  327. recursively_delete_dir(st->cache_dir, "left-over chart");
  328. #ifdef ENABLE_ACLK
  329. aclk_del_collector(st->rrdhost->hostname, st->plugin_name, st->module_name);
  330. aclk_update_chart(st->rrdhost, st->id, ACLK_CMD_CHARTDEL);
  331. #endif
  332. }
  333. void rrdset_delete_obsolete_dimensions(RRDSET *st) {
  334. RRDDIM *rd;
  335. rrdset_check_rdlock(st);
  336. info("Deleting dimensions of chart '%s' ('%s') from disk...", st->id, st->name);
  337. rrddim_foreach_read(rd, st) {
  338. if(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
  339. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  340. info("Deleting dimension file '%s'.", rd->cache_filename);
  341. if(unlikely(unlink(rd->cache_filename) == -1))
  342. error("Cannot delete dimension file '%s'", rd->cache_filename);
  343. }
  344. }
  345. }
  346. }
  347. // ----------------------------------------------------------------------------
  348. // RRDSET - create a chart
  349. static inline RRDSET *rrdset_find_on_create(RRDHOST *host, const char *fullid) {
  350. RRDSET *st = rrdset_find(host, fullid);
  351. if(unlikely(st)) {
  352. rrdset_isnot_obsolete(st);
  353. debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
  354. return st;
  355. }
  356. return NULL;
  357. }
  358. RRDSET *rrdset_create_custom(
  359. RRDHOST *host
  360. , const char *type
  361. , const char *id
  362. , const char *name
  363. , const char *family
  364. , const char *context
  365. , const char *title
  366. , const char *units
  367. , const char *plugin
  368. , const char *module
  369. , long priority
  370. , int update_every
  371. , RRDSET_TYPE chart_type
  372. , RRD_MEMORY_MODE memory_mode
  373. , long history_entries
  374. ) {
  375. if(!type || !type[0]) {
  376. fatal("Cannot create rrd stats without a type: id '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  377. , (id && *id)?id:"<unset>"
  378. , (name && *name)?name:"<unset>"
  379. , (family && *family)?family:"<unset>"
  380. , (context && *context)?context:"<unset>"
  381. , (title && *title)?title:"<unset>"
  382. , (units && *units)?units:"<unset>"
  383. , (plugin && *plugin)?plugin:"<unset>"
  384. , (module && *module)?module:"<unset>"
  385. );
  386. return NULL;
  387. }
  388. if(!id || !id[0]) {
  389. fatal("Cannot create rrd stats without an id: type '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  390. , type
  391. , (name && *name)?name:"<unset>"
  392. , (family && *family)?family:"<unset>"
  393. , (context && *context)?context:"<unset>"
  394. , (title && *title)?title:"<unset>"
  395. , (units && *units)?units:"<unset>"
  396. , (plugin && *plugin)?plugin:"<unset>"
  397. , (module && *module)?module:"<unset>"
  398. );
  399. return NULL;
  400. }
  401. // ------------------------------------------------------------------------
  402. // check if it already exists
  403. char fullid[RRD_ID_LENGTH_MAX + 1];
  404. snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
  405. RRDSET *st = rrdset_find_on_create(host, fullid);
  406. if(st) {
  407. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  408. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  409. if(unlikely(name))
  410. rrdset_set_name(st, name);
  411. else
  412. rrdset_set_name(st, id);
  413. return st;
  414. }
  415. rrdhost_wrlock(host);
  416. st = rrdset_find_on_create(host, fullid);
  417. if(st) {
  418. rrdhost_unlock(host);
  419. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  420. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  421. return st;
  422. }
  423. char fullfilename[FILENAME_MAX + 1];
  424. // ------------------------------------------------------------------------
  425. // compose the config_section for this chart
  426. char config_section[RRD_ID_LENGTH_MAX + 1];
  427. if(host == localhost)
  428. strcpy(config_section, fullid);
  429. else
  430. snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
  431. // ------------------------------------------------------------------------
  432. // get the options from the config, we need to create it
  433. long rentries = config_get_number(config_section, "history", history_entries);
  434. long entries = align_entries_to_pagesize(memory_mode, rentries);
  435. if(entries != rentries) entries = config_set_number(config_section, "history", entries);
  436. if(memory_mode == RRD_MEMORY_MODE_NONE && entries != rentries)
  437. entries = config_set_number(config_section, "history", 10);
  438. int enabled = config_get_boolean(config_section, "enabled", 1);
  439. if(!enabled) entries = 5;
  440. if(memory_mode == RRD_MEMORY_MODE_DBENGINE)
  441. entries = config_set_number(config_section, "history", 5);
  442. unsigned long size = sizeof(RRDSET);
  443. char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
  444. time_t now = now_realtime_sec();
  445. // ------------------------------------------------------------------------
  446. // load it or allocate it
  447. debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
  448. snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
  449. if(memory_mode == RRD_MEMORY_MODE_SAVE || memory_mode == RRD_MEMORY_MODE_MAP ||
  450. memory_mode == RRD_MEMORY_MODE_RAM) {
  451. st = (RRDSET *) mymmap(
  452. (memory_mode == RRD_MEMORY_MODE_RAM) ? NULL : fullfilename
  453. , size
  454. , ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE)
  455. , 0
  456. );
  457. if(st) {
  458. memset(&st->avl, 0, sizeof(avl));
  459. memset(&st->avlname, 0, sizeof(avl));
  460. memset(&st->rrdvar_root_index, 0, sizeof(avl_tree_lock));
  461. memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
  462. memset(&st->rrdset_rwlock, 0, sizeof(netdata_rwlock_t));
  463. st->name = NULL;
  464. st->config_section = NULL;
  465. st->type = NULL;
  466. st->family = NULL;
  467. st->title = NULL;
  468. st->units = NULL;
  469. st->context = NULL;
  470. st->cache_dir = NULL;
  471. st->plugin_name = NULL;
  472. st->module_name = NULL;
  473. st->dimensions = NULL;
  474. st->rrdfamily = NULL;
  475. st->rrdhost = NULL;
  476. st->next = NULL;
  477. st->variables = NULL;
  478. st->alarms = NULL;
  479. st->flags = 0x00000000;
  480. st->exporting_flags = NULL;
  481. if(memory_mode == RRD_MEMORY_MODE_RAM) {
  482. memset(st, 0, size);
  483. }
  484. else {
  485. if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
  486. info("Initializing file %s.", fullfilename);
  487. memset(st, 0, size);
  488. }
  489. else if(strcmp(st->id, fullid) != 0) {
  490. error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
  491. // munmap(st, size);
  492. // st = NULL;
  493. memset(st, 0, size);
  494. }
  495. else if(st->memsize != size || st->entries != entries) {
  496. error("File %s does not have the desired size. Clearing it.", fullfilename);
  497. memset(st, 0, size);
  498. }
  499. else if(st->update_every != update_every) {
  500. error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
  501. memset(st, 0, size);
  502. }
  503. else if((now - st->last_updated.tv_sec) > update_every * entries) {
  504. info("File %s is too old. Clearing it.", fullfilename);
  505. memset(st, 0, size);
  506. }
  507. else if(st->last_updated.tv_sec > now + update_every) {
  508. error("File %s refers to the future by %zd secs. Resetting it to now.", fullfilename, (ssize_t)(st->last_updated.tv_sec - now));
  509. st->last_updated.tv_sec = now;
  510. }
  511. // make sure the database is aligned
  512. if(st->last_updated.tv_sec) {
  513. st->update_every = update_every;
  514. last_updated_time_align(st);
  515. }
  516. }
  517. // make sure we have the right memory mode
  518. // even if we cleared the memory
  519. st->rrd_memory_mode = memory_mode;
  520. }
  521. }
  522. if(unlikely(!st)) {
  523. st = callocz(1, size);
  524. if (memory_mode == RRD_MEMORY_MODE_DBENGINE)
  525. st->rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  526. else
  527. st->rrd_memory_mode = (memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_ALLOC;
  528. }
  529. st->plugin_name = plugin?strdupz(plugin):NULL;
  530. st->module_name = module?strdupz(module):NULL;
  531. st->config_section = strdupz(config_section);
  532. st->rrdhost = host;
  533. st->memsize = size;
  534. st->entries = entries;
  535. st->update_every = update_every;
  536. if(st->current_entry >= st->entries) st->current_entry = 0;
  537. strcpy(st->cache_filename, fullfilename);
  538. strcpy(st->magic, RRDSET_MAGIC);
  539. strcpy(st->id, fullid);
  540. st->hash = simple_hash(st->id);
  541. st->cache_dir = cache_dir;
  542. st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
  543. st->type = config_get(st->config_section, "type", type);
  544. st->family = config_get(st->config_section, "family", family?family:st->type);
  545. json_fix_string(st->family);
  546. st->units = config_get(st->config_section, "units", units?units:"");
  547. json_fix_string(st->units);
  548. st->context = config_get(st->config_section, "context", context?context:st->id);
  549. json_fix_string(st->context);
  550. st->hash_context = simple_hash(st->context);
  551. st->priority = config_get_number(st->config_section, "priority", priority);
  552. if(enabled)
  553. rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
  554. else
  555. rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
  556. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  557. rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
  558. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  559. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_SEND);
  560. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_IGNORE);
  561. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  562. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  563. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  564. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  565. // if(!strcmp(st->id, "disk_util.dm-0")) {
  566. // st->debug = 1;
  567. // error("enabled debugging for '%s'", st->id);
  568. // }
  569. // else error("not enabled debugging for '%s'", st->id);
  570. st->green = NAN;
  571. st->red = NAN;
  572. st->last_collected_time.tv_sec = 0;
  573. st->last_collected_time.tv_usec = 0;
  574. st->counter_done = 0;
  575. st->rrddim_page_alignment = 0;
  576. st->gap_when_lost_iterations_above = (int) (gap_when_lost_iterations_above + 2);
  577. st->last_accessed_time = 0;
  578. st->upstream_resync_time = 0;
  579. avl_init_lock(&st->dimensions_index, rrddim_compare);
  580. avl_init_lock(&st->rrdvar_root_index, rrdvar_compare);
  581. netdata_rwlock_init(&st->rrdset_rwlock);
  582. if(name && *name && rrdset_set_name(st, name))
  583. // we did set the name
  584. ;
  585. else
  586. // could not use the name, use the id
  587. rrdset_set_name(st, id);
  588. st->title = config_get(st->config_section, "title", title);
  589. json_fix_string(st->title);
  590. st->rrdfamily = rrdfamily_create(host, st->family);
  591. st->next = host->rrdset_root;
  592. host->rrdset_root = st;
  593. if(host->health_enabled) {
  594. rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  595. rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, RRDVAR_OPTION_DEFAULT);
  596. rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, RRDVAR_OPTION_DEFAULT);
  597. rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, RRDVAR_OPTION_DEFAULT);
  598. rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, RRDVAR_OPTION_DEFAULT);
  599. }
  600. if(unlikely(rrdset_index_add(host, st) != st))
  601. error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
  602. rrdsetcalc_link_matching(st);
  603. rrdcalctemplate_link_matching(st);
  604. rrdhost_cleanup_obsolete_charts(host);
  605. rrdhost_unlock(host);
  606. #ifdef ENABLE_ACLK
  607. aclk_add_collector(host->hostname, plugin, module);
  608. aclk_update_chart(host, st->id, ACLK_CMD_CHART);
  609. #endif
  610. return(st);
  611. }
  612. // ----------------------------------------------------------------------------
  613. // RRDSET - data collection iteration control
  614. inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
  615. if(unlikely(!st->last_collected_time.tv_sec || !microseconds || (rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK)))) {
  616. // call the full next_usec() function
  617. rrdset_next_usec(st, microseconds);
  618. return;
  619. }
  620. st->usec_since_last_update = microseconds;
  621. }
  622. inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
  623. struct timeval now;
  624. now_realtime_timeval(&now);
  625. #ifdef NETDATA_INTERNAL_CHECKS
  626. char *discard_reason = NULL;
  627. usec_t discarded = microseconds;
  628. #endif
  629. if(unlikely(rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK))) {
  630. // the chart needs to be re-synced to current time
  631. rrdset_flag_clear(st, RRDSET_FLAG_SYNC_CLOCK);
  632. // discard the microseconds supplied
  633. microseconds = 0;
  634. #ifdef NETDATA_INTERNAL_CHECKS
  635. if(!discard_reason) discard_reason = "SYNC CLOCK FLAG";
  636. #endif
  637. }
  638. if(unlikely(!st->last_collected_time.tv_sec)) {
  639. // the first entry
  640. microseconds = st->update_every * USEC_PER_SEC;
  641. #ifdef NETDATA_INTERNAL_CHECKS
  642. if(!discard_reason) discard_reason = "FIRST DATA COLLECTION";
  643. #endif
  644. }
  645. else if(unlikely(!microseconds)) {
  646. // no dt given by the plugin
  647. microseconds = dt_usec(&now, &st->last_collected_time);
  648. #ifdef NETDATA_INTERNAL_CHECKS
  649. if(!discard_reason) discard_reason = "NO USEC GIVEN BY COLLECTOR";
  650. #endif
  651. }
  652. else {
  653. // microseconds has the time since the last collection
  654. susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
  655. if(unlikely(since_last_usec < 0)) {
  656. // oops! the database is in the future
  657. 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);
  658. st->last_collected_time.tv_sec = now.tv_sec - st->update_every;
  659. st->last_collected_time.tv_usec = now.tv_usec;
  660. last_collected_time_align(st);
  661. st->last_updated.tv_sec = now.tv_sec - st->update_every;
  662. st->last_updated.tv_usec = now.tv_usec;
  663. last_updated_time_align(st);
  664. microseconds = st->update_every * USEC_PER_SEC;
  665. #ifdef NETDATA_INTERNAL_CHECKS
  666. if(!discard_reason) discard_reason = "COLLECTION TIME IN FUTURE";
  667. #endif
  668. }
  669. else if(unlikely((usec_t)since_last_usec > (usec_t)(st->update_every * 5 * USEC_PER_SEC))) {
  670. // oops! the database is too far behind
  671. 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);
  672. microseconds = (usec_t)since_last_usec;
  673. #ifdef NETDATA_INTERNAL_CHECKS
  674. if(!discard_reason) discard_reason = "COLLECTION TIME TOO FAR IN THE PAST";
  675. #endif
  676. }
  677. #ifdef NETDATA_INTERNAL_CHECKS
  678. if(since_last_usec > 0 && (susec_t)microseconds < since_last_usec) {
  679. static __thread susec_t min_delta = USEC_PER_SEC * 3600, permanent_min_delta = 0;
  680. static __thread time_t last_t = 0;
  681. // the first time initialize it so that it will make the check later
  682. if(last_t == 0) last_t = now.tv_sec + 60;
  683. susec_t delta = since_last_usec - (susec_t)microseconds;
  684. if(delta < min_delta) min_delta = delta;
  685. if(now.tv_sec >= last_t + 60) {
  686. last_t = now.tv_sec;
  687. if(min_delta > permanent_min_delta) {
  688. info("MINIMUM MICROSECONDS DELTA of thread %d increased from %lld to %lld (+%lld)", gettid(), permanent_min_delta, min_delta, min_delta - permanent_min_delta);
  689. permanent_min_delta = min_delta;
  690. }
  691. min_delta = USEC_PER_SEC * 3600;
  692. }
  693. }
  694. #endif
  695. }
  696. #ifdef NETDATA_INTERNAL_CHECKS
  697. debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
  698. rrdset_debug(st, "NEXT: %llu microseconds", microseconds);
  699. if(discarded && discarded != microseconds)
  700. 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");
  701. #endif
  702. st->usec_since_last_update = microseconds;
  703. }
  704. // ----------------------------------------------------------------------------
  705. // RRDSET - process the collected values for all dimensions of a chart
  706. static inline usec_t rrdset_init_last_collected_time(RRDSET *st) {
  707. now_realtime_timeval(&st->last_collected_time);
  708. last_collected_time_align(st);
  709. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  710. #ifdef NETDATA_INTERNAL_CHECKS
  711. rrdset_debug(st, "initialized last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  712. #endif
  713. return last_collect_ut;
  714. }
  715. static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
  716. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  717. usec_t ut = last_collect_ut + st->usec_since_last_update;
  718. st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
  719. st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
  720. #ifdef NETDATA_INTERNAL_CHECKS
  721. rrdset_debug(st, "updated last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  722. #endif
  723. return last_collect_ut;
  724. }
  725. static inline usec_t rrdset_init_last_updated_time(RRDSET *st) {
  726. // copy the last collected time to last updated time
  727. st->last_updated.tv_sec = st->last_collected_time.tv_sec;
  728. st->last_updated.tv_usec = st->last_collected_time.tv_usec;
  729. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  730. st->last_updated.tv_sec -= st->update_every;
  731. last_updated_time_align(st);
  732. usec_t last_updated_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  733. #ifdef NETDATA_INTERNAL_CHECKS
  734. rrdset_debug(st, "initialized last updated time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_updated_ut / USEC_PER_SEC);
  735. #endif
  736. return last_updated_ut;
  737. }
  738. static inline void rrdset_done_push_exclusive(RRDSET *st) {
  739. // usec_t update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  740. //
  741. // if(unlikely(st->usec_since_last_update > update_every_ut * remote_clock_resync_iterations)) {
  742. // error("Chart '%s' was last collected %llu usec before. Resetting it.", st->id, st->usec_since_last_update);
  743. // rrdset_reset(st);
  744. // st->usec_since_last_update = update_every_ut;
  745. // }
  746. if(unlikely(!st->last_collected_time.tv_sec)) {
  747. // it is the first entry
  748. // set the last_collected_time to now
  749. rrdset_init_last_collected_time(st);
  750. }
  751. else {
  752. // it is not the first entry
  753. // calculate the proper last_collected_time, using usec_since_last_update
  754. rrdset_update_last_collected_time(st);
  755. }
  756. st->counter_done++;
  757. rrdset_rdlock(st);
  758. rrdset_done_push(st);
  759. rrdset_unlock(st);
  760. }
  761. static inline size_t rrdset_done_interpolate(
  762. RRDSET *st
  763. , usec_t update_every_ut
  764. , usec_t last_stored_ut
  765. , usec_t next_store_ut
  766. , usec_t last_collect_ut
  767. , usec_t now_collect_ut
  768. , char store_this_entry
  769. , uint32_t storage_flags
  770. ) {
  771. RRDDIM *rd;
  772. size_t stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
  773. usec_t first_ut = last_stored_ut, last_ut = 0;
  774. (void)first_ut;
  775. ssize_t iterations = (ssize_t)((now_collect_ut - last_stored_ut) / (update_every_ut));
  776. if((now_collect_ut % (update_every_ut)) == 0) iterations++;
  777. size_t counter = st->counter;
  778. long current_entry = st->current_entry;
  779. for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
  780. #ifdef NETDATA_INTERNAL_CHECKS
  781. 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); }
  782. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  783. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  784. #endif
  785. last_ut = next_store_ut;
  786. rrddim_foreach_read(rd, st) {
  787. calculated_number new_value;
  788. switch(rd->algorithm) {
  789. case RRD_ALGORITHM_INCREMENTAL:
  790. new_value = (calculated_number)
  791. ( rd->calculated_value
  792. * (calculated_number)(next_store_ut - last_collect_ut)
  793. / (calculated_number)(now_collect_ut - last_collect_ut)
  794. );
  795. #ifdef NETDATA_INTERNAL_CHECKS
  796. rrdset_debug(st, "%s: CALC2 INC "
  797. CALCULATED_NUMBER_FORMAT " = "
  798. CALCULATED_NUMBER_FORMAT
  799. " * (%llu - %llu)"
  800. " / (%llu - %llu)"
  801. , rd->name
  802. , new_value
  803. , rd->calculated_value
  804. , next_store_ut, last_collect_ut
  805. , now_collect_ut, last_collect_ut
  806. );
  807. #endif
  808. rd->calculated_value -= new_value;
  809. new_value += rd->last_calculated_value;
  810. rd->last_calculated_value = 0;
  811. new_value /= (calculated_number)st->update_every;
  812. if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
  813. #ifdef NETDATA_INTERNAL_CHECKS
  814. rrdset_debug(st, "%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
  815. rd->name
  816. , (calculated_number)(next_store_ut - last_stored_ut)
  817. );
  818. #endif
  819. new_value = new_value * (calculated_number)(st->update_every * USEC_PER_SEC) / (calculated_number)(next_store_ut - last_stored_ut);
  820. }
  821. break;
  822. case RRD_ALGORITHM_ABSOLUTE:
  823. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  824. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  825. default:
  826. if(iterations == 1) {
  827. // this is the last iteration
  828. // do not interpolate
  829. // just show the calculated value
  830. new_value = rd->calculated_value;
  831. }
  832. else {
  833. // we have missed an update
  834. // interpolate in the middle values
  835. new_value = (calculated_number)
  836. ( ( (rd->calculated_value - rd->last_calculated_value)
  837. * (calculated_number)(next_store_ut - last_collect_ut)
  838. / (calculated_number)(now_collect_ut - last_collect_ut)
  839. )
  840. + rd->last_calculated_value
  841. );
  842. #ifdef NETDATA_INTERNAL_CHECKS
  843. rrdset_debug(st, "%s: CALC2 DEF "
  844. CALCULATED_NUMBER_FORMAT " = ((("
  845. "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
  846. " * %llu"
  847. " / %llu) + " CALCULATED_NUMBER_FORMAT
  848. , rd->name
  849. , new_value
  850. , rd->calculated_value, rd->last_calculated_value
  851. , (next_store_ut - first_ut)
  852. , (now_collect_ut - first_ut), rd->last_calculated_value
  853. );
  854. #endif
  855. }
  856. break;
  857. }
  858. if(unlikely(!store_this_entry)) {
  859. rd->state->collect_ops.store_metric(rd, next_store_ut, SN_EMPTY_SLOT); //pack_storage_number(0, SN_NOT_EXISTS)
  860. // rd->values[current_entry] = SN_EMPTY_SLOT; //pack_storage_number(0, SN_NOT_EXISTS);
  861. continue;
  862. }
  863. if(likely(rd->updated && rd->collections_counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
  864. rd->state->collect_ops.store_metric(rd, next_store_ut, pack_storage_number(new_value, storage_flags));
  865. // rd->values[current_entry] = pack_storage_number(new_value, storage_flags );
  866. rd->last_stored_value = new_value;
  867. #ifdef NETDATA_INTERNAL_CHECKS
  868. rrdset_debug(st, "%s: STORE[%ld] "
  869. CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
  870. , rd->name
  871. , current_entry
  872. , unpack_storage_number(rd->values[current_entry]), new_value
  873. );
  874. #endif
  875. }
  876. else {
  877. #ifdef NETDATA_INTERNAL_CHECKS
  878. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING "
  879. , rd->name
  880. , current_entry
  881. );
  882. #endif
  883. // rd->values[current_entry] = SN_EMPTY_SLOT; // pack_storage_number(0, SN_NOT_EXISTS);
  884. rd->state->collect_ops.store_metric(rd, next_store_ut, SN_EMPTY_SLOT); //pack_storage_number(0, SN_NOT_EXISTS)
  885. rd->last_stored_value = NAN;
  886. }
  887. stored_entries++;
  888. #ifdef NETDATA_INTERNAL_CHECKS
  889. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
  890. calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
  891. calculated_number t2 = unpack_storage_number(rd->values[current_entry]);
  892. calculated_number accuracy = accuracy_loss(t1, t2);
  893. debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
  894. , st->id, rd->name
  895. , current_entry
  896. , t2
  897. , get_storage_number_flags(rd->values[current_entry])
  898. , t1
  899. , accuracy
  900. , (accuracy > ACCURACY_LOSS_ACCEPTED_PERCENT) ? " **TOO BIG** " : ""
  901. );
  902. rd->collected_volume += t1;
  903. rd->stored_volume += t2;
  904. accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
  905. debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
  906. , st->id, rd->name
  907. , current_entry
  908. , rd->stored_volume
  909. , rd->collected_volume
  910. , accuracy
  911. , (accuracy > ACCURACY_LOSS_ACCEPTED_PERCENT) ? " **TOO BIG** " : ""
  912. );
  913. }
  914. #endif
  915. }
  916. // reset the storage flags for the next point, if any;
  917. storage_flags = SN_EXISTS;
  918. st->counter = ++counter;
  919. st->current_entry = current_entry = ((current_entry + 1) >= st->entries) ? 0 : current_entry + 1;
  920. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  921. st->last_updated.tv_usec = 0;
  922. last_stored_ut = next_store_ut;
  923. }
  924. /*
  925. st->counter = counter;
  926. st->current_entry = current_entry;
  927. if(likely(last_ut)) {
  928. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  929. st->last_updated.tv_usec = 0;
  930. }
  931. */
  932. return stored_entries;
  933. }
  934. static inline void rrdset_done_fill_the_gap(RRDSET *st) {
  935. usec_t update_every_ut = st->update_every * USEC_PER_SEC;
  936. usec_t now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  937. long c = 0, entries = st->entries;
  938. RRDDIM *rd;
  939. rrddim_foreach_read(rd, st) {
  940. usec_t next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  941. long current_entry = st->current_entry;
  942. for(c = 0; c < entries && next_store_ut <= now_collect_ut ; next_store_ut += update_every_ut, c++) {
  943. rd->values[current_entry] = SN_EMPTY_SLOT;
  944. current_entry = ((current_entry + 1) >= entries) ? 0 : current_entry + 1;
  945. #ifdef NETDATA_INTERNAL_CHECKS
  946. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING (FILLED THE GAP)", rd->name, current_entry);
  947. #endif
  948. }
  949. }
  950. if(c > 0) {
  951. c--;
  952. st->last_updated.tv_sec += c * st->update_every;
  953. st->current_entry += c;
  954. if(st->current_entry >= st->entries)
  955. st->current_entry -= st->entries;
  956. }
  957. }
  958. void rrdset_done(RRDSET *st) {
  959. if(unlikely(netdata_exit)) return;
  960. if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
  961. if(unlikely(st->rrdhost->rrdpush_send_enabled))
  962. rrdset_done_push_exclusive(st);
  963. return;
  964. }
  965. debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
  966. RRDDIM *rd;
  967. char
  968. store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
  969. first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
  970. usec_t
  971. last_collect_ut, // the timestamp in microseconds, of the last collected value
  972. now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
  973. last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
  974. next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
  975. update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  976. netdata_thread_disable_cancelability();
  977. // a read lock is OK here
  978. rrdset_rdlock(st);
  979. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))) {
  980. error("Chart '%s' has the OBSOLETE flag set, but it is collected.", st->id);
  981. rrdset_isnot_obsolete(st);
  982. }
  983. // check if the chart has a long time to be updated
  984. if(unlikely(st->usec_since_last_update > st->entries * update_every_ut &&
  985. st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
  986. 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);
  987. rrdset_reset(st);
  988. st->usec_since_last_update = update_every_ut;
  989. store_this_entry = 0;
  990. first_entry = 1;
  991. }
  992. #ifdef NETDATA_INTERNAL_CHECKS
  993. rrdset_debug(st, "microseconds since last update: %llu", st->usec_since_last_update);
  994. #endif
  995. // set last_collected_time
  996. if(unlikely(!st->last_collected_time.tv_sec)) {
  997. // it is the first entry
  998. // set the last_collected_time to now
  999. last_collect_ut = rrdset_init_last_collected_time(st) - update_every_ut;
  1000. // the first entry should not be stored
  1001. store_this_entry = 0;
  1002. first_entry = 1;
  1003. }
  1004. else {
  1005. // it is not the first entry
  1006. // calculate the proper last_collected_time, using usec_since_last_update
  1007. last_collect_ut = rrdset_update_last_collected_time(st);
  1008. }
  1009. // if this set has not been updated in the past
  1010. // we fake the last_update time to be = now - usec_since_last_update
  1011. if(unlikely(!st->last_updated.tv_sec)) {
  1012. // it has never been updated before
  1013. // set a fake last_updated, in the past using usec_since_last_update
  1014. rrdset_init_last_updated_time(st);
  1015. // the first entry should not be stored
  1016. store_this_entry = 0;
  1017. first_entry = 1;
  1018. }
  1019. // check if we will re-write the entire data set
  1020. if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut &&
  1021. st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
  1022. 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);
  1023. rrdset_reset(st);
  1024. rrdset_init_last_updated_time(st);
  1025. st->usec_since_last_update = update_every_ut;
  1026. // the first entry should not be stored
  1027. store_this_entry = 0;
  1028. first_entry = 1;
  1029. }
  1030. #ifdef ENABLE_DBENGINE
  1031. // check if we will re-write the entire page
  1032. if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE &&
  1033. dt_usec(&st->last_collected_time, &st->last_updated) > (RRDENG_BLOCK_SIZE / sizeof(storage_number)) * update_every_ut)) {
  1034. 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);
  1035. rrdset_reset(st);
  1036. rrdset_init_last_updated_time(st);
  1037. st->usec_since_last_update = update_every_ut;
  1038. // the first entry should not be stored
  1039. store_this_entry = 0;
  1040. first_entry = 1;
  1041. }
  1042. #endif
  1043. // these are the 3 variables that will help us in interpolation
  1044. // last_stored_ut = the last time we added a value to the storage
  1045. // now_collect_ut = the time the current value has been collected
  1046. // next_store_ut = the time of the next interpolation point
  1047. now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  1048. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1049. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1050. if(unlikely(!st->counter_done)) {
  1051. // if we have not collected metrics this session (st->counter_done == 0)
  1052. // and we have collected metrics for this chart in the past (st->counter != 0)
  1053. // fill the gap (the chart has been just loaded from disk)
  1054. if(unlikely(st->counter) && st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) {
  1055. rrdset_done_fill_the_gap(st);
  1056. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1057. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1058. }
  1059. if (st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  1060. // set a fake last_updated to jump to current time
  1061. rrdset_init_last_updated_time(st);
  1062. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1063. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1064. }
  1065. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))) {
  1066. store_this_entry = 1;
  1067. last_collect_ut = next_store_ut - update_every_ut;
  1068. #ifdef NETDATA_INTERNAL_CHECKS
  1069. rrdset_debug(st, "Fixed first entry.");
  1070. #endif
  1071. }
  1072. else {
  1073. store_this_entry = 0;
  1074. #ifdef NETDATA_INTERNAL_CHECKS
  1075. rrdset_debug(st, "Will not store the next entry.");
  1076. #endif
  1077. }
  1078. }
  1079. st->counter_done++;
  1080. if(unlikely(st->rrdhost->rrdpush_send_enabled))
  1081. rrdset_done_push(st);
  1082. #ifdef NETDATA_INTERNAL_CHECKS
  1083. rrdset_debug(st, "last_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (last collection time)", (LONG_DOUBLE)last_collect_ut/USEC_PER_SEC);
  1084. rrdset_debug(st, "now_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (current collection time)", (LONG_DOUBLE)now_collect_ut/USEC_PER_SEC);
  1085. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  1086. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  1087. #endif
  1088. // calculate totals and count the dimensions
  1089. int dimensions = 0;
  1090. st->collected_total = 0;
  1091. rrddim_foreach_read(rd, st) {
  1092. dimensions++;
  1093. if(likely(rd->updated))
  1094. st->collected_total += rd->collected_value;
  1095. }
  1096. uint32_t storage_flags = SN_EXISTS;
  1097. // process all dimensions to calculate their values
  1098. // based on the collected figures only
  1099. // at this stage we do not interpolate anything
  1100. rrddim_foreach_read(rd, st) {
  1101. if(unlikely(!rd->updated)) {
  1102. rd->calculated_value = 0;
  1103. continue;
  1104. }
  1105. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE))) {
  1106. error("Dimension %s in chart '%s' has the OBSOLETE flag set, but it is collected.", rd->name, st->id);
  1107. rrddim_isnot_obsolete(st, rd);
  1108. }
  1109. #ifdef NETDATA_INTERNAL_CHECKS
  1110. rrdset_debug(st, "%s: START "
  1111. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1112. " collected_value = " COLLECTED_NUMBER_FORMAT
  1113. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1114. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1115. , rd->name
  1116. , rd->last_collected_value
  1117. , rd->collected_value
  1118. , rd->last_calculated_value
  1119. , rd->calculated_value
  1120. );
  1121. #endif
  1122. switch(rd->algorithm) {
  1123. case RRD_ALGORITHM_ABSOLUTE:
  1124. rd->calculated_value = (calculated_number)rd->collected_value
  1125. * (calculated_number)rd->multiplier
  1126. / (calculated_number)rd->divisor;
  1127. #ifdef NETDATA_INTERNAL_CHECKS
  1128. rrdset_debug(st, "%s: CALC ABS/ABS-NO-IN "
  1129. CALCULATED_NUMBER_FORMAT " = "
  1130. COLLECTED_NUMBER_FORMAT
  1131. " * " CALCULATED_NUMBER_FORMAT
  1132. " / " CALCULATED_NUMBER_FORMAT
  1133. , rd->name
  1134. , rd->calculated_value
  1135. , rd->collected_value
  1136. , (calculated_number)rd->multiplier
  1137. , (calculated_number)rd->divisor
  1138. );
  1139. #endif
  1140. break;
  1141. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1142. if(unlikely(!st->collected_total))
  1143. rd->calculated_value = 0;
  1144. else
  1145. // the percentage of the current value
  1146. // over the total of all dimensions
  1147. rd->calculated_value =
  1148. (calculated_number)100
  1149. * (calculated_number)rd->collected_value
  1150. / (calculated_number)st->collected_total;
  1151. #ifdef NETDATA_INTERNAL_CHECKS
  1152. rrdset_debug(st, "%s: CALC PCENT-ROW "
  1153. CALCULATED_NUMBER_FORMAT " = 100"
  1154. " * " COLLECTED_NUMBER_FORMAT
  1155. " / " COLLECTED_NUMBER_FORMAT
  1156. , rd->name
  1157. , rd->calculated_value
  1158. , rd->collected_value
  1159. , st->collected_total
  1160. );
  1161. #endif
  1162. break;
  1163. case RRD_ALGORITHM_INCREMENTAL:
  1164. if(unlikely(rd->collections_counter <= 1)) {
  1165. rd->calculated_value = 0;
  1166. continue;
  1167. }
  1168. // If the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1169. // to reset the calculation (it will give zero as the calculation for this second).
  1170. // It is imperative to set the comparison to uint64_t since type collected_number is signed and
  1171. // produces wrong results as far as incremental counters are concerned.
  1172. if(unlikely((uint64_t)rd->last_collected_value > (uint64_t)rd->collected_value)) {
  1173. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1174. , st->name, rd->name
  1175. , rd->last_collected_value
  1176. , rd->collected_value);
  1177. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1178. storage_flags = SN_EXISTS_RESET;
  1179. uint64_t last = (uint64_t)rd->last_collected_value;
  1180. uint64_t new = (uint64_t)rd->collected_value;
  1181. uint64_t max = (uint64_t)rd->collected_value_max;
  1182. uint64_t cap = 0;
  1183. // Signed values are handled by exploiting two's complement which will produce positive deltas
  1184. if (max > 0x00000000FFFFFFFFULL)
  1185. cap = 0xFFFFFFFFFFFFFFFFULL; // handles signed and unsigned 64-bit counters
  1186. else
  1187. cap = 0x00000000FFFFFFFFULL; // handles signed and unsigned 32-bit counters
  1188. uint64_t delta = cap - last + new;
  1189. uint64_t max_acceptable_rate = (cap / 100) * MAX_INCREMENTAL_PERCENT_RATE;
  1190. // If the delta is less than the maximum acceptable rate and the previous value was near the cap
  1191. // then this is an overflow. There can be false positives such that a reset is detected as an
  1192. // overflow.
  1193. // TODO: remember recent history of rates and compare with current rate to reduce this chance.
  1194. if (delta < max_acceptable_rate) {
  1195. rd->calculated_value +=
  1196. (calculated_number) delta
  1197. * (calculated_number) rd->multiplier
  1198. / (calculated_number) rd->divisor;
  1199. } else {
  1200. // This is a reset. Any overflow with a rate greater than MAX_INCREMENTAL_PERCENT_RATE will also
  1201. // be detected as a reset instead.
  1202. rd->calculated_value += (calculated_number)0;
  1203. }
  1204. }
  1205. else {
  1206. rd->calculated_value +=
  1207. (calculated_number) (rd->collected_value - rd->last_collected_value)
  1208. * (calculated_number) rd->multiplier
  1209. / (calculated_number) rd->divisor;
  1210. }
  1211. #ifdef NETDATA_INTERNAL_CHECKS
  1212. rrdset_debug(st, "%s: CALC INC PRE "
  1213. CALCULATED_NUMBER_FORMAT " = ("
  1214. COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
  1215. ")"
  1216. " * " CALCULATED_NUMBER_FORMAT
  1217. " / " CALCULATED_NUMBER_FORMAT
  1218. , rd->name
  1219. , rd->calculated_value
  1220. , rd->collected_value, rd->last_collected_value
  1221. , (calculated_number)rd->multiplier
  1222. , (calculated_number)rd->divisor
  1223. );
  1224. #endif
  1225. break;
  1226. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1227. if(unlikely(rd->collections_counter <= 1)) {
  1228. rd->calculated_value = 0;
  1229. continue;
  1230. }
  1231. // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1232. // to reset the calculation (it will give zero as the calculation for this second)
  1233. if(unlikely(rd->last_collected_value > rd->collected_value)) {
  1234. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1235. , st->name, rd->name
  1236. , rd->last_collected_value
  1237. , rd->collected_value
  1238. );
  1239. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1240. storage_flags = SN_EXISTS_RESET;
  1241. rd->last_collected_value = rd->collected_value;
  1242. }
  1243. // the percentage of the current increment
  1244. // over the increment of all dimensions together
  1245. if(unlikely(st->collected_total == st->last_collected_total))
  1246. rd->calculated_value = 0;
  1247. else
  1248. rd->calculated_value =
  1249. (calculated_number)100
  1250. * (calculated_number)(rd->collected_value - rd->last_collected_value)
  1251. / (calculated_number)(st->collected_total - st->last_collected_total);
  1252. #ifdef NETDATA_INTERNAL_CHECKS
  1253. rrdset_debug(st, "%s: CALC PCENT-DIFF "
  1254. CALCULATED_NUMBER_FORMAT " = 100"
  1255. " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1256. " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1257. , rd->name
  1258. , rd->calculated_value
  1259. , rd->collected_value, rd->last_collected_value
  1260. , st->collected_total, st->last_collected_total
  1261. );
  1262. #endif
  1263. break;
  1264. default:
  1265. // make the default zero, to make sure
  1266. // it gets noticed when we add new types
  1267. rd->calculated_value = 0;
  1268. #ifdef NETDATA_INTERNAL_CHECKS
  1269. rrdset_debug(st, "%s: CALC "
  1270. CALCULATED_NUMBER_FORMAT " = 0"
  1271. , rd->name
  1272. , rd->calculated_value
  1273. );
  1274. #endif
  1275. break;
  1276. }
  1277. #ifdef NETDATA_INTERNAL_CHECKS
  1278. rrdset_debug(st, "%s: PHASE2 "
  1279. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1280. " collected_value = " COLLECTED_NUMBER_FORMAT
  1281. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1282. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1283. , rd->name
  1284. , rd->last_collected_value
  1285. , rd->collected_value
  1286. , rd->last_calculated_value
  1287. , rd->calculated_value
  1288. );
  1289. #endif
  1290. }
  1291. // at this point we have all the calculated values ready
  1292. // it is now time to interpolate values on a second boundary
  1293. #ifdef NETDATA_INTERNAL_CHECKS
  1294. if(unlikely(now_collect_ut < next_store_ut)) {
  1295. // this is collected in the same interpolation point
  1296. rrdset_debug(st, "THIS IS IN THE SAME INTERPOLATION POINT");
  1297. 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);
  1298. }
  1299. #endif
  1300. rrdset_done_interpolate(st
  1301. , update_every_ut
  1302. , last_stored_ut
  1303. , next_store_ut
  1304. , last_collect_ut
  1305. , now_collect_ut
  1306. , store_this_entry
  1307. , storage_flags
  1308. );
  1309. st->last_collected_total = st->collected_total;
  1310. rrddim_foreach_read(rd, st) {
  1311. if(unlikely(!rd->updated))
  1312. continue;
  1313. #ifdef NETDATA_INTERNAL_CHECKS
  1314. 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);
  1315. #endif
  1316. rd->last_collected_value = rd->collected_value;
  1317. switch(rd->algorithm) {
  1318. case RRD_ALGORITHM_INCREMENTAL:
  1319. if(unlikely(!first_entry)) {
  1320. #ifdef NETDATA_INTERNAL_CHECKS
  1321. 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);
  1322. #endif
  1323. rd->last_calculated_value += rd->calculated_value;
  1324. }
  1325. else {
  1326. #ifdef NETDATA_INTERNAL_CHECKS
  1327. rrdset_debug(st, "THIS IS THE FIRST POINT");
  1328. #endif
  1329. }
  1330. break;
  1331. case RRD_ALGORITHM_ABSOLUTE:
  1332. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1333. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1334. #ifdef NETDATA_INTERNAL_CHECKS
  1335. 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);
  1336. #endif
  1337. rd->last_calculated_value = rd->calculated_value;
  1338. break;
  1339. }
  1340. rd->calculated_value = 0;
  1341. rd->collected_value = 0;
  1342. rd->updated = 0;
  1343. #ifdef NETDATA_INTERNAL_CHECKS
  1344. rrdset_debug(st, "%s: END "
  1345. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1346. " collected_value = " COLLECTED_NUMBER_FORMAT
  1347. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1348. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1349. , rd->name
  1350. , rd->last_collected_value
  1351. , rd->collected_value
  1352. , rd->last_calculated_value
  1353. , rd->calculated_value
  1354. );
  1355. #endif
  1356. }
  1357. // ALL DONE ABOUT THE DATA UPDATE
  1358. // --------------------------------------------------------------------
  1359. // find if there are any obsolete dimensions
  1360. time_t now = now_realtime_sec();
  1361. if(unlikely(rrddim_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))) {
  1362. rrddim_foreach_read(rd, st)
  1363. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)))
  1364. break;
  1365. if(unlikely(rd)) {
  1366. RRDDIM *last;
  1367. // there is a dimension to free
  1368. // upgrade our read lock to a write lock
  1369. rrdset_unlock(st);
  1370. rrdset_wrlock(st);
  1371. for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
  1372. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE) && (rd->last_collected_time.tv_sec + rrdset_free_obsolete_time < now))) {
  1373. info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
  1374. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  1375. info("Deleting dimension file '%s'.", rd->cache_filename);
  1376. if(unlikely(unlink(rd->cache_filename) == -1))
  1377. error("Cannot delete dimension file '%s'", rd->cache_filename);
  1378. }
  1379. if(unlikely(!last)) {
  1380. rrddim_free(st, rd);
  1381. rd = st->dimensions;
  1382. continue;
  1383. }
  1384. else {
  1385. rrddim_free(st, rd);
  1386. rd = last->next;
  1387. continue;
  1388. }
  1389. }
  1390. last = rd;
  1391. rd = rd->next;
  1392. }
  1393. }
  1394. else {
  1395. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS);
  1396. }
  1397. }
  1398. rrdset_unlock(st);
  1399. netdata_thread_enable_cancelability();
  1400. }