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