rrdset.c 87 KB

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