]> arthur.barton.de Git - netdata.git/commitdiff
40% faster AVL searching and DICTIONARY operations
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 15 May 2016 23:40:29 +0000 (02:40 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 15 May 2016 23:40:29 +0000 (02:40 +0300)
profile/benchmark-dictionary.c
profile/benchmark-registry.c [new file with mode: 0755]
src/appconfig.c
src/apps_plugin.c
src/avl.c
src/avl.h
src/dictionary.c
src/plugin_tc.c
src/registry.c
src/rrd.c

index 8c414db8e7c4c02c0b55b583821785d02e434719..61f0d02ddadc7cab1fc76e631dc3ee9b517ea430 100644 (file)
@@ -22,14 +22,14 @@ struct myvalue {
 int main(int argc, char **argv) {
        if(argc || argv) {;}
 
-       DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_NAME_CORRUPTION_CHECK);
+       DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
        if(!dict) fatal("Cannot create dictionary.");
 
        struct rusage start, end;
        unsigned long long dt;
        char buf[100 + 1];
        struct myvalue value, *v;
-       int i, max = 100000, max2;
+       int i, max = 10000000, max2;
 
        // ------------------------------------------------------------------------
 
diff --git a/profile/benchmark-registry.c b/profile/benchmark-registry.c
new file mode 100755 (executable)
index 0000000..68475ea
--- /dev/null
@@ -0,0 +1,224 @@
+
+/*
+ * compile with
+ *  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\""
+ */
+
+char *hostname = "me";
+
+#include "../src/registry.c"
+
+// ----------------------------------------------------------------------------
+// TESTS
+
+int test1(int argc, char **argv) {
+
+       void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
+               fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
+                               requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
+
+               fprintf(stderr, " > DB   : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
+                               registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
+                               registry.persons_urls_count, registry.machines_urls_count);
+       }
+
+       (void) argc;
+       (void) argv;
+
+       uint32_t u, users = 1000000;
+       uint32_t m, machines = 200000;
+       uint32_t machines2 = machines * 2;
+
+       char **users_guids = malloc(users * sizeof(char *));
+       char **machines_guids = malloc(machines2 * sizeof(char *));
+       char **machines_urls = malloc(machines2 * sizeof(char *));
+       unsigned long long start;
+
+       registry_init();
+
+       fprintf(stderr, "Generating %u machine guids\n", machines2);
+       for(m = 0; m < machines2 ;m++) {
+               uuid_t uuid;
+               machines_guids[m] = malloc(36+1);
+               uuid_generate(uuid);
+               uuid_unparse(uuid, machines_guids[m]);
+
+               char buf[FILENAME_MAX + 1];
+               snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
+               machines_urls[m] = strdup(buf);
+
+               // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
+       }
+
+       start = timems();
+       fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
+       m = 0;
+       time_t now = time(NULL);
+       for(u = 0; u < users ; u++) {
+               if(++m == machines) m = 0;
+
+               PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
+               users_guids[u] = p->guid;
+       }
+       print_stats(u, start, timems());
+
+       start = timems();
+       fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
+       m = 0;
+       now = time(NULL);
+       for(u = 0; u < users ; u++) {
+               if(++m == machines) m = 0;
+
+               PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
+
+               if(p->guid != users_guids[u])
+                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
+       }
+       print_stats(u, start, timems());
+
+       start = timems();
+       fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
+       m = 1;
+       now = time(NULL);
+       for(u = 0; u < users ; u++) {
+               if(++m == machines) m = 0;
+
+               PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
+
+               if(p->guid != users_guids[u])
+                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
+       }
+       print_stats(u, start, timems());
+
+       start = timems();
+       fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
+       now = time(NULL);
+       for(u = 0; u < users ; u++) {
+               uint32_t tu = random() * users / RAND_MAX;
+               uint32_t tm = random() * machines / RAND_MAX;
+
+               PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
+
+               if(p->guid != users_guids[tu])
+                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
+       }
+       print_stats(u, start, timems());
+
+       start = timems();
+       fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
+       now = time(NULL);
+       for(u = 0; u < users ; u++) {
+               uint32_t tu = random() * users / RAND_MAX;
+               uint32_t tm = random() * machines2 / RAND_MAX;
+
+               PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
+
+               if(p->guid != users_guids[tu])
+                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
+       }
+       print_stats(u, start, timems());
+
+       for(m = 0; m < 10; m++) {
+               start = timems();
+               fprintf(stderr,
+                               "\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",
+                               users * 2, machines2);
+               now = time(NULL);
+               for (u = 0; u < users * 2; u++) {
+                       uint32_t tu = random() * users / RAND_MAX;
+                       uint32_t tm = random() * machines2 / RAND_MAX;
+
+                       char *url = machines_urls[tm];
+                       char buf[FILENAME_MAX + 1];
+                       if (random() % 10000 == 1234) {
+                               snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
+                               url = buf;
+                       }
+                       else if (random() % 1000 == 123)
+                               url = machines_urls[random() * machines2 / RAND_MAX];
+
+                       PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
+
+                       if (p->guid != users_guids[tu])
+                               fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
+               }
+               print_stats(u, start, timems());
+       }
+
+       fprintf(stderr, "\n\nSAVE\n");
+       start = timems();
+       registry_save();
+       print_stats(registry.persons_count, start, timems());
+
+       fprintf(stderr, "\n\nCLEANUP\n");
+       start = timems();
+       registry_free();
+       print_stats(registry.persons_count, start, timems());
+       return 0;
+}
+
+// ----------------------------------------------------------------------------
+// TESTING
+
+int main(int argc, char **argv) {
+       config_set_boolean("registry", "enabled", 1);
+
+       //debug_flags = 0xFFFFFFFF;
+       test1(argc, argv);
+       exit(0);
+
+       (void)argc;
+       (void)argv;
+
+
+       PERSON *p1, *p2;
+
+       fprintf(stderr, "\n\nINITIALIZATION\n");
+
+       registry_init();
+
+       int i = 2;
+
+       fprintf(stderr, "\n\nADDING ENTRY\n");
+       p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
+
+       if(0)
+       while(i--) {
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ENTRY\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
+
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ANOTHER URL\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
+
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ANOTHER URL\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
+
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
+
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
+
+#ifdef REGISTRY_STDOUT_DUMP
+               fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
+#endif /* REGISTRY_STDOUT_DUMP */
+               p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
+       }
+
+       fprintf(stderr, "\n\nSAVE\n");
+       registry_save();
+
+       fprintf(stderr, "\n\nCLEANUP\n");
+       registry_free();
+       return 0;
+}
index b43485cc98f059e5aaf933fcf900425eb14c0753..0ec4cad322575ff0852ae189fd87f9bd0e850b8c 100644 (file)
@@ -84,8 +84,6 @@ static inline void config_section_unlock(struct config *co) {
 // ----------------------------------------------------------------------------
 // config name-value index
 
-static int config_value_iterator(avl *a) { if(a) {}; return 0; }
-
 static int config_value_compare(void* a, void* b) {
        if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
        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) {
 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
 
 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
-       struct config_value *result = NULL, tmp;
+       struct config_value tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
        tmp.name = (char *)name;
 
-       avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result);
-       return result;
+       return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
 }
 
 
 // ----------------------------------------------------------------------------
 // config sections index
 
-static int config_iterator(avl *a) { if(a) {}; return 0; }
-
 static int config_compare(void* a, void* b) {
        if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
        else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
@@ -125,12 +120,11 @@ avl_tree_lock config_root_index = {
 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
 
 static struct config *config_index_find(const char *name, uint32_t hash) {
-       struct config *result = NULL, tmp;
+       struct config tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
        tmp.name = (char *)name;
 
-       avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result);
-       return result;
+       return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
 }
 
 
index 8a330ae75e28e997dd7a9f630b9a25397e5339e2..0bcdfcf508a6294d332dd7963b684e77718332ff 100644 (file)
@@ -1005,7 +1005,7 @@ avl_tree all_files_index = {
 };
 
 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
-       struct file_descriptor *result = NULL, tmp;
+       struct file_descriptor tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
        tmp.name = name;
        tmp.count = 0;
@@ -1014,8 +1014,7 @@ static struct file_descriptor *file_descriptor_find(const char *name, uint32_t h
        tmp.magic = 0x0BADCAFE;
 #endif /* NETDATA_INTERNAL_CHECKS */
 
-       avl_search(&all_files_index, (avl *) &tmp, file_descriptor_iterator, (avl **) &result);
-       return result;
+       return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
 }
 
 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
index db1984945380407f8b8100e76096fa8f5cd3bc93..067b0b3610d9a7e01d7b1b9248519191d8466e6b 100644 (file)
--- a/src/avl.c
+++ b/src/avl.c
@@ -309,6 +309,29 @@ int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) {
        return c;
 }
 
+/* high performance searching - by ktsaou */
+avl *avl_search(avl_tree *t, avl *a) {
+       avl *root = t->root;
+
+       while(root) {
+               int x = t->compar(root, a);
+
+               if(x > 0) {
+                       root = root->left;
+                       continue;
+               }
+
+               if(x < 0) {
+                       root = root->right;
+                       continue;
+               }
+
+               return root;
+       }
+
+       return NULL;
+}
+
 void avl_init(avl_tree *t, int (*compar)(void *a, void *b)) {
        t->root = NULL;
        t->compar = compar;
@@ -366,6 +389,13 @@ void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b)) {
 #endif /* AVL_WITHOUT_PTHREADS */
 }
 
