rrddim.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. #ifdef ENABLE_DBENGINE
  5. #include "database/engine/rrdengineapi.h"
  6. #endif
  7. #include "storage_engine.h"
  8. // ----------------------------------------------------------------------------
  9. // RRDDIM index
  10. int rrddim_compare(void* a, void* b) {
  11. if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
  12. else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
  13. else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
  14. }
  15. #define rrddim_index_add(st, rd) (RRDDIM *)avl_insert_lock(&((st)->dimensions_index), (avl_t *)(rd))
  16. #define rrddim_index_del(st,rd ) (RRDDIM *)avl_remove_lock(&((st)->dimensions_index), (avl_t *)(rd))
  17. static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
  18. RRDDIM tmp = {
  19. .id = id,
  20. .hash = (hash)?hash:simple_hash(id)
  21. };
  22. return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl_t *) &tmp);
  23. }
  24. // ----------------------------------------------------------------------------
  25. // RRDDIM - find a dimension
  26. inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
  27. debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
  28. return rrddim_index_find(st, id, 0);
  29. }
  30. // ----------------------------------------------------------------------------
  31. // RRDDIM rename a dimension
  32. inline int rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name) {
  33. if(unlikely(!name || !*name || (rd->name && !strcmp(rd->name, name))))
  34. return 0;
  35. debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
  36. if (rd->name)
  37. freez((void *) rd->name);
  38. rd->name = strdupz(name);
  39. rd->hash_name = simple_hash(rd->name);
  40. if (!st->state->is_ar_chart)
  41. rrddimvar_rename_all(rd);
  42. rd->exposed = 0;
  43. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  44. ml_dimension_update_name(st, rd, name);
  45. return 1;
  46. }
  47. inline int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm) {
  48. if(unlikely(rd->algorithm == algorithm))
  49. return 0;
  50. debug(D_RRD_CALLS, "Updating algorithm of dimension '%s/%s' from %s to %s", st->id, rd->name, rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm));
  51. rd->algorithm = algorithm;
  52. rd->exposed = 0;
  53. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  54. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  55. rrdcontext_updated_rrddim_algorithm(rd);
  56. return 1;
  57. }
  58. inline int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier) {
  59. if(unlikely(rd->multiplier == multiplier))
  60. return 0;
  61. debug(D_RRD_CALLS, "Updating multiplier of dimension '%s/%s' from " COLLECTED_NUMBER_FORMAT " to " COLLECTED_NUMBER_FORMAT, st->id, rd->name, rd->multiplier, multiplier);
  62. rd->multiplier = multiplier;
  63. rd->exposed = 0;
  64. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  65. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  66. rrdcontext_updated_rrddim_multiplier(rd);
  67. return 1;
  68. }
  69. inline int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor) {
  70. if(unlikely(rd->divisor == divisor))
  71. return 0;
  72. debug(D_RRD_CALLS, "Updating divisor of dimension '%s/%s' from " COLLECTED_NUMBER_FORMAT " to " COLLECTED_NUMBER_FORMAT, st->id, rd->name, rd->divisor, divisor);
  73. rd->divisor = divisor;
  74. rd->exposed = 0;
  75. rrdset_flag_set(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  76. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  77. rrdcontext_updated_rrddim_divisor(rd);
  78. return 1;
  79. }
  80. // ----------------------------------------------------------------------------
  81. // RRDDIM create a dimension
  82. void rrdcalc_link_to_rrddim(RRDDIM *rd, RRDSET *st, RRDHOST *host) {
  83. RRDCALC *rrdc;
  84. for (rrdc = host->alarms_with_foreach; rrdc ; rrdc = rrdc->next) {
  85. if (simple_pattern_matches(rrdc->spdim, rd->id) || simple_pattern_matches(rrdc->spdim, rd->name)) {
  86. if (rrdc->hash_chart == st->hash_name || !strcmp(rrdc->chart, st->name) || !strcmp(rrdc->chart, st->id)) {
  87. char *name = alarm_name_with_dim(rrdc->name, strlen(rrdc->name), rd->name, strlen(rd->name));
  88. if(rrdcalc_exists(host, st->name, name, 0, 0)) {
  89. freez(name);
  90. continue;
  91. }
  92. netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
  93. RRDCALC *child = rrdcalc_create_from_rrdcalc(rrdc, host, name, rd->name);
  94. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  95. if (child) {
  96. rrdcalc_add_to_host(host, child);
  97. RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_health_log,(avl_t *)child);
  98. if (rdcmp != child) {
  99. error("Cannot insert the alarm index ID %s",child->name);
  100. }
  101. }
  102. else {
  103. error("Cannot allocate a new alarm.");
  104. rrdc->foreachcounter--;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. // Return either
  111. // 0 : Dimension is live
  112. // last collected time : Dimension is not live
  113. #ifdef ENABLE_ACLK
  114. time_t calc_dimension_liveness(RRDDIM *rd, time_t now)
  115. {
  116. time_t last_updated = rd->last_collected_time.tv_sec;
  117. int live;
  118. if (rd->aclk_live_status == 1)
  119. live =
  120. ((now - last_updated) <
  121. MIN(rrdset_free_obsolete_time, RRDSET_MINIMUM_DIM_OFFLINE_MULTIPLIER * rd->update_every));
  122. else
  123. live = ((now - last_updated) < RRDSET_MINIMUM_DIM_LIVE_MULTIPLIER * rd->update_every);
  124. return live ? 0 : last_updated;
  125. }
  126. #endif
  127. RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collected_number multiplier,
  128. collected_number divisor, RRD_ALGORITHM algorithm, RRD_MEMORY_MODE memory_mode)
  129. {
  130. RRDHOST *host = st->rrdhost;
  131. rrdset_wrlock(st);
  132. RRDDIM *rd = rrddim_find(st, id);
  133. if(unlikely(rd)) {
  134. debug(D_RRD_CALLS, "Cannot create rrd dimension '%s/%s', it already exists.", st->id, name?name:"<NONAME>");
  135. int rc = rrddim_set_name(st, rd, name);
  136. rc += rrddim_set_algorithm(st, rd, algorithm);
  137. rc += rrddim_set_multiplier(st, rd, multiplier);
  138. rc += rrddim_set_divisor(st, rd, divisor);
  139. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  140. store_active_dimension(&rd->metric_uuid);
  141. for(int tier = 0; tier < storage_tiers ;tier++) {
  142. if (rd->tiers[tier])
  143. rd->tiers[tier]->db_collection_handle =
  144. rd->tiers[tier]->collect_ops.init(rd->tiers[tier]->db_metric_handle);
  145. }
  146. rrddim_flag_clear(rd, RRDDIM_FLAG_ARCHIVED);
  147. rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, RRDVAR_OPTION_DEFAULT);
  148. rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, RRDVAR_OPTION_DEFAULT);
  149. rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  150. rrddim_flag_set(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM);
  151. rrdset_flag_set(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS);
  152. rrdhost_flag_set(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS);
  153. }
  154. if (unlikely(rc)) {
  155. debug(D_METADATALOG, "DIMENSION [%s] metadata updated", rd->id);
  156. (void)sql_store_dimension(&rd->metric_uuid, rd->rrdset->chart_uuid, rd->id, rd->name, rd->multiplier, rd->divisor,
  157. rd->algorithm);
  158. #ifdef ENABLE_ACLK
  159. queue_dimension_to_aclk(rd, calc_dimension_liveness(rd, now_realtime_sec()));
  160. #endif
  161. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  162. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  163. }
  164. rrdset_unlock(st);
  165. rrdcontext_updated_rrddim(rd);
  166. return rd;
  167. }
  168. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  169. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  170. rd = callocz(1, sizeof(RRDDIM));
  171. rd->id = strdupz(id);
  172. rd->hash = simple_hash(rd->id);
  173. rd->name = (name && *name)?strdupz(name):strdupz(rd->id);
  174. rd->hash_name = simple_hash(rd->name);
  175. rd->algorithm = algorithm;
  176. rd->multiplier = multiplier;
  177. rd->divisor = divisor;
  178. if(!rd->divisor) rd->divisor = 1;
  179. rd->entries = st->entries;
  180. rd->update_every = st->update_every;
  181. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  182. rd->collections_counter = 1;
  183. rd->rrdset = st;
  184. if(memory_mode == RRD_MEMORY_MODE_MAP || memory_mode == RRD_MEMORY_MODE_SAVE) {
  185. if(!rrddim_memory_load_or_create_map_save(st, rd, memory_mode)) {
  186. info("Failed to use memory mode %s for chart '%s', dimension '%s', falling back to ram", (memory_mode == RRD_MEMORY_MODE_MAP)?"map":"save", st->name, rd->name);
  187. memory_mode = RRD_MEMORY_MODE_RAM;
  188. }
  189. }
  190. if(memory_mode == RRD_MEMORY_MODE_RAM) {
  191. size_t entries = st->entries;
  192. if(!entries) entries = 5;
  193. rd->db = netdata_mmap(NULL, entries * sizeof(storage_number), MAP_PRIVATE, 1);
  194. if(!rd->db) {
  195. info("Failed to use memory mode ram for chart '%s', dimension '%s', falling back to alloc", st->name, rd->name);
  196. memory_mode = RRD_MEMORY_MODE_ALLOC;
  197. }
  198. else rd->memsize = entries * sizeof(storage_number);
  199. }
  200. if(memory_mode == RRD_MEMORY_MODE_ALLOC || memory_mode == RRD_MEMORY_MODE_NONE) {
  201. size_t entries = st->entries;
  202. if(entries < 5) entries = 5;
  203. rd->db = callocz(entries, sizeof(storage_number));
  204. rd->memsize = entries * sizeof(storage_number);
  205. }
  206. rd->rrd_memory_mode = memory_mode;
  207. #ifdef ENABLE_ACLK
  208. rd->aclk_live_status = -1;
  209. #endif
  210. (void) find_dimension_uuid(st, rd, &(rd->metric_uuid));
  211. // initialize the db tiers
  212. {
  213. size_t initialized = 0;
  214. RRD_MEMORY_MODE wanted_mode = memory_mode;
  215. for(int tier = 0; tier < storage_tiers ; tier++, wanted_mode = RRD_MEMORY_MODE_DBENGINE) {
  216. STORAGE_ENGINE *eng = storage_engine_get(wanted_mode);
  217. if(!eng) continue;
  218. rd->tiers[tier] = callocz(1, sizeof(struct rrddim_tier));
  219. rd->tiers[tier]->tier_grouping = get_tier_grouping(tier);
  220. rd->tiers[tier]->mode = eng->id;
  221. rd->tiers[tier]->collect_ops = eng->api.collect_ops;
  222. rd->tiers[tier]->query_ops = eng->api.query_ops;
  223. rd->tiers[tier]->db_metric_handle = eng->api.init(rd, host->storage_instance[tier]);
  224. storage_point_unset(rd->tiers[tier]->virtual_point);
  225. initialized++;
  226. // 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);
  227. }
  228. if(!initialized)
  229. error("Failed to initialize all db tiers for chart '%s', dimension '%s", st->name, rd->name);
  230. if(!rd->tiers[0])
  231. error("Failed to initialize the first db tier for chart '%s', dimension '%s", st->name, rd->name);
  232. }
  233. store_active_dimension(&rd->metric_uuid);
  234. // initialize data collection for all tiers
  235. {
  236. size_t initialized = 0;
  237. for (int tier = 0; tier < storage_tiers; tier++) {
  238. if (rd->tiers[tier]) {
  239. rd->tiers[tier]->db_collection_handle = rd->tiers[tier]->collect_ops.init(rd->tiers[tier]->db_metric_handle);
  240. initialized++;
  241. }
  242. }
  243. if(!initialized)
  244. error("Failed to initialize data collection for all db tiers for chart '%s', dimension '%s", st->name, rd->name);
  245. }
  246. // append this dimension
  247. if(!st->dimensions)
  248. st->dimensions = rd;
  249. else {
  250. RRDDIM *td = st->dimensions;
  251. if(td->algorithm != rd->algorithm || ABS(td->multiplier) != ABS(rd->multiplier) || ABS(td->divisor) != ABS(rd->divisor)) {
  252. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  253. #ifdef NETDATA_INTERNAL_CHECKS
  254. info("Dimension '%s' added on chart '%s' of host '%s' is not homogeneous to other dimensions already present (algorithm is '%s' vs '%s', multiplier is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ", divisor is " COLLECTED_NUMBER_FORMAT " vs " COLLECTED_NUMBER_FORMAT ").",
  255. rd->name,
  256. st->name,
  257. host->hostname,
  258. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(td->algorithm),
  259. rd->multiplier, td->multiplier,
  260. rd->divisor, td->divisor
  261. );
  262. #endif
  263. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  264. }
  265. }
  266. for(; td->next; td = td->next) ;
  267. td->next = rd;
  268. }
  269. if(host->health_enabled && !st->state->is_ar_chart) {
  270. rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, RRDVAR_OPTION_DEFAULT);
  271. rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, RRDVAR_OPTION_DEFAULT);
  272. rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  273. }
  274. if(unlikely(rrddim_index_add(st, rd) != rd))
  275. error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
  276. rrddim_flag_set(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM);
  277. rrdset_flag_set(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS);
  278. rrdhost_flag_set(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS);
  279. ml_new_dimension(rd);
  280. rrdset_unlock(st);
  281. rrdcontext_updated_rrddim(rd);
  282. return(rd);
  283. }
  284. // ----------------------------------------------------------------------------
  285. // RRDDIM remove / free a dimension
  286. void rrddim_free(RRDSET *st, RRDDIM *rd)
  287. {
  288. rrdcontext_removed_rrddim(rd);
  289. ml_delete_dimension(rd);
  290. debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
  291. if (!rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  292. size_t tiers_available = 0, tiers_said_yes = 0;
  293. for(int tier = 0; tier < storage_tiers ;tier++) {
  294. if(rd->tiers[tier]) {
  295. tiers_available++;
  296. if(rd->tiers[tier]->collect_ops.finalize(rd->tiers[tier]->db_collection_handle))
  297. tiers_said_yes++;
  298. rd->tiers[tier]->db_collection_handle = NULL;
  299. }
  300. }
  301. if (tiers_available == tiers_said_yes && tiers_said_yes && rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  302. /* This metric has no data and no references */
  303. delete_dimension_uuid(&rd->metric_uuid);
  304. }
  305. }
  306. if(rd == st->dimensions)
  307. st->dimensions = rd->next;
  308. else {
  309. RRDDIM *i;
  310. for (i = st->dimensions; i && i->next != rd; i = i->next) ;
  311. if (i && i->next == rd)
  312. i->next = rd->next;
  313. else
  314. error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
  315. }
  316. rd->next = NULL;
  317. while(rd->variables)
  318. rrddimvar_free(rd->variables);
  319. if(unlikely(rrddim_index_del(st, rd) != rd))
  320. error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
  321. // free(rd->annotations);
  322. //#ifdef ENABLE_ACLK
  323. // if (!netdata_exit)
  324. // aclk_send_dimension_update(rd);
  325. //#endif
  326. // this will free MEMORY_MODE_SAVE and MEMORY_MODE_MAP structures
  327. rrddim_memory_file_free(rd);
  328. for(int tier = 0; tier < storage_tiers ;tier++) {
  329. if(!rd->tiers[tier]) continue;
  330. STORAGE_ENGINE* eng = storage_engine_get(rd->tiers[tier]->mode);
  331. if(eng)
  332. eng->api.free(rd->tiers[tier]->db_metric_handle);
  333. freez(rd->tiers[tier]);
  334. rd->tiers[tier] = NULL;
  335. }
  336. if(rd->db) {
  337. if(rd->rrd_memory_mode == RRD_MEMORY_MODE_RAM)
  338. munmap(rd->db, rd->memsize);
  339. else
  340. freez(rd->db);
  341. }
  342. freez((void *)rd->id);
  343. freez((void *)rd->name);
  344. freez(rd);
  345. }
  346. // ----------------------------------------------------------------------------
  347. // RRDDIM - set dimension options
  348. int rrddim_hide(RRDSET *st, const char *id) {
  349. debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
  350. RRDHOST *host = st->rrdhost;
  351. RRDDIM *rd = rrddim_find(st, id);
  352. if(unlikely(!rd)) {
  353. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  354. return 1;
  355. }
  356. if (!rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN))
  357. (void)sql_set_dimension_option(&rd->metric_uuid, "hidden");
  358. rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  359. rrddim_flag_set(rd, RRDDIM_FLAG_META_HIDDEN);
  360. rrdcontext_updated_rrddim_flags(rd);
  361. return 0;
  362. }
  363. int rrddim_unhide(RRDSET *st, const char *id) {
  364. debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
  365. RRDHOST *host = st->rrdhost;
  366. RRDDIM *rd = rrddim_find(st, id);
  367. if(unlikely(!rd)) {
  368. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  369. return 1;
  370. }
  371. if (rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN))
  372. (void)sql_set_dimension_option(&rd->metric_uuid, NULL);
  373. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  374. rrddim_flag_clear(rd, RRDDIM_FLAG_META_HIDDEN);
  375. rrdcontext_updated_rrddim_flags(rd);
  376. return 0;
  377. }
  378. inline void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd) {
  379. debug(D_RRD_CALLS, "rrddim_is_obsolete() for chart %s, dimension %s", st->name, rd->name);
  380. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))) {
  381. info("Cannot obsolete already archived dimension %s from chart %s", rd->name, st->name);
  382. return;
  383. }
  384. rrddim_flag_set(rd, RRDDIM_FLAG_OBSOLETE);
  385. rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS);
  386. rrdcontext_updated_rrddim_flags(rd);
  387. }
  388. inline void rrddim_isnot_obsolete(RRDSET *st __maybe_unused, RRDDIM *rd) {
  389. debug(D_RRD_CALLS, "rrddim_isnot_obsolete() for chart %s, dimension %s", st->name, rd->name);
  390. rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
  391. rrdcontext_updated_rrddim_flags(rd);
  392. }
  393. // ----------------------------------------------------------------------------
  394. // RRDDIM - collect values for a dimension
  395. inline collected_number rrddim_set_by_pointer(RRDSET *st __maybe_unused, RRDDIM *rd, collected_number value) {
  396. debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
  397. rrdcontext_collected_rrddim(rd);
  398. now_realtime_timeval(&rd->last_collected_time);
  399. rd->collected_value = value;
  400. rd->updated = 1;
  401. rd->collections_counter++;
  402. collected_number v = (value >= 0) ? value : -value;
  403. if(unlikely(v > rd->collected_value_max)) rd->collected_value_max = v;
  404. // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " NETDATA_DOUBLE_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (NETDATA_DOUBLE)((value - rd->last_collected_value) * (NETDATA_DOUBLE)rd->multiplier / (NETDATA_DOUBLE)rd->divisor * 1000000.0 / (NETDATA_DOUBLE)st->usec_since_last_update));
  405. return rd->last_collected_value;
  406. }
  407. collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
  408. RRDHOST *host = st->rrdhost;
  409. RRDDIM *rd = rrddim_find(st, id);
  410. if(unlikely(!rd)) {
  411. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  412. return 0;
  413. }
  414. return rrddim_set_by_pointer(st, rd, value);
  415. }
  416. // ----------------------------------------------------------------------------
  417. // compatibility layer for RRDDIM files v019
  418. #define RRDDIMENSION_MAGIC_V019 "NETDATA RRD DIMENSION FILE V019"
  419. struct avl_element_v019 {
  420. void *avl_link[2];
  421. signed char avl_balance;
  422. };
  423. struct rrddim_map_save_v019 {
  424. struct avl_element_v019 avl; // ignored
  425. void *id; // ignored
  426. void *name; // ignored
  427. uint32_t algorithm; // print warning on mismatch - update on load
  428. uint32_t rrd_memory_mode; // ignored
  429. long long multiplier; // print warning on mismatch - update on load
  430. long long divisor; // print warning on mismatch - update on load
  431. uint32_t flags; // ignored
  432. uint32_t hash; // ignored
  433. uint32_t hash_name; // ignored
  434. void *cache_filename; // ignored - we use it to keep the filename to save back
  435. size_t collections_counter; // ignored
  436. void *state; // ignored
  437. size_t unused[8]; // ignored
  438. long long collected_value_max; // ignored
  439. unsigned int updated:1; // ignored
  440. unsigned int exposed:1; // ignored
  441. struct timeval last_collected_time; // check to reset all - ignored after load
  442. long double calculated_value; // ignored
  443. long double last_calculated_value; // ignored
  444. long double last_stored_value; // ignored
  445. long long collected_value; // ignored
  446. long long last_collected_value; // ignored
  447. long double collected_volume; // ignored
  448. long double stored_volume; // ignored
  449. void *next; // ignored
  450. void *rrdset; // ignored
  451. long entries; // check to reset all - update on load
  452. int update_every; // check to reset all - update on load
  453. size_t memsize; // check to reset all - update on load
  454. char magic[sizeof(RRDDIMENSION_MAGIC_V019) + 1];// check to reset all - update on load
  455. void *variables; // ignored
  456. storage_number values[]; // the array of values
  457. };
  458. size_t rrddim_memory_file_header_size(void) {
  459. return sizeof(struct rrddim_map_save_v019);
  460. }
  461. void rrddim_memory_file_update(RRDDIM *rd) {
  462. if(!rd->rd_on_file) return;
  463. struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
  464. rd_on_file->last_collected_time.tv_sec = rd->last_collected_time.tv_sec;
  465. rd_on_file->last_collected_time.tv_usec = rd->last_collected_time.tv_usec;
  466. }
  467. void rrddim_memory_file_free(RRDDIM *rd) {
  468. if(!rd->rd_on_file) return;
  469. // needed for memory mode map, to save the latest state
  470. rrddim_memory_file_update(rd);
  471. struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
  472. freez(rd_on_file->cache_filename);
  473. munmap(rd_on_file, rd_on_file->memsize);
  474. // remove the pointers from the RRDDIM
  475. rd->rd_on_file = NULL;
  476. rd->db = NULL;
  477. }
  478. const char *rrddim_cache_filename(RRDDIM *rd) {
  479. if(!rd->rd_on_file) return NULL;
  480. struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
  481. return rd_on_file->cache_filename;
  482. }
  483. void rrddim_memory_file_save(RRDDIM *rd) {
  484. if(!rd->rd_on_file) return;
  485. rrddim_memory_file_update(rd);
  486. struct rrddim_map_save_v019 *rd_on_file = rd->rd_on_file;
  487. if(rd_on_file->rrd_memory_mode != RRD_MEMORY_MODE_SAVE) return;
  488. memory_file_save(rd_on_file->cache_filename, rd_on_file, rd_on_file->memsize);
  489. }
  490. bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode) {
  491. if(memory_mode != RRD_MEMORY_MODE_SAVE && memory_mode != RRD_MEMORY_MODE_MAP)
  492. return false;
  493. struct rrddim_map_save_v019 *rd_on_file = NULL;
  494. unsigned long size = sizeof(struct rrddim_map_save_v019) + (st->entries * sizeof(storage_number));
  495. char filename[FILENAME_MAX + 1];
  496. char fullfilename[FILENAME_MAX + 1];
  497. rrdset_strncpyz_name(filename, rd->id, FILENAME_MAX);
  498. snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
  499. rd_on_file = (struct rrddim_map_save_v019 *)netdata_mmap(fullfilename, size,
  500. ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1);
  501. if(unlikely(!rd_on_file)) return false;
  502. struct timeval now;
  503. now_realtime_timeval(&now);
  504. int reset = 0;
  505. rd_on_file->magic[sizeof(RRDDIMENSION_MAGIC_V019)] = '\0';
  506. if(strcmp(rd_on_file->magic, RRDDIMENSION_MAGIC_V019) != 0) {
  507. info("Initializing file %s.", fullfilename);
  508. memset(rd_on_file, 0, size);
  509. reset = 1;
  510. }
  511. else if(rd_on_file->memsize != size) {
  512. error("File %s does not have the desired size, expected %lu but found %lu. Clearing it.", fullfilename, size, rd_on_file->memsize);
  513. memset(rd_on_file, 0, size);
  514. reset = 1;
  515. }
  516. else if(rd_on_file->update_every != st->update_every) {
  517. 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);
  518. memset(rd_on_file, 0, size);
  519. reset = 1;
  520. }
  521. else if(dt_usec(&now, &rd_on_file->last_collected_time) > (rd_on_file->entries * rd_on_file->update_every * USEC_PER_SEC)) {
  522. 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);
  523. memset(rd_on_file, 0, size);
  524. reset = 1;
  525. }
  526. if(!reset) {
  527. if(rd_on_file->algorithm != rd->algorithm)
  528. info("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong.",
  529. fullfilename, rd->algorithm, rrd_algorithm_name(rd->algorithm), rd_on_file->algorithm, rrd_algorithm_name(rd_on_file->algorithm));
  530. if(rd_on_file->multiplier != rd->multiplier)
  531. info("File %s does not have the expected multiplier (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, rd->multiplier, rd_on_file->multiplier);
  532. if(rd_on_file->divisor != rd->divisor)
  533. info("File %s does not have the expected divisor (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, rd->divisor, rd_on_file->divisor);
  534. }
  535. // zero the entire header
  536. memset(rd_on_file, 0, sizeof(struct rrddim_map_save_v019));
  537. // set the important fields
  538. strcpy(rd_on_file->magic, RRDDIMENSION_MAGIC_V019);
  539. rd_on_file->algorithm = rd->algorithm;
  540. rd_on_file->multiplier = rd->multiplier;
  541. rd_on_file->divisor = rd->divisor;
  542. rd_on_file->entries = st->entries;
  543. rd_on_file->update_every = rd->update_every;
  544. rd_on_file->memsize = size;
  545. rd_on_file->rrd_memory_mode = memory_mode;
  546. rd_on_file->cache_filename = strdupz(fullfilename);
  547. rd->db = &rd_on_file->values[0];
  548. rd->rd_on_file = rd_on_file;
  549. rd->memsize = size;
  550. rrddim_memory_file_update(rd);
  551. return true;
  552. }