@cryptotaxi247 / netdata-1 / commits / 845a6a9fc

40% faster AVL searching and DICTIONARY operations

Costa Tsaousis (ktsaou) committed May 16, 2016 at 02:40 UTC 845a6a9fc1fe325f19fce1fbd34aa240ef70402a
10 files changed +275 -264
profile/benchmark-dictionary.c
+2 -2
@@ -22,14 +22,14 @@ struct myvalue {
22 int main(int argc, char **argv) {
23 if(argc || argv) {;}
24
25 - DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_NAME_CORRUPTION_CHECK);
25 + DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
26 if(!dict) fatal("Cannot create dictionary.");
27
28 struct rusage start, end;
29 unsigned long long dt;
30 char buf[100 + 1];
31 struct myvalue value, *v;
32 - int i, max = 100000, max2;
32 + int i, max = 10000000, max2;
33
34 // ------------------------------------------------------------------------
35
profile/benchmark-registry.c new
+224
@@ -0,0 +1,224 @@
1 +
2 +/*
3 + * compile with
4 + * gcc -O1 -ggdb -Wall -Wextra -I ../src/ -I ../ -o benchmark-registry benchmark-registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o ../src/appconfig.o ../src/web_buffer.o ../src/storage_number.o ../src/rrd.o -pthread -luuid -lm -DHAVE_CONFIG_H -DVARLIB_DIR="\"/tmp\""
5 + */
6 +
7 +char *hostname = "me";
8 +
9 +#include "../src/registry.c"
10 +
11 +// ----------------------------------------------------------------------------
12 +// TESTS
13 +
14 +int test1(int argc, char **argv) {
15 +
16 + void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
17 + fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
18 + requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
19 +
20 + fprintf(stderr, " > DB : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
21 + registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
22 + registry.persons_urls_count, registry.machines_urls_count);
23 + }
24 +
25 + (void) argc;
26 + (void) argv;
27 +
28 + uint32_t u, users = 1000000;
29 + uint32_t m, machines = 200000;
30 + uint32_t machines2 = machines * 2;
31 +
32 + char **users_guids = malloc(users * sizeof(char *));
33 + char **machines_guids = malloc(machines2 * sizeof(char *));
34 + char **machines_urls = malloc(machines2 * sizeof(char *));
35 + unsigned long long start;
36 +
37 + registry_init();
38 +
39 + fprintf(stderr, "Generating %u machine guids\n", machines2);
40 + for(m = 0; m < machines2 ;m++) {
41 + uuid_t uuid;
42 + machines_guids[m] = malloc(36+1);
43 + uuid_generate(uuid);
44 + uuid_unparse(uuid, machines_guids[m]);
45 +
46 + char buf[FILENAME_MAX + 1];
47 + snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
48 + machines_urls[m] = strdup(buf);
49 +
50 + // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
51 + }
52 +
53 + start = timems();
54 + fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
55 + m = 0;
56 + time_t now = time(NULL);
57 + for(u = 0; u < users ; u++) {
58 + if(++m == machines) m = 0;
59 +
60 + PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
61 + users_guids[u] = p->guid;
62 + }
63 + print_stats(u, start, timems());
64 +
65 + start = timems();
66 + fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
67 + m = 0;
68 + now = time(NULL);
69 + for(u = 0; u < users ; u++) {
70 + if(++m == machines) m = 0;
71 +
72 + PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
73 +
74 + if(p->guid != users_guids[u])
75 + fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
76 + }
77 + print_stats(u, start, timems());
78 +
79 + start = timems();
80 + fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
81 + m = 1;
82 + now = time(NULL);
83 + for(u = 0; u < users ; u++) {
84 + if(++m == machines) m = 0;
85 +
86 + PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
87 +
88 + if(p->guid != users_guids[u])
89 + fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
90 + }
91 + print_stats(u, start, timems());
92 +
93 + start = timems();
94 + fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
95 + now = time(NULL);
96 + for(u = 0; u < users ; u++) {
97 + uint32_t tu = random() * users / RAND_MAX;
98 + uint32_t tm = random() * machines / RAND_MAX;
99 +
100 + PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
101 +
102 + if(p->guid != users_guids[tu])
103 + fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
104 + }
105 + print_stats(u, start, timems());
106 +
107 + start = timems();
108 + fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
109 + now = time(NULL);
110 + for(u = 0; u < users ; u++) {
111 + uint32_t tu = random() * users / RAND_MAX;
112 + uint32_t tm = random() * machines2 / RAND_MAX;
113 +
114 + PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
115 +
116 + if(p->guid != users_guids[tu])
117 + fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
118 + }
119 + print_stats(u, start, timems());
120 +
121 + for(m = 0; m < 10; m++) {
122 + start = timems();
123 + fprintf(stderr,
124 + "\n%u random user accesses to a random server, out of %u servers,\n > using 1/10000 with a random url, 1/1000 with a mismatched url\n",
125 + users * 2, machines2);
126 + now = time(NULL);
127 + for (u = 0; u < users * 2; u++) {
128 + uint32_t tu = random() * users / RAND_MAX;
129 + uint32_t tm = random() * machines2 / RAND_MAX;
130 +
131 + char *url = machines_urls[tm];
132 + char buf[FILENAME_MAX + 1];
133 + if (random() % 10000 == 1234) {
134 + snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
135 + url = buf;
136 + }
137 + else if (random() % 1000 == 123)
138 + url = machines_urls[random() * machines2 / RAND_MAX];
139 +
140 + PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
141 +
142 + if (p->guid != users_guids[tu])
143 + fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
144 + }
145 + print_stats(u, start, timems());
146 + }
147 +
148 + fprintf(stderr, "\n\nSAVE\n");
149 + start = timems();
150 + registry_save();
151 + print_stats(registry.persons_count, start, timems());
152 +
153 + fprintf(stderr, "\n\nCLEANUP\n");
154 + start = timems();
155 + registry_free();
156 + print_stats(registry.persons_count, start, timems());
157 + return 0;
158 +}
159 +
160 +// ----------------------------------------------------------------------------
161 +// TESTING
162 +
163 +int main(int argc, char **argv) {
164 + config_set_boolean("registry", "enabled", 1);
165 +
166 + //debug_flags = 0xFFFFFFFF;
167 + test1(argc, argv);
168 + exit(0);
169 +
170 + (void)argc;
171 + (void)argv;
172 +
173 +
174 + PERSON *p1, *p2;
175 +
176 + fprintf(stderr, "\n\nINITIALIZATION\n");
177 +
178 + registry_init();
179 +
180 + int i = 2;
181 +
182 + fprintf(stderr, "\n\nADDING ENTRY\n");
183 + p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
184 +
185 + if(0)
186 + while(i--) {
187 +#ifdef REGISTRY_STDOUT_DUMP
188 + fprintf(stderr, "\n\nADDING ENTRY\n");
189 +#endif /* REGISTRY_STDOUT_DUMP */
190 + p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
191 +
192 +#ifdef REGISTRY_STDOUT_DUMP
193 + fprintf(stderr, "\n\nADDING ANOTHER URL\n");
194 +#endif /* REGISTRY_STDOUT_DUMP */
195 + p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
196 +
197 +#ifdef REGISTRY_STDOUT_DUMP
198 + fprintf(stderr, "\n\nADDING ANOTHER URL\n");
199 +#endif /* REGISTRY_STDOUT_DUMP */
200 + p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
201 +
202 +#ifdef REGISTRY_STDOUT_DUMP
203 + fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
204 +#endif /* REGISTRY_STDOUT_DUMP */
205 + p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
206 +
207 +#ifdef REGISTRY_STDOUT_DUMP
208 + fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
209 +#endif /* REGISTRY_STDOUT_DUMP */
210 + p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
211 +
212 +#ifdef REGISTRY_STDOUT_DUMP
213 + fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
214 +#endif /* REGISTRY_STDOUT_DUMP */
215 + p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
216 + }
217 +
218 + fprintf(stderr, "\n\nSAVE\n");
219 + registry_save();
220 +
221 + fprintf(stderr, "\n\nCLEANUP\n");
222 + registry_free();
223 + return 0;
224 +}
src/appconfig.c
+4 -10
@@ -84,8 +84,6 @@ static inline void config_section_unlock(struct config *co) {
84 // ----------------------------------------------------------------------------
85 // config name-value index
86
87 -static int config_value_iterator(avl *a) { if(a) {}; return 0; }
88 -
87 static int config_value_compare(void* a, void* b) {
88 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
89 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
@@ -96,20 +94,17 @@ static int config_value_compare(void* a, void* b) {
94 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
95
96 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
99 - struct config_value *result = NULL, tmp;
97 + struct config_value tmp;
98 tmp.hash = (hash)?hash:simple_hash(name);
99 tmp.name = (char *)name;
100
103 - avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result);
104 - return result;
101 + return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
102 }
103
104
105 // ----------------------------------------------------------------------------
106 // config sections index
107
111 -static int config_iterator(avl *a) { if(a) {}; return 0; }
112 -
108 static int config_compare(void* a, void* b) {
109 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
110 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
@@ -125,12 +120,11 @@ avl_tree_lock config_root_index = {
120 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
121
122 static struct config *config_index_find(const char *name, uint32_t hash) {
128 - struct config *result = NULL, tmp;
123 + struct config tmp;
124 tmp.hash = (hash)?hash:simple_hash(name);
125 tmp.name = (char *)name;
126
132 - avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result);
133 - return result;
127 + return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
128 }
129
130
src/apps_plugin.c
+2 -3
@@ -1005,7 +1005,7 @@ avl_tree all_files_index = {
1005 };
1006
1007 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
1008 - struct file_descriptor *result = NULL, tmp;
1008 + struct file_descriptor tmp;
1009 tmp.hash = (hash)?hash:simple_hash(name);
1010 tmp.name = name;
1011 tmp.count = 0;
@@ -1014,8 +1014,7 @@ static struct file_descriptor *file_descriptor_find(const char *name, uint32_t h
1014 tmp.magic = 0x0BADCAFE;
1015 #endif /* NETDATA_INTERNAL_CHECKS */
1016
1017 - avl_search(&all_files_index, (avl *) &tmp, file_descriptor_iterator, (avl **) &result);
1018 - return result;
1017 + return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
1018 }
1019
1020 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
src/avl.c
+30
@@ -309,6 +309,29 @@ int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) {
309 return c;
310 }
311
312 +/* high performance searching - by ktsaou */
313 +avl *avl_search(avl_tree *t, avl *a) {
314 + avl *root = t->root;
315 +
316 + while(root) {
317 + int x = t->compar(root, a);
318 +
319 + if(x > 0) {
320 + root = root->left;
321 + continue;
322 + }
323 +
324 + if(x < 0) {
325 + root = root->right;
326 + continue;
327 + }
328 +
329 + return root;
330 + }
331 +
332 + return NULL;
333 +}
334 +
335 void avl_init(avl_tree *t, int (*compar)(void *a, void *b)) {
336 t->root = NULL;
337 t->compar = compar;
@@ -366,6 +389,13 @@ void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b)) {
389 #endif /* AVL_WITHOUT_PTHREADS */
390 }
391
392 +avl *avl_search_lock(avl_tree_lock *t, avl *a) {
393 + avl_read_lock(t);
394 + avl *ret = avl_search(&t->avl_tree, a);
395 + avl_unlock(t);
396 + return ret;
397 +}
398 +
399 int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret) {
400 avl_read_lock(t);
401 int ret2 = avl_range(&t->avl_tree, a, b, iter, ret);
src/avl.h
+2 -2
@@ -93,8 +93,8 @@ int avl_range(avl_tree *t, avl *a, avl *b, int (*iter)(avl *), avl **ret);
93 * for each element calls iter(a) until it returns 0
94 * returns the last value returned by iterator or 0 if there were no calls
95 */
96 -#define avl_search_lock(t, a, iter, ret) avl_range_lock(t, a, a, iter, ret)
97 -#define avl_search(t, a, iter, ret) avl_range(t, a, a, iter, ret)
96 +avl *avl_search_lock(avl_tree_lock *t, avl *a);
97 +avl *avl_search(avl_tree *t, avl *a);
98
99 /* Initialize the avl_tree_lock
100 */
src/dictionary.c
+2 -6
@@ -40,8 +40,6 @@ static inline void dictionary_unlock(DICTIONARY *dict) {
40 // ----------------------------------------------------------------------------
41 // avl index
42
43 -static int name_value_iterator(avl *a) { if(a) {}; return 0; }
44 -
43 static int name_value_compare(void* a, void* b) {
44 if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
45 else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
@@ -52,14 +50,12 @@ static int name_value_compare(void* a, void* b) {
50 #define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
51
52 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
55 - NAME_VALUE *result = NULL, tmp;
53 + NAME_VALUE tmp;
54 tmp.hash = (hash)?hash:simple_hash(name);
55 tmp.name = (char *)name;
56
57 dict->searches++;
60 - avl_search(&(dict->values_index), (avl *) &tmp, name_value_iterator, (avl **) &result);
61 -
62 - return result;
58 + return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
59 }
60
61 // ----------------------------------------------------------------------------
src/plugin_tc.c
+4 -10
@@ -83,8 +83,6 @@ struct tc_device *tc_device_root = NULL;
83 // ----------------------------------------------------------------------------
84 // tc_device index
85
86 -static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
87 -
86 static int tc_device_compare(void* a, void* b) {
87 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
88 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
@@ -100,20 +98,17 @@ avl_tree tc_device_root_index = {
98 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
99
100 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
103 - struct tc_device *result = NULL, tmp;
101 + struct tc_device tmp;
102 tmp.id = (char *)id;
103 tmp.hash = (hash)?hash:simple_hash(tmp.id);
104
107 - avl_search(&(tc_device_root_index), (avl *) &tmp, tc_device_iterator, (avl **) &result);
108 - return result;
105 + return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
106 }
107
108
109 // ----------------------------------------------------------------------------
110 // tc_class index
111
115 -static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
116 -
112 static int tc_class_compare(void* a, void* b) {
113 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
114 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
@@ -124,12 +119,11 @@ static int tc_class_compare(void* a, void* b) {
119 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
120
121 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
127 - struct tc_class *result = NULL, tmp;
122 + struct tc_class tmp;
123 tmp.id = (char *)id;
124 tmp.hash = (hash)?hash:simple_hash(tmp.id);
125
131 - avl_search(&(st->classes_index), (avl *) &tmp, tc_class_iterator, (avl **) &result);
132 - return result;
126 + return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
127 }
128
129 // ----------------------------------------------------------------------------
src/registry.c
-218
@@ -1836,221 +1836,3 @@ void registry_statistics(void) {
1836 rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));
1837 rrdset_done(stm);
1838 }
1839 -
1840 -
1841 -#ifdef REGISTRY_STANDALONE_TESTS
1842 -
1843 -// ----------------------------------------------------------------------------
1844 -// TESTS
1845 -
1846 -int test1(int argc, char **argv) {
1847 -
1848 - void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
1849 - fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
1850 - requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
1851 -
1852 - fprintf(stderr, " > DB : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
1853 - registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
1854 - registry.persons_urls_count, registry.machines_urls_count);
1855 - }
1856 -
1857 - (void) argc;
1858 - (void) argv;
1859 -
1860 - uint32_t u, users = 1000000;
1861 - uint32_t m, machines = 200000;
1862 - uint32_t machines2 = machines * 2;
1863 -
1864 - char **users_guids = malloc(users * sizeof(char *));
1865 - char **machines_guids = malloc(machines2 * sizeof(char *));
1866 - char **machines_urls = malloc(machines2 * sizeof(char *));
1867 - unsigned long long start;
1868 -
1869 - registry_init();
1870 -
1871 - fprintf(stderr, "Generating %u machine guids\n", machines2);
1872 - for(m = 0; m < machines2 ;m++) {
1873 - uuid_t uuid;
1874 - machines_guids[m] = malloc(36+1);
1875 - uuid_generate(uuid);
1876 - uuid_unparse(uuid, machines_guids[m]);
1877 -
1878 - char buf[FILENAME_MAX + 1];
1879 - snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
1880 - machines_urls[m] = strdup(buf);
1881 -
1882 - // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
1883 - }
1884 -
1885 - start = timems();
1886 - fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
1887 - m = 0;
1888 - time_t now = time(NULL);
1889 - for(u = 0; u < users ; u++) {
1890 - if(++m == machines) m = 0;
1891 -
1892 - PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
1893 - users_guids[u] = p->guid;
1894 - }
1895 - print_stats(u, start, timems());
1896 -
1897 - start = timems();
1898 - fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
1899 - m = 0;
1900 - now = time(NULL);
1901 - for(u = 0; u < users ; u++) {
1902 - if(++m == machines) m = 0;
1903 -
1904 - PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
1905 -
1906 - if(p->guid != users_guids[u])
1907 - fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
1908 - }
1909 - print_stats(u, start, timems());
1910 -
1911 - start = timems();
1912 - fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
1913 - m = 1;
1914 - now = time(NULL);
1915 - for(u = 0; u < users ; u++) {
1916 - if(++m == machines) m = 0;
1917 -
1918 - PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
1919 -
1920 - if(p->guid != users_guids[u])
1921 - fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
1922 - }
1923 - print_stats(u, start, timems());
1924 -
1925 - start = timems();
1926 - fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
1927 - now = time(NULL);
1928 - for(u = 0; u < users ; u++) {
1929 - uint32_t tu = random() * users / RAND_MAX;
1930 - uint32_t tm = random() * machines / RAND_MAX;
1931 -
1932 - PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
1933 -
1934 - if(p->guid != users_guids[tu])
1935 - fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1936 - }
1937 - print_stats(u, start, timems());
1938 -
1939 - start = timems();
1940 - fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
1941 - now = time(NULL);
1942 - for(u = 0; u < users ; u++) {
1943 - uint32_t tu = random() * users / RAND_MAX;
1944 - uint32_t tm = random() * machines2 / RAND_MAX;
1945 -
1946 - PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
1947 -
1948 - if(p->guid != users_guids[tu])
1949 - fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1950 - }
1951 - print_stats(u, start, timems());
1952 -
1953 - for(m = 0; m < 10; m++) {
1954 - start = timems();
1955 - fprintf(stderr,
1956 - "\n%u random user accesses to a random server, out of %u servers,\n > using 1/10000 with a random url, 1/1000 with a mismatched url\n",
1957 - users * 2, machines2);
1958 - now = time(NULL);
1959 - for (u = 0; u < users * 2; u++) {
1960 - uint32_t tu = random() * users / RAND_MAX;
1961 - uint32_t tm = random() * machines2 / RAND_MAX;
1962 -
1963 - char *url = machines_urls[tm];
1964 - char buf[FILENAME_MAX + 1];
1965 - if (random() % 10000 == 1234) {
1966 - snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
1967 - url = buf;
1968 - }
1969 - else if (random() % 1000 == 123)
1970 - url = machines_urls[random() * machines2 / RAND_MAX];
1971 -
1972 - PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
1973 -
1974 - if (p->guid != users_guids[tu])
1975 - fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1976 - }
1977 - print_stats(u, start, timems());
1978 - }
1979 -
1980 - fprintf(stderr, "\n\nSAVE\n");
1981 - start = timems();
1982 - registry_save();
1983 - print_stats(registry.persons_count, start, timems());
1984 -
1985 - fprintf(stderr, "\n\nCLEANUP\n");
1986 - start = timems();
1987 - registry_free();
1988 - print_stats(registry.persons_count, start, timems());
1989 - return 0;
1990 -}
1991 -
1992 -// ----------------------------------------------------------------------------
1993 -// TESTING
1994 -
1995 -int main(int argc, char **argv) {
1996 - // debug_flags = 0xFFFFFFFF;
1997 - // test1(argc, argv);
1998 - // exit(0);
1999 -
2000 - (void)argc;
2001 - (void)argv;
2002 -
2003 -
2004 - PERSON *p1, *p2;
2005 -
2006 - fprintf(stderr, "\n\nINITIALIZATION\n");
2007 -
2008 - registry_init();
2009 -
2010 - int i = 2;
2011 -
2012 - fprintf(stderr, "\n\nADDING ENTRY\n");
2013 - p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
2014 -
2015 - if(0)
2016 - while(i--) {
2017 -#ifdef REGISTRY_STDOUT_DUMP
2018 - fprintf(stderr, "\n\nADDING ENTRY\n");
2019 -#endif /* REGISTRY_STDOUT_DUMP */
2020 - p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
2021 -
2022 -#ifdef REGISTRY_STDOUT_DUMP
2023 - fprintf(stderr, "\n\nADDING ANOTHER URL\n");
2024 -#endif /* REGISTRY_STDOUT_DUMP */
2025 - p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
2026 -
2027 -#ifdef REGISTRY_STDOUT_DUMP
2028 - fprintf(stderr, "\n\nADDING ANOTHER URL\n");
2029 -#endif /* REGISTRY_STDOUT_DUMP */
2030 - p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
2031 -
2032 -#ifdef REGISTRY_STDOUT_DUMP
2033 - fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
2034 -#endif /* REGISTRY_STDOUT_DUMP */
2035 - p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
2036 -
2037 -#ifdef REGISTRY_STDOUT_DUMP
2038 - fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
2039 -#endif /* REGISTRY_STDOUT_DUMP */
2040 - p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
2041 -
2042 -#ifdef REGISTRY_STDOUT_DUMP
2043 - fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
2044 -#endif /* REGISTRY_STDOUT_DUMP */
2045 - p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
2046 - }
2047 -
2048 - fprintf(stderr, "\n\nSAVE\n");
2049 - registry_save();
2050 -
2051 - fprintf(stderr, "\n\nCLEANUP\n");
2052 - registry_free();
2053 - return 0;
2054 -}
2055 -
2056 -#endif /* REGISTRY_STANDALONE_TESTS */
src/rrd.c
+5 -13
@@ -42,8 +42,6 @@ int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
42 // ----------------------------------------------------------------------------
43 // RRDSET index
44
45 -static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
46 -
45 static int rrdset_compare(void* a, void* b) {
46 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
47 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
@@ -59,12 +57,11 @@ avl_tree_lock rrdset_root_index = {
57 #define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
58
59 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
62 - RRDSET *result = NULL, tmp;
60 + RRDSET tmp;
61 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
62 tmp.hash = (hash)?hash:simple_hash(tmp.id);
63
66 - avl_search_lock(&(rrdset_root_index), (avl *) &tmp, rrdset_iterator, (avl **) &result);
67 - return result;
64 + return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
65 }
66
67 // ----------------------------------------------------------------------------
@@ -72,8 +69,6 @@ static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
69
70 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
71
75 -static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
76 -
72 static int rrdset_compare_name(void* a, void* b) {
73 RRDSET *A = rrdset_from_avlname(a);
74 RRDSET *B = rrdset_from_avlname(b);
@@ -104,7 +99,7 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
99 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
100
101 // fprintf(stderr, "SEARCHING: %s\n", name);
107 - avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)), rrdset_iterator_name, (avl **) &result);
102 + result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
103 if(result) {
104 RRDSET *st = rrdset_from_avlname(result);
105 if(strcmp(st->magic, RRDSET_MAGIC))
@@ -121,8 +116,6 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
116 // ----------------------------------------------------------------------------
117 // RRDDIM index
118
124 -static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
125 -
119 static int rrddim_compare(void* a, void* b) {
120 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
121 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
@@ -133,12 +126,11 @@ static int rrddim_compare(void* a, void* b) {
126 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
127
128 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
136 - RRDDIM *result = NULL, tmp;
129 + RRDDIM tmp;
130 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
131 tmp.hash = (hash)?hash:simple_hash(tmp.id);
132
140 - avl_search_lock(&(st->dimensions_index), (avl *) &tmp, rrddim_iterator, (avl **) &result);
141 - return result;
133 + return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
134 }
135
136 // ----------------------------------------------------------------------------