rrdset.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. #include <sched.h>
  5. #include "storage_engine.h"
  6. void rrdset_metadata_updated(RRDSET *st) {
  7. __atomic_add_fetch(&st->version, 1, __ATOMIC_RELAXED);
  8. rrdcontext_updated_rrdset(st);
  9. }
  10. // ----------------------------------------------------------------------------
  11. // RRDSET rrdpush send chart_slots
  12. static void rrdset_rrdpush_send_chart_slot_assign(RRDSET *st) {
  13. RRDHOST *host = st->rrdhost;
  14. spinlock_lock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  15. if(host->rrdpush.send.pluginsd_chart_slots.available.used > 0)
  16. st->rrdpush.sender.chart_slot =
  17. host->rrdpush.send.pluginsd_chart_slots.available.array[--host->rrdpush.send.pluginsd_chart_slots.available.used];
  18. else
  19. st->rrdpush.sender.chart_slot = ++host->rrdpush.send.pluginsd_chart_slots.last_used;
  20. spinlock_unlock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  21. }
  22. static void rrdset_rrdpush_send_chart_slot_release(RRDSET *st) {
  23. if(!st->rrdpush.sender.chart_slot || st->rrdhost->rrdpush.send.pluginsd_chart_slots.available.ignore)
  24. return;
  25. RRDHOST *host = st->rrdhost;
  26. spinlock_lock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  27. if(host->rrdpush.send.pluginsd_chart_slots.available.used >= host->rrdpush.send.pluginsd_chart_slots.available.size) {
  28. uint32_t old_size = host->rrdpush.send.pluginsd_chart_slots.available.size;
  29. uint32_t new_size = (old_size > 0) ? (old_size * 2) : 1024;
  30. host->rrdpush.send.pluginsd_chart_slots.available.array =
  31. reallocz(host->rrdpush.send.pluginsd_chart_slots.available.array, new_size * sizeof(uint32_t));
  32. host->rrdpush.send.pluginsd_chart_slots.available.size = new_size;
  33. }
  34. host->rrdpush.send.pluginsd_chart_slots.available.array[host->rrdpush.send.pluginsd_chart_slots.available.used++] =
  35. st->rrdpush.sender.chart_slot;
  36. st->rrdpush.sender.chart_slot = 0;
  37. spinlock_unlock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  38. }
  39. void rrdhost_pluginsd_send_chart_slots_free(RRDHOST *host) {
  40. spinlock_lock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  41. host->rrdpush.send.pluginsd_chart_slots.available.ignore = true;
  42. freez(host->rrdpush.send.pluginsd_chart_slots.available.array);
  43. host->rrdpush.send.pluginsd_chart_slots.available.array = NULL;
  44. host->rrdpush.send.pluginsd_chart_slots.available.used = 0;
  45. host->rrdpush.send.pluginsd_chart_slots.available.size = 0;
  46. spinlock_unlock(&host->rrdpush.send.pluginsd_chart_slots.available.spinlock);
  47. // zero all the slots on all charts, so that they will not attempt to access the array
  48. RRDSET *st;
  49. rrdset_foreach_read(st, host) {
  50. st->rrdpush.sender.chart_slot = 0;
  51. }
  52. rrdset_foreach_done(st);
  53. }
  54. void rrdset_pluginsd_receive_unslot(RRDSET *st) {
  55. for(size_t i = 0; i < st->pluginsd.size ;i++) {
  56. rrddim_acquired_release(st->pluginsd.prd_array[i].rda); // can be NULL
  57. st->pluginsd.prd_array[i].rda = NULL;
  58. st->pluginsd.prd_array[i].rd = NULL;
  59. st->pluginsd.prd_array[i].id = NULL;
  60. }
  61. RRDHOST *host = st->rrdhost;
  62. if(st->pluginsd.last_slot >= 0 &&
  63. (uint32_t)st->pluginsd.last_slot < host->rrdpush.receive.pluginsd_chart_slots.size &&
  64. host->rrdpush.receive.pluginsd_chart_slots.array[st->pluginsd.last_slot] == st) {
  65. host->rrdpush.receive.pluginsd_chart_slots.array[st->pluginsd.last_slot] = NULL;
  66. }
  67. st->pluginsd.last_slot = -1;
  68. st->pluginsd.dims_with_slots = false;
  69. }
  70. void rrdset_pluginsd_receive_unslot_and_cleanup(RRDSET *st) {
  71. if(!st)
  72. return;
  73. spinlock_lock(&st->pluginsd.spinlock);
  74. rrdset_pluginsd_receive_unslot(st);
  75. freez(st->pluginsd.prd_array);
  76. st->pluginsd.prd_array = NULL;
  77. st->pluginsd.size = 0;
  78. st->pluginsd.pos = 0;
  79. st->pluginsd.set = false;
  80. st->pluginsd.last_slot = -1;
  81. st->pluginsd.dims_with_slots = false;
  82. st->pluginsd.collector_tid = 0;
  83. spinlock_unlock(&st->pluginsd.spinlock);
  84. }
  85. static void rrdset_pluginsd_receive_slots_initialize(RRDSET *st) {
  86. spinlock_init(&st->pluginsd.spinlock);
  87. st->pluginsd.last_slot = -1;
  88. }
  89. void rrdhost_pluginsd_receive_chart_slots_free(RRDHOST *host) {
  90. spinlock_lock(&host->rrdpush.receive.pluginsd_chart_slots.spinlock);
  91. if(host->rrdpush.receive.pluginsd_chart_slots.array) {
  92. for (size_t s = 0; s < host->rrdpush.receive.pluginsd_chart_slots.size; s++)
  93. rrdset_pluginsd_receive_unslot_and_cleanup(host->rrdpush.receive.pluginsd_chart_slots.array[s]);
  94. freez(host->rrdpush.receive.pluginsd_chart_slots.array);
  95. host->rrdpush.receive.pluginsd_chart_slots.array = NULL;
  96. host->rrdpush.receive.pluginsd_chart_slots.size = 0;
  97. }
  98. spinlock_unlock(&host->rrdpush.receive.pluginsd_chart_slots.spinlock);
  99. }
  100. // ----------------------------------------------------------------------------
  101. // RRDSET name index
  102. static void rrdset_name_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *rrdhost __maybe_unused) {
  103. RRDSET *st = rrdset;
  104. rrdset_flag_set(st, RRDSET_FLAG_INDEXED_NAME);
  105. }
  106. static void rrdset_name_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *rrdhost __maybe_unused) {
  107. RRDSET *st = rrdset;
  108. rrdset_flag_clear(st, RRDSET_FLAG_INDEXED_NAME);
  109. }
  110. static inline void rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
  111. if(!st->name) return;
  112. dictionary_set(host->rrdset_root_index_name, rrdset_name(st), st, sizeof(RRDSET));
  113. }
  114. static inline void rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
  115. if(rrdset_flag_check(st, RRDSET_FLAG_INDEXED_NAME))
  116. dictionary_del(host->rrdset_root_index_name, rrdset_name(st));
  117. }
  118. static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name) {
  119. if (unlikely(!host->rrdset_root_index_name))
  120. return NULL;
  121. return dictionary_get(host->rrdset_root_index_name, name);
  122. }
  123. // ----------------------------------------------------------------------------
  124. // RRDSET index
  125. static inline void rrdset_update_permanent_labels(RRDSET *st) {
  126. if(!st->rrdlabels) return;
  127. rrdlabels_add(st->rrdlabels, "_collect_plugin", rrdset_plugin_name(st), RRDLABEL_SRC_AUTO | RRDLABEL_FLAG_DONT_DELETE);
  128. rrdlabels_add(st->rrdlabels, "_collect_module", rrdset_module_name(st), RRDLABEL_SRC_AUTO | RRDLABEL_FLAG_DONT_DELETE);
  129. }
  130. static STRING *rrdset_fix_name(RRDHOST *host, const char *chart_full_id, const char *type, const char *current_name, const char *name) {
  131. if(!name || !*name) return NULL;
  132. char full_name[RRD_ID_LENGTH_MAX + 1];
  133. char sanitized_name[CONFIG_MAX_VALUE + 1];
  134. char new_name[CONFIG_MAX_VALUE + 1];
  135. snprintfz(full_name, RRD_ID_LENGTH_MAX, "%s.%s", type, name);
  136. rrdset_strncpyz_name(sanitized_name, full_name, CONFIG_MAX_VALUE);
  137. strncpyz(new_name, sanitized_name, CONFIG_MAX_VALUE);
  138. if(rrdset_index_find_name(host, new_name)) {
  139. netdata_log_debug(D_RRD_CALLS, "RRDSET: chart name '%s' on host '%s' already exists.", new_name, rrdhost_hostname(host));
  140. if(!strcmp(chart_full_id, full_name) && (!current_name || !*current_name)) {
  141. unsigned i = 1;
  142. do {
  143. snprintfz(new_name, CONFIG_MAX_VALUE, "%s_%u", sanitized_name, i);
  144. i++;
  145. } while (rrdset_index_find_name(host, new_name));
  146. // netdata_log_info("RRDSET: using name '%s' for chart '%s' on host '%s'.", new_name, full_name, rrdhost_hostname(host));
  147. }
  148. else
  149. return NULL;
  150. }
  151. return string_strdupz(new_name);
  152. }
  153. struct rrdset_constructor {
  154. RRDHOST *host;
  155. const char *type;
  156. const char *id;
  157. const char *name;
  158. const char *family;
  159. const char *context;
  160. const char *title;
  161. const char *units;
  162. const char *plugin;
  163. const char *module;
  164. long priority;
  165. int update_every;
  166. RRDSET_TYPE chart_type;
  167. RRD_MEMORY_MODE memory_mode;
  168. long history_entries;
  169. enum {
  170. RRDSET_REACT_NONE = 0,
  171. RRDSET_REACT_NEW = (1 << 0),
  172. RRDSET_REACT_UPDATED = (1 << 1),
  173. RRDSET_REACT_PLUGIN_UPDATED = (1 << 2),
  174. RRDSET_REACT_MODULE_UPDATED = (1 << 3),
  175. RRDSET_REACT_CHART_ACTIVATED = (1 << 4),
  176. } react_action;
  177. };
  178. // the constructor - the dictionary is write locked while this runs
  179. static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *constructor_data) {
  180. struct rrdset_constructor *ctr = constructor_data;
  181. RRDHOST *host = ctr->host;
  182. RRDSET *st = rrdset;
  183. const char *chart_full_id = dictionary_acquired_item_name(item);
  184. st->id = string_strdupz(chart_full_id);
  185. st->name = rrdset_fix_name(host, chart_full_id, ctr->type, NULL, ctr->name);
  186. if(!st->name)
  187. st->name = rrdset_fix_name(host, chart_full_id, ctr->type, NULL, ctr->id);
  188. rrdset_index_add_name(host, st);
  189. st->parts.id = string_strdupz(ctr->id);
  190. st->parts.type = string_strdupz(ctr->type);
  191. st->parts.name = string_strdupz(ctr->name);
  192. st->family = (ctr->family && *ctr->family) ? rrd_string_strdupz(ctr->family) : rrd_string_strdupz(ctr->type);
  193. st->context = (ctr->context && *ctr->context) ? rrd_string_strdupz(ctr->context) : rrd_string_strdupz(chart_full_id);
  194. st->units = rrd_string_strdupz(ctr->units);
  195. st->title = rrd_string_strdupz(ctr->title);
  196. st->plugin_name = rrd_string_strdupz(ctr->plugin);
  197. st->module_name = rrd_string_strdupz(ctr->module);
  198. st->priority = ctr->priority;
  199. st->db.entries = (ctr->memory_mode != RRD_MEMORY_MODE_DBENGINE) ? align_entries_to_pagesize(ctr->memory_mode, ctr->history_entries) : 5;
  200. st->update_every = ctr->update_every;
  201. st->rrd_memory_mode = ctr->memory_mode;
  202. st->chart_type = ctr->chart_type;
  203. st->rrdhost = host;
  204. rrdset_rrdpush_send_chart_slot_assign(st);
  205. spinlock_init(&st->data_collection_lock);
  206. st->flags = RRDSET_FLAG_SYNC_CLOCK
  207. | RRDSET_FLAG_INDEXED_ID
  208. | RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED
  209. | RRDSET_FLAG_SENDER_REPLICATION_FINISHED
  210. ;
  211. rw_spinlock_init(&st->alerts.spinlock);
  212. // initialize the db tiers
  213. {
  214. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  215. STORAGE_ENGINE *eng = st->rrdhost->db[tier].eng;
  216. if(!eng) continue;
  217. st->smg[tier] = storage_engine_metrics_group_get(eng->seb, host->db[tier].si, &st->chart_uuid);
  218. }
  219. }
  220. rrddim_index_init(st);
  221. // chart variables - we need this for data collection to work (collector given chart variables) - not only health
  222. rrdsetvar_index_init(st);
  223. if (host->health.health_enabled) {
  224. st->rrdfamily = rrdfamily_add_and_acquire(host, rrdset_family(st));
  225. st->rrdvars = rrdvariables_create();
  226. rrddimvar_index_init(st);
  227. }
  228. st->rrdlabels = rrdlabels_create();
  229. rrdset_update_permanent_labels(st);
  230. st->green = NAN;
  231. st->red = NAN;
  232. rrdset_pluginsd_receive_slots_initialize(st);
  233. ctr->react_action = RRDSET_REACT_NEW;
  234. ml_chart_new(st);
  235. }
  236. void rrdset_finalize_collection(RRDSET *st, bool dimensions_too) {
  237. RRDHOST *host = st->rrdhost;
  238. rrdset_flag_set(st, RRDSET_FLAG_COLLECTION_FINISHED);
  239. if(dimensions_too) {
  240. RRDDIM *rd;
  241. rrddim_foreach_read(rd, st)
  242. rrddim_finalize_collection_and_check_retention(rd);
  243. rrddim_foreach_done(rd);
  244. }
  245. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  246. STORAGE_ENGINE *eng = st->rrdhost->db[tier].eng;
  247. if(!eng) continue;
  248. if(st->smg[tier]) {
  249. storage_engine_metrics_group_release(eng->seb, host->db[tier].si, st->smg[tier]);
  250. st->smg[tier] = NULL;
  251. }
  252. }
  253. rrdset_pluginsd_receive_unslot_and_cleanup(st);
  254. }
  255. // the destructor - the dictionary is write locked while this runs
  256. static void rrdset_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *rrdhost) {
  257. RRDHOST *host = rrdhost;
  258. RRDSET *st = rrdset;
  259. rrdset_flag_clear(st, RRDSET_FLAG_INDEXED_ID);
  260. rrdset_finalize_collection(st, false);
  261. rrdset_rrdpush_send_chart_slot_release(st);
  262. // remove it from the name index
  263. rrdset_index_del_name(host, st);
  264. // release the collector info
  265. dictionary_destroy(st->functions_view);
  266. rrdcalc_unlink_all_rrdset_alerts(st);
  267. // ------------------------------------------------------------------------
  268. // the order of destruction is important here
  269. // 1. delete RRDDIMVAR index - this will speed up the destruction of RRDDIMs
  270. // because each dimension loops to find its own variables in this index.
  271. // There are no references to the items on this index from the dimensions.
  272. // To find their own, they have to walk-through the dictionary.
  273. rrddimvar_index_destroy(st); // destroy the rrddimvar index
  274. // 2. delete RRDSETVAR index
  275. rrdsetvar_index_destroy(st); // destroy the rrdsetvar index
  276. // 3. delete RRDVAR index after the above, to avoid triggering its garbage collector (they have references on this)
  277. rrdvariables_destroy(st->rrdvars); // free all variables and destroy the rrdvar dictionary
  278. // 4. delete RRDFAMILY - this has to be last, because RRDDIMVAR and RRDSETVAR need the reference counter
  279. rrdfamily_release(host, st->rrdfamily); // release the acquired rrdfamily -- has to be after all variables
  280. // 5. delete RRDDIMs, now their variables are not existing, so this is fast
  281. rrddim_index_destroy(st); // free all the dimensions and destroy the dimensions index
  282. // 6. this has to be after the dimensions are freed, but before labels are freed (contexts need the labels)
  283. rrdcontext_removed_rrdset(st); // let contexts know
  284. // 7. destroy the chart labels
  285. rrdlabels_destroy(st->rrdlabels); // destroy the labels, after letting the contexts know
  286. // 8. destroy the ml handle
  287. ml_chart_delete(st);
  288. // ------------------------------------------------------------------------
  289. // free it
  290. string_freez(st->id);
  291. string_freez(st->name);
  292. string_freez(st->parts.id);
  293. string_freez(st->parts.type);
  294. string_freez(st->parts.name);
  295. string_freez(st->family);
  296. string_freez(st->title);
  297. string_freez(st->units);
  298. string_freez(st->context);
  299. string_freez(st->plugin_name);
  300. string_freez(st->module_name);
  301. freez(st->exporting_flags);
  302. freez(st->db.cache_dir);
  303. }
  304. // the item to be inserted, is already in the dictionary
  305. // this callback deals with the situation, migrating the existing object to the new values
  306. // the dictionary is write locked while this runs
  307. static bool rrdset_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *new_rrdset, void *constructor_data) {
  308. (void)new_rrdset; // it is NULL
  309. struct rrdset_constructor *ctr = constructor_data;
  310. RRDSET *st = rrdset;
  311. rrdset_isnot_obsolete___safe_from_collector_thread(st);
  312. ctr->react_action = RRDSET_REACT_NONE;
  313. if (rrdset_reset_name(st, (ctr->name && *ctr->name) ? ctr->name : ctr->id) == 2)
  314. ctr->react_action |= RRDSET_REACT_UPDATED;
  315. if (unlikely(st->priority != ctr->priority)) {
  316. st->priority = ctr->priority;
  317. ctr->react_action |= RRDSET_REACT_UPDATED;
  318. }
  319. if (unlikely(st->update_every != ctr->update_every)) {
  320. rrdset_set_update_every_s(st, ctr->update_every);
  321. ctr->react_action |= RRDSET_REACT_UPDATED;
  322. }
  323. if(ctr->plugin && *ctr->plugin) {
  324. STRING *old_plugin = st->plugin_name;
  325. st->plugin_name = rrd_string_strdupz(ctr->plugin);
  326. if (old_plugin != st->plugin_name)
  327. ctr->react_action |= RRDSET_REACT_PLUGIN_UPDATED;
  328. string_freez(old_plugin);
  329. }
  330. if(ctr->module && *ctr->module) {
  331. STRING *old_module = st->module_name;
  332. st->module_name = rrd_string_strdupz(ctr->module);
  333. if (old_module != st->module_name)
  334. ctr->react_action |= RRDSET_REACT_MODULE_UPDATED;
  335. string_freez(old_module);
  336. }
  337. if(ctr->title && *ctr->title) {
  338. STRING *old_title = st->title;
  339. st->title = rrd_string_strdupz(ctr->title);
  340. if(old_title != st->title)
  341. ctr->react_action |= RRDSET_REACT_UPDATED;
  342. string_freez(old_title);
  343. }
  344. if(ctr->units && *ctr->units) {
  345. STRING *old_units = st->units;
  346. st->units = rrd_string_strdupz(ctr->units);
  347. if(old_units != st->units)
  348. ctr->react_action |= RRDSET_REACT_UPDATED;
  349. string_freez(old_units);
  350. }
  351. if(ctr->family && *ctr->family) {
  352. STRING *old_family = st->family;
  353. st->family = rrd_string_strdupz(ctr->family);
  354. if(old_family != st->family)
  355. ctr->react_action |= RRDSET_REACT_UPDATED;
  356. string_freez(old_family);
  357. // TODO - we should rename RRDFAMILY variables
  358. }
  359. if(ctr->context && *ctr->context) {
  360. STRING *old_context = st->context;
  361. st->context = rrd_string_strdupz(ctr->context);
  362. if(old_context != st->context)
  363. ctr->react_action |= RRDSET_REACT_UPDATED;
  364. string_freez(old_context);
  365. }
  366. if(st->chart_type != ctr->chart_type) {
  367. st->chart_type = ctr->chart_type;
  368. ctr->react_action |= RRDSET_REACT_UPDATED;
  369. }
  370. rrdset_update_permanent_labels(st);
  371. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  372. return ctr->react_action != RRDSET_REACT_NONE;
  373. }
  374. // this is called after all insertions/conflicts, with the dictionary unlocked, with a reference to RRDSET
  375. // so, any actions requiring locks on other objects, should be placed here
  376. static void rrdset_react_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *constructor_data) {
  377. struct rrdset_constructor *ctr = constructor_data;
  378. RRDSET *st = rrdset;
  379. RRDHOST *host = st->rrdhost;
  380. st->last_accessed_time_s = now_realtime_sec();
  381. if(host->health.health_enabled && (ctr->react_action & (RRDSET_REACT_NEW | RRDSET_REACT_CHART_ACTIVATED))) {
  382. rrdset_flag_set(st, RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION);
  383. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION);
  384. }
  385. if(ctr->react_action & (RRDSET_REACT_NEW | RRDSET_REACT_PLUGIN_UPDATED | RRDSET_REACT_MODULE_UPDATED)) {
  386. if (ctr->react_action & RRDSET_REACT_NEW) {
  387. if(unlikely(rrdcontext_find_chart_uuid(st, &st->chart_uuid)))
  388. uuid_generate(st->chart_uuid);
  389. }
  390. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  391. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  392. }
  393. rrdset_metadata_updated(st);
  394. }
  395. void rrdset_index_init(RRDHOST *host) {
  396. if(!host->rrdset_root_index) {
  397. host->rrdset_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  398. &dictionary_stats_category_rrdset_rrddim, sizeof(RRDSET));
  399. dictionary_register_insert_callback(host->rrdset_root_index, rrdset_insert_callback, NULL);
  400. dictionary_register_conflict_callback(host->rrdset_root_index, rrdset_conflict_callback, NULL);
  401. dictionary_register_react_callback(host->rrdset_root_index, rrdset_react_callback, NULL);
  402. dictionary_register_delete_callback(host->rrdset_root_index, rrdset_delete_callback, host);
  403. }
  404. if(!host->rrdset_root_index_name) {
  405. host->rrdset_root_index_name = dictionary_create_advanced(
  406. DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE,
  407. &dictionary_stats_category_rrdset_rrddim, 0);
  408. dictionary_register_insert_callback(host->rrdset_root_index_name, rrdset_name_insert_callback, host);
  409. dictionary_register_delete_callback(host->rrdset_root_index_name, rrdset_name_delete_callback, host);
  410. }
  411. }
  412. void rrdset_index_destroy(RRDHOST *host) {
  413. // destroy the name index first
  414. dictionary_destroy(host->rrdset_root_index_name);
  415. host->rrdset_root_index_name = NULL;
  416. // destroy the id index last
  417. dictionary_destroy(host->rrdset_root_index);
  418. host->rrdset_root_index = NULL;
  419. }
  420. static inline RRDSET *rrdset_index_add(RRDHOST *host, const char *id, struct rrdset_constructor *st_ctr) {
  421. return dictionary_set_advanced(host->rrdset_root_index, id, -1, NULL, sizeof(RRDSET), st_ctr);
  422. }
  423. static inline void rrdset_index_del(RRDHOST *host, RRDSET *st) {
  424. if(rrdset_flag_check(st, RRDSET_FLAG_INDEXED_ID))
  425. dictionary_del(host->rrdset_root_index, rrdset_id(st));
  426. }
  427. static RRDSET *rrdset_index_find(RRDHOST *host, const char *id) {
  428. // TODO - the name index should have an acquired dictionary item, not just a pointer to RRDSET
  429. if (unlikely(!host->rrdset_root_index))
  430. return NULL;
  431. return dictionary_get(host->rrdset_root_index, id);
  432. }
  433. // ----------------------------------------------------------------------------
  434. // RRDSET - find charts
  435. inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
  436. netdata_log_debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, rrdhost_hostname(host));
  437. RRDSET *st = rrdset_index_find(host, id);
  438. if(st)
  439. st->last_accessed_time_s = now_realtime_sec();
  440. return(st);
  441. }
  442. inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
  443. netdata_log_debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, rrdhost_hostname(host));
  444. char buf[RRD_ID_LENGTH_MAX + 1];
  445. strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
  446. strcat(buf, ".");
  447. int len = (int) strlen(buf);
  448. strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
  449. return(rrdset_find(host, buf));
  450. }
  451. inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
  452. netdata_log_debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, rrdhost_hostname(host));
  453. RRDSET *st = rrdset_index_find_name(host, name);
  454. return(st);
  455. }
  456. RRDSET_ACQUIRED *rrdset_find_and_acquire(RRDHOST *host, const char *id) {
  457. netdata_log_debug(D_RRD_CALLS, "rrdset_find_and_acquire() for host %s, chart %s", rrdhost_hostname(host), id);
  458. return (RRDSET_ACQUIRED *)dictionary_get_and_acquire_item(host->rrdset_root_index, id);
  459. }
  460. RRDSET *rrdset_acquired_to_rrdset(RRDSET_ACQUIRED *rsa) {
  461. if(unlikely(!rsa))
  462. return NULL;
  463. return (RRDSET *) dictionary_acquired_item_value((const DICTIONARY_ITEM *)rsa);
  464. }
  465. void rrdset_acquired_release(RRDSET_ACQUIRED *rsa) {
  466. if(unlikely(!rsa))
  467. return;
  468. RRDSET *rs = rrdset_acquired_to_rrdset(rsa);
  469. dictionary_acquired_item_release(rs->rrdhost->rrdset_root_index, (const DICTIONARY_ITEM *)rsa);
  470. }
  471. // ----------------------------------------------------------------------------
  472. // RRDSET - rename charts
  473. char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
  474. char c, *p = to;
  475. while (length-- && (c = *from++)) {
  476. if(c != '.' && c != '-' && !isalnum(c))
  477. c = '_';
  478. *p++ = c;
  479. }
  480. *p = '\0';
  481. return to;
  482. }
  483. int rrdset_reset_name(RRDSET *st, const char *name) {
  484. if(unlikely(!strcmp(rrdset_name(st), name)))
  485. return 1;
  486. RRDHOST *host = st->rrdhost;
  487. netdata_log_debug(D_RRD_CALLS, "rrdset_reset_name() old: '%s', new: '%s'", rrdset_name(st), name);
  488. STRING *name_string = rrdset_fix_name(host, rrdset_id(st), rrdset_parts_type(st), string2str(st->name), name);
  489. if(!name_string) return 0;
  490. if(st->name) {
  491. rrdset_index_del_name(host, st);
  492. string_freez(st->name);
  493. st->name = name_string;
  494. rrdsetvar_rename_all(st);
  495. }
  496. else
  497. st->name = name_string;
  498. RRDDIM *rd;
  499. rrddim_foreach_read(rd, st)
  500. rrddimvar_rename_all(rd);
  501. rrddim_foreach_done(rd);
  502. rrdset_index_add_name(host, st);
  503. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_SEND);
  504. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_IGNORE);
  505. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  506. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  507. rrdset_metadata_updated(st);
  508. rrdcontext_updated_rrdset_name(st);
  509. return 2;
  510. }
  511. // get the timestamp of the last entry in the round-robin database
  512. time_t rrdset_last_entry_s(RRDSET *st) {
  513. RRDDIM *rd;
  514. time_t last_entry_s = 0;
  515. rrddim_foreach_read(rd, st) {
  516. time_t t = rrddim_last_entry_s(rd);
  517. if(t > last_entry_s) last_entry_s = t;
  518. }
  519. rrddim_foreach_done(rd);
  520. return last_entry_s;
  521. }
  522. time_t rrdset_last_entry_s_of_tier(RRDSET *st, size_t tier) {
  523. RRDDIM *rd;
  524. time_t last_entry_s = 0;
  525. rrddim_foreach_read(rd, st) {
  526. time_t t = rrddim_last_entry_s_of_tier(rd, tier);
  527. if(t > last_entry_s) last_entry_s = t;
  528. }
  529. rrddim_foreach_done(rd);
  530. return last_entry_s;
  531. }
  532. // get the timestamp of first entry in the round-robin database
  533. time_t rrdset_first_entry_s(RRDSET *st) {
  534. RRDDIM *rd;
  535. time_t first_entry_s = LONG_MAX;
  536. rrddim_foreach_read(rd, st) {
  537. time_t t = rrddim_first_entry_s(rd);
  538. if(t < first_entry_s)
  539. first_entry_s = t;
  540. }
  541. rrddim_foreach_done(rd);
  542. if (unlikely(LONG_MAX == first_entry_s)) return 0;
  543. return first_entry_s;
  544. }
  545. time_t rrdset_first_entry_s_of_tier(RRDSET *st, size_t tier) {
  546. if(unlikely(tier > storage_tiers))
  547. return 0;
  548. RRDDIM *rd;
  549. time_t first_entry_s = LONG_MAX;
  550. rrddim_foreach_read(rd, st) {
  551. time_t t = rrddim_first_entry_s_of_tier(rd, tier);
  552. if(t && t < first_entry_s)
  553. first_entry_s = t;
  554. }
  555. rrddim_foreach_done(rd);
  556. if (unlikely(LONG_MAX == first_entry_s)) return 0;
  557. return first_entry_s;
  558. }
  559. void rrdset_get_retention_of_tier_for_collected_chart(RRDSET *st, time_t *first_time_s, time_t *last_time_s, time_t now_s, size_t tier) {
  560. if(!now_s)
  561. now_s = now_realtime_sec();
  562. time_t db_first_entry_s = rrdset_first_entry_s_of_tier(st, tier);
  563. time_t db_last_entry_s = st->last_updated.tv_sec; // we assume this is a collected RRDSET
  564. if(unlikely(!db_last_entry_s)) {
  565. db_last_entry_s = rrdset_last_entry_s_of_tier(st, tier);
  566. if (unlikely(!db_last_entry_s)) {
  567. // we assume this is a collected RRDSET
  568. db_first_entry_s = 0;
  569. db_last_entry_s = 0;
  570. }
  571. }
  572. if(unlikely(db_last_entry_s > now_s)) {
  573. internal_error(db_last_entry_s > now_s + 1,
  574. "RRDSET: 'host:%s/chart:%s' latest db time %ld is in the future, adjusting it to now %ld",
  575. rrdhost_hostname(st->rrdhost), rrdset_id(st),
  576. db_last_entry_s, now_s);
  577. db_last_entry_s = now_s;
  578. }
  579. if(unlikely(db_first_entry_s && db_last_entry_s && db_first_entry_s >= db_last_entry_s)) {
  580. internal_error(db_first_entry_s > db_last_entry_s,
  581. "RRDSET: 'host:%s/chart:%s' oldest db time %ld is bigger than latest db time %ld, adjusting it to (latest time %ld - update every %ld)",
  582. rrdhost_hostname(st->rrdhost), rrdset_id(st),
  583. db_first_entry_s, db_last_entry_s,
  584. db_last_entry_s, (time_t)st->update_every);
  585. db_first_entry_s = db_last_entry_s - st->update_every;
  586. }
  587. if(unlikely(!db_first_entry_s && db_last_entry_s))
  588. // this can be the case on the first data collection of a chart
  589. db_first_entry_s = db_last_entry_s - st->update_every;
  590. *first_time_s = db_first_entry_s;
  591. *last_time_s = db_last_entry_s;
  592. }
  593. inline void rrdset_is_obsolete___safe_from_collector_thread(RRDSET *st) {
  594. rrdset_pluginsd_receive_unslot(st);
  595. if(unlikely(!(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  596. // netdata_log_info("Setting obsolete flag on chart 'host:%s/chart:%s'",
  597. // rrdhost_hostname(st->rrdhost), rrdset_id(st));
  598. rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE);
  599. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_PENDING_OBSOLETE_CHARTS);
  600. st->last_accessed_time_s = now_realtime_sec();
  601. rrdset_metadata_updated(st);
  602. // the chart will not get more updates (data collection)
  603. // so, we have to push its definition now
  604. rrdset_push_chart_definition_now(st);
  605. rrdcontext_updated_rrdset_flags(st);
  606. }
  607. }
  608. inline void rrdset_isnot_obsolete___safe_from_collector_thread(RRDSET *st) {
  609. if(unlikely((rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  610. // netdata_log_info("Clearing obsolete flag on chart 'host:%s/chart:%s'",
  611. // rrdhost_hostname(st->rrdhost), rrdset_id(st));
  612. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  613. st->last_accessed_time_s = now_realtime_sec();
  614. rrdset_metadata_updated(st);
  615. // the chart will be pushed upstream automatically
  616. // due to data collection
  617. rrdcontext_updated_rrdset_flags(st);
  618. }
  619. }
  620. inline void rrdset_update_heterogeneous_flag(RRDSET *st) {
  621. RRDHOST *host = st->rrdhost;
  622. (void)host;
  623. RRDDIM *rd;
  624. rrdset_flag_clear(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  625. bool init = false, is_heterogeneous = false;
  626. RRD_ALGORITHM algorithm;
  627. int32_t multiplier;
  628. int32_t divisor;
  629. rrddim_foreach_read(rd, st) {
  630. if(!init) {
  631. algorithm = rd->algorithm;
  632. multiplier = rd->multiplier;
  633. divisor = ABS(rd->divisor);
  634. init = true;
  635. continue;
  636. }
  637. if(algorithm != rd->algorithm || multiplier != ABS(rd->multiplier) || divisor != ABS(rd->divisor)) {
  638. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  639. #ifdef NETDATA_INTERNAL_CHECKS
  640. netdata_log_info("Dimension '%s' added on chart '%s' of host '%s' is not homogeneous to other dimensions already present "
  641. "(algorithm is '%s' vs '%s', multiplier is %d vs %d, "
  642. "divisor is %d vs %d).",
  643. rrddim_name(rd),
  644. rrdset_name(st),
  645. rrdhost_hostname(host),
  646. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm),
  647. rd->multiplier, multiplier,
  648. rd->divisor, divisor
  649. );
  650. #endif
  651. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  652. }
  653. is_heterogeneous = true;
  654. break;
  655. }
  656. }
  657. rrddim_foreach_done(rd);
  658. if(!is_heterogeneous) {
  659. rrdset_flag_clear(st, RRDSET_FLAG_HETEROGENEOUS);
  660. rrdcontext_updated_rrdset_flags(st);
  661. }
  662. }
  663. // ----------------------------------------------------------------------------
  664. // RRDSET - reset a chart
  665. void rrdset_reset(RRDSET *st) {
  666. netdata_log_debug(D_RRD_CALLS, "rrdset_reset() %s", rrdset_name(st));
  667. st->last_collected_time.tv_sec = 0;
  668. st->last_collected_time.tv_usec = 0;
  669. st->last_updated.tv_sec = 0;
  670. st->last_updated.tv_usec = 0;
  671. st->db.current_entry = 0;
  672. st->counter = 0;
  673. st->counter_done = 0;
  674. RRDDIM *rd;
  675. rrddim_foreach_read(rd, st) {
  676. rd->collector.last_collected_time.tv_sec = 0;
  677. rd->collector.last_collected_time.tv_usec = 0;
  678. rd->collector.counter = 0;
  679. if(!rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  680. for(size_t tier = 0; tier < storage_tiers ;tier++)
  681. storage_engine_store_flush(rd->tiers[tier].sch);
  682. }
  683. }
  684. rrddim_foreach_done(rd);
  685. }
  686. // ----------------------------------------------------------------------------
  687. // RRDSET - helpers for rrdset_create()
  688. inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
  689. if(mode == RRD_MEMORY_MODE_DBENGINE) return 0;
  690. if(mode == RRD_MEMORY_MODE_NONE) return 5;
  691. if(entries < 5) entries = 5;
  692. if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
  693. if(mode == RRD_MEMORY_MODE_RAM) {
  694. long header_size = 0;
  695. long page = (long)sysconf(_SC_PAGESIZE);
  696. long size = (long)(header_size + entries * sizeof(storage_number));
  697. if (unlikely(size % page)) {
  698. size -= (size % page);
  699. size += page;
  700. long n = (long)((size - header_size) / sizeof(storage_number));
  701. return n;
  702. }
  703. }
  704. return entries;
  705. }
  706. static inline void last_collected_time_align(RRDSET *st) {
  707. st->last_collected_time.tv_sec -= st->last_collected_time.tv_sec % st->update_every;
  708. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)))
  709. st->last_collected_time.tv_usec = 0;
  710. else
  711. st->last_collected_time.tv_usec = 500000;
  712. }
  713. static inline void last_updated_time_align(RRDSET *st) {
  714. st->last_updated.tv_sec -= st->last_updated.tv_sec % st->update_every;
  715. st->last_updated.tv_usec = 0;
  716. }
  717. // ----------------------------------------------------------------------------
  718. // RRDSET - free a chart
  719. void rrdset_free(RRDSET *st) {
  720. if(unlikely(!st)) return;
  721. rrdset_index_del(st->rrdhost, st);
  722. }
  723. // ----------------------------------------------------------------------------
  724. // RRDSET - create a chart
  725. RRDSET *rrdset_create_custom(
  726. RRDHOST *host
  727. , const char *type
  728. , const char *id
  729. , const char *name
  730. , const char *family
  731. , const char *context
  732. , const char *title
  733. , const char *units
  734. , const char *plugin
  735. , const char *module
  736. , long priority
  737. , int update_every
  738. , RRDSET_TYPE chart_type
  739. , RRD_MEMORY_MODE memory_mode
  740. , long history_entries
  741. ) {
  742. if (host != localhost)
  743. host->child_last_chart_command = now_realtime_sec();
  744. if(!type || !type[0])
  745. fatal("Cannot create rrd stats without a type: id '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  746. , (id && *id)?id:"<unset>"
  747. , (name && *name)?name:"<unset>"
  748. , (family && *family)?family:"<unset>"
  749. , (context && *context)?context:"<unset>"
  750. , (title && *title)?title:"<unset>"
  751. , (units && *units)?units:"<unset>"
  752. , (plugin && *plugin)?plugin:"<unset>"
  753. , (module && *module)?module:"<unset>"
  754. );
  755. if(!id || !id[0])
  756. fatal("Cannot create rrd stats without an id: type '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  757. , type
  758. , (name && *name)?name:"<unset>"
  759. , (family && *family)?family:"<unset>"
  760. , (context && *context)?context:"<unset>"
  761. , (title && *title)?title:"<unset>"
  762. , (units && *units)?units:"<unset>"
  763. , (plugin && *plugin)?plugin:"<unset>"
  764. , (module && *module)?module:"<unset>"
  765. );
  766. // ------------------------------------------------------------------------
  767. // check if it already exists
  768. char full_id[RRD_ID_LENGTH_MAX + 1];
  769. snprintfz(full_id, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
  770. // ------------------------------------------------------------------------
  771. // allocate it
  772. netdata_log_debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
  773. struct rrdset_constructor tmp = {
  774. .host = host,
  775. .type = type,
  776. .id = id,
  777. .name = name,
  778. .family = family,
  779. .context = context,
  780. .title = title,
  781. .units = units,
  782. .plugin = plugin,
  783. .module = module,
  784. .priority = priority,
  785. .update_every = update_every,
  786. .chart_type = chart_type,
  787. .memory_mode = memory_mode,
  788. .history_entries = history_entries,
  789. };
  790. RRDSET *st = rrdset_index_add(host, full_id, &tmp);
  791. return(st);
  792. }
  793. // ----------------------------------------------------------------------------
  794. // RRDSET - data collection iteration control
  795. void rrdset_timed_next(RRDSET *st, struct timeval now, usec_t duration_since_last_update) {
  796. #ifdef NETDATA_INTERNAL_CHECKS
  797. char *discard_reason = NULL;
  798. usec_t discarded = duration_since_last_update;
  799. #endif
  800. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_SYNC_CLOCK))) {
  801. // the chart needs to be re-synced to current time
  802. rrdset_flag_clear(st, RRDSET_FLAG_SYNC_CLOCK);
  803. // discard the duration supplied
  804. duration_since_last_update = 0;
  805. #ifdef NETDATA_INTERNAL_CHECKS
  806. if(!discard_reason) discard_reason = "SYNC CLOCK FLAG";
  807. #endif
  808. }
  809. if(unlikely(!st->last_collected_time.tv_sec)) {
  810. // the first entry
  811. duration_since_last_update = st->update_every * USEC_PER_SEC;
  812. #ifdef NETDATA_INTERNAL_CHECKS
  813. if(!discard_reason) discard_reason = "FIRST DATA COLLECTION";
  814. #endif
  815. }
  816. else if(unlikely(!duration_since_last_update)) {
  817. // no dt given by the plugin
  818. duration_since_last_update = dt_usec(&now, &st->last_collected_time);
  819. #ifdef NETDATA_INTERNAL_CHECKS
  820. if(!discard_reason) discard_reason = "NO USEC GIVEN BY COLLECTOR";
  821. #endif
  822. }
  823. else {
  824. // microseconds has the time since the last collection
  825. susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
  826. if(unlikely(since_last_usec < 0)) {
  827. // oops! the database is in the future
  828. #ifdef NETDATA_INTERNAL_CHECKS
  829. netdata_log_info("RRD database for chart '%s' on host '%s' is %0.5" NETDATA_DOUBLE_MODIFIER
  830. " secs in the future (counter #%u, update #%u). Adjusting it to current time."
  831. , rrdset_id(st)
  832. , rrdhost_hostname(st->rrdhost)
  833. , (NETDATA_DOUBLE)-since_last_usec / USEC_PER_SEC
  834. , st->counter
  835. , st->counter_done
  836. );
  837. #endif
  838. duration_since_last_update = 0;
  839. #ifdef NETDATA_INTERNAL_CHECKS
  840. if(!discard_reason) discard_reason = "COLLECTION TIME IN FUTURE";
  841. #endif
  842. }
  843. else if(unlikely((usec_t)since_last_usec > (usec_t)(st->update_every * 5 * USEC_PER_SEC))) {
  844. // oops! the database is too far behind
  845. #ifdef NETDATA_INTERNAL_CHECKS
  846. netdata_log_info("RRD database for chart '%s' on host '%s' is %0.5" NETDATA_DOUBLE_MODIFIER
  847. " secs in the past (counter #%u, update #%u). Adjusting it to current time.",
  848. rrdset_id(st), rrdhost_hostname(st->rrdhost), (NETDATA_DOUBLE)since_last_usec / USEC_PER_SEC,
  849. st->counter, st->counter_done);
  850. #endif
  851. duration_since_last_update = (usec_t)since_last_usec;
  852. #ifdef NETDATA_INTERNAL_CHECKS
  853. if(!discard_reason) discard_reason = "COLLECTION TIME TOO FAR IN THE PAST";
  854. #endif
  855. }
  856. #ifdef NETDATA_INTERNAL_CHECKS
  857. if(since_last_usec > 0 && (susec_t) duration_since_last_update < since_last_usec) {
  858. static __thread susec_t min_delta = USEC_PER_SEC * 3600, permanent_min_delta = 0;
  859. static __thread time_t last_time_s = 0;
  860. // the first time initialize it so that it will make the check later
  861. if(last_time_s == 0) last_time_s = now.tv_sec + 60;
  862. susec_t delta = since_last_usec - (susec_t) duration_since_last_update;
  863. if(delta < min_delta) min_delta = delta;
  864. if(now.tv_sec >= last_time_s + 60) {
  865. last_time_s = now.tv_sec;
  866. if(min_delta > permanent_min_delta) {
  867. netdata_log_info("MINIMUM MICROSECONDS DELTA of thread %d increased from %"PRIi64" to %"PRIi64" (+%"PRIi64")", gettid(), permanent_min_delta, min_delta, min_delta - permanent_min_delta);
  868. permanent_min_delta = min_delta;
  869. }
  870. min_delta = USEC_PER_SEC * 3600;
  871. }
  872. }
  873. #endif
  874. }
  875. netdata_log_debug(D_RRD_CALLS, "rrdset_timed_next() for chart %s with duration since last update %"PRIu64" usec", rrdset_name(st), duration_since_last_update);
  876. rrdset_debug(st, "NEXT: %"PRIu64" microseconds", duration_since_last_update);
  877. internal_error(discarded && discarded != duration_since_last_update,
  878. "host '%s', chart '%s': discarded data collection time of %"PRIu64" usec, "
  879. "replaced with %"PRIu64" usec, reason: '%s'"
  880. , rrdhost_hostname(st->rrdhost)
  881. , rrdset_id(st)
  882. , discarded
  883. , duration_since_last_update
  884. , discard_reason?discard_reason:"UNDEFINED"
  885. );
  886. st->usec_since_last_update = duration_since_last_update;
  887. }
  888. inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t duration_since_last_update) {
  889. if(unlikely(!st->last_collected_time.tv_sec || !duration_since_last_update || (rrdset_flag_check(st, RRDSET_FLAG_SYNC_CLOCK)))) {
  890. // call the full next_usec() function
  891. rrdset_next_usec(st, duration_since_last_update);
  892. return;
  893. }
  894. st->usec_since_last_update = duration_since_last_update;
  895. }
  896. inline void rrdset_next_usec(RRDSET *st, usec_t duration_since_last_update) {
  897. struct timeval now;
  898. now_realtime_timeval(&now);
  899. rrdset_timed_next(st, now, duration_since_last_update);
  900. }
  901. // ----------------------------------------------------------------------------
  902. // RRDSET - process the collected values for all dimensions of a chart
  903. static inline usec_t rrdset_init_last_collected_time(RRDSET *st, struct timeval now) {
  904. st->last_collected_time = now;
  905. last_collected_time_align(st);
  906. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  907. rrdset_debug(st, "initialized last collected time to %0.3" NETDATA_DOUBLE_MODIFIER, (NETDATA_DOUBLE)last_collect_ut / USEC_PER_SEC);
  908. return last_collect_ut;
  909. }
  910. static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
  911. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  912. usec_t ut = last_collect_ut + st->usec_since_last_update;
  913. st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
  914. st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
  915. rrdset_debug(st, "updated last collected time to %0.3" NETDATA_DOUBLE_MODIFIER, (NETDATA_DOUBLE)last_collect_ut / USEC_PER_SEC);
  916. return last_collect_ut;
  917. }
  918. static inline void rrdset_init_last_updated_time(RRDSET *st) {
  919. // copy the last collected time to last updated time
  920. st->last_updated.tv_sec = st->last_collected_time.tv_sec;
  921. st->last_updated.tv_usec = st->last_collected_time.tv_usec;
  922. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  923. st->last_updated.tv_sec -= st->update_every;
  924. last_updated_time_align(st);
  925. }
  926. static __thread size_t rrdset_done_statistics_points_stored_per_tier[RRD_STORAGE_TIERS];
  927. static inline time_t tier_next_point_time_s(RRDDIM *rd, struct rrddim_tier *t, time_t now_s) {
  928. time_t loop = (time_t)rd->rrdset->update_every * (time_t)t->tier_grouping;
  929. return now_s + loop - ((now_s + loop) % loop);
  930. }
  931. void store_metric_at_tier(RRDDIM *rd, size_t tier, struct rrddim_tier *t, STORAGE_POINT sp, usec_t now_ut __maybe_unused) {
  932. if (unlikely(!t->next_point_end_time_s))
  933. t->next_point_end_time_s = tier_next_point_time_s(rd, t, sp.end_time_s);
  934. if(unlikely(sp.start_time_s >= t->next_point_end_time_s)) {
  935. // flush the virtual point, it is done
  936. if (likely(!storage_point_is_unset(t->virtual_point))) {
  937. storage_engine_store_metric(
  938. t->sch,
  939. t->next_point_end_time_s * USEC_PER_SEC,
  940. t->virtual_point.sum,
  941. t->virtual_point.min,
  942. t->virtual_point.max,
  943. t->virtual_point.count,
  944. t->virtual_point.anomaly_count,
  945. t->virtual_point.flags);
  946. }
  947. else {
  948. storage_engine_store_metric(
  949. t->sch,
  950. t->next_point_end_time_s * USEC_PER_SEC,
  951. NAN,
  952. NAN,
  953. NAN,
  954. 0,
  955. 0, SN_FLAG_NONE);
  956. }
  957. rrdset_done_statistics_points_stored_per_tier[tier]++;
  958. t->virtual_point.count = 0; // make the point unset
  959. t->next_point_end_time_s = tier_next_point_time_s(rd, t, sp.end_time_s);
  960. }
  961. // merge the dates into our virtual point
  962. if (unlikely(sp.start_time_s < t->virtual_point.start_time_s))
  963. t->virtual_point.start_time_s = sp.start_time_s;
  964. if (likely(sp.end_time_s > t->virtual_point.end_time_s))
  965. t->virtual_point.end_time_s = sp.end_time_s;
  966. // merge the values into our virtual point
  967. if (likely(!storage_point_is_gap(sp))) {
  968. // we aggregate only non NULLs into higher tiers
  969. if (likely(!storage_point_is_unset(t->virtual_point))) {
  970. // merge the collected point to our virtual one
  971. t->virtual_point.sum += sp.sum;
  972. t->virtual_point.min = MIN(t->virtual_point.min, sp.min);
  973. t->virtual_point.max = MAX(t->virtual_point.max, sp.max);
  974. t->virtual_point.count += sp.count;
  975. t->virtual_point.anomaly_count += sp.anomaly_count;
  976. t->virtual_point.flags |= sp.flags;
  977. }
  978. else {
  979. // reset our virtual point to this one
  980. t->virtual_point = sp;
  981. }
  982. }
  983. }
  984. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  985. void rrddim_store_metric_with_trace(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags, const char *function) {
  986. #else // !NETDATA_LOG_COLLECTION_ERRORS
  987. void rrddim_store_metric(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags) {
  988. #endif // !NETDATA_LOG_COLLECTION_ERRORS
  989. static __thread struct log_stack_entry lgs[] = {
  990. [0] = ND_LOG_FIELD_STR(NDF_NIDL_DIMENSION, NULL),
  991. [1] = ND_LOG_FIELD_END(),
  992. };
  993. lgs[0].str = rd->id;
  994. log_stack_push(lgs);
  995. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  996. rd->rrddim_store_metric_count++;
  997. if(likely(rd->rrddim_store_metric_count > 1)) {
  998. usec_t expected = rd->rrddim_store_metric_last_ut + rd->update_every * USEC_PER_SEC;
  999. if(point_end_time_ut != rd->rrddim_store_metric_last_ut) {
  1000. internal_error(true,
  1001. "%s COLLECTION: 'host:%s/chart:%s/dim:%s' granularity %d, collection %zu, expected to store at tier 0 a value at %llu, but it gave %llu [%s%llu usec] (called from %s(), previously by %s())",
  1002. (point_end_time_ut < rd->rrddim_store_metric_last_ut) ? "**PAST**" : "GAP",
  1003. rrdhost_hostname(rd->rrdset->rrdhost), rrdset_id(rd->rrdset), rrddim_id(rd),
  1004. rd->update_every,
  1005. rd->rrddim_store_metric_count,
  1006. expected, point_end_time_ut,
  1007. (point_end_time_ut < rd->rrddim_store_metric_last_ut)?"by -" : "gap ",
  1008. expected - point_end_time_ut,
  1009. function,
  1010. rd->rrddim_store_metric_last_caller?rd->rrddim_store_metric_last_caller:"none");
  1011. }
  1012. }
  1013. rd->rrddim_store_metric_last_ut = point_end_time_ut;
  1014. rd->rrddim_store_metric_last_caller = function;
  1015. #endif // NETDATA_LOG_COLLECTION_ERRORS
  1016. // store the metric on tier 0
  1017. storage_engine_store_metric(rd->tiers[0].sch, point_end_time_ut,
  1018. n, 0, 0,
  1019. 1, 0, flags);
  1020. rrdset_done_statistics_points_stored_per_tier[0]++;
  1021. time_t now_s = (time_t)(point_end_time_ut / USEC_PER_SEC);
  1022. STORAGE_POINT sp = {
  1023. .start_time_s = now_s - rd->rrdset->update_every,
  1024. .end_time_s = now_s,
  1025. .min = n,
  1026. .max = n,
  1027. .sum = n,
  1028. .count = 1,
  1029. .anomaly_count = (flags & SN_FLAG_NOT_ANOMALOUS) ? 0 : 1,
  1030. .flags = flags
  1031. };
  1032. for(size_t tier = 1; tier < storage_tiers ;tier++) {
  1033. if(unlikely(!rd->tiers[tier].smh)) continue;
  1034. struct rrddim_tier *t = &rd->tiers[tier];
  1035. if(!rrddim_option_check(rd, RRDDIM_OPTION_BACKFILLED_HIGH_TIERS)) {
  1036. // we have not collected this tier before
  1037. // let's fill any gap that may exist
  1038. rrdr_fill_tier_gap_from_smaller_tiers(rd, tier, now_s);
  1039. rrddim_option_set(rd, RRDDIM_OPTION_BACKFILLED_HIGH_TIERS);
  1040. }
  1041. store_metric_at_tier(rd, tier, t, sp, point_end_time_ut);
  1042. }
  1043. rrdcontext_collected_rrddim(rd);
  1044. log_stack_pop(&lgs);
  1045. }
  1046. void store_metric_collection_completed() {
  1047. global_statistics_rrdset_done_chart_collection_completed(rrdset_done_statistics_points_stored_per_tier);
  1048. }
  1049. // caching of dimensions rrdset_done() and rrdset_done_interpolate() loop through
  1050. struct rda_item {
  1051. const DICTIONARY_ITEM *item;
  1052. RRDDIM *rd;
  1053. };
  1054. static __thread struct rda_item *thread_rda = NULL;
  1055. static __thread size_t thread_rda_entries = 0;
  1056. struct rda_item *rrdset_thread_rda_get(size_t *dimensions) {
  1057. if(unlikely(!thread_rda || (*dimensions) > thread_rda_entries)) {
  1058. size_t old_mem = thread_rda_entries * sizeof(struct rda_item);
  1059. freez(thread_rda);
  1060. thread_rda_entries = *dimensions;
  1061. size_t new_mem = thread_rda_entries * sizeof(struct rda_item);
  1062. thread_rda = mallocz(new_mem);
  1063. __atomic_add_fetch(&netdata_buffers_statistics.rrdset_done_rda_size, new_mem - old_mem, __ATOMIC_RELAXED);
  1064. }
  1065. *dimensions = thread_rda_entries;
  1066. return thread_rda;
  1067. }
  1068. void rrdset_thread_rda_free(void) {
  1069. __atomic_sub_fetch(&netdata_buffers_statistics.rrdset_done_rda_size, thread_rda_entries * sizeof(struct rda_item), __ATOMIC_RELAXED);
  1070. freez(thread_rda);
  1071. thread_rda = NULL;
  1072. thread_rda_entries = 0;
  1073. }
  1074. static inline size_t rrdset_done_interpolate(
  1075. RRDSET_STREAM_BUFFER *rsb
  1076. , RRDSET *st
  1077. , struct rda_item *rda_base
  1078. , size_t rda_slots
  1079. , usec_t update_every_ut
  1080. , usec_t last_stored_ut
  1081. , usec_t next_store_ut
  1082. , usec_t last_collect_ut
  1083. , usec_t now_collect_ut
  1084. , char store_this_entry
  1085. , uint32_t has_reset_value
  1086. ) {
  1087. RRDDIM *rd;
  1088. size_t stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
  1089. usec_t first_ut = last_stored_ut, last_ut = 0;
  1090. (void)first_ut;
  1091. ssize_t iterations = (ssize_t)((now_collect_ut - last_stored_ut) / (update_every_ut));
  1092. if((now_collect_ut % (update_every_ut)) == 0) iterations++;
  1093. size_t counter = st->counter;
  1094. long current_entry = st->db.current_entry;
  1095. SN_FLAGS storage_flags = SN_DEFAULT_FLAGS;
  1096. if (has_reset_value)
  1097. storage_flags |= SN_FLAG_RESET;
  1098. for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
  1099. internal_error(iterations < 0,
  1100. "RRDSET: '%s': iterations calculation wrapped! "
  1101. "first_ut = %"PRIu64", last_stored_ut = %"PRIu64", next_store_ut = %"PRIu64", now_collect_ut = %"PRIu64""
  1102. , rrdset_id(st)
  1103. , first_ut
  1104. , last_stored_ut
  1105. , next_store_ut
  1106. , now_collect_ut
  1107. );
  1108. rrdset_debug(st, "last_stored_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (last updated time)", (NETDATA_DOUBLE)last_stored_ut/USEC_PER_SEC);
  1109. rrdset_debug(st, "next_store_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (next interpolation point)", (NETDATA_DOUBLE)next_store_ut/USEC_PER_SEC);
  1110. last_ut = next_store_ut;
  1111. ml_chart_update_begin(st);
  1112. struct rda_item *rda;
  1113. size_t dim_id;
  1114. for(dim_id = 0, rda = rda_base ; dim_id < rda_slots ; ++dim_id, ++rda) {
  1115. rd = rda->rd;
  1116. if(unlikely(!rd)) continue;
  1117. NETDATA_DOUBLE new_value;
  1118. switch(rd->algorithm) {
  1119. case RRD_ALGORITHM_INCREMENTAL:
  1120. new_value = (NETDATA_DOUBLE)
  1121. ( rd->collector.calculated_value
  1122. * (NETDATA_DOUBLE)(next_store_ut - last_collect_ut)
  1123. / (NETDATA_DOUBLE)(now_collect_ut - last_collect_ut)
  1124. );
  1125. rrdset_debug(st, "%s: CALC2 INC " NETDATA_DOUBLE_FORMAT " = "
  1126. NETDATA_DOUBLE_FORMAT
  1127. " * (%"PRIu64" - %"PRIu64")"
  1128. " / (%"PRIu64" - %"PRIu64""
  1129. , rrddim_name(rd)
  1130. , new_value
  1131. , rd->collector.calculated_value
  1132. , next_store_ut, last_collect_ut
  1133. , now_collect_ut, last_collect_ut
  1134. );
  1135. rd->collector.calculated_value -= new_value;
  1136. new_value += rd->collector.last_calculated_value;
  1137. rd->collector.last_calculated_value = 0;
  1138. new_value /= (NETDATA_DOUBLE)st->update_every;
  1139. if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
  1140. rrdset_debug(st, "%s: COLLECTION POINT IS SHORT " NETDATA_DOUBLE_FORMAT " - EXTRAPOLATING",
  1141. rrddim_name(rd)
  1142. , (NETDATA_DOUBLE)(next_store_ut - last_stored_ut)
  1143. );
  1144. new_value = new_value * (NETDATA_DOUBLE)(st->update_every * USEC_PER_SEC) / (NETDATA_DOUBLE)(next_store_ut - last_stored_ut);
  1145. }
  1146. break;
  1147. case RRD_ALGORITHM_ABSOLUTE:
  1148. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1149. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1150. default:
  1151. if(iterations == 1) {
  1152. // this is the last iteration
  1153. // do not interpolate
  1154. // just show the calculated value
  1155. new_value = rd->collector.calculated_value;
  1156. }
  1157. else {
  1158. // we have missed an update
  1159. // interpolate in the middle values
  1160. new_value = (NETDATA_DOUBLE)
  1161. ( ( (rd->collector.calculated_value - rd->collector.last_calculated_value)
  1162. * (NETDATA_DOUBLE)(next_store_ut - last_collect_ut)
  1163. / (NETDATA_DOUBLE)(now_collect_ut - last_collect_ut)
  1164. )
  1165. + rd->collector.last_calculated_value
  1166. );
  1167. rrdset_debug(st, "%s: CALC2 DEF " NETDATA_DOUBLE_FORMAT " = ((("
  1168. "(" NETDATA_DOUBLE_FORMAT " - " NETDATA_DOUBLE_FORMAT ")"
  1169. " * %"PRIu64""
  1170. " / %"PRIu64") + " NETDATA_DOUBLE_FORMAT, rrddim_name(rd)
  1171. , new_value
  1172. , rd->collector.calculated_value, rd->collector.last_calculated_value
  1173. , (next_store_ut - first_ut)
  1174. , (now_collect_ut - first_ut), rd->collector.last_calculated_value
  1175. );
  1176. }
  1177. break;
  1178. }
  1179. time_t current_time_s = (time_t) (next_store_ut / USEC_PER_SEC);
  1180. if(unlikely(!store_this_entry)) {
  1181. (void) ml_dimension_is_anomalous(rd, current_time_s, 0, false);
  1182. if(rsb->wb && rsb->v2)
  1183. rrddim_push_metrics_v2(rsb, rd, next_store_ut, NAN, SN_FLAG_NONE);
  1184. rrddim_store_metric(rd, next_store_ut, NAN, SN_FLAG_NONE);
  1185. continue;
  1186. }
  1187. if(likely(rrddim_check_updated(rd) && rd->collector.counter > 1 && iterations < gap_when_lost_iterations_above)) {
  1188. uint32_t dim_storage_flags = storage_flags;
  1189. if (ml_dimension_is_anomalous(rd, current_time_s, new_value, true)) {
  1190. // clear anomaly bit: 0 -> is anomalous, 1 -> not anomalous
  1191. dim_storage_flags &= ~((storage_number)SN_FLAG_NOT_ANOMALOUS);
  1192. }
  1193. if(rsb->wb && rsb->v2)
  1194. rrddim_push_metrics_v2(rsb, rd, next_store_ut, new_value, dim_storage_flags);
  1195. rrddim_store_metric(rd, next_store_ut, new_value, dim_storage_flags);
  1196. rd->collector.last_stored_value = new_value;
  1197. }
  1198. else {
  1199. (void) ml_dimension_is_anomalous(rd, current_time_s, 0, false);
  1200. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING ", rrddim_name(rd), current_entry);
  1201. if(rsb->wb && rsb->v2)
  1202. rrddim_push_metrics_v2(rsb, rd, next_store_ut, NAN, SN_FLAG_NONE);
  1203. rrddim_store_metric(rd, next_store_ut, NAN, SN_FLAG_NONE);
  1204. rd->collector.last_stored_value = NAN;
  1205. }
  1206. stored_entries++;
  1207. }
  1208. ml_chart_update_end(st);
  1209. // reset the storage flags for the next point, if any;
  1210. storage_flags = SN_DEFAULT_FLAGS;
  1211. st->counter = ++counter;
  1212. st->db.current_entry = current_entry = ((current_entry + 1) >= st->db.entries) ? 0 : current_entry + 1;
  1213. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  1214. st->last_updated.tv_usec = 0;
  1215. last_stored_ut = next_store_ut;
  1216. }
  1217. /*
  1218. st->counter = counter;
  1219. st->current_entry = current_entry;
  1220. if(likely(last_ut)) {
  1221. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  1222. st->last_updated.tv_usec = 0;
  1223. }
  1224. */
  1225. return stored_entries;
  1226. }
  1227. void rrdset_done(RRDSET *st) {
  1228. struct timeval now;
  1229. now_realtime_timeval(&now);
  1230. rrdset_timed_done(st, now, /* pending_rrdset_next = */ st->counter_done != 0);
  1231. }
  1232. void rrdset_timed_done(RRDSET *st, struct timeval now, bool pending_rrdset_next) {
  1233. if(unlikely(!service_running(SERVICE_COLLECTORS))) return;
  1234. RRDSET_STREAM_BUFFER stream_buffer = { .wb = NULL, };
  1235. if(unlikely(rrdhost_has_rrdpush_sender_enabled(st->rrdhost)))
  1236. stream_buffer = rrdset_push_metric_initialize(st, now.tv_sec);
  1237. spinlock_lock(&st->data_collection_lock);
  1238. if (pending_rrdset_next)
  1239. rrdset_timed_next(st, now, 0ULL);
  1240. netdata_log_debug(D_RRD_CALLS, "rrdset_done() for chart '%s'", rrdset_name(st));
  1241. RRDDIM *rd;
  1242. char
  1243. store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
  1244. first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
  1245. usec_t
  1246. last_collect_ut = 0, // the timestamp in microseconds, of the last collected value
  1247. now_collect_ut = 0, // the timestamp in microseconds, of this collected value (this is NOW)
  1248. last_stored_ut = 0, // the timestamp in microseconds, of the last stored entry in the db
  1249. next_store_ut = 0, // the timestamp in microseconds, of the next entry to store in the db
  1250. update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  1251. RRDSET_FLAGS rrdset_flags = rrdset_flag_check(st, ~0);
  1252. if(unlikely(rrdset_flags & RRDSET_FLAG_COLLECTION_FINISHED)) {
  1253. spinlock_unlock(&st->data_collection_lock);
  1254. return;
  1255. }
  1256. if (unlikely(rrdset_flags & RRDSET_FLAG_OBSOLETE)) {
  1257. netdata_log_error("Chart '%s' has the OBSOLETE flag set, but it is collected.", rrdset_id(st));
  1258. rrdset_isnot_obsolete___safe_from_collector_thread(st);
  1259. }
  1260. // check if the chart has a long time to be updated
  1261. if(unlikely(st->usec_since_last_update > MAX(st->db.entries, 60) * update_every_ut)) {
  1262. nd_log_daemon(NDLP_DEBUG, "host '%s', chart '%s': took too long to be updated (counter #%u, update #%u, %0.3" NETDATA_DOUBLE_MODIFIER
  1263. " secs). Resetting it.", rrdhost_hostname(st->rrdhost), rrdset_id(st), st->counter, st->counter_done,
  1264. (NETDATA_DOUBLE)st->usec_since_last_update / USEC_PER_SEC);
  1265. rrdset_reset(st);
  1266. st->usec_since_last_update = update_every_ut;
  1267. store_this_entry = 0;
  1268. first_entry = 1;
  1269. }
  1270. rrdset_debug(st, "microseconds since last update: %"PRIu64"", st->usec_since_last_update);
  1271. // set last_collected_time
  1272. if(unlikely(!st->last_collected_time.tv_sec)) {
  1273. // it is the first entry
  1274. // set the last_collected_time to now
  1275. last_collect_ut = rrdset_init_last_collected_time(st, now) - update_every_ut;
  1276. // the first entry should not be stored
  1277. store_this_entry = 0;
  1278. first_entry = 1;
  1279. }
  1280. else {
  1281. // it is not the first entry
  1282. // calculate the proper last_collected_time, using usec_since_last_update
  1283. last_collect_ut = rrdset_update_last_collected_time(st);
  1284. }
  1285. // if this set has not been updated in the past
  1286. // we fake the last_update time to be = now - usec_since_last_update
  1287. if(unlikely(!st->last_updated.tv_sec)) {
  1288. // it has never been updated before
  1289. // set a fake last_updated, in the past using usec_since_last_update
  1290. rrdset_init_last_updated_time(st);
  1291. // the first entry should not be stored
  1292. store_this_entry = 0;
  1293. first_entry = 1;
  1294. }
  1295. // check if we will re-write the entire data set
  1296. if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->db.entries * update_every_ut &&
  1297. st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
  1298. nd_log_daemon(NDLP_DEBUG, "'%s': too old data (last updated at %" PRId64 ".%" PRId64 ", last collected at %" PRId64 ".%" PRId64 "). "
  1299. "Resetting it. Will not store the next entry.",
  1300. rrdset_id(st),
  1301. (int64_t)st->last_updated.tv_sec,
  1302. (int64_t)st->last_updated.tv_usec,
  1303. (int64_t)st->last_collected_time.tv_sec,
  1304. (int64_t)st->last_collected_time.tv_usec);
  1305. rrdset_reset(st);
  1306. rrdset_init_last_updated_time(st);
  1307. st->usec_since_last_update = update_every_ut;
  1308. // the first entry should not be stored
  1309. store_this_entry = 0;
  1310. first_entry = 1;
  1311. }
  1312. // these are the 3 variables that will help us in interpolation
  1313. // last_stored_ut = the last time we added a value to the storage
  1314. // now_collect_ut = the time the current value has been collected
  1315. // next_store_ut = the time of the next interpolation point
  1316. now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  1317. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1318. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1319. if(unlikely(!st->counter_done)) {
  1320. // set a fake last_updated to jump to current time
  1321. rrdset_init_last_updated_time(st);
  1322. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1323. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1324. if(unlikely(rrdset_flags & RRDSET_FLAG_STORE_FIRST)) {
  1325. store_this_entry = 1;
  1326. last_collect_ut = next_store_ut - update_every_ut;
  1327. rrdset_debug(st, "Fixed first entry.");
  1328. }
  1329. else {
  1330. store_this_entry = 0;
  1331. rrdset_debug(st, "Will not store the next entry.");
  1332. }
  1333. }
  1334. st->counter_done++;
  1335. if(stream_buffer.wb && !stream_buffer.v2)
  1336. rrdset_push_metrics_v1(&stream_buffer, st);
  1337. uint32_t has_reset_value = 0;
  1338. size_t rda_slots = dictionary_entries(st->rrddim_root_index);
  1339. struct rda_item *rda_base = rrdset_thread_rda_get(&rda_slots);
  1340. size_t dim_id;
  1341. size_t dimensions = 0;
  1342. struct rda_item *rda = rda_base;
  1343. total_number collected_total = 0;
  1344. total_number last_collected_total = 0;
  1345. rrddim_foreach_read(rd, st) {
  1346. if(rd_dfe.counter >= rda_slots)
  1347. break;
  1348. rda = &rda_base[dimensions++];
  1349. if(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  1350. rda->item = NULL;
  1351. rda->rd = NULL;
  1352. continue;
  1353. }
  1354. // store the dimension in the array
  1355. rda->item = dictionary_acquired_item_dup(st->rrddim_root_index, rd_dfe.item);
  1356. rda->rd = dictionary_acquired_item_value(rda->item);
  1357. // calculate totals
  1358. if(likely(rrddim_check_updated(rd))) {
  1359. // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1360. // to reset the calculation (it will give zero as the calculation for this second)
  1361. if(unlikely(rd->algorithm == RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL && rd->collector.last_collected_value > rd->collector.collected_value)) {
  1362. netdata_log_debug(D_RRD_STATS, "'%s' / '%s': RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1363. , rrdset_id(st)
  1364. , rrddim_name(rd)
  1365. , rd->collector.last_collected_value
  1366. , rd->collector.collected_value
  1367. );
  1368. if(!(rrddim_option_check(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1369. has_reset_value = 1;
  1370. rd->collector.last_collected_value = rd->collector.collected_value;
  1371. }
  1372. last_collected_total += rd->collector.last_collected_value;
  1373. collected_total += rd->collector.collected_value;
  1374. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE))) {
  1375. netdata_log_error("Dimension %s in chart '%s' has the OBSOLETE flag set, but it is collected.", rrddim_name(rd), rrdset_id(st));
  1376. rrddim_isnot_obsolete___safe_from_collector_thread(st, rd);
  1377. }
  1378. }
  1379. }
  1380. rrddim_foreach_done(rd);
  1381. rda_slots = dimensions;
  1382. rrdset_debug(st, "last_collect_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (last collection time)", (NETDATA_DOUBLE)last_collect_ut/USEC_PER_SEC);
  1383. rrdset_debug(st, "now_collect_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (current collection time)", (NETDATA_DOUBLE)now_collect_ut/USEC_PER_SEC);
  1384. rrdset_debug(st, "last_stored_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (last updated time)", (NETDATA_DOUBLE)last_stored_ut/USEC_PER_SEC);
  1385. rrdset_debug(st, "next_store_ut = %0.3" NETDATA_DOUBLE_MODIFIER " (next interpolation point)", (NETDATA_DOUBLE)next_store_ut/USEC_PER_SEC);
  1386. // process all dimensions to calculate their values
  1387. // based on the collected figures only
  1388. // at this stage we do not interpolate anything
  1389. for(dim_id = 0, rda = rda_base ; dim_id < rda_slots ; ++dim_id, ++rda) {
  1390. rd = rda->rd;
  1391. if(unlikely(!rd)) continue;
  1392. if(unlikely(!rrddim_check_updated(rd))) {
  1393. rd->collector.calculated_value = 0;
  1394. continue;
  1395. }
  1396. rrdset_debug(st, "%s: START "
  1397. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1398. " collected_value = " COLLECTED_NUMBER_FORMAT
  1399. " last_calculated_value = " NETDATA_DOUBLE_FORMAT
  1400. " calculated_value = " NETDATA_DOUBLE_FORMAT
  1401. , rrddim_name(rd)
  1402. , rd->collector.last_collected_value
  1403. , rd->collector.collected_value
  1404. , rd->collector.last_calculated_value
  1405. , rd->collector.calculated_value
  1406. );
  1407. switch(rd->algorithm) {
  1408. case RRD_ALGORITHM_ABSOLUTE:
  1409. rd->collector.calculated_value = (NETDATA_DOUBLE)rd->collector.collected_value
  1410. * (NETDATA_DOUBLE)rd->multiplier
  1411. / (NETDATA_DOUBLE)rd->divisor;
  1412. rrdset_debug(st, "%s: CALC ABS/ABS-NO-IN " NETDATA_DOUBLE_FORMAT " = "
  1413. COLLECTED_NUMBER_FORMAT
  1414. " * " NETDATA_DOUBLE_FORMAT
  1415. " / " NETDATA_DOUBLE_FORMAT
  1416. , rrddim_name(rd)
  1417. , rd->collector.calculated_value
  1418. , rd->collector.collected_value
  1419. , (NETDATA_DOUBLE)rd->multiplier
  1420. , (NETDATA_DOUBLE)rd->divisor
  1421. );
  1422. break;
  1423. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1424. if(unlikely(!collected_total))
  1425. rd->collector.calculated_value = 0;
  1426. else
  1427. // the percentage of the current value
  1428. // over the total of all dimensions
  1429. rd->collector.calculated_value =
  1430. (NETDATA_DOUBLE)100
  1431. * (NETDATA_DOUBLE)rd->collector.collected_value
  1432. / (NETDATA_DOUBLE)collected_total;
  1433. rrdset_debug(st, "%s: CALC PCENT-ROW " NETDATA_DOUBLE_FORMAT " = 100"
  1434. " * " COLLECTED_NUMBER_FORMAT
  1435. " / " COLLECTED_NUMBER_FORMAT
  1436. , rrddim_name(rd)
  1437. , rd->collector.calculated_value
  1438. , rd->collector.collected_value
  1439. , collected_total
  1440. );
  1441. break;
  1442. case RRD_ALGORITHM_INCREMENTAL:
  1443. if(unlikely(rd->collector.counter <= 1)) {
  1444. rd->collector.calculated_value = 0;
  1445. continue;
  1446. }
  1447. // If the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1448. // to reset the calculation (it will give zero as the calculation for this second).
  1449. // It is imperative to set the comparison to uint64_t since type collected_number is signed and
  1450. // produces wrong results as far as incremental counters are concerned.
  1451. if(unlikely((uint64_t)rd->collector.last_collected_value > (uint64_t)rd->collector.collected_value)) {
  1452. netdata_log_debug(D_RRD_STATS, "'%s' / '%s': RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1453. , rrdset_id(st)
  1454. , rrddim_name(rd)
  1455. , rd->collector.last_collected_value
  1456. , rd->collector.collected_value);
  1457. if(!(rrddim_option_check(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1458. has_reset_value = 1;
  1459. uint64_t last = (uint64_t)rd->collector.last_collected_value;
  1460. uint64_t new = (uint64_t)rd->collector.collected_value;
  1461. uint64_t max = (uint64_t)rd->collector.collected_value_max;
  1462. uint64_t cap = 0;
  1463. // Signed values are handled by exploiting two's complement which will produce positive deltas
  1464. if (max > 0x00000000FFFFFFFFULL)
  1465. cap = 0xFFFFFFFFFFFFFFFFULL; // handles signed and unsigned 64-bit counters
  1466. else
  1467. cap = 0x00000000FFFFFFFFULL; // handles signed and unsigned 32-bit counters
  1468. uint64_t delta = cap - last + new;
  1469. uint64_t max_acceptable_rate = (cap / 100) * MAX_INCREMENTAL_PERCENT_RATE;
  1470. // If the delta is less than the maximum acceptable rate and the previous value was near the cap
  1471. // then this is an overflow. There can be false positives such that a reset is detected as an
  1472. // overflow.
  1473. // TODO: remember recent history of rates and compare with current rate to reduce this chance.
  1474. if (delta < max_acceptable_rate) {
  1475. rd->collector.calculated_value +=
  1476. (NETDATA_DOUBLE) delta
  1477. * (NETDATA_DOUBLE) rd->multiplier
  1478. / (NETDATA_DOUBLE) rd->divisor;
  1479. } else {
  1480. // This is a reset. Any overflow with a rate greater than MAX_INCREMENTAL_PERCENT_RATE will also
  1481. // be detected as a reset instead.
  1482. rd->collector.calculated_value += (NETDATA_DOUBLE)0;
  1483. }
  1484. }
  1485. else {
  1486. rd->collector.calculated_value +=
  1487. (NETDATA_DOUBLE) (rd->collector.collected_value - rd->collector.last_collected_value)
  1488. * (NETDATA_DOUBLE) rd->multiplier
  1489. / (NETDATA_DOUBLE) rd->divisor;
  1490. }
  1491. rrdset_debug(st, "%s: CALC INC PRE " NETDATA_DOUBLE_FORMAT " = ("
  1492. COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
  1493. ")"
  1494. " * " NETDATA_DOUBLE_FORMAT
  1495. " / " NETDATA_DOUBLE_FORMAT
  1496. , rrddim_name(rd)
  1497. , rd->collector.calculated_value
  1498. , rd->collector.collected_value, rd->collector.last_collected_value
  1499. , (NETDATA_DOUBLE)rd->multiplier
  1500. , (NETDATA_DOUBLE)rd->divisor
  1501. );
  1502. break;
  1503. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1504. if(unlikely(rd->collector.counter <= 1)) {
  1505. rd->collector.calculated_value = 0;
  1506. continue;
  1507. }
  1508. // the percentage of the current increment
  1509. // over the increment of all dimensions together
  1510. if(unlikely(collected_total == last_collected_total))
  1511. rd->collector.calculated_value = 0;
  1512. else
  1513. rd->collector.calculated_value =
  1514. (NETDATA_DOUBLE)100
  1515. * (NETDATA_DOUBLE)(rd->collector.collected_value - rd->collector.last_collected_value)
  1516. / (NETDATA_DOUBLE)(collected_total - last_collected_total);
  1517. rrdset_debug(st, "%s: CALC PCENT-DIFF " NETDATA_DOUBLE_FORMAT " = 100"
  1518. " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1519. " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1520. , rrddim_name(rd)
  1521. , rd->collector.calculated_value
  1522. , rd->collector.collected_value, rd->collector.last_collected_value
  1523. , collected_total, last_collected_total
  1524. );
  1525. break;
  1526. default:
  1527. // make the default zero, to make sure
  1528. // it gets noticed when we add new types
  1529. rd->collector.calculated_value = 0;
  1530. rrdset_debug(st, "%s: CALC " NETDATA_DOUBLE_FORMAT " = 0"
  1531. , rrddim_name(rd)
  1532. , rd->collector.calculated_value
  1533. );
  1534. break;
  1535. }
  1536. rrdset_debug(st, "%s: PHASE2 "
  1537. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1538. " collected_value = " COLLECTED_NUMBER_FORMAT
  1539. " last_calculated_value = " NETDATA_DOUBLE_FORMAT
  1540. " calculated_value = " NETDATA_DOUBLE_FORMAT
  1541. , rrddim_name(rd)
  1542. , rd->collector.last_collected_value
  1543. , rd->collector.collected_value
  1544. , rd->collector.last_calculated_value
  1545. , rd->collector.calculated_value
  1546. );
  1547. }
  1548. // at this point we have all the calculated values ready
  1549. // it is now time to interpolate values on a second boundary
  1550. // #ifdef NETDATA_INTERNAL_CHECKS
  1551. // if(unlikely(now_collect_ut < next_store_ut && st->counter_done > 1)) {
  1552. // // this is collected in the same interpolation point
  1553. // rrdset_debug(st, "THIS IS IN THE SAME INTERPOLATION POINT");
  1554. // netdata_log_info("INTERNAL CHECK: host '%s', chart '%s' collection %zu is in the same interpolation point: short by %llu microseconds", st->rrdhost->hostname, rrdset_name(st), st->counter_done, next_store_ut - now_collect_ut);
  1555. // }
  1556. // #endif
  1557. rrdset_done_interpolate(
  1558. &stream_buffer
  1559. , st
  1560. , rda_base
  1561. , rda_slots
  1562. , update_every_ut
  1563. , last_stored_ut
  1564. , next_store_ut
  1565. , last_collect_ut
  1566. , now_collect_ut
  1567. , store_this_entry
  1568. , has_reset_value
  1569. );
  1570. for(dim_id = 0, rda = rda_base ; dim_id < rda_slots ; ++dim_id, ++rda) {
  1571. rd = rda->rd;
  1572. if(unlikely(!rd)) continue;
  1573. if(unlikely(!rrddim_check_updated(rd)))
  1574. continue;
  1575. rrdset_debug(st, "%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", rrddim_name(rd), rd->collector.last_collected_value, rd->collector.collected_value);
  1576. rd->collector.last_collected_value = rd->collector.collected_value;
  1577. switch(rd->algorithm) {
  1578. case RRD_ALGORITHM_INCREMENTAL:
  1579. if(unlikely(!first_entry)) {
  1580. rrdset_debug(st, "%s: setting last_calculated_value (old: " NETDATA_DOUBLE_FORMAT ") to "
  1581. "last_calculated_value (new: " NETDATA_DOUBLE_FORMAT ")"
  1582. , rrddim_name(rd)
  1583. , rd->collector.last_calculated_value + rd->collector.calculated_value
  1584. , rd->collector.calculated_value);
  1585. rd->collector.last_calculated_value += rd->collector.calculated_value;
  1586. }
  1587. else {
  1588. rrdset_debug(st, "THIS IS THE FIRST POINT");
  1589. }
  1590. break;
  1591. case RRD_ALGORITHM_ABSOLUTE:
  1592. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1593. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1594. rrdset_debug(st, "%s: setting last_calculated_value (old: " NETDATA_DOUBLE_FORMAT ") to "
  1595. "last_calculated_value (new: " NETDATA_DOUBLE_FORMAT ")"
  1596. , rrddim_name(rd)
  1597. , rd->collector.last_calculated_value
  1598. , rd->collector.calculated_value);
  1599. rd->collector.last_calculated_value = rd->collector.calculated_value;
  1600. break;
  1601. }
  1602. rd->collector.calculated_value = 0;
  1603. rd->collector.collected_value = 0;
  1604. rrddim_clear_updated(rd);
  1605. rrdset_debug(st, "%s: END "
  1606. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1607. " collected_value = " COLLECTED_NUMBER_FORMAT
  1608. " last_calculated_value = " NETDATA_DOUBLE_FORMAT
  1609. " calculated_value = " NETDATA_DOUBLE_FORMAT
  1610. , rrddim_name(rd)
  1611. , rd->collector.last_collected_value
  1612. , rd->collector.collected_value
  1613. , rd->collector.last_calculated_value
  1614. , rd->collector.calculated_value
  1615. );
  1616. }
  1617. spinlock_unlock(&st->data_collection_lock);
  1618. rrdset_push_metrics_finished(&stream_buffer, st);
  1619. // ALL DONE ABOUT THE DATA UPDATE
  1620. // --------------------------------------------------------------------
  1621. for(dim_id = 0, rda = rda_base; dim_id < rda_slots ; ++dim_id, ++rda) {
  1622. rd = rda->rd;
  1623. if(unlikely(!rd)) continue;
  1624. dictionary_acquired_item_release(st->rrddim_root_index, rda->item);
  1625. rda->item = NULL;
  1626. rda->rd = NULL;
  1627. }
  1628. rrdcontext_collected_rrdset(st);
  1629. store_metric_collection_completed();
  1630. }
  1631. time_t rrdset_set_update_every_s(RRDSET *st, time_t update_every_s) {
  1632. if(unlikely(update_every_s == st->update_every))
  1633. return st->update_every;
  1634. internal_error(true, "RRDSET '%s' switching update every from %d to %d",
  1635. rrdset_id(st), (int)st->update_every, (int)update_every_s);
  1636. time_t prev_update_every_s = (time_t) st->update_every;
  1637. st->update_every = (int) update_every_s;
  1638. // switch update every to the storage engine
  1639. RRDDIM *rd;
  1640. rrddim_foreach_read(rd, st) {
  1641. for (size_t tier = 0; tier < storage_tiers; tier++) {
  1642. if (rd->tiers[tier].sch)
  1643. storage_engine_store_change_collection_frequency(
  1644. rd->tiers[tier].sch,
  1645. (int)(st->rrdhost->db[tier].tier_grouping * st->update_every));
  1646. }
  1647. }
  1648. rrddim_foreach_done(rd);
  1649. return prev_update_every_s;
  1650. }