rrddim.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. #include "storage_engine.h"
  5. void rrddim_metadata_updated(RRDDIM *rd) {
  6. rrdcontext_updated_rrddim(rd);
  7. rrdset_metadata_updated(rd->rrdset);
  8. }
  9. // ----------------------------------------------------------------------------
  10. // RRDDIM index
  11. struct rrddim_constructor {
  12. RRDSET *st;
  13. const char *id;
  14. const char *name;
  15. collected_number multiplier;
  16. collected_number divisor;
  17. RRD_ALGORITHM algorithm;
  18. RRD_MEMORY_MODE memory_mode;
  19. enum {
  20. RRDDIM_REACT_NONE = 0,
  21. RRDDIM_REACT_NEW = (1 << 0),
  22. RRDDIM_REACT_UPDATED = (1 << 2),
  23. } react_action;
  24. };
  25. // isolated call to appear
  26. // separate in statistics
  27. static void *rrddim_alloc_db(size_t entries) {
  28. return callocz(entries, sizeof(storage_number));
  29. }
  30. static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *constructor_data) {
  31. struct rrddim_constructor *ctr = constructor_data;
  32. RRDDIM *rd = rrddim;
  33. RRDSET *st = ctr->st;
  34. RRDHOST *host = st->rrdhost;
  35. rd->flags = RRDDIM_FLAG_NONE;
  36. rd->id = string_strdupz(ctr->id);
  37. rd->name = (ctr->name && *ctr->name)?rrd_string_strdupz(ctr->name):string_dup(rd->id);
  38. rd->algorithm = ctr->algorithm;
  39. rd->multiplier = ctr->multiplier;
  40. rd->divisor = ctr->divisor;
  41. if(!rd->divisor) rd->divisor = 1;
  42. rd->rrdset = st;
  43. rd->rrdpush.sender.dim_slot = __atomic_add_fetch(&st->rrdpush.sender.dim_last_slot_used, 1, __ATOMIC_RELAXED);
  44. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  45. rd->collector.counter = 1;
  46. if(ctr->memory_mode == RRD_MEMORY_MODE_MAP || ctr->memory_mode == RRD_MEMORY_MODE_SAVE) {
  47. if(!rrddim_memory_load_or_create_map_save(st, rd, ctr->memory_mode)) {
  48. netdata_log_info("Failed to use memory mode %s for chart '%s', dimension '%s', falling back to ram", (ctr->memory_mode == RRD_MEMORY_MODE_MAP)?"map":"save", rrdset_name(st), rrddim_name(rd));
  49. ctr->memory_mode = RRD_MEMORY_MODE_RAM;
  50. }
  51. }
  52. if(ctr->memory_mode == RRD_MEMORY_MODE_RAM) {
  53. size_t entries = st->db.entries;
  54. if(!entries) entries = 5;
  55. rd->db.data = netdata_mmap(NULL, entries * sizeof(storage_number), MAP_PRIVATE, 1, false, NULL);
  56. if(!rd->db.data) {
  57. netdata_log_info("Failed to use memory mode ram for chart '%s', dimension '%s', falling back to alloc", rrdset_name(st), rrddim_name(rd));
  58. ctr->memory_mode = RRD_MEMORY_MODE_ALLOC;
  59. }
  60. else {
  61. rd->db.memsize = entries * sizeof(storage_number);
  62. __atomic_add_fetch(&rrddim_db_memory_size, rd->db.memsize, __ATOMIC_RELAXED);
  63. }
  64. }
  65. if(ctr->memory_mode == RRD_MEMORY_MODE_ALLOC || ctr->memory_mode == RRD_MEMORY_MODE_NONE) {
  66. size_t entries = st->db.entries;
  67. if(entries < 5) entries = 5;
  68. rd->db.data = rrddim_alloc_db(entries);
  69. rd->db.memsize = entries * sizeof(storage_number);
  70. __atomic_add_fetch(&rrddim_db_memory_size, rd->db.memsize, __ATOMIC_RELAXED);
  71. }
  72. rd->rrd_memory_mode = ctr->memory_mode;
  73. if (unlikely(rrdcontext_find_dimension_uuid(st, rrddim_id(rd), &(rd->metric_uuid))))
  74. uuid_generate(rd->metric_uuid);
  75. // initialize the db tiers
  76. {
  77. size_t initialized = 0;
  78. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  79. STORAGE_ENGINE *eng = host->db[tier].eng;
  80. rd->tiers[tier].backend = eng->backend;
  81. rd->tiers[tier].tier_grouping = host->db[tier].tier_grouping;
  82. rd->tiers[tier].db_metric_handle = eng->api.metric_get_or_create(rd, host->db[tier].instance);
  83. storage_point_unset(rd->tiers[tier].virtual_point);
  84. initialized++;
  85. // internal_error(true, "TIER GROUPING of chart '%s', dimension '%s' for tier %d is set to %d", rd->rrdset->name, rd->name, tier, rd->tiers[tier]->tier_grouping);
  86. }
  87. if(!initialized)
  88. netdata_log_error("Failed to initialize all db tiers for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
  89. if(!rd->tiers[0].db_metric_handle)
  90. netdata_log_error("Failed to initialize the first db tier for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
  91. }
  92. // initialize data collection for all tiers
  93. {
  94. size_t initialized = 0;
  95. for (size_t tier = 0; tier < storage_tiers; tier++) {
  96. if (rd->tiers[tier].db_metric_handle) {
  97. rd->tiers[tier].db_collection_handle =
  98. storage_metric_store_init(rd->tiers[tier].backend, rd->tiers[tier].db_metric_handle, st->rrdhost->db[tier].tier_grouping * st->update_every, rd->rrdset->storage_metrics_groups[tier]);
  99. initialized++;
  100. }
  101. }
  102. if(!initialized)
  103. netdata_log_error("Failed to initialize data collection for all db tiers for chart '%s', dimension '%s", rrdset_name(st), rrddim_name(rd));
  104. }
  105. if(rrdset_number_of_dimensions(st) != 0) {
  106. RRDDIM *td;
  107. dfe_start_write(st->rrddim_root_index, td) {
  108. if(td) break;
  109. }
  110. dfe_done(td);
  111. if(td && (td->algorithm != rd->algorithm || ABS(td->multiplier) != ABS(rd->multiplier) || ABS(td->divisor) != ABS(rd->divisor))) {
  112. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  113. #ifdef NETDATA_INTERNAL_CHECKS
  114. netdata_log_info("Dimension '%s' added on chart '%s' of host '%s' is not homogeneous to other dimensions already "
  115. "present (algorithm is '%s' vs '%s', multiplier is %d vs %d, "
  116. "divisor is %d vs %d).",
  117. rrddim_name(rd),
  118. rrdset_name(st),
  119. rrdhost_hostname(host),
  120. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(td->algorithm),
  121. rd->multiplier, td->multiplier,
  122. rd->divisor, td->divisor
  123. );
  124. #endif
  125. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  126. }
  127. }
  128. }
  129. rrddim_flag_set(rd, RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION);
  130. rrdset_flag_set(rd->rrdset, RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION);
  131. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION);
  132. // let the chart resync
  133. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  134. ml_dimension_new(rd);
  135. ctr->react_action = RRDDIM_REACT_NEW;
  136. internal_error(false, "RRDDIM: inserted dimension '%s' of chart '%s' of host '%s'",
  137. rrddim_name(rd), rrdset_name(st), rrdhost_hostname(st->rrdhost));
  138. }
  139. bool rrddim_finalize_collection_and_check_retention(RRDDIM *rd) {
  140. size_t tiers_available = 0, tiers_said_no_retention = 0;
  141. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  142. if(!rd->tiers[tier].db_collection_handle)
  143. continue;
  144. tiers_available++;
  145. if(storage_engine_store_finalize(rd->tiers[tier].db_collection_handle))
  146. tiers_said_no_retention++;
  147. rd->tiers[tier].db_collection_handle = NULL;
  148. }
  149. // return true if the dimension has retention in the db
  150. return (!tiers_said_no_retention || tiers_available > tiers_said_no_retention);
  151. }
  152. static void rrddim_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *rrdset) {
  153. RRDDIM *rd = rrddim;
  154. RRDSET *st = rrdset;
  155. RRDHOST *host = st->rrdhost;
  156. internal_error(false, "RRDDIM: deleting dimension '%s' of chart '%s' of host '%s'",
  157. rrddim_name(rd), rrdset_name(st), rrdhost_hostname(host));
  158. rrdcontext_removed_rrddim(rd);
  159. ml_dimension_delete(rd);
  160. netdata_log_debug(D_RRD_CALLS, "rrddim_free() %s.%s", rrdset_name(st), rrddim_name(rd));
  161. if (!rrddim_finalize_collection_and_check_retention(rd) && rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  162. /* This metric has no data and no references */
  163. metaqueue_delete_dimension_uuid(&rd->metric_uuid);
  164. }
  165. rrddimvar_delete_all(rd);
  166. // free(rd->annotations);
  167. //#ifdef ENABLE_ACLK
  168. // if (!netdata_exit)
  169. // aclk_send_dimension_update(rd);
  170. //#endif
  171. // this will free MEMORY_MODE_SAVE and MEMORY_MODE_MAP structures
  172. rrddim_memory_file_free(rd);
  173. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  174. if(!rd->tiers[tier].db_metric_handle) continue;
  175. STORAGE_ENGINE* eng = host->db[tier].eng;
  176. eng->api.metric_release(rd->tiers[tier].db_metric_handle);
  177. rd->tiers[tier].db_metric_handle = NULL;
  178. }
  179. if(rd->db.data) {
  180. __atomic_sub_fetch(&rrddim_db_memory_size, rd->db.memsize, __ATOMIC_RELAXED);
  181. if(rd->rrd_memory_mode == RRD_MEMORY_MODE_RAM)
  182. netdata_munmap(rd->db.data, rd->db.memsize);
  183. else
  184. freez(rd->db.data);
  185. }
  186. string_freez(rd->id);
  187. string_freez(rd->name);
  188. }
  189. static bool rrddim_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *new_rrddim, void *constructor_data) {
  190. (void)new_rrddim; // it is NULL
  191. struct rrddim_constructor *ctr = constructor_data;
  192. RRDDIM *rd = rrddim;
  193. RRDSET *st = ctr->st;
  194. ctr->react_action = RRDDIM_REACT_NONE;
  195. int rc = rrddim_reset_name(st, rd, ctr->name);
  196. rc += rrddim_set_algorithm(st, rd, ctr->algorithm);
  197. rc += rrddim_set_multiplier(st, rd, ctr->multiplier);
  198. rc += rrddim_set_divisor(st, rd, ctr->divisor);
  199. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  200. if (!rd->tiers[tier].db_collection_handle)
  201. rd->tiers[tier].db_collection_handle =
  202. storage_metric_store_init(rd->tiers[tier].backend, rd->tiers[tier].db_metric_handle, st->rrdhost->db[tier].tier_grouping * st->update_every, rd->rrdset->storage_metrics_groups[tier]);
  203. }
  204. if(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  205. rrddim_flag_clear(rd, RRDDIM_FLAG_ARCHIVED);
  206. rrddim_flag_set(rd, RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION);
  207. rrdset_flag_set(rd->rrdset, RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION);
  208. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION);
  209. }
  210. if(unlikely(rc))
  211. ctr->react_action = RRDDIM_REACT_UPDATED;
  212. return ctr->react_action == RRDDIM_REACT_UPDATED;
  213. }
  214. static void rrddim_react_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *constructor_data) {
  215. struct rrddim_constructor *ctr = constructor_data;
  216. RRDDIM *rd = rrddim;
  217. RRDSET *st = ctr->st;
  218. if(ctr->react_action & (RRDDIM_REACT_UPDATED | RRDDIM_REACT_NEW)) {
  219. rrddim_flag_set(rd, RRDDIM_FLAG_METADATA_UPDATE);
  220. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  221. }
  222. if(ctr->react_action == RRDDIM_REACT_UPDATED) {
  223. // the chart needs to be updated to the parent
  224. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  225. }
  226. rrddim_metadata_updated(rd);
  227. }
  228. size_t rrddim_size(void) {
  229. return sizeof(RRDDIM) + storage_tiers * sizeof(struct rrddim_tier);
  230. }
  231. void rrddim_index_init(RRDSET *st) {
  232. if(!st->rrddim_root_index) {
  233. st->rrddim_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  234. &dictionary_stats_category_rrdset_rrddim, rrddim_size());
  235. dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
  236. dictionary_register_conflict_callback(st->rrddim_root_index, rrddim_conflict_callback, NULL);
  237. dictionary_register_delete_callback(st->rrddim_root_index, rrddim_delete_callback, st);
  238. dictionary_register_react_callback(st->rrddim_root_index, rrddim_react_callback, st);
  239. }
  240. }
  241. void rrddim_index_destroy(RRDSET *st) {
  242. dictionary_destroy(st->rrddim_root_index);
  243. st->rrddim_root_index = NULL;
  244. }
  245. static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id) {
  246. return dictionary_get(st->rrddim_root_index, id);
  247. }
  248. // ----------------------------------------------------------------------------
  249. // RRDDIM - find a dimension
  250. inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
  251. netdata_log_debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", rrdset_name(st), id);
  252. return rrddim_index_find(st, id);
  253. }
  254. inline RRDDIM_ACQUIRED *rrddim_find_and_acquire(RRDSET *st, const char *id) {
  255. netdata_log_debug(D_RRD_CALLS, "rrddim_find_and_acquire() for chart %s, dimension %s", rrdset_name(st), id);
  256. return (RRDDIM_ACQUIRED *)dictionary_get_and_acquire_item(st->rrddim_root_index, id);
  257. }
  258. RRDDIM *rrddim_acquired_to_rrddim(RRDDIM_ACQUIRED *rda) {
  259. if(unlikely(!rda))
  260. return NULL;
  261. return (RRDDIM *) dictionary_acquired_item_value((const DICTIONARY_ITEM *)rda);
  262. }
  263. void rrddim_acquired_release(RRDDIM_ACQUIRED *rda) {
  264. if(unlikely(!rda))
  265. return;
  266. RRDDIM *rd = rrddim_acquired_to_rrddim(rda);
  267. dictionary_acquired_item_release(rd->rrdset->rrddim_root_index, (const DICTIONARY_ITEM *)rda);
  268. }
  269. // This will not return dimensions that are archived
  270. RRDDIM *rrddim_find_active(RRDSET *st, const char *id) {
  271. RRDDIM *rd = rrddim_find(st, id);
  272. if (unlikely(rd && rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)))
  273. return NULL;
  274. return rd;
  275. }
  276. // ----------------------------------------------------------------------------
  277. // RRDDIM rename a dimension
  278. inline int rrddim_reset_name(RRDSET *st, RRDDIM *rd, const char *name) {
  279. if(unlikely(!name || !*name || !strcmp(rrddim_name(rd), name)))
  280. return 0;
  281. netdata_log_debug(D_RRD_CALLS, "rrddim_reset_name() from %s.%s to %s.%s", rrdset_name(st), rrddim_name(rd), rrdset_name(st), name);
  282. STRING *old = rd->name;
  283. rd->name = rrd_string_strdupz(name);
  284. string_freez(old);
  285. rrddimvar_rename_all(rd);
  286. rrddim_metadata_updated(rd);
  287. return 1;
  288. }
  289. inline int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm) {
  290. if(unlikely(rd->algorithm == algorithm))
  291. return 0;
  292. netdata_log_debug(D_RRD_CALLS, "Updating algorithm of dimension '%s/%s' from %s to %s", rrdset_id(st), rrddim_name(rd), rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm));
  293. rd->algorithm = algorithm;
  294. rrddim_metadata_updated(rd);
  295. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  296. rrdcontext_updated_rrddim_algorithm(rd);
  297. return 1;
  298. }
  299. inline int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, int32_t multiplier) {
  300. if(unlikely(rd->multiplier == multiplier))
  301. return 0;
  302. netdata_log_debug(D_RRD_CALLS, "Updating multiplier of dimension '%s/%s' from %d to %d",
  303. rrdset_id(st), rrddim_name(rd), rd->multiplier, multiplier);
  304. rd->multiplier = multiplier;
  305. rrddim_metadata_updated(rd);
  306. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  307. rrdcontext_updated_rrddim_multiplier(rd);
  308. return 1;
  309. }
  310. inline int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, int32_t divisor) {
  311. if(unlikely(rd->divisor == divisor))
  312. return 0;
  313. netdata_log_debug(D_RRD_CALLS, "Updating divisor of dimension '%s/%s' from %d to %d",
  314. rrdset_id(st), rrddim_name(rd), rd->divisor, divisor);
  315. rd->divisor = divisor;
  316. rrddim_metadata_updated(rd);
  317. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  318. rrdcontext_updated_rrddim_divisor(rd);
  319. return 1;
  320. }
  321. // ----------------------------------------------------------------------------
  322. time_t rrddim_last_entry_s_of_tier(RRDDIM *rd, size_t tier) {
  323. if(unlikely(tier > storage_tiers || !rd->tiers[tier].db_metric_handle))
  324. return 0;
  325. return storage_engine_latest_time_s(rd->tiers[tier].backend, rd->tiers[tier].db_metric_handle);
  326. }
  327. // get the timestamp of the last entry in the round-robin database
  328. time_t rrddim_last_entry_s(RRDDIM *rd) {
  329. time_t latest_time_s = rrddim_last_entry_s_of_tier(rd, 0);
  330. for(size_t tier = 1; tier < storage_tiers ;tier++) {
  331. if(unlikely(!rd->tiers[tier].db_metric_handle)) continue;
  332. time_t t = rrddim_last_entry_s_of_tier(rd, tier);
  333. if(t > latest_time_s)
  334. latest_time_s = t;
  335. }
  336. return latest_time_s;
  337. }
  338. time_t rrddim_first_entry_s_of_tier(RRDDIM *rd, size_t tier) {
  339. if(unlikely(tier > storage_tiers || !rd->tiers[tier].db_metric_handle))
  340. return 0;
  341. return storage_engine_oldest_time_s(rd->tiers[tier].backend, rd->tiers[tier].db_metric_handle);
  342. }
  343. time_t rrddim_first_entry_s(RRDDIM *rd) {
  344. time_t oldest_time_s = 0;
  345. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  346. time_t t = rrddim_first_entry_s_of_tier(rd, tier);
  347. if(t != 0 && (oldest_time_s == 0 || t < oldest_time_s))
  348. oldest_time_s = t;
  349. }
  350. return oldest_time_s;
  351. }
  352. RRDDIM *rrddim_add_custom(RRDSET *st
  353. , const char *id
  354. , const char *name
  355. , collected_number multiplier
  356. , collected_number divisor
  357. , RRD_ALGORITHM algorithm
  358. , RRD_MEMORY_MODE memory_mode
  359. ) {
  360. struct rrddim_constructor tmp = {
  361. .st = st,
  362. .id = id,
  363. .name = name,
  364. .multiplier = multiplier,
  365. .divisor = divisor,
  366. .algorithm = algorithm,
  367. .memory_mode = memory_mode,
  368. };
  369. RRDDIM *rd = dictionary_set_advanced(st->rrddim_root_index, tmp.id, -1, NULL, rrddim_size(), &tmp);
  370. return(rd);
  371. }
  372. // ----------------------------------------------------------------------------
  373. // RRDDIM remove / free a dimension
  374. void rrddim_free(RRDSET *st, RRDDIM *rd) {
  375. dictionary_del(st->rrddim_root_index, string2str(rd->id));
  376. }
  377. // ----------------------------------------------------------------------------
  378. // RRDDIM - set dimension options
  379. int rrddim_hide(RRDSET *st, const char *id) {
  380. netdata_log_debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", rrdset_name(st), id);
  381. RRDHOST *host = st->rrdhost;
  382. RRDDIM *rd = rrddim_find(st, id);
  383. if(unlikely(!rd)) {
  384. netdata_log_error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
  385. return 1;
  386. }
  387. if (!rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  388. rrddim_flag_set(rd, RRDDIM_FLAG_META_HIDDEN | RRDDIM_FLAG_METADATA_UPDATE);
  389. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  390. }
  391. rrddim_option_set(rd, RRDDIM_OPTION_HIDDEN);
  392. rrdcontext_updated_rrddim_flags(rd);
  393. return 0;
  394. }
  395. int rrddim_unhide(RRDSET *st, const char *id) {
  396. netdata_log_debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", rrdset_name(st), id);
  397. RRDHOST *host = st->rrdhost;
  398. RRDDIM *rd = rrddim_find(st, id);
  399. if(unlikely(!rd)) {
  400. netdata_log_error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
  401. return 1;
  402. }
  403. if (rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  404. rrddim_flag_clear(rd, RRDDIM_FLAG_META_HIDDEN);
  405. rrddim_flag_set(rd, RRDDIM_FLAG_METADATA_UPDATE);
  406. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  407. }
  408. rrddim_option_clear(rd, RRDDIM_OPTION_HIDDEN);
  409. rrdcontext_updated_rrddim_flags(rd);
  410. return 0;
  411. }
  412. inline void rrddim_is_obsolete___safe_from_collector_thread(RRDSET *st, RRDDIM *rd) {
  413. netdata_log_debug(D_RRD_CALLS, "rrddim_is_obsolete___safe_from_collector_thread() for chart %s, dimension %s", rrdset_name(st), rrddim_name(rd));
  414. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))) {
  415. netdata_log_info("Cannot obsolete already archived dimension %s from chart %s", rrddim_name(rd), rrdset_name(st));
  416. return;
  417. }
  418. rrddim_flag_set(rd, RRDDIM_FLAG_OBSOLETE);
  419. rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS);
  420. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_PENDING_OBSOLETE_DIMENSIONS);
  421. rrdcontext_updated_rrddim_flags(rd);
  422. }
  423. inline void rrddim_isnot_obsolete___safe_from_collector_thread(RRDSET *st __maybe_unused, RRDDIM *rd) {
  424. netdata_log_debug(D_RRD_CALLS, "rrddim_isnot_obsolete___safe_from_collector_thread() for chart %s, dimension %s", rrdset_name(st), rrddim_name(rd));
  425. rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
  426. rrdcontext_updated_rrddim_flags(rd);
  427. }
  428. // ----------------------------------------------------------------------------
  429. // RRDDIM - collect values for a dimension
  430. inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
  431. struct timeval now;
  432. now_realtime_timeval(&now);
  433. return rrddim_timed_set_by_pointer(st, rd, now, value);
  434. }
  435. collected_number rrddim_timed_set_by_pointer(RRDSET *st __maybe_unused, RRDDIM *rd, struct timeval collected_time, collected_number value) {
  436. netdata_log_debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, rrdset_name(st), rrddim_name(rd), value);
  437. rd->collector.last_collected_time = collected_time;
  438. rd->collector.collected_value = value;
  439. rrddim_set_updated(rd);
  440. rd->collector.counter++;
  441. collected_number v = (value >= 0) ? value : -value;
  442. if (unlikely(v > rd->collector.collected_value_max))
  443. rd->collector.collected_value_max = v;
  444. return rd->collector.last_collected_value;
  445. }
  446. collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
  447. RRDHOST *host = st->rrdhost;
  448. RRDDIM *rd = rrddim_find(st, id);
  449. if(unlikely(!rd)) {
  450. netdata_log_error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, rrdset_name(st), rrdset_id(st), rrdhost_hostname(host));
  451. return 0;
  452. }
  453. return rrddim_set_by_pointer(st, rd, value);
  454. }
  455. // ----------------------------------------------------------------------------
  456. // compatibility layer for RRDDIM files v019
  457. #define RRDDIMENSION_MAGIC_V019 "NETDATA RRD DIMENSION FILE V019"
  458. struct avl_element_v019 {
  459. void *avl_link[2];
  460. signed char avl_balance;
  461. };
  462. struct rrddim_map_save_v019 {
  463. struct avl_element_v019 avl; // ignored
  464. void *id; // ignored
  465. void *name; // ignored
  466. uint32_t algorithm; // print warning on mismatch - update on load
  467. uint32_t rrd_memory_mode; // ignored
  468. long long multiplier; // print warning on mismatch - update on load
  469. long long divisor; // print warning on mismatch - update on load
  470. uint32_t flags; // ignored
  471. uint32_t hash; // ignored
  472. uint32_t hash_name; // ignored
  473. void *cache_filename; // ignored - we use it to keep the filename to save back
  474. size_t collections_counter; // ignored
  475. void *state; // ignored
  476. size_t unused[8]; // ignored
  477. long long collected_value_max; // ignored
  478. unsigned int updated:1; // ignored
  479. unsigned int exposed:1; // ignored
  480. struct timeval last_collected_time; // check to reset all - ignored after load
  481. long double calculated_value; // ignored
  482. long double last_calculated_value; // ignored
  483. long double last_stored_value; // ignored
  484. long long collected_value; // ignored
  485. long long last_collected_value; // load and save
  486. long double collected_volume; // ignored
  487. long double stored_volume; // ignored
  488. void *next; // ignored
  489. void *rrdset; // ignored
  490. long entries; // check to reset all - update on load
  491. int update_every; // check to reset all - update on load
  492. size_t memsize; // check to reset all - update on load
  493. char magic[sizeof(RRDDIMENSION_MAGIC_V019) + 1];// check to reset all - update on load
  494. void *variables; // ignored
  495. storage_number values[]; // the array of values
  496. };
  497. size_t rrddim_memory_file_header_size(void) {
  498. return sizeof(struct rrddim_map_save_v019);
  499. }
  500. void rrddim_memory_file_update(RRDDIM *rd) {
  501. if(!rd || !rd->db.rd_on_file) return;
  502. struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
  503. rd_on_file->last_collected_time.tv_sec = rd->collector.last_collected_time.tv_sec;
  504. rd_on_file->last_collected_time.tv_usec = rd->collector.last_collected_time.tv_usec;
  505. rd_on_file->last_collected_value = rd->collector.last_collected_value;
  506. }
  507. void rrddim_memory_file_free(RRDDIM *rd) {
  508. if(!rd || !rd->db.rd_on_file) return;
  509. // needed for memory mode map, to save the latest state
  510. rrddim_memory_file_update(rd);
  511. struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
  512. __atomic_sub_fetch(&rrddim_db_memory_size, rd_on_file->memsize + strlen(rd_on_file->cache_filename), __ATOMIC_RELAXED);
  513. freez(rd_on_file->cache_filename);
  514. netdata_munmap(rd_on_file, rd_on_file->memsize);
  515. // remove the pointers from the RRDDIM
  516. rd->db.rd_on_file = NULL;
  517. rd->db.data = NULL;
  518. }
  519. const char *rrddim_cache_filename(RRDDIM *rd) {
  520. if(!rd || !rd->db.rd_on_file) return NULL;
  521. struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
  522. return rd_on_file->cache_filename;
  523. }
  524. void rrddim_memory_file_save(RRDDIM *rd) {
  525. if(!rd || !rd->db.rd_on_file) return;
  526. rrddim_memory_file_update(rd);
  527. struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
  528. if(rd_on_file->rrd_memory_mode != RRD_MEMORY_MODE_SAVE) return;
  529. memory_file_save(rd_on_file->cache_filename, rd_on_file, rd_on_file->memsize);
  530. }
  531. bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode) {
  532. if(memory_mode != RRD_MEMORY_MODE_SAVE && memory_mode != RRD_MEMORY_MODE_MAP)
  533. return false;
  534. struct rrddim_map_save_v019 *rd_on_file = NULL;
  535. unsigned long size = sizeof(struct rrddim_map_save_v019) + (st->db.entries * sizeof(storage_number));
  536. char filename[FILENAME_MAX + 1];
  537. char fullfilename[FILENAME_MAX + 1];
  538. rrdset_strncpyz_name(filename, rrddim_id(rd), FILENAME_MAX);
  539. snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", rrdset_cache_dir(st), filename);
  540. rd_on_file = (struct rrddim_map_save_v019 *)netdata_mmap(
  541. fullfilename, size, ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1, false, NULL);
  542. if(unlikely(!rd_on_file)) return false;
  543. struct timeval now;
  544. now_realtime_timeval(&now);
  545. int reset = 0;
  546. rd_on_file->magic[sizeof(RRDDIMENSION_MAGIC_V019)] = '\0';
  547. if(strcmp(rd_on_file->magic, RRDDIMENSION_MAGIC_V019) != 0) {
  548. netdata_log_info("Initializing file %s.", fullfilename);
  549. memset(rd_on_file, 0, size);
  550. reset = 1;
  551. }
  552. else if(rd_on_file->memsize != size) {
  553. netdata_log_error("File %s does not have the desired size, expected %lu but found %lu. Clearing it.", fullfilename, size, (unsigned long int) rd_on_file->memsize);
  554. memset(rd_on_file, 0, size);
  555. reset = 1;
  556. }
  557. else if(rd_on_file->update_every != st->update_every) {
  558. netdata_log_error("File %s does not have the same update frequency, expected %d but found %d. Clearing it.", fullfilename, st->update_every, rd_on_file->update_every);
  559. memset(rd_on_file, 0, size);
  560. reset = 1;
  561. }
  562. else if(dt_usec(&now, &rd_on_file->last_collected_time) > (rd_on_file->entries * rd_on_file->update_every * USEC_PER_SEC)) {
  563. netdata_log_info("File %s is too old (last collected %llu seconds ago, but the database is %ld seconds). Clearing it.", fullfilename, dt_usec(&now, &rd_on_file->last_collected_time) / USEC_PER_SEC, rd_on_file->entries * rd_on_file->update_every);
  564. memset(rd_on_file, 0, size);
  565. reset = 1;
  566. }
  567. if(!reset) {
  568. rd->collector.last_collected_value = rd_on_file->last_collected_value;
  569. if(rd_on_file->algorithm != rd->algorithm)
  570. netdata_log_info("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong.",
  571. fullfilename, rd->algorithm, rrd_algorithm_name(rd->algorithm), rd_on_file->algorithm, rrd_algorithm_name(rd_on_file->algorithm));
  572. if(rd_on_file->multiplier != rd->multiplier)
  573. netdata_log_info("File %s does not have the expected multiplier (expected %d, found %ld). "
  574. "Previous values may be wrong.", fullfilename, rd->multiplier, (long)rd_on_file->multiplier);
  575. if(rd_on_file->divisor != rd->divisor)
  576. netdata_log_info("File %s does not have the expected divisor (expected %d, found %ld). "
  577. "Previous values may be wrong.", fullfilename, rd->divisor, (long)rd_on_file->divisor);
  578. }
  579. // zero the entire header
  580. memset(rd_on_file, 0, sizeof(struct rrddim_map_save_v019));
  581. // set the important fields
  582. strcpy(rd_on_file->magic, RRDDIMENSION_MAGIC_V019);
  583. rd_on_file->algorithm = rd->algorithm;
  584. rd_on_file->multiplier = rd->multiplier;
  585. rd_on_file->divisor = rd->divisor;
  586. rd_on_file->entries = st->db.entries;
  587. rd_on_file->update_every = rd->rrdset->update_every;
  588. rd_on_file->memsize = size;
  589. rd_on_file->rrd_memory_mode = memory_mode;
  590. rd_on_file->cache_filename = strdupz(fullfilename);
  591. __atomic_add_fetch(&rrddim_db_memory_size, rd_on_file->memsize + strlen(rd_on_file->cache_filename), __ATOMIC_RELAXED);
  592. rd->db.data = &rd_on_file->values[0];
  593. rd->db.rd_on_file = rd_on_file;
  594. rd->db.memsize = size;
  595. rrddim_memory_file_update(rd);
  596. return true;
  597. }