rrdset.c 83 KB

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