]> arthur.barton.de Git - netdata.git/blob - src/rrd.c
code optimizations; added temperature charts.d plugin
[netdata.git] / src / rrd.c
1 #include <stddef.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include <sys/time.h>
6 #include <sys/mman.h>
7 #include <pthread.h>
8 #include <errno.h>
9 #include <ctype.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <stdlib.h>
13
14 #include "log.h"
15 #include "config.h"
16 #include "common.h"
17
18 #include "rrd.h"
19
20 #define RRD_DEFAULT_GAP_INTERPOLATIONS 10
21
22 // ----------------------------------------------------------------------------
23 // globals
24
25 // if not zero it gives the time (in seconds) to remove un-updated dimensions
26 int rrd_delete_unupdated_dimensions = 0;
27
28 int rrd_update_every = UPDATE_EVERY;
29 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
30
31 RRDSET *rrdset_root = NULL;
32 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
33
34 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
35
36
37 // ----------------------------------------------------------------------------
38 // RRDSET index
39
40 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
41
42 static int rrdset_compare(void* a, void* b) {
43         if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
44         else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
45         else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
46 }
47
48 avl_tree rrdset_root_index = {
49                 NULL,
50                 rrdset_compare
51 };
52
53 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
54 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
55
56 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
57         RRDSET *result = NULL, tmp;
58         strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
59         tmp.id[RRD_ID_LENGTH_MAX] = '\0';
60         tmp.hash = (hash)?hash:simple_hash(tmp.id);
61
62         avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
63         return result;
64 }
65
66 // ----------------------------------------------------------------------------
67 // RRDSET name index
68
69 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
70
71 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
72
73 static int rrdset_compare_name(void* a, void* b) {
74         RRDSET *A = rrdset_from_avlname(a);
75         RRDSET *B = rrdset_from_avlname(b);
76
77         // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
78
79         if(A->hash_name < B->hash_name) return -1;
80         else if(A->hash_name > B->hash_name) return 1;
81         else return strcmp(A->name, B->name);
82 }
83
84 avl_tree rrdset_root_index_name = {
85                 NULL,
86                 rrdset_compare_name
87 };
88
89 int rrdset_index_add_name(RRDSET *st) {
90         // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
91         return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
92 }
93
94 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
95
96 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
97         void *result = NULL;
98         RRDSET tmp;
99         tmp.name = name;
100         tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
101
102         // fprintf(stderr, "SEARCHING: %s\n", name);
103         avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
104         if(result) {
105                 RRDSET *st = rrdset_from_avlname(result);
106                 if(strcmp(st->magic, RRDSET_MAGIC))
107                         error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
108
109                 // fprintf(stderr, "FOUND: %s\n", name);
110                 return rrdset_from_avlname(result);
111         }
112         // fprintf(stderr, "NOT FOUND: %s\n", name);
113         return NULL;
114 }
115
116
117 // ----------------------------------------------------------------------------
118 // RRDDIM index
119
120 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
121
122 static int rrddim_compare(void* a, void* b) {
123         if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
124         else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
125         else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
126 }
127
128 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
129 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
130
131 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
132         RRDDIM *result = NULL, tmp;
133         strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
134         tmp.id[RRD_ID_LENGTH_MAX] = '\0';
135         tmp.hash = (hash)?hash:simple_hash(tmp.id);
136
137         avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
138         return result;
139 }
140
141 // ----------------------------------------------------------------------------
142 // chart types
143
144 int rrdset_type_id(const char *name)
145 {
146         if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
147         else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
148         else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
149         return RRDSET_TYPE_LINE;
150 }
151
152 const char *rrdset_type_name(int chart_type)
153 {
154         static char line[] = RRDSET_TYPE_LINE_NAME;
155         static char area[] = RRDSET_TYPE_AREA_NAME;
156         static char stacked[] = RRDSET_TYPE_STACKED_NAME;
157
158         switch(chart_type) {
159                 case RRDSET_TYPE_LINE:
160                         return line;
161
162                 case RRDSET_TYPE_AREA:
163                         return area;
164
165                 case RRDSET_TYPE_STACKED:
166                         return stacked;
167         }
168         return line;
169 }
170
171 // ----------------------------------------------------------------------------
172 // load / save
173
174 const char *rrd_memory_mode_name(int id)
175 {
176         static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
177         static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
178         static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
179
180         switch(id) {
181                 case RRD_MEMORY_MODE_RAM:
182                         return ram;
183
184                 case RRD_MEMORY_MODE_MAP:
185                         return map;
186
187                 case RRD_MEMORY_MODE_SAVE:
188                 default:
189                         return save;
190         }
191
192         return save;
193 }
194
195 int rrd_memory_mode_id(const char *name)
196 {
197         if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
198                 return RRD_MEMORY_MODE_RAM;
199         else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
200                 return RRD_MEMORY_MODE_MAP;
201
202         return RRD_MEMORY_MODE_SAVE;
203 }
204
205 // ----------------------------------------------------------------------------
206 // algorithms types
207
208 int rrddim_algorithm_id(const char *name)
209 {
210         if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0)                     return RRDDIM_ABSOLUTE;
211         if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0)                  return RRDDIM_INCREMENTAL;
212         if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0)                 return RRDDIM_PCENT_OVER_ROW_TOTAL;
213         if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0)        return RRDDIM_PCENT_OVER_DIFF_TOTAL;
214         return RRDDIM_ABSOLUTE;
215 }
216
217 const char *rrddim_algorithm_name(int chart_type)
218 {
219         static char absolute[] = RRDDIM_ABSOLUTE_NAME;
220         static char incremental[] = RRDDIM_INCREMENTAL_NAME;
221         static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
222         static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
223
224         switch(chart_type) {
225                 case RRDDIM_ABSOLUTE:
226                         return absolute;
227
228                 case RRDDIM_INCREMENTAL:
229                         return incremental;
230
231                 case RRDDIM_PCENT_OVER_ROW_TOTAL:
232                         return percentage_of_absolute_row;
233
234                 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
235                         return percentage_of_incremental_row;
236         }
237         return absolute;
238 }
239
240 // ----------------------------------------------------------------------------
241 // chart names
242
243 char *rrdset_strncpy_name(char *to, const char *from, int length)
244 {
245         int i;
246         for(i = 0; i < length && from[i] ;i++) {
247                 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
248                 else to[i] = '_';
249         }
250         if(i < length) to[i] = '\0';
251         to[length - 1] = '\0';
252
253         return to;
254 }
255
256 void rrdset_set_name(RRDSET *st, const char *name)
257 {
258         debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
259
260         if(st->name) rrdset_index_del_name(st);
261
262         char b[CONFIG_MAX_VALUE + 1];
263         char n[RRD_ID_LENGTH_MAX + 1];
264
265         snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
266         rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
267         st->name = config_get(st->id, "name", b);
268         st->hash_name = simple_hash(st->name);
269
270         rrdset_index_add_name(st);
271 }
272
273 // ----------------------------------------------------------------------------
274 // cache directory
275
276 char *rrdset_cache_dir(const char *id)
277 {
278         char *ret = NULL;
279
280         static char *cache_dir = NULL;
281         if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
282
283         char b[FILENAME_MAX + 1];
284         char n[FILENAME_MAX + 1];
285         rrdset_strncpy_name(b, id, FILENAME_MAX);
286
287         snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
288         ret = config_get(id, "database directory", n);
289
290         if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
291                 int r = mkdir(ret, 0775);
292                 if(r != 0 && errno != EEXIST)
293                         error("Cannot create directory '%s'", ret);
294         }
295
296         return ret;
297 }
298
299 // ----------------------------------------------------------------------------
300 // core functions
301
302 void rrdset_reset(RRDSET *st)
303 {
304         debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
305
306         st->last_collected_time.tv_sec = 0;
307         st->last_collected_time.tv_usec = 0;
308         st->last_updated.tv_sec = 0;
309         st->last_updated.tv_usec = 0;
310         st->current_entry = 0;
311         st->counter = 0;
312         st->counter_done = 0;
313
314         RRDDIM *rd;
315         for(rd = st->dimensions; rd ; rd = rd->next) {
316                 rd->last_collected_time.tv_sec = 0;
317                 rd->last_collected_time.tv_usec = 0;
318                 bzero(rd->values, rd->entries * sizeof(storage_number));
319         }
320 }
321
322 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *title, const char *units, long priority, int update_every, int chart_type)
323 {
324         if(!id || !id[0]) {
325                 fatal("Cannot create rrd stats without an id.");
326                 return NULL;
327         }
328
329         char fullid[RRD_ID_LENGTH_MAX + 1];
330         char fullfilename[FILENAME_MAX + 1];
331         RRDSET *st = NULL;
332
333         snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
334
335         long entries = config_get_number(fullid, "history", rrd_default_history_entries);
336         if(entries < 5) entries = config_set_number(fullid, "history", 5);
337         if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
338
339         int enabled = config_get_boolean(fullid, "enabled", 1);
340         if(!enabled) entries = 5;
341
342         unsigned long size = sizeof(RRDSET);
343         char *cache_dir = rrdset_cache_dir(fullid);
344
345         debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
346
347         snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
348         if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
349         if(st) {
350                 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
351                         errno = 0;
352                         info("Initializing file %s.", fullfilename);
353                         bzero(st, size);
354                 }
355                 else if(strcmp(st->id, fullid) != 0) {
356                         errno = 0;
357                         error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
358                         // munmap(st, size);
359                         // st = NULL;
360                         bzero(st, size);
361                 }
362                 else if(st->memsize != size || st->entries != entries) {
363                         errno = 0;
364                         error("File %s does not have the desired size. Clearing it.", fullfilename);
365                         bzero(st, size);
366                 }
367                 else if(st->update_every != update_every) {
368                         errno = 0;
369                         error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
370                         bzero(st, size);
371                 }
372                 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
373                         errno = 0;
374                         error("File %s is too old. Clearing it.", fullfilename);
375                         bzero(st, size);
376                 }
377         }
378
379         if(st) {
380                 st->name = NULL;
381                 st->type = NULL;
382                 st->family = NULL;
383                 st->title = NULL;
384                 st->units = NULL;
385                 st->dimensions = NULL;
386                 st->next = NULL;
387                 st->mapped = rrd_memory_mode;
388         }
389         else {
390                 st = calloc(1, size);
391                 if(!st) {
392                         fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
393                         return NULL;
394                 }
395                 st->mapped = RRD_MEMORY_MODE_RAM;
396         }
397         st->memsize = size;
398         st->entries = entries;
399         st->update_every = update_every;
400
401         strcpy(st->cache_filename, fullfilename);
402         strcpy(st->magic, RRDSET_MAGIC);
403
404         strcpy(st->id, fullid);
405         st->hash = simple_hash(st->id);
406
407         st->cache_dir = cache_dir;
408
409         st->family     = config_get(st->id, "family", family?family:st->id);
410         st->units      = config_get(st->id, "units", units?units:"");
411         st->type       = config_get(st->id, "type", type);
412         st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
413
414         st->priority = config_get_number(st->id, "priority", priority);
415         st->enabled = enabled;
416         
417         st->isdetail = 0;
418         st->debug = 0;
419
420         st->last_collected_time.tv_sec = 0;
421         st->last_collected_time.tv_usec = 0;
422         st->counter_done = 0;
423
424         st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS);
425
426         st->dimensions_index.root = NULL;
427         st->dimensions_index.compar = rrddim_compare;
428
429         pthread_rwlock_init(&st->rwlock, NULL);
430         pthread_rwlock_wrlock(&rrdset_root_rwlock);
431
432         if(name && *name) rrdset_set_name(st, name);
433         else rrdset_set_name(st, id);
434
435         {
436                 char varvalue[CONFIG_MAX_VALUE + 1];
437                 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
438                 st->title = config_get(st->id, "title", varvalue);
439         }
440
441         st->next = rrdset_root;
442         rrdset_root = st;
443
444         rrdset_index_add(st);
445
446         pthread_rwlock_unlock(&rrdset_root_rwlock);
447
448         return(st);
449 }
450
451 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
452 {
453         char filename[FILENAME_MAX + 1];
454         char fullfilename[FILENAME_MAX + 1];
455
456         char varname[CONFIG_MAX_NAME + 1];
457         RRDDIM *rd = NULL;
458         unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
459
460         debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
461
462         rrdset_strncpy_name(filename, id, FILENAME_MAX);
463         snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
464         if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
465         if(rd) {
466                 struct timeval now;
467                 gettimeofday(&now, NULL);
468
469                 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
470                         errno = 0;
471                         info("Initializing file %s.", fullfilename);
472                         bzero(rd, size);
473                 }
474                 else if(rd->memsize != size) {
475                         errno = 0;
476                         error("File %s does not have the desired size. Clearing it.", fullfilename);
477                         bzero(rd, size);
478                 }
479                 else if(rd->multiplier != multiplier) {
480                         errno = 0;
481                         error("File %s does not have the same multiplier. Clearing it.", fullfilename);
482                         bzero(rd, size);
483                 }
484                 else if(rd->divisor != divisor) {
485                         errno = 0;
486                         error("File %s does not have the same divisor. Clearing it.", fullfilename);
487                         bzero(rd, size);
488                 }
489                 else if(rd->algorithm != algorithm) {
490                         errno = 0;
491                         error("File %s does not have the same algorithm. Clearing it.", fullfilename);
492                         bzero(rd, size);
493                 }
494                 else if(rd->update_every != st->update_every) {
495                         errno = 0;
496                         error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
497                         bzero(rd, size);
498                 }
499                 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
500                         errno = 0;
501                         error("File %s is too old. Clearing it.", fullfilename);
502                         bzero(rd, size);
503                 }
504                 else if(strcmp(rd->id, id) != 0) {
505                         errno = 0;
506                         error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
507                         // munmap(rd, size);
508                         // rd = NULL;
509                         bzero(rd, size);
510                 }
511         }
512
513         if(rd) {
514                 // we have a file mapped for rd
515                 rd->mapped = rrd_memory_mode;
516                 rd->hidden = 0;
517                 rd->next = NULL;
518                 rd->name = NULL;
519         }
520         else {
521                 // if we didn't manage to get a mmap'd dimension, just create one
522
523                 rd = calloc(1, size);
524                 if(!rd) {
525                         fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
526                         return NULL;
527                 }
528
529                 rd->mapped = RRD_MEMORY_MODE_RAM;
530         }
531         rd->memsize = size;
532
533         strcpy(rd->magic, RRDDIMENSION_MAGIC);
534         strcpy(rd->cache_filename, fullfilename);
535         strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
536         rd->hash = simple_hash(rd->id);
537
538         snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
539         rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
540
541         snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
542         rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
543
544         snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
545         rd->multiplier = config_get_number(st->id, varname, multiplier);
546
547         snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
548         rd->divisor = config_get_number(st->id, varname, divisor);
549         if(!rd->divisor) rd->divisor = 1;
550
551         rd->entries = st->entries;
552         rd->update_every = st->update_every;
553         
554         // append this dimension
555         if(!st->dimensions)
556                 st->dimensions = rd;
557         else {
558                 RRDDIM *td = st->dimensions;
559                 for(; td->next; td = td->next) ;
560                 td->next = rd;
561         }
562
563         rrddim_index_add(st, rd);
564
565         return(rd);
566 }
567
568 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
569 {
570         debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
571
572         char varname[CONFIG_MAX_NAME + 1];
573         snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
574         config_get(st->id, varname, name);
575 }
576
577 void rrddim_free(RRDSET *st, RRDDIM *rd)
578 {
579         debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
580
581         RRDDIM *i = st->dimensions, *last = NULL;
582         for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
583
584         if(!i) {
585                 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
586                 return;
587         }
588
589         if(last) last = i->next;
590         else st->dimensions = i->next;
591
592         rrddim_index_del(st, rd);
593
594         // free(rd->annotations);
595         if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
596                 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
597                 savememory(rd->cache_filename, rd, rd->memsize);
598
599                 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
600                 munmap(rd, rd->memsize);
601         }
602         else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
603                 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
604                 munmap(rd, rd->memsize);
605         }
606         else {
607                 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
608                 free(rd);
609         }
610 }
611
612 void rrdset_free_all(void)
613 {
614         info("Freeing all memory...");
615
616         RRDSET *st;
617         for(st = rrdset_root; st ;) {
618                 RRDSET *next = st->next;
619
620                 while(st->dimensions)
621                         rrddim_free(st, st->dimensions);
622
623                 rrdset_index_del(st);
624
625                 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
626                         debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
627                         savememory(st->cache_filename, st, st->memsize);
628
629                         debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
630                         munmap(st, st->memsize);
631                 }
632                 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
633                         debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
634                         munmap(st, st->memsize);
635                 }
636                 else
637                         free(st);
638
639                 st = next;
640         }
641         rrdset_root = NULL;
642
643         info("Memory cleanup completed...");
644 }
645
646 void rrdset_save_all(void)
647 {
648         debug(D_RRD_CALLS, "rrdset_save_all()");
649
650         RRDSET *st;
651         RRDDIM *rd;
652
653         pthread_rwlock_wrlock(&rrdset_root_rwlock);
654         for(st = rrdset_root; st ; st = st->next) {
655                 pthread_rwlock_wrlock(&st->rwlock);
656
657                 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
658                         debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
659                         savememory(st->cache_filename, st, st->memsize);
660                 }
661
662                 for(rd = st->dimensions; rd ; rd = rd->next) {
663                         if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
664                                 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
665                                 savememory(rd->cache_filename, rd, rd->memsize);
666                         }
667                 }
668
669                 pthread_rwlock_unlock(&st->rwlock);
670         }
671         pthread_rwlock_unlock(&rrdset_root_rwlock);
672 }
673
674
675 RRDSET *rrdset_find(const char *id)
676 {
677         debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
678
679         pthread_rwlock_rdlock(&rrdset_root_rwlock);
680         RRDSET *st = rrdset_index_find(id, 0);
681         pthread_rwlock_unlock(&rrdset_root_rwlock);
682
683         return(st);
684 }
685
686 RRDSET *rrdset_find_bytype(const char *type, const char *id)
687 {
688         debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
689
690         char buf[RRD_ID_LENGTH_MAX + 1];
691
692         strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
693         buf[RRD_ID_LENGTH_MAX - 1] = '\0';
694         strcat(buf, ".");
695         int len = strlen(buf);
696         strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
697         buf[RRD_ID_LENGTH_MAX] = '\0';
698
699         return(rrdset_find(buf));
700 }
701
702 RRDSET *rrdset_find_byname(const char *name)
703 {
704         debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
705
706         pthread_rwlock_rdlock(&rrdset_root_rwlock);
707         RRDSET *st = rrdset_index_find_name(name, 0);
708         pthread_rwlock_unlock(&rrdset_root_rwlock);
709
710         return(st);
711 }
712
713 RRDDIM *rrddim_find(RRDSET *st, const char *id)
714 {
715         debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
716
717         return rrddim_index_find(st, id, 0);
718 }
719
720 int rrddim_hide(RRDSET *st, const char *id)
721 {
722         debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
723
724         RRDDIM *rd = rrddim_find(st, id);
725         if(unlikely(!rd)) {
726                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
727                 return 1;
728         }
729
730         rd->hidden = 1;
731         return 0;
732 }
733
734 int rrddim_unhide(RRDSET *st, const char *id)
735 {
736         debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
737
738         RRDDIM *rd = rrddim_find(st, id);
739         if(unlikely(!rd)) {
740                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
741                 return 1;
742         }
743
744         rd->hidden = 0;
745         return 0;
746 }
747
748 void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
749 {
750         debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
751         
752         gettimeofday(&rd->last_collected_time, NULL);
753         rd->collected_value = value;
754         rd->updated = 1;
755 }
756
757 int rrddim_set(RRDSET *st, const char *id, collected_number value)
758 {
759         RRDDIM *rd = rrddim_find(st, id);
760         if(unlikely(!rd)) {
761                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
762                 return 1;
763         }
764
765         rrddim_set_by_pointer(st, rd, value);
766         return 0;
767 }
768
769 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
770 {
771         debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
772
773         if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
774         st->usec_since_last_update = microseconds;
775 }
776
777 void rrdset_next(RRDSET *st)
778 {
779         unsigned long long microseconds = 0;
780
781         if(likely(st->last_collected_time.tv_sec)) {
782                 struct timeval now;
783                 gettimeofday(&now, NULL);
784                 microseconds = usecdiff(&now, &st->last_collected_time);
785         }
786
787         rrdset_next_usec(st, microseconds);
788 }
789
790 void rrdset_next_plugins(RRDSET *st)
791 {
792         rrdset_next(st);
793 }
794
795 unsigned long long rrdset_done(RRDSET *st)
796 {
797         debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
798
799         RRDDIM *rd, *last;
800         int oldstate, store_this_entry = 1;
801
802         if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
803                 error("Cannot set pthread cancel state to DISABLE.");
804
805         // a read lock is OK here
806         pthread_rwlock_rdlock(&st->rwlock);
807
808         // check if the chart has a long time to be refreshed
809         if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
810                 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
811                 rrdset_reset(st);
812                 st->usec_since_last_update = st->update_every * 1000000ULL;
813         }
814         if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
815
816         // set last_collected_time
817         if(unlikely(!st->last_collected_time.tv_sec)) {
818                 // it is the first entry
819                 // set the last_collected_time to now
820                 gettimeofday(&st->last_collected_time, NULL);
821
822                 // the first entry should not be stored
823                 store_this_entry = 0;
824
825                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
826         }
827         else {
828                 // it is not the first entry
829                 // calculate the proper last_collected_time, using usec_since_last_update
830                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
831                 st->last_collected_time.tv_sec = ut / 1000000ULL;
832                 st->last_collected_time.tv_usec = ut % 1000000ULL;
833         }
834
835         // if this set has not been updated in the past
836         // we fake the last_update time to be = now - usec_since_last_update
837         if(unlikely(!st->last_updated.tv_sec)) {
838                 // it has never been updated before
839                 // set a fake last_updated, in the past using usec_since_last_update
840                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
841                 st->last_updated.tv_sec = ut / 1000000ULL;
842                 st->last_updated.tv_usec = ut % 1000000ULL;
843
844                 // the first entry should not be stored
845                 store_this_entry = 0;
846
847                 if(unlikely(st->debug)) 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);
848         }
849
850         // check if we will re-write the entire data set
851         if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
852                 info("%s: too old data (last updated at %u.%u, last collected at %u.%u). Reseting 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);
853                 rrdset_reset(st);
854
855                 st->usec_since_last_update = st->update_every * 1000000ULL;
856
857                 gettimeofday(&st->last_collected_time, NULL);
858
859                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
860                 st->last_updated.tv_sec = ut / 1000000ULL;
861                 st->last_updated.tv_usec = ut % 1000000ULL;
862
863                 // the first entry should not be stored
864                 store_this_entry = 0;
865         }
866
867         // these are the 3 variables that will help us in interpolation
868         // last_ut = the last time we added a value to the storage
869         //  now_ut = the time the current value is taken at
870         // next_ut = the time of the next interpolation point
871         unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
872         unsigned long long now_ut  = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
873         unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
874
875         if(unlikely(st->debug)) {
876                 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
877                 debug(D_RRD_STATS, "%s: now  ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
878                 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
879         }
880
881         if(unlikely(!st->counter_done)) {
882                 store_this_entry = 0;
883                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
884         }
885         st->counter_done++;
886
887         // calculate totals and count the dimensions
888         int dimensions;
889         st->collected_total = 0;
890         for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
891                 st->collected_total += rd->collected_value;
892
893         uint32_t storage_flags = SN_EXISTS;
894
895         // process all dimensions to calculate their values
896         // based on the collected figures only
897         // at this stage we do not interpolate anything
898         for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
899
900                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: "
901                         " last_collected_value = " COLLECTED_NUMBER_FORMAT
902                         " collected_value = " COLLECTED_NUMBER_FORMAT
903                         " last_calculated_value = " CALCULATED_NUMBER_FORMAT
904                         " calculated_value = " CALCULATED_NUMBER_FORMAT
905                         , st->id, rd->name
906                         , rd->last_collected_value
907                         , rd->collected_value
908                         , rd->last_calculated_value
909                         , rd->calculated_value
910                         );
911
912                 switch(rd->algorithm) {
913                         case RRDDIM_PCENT_OVER_DIFF_TOTAL:
914                                 // the percentage of the current increment
915                                 // over the increment of all dimensions together
916                                 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
917                                 else rd->calculated_value =
918                                           (calculated_number)100
919                                         * (calculated_number)(rd->collected_value - rd->last_collected_value)
920                                         / (calculated_number)(st->collected_total  - st->last_collected_total);
921
922                                 if(unlikely(st->debug))
923                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
924                                                 CALCULATED_NUMBER_FORMAT " = 100"
925                                                 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
926                                                 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
927                                                 , st->id, rd->name
928                                                 , rd->calculated_value
929                                                 , rd->collected_value, rd->last_collected_value
930                                                 , st->collected_total, st->last_collected_total
931                                                 );
932                                 break;
933
934                         case RRDDIM_PCENT_OVER_ROW_TOTAL:
935                                 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
936                                 else
937                                 // the percentage of the current value
938                                 // over the total of all dimensions
939                                 rd->calculated_value =
940                                           (calculated_number)100
941                                         * (calculated_number)rd->collected_value
942                                         / (calculated_number)st->collected_total;
943
944                                 if(unlikely(st->debug))
945                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
946                                                 CALCULATED_NUMBER_FORMAT " = 100"
947                                                 " * " COLLECTED_NUMBER_FORMAT
948                                                 " / " COLLECTED_NUMBER_FORMAT
949                                                 , st->id, rd->name
950                                                 , rd->calculated_value
951                                                 , rd->collected_value
952                                                 , st->collected_total
953                                                 );
954                                 break;
955
956                         case RRDDIM_INCREMENTAL:
957                                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
958                                 // to reset the calculation (it will give zero as the calculation for this second)
959                                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
960                                         info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
961                                                         , st->name, rd->name
962                                                         , rd->last_collected_value
963                                                         , rd->collected_value);
964                                         storage_flags = SN_EXISTS_RESET;
965                                         rd->last_collected_value = rd->collected_value;
966                                 }
967
968                                 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
969
970                                 if(unlikely(st->debug))
971                                         debug(D_RRD_STATS, "%s/%s: CALC INC "
972                                                 CALCULATED_NUMBER_FORMAT " += "
973                                                 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
974                                                 , st->id, rd->name
975                                                 , rd->calculated_value
976                                                 , rd->collected_value, rd->last_collected_value
977                                                 );
978                                 break;
979
980                         case RRDDIM_ABSOLUTE:
981                                 rd->calculated_value = (calculated_number)rd->collected_value;
982
983                                 if(unlikely(st->debug))
984                                         debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
985                                                 CALCULATED_NUMBER_FORMAT " = "
986                                                 COLLECTED_NUMBER_FORMAT
987                                                 , st->id, rd->name
988                                                 , rd->calculated_value
989                                                 , rd->collected_value
990                                                 );
991                                 break;
992
993                         default:
994                                 // make the default zero, to make sure
995                                 // it gets noticed when we add new types
996                                 rd->calculated_value = 0;
997
998                                 if(unlikely(st->debug))
999                                         debug(D_RRD_STATS, "%s/%s: CALC "
1000                                                 CALCULATED_NUMBER_FORMAT " = 0"
1001                                                 , st->id, rd->name
1002                                                 , rd->calculated_value
1003                                                 );
1004                                 break;
1005                 }
1006         }
1007
1008         // at this point we have all the calculated values ready
1009         // it is now time to interpolate values on a second boundary
1010
1011         unsigned long long first_ut = last_ut;
1012         int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1013
1014         for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1015                 if(iterations < 0) error("iterations calculation wrapped!");
1016
1017                 if(unlikely(st->debug)) {
1018                         debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1019                         debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1020                 }
1021
1022                 st->last_updated.tv_sec = next_ut / 1000000ULL;
1023                 st->last_updated.tv_usec = 0;
1024
1025                 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1026                         calculated_number new_value;
1027
1028                         switch(rd->algorithm) {
1029                                 case RRDDIM_INCREMENTAL:
1030                                         new_value = (calculated_number)
1031                                                 (          rd->calculated_value
1032                                                         * (calculated_number)(next_ut - last_ut)
1033                                                         / (calculated_number)(now_ut - last_ut)
1034                                                 );
1035
1036                                         if(unlikely(st->debug))
1037                                                 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1038                                                         CALCULATED_NUMBER_FORMAT " = "
1039                                                         CALCULATED_NUMBER_FORMAT
1040                                                         " * %llu"
1041                                                         " / %llu"
1042                                                         , st->id, rd->name
1043                                                         , new_value
1044                                                         , rd->calculated_value
1045                                                         , (unsigned long long)(next_ut - last_ut)
1046                                                         , (unsigned long long)(now_ut - last_ut)
1047                                                         );
1048
1049                                         rd->calculated_value -= new_value;
1050                                         break;
1051
1052                                 case RRDDIM_ABSOLUTE:
1053                                 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1054                                 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1055                                 default:
1056                                         new_value = (calculated_number)
1057                                                 (       (         (rd->calculated_value - rd->last_calculated_value)
1058                                                                 * (calculated_number)(next_ut - first_ut)
1059                                                                 / (calculated_number)(now_ut - first_ut)
1060                                                         )
1061                                                         +  rd->last_calculated_value
1062                                                 );
1063
1064                                         if(unlikely(st->debug))
1065                                                 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1066                                                         CALCULATED_NUMBER_FORMAT " = ((("
1067                                                         "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1068                                                         " * %llu"
1069                                                         " / %llu) + " CALCULATED_NUMBER_FORMAT
1070                                                         , st->id, rd->name
1071                                                         , new_value
1072                                                         , rd->calculated_value, rd->last_calculated_value
1073                                                         , (next_ut - first_ut)
1074                                                         , (now_ut - first_ut), rd->last_calculated_value
1075                                                         );
1076
1077                                         if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1078                                         break;
1079                         }
1080
1081                         if(unlikely(!store_this_entry)) {
1082                                 store_this_entry = 1;
1083                                 continue;
1084                         }
1085
1086                         if(likely(rd->updated && iterations < st->gap_when_lost_iterations_above)) {
1087                                 rd->values[st->current_entry] = pack_storage_number(
1088                                                   new_value
1089                                                 * (calculated_number)rd->multiplier
1090                                                 / (calculated_number)rd->divisor
1091                                         , storage_flags );
1092
1093                                 if(unlikely(st->debug))
1094                                         debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1095                                                 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1096                                                 " * %ld"
1097                                                 " / %ld"
1098                                                 , st->id, rd->name
1099                                                 , st->current_entry
1100                                                 , unpack_storage_number(rd->values[st->current_entry]), new_value
1101                                                 , rd->multiplier
1102                                                 , rd->divisor
1103                                                 );
1104                         }
1105                         else {
1106                                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1107                                                 , st->id, rd->name
1108                                                 , st->current_entry
1109                                                 );
1110                                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1111                         }
1112
1113                         if(unlikely(st->debug)) {
1114                                 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1115                                 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1116                                 calculated_number accuracy = accuracy_loss(t1, t2);
1117                                 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1118                                                 , st->id, rd->name
1119                                                 , st->current_entry
1120                                                 , t2
1121                                                 , get_storage_number_flags(rd->values[st->current_entry])
1122                                                 , t1
1123                                                 , accuracy
1124                                                 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1125                                                 );
1126
1127                                 rd->collected_volume += t1;
1128                                 rd->stored_volume += t2;
1129                                 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1130                                 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated  = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1131                                                 , st->id, rd->name
1132                                                 , st->current_entry
1133                                                 , rd->stored_volume
1134                                                 , rd->collected_volume
1135                                                 , accuracy
1136                                                 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1137                                                 );
1138
1139                         }
1140                 }
1141                 // reset the storage flags for the next point, if any;
1142                 storage_flags = SN_EXISTS;
1143
1144                 if(unlikely(st->first_entry_t && st->counter >= (unsigned long long)st->entries)) {
1145                         // the db is overwriting values
1146                         // add the value we will overwrite
1147                         st->first_entry_t += st->update_every * 1000000ULL;
1148                 }
1149                 
1150                 st->counter++;
1151                 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1152                 if(unlikely(!st->first_entry_t)) st->first_entry_t = next_ut;
1153                 last_ut = next_ut;
1154         }
1155
1156         for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1157                 if(unlikely(!rd->updated)) continue;
1158                 rd->last_collected_value = rd->collected_value;
1159                 rd->last_calculated_value = rd->calculated_value;
1160                 rd->collected_value = 0;
1161                 rd->updated = 0;
1162
1163                 // if this is the first entry of incremental dimensions
1164                 // we have to set the first calculated_value to zero
1165                 // to eliminate the first spike
1166                 if(unlikely(st->counter_done == 1)) switch(rd->algorithm) {
1167                         case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1168                         case RRDDIM_INCREMENTAL:
1169                                 rd->calculated_value = 0;
1170                                 // the next time, a new incremental total will be calculated
1171                                 break;
1172                 }
1173         }
1174         st->last_collected_total  = st->collected_total;
1175
1176         // ALL DONE ABOUT THE DATA UPDATE
1177         // --------------------------------------------------------------------
1178
1179         // find if there are any obsolete dimensions (not updated recently)
1180         if(unlikely(rrd_delete_unupdated_dimensions)) {
1181
1182                 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1183                         if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1184                                 break;
1185
1186                 if(unlikely(rd)) {
1187                         // there is dimension to free
1188                         // upgrade our read lock to a write lock
1189                         pthread_rwlock_unlock(&st->rwlock);
1190                         pthread_rwlock_wrlock(&st->rwlock);
1191
1192                         for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1193                                 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1194
1195                                 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1196                                         info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1197
1198                                         if(unlikely(!last)) {
1199                                                 st->dimensions = rd->next;
1200                                                 rd->next = NULL;
1201                                                 rrddim_free(st, rd);
1202                                                 rd = st->dimensions;
1203                                                 continue;
1204                                         }
1205                                         else {
1206                                                 last->next = rd->next;
1207                                                 rd->next = NULL;
1208                                                 rrddim_free(st, rd);
1209                                                 rd = last->next;
1210                                                 continue;
1211                                         }
1212                                 }
1213
1214                                 last = rd;
1215                                 rd = rd->next;
1216                         }
1217
1218                         if(unlikely(!st->dimensions)) {
1219                                 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1220                                 st->enabled = 0;
1221                         }
1222                 }
1223         }
1224
1225         pthread_rwlock_unlock(&st->rwlock);
1226
1227         if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1228                 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1229
1230         return(st->usec_since_last_update);
1231 }
1232
1233
1234 // find the oldest entry in the data, skipping all empty slots
1235 time_t rrdset_first_entry_t(RRDSET *st)
1236 {
1237         if(unlikely(!st->first_entry_t)) return st->last_updated.tv_sec;
1238         
1239         return st->first_entry_t / 1000000;
1240 }