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