+avl *avl_search_lock(avl_tree_lock *t, avl *a) {
+       avl_read_lock(t);
+       avl *ret = avl_search(&t->avl_tree, a);
+       avl_unlock(t);
+       return ret;
+}
+
 int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret) {
        avl_read_lock(t);
        int ret2 = avl_range(&t->avl_tree, a, b, iter, ret);
index 04e95ea80c2501cd4aa83cd7c5eb4cc0a231faff..5397b196e81a9b34b7f980d40cbe2cdcad0af50f 100644 (file)
--- a/src/avl.h
+++ b/src/avl.h
@@ -93,8 +93,8 @@ int avl_range(avl_tree *t, avl *a, avl *b, int (*iter)(avl *), avl **ret);
  * for each element calls iter(a) until it returns 0
  * returns the last value returned by iterator or 0 if there were no calls
  */
-#define avl_search_lock(t, a, iter, ret) avl_range_lock(t, a, a, iter, ret)
-#define avl_search(t, a, iter, ret) avl_range(t, a, a, iter, ret)
+avl *avl_search_lock(avl_tree_lock *t, avl *a);
+avl *avl_search(avl_tree *t, avl *a);
 
 /* Initialize the avl_tree_lock
  */
index b2151ff6d813f688f0c543951a272fe7aef82849..1543f4d0e7334bce5ad63e2b5469e5b47b0dffb8 100644 (file)
@@ -40,8 +40,6 @@ static inline void dictionary_unlock(DICTIONARY *dict) {
 // ----------------------------------------------------------------------------
 // avl index
 
-static int name_value_iterator(avl *a) { if(a) {}; return 0; }
-
 static int name_value_compare(void* a, void* b) {
        if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
        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) {
 #define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
 
 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
-       NAME_VALUE *result = NULL, tmp;
+       NAME_VALUE tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
        tmp.name = (char *)name;
 
        dict->searches++;
-       avl_search(&(dict->values_index), (avl *) &tmp, name_value_iterator, (avl **) &result);
-
-       return result;
+       return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
index 511dd1622ddf8124e390c883a024e44d89348fcf..3d3e35217c0404ed6491cc02a545170f6ce6044a 100644 (file)
@@ -83,8 +83,6 @@ struct tc_device *tc_device_root = NULL;
 // ----------------------------------------------------------------------------
 // tc_device index
 
-static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
-
 static int tc_device_compare(void* a, void* b) {
        if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
        else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
@@ -100,20 +98,17 @@ avl_tree tc_device_root_index = {
 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
 
 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
-       struct tc_device *result = NULL, tmp;
+       struct tc_device tmp;
        tmp.id = (char *)id;
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search(&(tc_device_root_index), (avl *) &tmp, tc_device_iterator, (avl **) &result);
-       return result;
+       return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
 }
 
 
 // ----------------------------------------------------------------------------
 // tc_class index
 
-static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
-
 static int tc_class_compare(void* a, void* b) {
        if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
        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) {
 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
 
 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
-       struct tc_class *result = NULL, tmp;
+       struct tc_class tmp;
        tmp.id = (char *)id;
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search(&(st->classes_index), (avl *) &tmp, tc_class_iterator, (avl **) &result);
-       return result;
+       return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
index 410d3a48bbd41ae7e0a3c7e983f4fd9bd0f83ef1..f39ce3e2edc2c4350e12173b274a018ffc169b77 100644 (file)
@@ -1836,221 +1836,3 @@ void registry_statistics(void) {
        rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));
        rrdset_done(stm);
 }
-
-
-#ifdef REGISTRY_STANDALONE_TESTS
-
-// ----------------------------------------------------------------------------
-// TESTS
-
-int test1(int argc, char **argv) {
-
-       void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
-               fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
-                               requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
-
-               fprintf(stderr, " > DB   : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
-                               registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
-                               registry.persons_urls_count, registry.machines_urls_count);
-       }
-
-       (void) argc;
-       (void) argv;
-
-       uint32_t u, users = 1000000;
-       uint32_t m, machines = 200000;
-       uint32_t machines2 = machines * 2;
-
-       char **users_guids = malloc(users * sizeof(char *));
-       char **machines_guids = malloc(machines2 * sizeof(char *));
-       char **machines_urls = malloc(machines2 * sizeof(char *));
-       unsigned long long start;
-
-       registry_init();
-
-       fprintf(stderr, "Generating %u machine guids\n", machines2);
-       for(m = 0; m < machines2 ;m++) {
-               uuid_t uuid;
-               machines_guids[m] = malloc(36+1);
-               uuid_generate(uuid);
-               uuid_unparse(uuid, machines_guids[m]);
-
-               char buf[FILENAME_MAX + 1];
-               snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
-               machines_urls[m] = strdup(buf);
-
-               // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
-       }
-
-       start = timems();
-       fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
-       m = 0;
-       time_t now = time(NULL);
-       for(u = 0; u < users ; u++) {
-               if(++m == machines) m = 0;
-
-               PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
-               users_guids[u] = p->guid;
-       }
-       print_stats(u, start, timems());
-
-       start = timems();
-       fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
-       m = 0;
-       now = time(NULL);
-       for(u = 0; u < users ; u++) {
-               if(++m == machines) m = 0;
-
-               PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
-
-               if(p->guid != users_guids[u])
-                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
-       }
-       print_stats(u, start, timems());
-
-       start = timems();
-       fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
-       m = 1;
-       now = time(NULL);
-       for(u = 0; u < users ; u++) {
-               if(++m == machines) m = 0;
-
-               PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
-
-               if(p->guid != users_guids[u])
-                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
-       }
-       print_stats(u, start, timems());
-
-       start = timems();
-       fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
-       now = time(NULL);
-       for(u = 0; u < users ; u++) {
-               uint32_t tu = random() * users / RAND_MAX;
-               uint32_t tm = random() * machines / RAND_MAX;
-
-               PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
-
-               if(p->guid != users_guids[tu])
-                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
-       }
-       print_stats(u, start, timems());
-
-       start = timems();
-       fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
-       now = time(NULL);
-       for(u = 0; u < users ; u++) {
-               uint32_t tu = random() * users / RAND_MAX;
-               uint32_t tm = random() * machines2 / RAND_MAX;
-
-               PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
-
-               if(p->guid != users_guids[tu])
-                       fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
-       }
-       print_stats(u, start, timems());
-
-       for(m = 0; m < 10; m++) {
-               start = timems();
-               fprintf(stderr,
-                               "\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",
-                               users * 2, machines2);
-               now = time(NULL);
-               for (u = 0; u < users * 2; u++) {
-                       uint32_t tu = random() * users / RAND_MAX;
-                       uint32_t tm = random() * machines2 / RAND_MAX;
-
-                       char *url = machines_urls[tm];
-                       char buf[FILENAME_MAX + 1];
-                       if (random() % 10000 == 1234) {
-                               snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
-                               url = buf;
-                       }
-                       else if (random() % 1000 == 123)
-                               url = machines_urls[random() * machines2 / RAND_MAX];
-
-                       PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
-
-                       if (p->guid != users_guids[tu])
-                               fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
-               }
-               print_stats(u, start, timems());
-       }
-
-       fprintf(stderr, "\n\nSAVE\n");
-       start = timems();
-       registry_save();
-       print_stats(registry.persons_count, start, timems());
-
-       fprintf(stderr, "\n\nCLEANUP\n");
-       start = timems();
-       registry_free();
-       print_stats(registry.persons_count, start, timems());
-       return 0;
-}
-
-// ----------------------------------------------------------------------------
-// TESTING
-
-int main(int argc, char **argv) {
-       // debug_flags = 0xFFFFFFFF;
-       // test1(argc, argv);
-       // exit(0);
-
-       (void)argc;
-       (void)argv;
-
-
-       PERSON *p1, *p2;
-
-       fprintf(stderr, "\n\nINITIALIZATION\n");
-
-       registry_init();
-
-       int i = 2;
-
-       fprintf(stderr, "\n\nADDING ENTRY\n");
-       p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
-
-       if(0)
-       while(i--) {
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ENTRY\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
-
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ANOTHER URL\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
-
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ANOTHER URL\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
-
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
-
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
-
-#ifdef REGISTRY_STDOUT_DUMP
-               fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
-#endif /* REGISTRY_STDOUT_DUMP */
-               p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
-       }
-
-       fprintf(stderr, "\n\nSAVE\n");
-       registry_save();
-
-       fprintf(stderr, "\n\nCLEANUP\n");
-       registry_free();
-       return 0;
-}
-
-#endif /* REGISTRY_STANDALONE_TESTS */
index 5971afb2f71d8daf3da497d3c42b6dc856ac3ca1..ee23da0c2ebec91184c52d947d1fc46bcba7d2f3 100644 (file)
--- a/src/rrd.c
+++ b/src/rrd.c
@@ -42,8 +42,6 @@ int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
 // ----------------------------------------------------------------------------
 // RRDSET index
 
-static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
-
 static 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;
@@ -59,12 +57,11 @@ avl_tree_lock rrdset_root_index = {
 #define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
 
 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
-       RRDSET *result = NULL, tmp;
+       RRDSET tmp;
        strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search_lock(&(rrdset_root_index), (avl *) &tmp, rrdset_iterator, (avl **) &result);
-       return result;
+       return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
@@ -72,8 +69,6 @@ static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
 
 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
 
-static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
-
 static int rrdset_compare_name(void* a, void* b) {
        RRDSET *A = rrdset_from_avlname(a);
        RRDSET *B = rrdset_from_avlname(b);
@@ -104,7 +99,7 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
        tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
 
        // fprintf(stderr, "SEARCHING: %s\n", name);
-       avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)), rrdset_iterator_name, (avl **) &result);
+       result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
        if(result) {
                RRDSET *st = rrdset_from_avlname(result);
                if(strcmp(st->magic, RRDSET_MAGIC))
@@ -121,8 +116,6 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
 // ----------------------------------------------------------------------------
 // RRDDIM index
 
-static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
-
 static int rrddim_compare(void* a, void* b) {
        if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
        else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
@@ -133,12 +126,11 @@ static int rrddim_compare(void* a, void* b) {
 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
 
 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
-       RRDDIM *result = NULL, tmp;
+       RRDDIM tmp;
        strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search_lock(&(st->dimensions_index), (avl *) &tmp, rrddim_iterator, (avl **) &result);
-       return result;
+       return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------