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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
|
#define NETDATA_RRD_INTERNALS 1
#include "common.h"
#define RRD_DEFAULT_GAP_INTERPOLATIONS 1
void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
debug(D_RRD_CALLS, "Checking read lock on chart '%s'", st->id);
int ret = netdata_rwlock_trywrlock(&st->rrdset_rwlock);
if(ret == 0)
fatal("RRDSET '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
}
void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line) {
debug(D_RRD_CALLS, "Checking write lock on chart '%s'", st->id);
int ret = netdata_rwlock_tryrdlock(&st->rrdset_rwlock);
if(ret == 0)
fatal("RRDSET '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", st->id, function, line, file);
}
// ----------------------------------------------------------------------------
// RRDSET index
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);
}
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)))
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;
}
// ----------------------------------------------------------------------------
// RRDSET - find charts
static inline 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;
}
inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, host->hostname);
RRDSET *st = rrdset_index_find(host, id, 0);
return(st);
}
inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, host->hostname);
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(host, buf));
}
inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, host->hostname);
RRDSET *st = rrdset_index_find_name(host, name, 0);
return(st);
}
// ----------------------------------------------------------------------------
// RRDSET - rename charts
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(st->rrdhost, st);
st->name = config_set_default(st->config_section, "name", b);
st->hash_name = simple_hash(st->name);
rrdsetvar_rename_all(st);
}
else {
st->name = config_get(st->config_section, "name", b);
st->hash_name = simple_hash(st->name);
}
rrdset_wrlock(st);
RRDDIM *rd;
rrddim_foreach_write(rd, st)
rrddimvar_rename_all(rd);
rrdset_unlock(st);
if(unlikely(rrdset_index_add_name(st->rrdhost, st) != st))
error("RRDSET: INTERNAL ERROR: attempted to index duplicate chart name '%s'", st->name);
}
// ----------------------------------------------------------------------------
// RRDSET - reset a chart
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;
rrddim_foreach_read(rd, st) {
rd->last_collected_time.tv_sec = 0;
rd->last_collected_time.tv_usec = 0;
rd->collections_counter = 0;
memset(rd->values, 0, rd->entries * sizeof(storage_number));
}
}
// ----------------------------------------------------------------------------
// RRDSET - helpers for rrdset_create()
inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
if(unlikely(entries < 5)) entries = 5;
if(unlikely(entries > RRD_HISTORY_ENTRIES_MAX)) entries = RRD_HISTORY_ENTRIES_MAX;
if(unlikely(mode == RRD_MEMORY_MODE_NONE || mode == RRD_MEMORY_MODE_RAM))
return entries;
long page = (size_t)sysconf(_SC_PAGESIZE);
long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
if(unlikely(size % page)) {
size -= (size % page);
size += page;
long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
return n;
}
return entries;
}
static inline void last_collected_time_align(struct timeval *tv, int update_every) {
tv->tv_sec -= tv->tv_sec % update_every;
tv->tv_usec = 500000;
}
static inline void last_updated_time_align(struct timeval *tv, int update_every) {
tv->tv_sec -= tv->tv_sec % update_every;
tv->tv_usec = 0;
}
// ----------------------------------------------------------------------------
// RRDSET - free a chart
void rrdset_free(RRDSET *st) {
if(unlikely(!st)) return;
rrdhost_check_wrlock(st->rrdhost); // make sure we have a write lock on the host
rrdset_wrlock(st); // lock this RRDSET
// info("Removing chart '%s' ('%s')", st->id, st->name);
// ------------------------------------------------------------------------
// remove it from the indexes
if(unlikely(rrdset_index_del(st->rrdhost, st) != st))
error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
rrdset_index_del_name(st->rrdhost, st);
// ------------------------------------------------------------------------
// free its children structures
while(st->variables) rrdsetvar_free(st->variables);
while(st->alarms) rrdsetcalc_unlink(st->alarms);
while(st->dimensions) rrddim_free(st, st->dimensions);
rrdfamily_free(st->rrdhost, st->rrdfamily);
// ------------------------------------------------------------------------
// unlink it from the host
if(st == st->rrdhost->rrdset_root) {
st->rrdhost->rrdset_root = st->next;
}
else {
// find the previous one
RRDSET *s;
for(s = st->rrdhost->rrdset_root; s && s->next != st ; s = s->next) ;
// bypass it
if(s) s->next = st->next;
else error("Request to free RRDSET '%s': cannot find it under host '%s'", st->id, st->rrdhost->hostname);
}
rrdset_unlock(st);
// ------------------------------------------------------------------------
// free it
netdata_rwlock_destroy(&st->rrdset_rwlock);
// free directly allocated members
freez(st->config_section);
if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
munmap(st, st->memsize);
}
else
freez(st);
}
void rrdset_save(RRDSET *st) {
RRDDIM *rd;
rrdset_check_rdlock(st);
// info("Saving chart '%s' ('%s')", st->id, st->name);
if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
savememory(st->cache_filename, st, st->memsize);
}
rrddim_foreach_read(rd, st) {
if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
savememory(rd->cache_filename, rd, rd->memsize);
}
}
}
void rrdset_delete(RRDSET *st) {
RRDDIM *rd;
rrdset_check_rdlock(st);
// info("Deleting chart '%s' ('%s')", st->id, st->name);
if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
debug(D_RRD_STATS, "Deleting stats '%s' to '%s'.", st->name, st->cache_filename);
unlink(st->cache_filename);
}
rrddim_foreach_read(rd, st) {
if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
debug(D_RRD_STATS, "Deleting dimension '%s' to '%s'.", rd->name, rd->cache_filename);
unlink(rd->cache_filename);
}
}
}
// ----------------------------------------------------------------------------
// RRDSET - create a chart
static inline RRDSET *rrdset_find_on_create(RRDHOST *host, const char *fullid) {
RRDSET *st = rrdset_find(host, fullid);
if(unlikely(st)) {
rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
return st;
}
return NULL;
}
RRDSET *rrdset_create(
RRDHOST *host
, 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
, RRDSET_TYPE 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;
}
// ------------------------------------------------------------------------
// check if it already exists
char fullid[RRD_ID_LENGTH_MAX + 1];
snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
RRDSET *st = rrdset_find_on_create(host, fullid);
if(st) return st;
rrdhost_wrlock(host);
st = rrdset_find_on_create(host, fullid);
if(st) {
rrdhost_unlock(host);
return st;
}
char fullfilename[FILENAME_MAX + 1];
// ------------------------------------------------------------------------
// compose the config_section for this chart
char config_section[RRD_ID_LENGTH_MAX + 1];
if(host == localhost)
strcpy(config_section, fullid);
else
snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
// ------------------------------------------------------------------------
// get the options from the config, we need to create it
long rentries = config_get_number(config_section, "history", host->rrd_history_entries);
long entries = align_entries_to_pagesize(host->rrd_memory_mode, rentries);
if(entries != rentries) entries = config_set_number(config_section, "history", entries);
if(host->rrd_memory_mode == RRD_MEMORY_MODE_NONE && entries != rentries)
entries = config_set_number(config_section, "history", 10);
int enabled = config_get_boolean(config_section, "enabled", 1);
if(!enabled) entries = 5;
unsigned long size = sizeof(RRDSET);
char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
time_t now = now_realtime_sec();
// ------------------------------------------------------------------------
// load it or allocate it
debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
if(host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
st = (RRDSET *) mymmap(fullfilename, size, ((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0);
if(st) {
memset(&st->avl, 0, sizeof(avl));
memset(&st->avlname, 0, sizeof(avl));
memset(&st->variables_root_index, 0, sizeof(avl_tree_lock));
memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
memset(&st->rrdset_rwlock, 0, sizeof(netdata_rwlock_t));
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->variables = NULL;
st->alarms = NULL;
st->flags = 0x00000000;
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 - st->last_updated.tv_sec) > update_every * entries) {
errno = 0;
error("File %s is too old. Clearing it.", fullfilename);
memset(st, 0, size);
}
else if(st->last_updated.tv_sec > now + update_every) {
errno = 0;
error("File %s refers to the future. Clearing it.", fullfilename);
memset(st, 0, size);
}
// make sure the database is aligned
if(st->last_updated.tv_sec)
last_updated_time_align(&st->last_updated, update_every);
// make sure we have the right memory mode
// even if we cleared the memory
st->rrd_memory_mode = host->rrd_memory_mode;
}
}
if(unlikely(!st)) {
st = callocz(1, size);
st->rrd_memory_mode = (host->rrd_memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_RAM;
}
st->config_section = strdup(config_section);
st->rrdhost = host;
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->config_section, "chart type", rrdset_type_name(chart_type)));
st->type = config_get(st->config_section, "type", type);
st->family = config_get(st->config_section, "family", family?family:st->type);
st->units = config_get(st->config_section, "units", units?units:"");
st->context = config_get(st->config_section, "context", context?context:st->id);
st->hash_context = simple_hash(st->context);
st->priority = config_get_number(st->config_section, "priority", priority);
if(enabled)
rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
else
rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
// 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->config_section, "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);
netdata_rwlock_init(&st->rrdset_rwlock);
if(name && *name) rrdset_set_name(st, name);
else rrdset_set_name(st, id);
{
char varvalue[CONFIG_MAX_VALUE + 1];
char varvalue2[CONFIG_MAX_VALUE + 1];
snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
st->title = config_get(st->config_section, "title", varvalue2);
}
st->rrdfamily = rrdfamily_create(host, st->family);
st->next = host->rrdset_root;
host->rrdset_root = st;
if(host->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);
}
if(unlikely(rrdset_index_add(host, st) != st))
error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
rrdsetcalc_link_matching(st);
rrdcalctemplate_link_matching(st);
rrdhost_cleanup_obsolete(host);
rrdhost_unlock(host);
return(st);
}
// ----------------------------------------------------------------------------
// RRDSET - data collection iteration control
inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
if(unlikely(!st->last_collected_time.tv_sec)) {
// the first entry
microseconds = st->update_every * USEC_PER_SEC;
}
else if(unlikely(!microseconds)) {
// no dt given by the plugin
struct timeval now;
now_realtime_timeval(&now);
microseconds = dt_usec(&now, &st->last_collected_time);
}
st->usec_since_last_update = microseconds;
}
inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
struct timeval now;
now_realtime_timeval(&now);
if(unlikely(!st->last_collected_time.tv_sec)) {
// the first entry
microseconds = st->update_every * USEC_PER_SEC;
}
else if(unlikely(!microseconds)) {
// no dt given by the plugin
microseconds = dt_usec(&now, &st->last_collected_time);
}
else {
// microseconds has the time since the last collection
//#ifdef NETDATA_INTERNAL_CHECKS
// usec_t now_usec = timeval_usec(&now);
// usec_t last_usec = timeval_usec(&st->last_collected_time);
//#endif
susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
if(unlikely(since_last_usec < 0)) {
// oops! the database is in the future
error("Database for chart '%s' on host '%s' is %lld microseconds in the future. Adjusting it to current time.", st->id, st->rrdhost->hostname, -since_last_usec);
st->last_collected_time.tv_sec = now.tv_sec - st->update_every;
st->last_collected_time.tv_usec = now.tv_usec;
last_collected_time_align(&st->last_collected_time, st->update_every);
st->last_updated.tv_sec = now.tv_sec - st->update_every;
st->last_updated.tv_usec = now.tv_usec;
last_updated_time_align(&st->last_updated, st->update_every);
microseconds = st->update_every * USEC_PER_SEC;
since_last_usec = st->update_every * USEC_PER_SEC;
}
// verify the microseconds given is good
if(unlikely(microseconds > (usec_t)since_last_usec)) {
debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - (usec_t)since_last_usec, st->name, st->id);
//#ifdef NETDATA_INTERNAL_CHECKS
// if(unlikely(last_usec + microseconds > now_usec + 1000))
// error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - (usec_t)since_last_usec, st->name, st->id);
//#endif
microseconds = (usec_t)since_last_usec;
}
else if(unlikely(microseconds < (usec_t)since_last_usec * 0.8)) {
debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, (usec_t)since_last_usec, st->name, st->id);
//#ifdef NETDATA_INTERNAL_CHECKS
// error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, (usec_t)since_last_usec, st->name, st->id);
//#endif
microseconds = (usec_t)since_last_usec;
}
}
debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
st->usec_since_last_update = microseconds;
}
// ----------------------------------------------------------------------------
// RRDSET - process the collected values for all dimensions of a chart
static inline void rrdset_init_last_collected_time(RRDSET *st) {
now_realtime_timeval(&st->last_collected_time);
last_collected_time_align(&st->last_collected_time, st->update_every);
}
static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
usec_t ut = last_collect_ut + st->usec_since_last_update;
st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
return last_collect_ut;
}
static inline void rrdset_init_last_updated_time(RRDSET *st) {
// copy the last collected time to last updated time
st->last_updated.tv_sec = st->last_collected_time.tv_sec;
st->last_updated.tv_usec = st->last_collected_time.tv_usec;
last_updated_time_align(&st->last_updated, st->update_every);
}
static inline void rrdset_done_push_exclusive(RRDSET *st) {
if(unlikely(!st->last_collected_time.tv_sec)) {
// it is the first entry
// set the last_collected_time to now
rrdset_init_last_collected_time(st);
}
else {
// it is not the first entry
// calculate the proper last_collected_time, using usec_since_last_update
rrdset_update_last_collected_time(st);
}
st->counter_done++;
rrdset_rdlock(st);
rrdset_done_push(st);
rrdset_unlock(st);
}
void rrdset_done(RRDSET *st) {
if(unlikely(netdata_exit)) return;
if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
if(unlikely(st->rrdhost->rrdpush_enabled))
rrdset_done_push_exclusive(st);
return;
}
debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
RRDDIM *rd;
int
pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
char
store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
unsigned int
stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
usec_t
last_collect_ut, // the timestamp in microseconds, of the last collected value
now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
error("Cannot set pthread cancel state to DISABLE.");
// a read lock is OK here
rrdset_rdlock(st);
/*
// enable the chart, if it was disabled
if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
st->enabled = 1;
*/
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))) {
error("Chart '%s' has the OBSOLETE flag set, but it is collected.", st->id);
rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
}
// check if the chart has a long time to be updated
if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
info("%s: took too long to be updated (%0.3Lf secs). Resetting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
rrdset_reset(st);
st->usec_since_last_update = update_every_ut;
first_entry = 1;
}
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
// set last_collected_time
if(unlikely(!st->last_collected_time.tv_sec)) {
// it is the first entry
// set the last_collected_time to now
rrdset_init_last_collected_time(st);
last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
// the first entry should not be stored
store_this_entry = 0;
first_entry = 1;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
}
else {
// it is not the first entry
// calculate the proper last_collected_time, using usec_since_last_update
last_collect_ut = rrdset_update_last_collected_time(st);
}
// if this set has not been updated in the past
// we fake the last_update time to be = now - usec_since_last_update
if(unlikely(!st->last_updated.tv_sec)) {
// it has never been updated before
// set a fake last_updated, in the past using usec_since_last_update
rrdset_init_last_updated_time(st);
// the first entry should not be stored
store_this_entry = 0;
first_entry = 1;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: initializing last_updated to last_collected_time - %llu microseconds. Will not store the next entry.", st->name, st->usec_since_last_update);
}
// check if we will re-write the entire data set
if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Resetting 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);
rrdset_reset(st);
rrdset_init_last_updated_time(st);
st->usec_since_last_update = update_every_ut;
// the first entry should not be stored
store_this_entry = 0;
first_entry = 1;
}
// these are the 3 variables that will help us in interpolation
// last_stored_ut = the last time we added a value to the storage
// now_collect_ut = the time the current value has been collected
// next_store_ut = the time of the next interpolation point
last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
debug(D_RRD_STATS, "%s: now_collect_ut = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
}
if(unlikely(!st->counter_done)) {
store_this_entry = 0;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
}
st->counter_done++;
if(unlikely(st->rrdhost->rrdpush_enabled))
rrdset_done_push(st);
// calculate totals and count the dimensions
int dimensions = 0;
st->collected_total = 0;
rrddim_foreach_read(rd, st) {
dimensions++;
if(likely(rd->updated))
st->collected_total += rd->collected_value;
}
uint32_t storage_flags = SN_EXISTS;
// process all dimensions to calculate their values
// based on the collected figures only
// at this stage we do not interpolate anything
rrddim_foreach_read(rd, st) {
if(unlikely(!rd->updated)) {
rd->calculated_value = 0;
continue;
}
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: START "
" last_collected_value = " COLLECTED_NUMBER_FORMAT
" collected_value = " COLLECTED_NUMBER_FORMAT
" last_calculated_value = " CALCULATED_NUMBER_FORMAT
" calculated_value = " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, rd->last_collected_value
, rd->collected_value
, rd->last_calculated_value
, rd->calculated_value
);
switch(rd->algorithm) {
case RRD_ALGORITHM_ABSOLUTE:
rd->calculated_value = (calculated_number)rd->collected_value
* (calculated_number)rd->multiplier
/ (calculated_number)rd->divisor;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
CALCULATED_NUMBER_FORMAT " = "
COLLECTED_NUMBER_FORMAT
" * " CALCULATED_NUMBER_FORMAT
" / " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, rd->calculated_value
, rd->collected_value
, (calculated_number)rd->multiplier
, (calculated_number)rd->divisor
);
break;
case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
if(unlikely(!st->collected_total))
rd->calculated_value = 0;
else
// the percentage of the current value
// over the total of all dimensions
rd->calculated_value =
(calculated_number)100
* (calculated_number)rd->collected_value
/ (calculated_number)st->collected_total;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
CALCULATED_NUMBER_FORMAT " = 100"
" * " COLLECTED_NUMBER_FORMAT
" / " COLLECTED_NUMBER_FORMAT
, st->id, rd->name
, rd->calculated_value
, rd->collected_value
, st->collected_total
);
break;
case RRD_ALGORITHM_INCREMENTAL:
if(unlikely(rd->collections_counter <= 1)) {
rd->calculated_value = 0;
continue;
}
// if the new is smaller than the old (an overflow, or reset), set the old equal to the new
// to reset the calculation (it will give zero as the calculation for this second)
if(unlikely(rd->last_collected_value > rd->collected_value)) {
debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
, st->name, rd->name
, rd->last_collected_value
, rd->collected_value);
if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
storage_flags = SN_EXISTS_RESET;
rd->last_collected_value = rd->collected_value;
}
rd->calculated_value +=
(calculated_number)(rd->collected_value - rd->last_collected_value)
* (calculated_number)rd->multiplier
/ (calculated_number)rd->divisor;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
CALCULATED_NUMBER_FORMAT " = ("
COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
")"
" * " CALCULATED_NUMBER_FORMAT
" / " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, rd->calculated_value
, rd->collected_value, rd->last_collected_value
, (calculated_number)rd->multiplier
, (calculated_number)rd->divisor
);
break;
case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
if(unlikely(rd->collections_counter <= 1)) {
rd->calculated_value = 0;
continue;
}
// if the new is smaller than the old (an overflow, or reset), set the old equal to the new
// to reset the calculation (it will give zero as the calculation for this second)
if(unlikely(rd->last_collected_value > rd->collected_value)) {
debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
, st->name, rd->name
, rd->last_collected_value
, rd->collected_value);
if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
storage_flags = SN_EXISTS_RESET;
rd->last_collected_value = rd->collected_value;
}
// the percentage of the current increment
// over the increment of all dimensions together
if(unlikely(st->collected_total == st->last_collected_total))
rd->calculated_value = 0;
else
rd->calculated_value =
(calculated_number)100
* (calculated_number)(rd->collected_value - rd->last_collected_value)
/ (calculated_number)(st->collected_total - st->last_collected_total);
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
CALCULATED_NUMBER_FORMAT " = 100"
" * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
" / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
, st->id, rd->name
, rd->calculated_value
, rd->collected_value, rd->last_collected_value
, st->collected_total, st->last_collected_total
);
break;
default:
// make the default zero, to make sure
// it gets noticed when we add new types
rd->calculated_value = 0;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC "
CALCULATED_NUMBER_FORMAT " = 0"
, st->id, rd->name
, rd->calculated_value
);
break;
}
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: PHASE2 "
" last_collected_value = " COLLECTED_NUMBER_FORMAT
" collected_value = " COLLECTED_NUMBER_FORMAT
" last_calculated_value = " CALCULATED_NUMBER_FORMAT
" calculated_value = " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, rd->last_collected_value
, rd->collected_value
, rd->last_calculated_value
, rd->calculated_value
);
}
// at this point we have all the calculated values ready
// it is now time to interpolate values on a second boundary
if(unlikely(now_collect_ut < next_store_ut)) {
// this is collected in the same interpolation point
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
//#ifdef NETDATA_INTERNAL_CHECKS
// info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
//#endif
}
usec_t first_ut = last_stored_ut;
long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
if((now_collect_ut % (update_every_ut)) == 0) iterations++;
for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
//#ifdef NETDATA_INTERNAL_CHECKS
// if(iterations < 0) { error("%s: iterations calculation wrapped! first_ut = %llu, last_stored_ut = %llu, next_store_ut = %llu, now_collect_ut = %llu", st->name, first_ut, last_stored_ut, next_store_ut, now_collect_ut); }
//#endif
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
}
st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
st->last_updated.tv_usec = 0;
rrddim_foreach_read(rd, st) {
calculated_number new_value;
switch(rd->algorithm) {
case RRD_ALGORITHM_INCREMENTAL:
new_value = (calculated_number)
( rd->calculated_value
* (calculated_number)(next_store_ut - last_collect_ut)
/ (calculated_number)(now_collect_ut - last_collect_ut)
);
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC2 INC "
CALCULATED_NUMBER_FORMAT " = "
CALCULATED_NUMBER_FORMAT
" * %llu"
" / %llu"
, st->id, rd->name
, new_value
, rd->calculated_value
, (next_store_ut - last_stored_ut)
, (now_collect_ut - last_stored_ut)
);
rd->calculated_value -= new_value;
new_value += rd->last_calculated_value;
rd->last_calculated_value = 0;
new_value /= (calculated_number)st->update_every;
if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
st->id, rd->name
, (calculated_number)(next_store_ut - last_stored_ut)
);
new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
}
break;
case RRD_ALGORITHM_ABSOLUTE:
case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
default:
if(iterations == 1) {
// this is the last iteration
// do not interpolate
// just show the calculated value
new_value = rd->calculated_value;
}
else {
// we have missed an update
// interpolate in the middle values
new_value = (calculated_number)
( ( (rd->calculated_value - rd->last_calculated_value)
* (calculated_number)(next_store_ut - last_collect_ut)
/ (calculated_number)(now_collect_ut - last_collect_ut)
)
+ rd->last_calculated_value
);
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
CALCULATED_NUMBER_FORMAT " = ((("
"(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
" * %llu"
" / %llu) + " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, new_value
, rd->calculated_value, rd->last_calculated_value
, (next_store_ut - first_ut)
, (now_collect_ut - first_ut), rd->last_calculated_value
);
}
break;
}
if(unlikely(!store_this_entry)) {
rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
continue;
}
if(likely(rd->updated && rd->collections_counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
rd->last_stored_value = new_value;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, st->current_entry
, unpack_storage_number(rd->values[st->current_entry]), new_value
);
}
else {
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
, st->id, rd->name
, st->current_entry
);
rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
rd->last_stored_value = NAN;
}
stored_entries++;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
calculated_number accuracy = accuracy_loss(t1, t2);
debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
, st->id, rd->name
, st->current_entry
, t2
, get_storage_number_flags(rd->values[st->current_entry])
, t1
, accuracy
, (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
);
rd->collected_volume += t1;
rd->stored_volume += t2;
accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
, st->id, rd->name
, st->current_entry
, rd->stored_volume
, rd->collected_volume
, accuracy
, (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
);
}
}
// reset the storage flags for the next point, if any;
storage_flags = SN_EXISTS;
st->counter++;
st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
last_stored_ut = next_store_ut;
}
st->last_collected_total = st->collected_total;
rrddim_foreach_read(rd, st) {
if(unlikely(!rd->updated))
continue;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_collected_value, rd->collected_value);
rd->last_collected_value = rd->collected_value;
switch(rd->algorithm) {
case RRD_ALGORITHM_INCREMENTAL:
if(unlikely(!first_entry)) {
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value + rd->calculated_value, rd->calculated_value);
rd->last_calculated_value += rd->calculated_value;
}
else {
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
}
break;
case RRD_ALGORITHM_ABSOLUTE:
case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value, rd->calculated_value);
rd->last_calculated_value = rd->calculated_value;
break;
}
rd->calculated_value = 0;
rd->collected_value = 0;
rd->updated = 0;
if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
debug(D_RRD_STATS, "%s/%s: END "
" last_collected_value = " COLLECTED_NUMBER_FORMAT
" collected_value = " COLLECTED_NUMBER_FORMAT
" last_calculated_value = " CALCULATED_NUMBER_FORMAT
" calculated_value = " CALCULATED_NUMBER_FORMAT
, st->id, rd->name
, rd->last_collected_value
, rd->collected_value
, rd->last_calculated_value
, rd->calculated_value
);
}
// ALL DONE ABOUT THE DATA UPDATE
// --------------------------------------------------------------------
/*
// find if there are any obsolete dimensions (not updated recently)
if(unlikely(rrd_delete_unupdated_dimensions)) {
for( rd = st->dimensions; likely(rd) ; rd = rd->next )
if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
break;
if(unlikely(rd)) {
RRDDIM *last;
// there is dimension to free
// upgrade our read lock to a write lock
rrdset_unlock(st);
rrdset_wrlock(st);
for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
// remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
if(unlikely(!last)) {
st->dimensions = rd->next;
rd->next = NULL;
rrddim_free(st, rd);
rd = st->dimensions;
continue;
}
else {
last->next = rd->next;
rd->next = NULL;
rrddim_free(st, rd);
rd = last->next;
continue;
}
}
last = rd;
rd = rd->next;
}
if(unlikely(!st->dimensions)) {
info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
st->enabled = 0;
}
}
}
*/
rrdset_unlock(st);
if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
}
|