rrdset.c 80 KB

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