rrdset.c 86 KB

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