]> arthur.barton.de Git - netdata.git/blob - src/rrdset.c
RRDHOSTs free all their memory (all substructures) when they are deallocated
[netdata.git] / src / rrdset.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
5
6 // ----------------------------------------------------------------------------
7 // RRDSET index
8
9 int rrdset_compare(void* a, void* b) {
10     if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
11     else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
12     else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
13 }
14
15 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
16     RRDSET tmp;
17     strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
18     tmp.hash = (hash)?hash:simple_hash(tmp.id);
19
20     return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
21 }
22
23 // ----------------------------------------------------------------------------
24 // RRDSET name index
25
26 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
27
28 int rrdset_compare_name(void* a, void* b) {
29     RRDSET *A = rrdset_from_avlname(a);
30     RRDSET *B = rrdset_from_avlname(b);
31
32     // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
33
34     if(A->hash_name < B->hash_name) return -1;
35     else if(A->hash_name > B->hash_name) return 1;
36     else return strcmp(A->name, B->name);
37 }
38
39 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
40     void *result;
41     // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
42     result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
43     if(result) return rrdset_from_avlname(result);
44     return NULL;
45 }
46
47 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
48     void *result;
49     // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
50     result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
51     if(result) return rrdset_from_avlname(result);
52     return NULL;
53 }
54
55
56 // ----------------------------------------------------------------------------
57 // RRDSET - find charts
58
59 static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
60     void *result = NULL;
61     RRDSET tmp;
62     tmp.name = name;
63     tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
64
65     // fprintf(stderr, "SEARCHING: %s\n", name);
66     result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
67     if(result) {
68         RRDSET *st = rrdset_from_avlname(result);
69         if(strcmp(st->magic, RRDSET_MAGIC))
70             error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
71
72         // fprintf(stderr, "FOUND: %s\n", name);
73         return rrdset_from_avlname(result);
74     }
75     // fprintf(stderr, "NOT FOUND: %s\n", name);
76     return NULL;
77 }
78
79 inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
80     debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, host->hostname);
81     RRDSET *st = rrdset_index_find(host, id, 0);
82     return(st);
83 }
84
85 inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
86     debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, host->hostname);
87
88     char buf[RRD_ID_LENGTH_MAX + 1];
89     strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
90     strcat(buf, ".");
91     int len = (int) strlen(buf);
92     strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
93
94     return(rrdset_find(host, buf));
95 }
96
97 inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
98     debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, host->hostname);
99     RRDSET *st = rrdset_index_find_name(host, name, 0);
100     return(st);
101 }
102
103 // ----------------------------------------------------------------------------
104 // RRDSET - rename charts
105
106 char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
107     char c, *p = to;
108
109     while (length-- && (c = *from++)) {
110         if(c != '.' && !isalnum(c))
111             c = '_';
112
113         *p++ = c;
114     }
115
116     *p = '\0';
117
118     return to;
119 }
120
121 void rrdset_set_name(RRDSET *st, const char *name) {
122     if(unlikely(st->name && !strcmp(st->name, name)))
123         return;
124
125     debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
126
127     char b[CONFIG_MAX_VALUE + 1];
128     char n[RRD_ID_LENGTH_MAX + 1];
129
130     snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
131     rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
132
133     if(st->name) {
134         rrdset_index_del_name(st->rrdhost, st);
135         st->name = config_set_default(st->config_section, "name", b);
136         st->hash_name = simple_hash(st->name);
137         rrdsetvar_rename_all(st);
138     }
139     else {
140         st->name = config_get(st->config_section, "name", b);
141         st->hash_name = simple_hash(st->name);
142     }
143
144     rrdset_wrlock(st);
145     RRDDIM *rd;
146     for(rd = st->dimensions; rd ;rd = rd->next)
147         rrddimvar_rename_all(rd);
148     rrdset_unlock(st);
149
150     if(unlikely(rrdset_index_add_name(st->rrdhost, st) != st))
151         error("RRDSET: INTERNAL ERROR: attempted to index duplicate chart name '%s'", st->name);
152 }
153
154
155 // ----------------------------------------------------------------------------
156 // RRDSET - reset a chart
157
158 void rrdset_reset(RRDSET *st) {
159     debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
160
161     st->last_collected_time.tv_sec = 0;
162     st->last_collected_time.tv_usec = 0;
163     st->last_updated.tv_sec = 0;
164     st->last_updated.tv_usec = 0;
165     st->current_entry = 0;
166     st->counter = 0;
167     st->counter_done = 0;
168
169     RRDDIM *rd;
170     for(rd = st->dimensions; rd ; rd = rd->next) {
171         rd->last_collected_time.tv_sec = 0;
172         rd->last_collected_time.tv_usec = 0;
173         rd->counter = 0;
174         memset(rd->values, 0, rd->entries * sizeof(storage_number));
175     }
176 }
177
178 // ----------------------------------------------------------------------------
179 // RRDSET - helpers for rrdset_create()
180
181 static inline long align_entries_to_pagesize(long entries) {
182     if(entries < 5) entries = 5;
183     if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
184
185 #ifdef NETDATA_LOG_ALLOCATIONS
186     long page = (size_t)sysconf(_SC_PAGESIZE);
187
188     long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
189     if(size % page) {
190         size -= (size % page);
191         size += page;
192
193         long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
194         return n;
195     }
196
197     return entries;
198 #else
199     return entries;
200 #endif
201 }
202
203 static inline void timeval_align(struct timeval *tv, int update_every) {
204     tv->tv_sec -= tv->tv_sec % update_every;
205     tv->tv_usec = 500000;
206 }
207
208 // ----------------------------------------------------------------------------
209 // RRDSET - free a chart
210
211 void rrdset_free(RRDSET *st) {
212     if(unlikely(!st)) return;
213
214     rrdhost_check_wrlock(st->rrdhost);  // make sure we have a write lock on the host
215     rrdset_wrlock(st);                  // lock this RRDSET
216
217     // ------------------------------------------------------------------------
218     // free its children structures
219
220     while(st->variables)  rrdsetvar_free(st->variables);
221     while(st->alarms)     rrdsetcalc_unlink(st->alarms);
222     while(st->dimensions) rrddim_free(st, st->dimensions);
223
224     rrdfamily_free(st->rrdhost, st->rrdfamily);
225
226     // ------------------------------------------------------------------------
227     // remove it from the indexes
228
229     if(unlikely(rrdset_index_del(st->rrdhost, st) != st))
230         error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
231
232     rrdset_index_del_name(st->rrdhost, st);
233
234     // ------------------------------------------------------------------------
235     // unlink it from the host
236
237     if(st == st->rrdhost->rrdset_root) {
238         st->rrdhost->rrdset_root = st->next;
239     }
240     else {
241         // find the previous one
242         RRDSET *s;
243         for(s = st->rrdhost->rrdset_root; s && s->next != st ; s = s->next) ;
244
245         // bypass it
246         if(s) s->next = st->next;
247         else error("Request to free RRDSET '%s': cannot find it under host '%s'", st->id, st->rrdhost->hostname);
248     }
249
250     rrdset_unlock(st);
251
252     // ------------------------------------------------------------------------
253     // free it
254
255     // free directly allocated members
256     freez(st->config_section);
257
258     if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
259         debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
260         munmap(st, st->memsize);
261     }
262     else
263         freez(st);
264 }
265
266 // ----------------------------------------------------------------------------
267 // RRDSET - create a chart
268
269 RRDSET *rrdset_create(RRDHOST *host, const char *type, const char *id, const char *name, const char *family
270                       , const char *context, const char *title, const char *units, long priority
271                       , int update_every, RRDSET_TYPE chart_type) {
272
273     if(!type || !type[0]) {
274         fatal("Cannot create rrd stats without a type.");
275         return NULL;
276     }
277
278     if(!id || !id[0]) {
279         fatal("Cannot create rrd stats without an id.");
280         return NULL;
281     }
282
283     // ------------------------------------------------------------------------
284     // check if it already exists
285
286     char fullid[RRD_ID_LENGTH_MAX + 1];
287     snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
288
289     RRDSET *st = rrdset_find(host, fullid);
290     if(st) {
291         debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
292         return st;
293     }
294
295     char fullfilename[FILENAME_MAX + 1];
296
297     // ------------------------------------------------------------------------
298     // compose the config_section for this chart
299
300     char config_section[RRD_ID_LENGTH_MAX + 1];
301     if(host == localhost)
302         strcpy(config_section, fullid);
303     else
304         snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
305
306     // ------------------------------------------------------------------------
307     // get the options from the config, we need to create it
308
309     long rentries = config_get_number(config_section, "history", host->rrd_history_entries);
310     long entries = align_entries_to_pagesize(rentries);
311     if(entries != rentries) entries = config_set_number(config_section, "history", entries);
312
313     int enabled = config_get_boolean(config_section, "enabled", 1);
314     if(!enabled) entries = 5;
315
316     unsigned long size = sizeof(RRDSET);
317     char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
318
319     // ------------------------------------------------------------------------
320     // load it or allocate it
321
322     debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
323
324     snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
325     if(host->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
326         st = (RRDSET *) mymmap(fullfilename, size, ((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0);
327         if(st) {
328             memset(&st->avl, 0, sizeof(avl));
329             memset(&st->avlname, 0, sizeof(avl));
330             memset(&st->variables_root_index, 0, sizeof(avl_tree_lock));
331             memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
332             memset(&st->rrdset_rwlock, 0, sizeof(pthread_rwlock_t));
333
334             st->name = NULL;
335             st->type = NULL;
336             st->family = NULL;
337             st->context = NULL;
338             st->title = NULL;
339             st->units = NULL;
340             st->dimensions = NULL;
341             st->next = NULL;
342             st->variables = NULL;
343             st->alarms = NULL;
344             st->flags = 0x00000000;
345
346             if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
347                 errno = 0;
348                 info("Initializing file %s.", fullfilename);
349                 memset(st, 0, size);
350             }
351             else if(strcmp(st->id, fullid) != 0) {
352                 errno = 0;
353                 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
354                 // munmap(st, size);
355                 // st = NULL;
356                 memset(st, 0, size);
357             }
358             else if(st->memsize != size || st->entries != entries) {
359                 errno = 0;
360                 error("File %s does not have the desired size. Clearing it.", fullfilename);
361                 memset(st, 0, size);
362             }
363             else if(st->update_every != update_every) {
364                 errno = 0;
365                 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
366                 memset(st, 0, size);
367             }
368             else if((now_realtime_sec() - st->last_updated.tv_sec) > update_every * entries) {
369                 errno = 0;
370                 error("File %s is too old. Clearing it.", fullfilename);
371                 memset(st, 0, size);
372             }
373
374             // make sure the database is aligned
375             if(st->last_updated.tv_sec)
376                 timeval_align(&st->last_updated, update_every);
377
378             // make sure we have the right memory mode
379             // even if we cleared the memory
380             st->rrd_memory_mode = host->rrd_memory_mode;
381         }
382     }
383
384     if(unlikely(!st)) {
385         st = callocz(1, size);
386         st->rrd_memory_mode = RRD_MEMORY_MODE_RAM;
387     }
388
389     st->config_section = strdup(config_section);
390     st->rrdhost = host;
391     st->memsize = size;
392     st->entries = entries;
393     st->update_every = update_every;
394
395     if(st->current_entry >= st->entries) st->current_entry = 0;
396
397     strcpy(st->cache_filename, fullfilename);
398     strcpy(st->magic, RRDSET_MAGIC);
399
400     strcpy(st->id, fullid);
401     st->hash = simple_hash(st->id);
402
403     st->cache_dir = cache_dir;
404
405     st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
406     st->type       = config_get(st->config_section, "type", type);
407     st->family     = config_get(st->config_section, "family", family?family:st->type);
408     st->units      = config_get(st->config_section, "units", units?units:"");
409
410     st->context    = config_get(st->config_section, "context", context?context:st->id);
411     st->hash_context = simple_hash(st->context);
412
413     st->priority = config_get_number(st->config_section, "priority", priority);
414     if(enabled)
415         rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
416     else
417         rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
418
419     rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
420     rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
421
422     // if(!strcmp(st->id, "disk_util.dm-0")) {
423     //     st->debug = 1;
424     //     error("enabled debugging for '%s'", st->id);
425     // }
426     // else error("not enabled debugging for '%s'", st->id);
427
428     st->green = NAN;
429     st->red = NAN;
430
431     st->last_collected_time.tv_sec = 0;
432     st->last_collected_time.tv_usec = 0;
433     st->counter_done = 0;
434
435     st->gap_when_lost_iterations_above = (int) (
436             config_get_number(st->config_section, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
437
438     avl_init_lock(&st->dimensions_index, rrddim_compare);
439     avl_init_lock(&st->variables_root_index, rrdvar_compare);
440
441     pthread_rwlock_init(&st->rrdset_rwlock, NULL);
442     rrdhost_wrlock(host);
443
444     if(name && *name) rrdset_set_name(st, name);
445     else rrdset_set_name(st, id);
446
447     {
448         char varvalue[CONFIG_MAX_VALUE + 1];
449         char varvalue2[CONFIG_MAX_VALUE + 1];
450         snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
451         json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
452         st->title = config_get(st->config_section, "title", varvalue2);
453     }
454
455     st->rrdfamily = rrdfamily_create(host, st->family);
456
457     st->next = host->rrdset_root;
458     host->rrdset_root = st;
459
460     if(host->health_enabled) {
461         rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
462         rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
463         rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
464         rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
465         rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
466     }
467
468     if(unlikely(rrdset_index_add(host, st) != st))
469         error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
470
471     rrdsetcalc_link_matching(st);
472     rrdcalctemplate_link_matching(st);
473
474     rrdhost_unlock(host);
475
476     return(st);
477 }
478
479
480 // ----------------------------------------------------------------------------
481 // RRDSET - data collection iteration control
482
483 void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
484     if(unlikely(!st->last_collected_time.tv_sec || !microseconds)) {
485         // the first entry
486         microseconds = st->update_every * USEC_PER_SEC;
487     }
488     st->usec_since_last_update = microseconds;
489 }
490
491 void rrdset_next_usec(RRDSET *st, usec_t microseconds)
492 {
493     struct timeval now;
494     now_realtime_timeval(&now);
495
496     if(unlikely(!st->last_collected_time.tv_sec)) {
497         // the first entry
498         microseconds = st->update_every * USEC_PER_SEC;
499     }
500     else if(unlikely(!microseconds)) {
501         // no dt given by the plugin
502         microseconds = dt_usec(&now, &st->last_collected_time);
503     }
504     else {
505         // microseconds has the time since the last collection
506 #ifdef NETDATA_INTERNAL_CHECKS
507         usec_t now_usec = timeval_usec(&now);
508         usec_t last_usec = timeval_usec(&st->last_collected_time);
509 #endif
510         usec_t since_last_usec = dt_usec(&now, &st->last_collected_time);
511
512         // verify the microseconds given is good
513         if(unlikely(microseconds > since_last_usec)) {
514             debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
515
516 #ifdef NETDATA_INTERNAL_CHECKS
517             if(unlikely(last_usec + microseconds > now_usec + 1000))
518                 error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
519 #endif
520
521             microseconds = since_last_usec;
522         }
523         else if(unlikely(microseconds < since_last_usec * 0.8)) {
524             debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
525
526 #ifdef NETDATA_INTERNAL_CHECKS
527             error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
528 #endif
529             microseconds = since_last_usec;
530         }
531     }
532     debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
533
534     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
535         debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
536
537     st->usec_since_last_update = microseconds;
538 }
539
540
541 // ----------------------------------------------------------------------------
542 // RRDSET - process the collected values for all dimensions of a chart
543
544 usec_t rrdset_done(RRDSET *st) {
545     if(unlikely(netdata_exit)) return 0;
546
547     debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
548
549     RRDDIM *rd;
550
551     int
552             pthreadoldcancelstate;  // store the old cancelable pthread state, to restore it at the end
553
554     char
555             store_this_entry = 1,   // boolean: 1 = store this entry, 0 = don't store this entry
556             first_entry = 0;        // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
557
558     unsigned int
559             stored_entries = 0;     // the number of entries we have stored in the db, during this call to rrdset_done()
560
561     usec_t
562             last_collect_ut,        // the timestamp in microseconds, of the last collected value
563             now_collect_ut,         // the timestamp in microseconds, of this collected value (this is NOW)
564             last_stored_ut,         // the timestamp in microseconds, of the last stored entry in the db
565             next_store_ut,          // the timestamp in microseconds, of the next entry to store in the db
566             update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
567
568     if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
569         error("Cannot set pthread cancel state to DISABLE.");
570
571     // a read lock is OK here
572     rrdset_rdlock(st);
573
574 /*
575     // enable the chart, if it was disabled
576     if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
577         st->enabled = 1;
578 */
579
580     // check if the chart has a long time to be updated
581     if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
582         info("%s: took too long to be updated (%0.3Lf secs). Resetting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
583         rrdset_reset(st);
584         st->usec_since_last_update = update_every_ut;
585         first_entry = 1;
586     }
587
588     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
589         debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
590
591     // set last_collected_time
592     if(unlikely(!st->last_collected_time.tv_sec)) {
593         // it is the first entry
594         // set the last_collected_time to now
595         now_realtime_timeval(&st->last_collected_time);
596         timeval_align(&st->last_collected_time, st->update_every);
597
598         last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
599
600         // the first entry should not be stored
601         store_this_entry = 0;
602         first_entry = 1;
603
604         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
605             debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
606     }
607     else {
608         // it is not the first entry
609         // calculate the proper last_collected_time, using usec_since_last_update
610         last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
611         usec_t ut = last_collect_ut + st->usec_since_last_update;
612         st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
613         st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
614     }
615
616     // if this set has not been updated in the past
617     // we fake the last_update time to be = now - usec_since_last_update
618     if(unlikely(!st->last_updated.tv_sec)) {
619         // it has never been updated before
620         // set a fake last_updated, in the past using usec_since_last_update
621         usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
622         st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
623         st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
624
625         // the first entry should not be stored
626         store_this_entry = 0;
627         first_entry = 1;
628
629         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
630             debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf). Will not store the next entry.", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
631     }
632
633     // check if we will re-write the entire data set
634     if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
635         info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Resetting it. Will not store the next entry.", st->name, st->last_updated.tv_sec, st->last_updated.tv_usec, st->last_collected_time.tv_sec, st->last_collected_time.tv_usec);
636         rrdset_reset(st);
637
638         st->usec_since_last_update = update_every_ut;
639
640         now_realtime_timeval(&st->last_collected_time);
641         timeval_align(&st->last_collected_time, st->update_every);
642
643         usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
644         st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
645         st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
646
647         // the first entry should not be stored
648         store_this_entry = 0;
649         first_entry = 1;
650     }
651
652     // these are the 3 variables that will help us in interpolation
653     // last_stored_ut = the last time we added a value to the storage
654     // now_collect_ut = the time the current value has been collected
655     // next_store_ut  = the time of the next interpolation point
656     last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
657     now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
658     next_store_ut  = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
659
660     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
661         debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
662         debug(D_RRD_STATS, "%s: now_collect_ut  = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
663         debug(D_RRD_STATS, "%s: last_stored_ut  = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
664         debug(D_RRD_STATS, "%s: next_store_ut   = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
665     }
666
667     if(unlikely(!st->counter_done)) {
668         store_this_entry = 0;
669         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
670             debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
671     }
672     st->counter_done++;
673
674     // calculate totals and count the dimensions
675     int dimensions;
676     st->collected_total = 0;
677     for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
678         if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
679             st->collected_total += rd->collected_value;
680
681     uint32_t storage_flags = SN_EXISTS;
682
683     // process all dimensions to calculate their values
684     // based on the collected figures only
685     // at this stage we do not interpolate anything
686     for( rd = st->dimensions ; rd ; rd = rd->next ) {
687
688         if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED))) {
689             rd->calculated_value = 0;
690             continue;
691         }
692
693         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
694             debug(D_RRD_STATS, "%s/%s: START "
695                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
696                     " collected_value = " COLLECTED_NUMBER_FORMAT
697                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
698                     " calculated_value = " CALCULATED_NUMBER_FORMAT
699                                       , st->id, rd->name
700                                       , rd->last_collected_value
701                                       , rd->collected_value
702                                       , rd->last_calculated_value
703                                       , rd->calculated_value
704             );
705
706         switch(rd->algorithm) {
707             case RRD_ALGORITHM_ABSOLUTE:
708                 rd->calculated_value = (calculated_number)rd->collected_value
709                                        * (calculated_number)rd->multiplier
710                                        / (calculated_number)rd->divisor;
711
712                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
713                     debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
714                             CALCULATED_NUMBER_FORMAT " = "
715                             COLLECTED_NUMBER_FORMAT
716                             " * " CALCULATED_NUMBER_FORMAT
717                             " / " CALCULATED_NUMBER_FORMAT
718                           , st->id, rd->name
719                           , rd->calculated_value
720                           , rd->collected_value
721                           , (calculated_number)rd->multiplier
722                           , (calculated_number)rd->divisor
723                     );
724                 break;
725
726             case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
727                 if(unlikely(!st->collected_total))
728                     rd->calculated_value = 0;
729                 else
730                     // the percentage of the current value
731                     // over the total of all dimensions
732                     rd->calculated_value =
733                             (calculated_number)100
734                             * (calculated_number)rd->collected_value
735                             / (calculated_number)st->collected_total;
736
737                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
738                     debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
739                             CALCULATED_NUMBER_FORMAT " = 100"
740                                     " * " COLLECTED_NUMBER_FORMAT
741                             " / " COLLECTED_NUMBER_FORMAT
742                           , st->id, rd->name
743                           , rd->calculated_value
744                           , rd->collected_value
745                           , st->collected_total
746                     );
747                 break;
748
749             case RRD_ALGORITHM_INCREMENTAL:
750                 if(unlikely(rd->counter <= 1)) {
751                     rd->calculated_value = 0;
752                     continue;
753                 }
754
755                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
756                 // to reset the calculation (it will give zero as the calculation for this second)
757                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
758                     debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
759                           , st->name, rd->name
760                           , rd->last_collected_value
761                           , rd->collected_value);
762
763                     if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
764                         storage_flags = SN_EXISTS_RESET;
765
766                     rd->last_collected_value = rd->collected_value;
767                 }
768
769                 rd->calculated_value +=
770                         (calculated_number)(rd->collected_value - rd->last_collected_value)
771                         * (calculated_number)rd->multiplier
772                         / (calculated_number)rd->divisor;
773
774                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
775                     debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
776                             CALCULATED_NUMBER_FORMAT " = ("
777                             COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
778                             ")"
779                                     " * " CALCULATED_NUMBER_FORMAT
780                             " / " CALCULATED_NUMBER_FORMAT
781                           , st->id, rd->name
782                           , rd->calculated_value
783                           , rd->collected_value, rd->last_collected_value
784                           , (calculated_number)rd->multiplier
785                           , (calculated_number)rd->divisor
786                     );
787                 break;
788
789             case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
790                 if(unlikely(rd->counter <= 1)) {
791                     rd->calculated_value = 0;
792                     continue;
793                 }
794
795                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
796                 // to reset the calculation (it will give zero as the calculation for this second)
797                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
798                     debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
799                           , st->name, rd->name
800                           , rd->last_collected_value
801                           , rd->collected_value);
802
803                     if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
804                         storage_flags = SN_EXISTS_RESET;
805
806                     rd->last_collected_value = rd->collected_value;
807                 }
808
809                 // the percentage of the current increment
810                 // over the increment of all dimensions together
811                 if(unlikely(st->collected_total == st->last_collected_total))
812                     rd->calculated_value = 0;
813                 else
814                     rd->calculated_value =
815                             (calculated_number)100
816                             * (calculated_number)(rd->collected_value - rd->last_collected_value)
817                             / (calculated_number)(st->collected_total - st->last_collected_total);
818
819                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
820                     debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
821                             CALCULATED_NUMBER_FORMAT " = 100"
822                                     " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
823                                     " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
824                           , st->id, rd->name
825                           , rd->calculated_value
826                           , rd->collected_value, rd->last_collected_value
827                           , st->collected_total, st->last_collected_total
828                     );
829                 break;
830
831             default:
832                 // make the default zero, to make sure
833                 // it gets noticed when we add new types
834                 rd->calculated_value = 0;
835
836                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
837                     debug(D_RRD_STATS, "%s/%s: CALC "
838                             CALCULATED_NUMBER_FORMAT " = 0"
839                           , st->id, rd->name
840                           , rd->calculated_value
841                     );
842                 break;
843         }
844
845         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
846             debug(D_RRD_STATS, "%s/%s: PHASE2 "
847                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
848                     " collected_value = " COLLECTED_NUMBER_FORMAT
849                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
850                     " calculated_value = " CALCULATED_NUMBER_FORMAT
851                                       , st->id, rd->name
852                                       , rd->last_collected_value
853                                       , rd->collected_value
854                                       , rd->last_calculated_value
855                                       , rd->calculated_value
856             );
857
858     }
859
860     // at this point we have all the calculated values ready
861     // it is now time to interpolate values on a second boundary
862
863     if(unlikely(now_collect_ut < next_store_ut)) {
864         // this is collected in the same interpolation point
865
866         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
867             debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
868
869 #ifdef NETDATA_INTERNAL_CHECKS
870         info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
871 #endif
872     }
873
874     usec_t first_ut = last_stored_ut;
875     long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
876     if((now_collect_ut % (update_every_ut)) == 0) iterations++;
877
878     for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
879 #ifdef NETDATA_INTERNAL_CHECKS
880         if(iterations < 0) { error("%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); }
881 #endif
882
883         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
884             debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
885             debug(D_RRD_STATS, "%s: next_store_ut  = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
886         }
887
888         st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
889         st->last_updated.tv_usec = 0;
890
891         for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
892             calculated_number new_value;
893
894             switch(rd->algorithm) {
895                 case RRD_ALGORITHM_INCREMENTAL:
896                     new_value = (calculated_number)
897                             (      rd->calculated_value
898                                    * (calculated_number)(next_store_ut - last_collect_ut)
899                                    / (calculated_number)(now_collect_ut - last_collect_ut)
900                             );
901
902                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
903                         debug(D_RRD_STATS, "%s/%s: CALC2 INC "
904                                 CALCULATED_NUMBER_FORMAT " = "
905                                 CALCULATED_NUMBER_FORMAT
906                                 " * %llu"
907                                         " / %llu"
908                               , st->id, rd->name
909                               , new_value
910                               , rd->calculated_value
911                               , (next_store_ut - last_stored_ut)
912                               , (now_collect_ut - last_stored_ut)
913                         );
914
915                     rd->calculated_value -= new_value;
916                     new_value += rd->last_calculated_value;
917                     rd->last_calculated_value = 0;
918                     new_value /= (calculated_number)st->update_every;
919
920                     if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
921                         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
922                             debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
923                                     st->id, rd->name
924                                   , (calculated_number)(next_store_ut - last_stored_ut)
925                             );
926                         new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
927                     }
928                     break;
929
930                 case RRD_ALGORITHM_ABSOLUTE:
931                 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
932                 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
933                 default:
934                     if(iterations == 1) {
935                         // this is the last iteration
936                         // do not interpolate
937                         // just show the calculated value
938
939                         new_value = rd->calculated_value;
940                     }
941                     else {
942                         // we have missed an update
943                         // interpolate in the middle values
944
945                         new_value = (calculated_number)
946                                 (   (     (rd->calculated_value - rd->last_calculated_value)
947                                           * (calculated_number)(next_store_ut - last_collect_ut)
948                                           / (calculated_number)(now_collect_ut - last_collect_ut)
949                                     )
950                                     +  rd->last_calculated_value
951                                 );
952
953                         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
954                             debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
955                                     CALCULATED_NUMBER_FORMAT " = ((("
956                                             "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
957                                             " * %llu"
958                                             " / %llu) + " CALCULATED_NUMBER_FORMAT
959                                   , st->id, rd->name
960                                   , new_value
961                                   , rd->calculated_value, rd->last_calculated_value
962                                   , (next_store_ut - first_ut)
963                                   , (now_collect_ut - first_ut), rd->last_calculated_value
964                             );
965                     }
966                     break;
967             }
968
969             if(unlikely(!store_this_entry)) {
970                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
971                 continue;
972             }
973
974             if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED) && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
975                 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
976                 rd->last_stored_value = new_value;
977
978                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
979                     debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
980                             CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
981                           , st->id, rd->name
982                           , st->current_entry
983                           , unpack_storage_number(rd->values[st->current_entry]), new_value
984                     );
985             }
986             else {
987                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
988                     debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
989                           , st->id, rd->name
990                           , st->current_entry
991                     );
992                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
993                 rd->last_stored_value = NAN;
994             }
995
996             stored_entries++;
997
998             if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
999                 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1000                 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1001                 calculated_number accuracy = accuracy_loss(t1, t2);
1002                 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1003                       , st->id, rd->name
1004                       , st->current_entry
1005                       , t2
1006                       , get_storage_number_flags(rd->values[st->current_entry])
1007                       , t1
1008                       , accuracy
1009                       , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1010                 );
1011
1012                 rd->collected_volume += t1;
1013                 rd->stored_volume += t2;
1014                 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1015                 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated  = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1016                       , st->id, rd->name
1017                       , st->current_entry
1018                       , rd->stored_volume
1019                       , rd->collected_volume
1020                       , accuracy
1021                       , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1022                 );
1023
1024             }
1025         }
1026         // reset the storage flags for the next point, if any;
1027         storage_flags = SN_EXISTS;
1028
1029         st->counter++;
1030         st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1031         last_stored_ut = next_store_ut;
1032     }
1033
1034     st->last_collected_total  = st->collected_total;
1035
1036     for( rd = st->dimensions; rd ; rd = rd->next ) {
1037         if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
1038             continue;
1039
1040         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1041             debug(D_RRD_STATS, "%s/%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_collected_value, rd->collected_value);
1042
1043         rd->last_collected_value = rd->collected_value;
1044
1045         switch(rd->algorithm) {
1046             case RRD_ALGORITHM_INCREMENTAL:
1047                 if(unlikely(!first_entry)) {
1048                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1049                         debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value + rd->calculated_value, rd->calculated_value);
1050                     rd->last_calculated_value += rd->calculated_value;
1051                 }
1052                 else {
1053                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1054                         debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
1055                 }
1056                 break;
1057
1058             case RRD_ALGORITHM_ABSOLUTE:
1059             case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
1060             case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
1061                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1062                     debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value, rd->calculated_value);
1063                 rd->last_calculated_value = rd->calculated_value;
1064                 break;
1065         }
1066
1067         rd->calculated_value = 0;
1068         rd->collected_value = 0;
1069         rrddim_flag_clear(rd, RRDDIM_FLAG_UPDATED);
1070
1071         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1072             debug(D_RRD_STATS, "%s/%s: END "
1073                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
1074                     " collected_value = " COLLECTED_NUMBER_FORMAT
1075                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1076                     " calculated_value = " CALCULATED_NUMBER_FORMAT
1077                                       , st->id, rd->name
1078                                       , rd->last_collected_value
1079                                       , rd->collected_value
1080                                       , rd->last_calculated_value
1081                                       , rd->calculated_value
1082             );
1083     }
1084
1085     // ALL DONE ABOUT THE DATA UPDATE
1086     // --------------------------------------------------------------------
1087
1088 /*
1089     // find if there are any obsolete dimensions (not updated recently)
1090     if(unlikely(rrd_delete_unupdated_dimensions)) {
1091
1092         for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1093             if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1094                 break;
1095
1096         if(unlikely(rd)) {
1097             RRDDIM *last;
1098             // there is dimension to free
1099             // upgrade our read lock to a write lock
1100             pthread_rwlock_unlock(&st->rrdset_rwlock);
1101             pthread_rwlock_wrlock(&st->rrdset_rwlock);
1102
1103             for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1104                 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1105
1106                 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1107                     info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1108
1109                     if(unlikely(!last)) {
1110                         st->dimensions = rd->next;
1111                         rd->next = NULL;
1112                         rrddim_free(st, rd);
1113                         rd = st->dimensions;
1114                         continue;
1115                     }
1116                     else {
1117                         last->next = rd->next;
1118                         rd->next = NULL;
1119                         rrddim_free(st, rd);
1120                         rd = last->next;
1121                         continue;
1122                     }
1123                 }
1124
1125                 last = rd;
1126                 rd = rd->next;
1127             }
1128
1129             if(unlikely(!st->dimensions)) {
1130                 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1131                 st->enabled = 0;
1132             }
1133         }
1134     }
1135 */
1136
1137     rrdset_unlock(st);
1138
1139     if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1140         error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
1141
1142     return(st->usec_since_last_update);
1143 }
1144