1 #include "adaptive_resortable_list.h"
3 // the default processor() of the ARL
4 // can be overwritten at arl_create()
5 static inline void arl_callback_str2ull(const char *name, uint32_t hash, const char *value, void *dst) {
9 register unsigned long long *d = dst;
11 // fprintf(stderr, "name '%s' with hash %u and value '%s' is %llu\n", name, hash, value, *d);
15 ARL_BASE *arl_create(const char *name, void (*processor)(const char *, uint32_t, const char *, void *), size_t rechecks) {
16 ARL_BASE *base = callocz(1, sizeof(ARL_BASE));
18 base->name = strdupz(name);
21 base->processor = arl_callback_str2ull;
23 base->processor = processor;
25 base->rechecks = rechecks;
30 void arl_free(ARL_BASE *arl_base) {
31 if(unlikely(!arl_base))
34 while(arl_base->head) {
35 ARL_ENTRY *e = arl_base->head;
36 arl_base->head = e->next;
39 #ifdef NETDATA_INTERNAL_CHECKS
40 memset(e, 0, sizeof(ARL_ENTRY));
45 freez(arl_base->name);
47 #ifdef NETDATA_INTERNAL_CHECKS
48 memset(arl_base, 0, sizeof(ARL_BASE));
54 void arl_begin(ARL_BASE *base) {
56 #ifdef NETDATA_INTERNAL_CHECKS
57 if(likely(base->iteration > 10)) {
58 // do these checks after the ARL has been sorted
60 if(unlikely(base->relinkings > (base->expected + base->allocated)))
61 info("ARL '%s' has %zu relinkings with %zu expected and %zu allocated entries. Is the source changing so fast?"
62 , base->name, base->relinkings, base->expected, base->allocated);
64 if(unlikely(base->slow > base->fast))
65 info("ARL '%s' has %zu fast searches and %zu slow searches. Is the source really changing so fast?"
66 , base->name, base->fast, base->slow);
68 if(unlikely(base->iteration % 60 == 0)) {
69 info("ARL '%s' statistics: iteration %zu, expected %zu, wanted %zu, allocated %zu, fred %zu, relinkings %zu, found %zu, added %zu, fast %zu, slow %zu"
82 // for(e = base->head; e; e = e->next) fprintf(stderr, "%s ", e->name);
83 // fprintf(stderr, "\n");
88 if(unlikely(base->added || base->iteration % base->rechecks) == 1) {
91 ARL_ENTRY *e = base->head;
93 if(e->flags & ARL_ENTRY_FLAG_FOUND) {
95 // remove the found flag
96 e->flags &= ~ARL_ENTRY_FLAG_FOUND;
99 if(e->flags & ARL_ENTRY_FLAG_EXPECTED)
103 else if(e->flags & ARL_ENTRY_FLAG_DYNAMIC && !(base->head == e && !e->next)) { // not last entry
104 // we can remove this entry
105 // it is not found, and it was created because
106 // it was found in the source file
108 // remember the next one
109 ARL_ENTRY *t = e->next;
111 // remove it from the list
112 if(e->next) e->next->prev = e->prev;
113 if(e->prev) e->prev->next = e->next;
114 if(base->head == e) base->head = e->next;
132 if(unlikely(!base->head)) {
133 // hm... no nodes at all in the list #1700
134 // add a fake one to prevent a crash
135 // this is better than checking for the existence of nodes all the time
136 arl_expect(base, "a-really-not-existing-source-keyword", NULL);
140 base->next_keyword = base->head;
145 // register an expected keyword to the ARL
146 // together with its destination ( i.e. the output of the processor() )
147 ARL_ENTRY *arl_expect(ARL_BASE *base, const char *keyword, void *dst) {
148 ARL_ENTRY *e = callocz(1, sizeof(ARL_ENTRY));
149 e->name = strdupz(keyword);
150 e->hash = simple_hash(e->name);
152 e->flags = ARL_ENTRY_FLAG_EXPECTED;
154 e->next = base->head;
156 if(base->head) base->head->prev = e;
157 else base->next_keyword = e;
163 base->wanted = base->expected;
168 int arl_find_or_create_and_relink(ARL_BASE *base, const char *s, const char *value) {
171 uint32_t hash = simple_hash(s);
173 // find if it already exists in the data
174 for(e = base->head; e ; e = e->next)
175 if(e->hash == hash && !strcmp(e->name, s))
178 #ifdef NETDATA_INTERNAL_CHECKS
179 if(unlikely(base->next_keyword && e == base->next_keyword))
180 fatal("Internal Error: e == base->last");
184 // found it in the keywords
188 // run the processor for it
189 if(unlikely(e->dst)) {
190 base->processor(e->name, hash, value, e->dst);
194 // unlink it - we will relink it below
195 if(e->next) e->next->prev = e->prev;
196 if(e->prev) e->prev->next = e->next;
198 // make sure the head is properly linked
200 base->head = e->next;
206 e = callocz(1, sizeof(ARL_ENTRY));
207 e->name = strdupz(s);
209 e->flags = ARL_ENTRY_FLAG_DYNAMIC;
215 #ifdef NETDATA_INTERNAL_CHECKS
216 if(unlikely(base->iteration % 60 == 0 && e->flags & ARL_ENTRY_FLAG_FOUND))
217 info("ARL '%s': entry '%s' is already found. Did you forget to call arl_begin()?", base->name, s);
220 e->flags |= ARL_ENTRY_FLAG_FOUND;
223 e->next = base->next_keyword;
224 if(base->next_keyword) {
225 e->prev = base->next_keyword->prev;
226 base->next_keyword->prev = e;
231 if(base->head == base->next_keyword)
237 base->next_keyword = e->next;
238 if(unlikely(!base->next_keyword))
239 base->next_keyword = base->head;
241 if(unlikely(base->found == base->wanted))