rrddim.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // RRDDIM index
  6. int rrddim_compare(void* a, void* b) {
  7. if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
  8. else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
  9. else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
  10. }
  11. #define rrddim_index_add(st, rd) (RRDDIM *)avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
  12. #define rrddim_index_del(st,rd ) (RRDDIM *)avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
  13. static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
  14. RRDDIM tmp = {
  15. .id = id,
  16. .hash = (hash)?hash:simple_hash(id)
  17. };
  18. return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
  19. }
  20. // ----------------------------------------------------------------------------
  21. // RRDDIM - find a dimension
  22. inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
  23. debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
  24. return rrddim_index_find(st, id, 0);
  25. }
  26. // ----------------------------------------------------------------------------
  27. // RRDDIM rename a dimension
  28. inline int rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name) {
  29. if(unlikely(!name || !*name || !strcmp(rd->name, name)))
  30. return 0;
  31. debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
  32. char varname[CONFIG_MAX_NAME + 1];
  33. snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
  34. rd->name = config_set_default(st->config_section, varname, name);
  35. rd->hash_name = simple_hash(rd->name);
  36. rrddimvar_rename_all(rd);
  37. rd->exposed = 0;
  38. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  39. return 1;
  40. }
  41. inline int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm) {
  42. if(unlikely(rd->algorithm == algorithm))
  43. return 0;
  44. 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));
  45. rd->algorithm = algorithm;
  46. rd->exposed = 0;
  47. rrdset_flag_set(st, RRDSET_FLAG_HOMEGENEOUS_CHECK);
  48. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  49. return 1;
  50. }
  51. inline int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier) {
  52. if(unlikely(rd->multiplier == multiplier))
  53. return 0;
  54. 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);
  55. rd->multiplier = multiplier;
  56. rd->exposed = 0;
  57. rrdset_flag_set(st, RRDSET_FLAG_HOMEGENEOUS_CHECK);
  58. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  59. return 1;
  60. }
  61. inline int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor) {
  62. if(unlikely(rd->divisor == divisor))
  63. return 0;
  64. 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);
  65. rd->divisor = divisor;
  66. rd->exposed = 0;
  67. rrdset_flag_set(st, RRDSET_FLAG_HOMEGENEOUS_CHECK);
  68. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  69. return 1;
  70. }
  71. // ----------------------------------------------------------------------------
  72. // RRDDIM create a dimension
  73. RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm, RRD_MEMORY_MODE memory_mode) {
  74. rrdset_wrlock(st);
  75. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  76. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  77. RRDDIM *rd = rrddim_find(st, id);
  78. if(unlikely(rd)) {
  79. debug(D_RRD_CALLS, "Cannot create rrd dimension '%s/%s', it already exists.", st->id, name?name:"<NONAME>");
  80. rrddim_set_name(st, rd, name);
  81. rrddim_set_algorithm(st, rd, algorithm);
  82. rrddim_set_multiplier(st, rd, multiplier);
  83. rrddim_set_divisor(st, rd, divisor);
  84. rrdset_unlock(st);
  85. return rd;
  86. }
  87. RRDHOST *host = st->rrdhost;
  88. char filename[FILENAME_MAX + 1];
  89. char fullfilename[FILENAME_MAX + 1];
  90. char varname[CONFIG_MAX_NAME + 1];
  91. unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
  92. debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
  93. rrdset_strncpyz_name(filename, id, FILENAME_MAX);
  94. snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
  95. if(memory_mode == RRD_MEMORY_MODE_SAVE || memory_mode == RRD_MEMORY_MODE_MAP || memory_mode == RRD_MEMORY_MODE_RAM) {
  96. rd = (RRDDIM *)mymmap(
  97. (memory_mode == RRD_MEMORY_MODE_RAM)?NULL:fullfilename
  98. , size
  99. , ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE)
  100. , 1
  101. );
  102. if(likely(rd)) {
  103. // we have a file mapped for rd
  104. memset(&rd->avl, 0, sizeof(avl));
  105. rd->id = NULL;
  106. rd->name = NULL;
  107. rd->cache_filename = NULL;
  108. rd->variables = NULL;
  109. rd->next = NULL;
  110. rd->rrdset = NULL;
  111. rd->exposed = 0;
  112. struct timeval now;
  113. now_realtime_timeval(&now);
  114. if(memory_mode == RRD_MEMORY_MODE_RAM) {
  115. memset(rd, 0, size);
  116. }
  117. else {
  118. int reset = 0;
  119. if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
  120. info("Initializing file %s.", fullfilename);
  121. memset(rd, 0, size);
  122. reset = 1;
  123. }
  124. else if(rd->memsize != size) {
  125. error("File %s does not have the desired size, expected %lu but found %lu. Clearing it.", fullfilename, size, rd->memsize);
  126. memset(rd, 0, size);
  127. reset = 1;
  128. }
  129. else if(rd->update_every != st->update_every) {
  130. error("File %s does not have the same update frequency, expected %d but found %d. Clearing it.", fullfilename, st->update_every, rd->update_every);
  131. memset(rd, 0, size);
  132. reset = 1;
  133. }
  134. else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
  135. info("File %s is too old (last collected %llu seconds ago, but the database is %ld seconds). Clearing it.", fullfilename, dt_usec(&now, &rd->last_collected_time) / USEC_PER_SEC, rd->entries * rd->update_every);
  136. memset(rd, 0, size);
  137. reset = 1;
  138. }
  139. if(!reset) {
  140. if(rd->algorithm != algorithm) {
  141. info("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong.",
  142. fullfilename, algorithm, rrd_algorithm_name(algorithm), rd->algorithm, rrd_algorithm_name(rd->algorithm));
  143. }
  144. if(rd->multiplier != multiplier) {
  145. info("File %s does not have the expected multiplier (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, multiplier, rd->multiplier);
  146. }
  147. if(rd->divisor != divisor) {
  148. info("File %s does not have the expected divisor (expected " COLLECTED_NUMBER_FORMAT ", found " COLLECTED_NUMBER_FORMAT "). Previous values may be wrong.", fullfilename, divisor, rd->divisor);
  149. }
  150. }
  151. }
  152. // make sure we have the right memory mode
  153. // even if we cleared the memory
  154. rd->rrd_memory_mode = memory_mode;
  155. }
  156. }
  157. if(unlikely(!rd)) {
  158. // if we didn't manage to get a mmap'd dimension, just create one
  159. rd = callocz(1, size);
  160. rd->rrd_memory_mode = (memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_ALLOC;
  161. }
  162. rd->memsize = size;
  163. strcpy(rd->magic, RRDDIMENSION_MAGIC);
  164. rd->id = strdupz(id);
  165. rd->hash = simple_hash(rd->id);
  166. rd->cache_filename = strdupz(fullfilename);
  167. snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
  168. rd->name = config_get(st->config_section, varname, (name && *name)?name:rd->id);
  169. rd->hash_name = simple_hash(rd->name);
  170. snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
  171. rd->algorithm = rrd_algorithm_id(config_get(st->config_section, varname, rrd_algorithm_name(algorithm)));
  172. snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
  173. rd->multiplier = config_get_number(st->config_section, varname, multiplier);
  174. snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
  175. rd->divisor = config_get_number(st->config_section, varname, divisor);
  176. if(!rd->divisor) rd->divisor = 1;
  177. rd->entries = st->entries;
  178. rd->update_every = st->update_every;
  179. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  180. rd->collections_counter = 1;
  181. else
  182. rd->collections_counter = 0;
  183. rd->updated = 0;
  184. rd->flags = 0x00000000;
  185. rd->calculated_value = 0;
  186. rd->last_calculated_value = 0;
  187. rd->collected_value = 0;
  188. rd->last_collected_value = 0;
  189. rd->collected_value_max = 0;
  190. rd->collected_volume = 0;
  191. rd->stored_volume = 0;
  192. rd->last_stored_value = 0;
  193. rd->values[st->current_entry] = SN_EMPTY_SLOT; // pack_storage_number(0, SN_NOT_EXISTS);
  194. rd->last_collected_time.tv_sec = 0;
  195. rd->last_collected_time.tv_usec = 0;
  196. rd->rrdset = st;
  197. // append this dimension
  198. if(!st->dimensions)
  199. st->dimensions = rd;
  200. else {
  201. RRDDIM *td = st->dimensions;
  202. if(td->algorithm != rd->algorithm || abs(td->multiplier) != abs(rd->multiplier) || abs(td->divisor) != abs(rd->divisor)) {
  203. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  204. #ifdef NETDATA_INTERNAL_CHECKS
  205. 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 ").",
  206. rd->name,
  207. st->name,
  208. host->hostname,
  209. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(td->algorithm),
  210. rd->multiplier, td->multiplier,
  211. rd->divisor, td->divisor
  212. );
  213. #endif
  214. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  215. }
  216. }
  217. for(; td->next; td = td->next) ;
  218. td->next = rd;
  219. }
  220. if(host->health_enabled) {
  221. rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, RRDVAR_OPTION_DEFAULT);
  222. rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, RRDVAR_OPTION_DEFAULT);
  223. rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  224. }
  225. if(unlikely(rrddim_index_add(st, rd) != rd))
  226. error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
  227. rrdset_unlock(st);
  228. return(rd);
  229. }
  230. // ----------------------------------------------------------------------------
  231. // RRDDIM remove / free a dimension
  232. void rrddim_free(RRDSET *st, RRDDIM *rd)
  233. {
  234. debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
  235. if(rd == st->dimensions)
  236. st->dimensions = rd->next;
  237. else {
  238. RRDDIM *i;
  239. for (i = st->dimensions; i && i->next != rd; i = i->next) ;
  240. if (i && i->next == rd)
  241. i->next = rd->next;
  242. else
  243. error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
  244. }
  245. rd->next = NULL;
  246. while(rd->variables)
  247. rrddimvar_free(rd->variables);
  248. if(unlikely(rrddim_index_del(st, rd) != rd))
  249. error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
  250. // free(rd->annotations);
  251. switch(rd->rrd_memory_mode) {
  252. case RRD_MEMORY_MODE_SAVE:
  253. case RRD_MEMORY_MODE_MAP:
  254. case RRD_MEMORY_MODE_RAM:
  255. debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
  256. freez((void *)rd->id);
  257. freez(rd->cache_filename);
  258. munmap(rd, rd->memsize);
  259. break;
  260. case RRD_MEMORY_MODE_ALLOC:
  261. case RRD_MEMORY_MODE_NONE:
  262. debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
  263. freez((void *)rd->id);
  264. freez(rd->cache_filename);
  265. freez(rd);
  266. break;
  267. }
  268. }
  269. // ----------------------------------------------------------------------------
  270. // RRDDIM - set dimension options
  271. int rrddim_hide(RRDSET *st, const char *id) {
  272. debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
  273. RRDHOST *host = st->rrdhost;
  274. RRDDIM *rd = rrddim_find(st, id);
  275. if(unlikely(!rd)) {
  276. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  277. return 1;
  278. }
  279. rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  280. return 0;
  281. }
  282. int rrddim_unhide(RRDSET *st, const char *id) {
  283. debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
  284. RRDHOST *host = st->rrdhost;
  285. RRDDIM *rd = rrddim_find(st, id);
  286. if(unlikely(!rd)) {
  287. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  288. return 1;
  289. }
  290. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  291. return 0;
  292. }
  293. // ----------------------------------------------------------------------------
  294. // RRDDIM - collect values for a dimension
  295. inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
  296. debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
  297. now_realtime_timeval(&rd->last_collected_time);
  298. rd->collected_value = value;
  299. rd->updated = 1;
  300. rd->collections_counter++;
  301. collected_number v = (value >= 0) ? value : -value;
  302. if(unlikely(v > rd->collected_value_max)) rd->collected_value_max = v;
  303. // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
  304. return rd->last_collected_value;
  305. }
  306. collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
  307. RRDHOST *host = st->rrdhost;
  308. RRDDIM *rd = rrddim_find(st, id);
  309. if(unlikely(!rd)) {
  310. error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
  311. return 0;
  312. }
  313. return rrddim_set_by_pointer(st, rd, value);
  314. }