rrdset.c 87 KB

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