rrdset.c 77 KB

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