rrdset.c 79 KB

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