]> arthur.barton.de Git - netdata.git/blob - src/rrd.c
splitted netdata to multiple source files
[netdata.git] / src / rrd.c
1 #include <string.h>
2 #include <unistd.h>
3 #include <time.h>
4 #include <sys/time.h>
5 #include <sys/mman.h>
6 #include <pthread.h>
7 #include <errno.h>
8 #include <ctype.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <stdlib.h>
12
13 #include "log.h"
14 #include "config.h"
15 #include "common.h"
16
17 #include "rrd.h"
18
19 int update_every = UPDATE_EVERY;
20 int save_history = HISTORY;
21
22 // ----------------------------------------------------------------------------
23 // chart types
24
25 int chart_type_id(const char *name)
26 {
27         if(strcmp(name, CHART_TYPE_AREA_NAME) == 0) return CHART_TYPE_AREA;
28         if(strcmp(name, CHART_TYPE_STACKED_NAME) == 0) return CHART_TYPE_STACKED;
29         if(strcmp(name, CHART_TYPE_LINE_NAME) == 0) return CHART_TYPE_LINE;
30         return CHART_TYPE_LINE;
31 }
32
33 const char *chart_type_name(int chart_type)
34 {
35         static char line[] = CHART_TYPE_LINE_NAME;
36         static char area[] = CHART_TYPE_AREA_NAME;
37         static char stacked[] = CHART_TYPE_STACKED_NAME;
38
39         switch(chart_type) {
40                 case CHART_TYPE_LINE:
41                         return line;
42
43                 case CHART_TYPE_AREA:
44                         return area;
45
46                 case CHART_TYPE_STACKED:
47                         return stacked;
48         }
49         return line;
50 }
51
52 // ----------------------------------------------------------------------------
53 // mmap() wrapper
54
55 int memory_mode = NETDATA_MEMORY_MODE_SAVE;
56
57 const char *memory_mode_name(int id)
58 {
59         static const char ram[] = NETDATA_MEMORY_MODE_RAM_NAME;
60         static const char map[] = NETDATA_MEMORY_MODE_MAP_NAME;
61         static const char save[] = NETDATA_MEMORY_MODE_SAVE_NAME;
62
63         switch(id) {
64                 case NETDATA_MEMORY_MODE_RAM:
65                         return ram;
66
67                 case NETDATA_MEMORY_MODE_MAP:
68                         return map;
69
70                 case NETDATA_MEMORY_MODE_SAVE:
71                 default:
72                         return save;
73         }
74
75         return save;
76 }
77
78 int memory_mode_id(const char *name)
79 {
80         if(!strcmp(name, NETDATA_MEMORY_MODE_RAM_NAME))
81                 return NETDATA_MEMORY_MODE_RAM;
82         else if(!strcmp(name, NETDATA_MEMORY_MODE_MAP_NAME))
83                 return NETDATA_MEMORY_MODE_MAP;
84
85         return NETDATA_MEMORY_MODE_SAVE;
86 }
87
88 // ----------------------------------------------------------------------------
89 // algorithms types
90
91 int algorithm_id(const char *name)
92 {
93         if(strcmp(name, RRD_DIMENSION_ABSOLUTE_NAME) == 0)                      return RRD_DIMENSION_ABSOLUTE;
94         if(strcmp(name, RRD_DIMENSION_INCREMENTAL_NAME) == 0)                   return RRD_DIMENSION_INCREMENTAL;
95         if(strcmp(name, RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME) == 0)          return RRD_DIMENSION_PCENT_OVER_ROW_TOTAL;
96         if(strcmp(name, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME) == 0)         return RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL;
97         return RRD_DIMENSION_ABSOLUTE;
98 }
99
100 const char *algorithm_name(int chart_type)
101 {
102         static char absolute[] = RRD_DIMENSION_ABSOLUTE_NAME;
103         static char incremental[] = RRD_DIMENSION_INCREMENTAL_NAME;
104         static char percentage_of_absolute_row[] = RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME;
105         static char percentage_of_incremental_row[] = RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME;
106
107         switch(chart_type) {
108                 case RRD_DIMENSION_ABSOLUTE:
109                         return absolute;
110
111                 case RRD_DIMENSION_INCREMENTAL:
112                         return incremental;
113
114                 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
115                         return percentage_of_absolute_row;
116
117                 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
118                         return percentage_of_incremental_row;
119         }
120         return absolute;
121 }
122
123 RRD_STATS *root = NULL;
124 pthread_rwlock_t root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
125
126 char *rrd_stats_strncpy_name(char *to, const char *from, int length)
127 {
128         int i;
129         for(i = 0; i < length && from[i] ;i++) {
130                 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
131                 else to[i] = '_';
132         }
133         if(i < length) to[i] = '\0';
134         to[length - 1] = '\0';
135
136         return to;
137 }
138
139 void rrd_stats_set_name(RRD_STATS *st, const char *name)
140 {
141         char b[CONFIG_MAX_VALUE + 1];
142         char n[RRD_STATS_NAME_MAX + 1];
143
144         snprintf(n, RRD_STATS_NAME_MAX, "%s.%s", st->type, name);
145         rrd_stats_strncpy_name(b, n, CONFIG_MAX_VALUE);
146         st->name = config_get(st->id, "name", b);
147         st->hash_name = simple_hash(st->name);
148 }
149
150 char *rrd_stats_cache_dir(const char *id)
151 {
152         char *ret = NULL;
153
154         static char *cache_dir = NULL;
155         if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
156
157         char b[FILENAME_MAX + 1];
158         char n[FILENAME_MAX + 1];
159         rrd_stats_strncpy_name(b, id, FILENAME_MAX);
160
161         snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
162         ret = config_get(id, "database directory", n);
163
164         if(memory_mode == NETDATA_MEMORY_MODE_MAP || memory_mode == NETDATA_MEMORY_MODE_SAVE) {
165                 int r = mkdir(ret, 0775);
166                 if(r != 0 && errno != EEXIST)
167                         error("Cannot create directory '%s'", ret);
168         }
169
170         return ret;
171 }
172
173 void rrd_stats_reset(RRD_STATS *st)
174 {
175         st->last_collected_time.tv_sec = 0;
176         st->last_collected_time.tv_usec = 0;
177         st->current_entry = 0;
178         st->counter_done = 0;
179
180         RRD_DIMENSION *rd;
181         for(rd = st->dimensions; rd ; rd = rd->next) {
182                 rd->last_collected_time.tv_sec = 0;
183                 rd->last_collected_time.tv_usec = 0;
184                 rd->current_entry = 0;
185         }
186 }
187
188 RRD_STATS *rrd_stats_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)
189 {
190         if(!id || !id[0]) {
191                 fatal("Cannot create rrd stats without an id.");
192                 return NULL;
193         }
194
195         char fullid[RRD_STATS_NAME_MAX + 1];
196         char fullfilename[FILENAME_MAX + 1];
197         RRD_STATS *st = NULL;
198
199         snprintf(fullid, RRD_STATS_NAME_MAX, "%s.%s", type, id);
200
201         long entries = config_get_number(fullid, "history", save_history);
202         if(entries < 5) entries = config_set_number(fullid, "history", 5);
203         if(entries > HISTORY_MAX) entries = config_set_number(fullid, "history", HISTORY_MAX);
204
205         int enabled = config_get_boolean(fullid, "enabled", 1);
206         if(!enabled) entries = 5;
207
208         unsigned long size = sizeof(RRD_STATS);
209         char *cache_dir = rrd_stats_cache_dir(fullid);
210
211         debug(D_RRD_STATS, "Creating RRD_STATS for '%s.%s'.", type, id);
212
213         snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
214         if(memory_mode != NETDATA_MEMORY_MODE_RAM) st = (RRD_STATS *)mymmap(fullfilename, size, ((memory_mode == NETDATA_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
215         if(st) {
216                 if(strcmp(st->magic, RRD_STATS_MAGIC) != 0) {
217                         errno = 0;
218                         error("File %s does not have our version. Clearing it.", fullfilename);
219                         bzero(st, size);
220                 }
221                 else if(strcmp(st->id, fullid) != 0) {
222                         errno = 0;
223                         error("File %s does not have our id. Unmapping it.", fullfilename);
224                         munmap(st, size);
225                         st = NULL;
226                 }
227                 else if(st->memsize != size || st->entries != entries) {
228                         errno = 0;
229                         error("File %s does not have the desired size. Clearing it.", fullfilename);
230                         bzero(st, size);
231                 }
232                 else if(st->update_every != update_every) {
233                         errno = 0;
234                         error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
235                         bzero(st, size);
236                 }
237         }
238
239         if(st) {
240                 st->name = NULL;
241                 st->type = NULL;
242                 st->family = NULL;
243                 st->title = NULL;
244                 st->units = NULL;
245                 st->dimensions = NULL;
246                 st->next = NULL;
247                 st->mapped = memory_mode;
248         }
249         else {
250                 st = calloc(1, size);
251                 if(!st) {
252                         fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
253                         return NULL;
254                 }
255                 st->mapped = NETDATA_MEMORY_MODE_RAM;
256         }
257         st->memsize = size;
258         st->entries = entries;
259         st->update_every = update_every;
260
261         strcpy(st->cache_file, fullfilename);
262         strcpy(st->magic, RRD_STATS_MAGIC);
263
264         strcpy(st->id, fullid);
265         st->hash = simple_hash(st->id);
266
267         st->cache_dir = cache_dir;
268
269         st->family     = config_get(st->id, "family", family?family:st->id);
270         st->units      = config_get(st->id, "units", units?units:"");
271         st->type       = config_get(st->id, "type", type);
272         st->chart_type = chart_type_id(config_get(st->id, "chart type", chart_type_name(chart_type)));
273
274         if(name && *name) rrd_stats_set_name(st, name);
275         else rrd_stats_set_name(st, id);
276
277         {
278                 char varvalue[CONFIG_MAX_VALUE + 1];
279                 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
280                 st->title = config_get(st->id, "title", varvalue);
281         }
282
283         st->priority = config_get_number(st->id, "priority", priority);
284         st->enabled = enabled;
285         
286         st->isdetail = 0;
287         st->debug = 0;
288
289         st->last_collected_time.tv_sec = 0;
290         st->last_collected_time.tv_usec = 0;
291         st->counter_done = 0;
292
293         pthread_rwlock_init(&st->rwlock, NULL);
294         pthread_rwlock_wrlock(&root_rwlock);
295
296         st->next = root;
297         root = st;
298
299         pthread_rwlock_unlock(&root_rwlock);
300
301         return(st);
302 }
303
304 RRD_DIMENSION *rrd_stats_dimension_add(RRD_STATS *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
305 {
306         char filename[FILENAME_MAX + 1];
307         char fullfilename[FILENAME_MAX + 1];
308
309         char varname[CONFIG_MAX_NAME + 1];
310         RRD_DIMENSION *rd = NULL;
311         unsigned long size = sizeof(RRD_DIMENSION) + (st->entries * sizeof(storage_number));
312
313         debug(D_RRD_STATS, "Adding dimension '%s/%s'.", st->id, id);
314
315         rrd_stats_strncpy_name(filename, id, FILENAME_MAX);
316         snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
317         if(memory_mode != NETDATA_MEMORY_MODE_RAM) rd = (RRD_DIMENSION *)mymmap(fullfilename, size, ((memory_mode == NETDATA_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
318         if(rd) {
319                 struct timeval now;
320                 gettimeofday(&now, NULL);
321
322                 if(strcmp(rd->magic, RRD_DIMENSION_MAGIC) != 0) {
323                         errno = 0;
324                         error("File %s does not have our version. Clearing it.", fullfilename);
325                         bzero(rd, size);
326                 }
327                 else if(rd->memsize != size) {
328                         errno = 0;
329                         error("File %s does not have the desired size. Clearing it.", fullfilename);
330                         bzero(rd, size);
331                 }
332                 else if(rd->multiplier != multiplier) {
333                         errno = 0;
334                         error("File %s does not have the same multiplier. Clearing it.", fullfilename);
335                         bzero(rd, size);
336                 }
337                 else if(rd->divisor != divisor) {
338                         errno = 0;
339                         error("File %s does not have the same divisor. Clearing it.", fullfilename);
340                         bzero(rd, size);
341                 }
342                 else if(rd->algorithm != algorithm) {
343                         errno = 0;
344                         error("File %s does not have the same algorithm. Clearing it.", fullfilename);
345                         bzero(rd, size);
346                 }
347                 else if(rd->update_every != st->update_every) {
348                         errno = 0;
349                         error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
350                         bzero(rd, size);
351                 }
352                 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
353                         errno = 0;
354                         error("File %s is too old. Clearing it.", fullfilename);
355                         bzero(rd, size);
356                 }
357                 else if(strcmp(rd->id, id) != 0) {
358                         errno = 0;
359                         error("File %s does not have our dimension id. Unmapping it.", fullfilename);
360                         munmap(rd, size);
361                         rd = NULL;
362                 }
363         }
364
365         if(rd) {
366                 // we have a file mapped for rd
367                 rd->mapped = memory_mode;
368                 rd->hidden = 0;
369                 rd->next = NULL;
370                 rd->name = NULL;
371         }
372         else {
373                 // if we didn't manage to get a mmap'd dimension, just create one
374
375                 rd = calloc(1, size);
376                 if(!rd) {
377                         fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
378                         return NULL;
379                 }
380
381                 rd->mapped = NETDATA_MEMORY_MODE_RAM;
382         }
383         rd->memsize = size;
384
385         strcpy(rd->magic, RRD_DIMENSION_MAGIC);
386         strcpy(rd->cache_file, fullfilename);
387         strncpy(rd->id, id, RRD_STATS_NAME_MAX);
388         rd->hash = simple_hash(rd->id);
389
390         snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
391         rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
392
393         snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
394         rd->algorithm = algorithm_id(config_get(st->id, varname, algorithm_name(algorithm)));
395
396         snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
397         rd->multiplier = config_get_number(st->id, varname, multiplier);
398
399         snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
400         rd->divisor = config_get_number(st->id, varname, divisor);
401         if(!rd->divisor) rd->divisor = 1;
402
403         rd->entries = st->entries;
404         rd->update_every = st->update_every;
405         
406         // append this dimension
407         if(!st->dimensions)
408                 st->dimensions = rd;
409         else {
410                 RRD_DIMENSION *td = st->dimensions;
411                 for(; td->next; td = td->next) ;
412                 td->next = rd;
413         }
414
415         return(rd);
416 }
417
418 void rrd_stats_dimension_set_name(RRD_STATS *st, RRD_DIMENSION *rd, const char *name)
419 {
420         char varname[CONFIG_MAX_NAME + 1];
421         snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
422         config_get(st->id, varname, name);
423 }
424
425 void rrd_stats_dimension_free(RRD_DIMENSION *rd)
426 {
427         if(rd->next) rrd_stats_dimension_free(rd->next);
428         // free(rd->annotations);
429         if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
430                 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
431                 savememory(rd->cache_file, rd, rd->memsize);
432
433                 debug(D_RRD_STATS, "Unmapping dimension '%s'.", rd->name);
434                 munmap(rd, rd->memsize);
435         }
436         else if(rd->mapped == NETDATA_MEMORY_MODE_MAP) {
437                 debug(D_RRD_STATS, "Unmapping dimension '%s'.", rd->name);
438                 munmap(rd, rd->memsize);
439         }
440         else {
441                 debug(D_RRD_STATS, "Removing dimension '%s'.", rd->name);
442                 free(rd);
443         }
444 }
445
446 void rrd_stats_free_all(void)
447 {
448         info("Freeing all memory...");
449
450         RRD_STATS *st;
451         for(st = root; st ;) {
452                 RRD_STATS *next = st->next;
453
454                 if(st->dimensions) rrd_stats_dimension_free(st->dimensions);
455                 st->dimensions = NULL;
456
457                 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
458                         debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
459                         savememory(st->cache_file, st, st->memsize);
460
461                         debug(D_RRD_STATS, "Unmapping stats '%s'.", st->name);
462                         munmap(st, st->memsize);
463                 }
464                 else if(st->mapped == NETDATA_MEMORY_MODE_MAP) {
465                         debug(D_RRD_STATS, "Unmapping stats '%s'.", st->name);
466                         munmap(st, st->memsize);
467                 }
468                 else
469                         free(st);
470
471                 st = next;
472         }
473         root = NULL;
474
475         info("Memory cleanup completed...");
476 }
477
478 void rrd_stats_save_all(void)
479 {
480         RRD_STATS *st;
481         RRD_DIMENSION *rd;
482
483         pthread_rwlock_wrlock(&root_rwlock);
484         for(st = root; st ; st = st->next) {
485                 pthread_rwlock_wrlock(&st->rwlock);
486
487                 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
488                         debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
489                         savememory(st->cache_file, st, st->memsize);
490                 }
491
492                 for(rd = st->dimensions; rd ; rd = rd->next) {
493                         if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
494                                 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
495                                 savememory(rd->cache_file, rd, rd->memsize);
496                         }
497                 }
498
499                 pthread_rwlock_unlock(&st->rwlock);
500         }
501         pthread_rwlock_unlock(&root_rwlock);
502 }
503
504
505 RRD_STATS *rrd_stats_find(const char *id)
506 {
507         unsigned long hash = simple_hash(id);
508
509         pthread_rwlock_rdlock(&root_rwlock);
510         RRD_STATS *st = root;
511         for ( ; st ; st = st->next )
512                 if(hash == st->hash)
513                         if(strcmp(st->id, id) == 0)
514                                 break;
515         pthread_rwlock_unlock(&root_rwlock);
516
517         return(st);
518 }
519
520 RRD_STATS *rrd_stats_find_bytype(const char *type, const char *id)
521 {
522         char buf[RRD_STATS_NAME_MAX + 1];
523
524         strncpy(buf, type, RRD_STATS_NAME_MAX - 1);
525         buf[RRD_STATS_NAME_MAX - 1] = '\0';
526         strcat(buf, ".");
527         int len = strlen(buf);
528         strncpy(&buf[len], id, RRD_STATS_NAME_MAX - len);
529         buf[RRD_STATS_NAME_MAX] = '\0';
530
531         return(rrd_stats_find(buf));
532 }
533
534 RRD_STATS *rrd_stats_find_byname(const char *name)
535 {
536         char b[CONFIG_MAX_VALUE + 1];
537
538         rrd_stats_strncpy_name(b, name, CONFIG_MAX_VALUE);
539         unsigned long hash = simple_hash(b);
540
541         pthread_rwlock_rdlock(&root_rwlock);
542         RRD_STATS *st = root;
543         for ( ; st ; st = st->next ) {
544                 if(hash == st->hash_name && strcmp(st->name, b) == 0) break;
545         }
546         pthread_rwlock_unlock(&root_rwlock);
547
548         return(st);
549 }
550
551 RRD_DIMENSION *rrd_stats_dimension_find(RRD_STATS *st, const char *id)
552 {
553         unsigned long hash = simple_hash(id);
554
555         RRD_DIMENSION *rd = st->dimensions;
556
557         for ( ; rd ; rd = rd->next )
558                 if(hash == rd->hash)
559                         if(strcmp(rd->id, id) == 0)
560                                 break;
561
562         return(rd);
563 }
564
565 int rrd_stats_dimension_hide(RRD_STATS *st, const char *id)
566 {
567         RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
568         if(!rd) {
569                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
570                 return 1;
571         }
572
573         rd->hidden = 1;
574         return 0;
575 }
576
577 void rrd_stats_dimension_set_by_pointer(RRD_STATS *st, RRD_DIMENSION *rd, collected_number value)
578 {
579         if(st) {;}
580         
581         gettimeofday(&rd->last_collected_time, NULL);
582         rd->collected_value = value;
583 }
584
585 int rrd_stats_dimension_set(RRD_STATS *st, char *id, collected_number value)
586 {
587         RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
588         if(!rd) {
589                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
590                 return 1;
591         }
592
593         rrd_stats_dimension_set_by_pointer(st, rd, value);
594         return 0;
595 }
596
597 void rrd_stats_next_usec(RRD_STATS *st, unsigned long long microseconds)
598 {
599         if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
600         st->usec_since_last_update = microseconds;
601 }
602
603 void rrd_stats_next(RRD_STATS *st)
604 {
605         unsigned long long microseconds = 0;
606
607         if(st->last_collected_time.tv_sec) {
608                 struct timeval now;
609                 gettimeofday(&now, NULL);
610                 microseconds = usecdiff(&now, &st->last_collected_time);
611         }
612
613         rrd_stats_next_usec(st, microseconds);
614 }
615
616 void rrd_stats_next_plugins(RRD_STATS *st)
617 {
618         rrd_stats_next(st);
619 }
620
621 unsigned long long rrd_stats_done(RRD_STATS *st)
622 {
623         RRD_DIMENSION *rd, *last;
624         int oldstate;
625
626         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
627                 error("Cannot set pthread cancel state to DISABLE.");
628
629         // a read lock is OK here
630         pthread_rwlock_rdlock(&st->rwlock);
631
632         if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
633
634         if(!st->last_collected_time.tv_sec) gettimeofday(&st->last_collected_time, NULL);
635         else {
636                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
637                 st->last_collected_time.tv_sec = ut / 1000000ULL;
638                 st->last_collected_time.tv_usec = ut % 1000000ULL;
639         }
640
641         if(!st->last_updated.tv_sec) {
642                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
643                 st->last_updated.tv_sec = ut / 1000000ULL;
644                 st->last_updated.tv_usec = ut % 1000000ULL;
645
646                 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf)", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
647         }
648
649         unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
650         unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
651         unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
652
653         if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
654         if(st->debug) debug(D_RRD_STATS, "%s: now  ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
655         if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
656
657         st->counter_done++;
658
659         // calculate totals and count the dimensions
660         int dimensions;
661         st->collected_total = 0;
662         for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
663                 st->collected_total += rd->collected_value;
664
665         // process all dimensions to calculate their values
666         // based on the collected figures only
667         // at this stage we do not interpolate anything
668         for( rd = st->dimensions ; rd ; rd = rd->next ) {
669                 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
670                         " last_collected_value = " COLLECTED_NUMBER_FORMAT
671                         " collected_value = " COLLECTED_NUMBER_FORMAT
672                         " last_calculated_value = " CALCULATED_NUMBER_FORMAT
673                         " calculated_value = " CALCULATED_NUMBER_FORMAT
674                         , st->id, rd->name
675                         , rd->last_collected_value
676                         , rd->collected_value
677                         , rd->last_calculated_value
678                         , rd->calculated_value
679                         );
680
681                 switch(rd->algorithm) {
682                         case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
683                                 // the percentage of the current increment
684                                 // over the increment of all dimensions together
685                                 if(st->collected_total == st->last_collected_total) rd->calculated_value = 0;
686                                 else rd->calculated_value =
687                                           (calculated_number)100
688                                         * (calculated_number)(rd->collected_value - rd->last_collected_value)
689                                         / (calculated_number)(st->collected_total  - st->last_collected_total);
690
691                                 if(st->debug)
692                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
693                                                 CALCULATED_NUMBER_FORMAT " = 100"
694                                                 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
695                                                 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
696                                                 , st->id, rd->name
697                                                 , rd->calculated_value
698                                                 , rd->collected_value, rd->last_collected_value
699                                                 , st->collected_total, st->last_collected_total
700                                                 );
701                                 break;
702
703                         case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
704                                 if(!st->collected_total) rd->calculated_value = 0;
705                                 else
706                                 // the percentage of the current value
707                                 // over the total of all dimensions
708                                 rd->calculated_value =
709                                           (calculated_number)100
710                                         * (calculated_number)rd->collected_value
711                                         / (calculated_number)st->collected_total;
712
713                                 if(st->debug)
714                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
715                                                 CALCULATED_NUMBER_FORMAT " = 100"
716                                                 " * " COLLECTED_NUMBER_FORMAT
717                                                 " / " COLLECTED_NUMBER_FORMAT
718                                                 , st->id, rd->name
719                                                 , rd->calculated_value
720                                                 , rd->collected_value
721                                                 , st->collected_total
722                                                 );
723                                 break;
724
725                         case RRD_DIMENSION_INCREMENTAL:
726                                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
727                                 // to reset the calculation (it will give zero as the calculation for this second)
728                                 if(rd->last_collected_value > rd->collected_value) rd->last_collected_value = rd->collected_value;
729
730                                 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
731
732                                 if(st->debug)
733                                         debug(D_RRD_STATS, "%s/%s: CALC INC "
734                                                 CALCULATED_NUMBER_FORMAT " += "
735                                                 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
736                                                 , st->id, rd->name
737                                                 , rd->calculated_value
738                                                 , rd->collected_value, rd->last_collected_value
739                                                 );
740                                 break;
741
742                         case RRD_DIMENSION_ABSOLUTE:
743                                 rd->calculated_value = (calculated_number)rd->collected_value;
744
745                                 if(st->debug)
746                                         debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
747                                                 CALCULATED_NUMBER_FORMAT " = "
748                                                 COLLECTED_NUMBER_FORMAT
749                                                 , st->id, rd->name
750                                                 , rd->calculated_value
751                                                 , rd->collected_value
752                                                 );
753                                 break;
754
755                         default:
756                                 // make the default zero, to make sure
757                                 // it gets noticed when we add new types
758                                 rd->calculated_value = 0;
759
760                                 if(st->debug)
761                                         debug(D_RRD_STATS, "%s/%s: CALC "
762                                                 CALCULATED_NUMBER_FORMAT " = 0"
763                                                 , st->id, rd->name
764                                                 , rd->calculated_value
765                                                 );
766                                 break;
767                 }
768         }
769         // at this point we have all the calculated values ready
770
771         if(st->counter_done == 1 || next_ut > now_ut) {
772                 // we don't have any usable data yet
773                 if(st->debug) debug(D_RRD_STATS, "%s: Skipping collected values (usec since last update = %llu, counter_done = %lu)", st->name, st->usec_since_last_update, st->counter_done);
774
775                 for( rd = st->dimensions; rd ; rd = rd->next ) {
776                         rd->last_calculated_value = rd->calculated_value;
777                         rd->last_collected_value = rd->collected_value;
778
779                         switch(rd->algorithm) {
780                                 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
781                                 case RRD_DIMENSION_INCREMENTAL:
782                                         if(!st->usec_since_last_update) rd->calculated_value = 0;
783                                         // keep the previous values
784                                         // the next time, a new incremental total will be calculated
785                                         break;
786                         }
787
788                         rd->collected_value = 0;
789                 }
790                 st->last_collected_total  = st->collected_total;
791
792                 pthread_rwlock_unlock(&st->rwlock);
793                 if(pthread_setcancelstate(oldstate, NULL) != 0)
794                         error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
795
796                 return(st->usec_since_last_update);
797         }
798
799         // it is now time to interpolate values on a second boundary
800         unsigned long long first_ut = last_ut;
801         for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL ) {
802                 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
803                 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
804
805                 st->last_updated.tv_sec = next_ut / 1000000ULL;
806                 st->last_updated.tv_usec = 0;
807
808                 for( rd = st->dimensions ; rd ; rd = rd->next ) {
809                         calculated_number new_value;
810
811                         switch(rd->algorithm) {
812                                 case RRD_DIMENSION_INCREMENTAL:
813                                         new_value = (calculated_number)
814                                                 (          rd->calculated_value
815                                                         * (calculated_number)(next_ut - last_ut)
816                                                         / (calculated_number)(now_ut - last_ut)
817                                                 );
818
819                                         if(st->debug)
820                                                 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
821                                                         CALCULATED_NUMBER_FORMAT " = "
822                                                         CALCULATED_NUMBER_FORMAT
823                                                         " * %llu"
824                                                         " / %llu"
825                                                         , st->id, rd->name
826                                                         , new_value
827                                                         , rd->calculated_value
828                                                         , (unsigned long long)(next_ut - last_ut)
829                                                         , (unsigned long long)(now_ut - last_ut)
830                                                         );
831
832                                         rd->calculated_value -= new_value;
833                                         break;
834
835                                 case RRD_DIMENSION_ABSOLUTE:
836                                 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
837                                 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
838                                 default:
839                                         new_value = (calculated_number)
840                                                 (       (         (rd->calculated_value - rd->last_calculated_value)
841                                                                 * (calculated_number)(next_ut - first_ut)
842                                                                 / (calculated_number)(now_ut - first_ut)
843                                                         )
844                                                         +  rd->last_calculated_value
845                                                 );
846
847                                         if(st->debug)
848                                                 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
849                                                         CALCULATED_NUMBER_FORMAT " = ((("
850                                                         "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
851                                                         " * %llu"
852                                                         " / %llu) + " CALCULATED_NUMBER_FORMAT
853                                                         , st->id, rd->name
854                                                         , new_value
855                                                         , rd->calculated_value, rd->last_calculated_value
856                                                         , (next_ut - first_ut)
857                                                         , (now_ut - first_ut), rd->last_calculated_value
858                                                         );
859
860                                         if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
861                                         break;
862
863                         }
864
865
866                         rd->values[st->current_entry] = pack_storage_number(
867                                           new_value
868                                         * (calculated_number)rd->multiplier
869                                         / (calculated_number)rd->divisor
870                                 );
871
872                         if(st->debug)
873                                 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
874                                         CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
875                                         " * %ld"
876                                         " / %ld"
877                                         , st->id, rd->name
878                                         , st->current_entry
879                                         , unpack_storage_number(rd->values[st->current_entry]), new_value
880                                         , rd->multiplier
881                                         , rd->divisor
882                                         );
883                 }
884
885                 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
886                         // the db is overwriting values
887                         // add the value we will overwrite
888                         st->first_entry_t += st->update_every * 1000000ULL;
889                 }
890                 
891                 st->counter++;
892                 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
893                 if(!st->first_entry_t) st->first_entry_t = next_ut;
894                 last_ut = next_ut;
895         }
896
897         for( rd = st->dimensions; rd ; rd = rd->next ) {
898                 rd->last_collected_value = rd->collected_value;
899                 rd->last_calculated_value = rd->calculated_value;
900                 rd->collected_value = 0;
901         }
902         st->last_collected_total  = st->collected_total;
903
904         // ALL DONE ABOUT THE DATA UPDATE
905         // --------------------------------------------------------------------
906
907
908         // find if there are any obsolete dimensions (not updated recently)
909         for( rd = st->dimensions; rd ; rd = rd->next )
910                 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec)
911                         break;
912
913         if(rd) {
914                 // there is dimension to free
915                 // upgrade our read lock to a write lock
916                 pthread_rwlock_unlock(&st->rwlock);
917                 pthread_rwlock_wrlock(&st->rwlock);
918
919                 for( rd = st->dimensions, last = NULL ; rd ; ) {
920                         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
921                                 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
922
923                                 if(!last) {
924                                         st->dimensions = rd->next;
925                                         rd->next = NULL;
926                                         rrd_stats_dimension_free(rd);
927                                         rd = st->dimensions;
928                                         continue;
929                                 }
930                                 else {
931                                         last->next = rd->next;
932                                         rd->next = NULL;
933                                         rrd_stats_dimension_free(rd);
934                                         rd = last->next;
935                                         continue;
936                                 }
937                         }
938
939                         last = rd;
940                         rd = rd->next;
941                 }
942
943                 if(!st->dimensions) st->enabled = 0;
944         }
945
946         pthread_rwlock_unlock(&st->rwlock);
947
948         if(pthread_setcancelstate(oldstate, NULL) != 0)
949                 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
950
951         return(st->usec_since_last_update);
952 }
953
954
955 // find the oldest entry in the data, skipping all empty slots
956 time_t rrd_stats_first_entry_t(RRD_STATS *st)
957 {
958         if(!st->first_entry_t) return st->last_updated.tv_sec;
959         
960         return st->first_entry_t / 1000000;
961 }