rrdset.c 78 KB

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