rrdset.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. #include <sched.h>
  5. void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
  6. debug(D_RRD_CALLS, "Checking read lock on chart '%s'", st->id);
  7. int ret = netdata_rwlock_trywrlock(&st->rrdset_rwlock);
  8. if(ret == 0)
  9. fatal("RRDSET '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
  10. }
  11. void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
  12. debug(D_RRD_CALLS, "Checking write lock on chart '%s'", st->id);
  13. int ret = netdata_rwlock_tryrdlock(&st->rrdset_rwlock);
  14. if(ret == 0)
  15. fatal("RRDSET '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
  16. }
  17. // ----------------------------------------------------------------------------
  18. // RRDSET index
  19. int rrdset_compare(void* a, void* b) {
  20. if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
  21. else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
  22. else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
  23. }
  24. static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
  25. RRDSET tmp;
  26. strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
  27. tmp.hash = (hash)?hash:simple_hash(tmp.id);
  28. return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl_t *) &tmp);
  29. }
  30. // ----------------------------------------------------------------------------
  31. // RRDSET name index
  32. #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
  33. int rrdset_compare_name(void* a, void* b) {
  34. RRDSET *A = rrdset_from_avlname(a);
  35. RRDSET *B = rrdset_from_avlname(b);
  36. // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
  37. if(A->hash_name < B->hash_name) return -1;
  38. else if(A->hash_name > B->hash_name) return 1;
  39. else return strcmp(A->name, B->name);
  40. }
  41. RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
  42. void *result;
  43. // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
  44. result = avl_insert_lock(&host->rrdset_root_index_name, (avl_t *) (&st->avlname));
  45. if(result) return rrdset_from_avlname(result);
  46. return NULL;
  47. }
  48. RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
  49. void *result;
  50. // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
  51. result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl_t *)(&st->avlname));
  52. if(result) return rrdset_from_avlname(result);
  53. return NULL;
  54. }
  55. // ----------------------------------------------------------------------------
  56. // RRDSET - find charts
  57. static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
  58. void *result = NULL;
  59. RRDSET tmp;
  60. tmp.name = name;
  61. tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
  62. // fprintf(stderr, "SEARCHING: %s\n", name);
  63. result = avl_search_lock(&host->rrdset_root_index_name, (avl_t *) (&(tmp.avlname)));
  64. if(result) {
  65. RRDSET *st = rrdset_from_avlname(result);
  66. if(strcmp(st->magic, RRDSET_MAGIC) != 0)
  67. error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
  68. // fprintf(stderr, "FOUND: %s\n", name);
  69. return rrdset_from_avlname(result);
  70. }
  71. // fprintf(stderr, "NOT FOUND: %s\n", name);
  72. return NULL;
  73. }
  74. inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
  75. debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, host->hostname);
  76. RRDSET *st = rrdset_index_find(host, id, 0);
  77. return(st);
  78. }
  79. inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
  80. debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, host->hostname);
  81. char buf[RRD_ID_LENGTH_MAX + 1];
  82. strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
  83. strcat(buf, ".");
  84. int len = (int) strlen(buf);
  85. strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
  86. return(rrdset_find(host, buf));
  87. }
  88. inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
  89. debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, host->hostname);
  90. RRDSET *st = rrdset_index_find_name(host, name, 0);
  91. return(st);
  92. }
  93. // ----------------------------------------------------------------------------
  94. // RRDSET - rename charts
  95. char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
  96. char c, *p = to;
  97. while (length-- && (c = *from++)) {
  98. if(c != '.' && !isalnum(c))
  99. c = '_';
  100. *p++ = c;
  101. }
  102. *p = '\0';
  103. return to;
  104. }
  105. int rrdset_set_name(RRDSET *st, const char *name) {
  106. if(unlikely(st->name && !strcmp(st->name, name)))
  107. return 1;
  108. RRDHOST *host = st->rrdhost;
  109. debug(D_RRD_CALLS, "rrdset_set_name() old: '%s', new: '%s'", st->name?st->name:"", name);
  110. char b[CONFIG_MAX_VALUE + 1];
  111. char n[RRD_ID_LENGTH_MAX + 1];
  112. snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
  113. rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
  114. if(rrdset_index_find_name(host, b, 0)) {
  115. info("RRDSET: chart name '%s' on host '%s' already exists.", b, host->hostname);
  116. return 0;
  117. }
  118. if(st->name) {
  119. rrdset_index_del_name(host, st);
  120. st->name = config_set_default(st->config_section, "name", b);
  121. st->hash_name = simple_hash(st->name);
  122. rrdsetvar_rename_all(st);
  123. }
  124. else {
  125. st->name = config_get(st->config_section, "name", b);
  126. st->hash_name = simple_hash(st->name);
  127. }
  128. rrdset_wrlock(st);
  129. RRDDIM *rd;
  130. rrddim_foreach_write(rd, st)
  131. rrddimvar_rename_all(rd);
  132. rrdset_unlock(st);
  133. if(unlikely(rrdset_index_add_name(host, st) != st))
  134. error("RRDSET: INTERNAL ERROR: attempted to index duplicate chart name '%s'", st->name);
  135. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_SEND);
  136. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_IGNORE);
  137. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_SEND);
  138. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_IGNORE);
  139. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  140. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  141. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  142. return 2;
  143. }
  144. inline void rrdset_is_obsolete(RRDSET *st) {
  145. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED))) {
  146. info("Cannot obsolete already archived chart %s", st->name);
  147. return;
  148. }
  149. if(unlikely(!(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  150. rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE);
  151. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  152. // the chart will not get more updates (data collection)
  153. // so, we have to push its definition now
  154. rrdset_push_chart_definition_now(st);
  155. }
  156. }
  157. inline void rrdset_isnot_obsolete(RRDSET *st) {
  158. if(unlikely((rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)))) {
  159. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  160. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  161. // the chart will be pushed upstream automatically
  162. // due to data collection
  163. }
  164. }
  165. inline void rrdset_update_heterogeneous_flag(RRDSET *st) {
  166. RRDHOST *host = st->rrdhost;
  167. (void)host;
  168. RRDDIM *rd;
  169. rrdset_flag_clear(st, RRDSET_FLAG_HOMOGENEOUS_CHECK);
  170. RRD_ALGORITHM algorithm = st->dimensions->algorithm;
  171. collected_number multiplier = ABS(st->dimensions->multiplier);
  172. collected_number divisor = ABS(st->dimensions->divisor);
  173. rrddim_foreach_read(rd, st) {
  174. if(algorithm != rd->algorithm || multiplier != ABS(rd->multiplier) || divisor != ABS(rd->divisor)) {
  175. if(!rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS)) {
  176. #ifdef NETDATA_INTERNAL_CHECKS
  177. 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 ").",
  178. rd->name,
  179. st->name,
  180. host->hostname,
  181. rrd_algorithm_name(rd->algorithm), rrd_algorithm_name(algorithm),
  182. rd->multiplier, multiplier,
  183. rd->divisor, divisor
  184. );
  185. #endif
  186. rrdset_flag_set(st, RRDSET_FLAG_HETEROGENEOUS);
  187. }
  188. return;
  189. }
  190. }
  191. rrdset_flag_clear(st, RRDSET_FLAG_HETEROGENEOUS);
  192. }
  193. // ----------------------------------------------------------------------------
  194. // RRDSET - reset a chart
  195. void rrdset_reset(RRDSET *st) {
  196. debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
  197. st->last_collected_time.tv_sec = 0;
  198. st->last_collected_time.tv_usec = 0;
  199. st->last_updated.tv_sec = 0;
  200. st->last_updated.tv_usec = 0;
  201. st->current_entry = 0;
  202. st->counter = 0;
  203. st->counter_done = 0;
  204. st->rrddim_page_alignment = 0;
  205. RRDDIM *rd;
  206. rrddim_foreach_read(rd, st) {
  207. rd->last_collected_time.tv_sec = 0;
  208. rd->last_collected_time.tv_usec = 0;
  209. rd->collections_counter = 0;
  210. // memset(rd->values, 0, rd->entries * sizeof(storage_number));
  211. #ifdef ENABLE_DBENGINE
  212. if (RRD_MEMORY_MODE_DBENGINE == st->rrd_memory_mode && !rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  213. rrdeng_store_metric_flush_current_page(rd);
  214. }
  215. #endif
  216. }
  217. }
  218. // ----------------------------------------------------------------------------
  219. // RRDSET - helpers for rrdset_create()
  220. inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
  221. if(unlikely(entries < 5)) entries = 5;
  222. if(unlikely(entries > RRD_HISTORY_ENTRIES_MAX)) entries = RRD_HISTORY_ENTRIES_MAX;
  223. if(unlikely(mode == RRD_MEMORY_MODE_NONE || mode == RRD_MEMORY_MODE_ALLOC))
  224. return entries;
  225. long page = (size_t)sysconf(_SC_PAGESIZE);
  226. long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
  227. if(unlikely(size % page)) {
  228. size -= (size % page);
  229. size += page;
  230. long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
  231. return n;
  232. }
  233. return entries;
  234. }
  235. static inline void last_collected_time_align(RRDSET *st) {
  236. st->last_collected_time.tv_sec -= st->last_collected_time.tv_sec % st->update_every;
  237. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)))
  238. st->last_collected_time.tv_usec = 0;
  239. else
  240. st->last_collected_time.tv_usec = 500000;
  241. }
  242. static inline void last_updated_time_align(RRDSET *st) {
  243. st->last_updated.tv_sec -= st->last_updated.tv_sec % st->update_every;
  244. st->last_updated.tv_usec = 0;
  245. }
  246. // ----------------------------------------------------------------------------
  247. // RRDSET - free a chart
  248. void rrdset_free(RRDSET *st) {
  249. if(unlikely(!st)) return;
  250. RRDHOST *host = st->rrdhost;
  251. rrdhost_check_wrlock(host); // make sure we have a write lock on the host
  252. rrdset_wrlock(st); // lock this RRDSET
  253. // info("Removing chart '%s' ('%s')", st->id, st->name);
  254. // ------------------------------------------------------------------------
  255. // remove it from the indexes
  256. if(unlikely(rrdset_index_del(host, st) != st))
  257. error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
  258. rrdset_index_del_name(host, st);
  259. // ------------------------------------------------------------------------
  260. // free its children structures
  261. freez(st->exporting_flags);
  262. while(st->variables) rrdsetvar_free(st->variables);
  263. // while(st->alarms) rrdsetcalc_unlink(st->alarms);
  264. /* We must free all connected alarms here in case this has been an ephemeral chart whose alarm was
  265. * created by a template. This leads to an effective memory leak, which cannot be detected since the
  266. * alarms will still be connected to the host, and freed during shutdown. */
  267. while(st->alarms) rrdcalc_unlink_and_free(st->rrdhost, st->alarms);
  268. while(st->dimensions) rrddim_free(st, st->dimensions);
  269. rrdfamily_free(host, st->rrdfamily);
  270. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  271. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  272. // ------------------------------------------------------------------------
  273. // remove it from the configuration
  274. appconfig_section_destroy_non_loaded(&netdata_config, st->config_section);
  275. // ------------------------------------------------------------------------
  276. // unlink it from the host
  277. if(st == host->rrdset_root) {
  278. host->rrdset_root = st->next;
  279. }
  280. else {
  281. // find the previous one
  282. RRDSET *s;
  283. for(s = host->rrdset_root; s && s->next != st ; s = s->next) ;
  284. // bypass it
  285. if(s) s->next = st->next;
  286. else error("Request to free RRDSET '%s': cannot find it under host '%s'", st->id, host->hostname);
  287. }
  288. rrdset_unlock(st);
  289. // ------------------------------------------------------------------------
  290. // free it
  291. netdata_rwlock_destroy(&st->rrdset_rwlock);
  292. netdata_rwlock_destroy(&st->state->labels.labels_rwlock);
  293. // free directly allocated members
  294. freez(st->config_section);
  295. freez(st->plugin_name);
  296. freez(st->module_name);
  297. freez(st->state->old_title);
  298. freez(st->state->old_context);
  299. free_label_list(st->state->labels.head);
  300. freez(st->state);
  301. freez(st->chart_uuid);
  302. switch(st->rrd_memory_mode) {
  303. case RRD_MEMORY_MODE_SAVE:
  304. case RRD_MEMORY_MODE_MAP:
  305. case RRD_MEMORY_MODE_RAM:
  306. debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
  307. munmap(st, st->memsize);
  308. break;
  309. case RRD_MEMORY_MODE_ALLOC:
  310. case RRD_MEMORY_MODE_NONE:
  311. case RRD_MEMORY_MODE_DBENGINE:
  312. freez(st);
  313. break;
  314. }
  315. }
  316. void rrdset_save(RRDSET *st) {
  317. rrdset_check_rdlock(st);
  318. // info("Saving chart '%s' ('%s')", st->id, st->name);
  319. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
  320. debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
  321. memory_file_save(st->cache_filename, st, st->memsize);
  322. }
  323. RRDDIM *rd;
  324. rrddim_foreach_read(rd, st) {
  325. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
  326. debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
  327. memory_file_save(rd->cache_filename, rd, rd->memsize);
  328. }
  329. }
  330. }
  331. void rrdset_delete_custom(RRDSET *st, int db_rotated) {
  332. RRDDIM *rd;
  333. #ifndef ENABLE_ACLK
  334. UNUSED(db_rotated);
  335. #endif
  336. rrdset_check_rdlock(st);
  337. info("Deleting chart '%s' ('%s') from disk...", st->id, st->name);
  338. if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
  339. info("Deleting chart header file '%s'.", st->cache_filename);
  340. if(unlikely(unlink(st->cache_filename) == -1))
  341. error("Cannot delete chart header file '%s'", st->cache_filename);
  342. }
  343. rrddim_foreach_read(rd, st) {
  344. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  345. info("Deleting dimension file '%s'.", rd->cache_filename);
  346. if(unlikely(unlink(rd->cache_filename) == -1))
  347. error("Cannot delete dimension file '%s'", rd->cache_filename);
  348. }
  349. }
  350. recursively_delete_dir(st->cache_dir, "left-over chart");
  351. #ifdef ENABLE_ACLK
  352. if ((netdata_cloud_setting) && (db_rotated || RRD_MEMORY_MODE_DBENGINE != st->rrd_memory_mode)) {
  353. aclk_del_collector(st->rrdhost, st->plugin_name, st->module_name);
  354. st->rrdhost->obsolete_count++;
  355. }
  356. #endif
  357. }
  358. void rrdset_delete_obsolete_dimensions(RRDSET *st) {
  359. RRDDIM *rd;
  360. rrdset_check_rdlock(st);
  361. info("Deleting dimensions of chart '%s' ('%s') from disk...", st->id, st->name);
  362. rrddim_foreach_read(rd, st) {
  363. if(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
  364. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  365. info("Deleting dimension file '%s'.", rd->cache_filename);
  366. if(unlikely(unlink(rd->cache_filename) == -1))
  367. error("Cannot delete dimension file '%s'", rd->cache_filename);
  368. }
  369. }
  370. }
  371. }
  372. // ----------------------------------------------------------------------------
  373. // RRDSET - create a chart
  374. static inline RRDSET *rrdset_find_on_create(RRDHOST *host, const char *fullid) {
  375. RRDSET *st = rrdset_find(host, fullid);
  376. if(unlikely(st)) {
  377. rrdset_isnot_obsolete(st);
  378. debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
  379. return st;
  380. }
  381. return NULL;
  382. }
  383. RRDSET *rrdset_create_custom(
  384. RRDHOST *host
  385. , const char *type
  386. , const char *id
  387. , const char *name
  388. , const char *family
  389. , const char *context
  390. , const char *title
  391. , const char *units
  392. , const char *plugin
  393. , const char *module
  394. , long priority
  395. , int update_every
  396. , RRDSET_TYPE chart_type
  397. , RRD_MEMORY_MODE memory_mode
  398. , long history_entries
  399. ) {
  400. if(!type || !type[0]) {
  401. fatal("Cannot create rrd stats without a type: id '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  402. , (id && *id)?id:"<unset>"
  403. , (name && *name)?name:"<unset>"
  404. , (family && *family)?family:"<unset>"
  405. , (context && *context)?context:"<unset>"
  406. , (title && *title)?title:"<unset>"
  407. , (units && *units)?units:"<unset>"
  408. , (plugin && *plugin)?plugin:"<unset>"
  409. , (module && *module)?module:"<unset>"
  410. );
  411. return NULL;
  412. }
  413. if(!id || !id[0]) {
  414. fatal("Cannot create rrd stats without an id: type '%s', name '%s', family '%s', context '%s', title '%s', units '%s', plugin '%s', module '%s'."
  415. , type
  416. , (name && *name)?name:"<unset>"
  417. , (family && *family)?family:"<unset>"
  418. , (context && *context)?context:"<unset>"
  419. , (title && *title)?title:"<unset>"
  420. , (units && *units)?units:"<unset>"
  421. , (plugin && *plugin)?plugin:"<unset>"
  422. , (module && *module)?module:"<unset>"
  423. );
  424. return NULL;
  425. }
  426. // ------------------------------------------------------------------------
  427. // check if it already exists
  428. char fullid[RRD_ID_LENGTH_MAX + 1];
  429. snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
  430. int changed_from_archived_to_active = 0;
  431. RRDSET *st = rrdset_find_on_create(host, fullid);
  432. if (st) {
  433. int mark_rebuild = 0;
  434. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  435. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  436. if (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)) {
  437. rrdset_flag_clear(st, RRDSET_FLAG_ARCHIVED);
  438. changed_from_archived_to_active = 1;
  439. mark_rebuild |= META_CHART_ACTIVATED;
  440. }
  441. char *old_plugin = NULL, *old_module = NULL, *old_title = NULL, *old_context = NULL,
  442. *old_title_v = NULL, *old_context_v = NULL;
  443. int rc;
  444. if(unlikely(name))
  445. rc = rrdset_set_name(st, name);
  446. else
  447. rc = rrdset_set_name(st, id);
  448. if (rc == 2)
  449. mark_rebuild |= META_CHART_UPDATED;
  450. if (unlikely(st->priority != priority)) {
  451. st->priority = priority;
  452. mark_rebuild |= META_CHART_UPDATED;
  453. }
  454. if (unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && st->update_every != update_every)) {
  455. st->update_every = update_every;
  456. mark_rebuild |= META_CHART_UPDATED;
  457. }
  458. if (plugin && st->plugin_name) {
  459. if (unlikely(strcmp(plugin, st->plugin_name))) {
  460. old_plugin = st->plugin_name;
  461. st->plugin_name = strdupz(plugin);
  462. mark_rebuild |= META_PLUGIN_UPDATED;
  463. }
  464. } else {
  465. if (plugin != st->plugin_name) { // one is NULL?
  466. old_plugin = st->plugin_name;
  467. st->plugin_name = plugin ? strdupz(plugin) : NULL;
  468. mark_rebuild |= META_PLUGIN_UPDATED;
  469. }
  470. }
  471. if (module && st->module_name) {
  472. if (unlikely(strcmp(module, st->module_name))) {
  473. old_module = st->module_name;
  474. st->module_name = strdupz(module);
  475. mark_rebuild |= META_MODULE_UPDATED;
  476. }
  477. } else {
  478. if (module != st->module_name) {
  479. if (st->module_name && *st->module_name) {
  480. old_module = st->module_name;
  481. st->module_name = module ? strdupz(module) : NULL;
  482. mark_rebuild |= META_MODULE_UPDATED;
  483. }
  484. }
  485. }
  486. if (unlikely(title && st->state->old_title && strcmp(st->state->old_title, title))) {
  487. char *new_title = strdupz(title);
  488. old_title_v = st->state->old_title;
  489. st->state->old_title = strdupz(title);
  490. json_fix_string(new_title);
  491. old_title = st->title;
  492. st->title = new_title;
  493. mark_rebuild |= META_CHART_UPDATED;
  494. }
  495. RRDSET_TYPE new_chart_type =
  496. rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
  497. if (st->chart_type != new_chart_type) {
  498. st->chart_type = new_chart_type;
  499. mark_rebuild |= META_CHART_UPDATED;
  500. }
  501. if (unlikely(context && st->state->old_context && strcmp(st->state->old_context, context))) {
  502. char *new_context = strdupz(context);
  503. old_context_v = st->state->old_context;
  504. st->state->old_context = strdupz(context);
  505. json_fix_string(new_context);
  506. old_context = st->context;
  507. st->context = new_context;
  508. st->hash_context = simple_hash(st->context);
  509. mark_rebuild |= META_CHART_UPDATED;
  510. }
  511. if (mark_rebuild) {
  512. #ifdef ENABLE_ACLK
  513. if (netdata_cloud_setting) {
  514. if (mark_rebuild & META_CHART_ACTIVATED) {
  515. aclk_add_collector(host, st->plugin_name, st->module_name);
  516. }
  517. else {
  518. if (mark_rebuild & (META_PLUGIN_UPDATED | META_MODULE_UPDATED)) {
  519. aclk_del_collector(
  520. host, mark_rebuild & META_PLUGIN_UPDATED ? old_plugin : st->plugin_name,
  521. mark_rebuild & META_MODULE_UPDATED ? old_module : st->module_name);
  522. aclk_add_collector(host, st->plugin_name, st->module_name);
  523. }
  524. }
  525. rrdset_flag_set(st, RRDSET_FLAG_ACLK);
  526. }
  527. #endif
  528. freez(old_plugin);
  529. freez(old_module);
  530. freez(old_title);
  531. freez(old_context);
  532. freez(old_title_v);
  533. freez(old_context_v);
  534. if (mark_rebuild != META_CHART_ACTIVATED) {
  535. info("Collector updated metadata for chart %s", st->id);
  536. sched_yield();
  537. }
  538. }
  539. if (mark_rebuild & (META_CHART_UPDATED | META_PLUGIN_UPDATED | META_MODULE_UPDATED)) {
  540. debug(D_METADATALOG, "CHART [%s] metadata updated", st->id);
  541. int rc = update_chart_metadata(st->chart_uuid, st, id, name);
  542. if (unlikely(rc))
  543. error_report("Failed to update chart metadata in the database");
  544. }
  545. /* Fall-through during switch from archived to active so that the host lock is taken and health is linked */
  546. if (!changed_from_archived_to_active)
  547. return st;
  548. }
  549. rrdhost_wrlock(host);
  550. st = rrdset_find_on_create(host, fullid);
  551. if(st) {
  552. if (changed_from_archived_to_active) {
  553. rrdset_flag_clear(st, RRDSET_FLAG_ARCHIVED);
  554. rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  555. rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, RRDVAR_OPTION_DEFAULT);
  556. rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, RRDVAR_OPTION_DEFAULT);
  557. rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, RRDVAR_OPTION_DEFAULT);
  558. rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, RRDVAR_OPTION_DEFAULT);
  559. rrdsetcalc_link_matching(st);
  560. rrdcalctemplate_link_matching(st);
  561. }
  562. rrdhost_unlock(host);
  563. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  564. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  565. return st;
  566. }
  567. char fullfilename[FILENAME_MAX + 1];
  568. // ------------------------------------------------------------------------
  569. // compose the config_section for this chart
  570. char config_section[RRD_ID_LENGTH_MAX + 1];
  571. if(host == localhost)
  572. strcpy(config_section, fullid);
  573. else
  574. snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
  575. // ------------------------------------------------------------------------
  576. // get the options from the config, we need to create it
  577. long entries;
  578. if(memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  579. // only sets it the first time
  580. entries = config_get_number(config_section, "history", 5);
  581. } else {
  582. long rentries = config_get_number(config_section, "history", history_entries);
  583. entries = align_entries_to_pagesize(memory_mode, rentries);
  584. if (entries != rentries) entries = config_set_number(config_section, "history", entries);
  585. if (memory_mode == RRD_MEMORY_MODE_NONE && entries != rentries)
  586. entries = config_set_number(config_section, "history", 10);
  587. }
  588. int enabled = config_get_boolean(config_section, "enabled", 1);
  589. if(!enabled) entries = 5;
  590. unsigned long size = sizeof(RRDSET);
  591. char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
  592. time_t now = now_realtime_sec();
  593. // ------------------------------------------------------------------------
  594. // load it or allocate it
  595. debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
  596. snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
  597. if(memory_mode == RRD_MEMORY_MODE_SAVE || memory_mode == RRD_MEMORY_MODE_MAP ||
  598. memory_mode == RRD_MEMORY_MODE_RAM) {
  599. st = (RRDSET *) mymmap(
  600. (memory_mode == RRD_MEMORY_MODE_RAM) ? NULL : fullfilename
  601. , size
  602. , ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE)
  603. , 0
  604. );
  605. if(st) {
  606. memset(&st->avl, 0, sizeof(avl_t));
  607. memset(&st->avlname, 0, sizeof(avl_t));
  608. memset(&st->rrdvar_root_index, 0, sizeof(avl_tree_lock));
  609. memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
  610. memset(&st->rrdset_rwlock, 0, sizeof(netdata_rwlock_t));
  611. st->name = NULL;
  612. st->config_section = NULL;
  613. st->type = NULL;
  614. st->family = NULL;
  615. st->title = NULL;
  616. st->units = NULL;
  617. st->context = NULL;
  618. st->cache_dir = NULL;
  619. st->plugin_name = NULL;
  620. st->module_name = NULL;
  621. st->dimensions = NULL;
  622. st->rrdfamily = NULL;
  623. st->rrdhost = NULL;
  624. st->next = NULL;
  625. st->variables = NULL;
  626. st->alarms = NULL;
  627. st->flags = 0x00000000;
  628. st->exporting_flags = NULL;
  629. if(memory_mode == RRD_MEMORY_MODE_RAM) {
  630. memset(st, 0, size);
  631. }
  632. else {
  633. if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
  634. info("Initializing file %s.", fullfilename);
  635. memset(st, 0, size);
  636. }
  637. else if(strcmp(st->id, fullid) != 0) {
  638. error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
  639. // munmap(st, size);
  640. // st = NULL;
  641. memset(st, 0, size);
  642. }
  643. else if(st->memsize != size || st->entries != entries) {
  644. error("File %s does not have the desired size. Clearing it.", fullfilename);
  645. memset(st, 0, size);
  646. }
  647. else if(st->update_every != update_every) {
  648. error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
  649. memset(st, 0, size);
  650. }
  651. else if((now - st->last_updated.tv_sec) > update_every * entries) {
  652. info("File %s is too old. Clearing it.", fullfilename);
  653. memset(st, 0, size);
  654. }
  655. else if(st->last_updated.tv_sec > now + update_every) {
  656. error("File %s refers to the future by %zd secs. Resetting it to now.", fullfilename, (ssize_t)(st->last_updated.tv_sec - now));
  657. st->last_updated.tv_sec = now;
  658. }
  659. // make sure the database is aligned
  660. if(st->last_updated.tv_sec) {
  661. st->update_every = update_every;
  662. last_updated_time_align(st);
  663. }
  664. }
  665. // make sure we have the right memory mode
  666. // even if we cleared the memory
  667. st->rrd_memory_mode = memory_mode;
  668. }
  669. }
  670. if(unlikely(!st)) {
  671. st = callocz(1, size);
  672. if (memory_mode == RRD_MEMORY_MODE_DBENGINE)
  673. st->rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  674. else
  675. st->rrd_memory_mode = (memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_ALLOC;
  676. }
  677. st->plugin_name = plugin?strdupz(plugin):NULL;
  678. st->module_name = module?strdupz(module):NULL;
  679. st->config_section = strdupz(config_section);
  680. st->rrdhost = host;
  681. st->memsize = size;
  682. st->entries = entries;
  683. st->update_every = update_every;
  684. if(st->current_entry >= st->entries) st->current_entry = 0;
  685. strcpy(st->cache_filename, fullfilename);
  686. strcpy(st->magic, RRDSET_MAGIC);
  687. strcpy(st->id, fullid);
  688. st->hash = simple_hash(st->id);
  689. st->cache_dir = cache_dir;
  690. st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
  691. st->type = config_get(st->config_section, "type", type);
  692. st->state = callocz(1, sizeof(*st->state));
  693. st->family = config_get(st->config_section, "family", family?family:st->type);
  694. json_fix_string(st->family);
  695. st->units = config_get(st->config_section, "units", units?units:"");
  696. json_fix_string(st->units);
  697. st->context = config_get(st->config_section, "context", context?context:st->id);
  698. st->state->old_context = strdupz(st->context);
  699. json_fix_string(st->context);
  700. st->hash_context = simple_hash(st->context);
  701. st->priority = config_get_number(st->config_section, "priority", priority);
  702. if(enabled)
  703. rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
  704. else
  705. rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
  706. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  707. rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
  708. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  709. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_SEND);
  710. rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_IGNORE);
  711. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_SEND);
  712. rrdset_flag_clear(st, RRDSET_FLAG_BACKEND_IGNORE);
  713. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  714. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  715. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  716. rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
  717. // if(!strcmp(st->id, "disk_util.dm-0")) {
  718. // st->debug = 1;
  719. // error("enabled debugging for '%s'", st->id);
  720. // }
  721. // else error("not enabled debugging for '%s'", st->id);
  722. st->green = NAN;
  723. st->red = NAN;
  724. st->last_collected_time.tv_sec = 0;
  725. st->last_collected_time.tv_usec = 0;
  726. st->counter_done = 0;
  727. st->rrddim_page_alignment = 0;
  728. st->gap_when_lost_iterations_above = (int) (gap_when_lost_iterations_above + 2);
  729. st->last_accessed_time = 0;
  730. st->upstream_resync_time = 0;
  731. avl_init_lock(&st->dimensions_index, rrddim_compare);
  732. avl_init_lock(&st->rrdvar_root_index, rrdvar_compare);
  733. netdata_rwlock_init(&st->rrdset_rwlock);
  734. netdata_rwlock_init(&st->state->labels.labels_rwlock);
  735. if(name && *name && rrdset_set_name(st, name))
  736. // we did set the name
  737. ;
  738. else
  739. // could not use the name, use the id
  740. rrdset_set_name(st, id);
  741. st->title = config_get(st->config_section, "title", title);
  742. st->state->old_title = strdupz(st->title);
  743. json_fix_string(st->title);
  744. st->rrdfamily = rrdfamily_create(host, st->family);
  745. st->next = host->rrdset_root;
  746. host->rrdset_root = st;
  747. if(host->health_enabled) {
  748. rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, RRDVAR_OPTION_DEFAULT);
  749. rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, RRDVAR_OPTION_DEFAULT);
  750. rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, RRDVAR_OPTION_DEFAULT);
  751. rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, RRDVAR_OPTION_DEFAULT);
  752. rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, RRDVAR_OPTION_DEFAULT);
  753. }
  754. if(unlikely(rrdset_index_add(host, st) != st))
  755. error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
  756. rrdsetcalc_link_matching(st);
  757. rrdcalctemplate_link_matching(st);
  758. st->chart_uuid = find_chart_uuid(host, type, id, name);
  759. if (unlikely(!st->chart_uuid))
  760. st->chart_uuid = create_chart_uuid(st, id, name);
  761. else
  762. update_chart_metadata(st->chart_uuid, st, id, name);
  763. store_active_chart(st->chart_uuid);
  764. #ifdef ENABLE_ACLK
  765. host->obsolete_count = 0;
  766. #endif
  767. rrdhost_cleanup_obsolete_charts(host);
  768. #ifdef ENABLE_ACLK
  769. if (host->obsolete_count)
  770. aclk_update_chart(st->rrdhost, "dummy-chart", ACLK_CMD_CHARTDEL);
  771. #endif
  772. rrdhost_unlock(host);
  773. #ifdef ENABLE_ACLK
  774. if (netdata_cloud_setting)
  775. aclk_add_collector(host, plugin, module);
  776. rrdset_flag_set(st, RRDSET_FLAG_ACLK);
  777. #endif
  778. return(st);
  779. }
  780. // ----------------------------------------------------------------------------
  781. // RRDSET - data collection iteration control
  782. inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
  783. if(unlikely(!st->last_collected_time.tv_sec || !microseconds || (rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK)))) {
  784. // call the full next_usec() function
  785. rrdset_next_usec(st, microseconds);
  786. return;
  787. }
  788. st->usec_since_last_update = microseconds;
  789. }
  790. inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
  791. struct timeval now;
  792. now_realtime_timeval(&now);
  793. #ifdef NETDATA_INTERNAL_CHECKS
  794. char *discard_reason = NULL;
  795. usec_t discarded = microseconds;
  796. #endif
  797. if(unlikely(rrdset_flag_check_noatomic(st, RRDSET_FLAG_SYNC_CLOCK))) {
  798. // the chart needs to be re-synced to current time
  799. rrdset_flag_clear(st, RRDSET_FLAG_SYNC_CLOCK);
  800. // discard the microseconds supplied
  801. microseconds = 0;
  802. #ifdef NETDATA_INTERNAL_CHECKS
  803. if(!discard_reason) discard_reason = "SYNC CLOCK FLAG";
  804. #endif
  805. }
  806. if(unlikely(!st->last_collected_time.tv_sec)) {
  807. // the first entry
  808. microseconds = st->update_every * USEC_PER_SEC;
  809. #ifdef NETDATA_INTERNAL_CHECKS
  810. if(!discard_reason) discard_reason = "FIRST DATA COLLECTION";
  811. #endif
  812. }
  813. else if(unlikely(!microseconds)) {
  814. // no dt given by the plugin
  815. microseconds = dt_usec(&now, &st->last_collected_time);
  816. #ifdef NETDATA_INTERNAL_CHECKS
  817. if(!discard_reason) discard_reason = "NO USEC GIVEN BY COLLECTOR";
  818. #endif
  819. }
  820. else {
  821. // microseconds has the time since the last collection
  822. susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
  823. if(unlikely(since_last_usec < 0)) {
  824. // oops! the database is in the future
  825. info("RRD database for chart '%s' on host '%s' is %0.5" LONG_DOUBLE_MODIFIER " secs in the future (counter #%zu, update #%zu). Adjusting it to current time.", st->id, st->rrdhost->hostname, (LONG_DOUBLE)-since_last_usec / USEC_PER_SEC, st->counter, st->counter_done);
  826. st->last_collected_time.tv_sec = now.tv_sec - st->update_every;
  827. st->last_collected_time.tv_usec = now.tv_usec;
  828. last_collected_time_align(st);
  829. st->last_updated.tv_sec = now.tv_sec - st->update_every;
  830. st->last_updated.tv_usec = now.tv_usec;
  831. last_updated_time_align(st);
  832. microseconds = st->update_every * USEC_PER_SEC;
  833. #ifdef NETDATA_INTERNAL_CHECKS
  834. if(!discard_reason) discard_reason = "COLLECTION TIME IN FUTURE";
  835. #endif
  836. }
  837. else if(unlikely((usec_t)since_last_usec > (usec_t)(st->update_every * 5 * USEC_PER_SEC))) {
  838. // oops! the database is too far behind
  839. info("RRD database for chart '%s' on host '%s' is %0.5" LONG_DOUBLE_MODIFIER " secs in the past (counter #%zu, update #%zu). Adjusting it to current time.", st->id, st->rrdhost->hostname, (LONG_DOUBLE)since_last_usec / USEC_PER_SEC, st->counter, st->counter_done);
  840. microseconds = (usec_t)since_last_usec;
  841. #ifdef NETDATA_INTERNAL_CHECKS
  842. if(!discard_reason) discard_reason = "COLLECTION TIME TOO FAR IN THE PAST";
  843. #endif
  844. }
  845. #ifdef NETDATA_INTERNAL_CHECKS
  846. if(since_last_usec > 0 && (susec_t)microseconds < since_last_usec) {
  847. static __thread susec_t min_delta = USEC_PER_SEC * 3600, permanent_min_delta = 0;
  848. static __thread time_t last_t = 0;
  849. // the first time initialize it so that it will make the check later
  850. if(last_t == 0) last_t = now.tv_sec + 60;
  851. susec_t delta = since_last_usec - (susec_t)microseconds;
  852. if(delta < min_delta) min_delta = delta;
  853. if(now.tv_sec >= last_t + 60) {
  854. last_t = now.tv_sec;
  855. if(min_delta > permanent_min_delta) {
  856. info("MINIMUM MICROSECONDS DELTA of thread %d increased from %lld to %lld (+%lld)", gettid(), permanent_min_delta, min_delta, min_delta - permanent_min_delta);
  857. permanent_min_delta = min_delta;
  858. }
  859. min_delta = USEC_PER_SEC * 3600;
  860. }
  861. }
  862. #endif
  863. }
  864. #ifdef NETDATA_INTERNAL_CHECKS
  865. debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
  866. rrdset_debug(st, "NEXT: %llu microseconds", microseconds);
  867. if(discarded && discarded != microseconds)
  868. info("host '%s', chart '%s': discarded data collection time of %llu usec, replaced with %llu usec, reason: '%s'", st->rrdhost->hostname, st->id, discarded, microseconds, discard_reason?discard_reason:"UNDEFINED");
  869. #endif
  870. st->usec_since_last_update = microseconds;
  871. }
  872. // ----------------------------------------------------------------------------
  873. // RRDSET - process the collected values for all dimensions of a chart
  874. static inline usec_t rrdset_init_last_collected_time(RRDSET *st) {
  875. now_realtime_timeval(&st->last_collected_time);
  876. last_collected_time_align(st);
  877. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  878. #ifdef NETDATA_INTERNAL_CHECKS
  879. rrdset_debug(st, "initialized last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  880. #endif
  881. return last_collect_ut;
  882. }
  883. static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
  884. usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  885. usec_t ut = last_collect_ut + st->usec_since_last_update;
  886. st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
  887. st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
  888. #ifdef NETDATA_INTERNAL_CHECKS
  889. rrdset_debug(st, "updated last collected time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_collect_ut / USEC_PER_SEC);
  890. #endif
  891. return last_collect_ut;
  892. }
  893. static inline usec_t rrdset_init_last_updated_time(RRDSET *st) {
  894. // copy the last collected time to last updated time
  895. st->last_updated.tv_sec = st->last_collected_time.tv_sec;
  896. st->last_updated.tv_usec = st->last_collected_time.tv_usec;
  897. if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
  898. st->last_updated.tv_sec -= st->update_every;
  899. last_updated_time_align(st);
  900. usec_t last_updated_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  901. #ifdef NETDATA_INTERNAL_CHECKS
  902. rrdset_debug(st, "initialized last updated time to %0.3" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)last_updated_ut / USEC_PER_SEC);
  903. #endif
  904. return last_updated_ut;
  905. }
  906. static inline size_t rrdset_done_interpolate(
  907. RRDSET *st
  908. , usec_t update_every_ut
  909. , usec_t last_stored_ut
  910. , usec_t next_store_ut
  911. , usec_t last_collect_ut
  912. , usec_t now_collect_ut
  913. , char store_this_entry
  914. , uint32_t storage_flags
  915. ) {
  916. RRDDIM *rd;
  917. size_t stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
  918. usec_t first_ut = last_stored_ut, last_ut = 0;
  919. (void)first_ut;
  920. ssize_t iterations = (ssize_t)((now_collect_ut - last_stored_ut) / (update_every_ut));
  921. if((now_collect_ut % (update_every_ut)) == 0) iterations++;
  922. size_t counter = st->counter;
  923. long current_entry = st->current_entry;
  924. for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
  925. #ifdef NETDATA_INTERNAL_CHECKS
  926. if(iterations < 0) { error("INTERNAL CHECK: %s: iterations calculation wrapped! first_ut = %llu, last_stored_ut = %llu, next_store_ut = %llu, now_collect_ut = %llu", st->name, first_ut, last_stored_ut, next_store_ut, now_collect_ut); }
  927. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  928. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  929. #endif
  930. last_ut = next_store_ut;
  931. rrddim_foreach_read(rd, st) {
  932. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))
  933. continue;
  934. calculated_number new_value;
  935. switch(rd->algorithm) {
  936. case RRD_ALGORITHM_INCREMENTAL:
  937. new_value = (calculated_number)
  938. ( rd->calculated_value
  939. * (calculated_number)(next_store_ut - last_collect_ut)
  940. / (calculated_number)(now_collect_ut - last_collect_ut)
  941. );
  942. #ifdef NETDATA_INTERNAL_CHECKS
  943. rrdset_debug(st, "%s: CALC2 INC "
  944. CALCULATED_NUMBER_FORMAT " = "
  945. CALCULATED_NUMBER_FORMAT
  946. " * (%llu - %llu)"
  947. " / (%llu - %llu)"
  948. , rd->name
  949. , new_value
  950. , rd->calculated_value
  951. , next_store_ut, last_collect_ut
  952. , now_collect_ut, last_collect_ut
  953. );
  954. #endif
  955. rd->calculated_value -= new_value;
  956. new_value += rd->last_calculated_value;
  957. rd->last_calculated_value = 0;
  958. new_value /= (calculated_number)st->update_every;
  959. if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
  960. #ifdef NETDATA_INTERNAL_CHECKS
  961. rrdset_debug(st, "%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
  962. rd->name
  963. , (calculated_number)(next_store_ut - last_stored_ut)
  964. );
  965. #endif
  966. new_value = new_value * (calculated_number)(st->update_every * USEC_PER_SEC) / (calculated_number)(next_store_ut - last_stored_ut);
  967. }
  968. break;
  969. case RRD_ALGORITHM_ABSOLUTE:
  970. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  971. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  972. default:
  973. if(iterations == 1) {
  974. // this is the last iteration
  975. // do not interpolate
  976. // just show the calculated value
  977. new_value = rd->calculated_value;
  978. }
  979. else {
  980. // we have missed an update
  981. // interpolate in the middle values
  982. new_value = (calculated_number)
  983. ( ( (rd->calculated_value - rd->last_calculated_value)
  984. * (calculated_number)(next_store_ut - last_collect_ut)
  985. / (calculated_number)(now_collect_ut - last_collect_ut)
  986. )
  987. + rd->last_calculated_value
  988. );
  989. #ifdef NETDATA_INTERNAL_CHECKS
  990. rrdset_debug(st, "%s: CALC2 DEF "
  991. CALCULATED_NUMBER_FORMAT " = ((("
  992. "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
  993. " * %llu"
  994. " / %llu) + " CALCULATED_NUMBER_FORMAT
  995. , rd->name
  996. , new_value
  997. , rd->calculated_value, rd->last_calculated_value
  998. , (next_store_ut - first_ut)
  999. , (now_collect_ut - first_ut), rd->last_calculated_value
  1000. );
  1001. #endif
  1002. }
  1003. break;
  1004. }
  1005. if(unlikely(!store_this_entry)) {
  1006. rd->state->collect_ops.store_metric(rd, next_store_ut, SN_EMPTY_SLOT); //pack_storage_number(0, SN_NOT_EXISTS)
  1007. // rd->values[current_entry] = SN_EMPTY_SLOT; //pack_storage_number(0, SN_NOT_EXISTS);
  1008. continue;
  1009. }
  1010. if(likely(rd->updated && rd->collections_counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
  1011. rd->state->collect_ops.store_metric(rd, next_store_ut, pack_storage_number(new_value, storage_flags));
  1012. // rd->values[current_entry] = pack_storage_number(new_value, storage_flags );
  1013. rd->last_stored_value = new_value;
  1014. #ifdef NETDATA_INTERNAL_CHECKS
  1015. rrdset_debug(st, "%s: STORE[%ld] "
  1016. CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
  1017. , rd->name
  1018. , current_entry
  1019. , unpack_storage_number(rd->values[current_entry]), new_value
  1020. );
  1021. #endif
  1022. }
  1023. else {
  1024. #ifdef NETDATA_INTERNAL_CHECKS
  1025. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING "
  1026. , rd->name
  1027. , current_entry
  1028. );
  1029. #endif
  1030. // rd->values[current_entry] = SN_EMPTY_SLOT; // pack_storage_number(0, SN_NOT_EXISTS);
  1031. rd->state->collect_ops.store_metric(rd, next_store_ut, SN_EMPTY_SLOT); //pack_storage_number(0, SN_NOT_EXISTS)
  1032. rd->last_stored_value = NAN;
  1033. }
  1034. stored_entries++;
  1035. #ifdef NETDATA_INTERNAL_CHECKS
  1036. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
  1037. calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
  1038. calculated_number t2 = unpack_storage_number(rd->values[current_entry]);
  1039. calculated_number accuracy = accuracy_loss(t1, t2);
  1040. debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
  1041. , st->id, rd->name
  1042. , current_entry
  1043. , t2
  1044. , get_storage_number_flags(rd->values[current_entry])
  1045. , t1
  1046. , accuracy
  1047. , (accuracy > ACCURACY_LOSS_ACCEPTED_PERCENT) ? " **TOO BIG** " : ""
  1048. );
  1049. rd->collected_volume += t1;
  1050. rd->stored_volume += t2;
  1051. accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
  1052. debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
  1053. , st->id, rd->name
  1054. , current_entry
  1055. , rd->stored_volume
  1056. , rd->collected_volume
  1057. , accuracy
  1058. , (accuracy > ACCURACY_LOSS_ACCEPTED_PERCENT) ? " **TOO BIG** " : ""
  1059. );
  1060. }
  1061. #endif
  1062. }
  1063. // reset the storage flags for the next point, if any;
  1064. storage_flags = SN_EXISTS;
  1065. st->counter = ++counter;
  1066. st->current_entry = current_entry = ((current_entry + 1) >= st->entries) ? 0 : current_entry + 1;
  1067. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  1068. st->last_updated.tv_usec = 0;
  1069. last_stored_ut = next_store_ut;
  1070. }
  1071. /*
  1072. st->counter = counter;
  1073. st->current_entry = current_entry;
  1074. if(likely(last_ut)) {
  1075. st->last_updated.tv_sec = (time_t) (last_ut / USEC_PER_SEC);
  1076. st->last_updated.tv_usec = 0;
  1077. }
  1078. */
  1079. return stored_entries;
  1080. }
  1081. static inline void rrdset_done_fill_the_gap(RRDSET *st) {
  1082. usec_t update_every_ut = st->update_every * USEC_PER_SEC;
  1083. usec_t now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  1084. long c = 0, entries = st->entries;
  1085. RRDDIM *rd;
  1086. rrddim_foreach_read(rd, st) {
  1087. usec_t next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1088. long current_entry = st->current_entry;
  1089. for(c = 0; c < entries && next_store_ut <= now_collect_ut ; next_store_ut += update_every_ut, c++) {
  1090. rd->values[current_entry] = SN_EMPTY_SLOT;
  1091. current_entry = ((current_entry + 1) >= entries) ? 0 : current_entry + 1;
  1092. #ifdef NETDATA_INTERNAL_CHECKS
  1093. rrdset_debug(st, "%s: STORE[%ld] = NON EXISTING (FILLED THE GAP)", rd->name, current_entry);
  1094. #endif
  1095. }
  1096. }
  1097. if(c > 0) {
  1098. c--;
  1099. st->last_updated.tv_sec += c * st->update_every;
  1100. st->current_entry += c;
  1101. if(st->current_entry >= st->entries)
  1102. st->current_entry -= st->entries;
  1103. }
  1104. }
  1105. void rrdset_done(RRDSET *st) {
  1106. if(unlikely(netdata_exit)) return;
  1107. debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
  1108. RRDDIM *rd;
  1109. char
  1110. store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
  1111. first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
  1112. usec_t
  1113. last_collect_ut = 0, // the timestamp in microseconds, of the last collected value
  1114. now_collect_ut = 0, // the timestamp in microseconds, of this collected value (this is NOW)
  1115. last_stored_ut = 0, // the timestamp in microseconds, of the last stored entry in the db
  1116. next_store_ut = 0, // the timestamp in microseconds, of the next entry to store in the db
  1117. update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
  1118. netdata_thread_disable_cancelability();
  1119. // a read lock is OK here
  1120. rrdset_rdlock(st);
  1121. #ifdef ENABLE_ACLK
  1122. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_ACLK))) {
  1123. rrdset_flag_clear(st, RRDSET_FLAG_ACLK);
  1124. aclk_update_chart(st->rrdhost, st->id, ACLK_CMD_CHART);
  1125. }
  1126. #endif
  1127. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))) {
  1128. error("Chart '%s' has the OBSOLETE flag set, but it is collected.", st->id);
  1129. rrdset_isnot_obsolete(st);
  1130. }
  1131. // check if the chart has a long time to be updated
  1132. if(unlikely(st->usec_since_last_update > st->entries * update_every_ut &&
  1133. st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && st->rrd_memory_mode != RRD_MEMORY_MODE_NONE)) {
  1134. info("host '%s', chart %s: took too long to be updated (counter #%zu, update #%zu, %0.3" LONG_DOUBLE_MODIFIER " secs). Resetting it.", st->rrdhost->hostname, st->name, st->counter, st->counter_done, (LONG_DOUBLE)st->usec_since_last_update / USEC_PER_SEC);
  1135. rrdset_reset(st);
  1136. st->usec_since_last_update = update_every_ut;
  1137. store_this_entry = 0;
  1138. first_entry = 1;
  1139. }
  1140. #ifdef NETDATA_INTERNAL_CHECKS
  1141. rrdset_debug(st, "microseconds since last update: %llu", st->usec_since_last_update);
  1142. #endif
  1143. // set last_collected_time
  1144. if(unlikely(!st->last_collected_time.tv_sec)) {
  1145. // it is the first entry
  1146. // set the last_collected_time to now
  1147. last_collect_ut = rrdset_init_last_collected_time(st) - update_every_ut;
  1148. // the first entry should not be stored
  1149. store_this_entry = 0;
  1150. first_entry = 1;
  1151. }
  1152. else {
  1153. // it is not the first entry
  1154. // calculate the proper last_collected_time, using usec_since_last_update
  1155. last_collect_ut = rrdset_update_last_collected_time(st);
  1156. }
  1157. if (unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
  1158. goto after_first_database_work;
  1159. }
  1160. // if this set has not been updated in the past
  1161. // we fake the last_update time to be = now - usec_since_last_update
  1162. if(unlikely(!st->last_updated.tv_sec)) {
  1163. // it has never been updated before
  1164. // set a fake last_updated, in the past using usec_since_last_update
  1165. rrdset_init_last_updated_time(st);
  1166. // the first entry should not be stored
  1167. store_this_entry = 0;
  1168. first_entry = 1;
  1169. }
  1170. // check if we will re-write the entire data set
  1171. if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut &&
  1172. st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
  1173. info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Resetting it. Will not store the next entry.", st->name, st->last_updated.tv_sec, st->last_updated.tv_usec, st->last_collected_time.tv_sec, st->last_collected_time.tv_usec);
  1174. rrdset_reset(st);
  1175. rrdset_init_last_updated_time(st);
  1176. st->usec_since_last_update = update_every_ut;
  1177. // the first entry should not be stored
  1178. store_this_entry = 0;
  1179. first_entry = 1;
  1180. }
  1181. #ifdef ENABLE_DBENGINE
  1182. // check if we will re-write the entire page
  1183. if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE &&
  1184. dt_usec(&st->last_collected_time, &st->last_updated) > (RRDENG_BLOCK_SIZE / sizeof(storage_number)) * update_every_ut)) {
  1185. info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Resetting it. Will not store the next entry.", st->name, st->last_updated.tv_sec, st->last_updated.tv_usec, st->last_collected_time.tv_sec, st->last_collected_time.tv_usec);
  1186. rrdset_reset(st);
  1187. rrdset_init_last_updated_time(st);
  1188. st->usec_since_last_update = update_every_ut;
  1189. // the first entry should not be stored
  1190. store_this_entry = 0;
  1191. first_entry = 1;
  1192. }
  1193. #endif
  1194. // these are the 3 variables that will help us in interpolation
  1195. // last_stored_ut = the last time we added a value to the storage
  1196. // now_collect_ut = the time the current value has been collected
  1197. // next_store_ut = the time of the next interpolation point
  1198. now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
  1199. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1200. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1201. if(unlikely(!st->counter_done)) {
  1202. // if we have not collected metrics this session (st->counter_done == 0)
  1203. // and we have collected metrics for this chart in the past (st->counter != 0)
  1204. // fill the gap (the chart has been just loaded from disk)
  1205. if(unlikely(st->counter) && st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) {
  1206. rrdset_done_fill_the_gap(st);
  1207. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1208. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1209. }
  1210. if (st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  1211. // set a fake last_updated to jump to current time
  1212. rrdset_init_last_updated_time(st);
  1213. last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
  1214. next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
  1215. }
  1216. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))) {
  1217. store_this_entry = 1;
  1218. last_collect_ut = next_store_ut - update_every_ut;
  1219. #ifdef NETDATA_INTERNAL_CHECKS
  1220. rrdset_debug(st, "Fixed first entry.");
  1221. #endif
  1222. }
  1223. else {
  1224. store_this_entry = 0;
  1225. #ifdef NETDATA_INTERNAL_CHECKS
  1226. rrdset_debug(st, "Will not store the next entry.");
  1227. #endif
  1228. }
  1229. }
  1230. after_first_database_work:
  1231. st->counter_done++;
  1232. if(unlikely(st->rrdhost->rrdpush_send_enabled))
  1233. rrdset_done_push(st);
  1234. if (unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
  1235. goto after_second_database_work;
  1236. }
  1237. #ifdef NETDATA_INTERNAL_CHECKS
  1238. rrdset_debug(st, "last_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (last collection time)", (LONG_DOUBLE)last_collect_ut/USEC_PER_SEC);
  1239. rrdset_debug(st, "now_collect_ut = %0.3" LONG_DOUBLE_MODIFIER " (current collection time)", (LONG_DOUBLE)now_collect_ut/USEC_PER_SEC);
  1240. rrdset_debug(st, "last_stored_ut = %0.3" LONG_DOUBLE_MODIFIER " (last updated time)", (LONG_DOUBLE)last_stored_ut/USEC_PER_SEC);
  1241. rrdset_debug(st, "next_store_ut = %0.3" LONG_DOUBLE_MODIFIER " (next interpolation point)", (LONG_DOUBLE)next_store_ut/USEC_PER_SEC);
  1242. #endif
  1243. // calculate totals and count the dimensions
  1244. int dimensions = 0;
  1245. st->collected_total = 0;
  1246. rrddim_foreach_read(rd, st) {
  1247. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))
  1248. continue;
  1249. dimensions++;
  1250. if(likely(rd->updated))
  1251. st->collected_total += rd->collected_value;
  1252. }
  1253. uint32_t storage_flags = SN_EXISTS;
  1254. // process all dimensions to calculate their values
  1255. // based on the collected figures only
  1256. // at this stage we do not interpolate anything
  1257. rrddim_foreach_read(rd, st) {
  1258. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))
  1259. continue;
  1260. if(unlikely(!rd->updated)) {
  1261. rd->calculated_value = 0;
  1262. continue;
  1263. }
  1264. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE))) {
  1265. error("Dimension %s in chart '%s' has the OBSOLETE flag set, but it is collected.", rd->name, st->id);
  1266. rrddim_isnot_obsolete(st, rd);
  1267. }
  1268. #ifdef NETDATA_INTERNAL_CHECKS
  1269. rrdset_debug(st, "%s: START "
  1270. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1271. " collected_value = " COLLECTED_NUMBER_FORMAT
  1272. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1273. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1274. , rd->name
  1275. , rd->last_collected_value
  1276. , rd->collected_value
  1277. , rd->last_calculated_value
  1278. , rd->calculated_value
  1279. );
  1280. #endif
  1281. switch(rd->algorithm) {
  1282. case RRD_ALGORITHM_ABSOLUTE:
  1283. rd->calculated_value = (calculated_number)rd->collected_value
  1284. * (calculated_number)rd->multiplier
  1285. / (calculated_number)rd->divisor;
  1286. #ifdef NETDATA_INTERNAL_CHECKS
  1287. rrdset_debug(st, "%s: CALC ABS/ABS-NO-IN "
  1288. CALCULATED_NUMBER_FORMAT " = "
  1289. COLLECTED_NUMBER_FORMAT
  1290. " * " CALCULATED_NUMBER_FORMAT
  1291. " / " CALCULATED_NUMBER_FORMAT
  1292. , rd->name
  1293. , rd->calculated_value
  1294. , rd->collected_value
  1295. , (calculated_number)rd->multiplier
  1296. , (calculated_number)rd->divisor
  1297. );
  1298. #endif
  1299. break;
  1300. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1301. if(unlikely(!st->collected_total))
  1302. rd->calculated_value = 0;
  1303. else
  1304. // the percentage of the current value
  1305. // over the total of all dimensions
  1306. rd->calculated_value =
  1307. (calculated_number)100
  1308. * (calculated_number)rd->collected_value
  1309. / (calculated_number)st->collected_total;
  1310. #ifdef NETDATA_INTERNAL_CHECKS
  1311. rrdset_debug(st, "%s: CALC PCENT-ROW "
  1312. CALCULATED_NUMBER_FORMAT " = 100"
  1313. " * " COLLECTED_NUMBER_FORMAT
  1314. " / " COLLECTED_NUMBER_FORMAT
  1315. , rd->name
  1316. , rd->calculated_value
  1317. , rd->collected_value
  1318. , st->collected_total
  1319. );
  1320. #endif
  1321. break;
  1322. case RRD_ALGORITHM_INCREMENTAL:
  1323. if(unlikely(rd->collections_counter <= 1)) {
  1324. rd->calculated_value = 0;
  1325. continue;
  1326. }
  1327. // If the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1328. // to reset the calculation (it will give zero as the calculation for this second).
  1329. // It is imperative to set the comparison to uint64_t since type collected_number is signed and
  1330. // produces wrong results as far as incremental counters are concerned.
  1331. if(unlikely((uint64_t)rd->last_collected_value > (uint64_t)rd->collected_value)) {
  1332. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1333. , st->name, rd->name
  1334. , rd->last_collected_value
  1335. , rd->collected_value);
  1336. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1337. storage_flags = SN_EXISTS_RESET;
  1338. uint64_t last = (uint64_t)rd->last_collected_value;
  1339. uint64_t new = (uint64_t)rd->collected_value;
  1340. uint64_t max = (uint64_t)rd->collected_value_max;
  1341. uint64_t cap = 0;
  1342. // Signed values are handled by exploiting two's complement which will produce positive deltas
  1343. if (max > 0x00000000FFFFFFFFULL)
  1344. cap = 0xFFFFFFFFFFFFFFFFULL; // handles signed and unsigned 64-bit counters
  1345. else
  1346. cap = 0x00000000FFFFFFFFULL; // handles signed and unsigned 32-bit counters
  1347. uint64_t delta = cap - last + new;
  1348. uint64_t max_acceptable_rate = (cap / 100) * MAX_INCREMENTAL_PERCENT_RATE;
  1349. // If the delta is less than the maximum acceptable rate and the previous value was near the cap
  1350. // then this is an overflow. There can be false positives such that a reset is detected as an
  1351. // overflow.
  1352. // TODO: remember recent history of rates and compare with current rate to reduce this chance.
  1353. if (delta < max_acceptable_rate) {
  1354. rd->calculated_value +=
  1355. (calculated_number) delta
  1356. * (calculated_number) rd->multiplier
  1357. / (calculated_number) rd->divisor;
  1358. } else {
  1359. // This is a reset. Any overflow with a rate greater than MAX_INCREMENTAL_PERCENT_RATE will also
  1360. // be detected as a reset instead.
  1361. rd->calculated_value += (calculated_number)0;
  1362. }
  1363. }
  1364. else {
  1365. rd->calculated_value +=
  1366. (calculated_number) (rd->collected_value - rd->last_collected_value)
  1367. * (calculated_number) rd->multiplier
  1368. / (calculated_number) rd->divisor;
  1369. }
  1370. #ifdef NETDATA_INTERNAL_CHECKS
  1371. rrdset_debug(st, "%s: CALC INC PRE "
  1372. CALCULATED_NUMBER_FORMAT " = ("
  1373. COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
  1374. ")"
  1375. " * " CALCULATED_NUMBER_FORMAT
  1376. " / " CALCULATED_NUMBER_FORMAT
  1377. , rd->name
  1378. , rd->calculated_value
  1379. , rd->collected_value, rd->last_collected_value
  1380. , (calculated_number)rd->multiplier
  1381. , (calculated_number)rd->divisor
  1382. );
  1383. #endif
  1384. break;
  1385. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1386. if(unlikely(rd->collections_counter <= 1)) {
  1387. rd->calculated_value = 0;
  1388. continue;
  1389. }
  1390. // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
  1391. // to reset the calculation (it will give zero as the calculation for this second)
  1392. if(unlikely(rd->last_collected_value > rd->collected_value)) {
  1393. debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
  1394. , st->name, rd->name
  1395. , rd->last_collected_value
  1396. , rd->collected_value
  1397. );
  1398. if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
  1399. storage_flags = SN_EXISTS_RESET;
  1400. rd->last_collected_value = rd->collected_value;
  1401. }
  1402. // the percentage of the current increment
  1403. // over the increment of all dimensions together
  1404. if(unlikely(st->collected_total == st->last_collected_total))
  1405. rd->calculated_value = 0;
  1406. else
  1407. rd->calculated_value =
  1408. (calculated_number)100
  1409. * (calculated_number)(rd->collected_value - rd->last_collected_value)
  1410. / (calculated_number)(st->collected_total - st->last_collected_total);
  1411. #ifdef NETDATA_INTERNAL_CHECKS
  1412. rrdset_debug(st, "%s: CALC PCENT-DIFF "
  1413. CALCULATED_NUMBER_FORMAT " = 100"
  1414. " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1415. " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
  1416. , rd->name
  1417. , rd->calculated_value
  1418. , rd->collected_value, rd->last_collected_value
  1419. , st->collected_total, st->last_collected_total
  1420. );
  1421. #endif
  1422. break;
  1423. default:
  1424. // make the default zero, to make sure
  1425. // it gets noticed when we add new types
  1426. rd->calculated_value = 0;
  1427. #ifdef NETDATA_INTERNAL_CHECKS
  1428. rrdset_debug(st, "%s: CALC "
  1429. CALCULATED_NUMBER_FORMAT " = 0"
  1430. , rd->name
  1431. , rd->calculated_value
  1432. );
  1433. #endif
  1434. break;
  1435. }
  1436. #ifdef NETDATA_INTERNAL_CHECKS
  1437. rrdset_debug(st, "%s: PHASE2 "
  1438. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1439. " collected_value = " COLLECTED_NUMBER_FORMAT
  1440. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1441. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1442. , rd->name
  1443. , rd->last_collected_value
  1444. , rd->collected_value
  1445. , rd->last_calculated_value
  1446. , rd->calculated_value
  1447. );
  1448. #endif
  1449. }
  1450. // at this point we have all the calculated values ready
  1451. // it is now time to interpolate values on a second boundary
  1452. #ifdef NETDATA_INTERNAL_CHECKS
  1453. if(unlikely(now_collect_ut < next_store_ut)) {
  1454. // this is collected in the same interpolation point
  1455. rrdset_debug(st, "THIS IS IN THE SAME INTERPOLATION POINT");
  1456. info("INTERNAL CHECK: host '%s', chart '%s' is collected in the same interpolation point: short by %llu microseconds", st->rrdhost->hostname, st->name, next_store_ut - now_collect_ut);
  1457. }
  1458. #endif
  1459. rrdset_done_interpolate(st
  1460. , update_every_ut
  1461. , last_stored_ut
  1462. , next_store_ut
  1463. , last_collect_ut
  1464. , now_collect_ut
  1465. , store_this_entry
  1466. , storage_flags
  1467. );
  1468. after_second_database_work:
  1469. st->last_collected_total = st->collected_total;
  1470. rrddim_foreach_read(rd, st) {
  1471. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED))
  1472. continue;
  1473. if(unlikely(!rd->updated))
  1474. continue;
  1475. #ifdef NETDATA_INTERNAL_CHECKS
  1476. rrdset_debug(st, "%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", rd->name, rd->last_collected_value, rd->collected_value);
  1477. #endif
  1478. rd->last_collected_value = rd->collected_value;
  1479. switch(rd->algorithm) {
  1480. case RRD_ALGORITHM_INCREMENTAL:
  1481. if(unlikely(!first_entry)) {
  1482. #ifdef NETDATA_INTERNAL_CHECKS
  1483. rrdset_debug(st, "%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", rd->name, rd->last_calculated_value + rd->calculated_value, rd->calculated_value);
  1484. #endif
  1485. rd->last_calculated_value += rd->calculated_value;
  1486. }
  1487. else {
  1488. #ifdef NETDATA_INTERNAL_CHECKS
  1489. rrdset_debug(st, "THIS IS THE FIRST POINT");
  1490. #endif
  1491. }
  1492. break;
  1493. case RRD_ALGORITHM_ABSOLUTE:
  1494. case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
  1495. case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
  1496. #ifdef NETDATA_INTERNAL_CHECKS
  1497. rrdset_debug(st, "%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", rd->name, rd->last_calculated_value, rd->calculated_value);
  1498. #endif
  1499. rd->last_calculated_value = rd->calculated_value;
  1500. break;
  1501. }
  1502. rd->calculated_value = 0;
  1503. rd->collected_value = 0;
  1504. rd->updated = 0;
  1505. #ifdef NETDATA_INTERNAL_CHECKS
  1506. rrdset_debug(st, "%s: END "
  1507. " last_collected_value = " COLLECTED_NUMBER_FORMAT
  1508. " collected_value = " COLLECTED_NUMBER_FORMAT
  1509. " last_calculated_value = " CALCULATED_NUMBER_FORMAT
  1510. " calculated_value = " CALCULATED_NUMBER_FORMAT
  1511. , rd->name
  1512. , rd->last_collected_value
  1513. , rd->collected_value
  1514. , rd->last_calculated_value
  1515. , rd->calculated_value
  1516. );
  1517. #endif
  1518. }
  1519. // ALL DONE ABOUT THE DATA UPDATE
  1520. // --------------------------------------------------------------------
  1521. // find if there are any obsolete dimensions
  1522. time_t now = now_realtime_sec();
  1523. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))) {
  1524. rrddim_foreach_read(rd, st)
  1525. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)))
  1526. break;
  1527. if(unlikely(rd)) {
  1528. RRDDIM *last;
  1529. // there is a dimension to free
  1530. // upgrade our read lock to a write lock
  1531. rrdset_unlock(st);
  1532. rrdset_wrlock(st);
  1533. for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
  1534. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE) && (rd->last_collected_time.tv_sec + rrdset_free_obsolete_time < now))) {
  1535. info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
  1536. if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || rd->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
  1537. info("Deleting dimension file '%s'.", rd->cache_filename);
  1538. if(unlikely(unlink(rd->cache_filename) == -1))
  1539. error("Cannot delete dimension file '%s'", rd->cache_filename);
  1540. }
  1541. #ifdef ENABLE_DBENGINE
  1542. if (rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  1543. rrddim_flag_set(rd, RRDDIM_FLAG_ARCHIVED);
  1544. while(rd->variables)
  1545. rrddimvar_free(rd->variables);
  1546. rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
  1547. /* only a collector can mark a chart as obsolete, so we must remove the reference */
  1548. uint8_t can_delete_metric = rd->state->collect_ops.finalize(rd);
  1549. if (can_delete_metric) {
  1550. /* This metric has no data and no references */
  1551. delete_dimension_uuid(rd->state->metric_uuid);
  1552. } else {
  1553. /* Do not delete this dimension */
  1554. last = rd;
  1555. rd = rd->next;
  1556. continue;
  1557. }
  1558. }
  1559. #endif
  1560. if(unlikely(!last)) {
  1561. rrddim_free(st, rd);
  1562. rd = st->dimensions;
  1563. continue;
  1564. }
  1565. else {
  1566. rrddim_free(st, rd);
  1567. rd = last->next;
  1568. continue;
  1569. }
  1570. }
  1571. last = rd;
  1572. rd = rd->next;
  1573. }
  1574. }
  1575. else {
  1576. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS);
  1577. }
  1578. }
  1579. rrdset_unlock(st);
  1580. netdata_thread_enable_cancelability();
  1581. }
  1582. void rrdset_add_label_to_new_list(RRDSET *st, char *key, char *value, LABEL_SOURCE source)
  1583. {
  1584. st->state->new_labels = add_label_to_list(st->state->new_labels, key, value, source);
  1585. }
  1586. void rrdset_finalize_labels(RRDSET *st)
  1587. {
  1588. struct label *new_labels = st->state->new_labels;
  1589. struct label_index *labels = &st->state->labels;
  1590. if (!labels->head) {
  1591. labels->head = new_labels;
  1592. } else {
  1593. replace_label_list(labels, new_labels);
  1594. }
  1595. netdata_rwlock_wrlock(&labels->labels_rwlock);
  1596. struct label *lbl = labels->head;
  1597. while (lbl) {
  1598. sql_store_chart_label(st->chart_uuid, (int)lbl->label_source, lbl->key, lbl->value);
  1599. lbl = lbl->next;
  1600. }
  1601. netdata_rwlock_unlock(&labels->labels_rwlock);
  1602. st->state->new_labels = NULL;
  1603. }
  1604. void rrdset_update_labels(RRDSET *st, struct label *labels)
  1605. {
  1606. if (!labels)
  1607. return;
  1608. update_label_list(&st->state->new_labels, labels);
  1609. rrdset_finalize_labels(st);
  1610. }
  1611. int rrdset_contains_label_keylist(RRDSET *st, char *keylist)
  1612. {
  1613. struct label_index *labels = &st->state->labels;
  1614. int ret;
  1615. if (!labels->head)
  1616. return 0;
  1617. netdata_rwlock_rdlock(&labels->labels_rwlock);
  1618. ret = label_list_contains_keylist(labels->head, keylist);
  1619. netdata_rwlock_unlock(&labels->labels_rwlock);
  1620. return ret;
  1621. }
  1622. struct label *rrdset_lookup_label_key(RRDSET *st, char *key, uint32_t key_hash)
  1623. {
  1624. struct label_index *labels = &st->state->labels;
  1625. struct label *ret = NULL;
  1626. if (labels->head) {
  1627. netdata_rwlock_rdlock(&labels->labels_rwlock);
  1628. ret = label_list_lookup_key(labels->head, key, key_hash);
  1629. netdata_rwlock_unlock(&labels->labels_rwlock);
  1630. }
  1631. return ret;
  1632. }