forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrrd.c
1630 lines (1324 loc) · 60.2 KB
/
rrd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#define RRD_DEFAULT_GAP_INTERPOLATIONS 1
// ----------------------------------------------------------------------------
// globals
/*
// if not zero it gives the time (in seconds) to remove un-updated dimensions
// DO NOT ENABLE
// if dimensions are removed, the chart generation will have to run again
int rrd_delete_unupdated_dimensions = 0;
*/
int rrd_update_every = UPDATE_EVERY;
int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
static int rrdset_compare(void* a, void* b);
static int rrdset_compare_name(void* a, void* b);
static int rrdfamily_compare(void *a, void *b);
// ----------------------------------------------------------------------------
// RRDHOST
RRDHOST localhost = {
.hostname = "localhost",
.rrdset_root = NULL,
.rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
.rrdset_root_index = {
{ NULL, rrdset_compare },
AVL_LOCK_INITIALIZER
},
.rrdset_root_index_name = {
{ NULL, rrdset_compare_name },
AVL_LOCK_INITIALIZER
},
.rrdfamily_root_index = {
{ NULL, rrdfamily_compare },
AVL_LOCK_INITIALIZER
},
.variables_root_index = {
{ NULL, rrdvar_compare },
AVL_LOCK_INITIALIZER
},
.health_log = {
.next_log_id = 1,
.next_alarm_id = 1,
.count = 0,
.max = 1000,
.alarms = NULL,
.alarm_log_rwlock = PTHREAD_RWLOCK_INITIALIZER
}
};
void rrdhost_init(char *hostname) {
localhost.hostname = hostname;
localhost.health_log.next_log_id =
localhost.health_log.next_alarm_id = now_realtime_sec();
}
void rrdhost_rwlock(RRDHOST *host) {
pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
}
void rrdhost_rdlock(RRDHOST *host) {
pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
}
void rrdhost_unlock(RRDHOST *host) {
pthread_rwlock_unlock(&host->rrdset_root_rwlock);
}
void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
if(ret == 0)
fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
}
void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
if(ret == 0)
fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
}
// ----------------------------------------------------------------------------
// RRDFAMILY index
static int rrdfamily_compare(void *a, void *b) {
if(((RRDFAMILY *)a)->hash_family < ((RRDFAMILY *)b)->hash_family) return -1;
else if(((RRDFAMILY *)a)->hash_family > ((RRDFAMILY *)b)->hash_family) return 1;
else return strcmp(((RRDFAMILY *)a)->family, ((RRDFAMILY *)b)->family);
}
#define rrdfamily_index_add(host, rc) (RRDFAMILY *)avl_insert_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
#define rrdfamily_index_del(host, rc) (RRDFAMILY *)avl_remove_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
static RRDFAMILY *rrdfamily_index_find(RRDHOST *host, const char *id, uint32_t hash) {
RRDFAMILY tmp;
tmp.family = id;
tmp.hash_family = (hash)?hash:simple_hash(tmp.family);
return (RRDFAMILY *)avl_search_lock(&(host->rrdfamily_root_index), (avl *) &tmp);
}
RRDFAMILY *rrdfamily_create(const char *id) {
RRDFAMILY *rc = rrdfamily_index_find(&localhost, id, 0);
if(!rc) {
rc = callocz(1, sizeof(RRDFAMILY));
rc->family = strdupz(id);
rc->hash_family = simple_hash(rc->family);
// initialize the variables index
avl_init_lock(&rc->variables_root_index, rrdvar_compare);
RRDFAMILY *ret = rrdfamily_index_add(&localhost, rc);
if(ret != rc)
fatal("INTERNAL ERROR: Expected to INSERT RRDFAMILY '%s' into index, but inserted '%s'.", rc->family, (ret)?ret->family:"NONE");
}
rc->use_count++;
return rc;
}
void rrdfamily_free(RRDFAMILY *rc) {
rc->use_count--;
if(!rc->use_count) {
RRDFAMILY *ret = rrdfamily_index_del(&localhost, rc);
if(ret != rc)
fatal("INTERNAL ERROR: Expected to DELETE RRDFAMILY '%s' from index, but deleted '%s'.", rc->family, (ret)?ret->family:"NONE");
if(rc->variables_root_index.avl_tree.root != NULL)
fatal("INTERNAL ERROR: Variables index of RRDFAMILY '%s' that is freed, is not empty.", rc->family);
freez((void *)rc->family);
freez(rc);
}
}
// ----------------------------------------------------------------------------
// RRDSET index
static int rrdset_compare(void* a, void* b) {
if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
}
#define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
#define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
RRDSET tmp;
strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
tmp.hash = (hash)?hash:simple_hash(tmp.id);
return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
}
// ----------------------------------------------------------------------------
// RRDSET name index
#define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
static int rrdset_compare_name(void* a, void* b) {
RRDSET *A = rrdset_from_avlname(a);
RRDSET *B = rrdset_from_avlname(b);
// fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
if(A->hash_name < B->hash_name) return -1;
else if(A->hash_name > B->hash_name) return 1;
else return strcmp(A->name, B->name);
}
RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
void *result;
// fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
if(result) return rrdset_from_avlname(result);
return NULL;
}
RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
void *result;
// fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
if(result) return rrdset_from_avlname(result);
return NULL;
}
static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
void *result = NULL;
RRDSET tmp;
tmp.name = name;
tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
// fprintf(stderr, "SEARCHING: %s\n", name);
result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
if(result) {
RRDSET *st = rrdset_from_avlname(result);
if(strcmp(st->magic, RRDSET_MAGIC))
error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
// fprintf(stderr, "FOUND: %s\n", name);
return rrdset_from_avlname(result);
}
// fprintf(stderr, "NOT FOUND: %s\n", name);
return NULL;
}
// ----------------------------------------------------------------------------
// RRDDIM index
static int rrddim_compare(void* a, void* b) {
if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
}
#define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
#define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
RRDDIM tmp;
strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
tmp.hash = (hash)?hash:simple_hash(tmp.id);
return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
}
// ----------------------------------------------------------------------------
// chart types
int rrdset_type_id(const char *name)
{
if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
return RRDSET_TYPE_LINE;
}
const char *rrdset_type_name(int chart_type)
{
static char line[] = RRDSET_TYPE_LINE_NAME;
static char area[] = RRDSET_TYPE_AREA_NAME;
static char stacked[] = RRDSET_TYPE_STACKED_NAME;
switch(chart_type) {
case RRDSET_TYPE_LINE:
return line;
case RRDSET_TYPE_AREA:
return area;
case RRDSET_TYPE_STACKED:
return stacked;
}
return line;
}
// ----------------------------------------------------------------------------
// load / save
const char *rrd_memory_mode_name(int id)
{
static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
switch(id) {
case RRD_MEMORY_MODE_RAM:
return ram;
case RRD_MEMORY_MODE_MAP:
return map;
case RRD_MEMORY_MODE_SAVE:
default:
return save;
}
return save;
}
int rrd_memory_mode_id(const char *name)
{
if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
return RRD_MEMORY_MODE_RAM;
else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
return RRD_MEMORY_MODE_MAP;
return RRD_MEMORY_MODE_SAVE;
}
// ----------------------------------------------------------------------------
// algorithms types
int rrddim_algorithm_id(const char *name)
{
if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
return RRDDIM_ABSOLUTE;
}
const char *rrddim_algorithm_name(int chart_type)
{
static char absolute[] = RRDDIM_ABSOLUTE_NAME;
static char incremental[] = RRDDIM_INCREMENTAL_NAME;
static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
switch(chart_type) {
case RRDDIM_ABSOLUTE:
return absolute;
case RRDDIM_INCREMENTAL:
return incremental;
case RRDDIM_PCENT_OVER_ROW_TOTAL:
return percentage_of_absolute_row;
case RRDDIM_PCENT_OVER_DIFF_TOTAL:
return percentage_of_incremental_row;
}
return absolute;
}
// ----------------------------------------------------------------------------
// chart names
char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
{
char c, *p = to;
while (length-- && (c = *from++)) {
if(c != '.' && !isalnum(c))
c = '_';
*p++ = c;
}
*p = '\0';
return to;
}
void rrdset_set_name(RRDSET *st, const char *name)
{
if(unlikely(st->name && !strcmp(st->name, name)))
return;
debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
char b[CONFIG_MAX_VALUE + 1];
char n[RRD_ID_LENGTH_MAX + 1];
snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
if(st->name) {
rrdset_index_del_name(&localhost, st);
st->name = config_set_default(st->id, "name", b);
st->hash_name = simple_hash(st->name);
rrdsetvar_rename_all(st);
}
else {
st->name = config_get(st->id, "name", b);
st->hash_name = simple_hash(st->name);
}
pthread_rwlock_wrlock(&st->rwlock);
RRDDIM *rd;
for(rd = st->dimensions; rd ;rd = rd->next)
rrddimvar_rename_all(rd);
pthread_rwlock_unlock(&st->rwlock);
rrdset_index_add_name(&localhost, st);
}
// ----------------------------------------------------------------------------
// cache directory
char *rrdset_cache_dir(const char *id)
{
char *ret = NULL;
static char *cache_dir = NULL;
if(!cache_dir) {
cache_dir = config_get("global", "cache directory", CACHE_DIR);
int r = mkdir(cache_dir, 0755);
if(r != 0 && errno != EEXIST)
error("Cannot create directory '%s'", cache_dir);
}
char b[FILENAME_MAX + 1];
char n[FILENAME_MAX + 1];
rrdset_strncpyz_name(b, id, FILENAME_MAX);
snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
ret = config_get(id, "cache directory", n);
if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
int r = mkdir(ret, 0775);
if(r != 0 && errno != EEXIST)
error("Cannot create directory '%s'", ret);
}
return ret;
}
// ----------------------------------------------------------------------------
// core functions
void rrdset_reset(RRDSET *st)
{
debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
st->last_collected_time.tv_sec = 0;
st->last_collected_time.tv_usec = 0;
st->last_updated.tv_sec = 0;
st->last_updated.tv_usec = 0;
st->current_entry = 0;
st->counter = 0;
st->counter_done = 0;
RRDDIM *rd;
for(rd = st->dimensions; rd ; rd = rd->next) {
rd->last_collected_time.tv_sec = 0;
rd->last_collected_time.tv_usec = 0;
rd->counter = 0;
memset(rd->values, 0, rd->entries * sizeof(storage_number));
}
}
static inline long align_entries_to_pagesize(long entries) {
if(entries < 5) entries = 5;
if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
#ifdef NETDATA_LOG_ALLOCATIONS
long page = (size_t)sysconf(_SC_PAGESIZE);
long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
if(size % page) {
size -= (size % page);
size += page;
long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
return n;
}
return entries;
#else
return entries;
#endif
}
static inline void timeval_align(struct timeval *tv, int update_every) {
tv->tv_sec -= tv->tv_sec % update_every;
tv->tv_usec = 500000;
}
RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *context, const char *title, const char *units, long priority, int update_every, int chart_type)
{
if(!type || !type[0]) {
fatal("Cannot create rrd stats without a type.");
return NULL;
}
if(!id || !id[0]) {
fatal("Cannot create rrd stats without an id.");
return NULL;
}
char fullid[RRD_ID_LENGTH_MAX + 1];
char fullfilename[FILENAME_MAX + 1];
snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
RRDSET *st = rrdset_find(fullid);
if(st) {
error("Cannot create rrd stats for '%s', it already exists.", fullid);
return st;
}
long rentries = config_get_number(fullid, "history", rrd_default_history_entries);
long entries = align_entries_to_pagesize(rentries);
if(entries != rentries) entries = config_set_number(fullid, "history", entries);
int enabled = config_get_boolean(fullid, "enabled", 1);
if(!enabled) entries = 5;
unsigned long size = sizeof(RRDSET);
char *cache_dir = rrdset_cache_dir(fullid);
debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 0);
if(st) {
if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
errno = 0;
info("Initializing file %s.", fullfilename);
memset(st, 0, size);
}
else if(strcmp(st->id, fullid) != 0) {
errno = 0;
error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
// munmap(st, size);
// st = NULL;
memset(st, 0, size);
}
else if(st->memsize != size || st->entries != entries) {
errno = 0;
error("File %s does not have the desired size. Clearing it.", fullfilename);
memset(st, 0, size);
}
else if(st->update_every != update_every) {
errno = 0;
error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
memset(st, 0, size);
}
else if((now_realtime_sec() - st->last_updated.tv_sec) > update_every * entries) {
errno = 0;
error("File %s is too old. Clearing it.", fullfilename);
memset(st, 0, size);
}
// make sure the database is aligned
if(st->last_updated.tv_sec)
timeval_align(&st->last_updated, update_every);
}
if(st) {
st->name = NULL;
st->type = NULL;
st->family = NULL;
st->context = NULL;
st->title = NULL;
st->units = NULL;
st->dimensions = NULL;
st->next = NULL;
st->mapped = rrd_memory_mode;
st->variables = NULL;
st->alarms = NULL;
}
else {
st = callocz(1, size);
st->mapped = RRD_MEMORY_MODE_RAM;
}
st->memsize = size;
st->entries = entries;
st->update_every = update_every;
if(st->current_entry >= st->entries) st->current_entry = 0;
strcpy(st->cache_filename, fullfilename);
strcpy(st->magic, RRDSET_MAGIC);
strcpy(st->id, fullid);
st->hash = simple_hash(st->id);
st->cache_dir = cache_dir;
st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
st->type = config_get(st->id, "type", type);
st->family = config_get(st->id, "family", family?family:st->type);
st->units = config_get(st->id, "units", units?units:"");
st->context = config_get(st->id, "context", context?context:st->id);
st->hash_context = simple_hash(st->context);
st->priority = config_get_number(st->id, "priority", priority);
st->enabled = enabled;
st->isdetail = 0;
st->debug = 0;
// if(!strcmp(st->id, "disk_util.dm-0")) {
// st->debug = 1;
// error("enabled debugging for '%s'", st->id);
// }
// else error("not enabled debugging for '%s'", st->id);
st->green = NAN;
st->red = NAN;
st->last_collected_time.tv_sec = 0;
st->last_collected_time.tv_usec = 0;
st->counter_done = 0;
st->gap_when_lost_iterations_above = (int) (
config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
avl_init_lock(&st->dimensions_index, rrddim_compare);
avl_init_lock(&st->variables_root_index, rrdvar_compare);
pthread_rwlock_init(&st->rwlock, NULL);
rrdhost_rwlock(&localhost);
if(name && *name) rrdset_set_name(st, name);
else rrdset_set_name(st, id);
{
char varvalue[CONFIG_MAX_VALUE + 1];
snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
st->title = config_get(st->id, "title", varvalue);
}
st->rrdfamily = rrdfamily_create(st->family);
st->rrdhost = &localhost;
st->next = localhost.rrdset_root;
localhost.rrdset_root = st;
if(health_enabled) {
rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
}
rrdset_index_add(&localhost, st);
rrdsetcalc_link_matching(st);
rrdcalctemplate_link_matching(st);
rrdhost_unlock(&localhost);
return(st);
}
RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
{
char filename[FILENAME_MAX + 1];
char fullfilename[FILENAME_MAX + 1];
char varname[CONFIG_MAX_NAME + 1];
RRDDIM *rd = NULL;
unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
rrdset_strncpyz_name(filename, id, FILENAME_MAX);
snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 1);
if(rd) {
struct timeval now;
now_realtime_timeval(&now);
if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
errno = 0;
info("Initializing file %s.", fullfilename);
memset(rd, 0, size);
}
else if(rd->memsize != size) {
errno = 0;
error("File %s does not have the desired size. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(rd->multiplier != multiplier) {
errno = 0;
error("File %s does not have the same multiplier. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(rd->divisor != divisor) {
errno = 0;
error("File %s does not have the same divisor. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(rd->algorithm != algorithm) {
errno = 0;
error("File %s does not have the same algorithm. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(rd->update_every != st->update_every) {
errno = 0;
error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
errno = 0;
error("File %s is too old. Clearing it.", fullfilename);
memset(rd, 0, size);
}
else if(strcmp(rd->id, id) != 0) {
errno = 0;
error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
// munmap(rd, size);
// rd = NULL;
memset(rd, 0, size);
}
}
if(rd) {
// we have a file mapped for rd
rd->mapped = rrd_memory_mode;
rd->flags = 0x00000000;
rd->variables = NULL;
rd->next = NULL;
rd->name = NULL;
}
else {
// if we didn't manage to get a mmap'd dimension, just create one
rd = callocz(1, size);
rd->mapped = RRD_MEMORY_MODE_RAM;
}
rd->memsize = size;
strcpy(rd->magic, RRDDIMENSION_MAGIC);
strcpy(rd->cache_filename, fullfilename);
strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
rd->hash = simple_hash(rd->id);
snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
rd->multiplier = config_get_number(st->id, varname, multiplier);
snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
rd->divisor = config_get_number(st->id, varname, divisor);
if(!rd->divisor) rd->divisor = 1;
rd->entries = st->entries;
rd->update_every = st->update_every;
// prevent incremental calculation spikes
rd->counter = 0;
rd->updated = 0;
rd->calculated_value = 0;
rd->last_calculated_value = 0;
rd->collected_value = 0;
rd->last_collected_value = 0;
rd->collected_volume = 0;
rd->stored_volume = 0;
rd->last_stored_value = 0;
rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
rd->last_collected_time.tv_sec = 0;
rd->last_collected_time.tv_usec = 0;
rd->rrdset = st;
// append this dimension
pthread_rwlock_wrlock(&st->rwlock);
if(!st->dimensions)
st->dimensions = rd;
else {
RRDDIM *td = st->dimensions;
for(; td->next; td = td->next) ;
td->next = rd;
}
if(health_enabled) {
rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
}
pthread_rwlock_unlock(&st->rwlock);
rrddim_index_add(st, rd);
return(rd);
}
void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
{
if(unlikely(rd->name && !strcmp(rd->name, name)))
return;
debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
char varname[CONFIG_MAX_NAME + 1];
snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
rd->name = config_set_default(st->id, varname, name);
rrddimvar_rename_all(rd);
}
void rrddim_free(RRDSET *st, RRDDIM *rd)
{
debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
if(rd == st->dimensions)
st->dimensions = rd->next;
else {
RRDDIM *i;
for (i = st->dimensions; i && i->next != rd; i = i->next) ;
if (i && i->next == rd)
i->next = rd->next;
else
error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
}
rd->next = NULL;
while(rd->variables)
rrddimvar_free(rd->variables);
rrddim_index_del(st, rd);
// free(rd->annotations);
if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
savememory(rd->cache_filename, rd, rd->memsize);
debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
munmap(rd, rd->memsize);
}
else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
munmap(rd, rd->memsize);
}
else {
debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
freez(rd);
}
}
void rrdset_free_all(void)
{
info("Freeing all memory...");
rrdhost_rwlock(&localhost);
RRDSET *st;
for(st = localhost.rrdset_root; st ;) {
RRDSET *next = st->next;
pthread_rwlock_wrlock(&st->rwlock);
while(st->variables)
rrdsetvar_free(st->variables);
while(st->alarms)
rrdsetcalc_unlink(st->alarms);
while(st->dimensions)
rrddim_free(st, st->dimensions);
rrdset_index_del(&localhost, st);
st->rrdfamily->use_count--;
if(!st->rrdfamily->use_count)
rrdfamily_free(st->rrdfamily);
pthread_rwlock_unlock(&st->rwlock);
if(st->mapped == RRD_MEMORY_MODE_SAVE) {
debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
savememory(st->cache_filename, st, st->memsize);
debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
munmap(st, st->memsize);
}
else if(st->mapped == RRD_MEMORY_MODE_MAP) {
debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
munmap(st, st->memsize);
}
else
freez(st);
st = next;
}
localhost.rrdset_root = NULL;
rrdhost_unlock(&localhost);
info("Memory cleanup completed...");
}
void rrdset_save_all(void) {
info("Saving database...");
RRDSET *st;
RRDDIM *rd;
rrdhost_rwlock(&localhost);
for(st = localhost.rrdset_root; st ; st = st->next) {
pthread_rwlock_wrlock(&st->rwlock);
if(st->mapped == RRD_MEMORY_MODE_SAVE) {
debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
savememory(st->cache_filename, st, st->memsize);
}
for(rd = st->dimensions; rd ; rd = rd->next) {
if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
savememory(rd->cache_filename, rd, rd->memsize);
}
}
pthread_rwlock_unlock(&st->rwlock);
}
rrdhost_unlock(&localhost);
}
RRDSET *rrdset_find(const char *id)
{
debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
RRDSET *st = rrdset_index_find(&localhost, id, 0);
return(st);
}
RRDSET *rrdset_find_bytype(const char *type, const char *id)
{
debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
char buf[RRD_ID_LENGTH_MAX + 1];
strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
strcat(buf, ".");
int len = (int) strlen(buf);
strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
return(rrdset_find(buf));
}
RRDSET *rrdset_find_byname(const char *name)
{
debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
return(st);
}
RRDDIM *rrddim_find(RRDSET *st, const char *id)
{
debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
return rrddim_index_find(st, id, 0);
}
int rrddim_hide(RRDSET *st, const char *id)
{
debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
RRDDIM *rd = rrddim_find(st, id);
if(unlikely(!rd)) {
error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
return 1;
}
rd->flags |= RRDDIM_FLAG_HIDDEN;
return 0;
}
int rrddim_unhide(RRDSET *st, const char *id)
{
debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
RRDDIM *rd = rrddim_find(st, id);
if(unlikely(!rd)) {
error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
return 1;
}
if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
return 0;
}
collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
{
debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
now_realtime_timeval(&rd->last_collected_time);
rd->collected_value = value;
rd->updated = 1;
rd->counter++;
// fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
return rd->last_collected_value;
}
collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
{
RRDDIM *rd = rrddim_find(st, id);
if(unlikely(!rd)) {
error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
return 0;
}
return rrddim_set_by_pointer(st, rd, value);
}