From 845a6a9fc1fe325f19fce1fbd34aa240ef70402a Mon Sep 17 00:00:00 2001 From: "Costa Tsaousis (ktsaou)" Date: Mon, 16 May 2016 02:40:29 +0300 Subject: [PATCH] 40% faster AVL searching and DICTIONARY operations --- profile/benchmark-dictionary.c | 4 +- profile/benchmark-registry.c | 224 +++++++++++++++++++++++++++++++++ src/appconfig.c | 14 +-- src/apps_plugin.c | 5 +- src/avl.c | 30 +++++ src/avl.h | 4 +- src/dictionary.c | 8 +- src/plugin_tc.c | 14 +-- src/registry.c | 218 -------------------------------- src/rrd.c | 18 +-- 10 files changed, 275 insertions(+), 264 deletions(-) create mode 100755 profile/benchmark-registry.c diff --git a/profile/benchmark-dictionary.c b/profile/benchmark-dictionary.c index 8c414db8..61f0d02d 100644 --- a/profile/benchmark-dictionary.c +++ b/profile/benchmark-dictionary.c @@ -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 index 00000000..68475eae --- /dev/null +++ b/profile/benchmark-registry.c @@ -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; +} diff --git a/src/appconfig.c b/src/appconfig.c index b43485cc..0ec4cad3 100644 --- a/src/appconfig.c +++ b/src/appconfig.c @@ -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); } diff --git a/src/apps_plugin.c b/src/apps_plugin.c index 8a330ae7..0bcdfcf5 100644 --- a/src/apps_plugin.c +++ b/src/apps_plugin.c @@ -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)) diff --git a/src/avl.c b/src/avl.c index db198494..067b0b36 100644 --- 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); diff --git a/src/avl.h b/src/avl.h index 04e95ea8..5397b196 100644 --- 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 */ diff --git a/src/dictionary.c b/src/dictionary.c index b2151ff6..1543f4d0 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -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); } // ---------------------------------------------------------------------------- diff --git a/src/plugin_tc.c b/src/plugin_tc.c index 511dd162..3d3e3521 100644 --- a/src/plugin_tc.c +++ b/src/plugin_tc.c @@ -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); } // ---------------------------------------------------------------------------- diff --git a/src/registry.c b/src/registry.c index 410d3a48..f39ce3e2 100644 --- a/src/registry.c +++ b/src/registry.c @@ -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 */ diff --git a/src/rrd.c b/src/rrd.c index 5971afb2..ee23da0c 100644 --- 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); } // ---------------------------------------------------------------------------- -- 2.39.2