rrddim.c 29 KB

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