rrdset.c 78 KB

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