rrdset.c 81 KB

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