added context index (to be used for storing context scope variables for health monitoring)
Costa Tsaousis committed
Aug 5, 2016 at 09:44 UTC
929fcc7b073a75c861a3d9025859bcfeffaa7875
2 files changed
+103
-23
src/rrd.c
+81
-11
@@ -37,6 +37,7 @@ int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
37
38
static int rrdset_compare(void* a, void* b);
39
static int rrdset_compare_name(void* a, void* b);
40
+static int rrdcontext_compare(void* a, void* b);
41
42
RRDHOST localhost = {
43
.hostname = "localhost",
@@ -49,27 +50,81 @@ RRDHOST localhost = {
50
.rrdset_root_index_name = {
51
{ NULL, rrdset_compare_name },
52
AVL_LOCK_INITIALIZER
53
+ },
54
+ .rrdcontext_root_index = {
55
+ { NULL, rrdcontext_compare },
56
+ AVL_LOCK_INITIALIZER
57
}
58
};
59
60
+// ----------------------------------------------------------------------------
61
+// RRDCONTEXT index
62
+
63
+static int rrdcontext_compare(void* a, void* b) {
64
+ if(((RRDCONTEXT *)a)->hash < ((RRDCONTEXT *)b)->hash) return -1;
65
+ else if(((RRDCONTEXT *)a)->hash > ((RRDCONTEXT *)b)->hash) return 1;
66
+ else return strcmp(((RRDCONTEXT *)a)->id, ((RRDCONTEXT *)b)->id);
67
+}
68
+
69
+#define rrdcontext_index_add(host, rc) (RRDCONTEXT *)avl_insert_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
70
+#define rrdcontext_index_del(host, rc) (RRDCONTEXT *)avl_remove_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
71
+
72
+static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t hash) {
73
+ RRDCONTEXT tmp;
74
+ tmp.id = id;
75
+ tmp.hash = (hash)?hash:simple_hash(tmp.id);
76
+
77
+ return (RRDCONTEXT *)avl_search_lock(&(host->rrdcontext_root_index), (avl *) &tmp);
78
+}
79
+
80
+RRDCONTEXT *rrdcontext_create(const char *id) {
81
+ RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
82
+ if(!rc) {
83
+ rc = calloc(1, sizeof(RRDCONTEXT));
84
+ if(!rc) fatal("Cannot allocate RRDCONTEXT memory");
85
+ rc->id = strdup(id);
86
+ if(!rc->id) fatal("Cannot allocate RRDCONTEXT.id memory");
87
+ rc->hash = simple_hash(rc->id);
88
+ // avl_init_lock(&rc->variables_root_index, compar);
89
+ RRDCONTEXT *ret = rrdcontext_index_add(&localhost, rc);
90
+ if(ret != rc)
91
+ fatal("INTERNAL ERROR: Expected to INSERT RRDCONTEXT '%s' into index, but inserted '%s'.", rc->id, (ret)?ret->id:"NONE");
92
+ }
93
+
94
+ rc->use_count++;
95
+ return rc;
96
+}
97
+
98
+void rrdcontext_free(RRDCONTEXT *rc) {
99
+ rc->use_count--;
100
+ if(!rc->use_count) {
101
+ RRDCONTEXT *ret = rrdcontext_index_del(&localhost, rc);
102
+ if(ret != rc)
103
+ fatal("INTERNAL ERROR: Expected to DELETE RRDCONTEXT '%s' from index, but deleted '%s'.", rc->id, (ret)?ret->id:"NONE");
104
+
105
+ free((void *)rc->id);
106
+ free(rc);
107
+ }
108
+}
109
+
110
// ----------------------------------------------------------------------------
111
// RRDSET index
112
113
static int rrdset_compare(void* a, void* b) {
59
- if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
60
- else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
61
- else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
114
+ if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
115
+ else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
116
+ else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
117
}
118
64
-#define rrdset_index_add(host, st) avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
65
-#define rrdset_index_del(host, st) avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
119
+#define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
120
+#define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
121
122
static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
68
- RRDSET tmp;
69
- strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
70
- tmp.hash = (hash)?hash:simple_hash(tmp.id);
123
+ RRDSET tmp;
124
+ strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
125
+ tmp.hash = (hash)?hash:simple_hash(tmp.id);
126
72
- return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
127
+ return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
128
}
129
130
// ----------------------------------------------------------------------------
@@ -89,11 +144,20 @@ static int rrdset_compare_name(void* a, void* b) {
144
}
145
146
RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
147
+ void *result;
148
// fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
93
- return (RRDSET *)avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
149
+ result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
150
+ if(result) return rrdset_from_avlname(result);
151
+ return NULL;
152
}
153
96
-#define rrdset_index_del_name(host, st) avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname))
154
+RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
155
+ void *result;
156
+ // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
157
+ return (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
158
+ if(result) return rrdset_from_avlname(result);
159
+ return NULL;
160
+}
161
162
static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
163
void *result = NULL;
@@ -465,6 +529,8 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
529
530
rrdset_index_add(&localhost, st);
531
532
+ st->rrdcontext = rrdcontext_create(st->context);
533
+
534
pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
535
536
return(st);
@@ -660,6 +726,10 @@ void rrdset_free_all(void)
726
727
rrdset_index_del(&localhost, st);
728
729
+ st->rrdcontext->use_count--;
730
+ if(!st->rrdcontext->use_count)
731
+ rrdcontext_free(st->rrdcontext);
732
+
733
if(st->mapped == RRD_MEMORY_MODE_SAVE) {
734
debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
735
savememory(st->cache_filename, st, st->memsize);
src/rrd.h
+22
-12
@@ -22,7 +22,7 @@ extern int rrd_delete_unupdated_dimensions;
22
23
#define RRD_ID_LENGTH_MAX 1024
24
25
-#define RRDSET_MAGIC "NETDATA RRD SET FILE V017"
25
+#define RRDSET_MAGIC "NETDATA RRD SET FILE V018"
26
#define RRDDIMENSION_MAGIC "NETDATA RRD DIMENSION FILE V017"
27
28
typedef long long total_number;
@@ -82,6 +82,21 @@ extern const char *rrddim_algorithm_name(int chart_type);
82
#define RRDDIM_FLAG_HIDDEN 0x00000001 // this dimension will not be offered to callers
83
#define RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS 0x00000002 // do not offer RESET or OVERFLOW info to callers
84
85
+// ----------------------------------------------------------------------------
86
+// RRD CONTEXT
87
+
88
+struct rrdcontext {
89
+ avl avl;
90
+
91
+ const char *id;
92
+ uint32_t hash;
93
+
94
+ size_t use_count;
95
+
96
+ // avl_tree_lock variables_root_index;
97
+};
98
+typedef struct rrdcontext RRDCONTEXT;
99
+
100
// ----------------------------------------------------------------------------
101
// RRD DIMENSION
102
@@ -237,6 +252,7 @@ struct rrdset {
252
total_number collected_total; // used internally to calculate percentages
253
total_number last_collected_total; // used internally to calculate percentages
254
255
+ RRDCONTEXT *rrdcontext;
256
struct rrdset *next; // linking of rrdsets
257
258
// ------------------------------------------------------------------------
@@ -254,21 +270,12 @@ struct rrdset {
270
};
271
typedef struct rrdset RRDSET;
272
257
-// ----------------------------------------------------------------------------
258
-// RRD CONTEXT
259
-
260
-struct rrdcontext {
261
- avl avl;
262
-
263
- char *context;
264
-};
265
-typedef struct rrdcontext RRDCONTEXT;
266
-
267
-
273
// ----------------------------------------------------------------------------
274
// RRD HOST
275
276
struct rrdhost {
277
+ avl avl;
278
+
279
char *hostname;
280
281
RRDSET *rrdset_root;
@@ -276,6 +283,9 @@ struct rrdhost {
283
284
avl_tree_lock rrdset_root_index;
285
avl_tree_lock rrdset_root_index_name;
286
+
287
+ avl_tree_lock rrdcontext_root_index;
288
+ // avl_tree_lock variables_root_index;
289
};
290
typedef struct rrdhost RRDHOST;
291