64
} \
65
while (0)
66
67
-static inline void STATS_PLUS_MEMORY(struct dictionary_stats *stats, int64_t judy_mem, size_t item_size, size_t value_size) {
68
- if(judy_mem)
69
- __atomic_fetch_add(&stats->memory.index, judy_mem, __ATOMIC_RELAXED);
70
-
71
- if(item_size)
72
- __atomic_fetch_add(&stats->memory.dict, (ssize_t)item_size, __ATOMIC_RELAXED);
73
-
74
- if(value_size)
75
- __atomic_fetch_add(&stats->memory.values, (long)value_size, __ATOMIC_RELAXED);
76
-}
77
-
78
-static inline void STATS_MINUS_MEMORY(struct dictionary_stats *stats, int64_t judy_mem, size_t item_size, size_t value_size) {
79
- if(judy_mem)
80
- __atomic_fetch_add(&stats->memory.index, judy_mem, __ATOMIC_RELAXED);
81
-
82
- if(item_size)
83
- __atomic_fetch_sub(&stats->memory.dict, (ssize_t)item_size, __ATOMIC_RELAXED);
84
-
85
- if(value_size)
86
- __atomic_fetch_sub(&stats->memory.values, (long)value_size, __ATOMIC_RELAXED);
67
+static inline void RRDLABELS_MEMORY_DELTA(struct dictionary_stats *stats, int64_t judy_mem, int64_t item_size) {
68
+ __atomic_fetch_add(&stats->memory.index, judy_mem, __ATOMIC_RELAXED);
69
+ __atomic_fetch_add(&stats->memory.dict, item_size, __ATOMIC_RELAXED);
70
}
71
89
-#define RRDLABELS_MAX_NAME_LENGTH 200
90
-#define RRDLABELS_MAX_VALUE_LENGTH 800 // 800 in bytes, up to 200 UTF-8 characters
91
-
72
__attribute__((constructor)) void initialize_label_stats(void) {
73
dictionary_stats_category_rrdlabels.memory.dict = 0;
74
dictionary_stats_category_rrdlabels.memory.index = 0;
81
RRDLABELS *rrdlabels_create(void)
82
{
83
RRDLABELS *labels = callocz(1, sizeof(*labels));
104
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, sizeof(RRDLABELS), 0);
84
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, 0, sizeof(RRDLABELS));
85
return labels;
86
}
87
123
rrdlabel = *PValue;
124
string_freez(label_index.key);
125
string_freez(label_index.value);
146
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, judy_mem, 0, 0);
126
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, 0);
127
} else {
128
rrdlabel = callocz(1, sizeof(*rrdlabel));
129
rrdlabel->label.index = label_index;
130
*PValue = rrdlabel;
151
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, judy_mem, sizeof(RRDLABEL_IDX), 0);
131
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, sizeof(RRDLABEL_IDX));
132
}
133
__atomic_add_fetch(&rrdlabel->refcount, 1, __ATOMIC_RELAXED);
134
151
152
int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
153
174
- STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, judy_mem, sizeof(*rrdlabel), 0);
154
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, -(int64_t)sizeof(*rrdlabel));
155
string_freez(label->index.key);
156
string_freez(label->index.value);
157
freez(rrdlabel);
175
while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
176
delete_label((RRDLABEL *)Index);
177
}
198
- size_t memory_freed = JudyLFreeArray(&labels->JudyL, PJE0);
199
- STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, memory_freed + sizeof(RRDLABELS), 0);
178
+ JudyAllocThreadPulseReset();
179
+ JudyLFreeArray(&labels->JudyL, PJE0);
180
+ int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
181
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, -(int64_t)sizeof(RRDLABELS));
182
spinlock_unlock(&labels->spinlock);
183
}
184
225
226
RRDLABEL_SRC new_ls = (ls & ~(RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_OLD));
227
246
- size_t mem_before_judyl = JudyLMemUsed(labels->JudyL);
228
+ JudyAllocThreadPulseReset();
229
230
Pvoid_t *PValue = JudyLIns(&labels->JudyL, (Word_t)new_label, PJE0);
231
if (!PValue || PValue == PJERR)
250
251
labels->version++;
252
271
- size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
272
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
253
+ int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
254
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, 0);
255
256
spinlock_unlock(&labels->spinlock);
257
}
486
while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
487
if (!((*((RRDLABEL_SRC *)PValue)) & (RRDLABEL_FLAG_INTERNAL))) {
488
507
- size_t mem_before_judyl = JudyLMemUsed(labels->JudyL);
489
+ JudyAllocThreadPulseReset();
490
(void)JudyLDel(&labels->JudyL, Index, PJE0);
509
- size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
491
+ int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
492
511
- STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_before_judyl - mem_after_judyl, 0);
493
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, 0);
494
495
delete_label((RRDLABEL *)Index);
496
if (labels->JudyL != (Pvoid_t) NULL) {
568
RRDLABEL_SRC ls;
569
lfe_start_nolock(src, label, ls)
570
{
589
- size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
571
+ JudyAllocThreadPulseGetAndReset();
572
+
573
PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
574
if(unlikely(!PValue || PValue == PJERR))
575
fatal("RRDLABELS migrate: corrupted labels array");
578
if (!*PValue) {
579
flag = (ls & ~(RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW)) | RRDLABEL_FLAG_NEW;
580
dup_label(label);
598
- size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
599
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
581
+ int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
582
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, 0);
583
}
584
else
585
flag = RRDLABEL_FLAG_OLD;
639
spinlock_lock(&dst->spinlock);
640
spinlock_lock(&src->spinlock);
641
659
- size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
642
+ JudyAllocThreadPulseReset();
643
+
644
bool update_statistics = false;
645
lfe_start_nolock(src, label, ls)
646
{
666
}
667
lfe_done_nolock();
668
if (update_statistics) {
685
- size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
686
- STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
669
+ int64_t judy_mem = JudyAllocThreadPulseGetAndReset();
670
+ RRDLABELS_MEMORY_DELTA(&dictionary_stats_category_rrdlabels, judy_mem, 0);
671
}
672
673
spinlock_unlock(&src->spinlock);
911
rrdset_metadata_updated(st);
912
}
913
930
-struct pattern_array *pattern_array_allocate()
931
-{
932
- struct pattern_array *pa = callocz(1, sizeof(*pa));
933
- return pa;
934
-}
935
-
936
-static void pattern_array_add_lblkey_with_sp(struct pattern_array *pa, const char *key, SIMPLE_PATTERN *sp)
937
-{
938
- if (!pa || !key || !sp)
939
- return;
940
-
941
- STRING *string_key = string_strdupz(key);
942
- Pvoid_t *Pvalue = JudyLIns(&pa->JudyL, (Word_t) string_key, PJE0);
943
- if (!Pvalue) {
944
- string_freez(string_key);
945
- simple_pattern_free(sp);
946
- return;
947
- }
948
-
949
- struct pattern_array_item *pai;
950
- if (*Pvalue) {
951
- pai = *Pvalue;
952
- } else {
953
- *Pvalue = pai = callocz(1, sizeof(*pai));
954
- pa->key_count++;
955
- }
956
-
957
- pai->size++;
958
- Pvalue = JudyLIns(&pai->JudyL, (Word_t) pai->size, PJE0);
959
- if (!Pvalue) {
960
- simple_pattern_free(sp);
961
- return;
962
- }
963
-
964
- *Pvalue = sp;
965
-}
966
-
967
-bool pattern_array_label_match(
968
- struct pattern_array *pa,
969
- RRDLABELS *labels,
970
- char eq,
971
- size_t *searches)
972
-{
973
- if (!pa || !labels)
974
- return true;
975
-
976
- Pvoid_t *Pvalue;
977
- Word_t Index = 0;
978
- bool first_then_next = true;
979
- while ((Pvalue = JudyLFirstThenNext(pa->JudyL, &Index, &first_then_next))) {
980
- // for each label key in the patterns array
981
-
982
- struct pattern_array_item *pai = *Pvalue;
983
- SIMPLE_PATTERN_RESULT match = SP_NOT_MATCHED ;
984
- for (Word_t i = 1; i <= pai->size; i++) {
985
- // for each pattern in the label key pattern list
986
-
987
- if (!(Pvalue = JudyLGet(pai->JudyL, i, PJE0)) || !*Pvalue)
988
- continue;
989
-
990
- match = rrdlabels_match_simple_pattern_parsed(labels, (SIMPLE_PATTERN *)(*Pvalue), eq, searches);
991
-
992
- if(match != SP_NOT_MATCHED)
993
- break;
994
- }
995
-
996
- if (match != SP_MATCHED_POSITIVE)
997
- return false;
998
- }
999
- return true;
1000
-}
1001
-
1002
-struct pattern_array *pattern_array_add_key_simple_pattern(struct pattern_array *pa, const char *key, SIMPLE_PATTERN *pattern)
1003
-{
1004
- if (unlikely(!pattern || !key))
1005
- return pa;
1006
-
1007
- if (!pa)
1008
- pa = pattern_array_allocate();
1009
-
1010
- pattern_array_add_lblkey_with_sp(pa, key, pattern);
1011
- return pa;
1012
-}
1013
-
1014
-struct pattern_array *pattern_array_add_simple_pattern(struct pattern_array *pa, SIMPLE_PATTERN *pattern, char sep)
1015
-{
1016
- if (unlikely(!pattern))
1017
- return pa;
1018
-
1019
- if (!pa)
1020
- pa = pattern_array_allocate();
1021
-
1022
- char *label_key;
1023
- while (pattern && (label_key = simple_pattern_iterate(&pattern))) {
1024
- char key[RRDLABELS_MAX_NAME_LENGTH + 1], *key_sep;
1025
-
1026
- if (unlikely(!label_key || !(key_sep = strchr(label_key, sep))))
1027
- return pa;
1028
-
1029
- *key_sep = '\0';
1030
- strncpyz(key, label_key, RRDLABELS_MAX_NAME_LENGTH);
1031
- *key_sep = sep;
1032
-
1033
- pattern_array_add_lblkey_with_sp(pa, key, string_to_simple_pattern(label_key));
1034
- }
1035
- return pa;
1036
-}
1037
-
1038
-struct pattern_array *pattern_array_add_key_value(struct pattern_array *pa, const char *key, const char *value, char sep)
1039
-{
1040
- if (unlikely(!key || !value))
1041
- return pa;
1042
-
1043
- if (!pa)
1044
- pa = pattern_array_allocate();
1045
-
1046
- char label_key[RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2];
1047
- snprintfz(label_key, sizeof(label_key) - 1, "%s%c%s", key, sep, value);
1048
- pattern_array_add_lblkey_with_sp(
1049
- pa, key, simple_pattern_create(label_key, SIMPLE_PATTERN_DEFAULT_WEB_SEPARATORS, SIMPLE_PATTERN_EXACT, true));
1050
- return pa;
1051
-}
1052
-
1053
-void pattern_array_free(struct pattern_array *pa)
1054
-{
1055
- if (!pa)
1056
- return;
1057
-
1058
- Pvoid_t *Pvalue;
1059
- Word_t Index = 0;
1060
- while ((Pvalue = JudyLFirst(pa->JudyL, &Index, PJE0))) {
1061
- struct pattern_array_item *pai = *Pvalue;
1062
-
1063
- for (Word_t i = 1; i <= pai->size; i++) {
1064
- if (!(Pvalue = JudyLGet(pai->JudyL, i, PJE0)))
1065
- continue;
1066
- simple_pattern_free((SIMPLE_PATTERN *) (*Pvalue));
1067
- }
1068
- JudyLFreeArray(&(pai->JudyL), PJE0);
1069
-
1070
- string_freez((STRING *)Index);
1071
- (void) JudyLDel(&(pa->JudyL), Index, PJE0);
1072
- freez(pai);
1073
- Index = 0;
1074
- }
1075
- freez(pa);
1076
-}
1077
-
914
// ----------------------------------------------------------------------------
915
// rrdlabels unit test
916
1116
return 1;
1117
}
1118
1119
+void pattern_array_add_lblkey_with_sp(struct pattern_array *pa, const char *key, SIMPLE_PATTERN *sp);
1120
static int rrdlabels_unittest_pattern_check()
1121
{
1122
fprintf(stderr, "\n%s() tests\n", __FUNCTION__);