rrdset.c 79 KB

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