rrdset.c 76 KB

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