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