From: Costa Tsaousis Date: Thu, 12 May 2016 18:09:41 +0000 (+0300) Subject: optimization: saved 84 bytes per dictionary, avl locking removed from tc plugin and... X-Git-Tag: v1.2.0~7^2~25 X-Git-Url: https://arthur.barton.de/gitweb/?p=netdata.git;a=commitdiff_plain;h=55ca63b5467a89fad5c7fbf2862c4efb50188b1e optimization: saved 84 bytes per dictionary, avl locking removed from tc plugin and apps.plugin (they are single threaded) --- diff --git a/src/appconfig.c b/src/appconfig.c index 061fe74f..b76c4f26 100644 --- a/src/appconfig.c +++ b/src/appconfig.c @@ -45,7 +45,7 @@ struct config { char *name; struct config_value *values; - avl_tree values_index; + avl_tree_lock values_index; struct config *next; @@ -64,15 +64,15 @@ static int config_value_compare(void* a, void* b) { else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name); } -#define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv)) -#define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv)) +#define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv)) +#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; tmp.hash = (hash)?hash:simple_hash(name); tmp.name = (char *)name; - avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result); + avl_search_lock(&(co->values_index), (avl *) &tmp, config_value_iterator, (avl **) &result); return result; } @@ -87,27 +87,20 @@ static int config_compare(void* a, void* b) { else return strcmp(((struct config *)a)->name, ((struct config *)b)->name); } -avl_tree config_root_index = { - NULL, - config_compare, -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -#endif +avl_tree_lock config_root_index = { + { NULL, config_compare }, + AVL_LOCK_INITIALIZER }; -#define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg)) -#define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg)) +#define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg)) +#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; tmp.hash = (hash)?hash:simple_hash(name); tmp.name = (char *)name; - avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result); + avl_search_lock(&config_root_index, (avl *) &tmp, config_iterator, (avl **) &result); return result; } @@ -155,7 +148,7 @@ struct config *config_create(const char *section) co->hash = simple_hash(co->name); pthread_rwlock_init(&co->rwlock, NULL); - avl_init(&co->values_index, config_value_compare); + avl_init_lock(&co->values_index, config_value_compare); config_index_add(co); diff --git a/src/apps_plugin.c b/src/apps_plugin.c index b6396bb2..3c4e3ee2 100644 --- a/src/apps_plugin.c +++ b/src/apps_plugin.c @@ -1000,14 +1000,7 @@ int file_descriptor_iterator(avl *a) { if(a) {}; return 0; } avl_tree all_files_index = { NULL, - file_descriptor_compare, -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -#endif /* AVL_WITHOUT_PTHREADS */ + file_descriptor_compare }; static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) { @@ -1020,7 +1013,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); + avl_search(&all_files_index, (avl *) &tmp, file_descriptor_iterator, (avl **) &result); return result; } diff --git a/src/avl.c b/src/avl.c index fd4fb142..c01b471d 100644 --- a/src/avl.c +++ b/src/avl.c @@ -19,9 +19,6 @@ #include "avl.h" #include "log.h" -/* Private methods */ -int _avl_removeroot(avl_tree* t); - /* Swing to the left * Warning: no balance maintainance */ @@ -69,7 +66,7 @@ void avl_nasty(avl* root) { * returns 1 if the depth of the tree has grown * Warning: do not insert elements already present */ -int _avl_insert(avl_tree* t, avl* a) { +int avl_insert(avl_tree* t, avl* a) { /* initialize */ a->left = 0; a->right = 0; @@ -86,7 +83,7 @@ int _avl_insert(avl_tree* t, avl* a) { avl_tree left_subtree; left_subtree.root = t->root->left; left_subtree.compar = t->compar; - if (_avl_insert(&left_subtree, a)) { + if (avl_insert(&left_subtree, a)) { switch (t->root->balance--) { case 1: return 0; @@ -117,7 +114,7 @@ int _avl_insert(avl_tree* t, avl* a) { avl_tree right_subtree; right_subtree.root = t->root->right; right_subtree.compar = t->compar; - if (_avl_insert(&right_subtree, a)) { + if (avl_insert(&right_subtree, a)) { switch (t->root->balance++) { case -1: return 0; @@ -144,36 +141,16 @@ int _avl_insert(avl_tree* t, avl* a) { } } } -int avl_insert(avl_tree* t, avl* a) { -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_lock(&t->mutex); -#else - pthread_rwlock_wrlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - - int ret = _avl_insert(t, a); - -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_unlock(&t->mutex); -#else - pthread_rwlock_unlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - return ret; -} /* Remove an element a from the AVL tree t * returns -1 if the depth of the tree has shrunk * Warning: if the element is not present in the tree, * returns 0 as if it had been removed succesfully. */ -int _avl_remove(avl_tree* t, avl* a) { +int avl_remove(avl_tree* t, avl* a) { int b; if (t->root == a) - return _avl_removeroot(t); + return avl_removeroot(t); b = t->compar(t->root, a); if (b >= 0) { /* remove from the left subtree */ @@ -181,7 +158,7 @@ int _avl_remove(avl_tree* t, avl* a) { avl_tree left_subtree; if ((left_subtree.root = t->root->left)) { left_subtree.compar = t->compar; - ch = _avl_remove(&left_subtree, a); + ch = avl_remove(&left_subtree, a); t->root->left = left_subtree.root; if (ch) { switch (t->root->balance++) { @@ -215,7 +192,7 @@ int _avl_remove(avl_tree* t, avl* a) { avl_tree right_subtree; if ((right_subtree.root = t->root->right)) { right_subtree.compar = t->compar; - ch = _avl_remove(&right_subtree, a); + ch = avl_remove(&right_subtree, a); t->root->right = right_subtree.root; if (ch) { switch (t->root->balance--) { @@ -246,31 +223,10 @@ int _avl_remove(avl_tree* t, avl* a) { return 0; } -int avl_remove(avl_tree* t, avl* a) { -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_lock(&t->mutex); -#else - pthread_rwlock_wrlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - - int ret = _avl_remove(t, a); - -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_unlock(&t->mutex); -#else - pthread_rwlock_unlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - return ret; -} - /* Remove the root of the AVL tree t * Warning: dumps core if t is empty */ -int _avl_removeroot(avl_tree* t) { +int avl_removeroot(avl_tree* t) { int ch; avl* a; if (!t->root->left) { @@ -296,7 +252,7 @@ int _avl_removeroot(avl_tree* t) { while (a->left) a = a->left; } - ch = _avl_remove(t, a); + ch = avl_remove(t, a); a->left = t->root->left; a->right = t->root->right; a->balance = t->root->balance; @@ -306,33 +262,12 @@ int _avl_removeroot(avl_tree* t) { return 0; } -int avl_removeroot(avl_tree* t) { -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_lock(&t->mutex); -#else - pthread_rwlock_wrlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - - int ret = _avl_removeroot(t); - -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_unlock(&t->mutex); -#else - pthread_rwlock_unlock(&t->rwlock); -#endif -#endif /* AVL_WITHOUT_PTHREADS */ - return ret; -} - /* Iterate through elements in t from a range between a and b (inclusive) * for each element calls iter(a) until it returns 0 * returns the last value returned by iterator or 0 if there were no calls * Warning: a<=b must hold */ -int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { +int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { int x, c = 0; if (!t->root) return 0; @@ -349,7 +284,7 @@ int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { avl_tree left_subtree; if ((left_subtree.root = t->root->left)) { left_subtree.compar = t->compar; - if (!(c = _avl_range(&left_subtree, a, b, iter, ret))) + if (!(c = avl_range(&left_subtree, a, b, iter, ret))) if (x > 0) return 0; } @@ -366,7 +301,7 @@ int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { avl_tree right_subtree; if ((right_subtree.root = t->root->right)) { right_subtree.compar = t->compar; - if (!(c = _avl_range(&right_subtree, a, b, iter, ret))) + if (!(c = avl_range(&right_subtree, a, b, iter, ret))) if (x < 0) return 0; } @@ -374,7 +309,14 @@ int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { return c; } -int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { +void avl_init(avl_tree *t, int (*compar)(void *a, void *b)) { + t->root = NULL; + t->compar = compar; +} + +/* ------------------------------------------------------------------------- */ + +void avl_lock(avl_tree_lock *t) { #ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_lock(&t->mutex); @@ -382,9 +324,9 @@ int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { pthread_rwlock_wrlock(&t->rwlock); #endif #endif /* AVL_WITHOUT_PTHREADS */ +} - int ret2 = _avl_range(t, a, b, iter, ret); - +void avl_unlock(avl_tree_lock *t) { #ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_unlock(&t->mutex); @@ -392,21 +334,12 @@ int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { pthread_rwlock_unlock(&t->rwlock); #endif #endif /* AVL_WITHOUT_PTHREADS */ - - return ret2; } -/* Iterate through elements in t equal to a - * for each element calls iter(a) until it returns 0 - * returns the last value returned by iterator or 0 if there were no calls - */ -int avl_search(avl_tree* t, avl* a, int (*iter)(avl* a), avl** ret) { - return avl_range(t, a, a, iter, ret); -} +/* ------------------------------------------------------------------------- */ -void avl_init(avl_tree* t, int (*compar)(void* a, void* b)) { - t->root = NULL; - t->compar = compar; +void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b)) { + avl_init(&t->avl_tree, compar); #ifndef AVL_WITHOUT_PTHREADS int lock; @@ -421,5 +354,32 @@ void avl_init(avl_tree* t, int (*compar)(void* a, void* b)) { fatal("Failed to initialize AVL mutex/rwlock, error: %d", lock); #endif /* AVL_WITHOUT_PTHREADS */ +} + +int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret) { + avl_lock(t); + int ret2 = avl_range(&t->avl_tree, a, b, iter, ret); + avl_unlock(t); + return ret2; +} +int avl_removeroot_lock(avl_tree_lock *t) { + avl_lock(t); + int ret = avl_removeroot(&t->avl_tree); + avl_unlock(t); + return ret; +} + +int avl_remove_lock(avl_tree_lock *t, avl *a) { + avl_lock(t); + int ret = avl_remove(&t->avl_tree, a); + avl_unlock(t); + return ret; +} + +int avl_insert_lock(avl_tree_lock *t, avl *a) { + avl_lock(t); + int ret = avl_insert(&t->avl_tree, a); + avl_lock(t); + return ret; } diff --git a/src/avl.h b/src/avl.h index 2d1fbc53..04e95ea8 100644 --- a/src/avl.h +++ b/src/avl.h @@ -17,10 +17,19 @@ #ifndef AVL_WITHOUT_PTHREADS #include -#endif /* AVL_WITHOUT_PTHREADS */ // #define AVL_LOCK_WITH_MUTEX 1 +#ifdef AVL_LOCK_WITH_MUTEX +#define AVL_LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#else /* AVL_LOCK_WITH_MUTEX */ +#define AVL_LOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER +#endif /* AVL_LOCK_WITH_MUTEX */ + +#else /* AVL_WITHOUT_PTHREADS */ +#define AVL_LOCK_INITIALIZER +#endif /* AVL_WITHOUT_PTHREADS */ + /* Data structures */ /* One element of the AVL tree */ @@ -32,8 +41,13 @@ typedef struct avl { /* An AVL tree */ typedef struct avl_tree { - avl* root; - int (*compar)(void* a, void* b); + avl *root; + + int (*compar)(void *a, void *b); +} avl_tree; + +typedef struct avl_tree_lock { + avl_tree avl_tree; #ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX @@ -42,7 +56,7 @@ typedef struct avl_tree { pthread_rwlock_t rwlock; #endif /* AVL_LOCK_WITH_MUTEX */ #endif /* AVL_WITHOUT_PTHREADS */ -} avl_tree; +} avl_tree_lock; /* Public methods */ @@ -50,35 +64,41 @@ typedef struct avl_tree { * returns 1 if the depth of the tree has grown * Warning: do not insert elements already present */ -int avl_insert(avl_tree* t, avl* a); +int avl_insert_lock(avl_tree_lock *t, avl *a); +int avl_insert(avl_tree *t, avl *a); /* Remove an element a from the AVL tree t * returns -1 if the depth of the tree has shrunk * Warning: if the element is not present in the tree, * returns 0 as if it had been removed succesfully. */ -int avl_remove(avl_tree* t, avl* a); +int avl_remove_lock(avl_tree_lock *t, avl *a); +int avl_remove(avl_tree *t, avl *a); /* Remove the root of the AVL tree t * Warning: dumps core if t is empty */ -int avl_removeroot(avl_tree* t); +int avl_removeroot_lock(avl_tree_lock *t); +int avl_removeroot(avl_tree *t); /* Iterate through elements in t from a range between a and b (inclusive) * for each element calls iter(a) until it returns 0 * returns the last value returned by iterator or 0 if there were no calls * Warning: a<=b must hold */ -int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret); +int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret); +int avl_range(avl_tree *t, avl *a, avl *b, int (*iter)(avl *), avl **ret); /* Iterate through elements in t equal to a * for each element calls iter(a) until it returns 0 * returns the last value returned by iterator or 0 if there were no calls */ -int avl_search(avl_tree* t, avl* a, int (*iter)(avl*), avl** ret); +#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) -/* Initialize the avl_tree +/* Initialize the avl_tree_lock */ -void avl_init(avl_tree* t, int (*compar)(void* a, void* b)); +void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b)); +void avl_init(avl_tree *t, int (*compar)(void *a, void *b)); #endif /* avl.h */ diff --git a/src/dictionary.c b/src/dictionary.c index 1f9a26ae..b2151ff6 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -15,20 +15,6 @@ // ---------------------------------------------------------------------------- // dictionary locks -static inline void dictionary_atomic_updates_lock(DICTIONARY *dict) { - if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) { - // debug(D_DICTIONARY, "Dictionary ATOMIC lock"); - pthread_mutex_lock(&dict->atomic_mutex); - } -} - -static inline void dictionary_atomic_updates_unlock(DICTIONARY *dict) { - if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) { - // debug(D_DICTIONARY, "Dictionary ATOMIC unlock"); - pthread_mutex_unlock(&dict->atomic_mutex); - } -} - static inline void dictionary_read_lock(DICTIONARY *dict) { if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) { // debug(D_DICTIONARY, "Dictionary READ lock"); @@ -71,7 +57,7 @@ static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *di tmp.name = (char *)name; dict->searches++; - avl_search(&(dict->values_index), (avl *)&tmp, name_value_iterator, (avl **)&result); + avl_search(&(dict->values_index), (avl *) &tmp, name_value_iterator, (avl **) &result); return result; } @@ -143,7 +129,6 @@ DICTIONARY *dictionary_create(uint32_t flags) { avl_init(&dict->values_index, name_value_compare); pthread_rwlock_init(&dict->rwlock, NULL); - pthread_mutex_init(&dict->atomic_mutex, NULL); dict->flags = flags; @@ -153,8 +138,6 @@ DICTIONARY *dictionary_create(uint32_t flags) { void dictionary_destroy(DICTIONARY *dict) { debug(D_DICTIONARY, "Destroying dictionary."); - dictionary_atomic_updates_lock(dict); - dictionary_write_lock(dict); while(dict->values_index.root) @@ -162,8 +145,6 @@ void dictionary_destroy(DICTIONARY *dict) { dictionary_unlock(dict); - dictionary_atomic_updates_unlock(dict); - free(dict); } @@ -174,19 +155,13 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val uint32_t hash = simple_hash(name); - dictionary_atomic_updates_lock(dict); + dictionary_write_lock(dict); - dictionary_read_lock(dict); NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash); - dictionary_unlock(dict); - if(unlikely(!nv)) { debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name); - dictionary_write_lock(dict); nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash); - dictionary_unlock(dict); - if(unlikely(!nv)) fatal("Cannot create name_value."); } @@ -214,7 +189,7 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val } } - dictionary_atomic_updates_unlock(dict); + dictionary_unlock(dict); return nv->value; } @@ -240,25 +215,20 @@ int dictionary_del(DICTIONARY *dict, const char *name) { debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name); - dictionary_atomic_updates_lock(dict); + dictionary_write_lock(dict); - dictionary_read_lock(dict); NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0); - dictionary_unlock(dict); - if(unlikely(!nv)) { debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name); ret = -1; } else { debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name); - dictionary_write_lock(dict); dictionary_name_value_destroy_nolock(dict, nv); - dictionary_unlock(dict); ret = 0; } - dictionary_atomic_updates_unlock(dict); + dictionary_unlock(dict); return ret; } diff --git a/src/dictionary.h b/src/dictionary.h index e1320da3..575f2827 100644 --- a/src/dictionary.h +++ b/src/dictionary.h @@ -17,16 +17,16 @@ typedef struct name_value { } NAME_VALUE; typedef struct dictionary { - uint32_t flags; + avl_tree values_index; + + uint8_t flags; unsigned long long inserts; unsigned long long deletes; unsigned long long searches; unsigned long long entries; - avl_tree values_index; pthread_rwlock_t rwlock; - pthread_mutex_t atomic_mutex; } DICTIONARY; #define DICTIONARY_FLAG_DEFAULT 0x00000000 diff --git a/src/plugin_tc.c b/src/plugin_tc.c index d70f5265..7478cd55 100644 --- a/src/plugin_tc.c +++ b/src/plugin_tc.c @@ -93,25 +93,18 @@ static int tc_device_compare(void* a, void* b) { avl_tree tc_device_root_index = { NULL, - tc_device_compare, -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -#endif + tc_device_compare }; #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st)) #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st)) -static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) { +static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) { struct tc_device *result = NULL, 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); + avl_search(&(tc_device_root_index), (avl *) &tmp, tc_device_iterator, (avl **) &result); return result; } @@ -130,18 +123,18 @@ static int tc_class_compare(void* a, void* b) { #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd)) #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd)) -static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) { +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; tmp.id = (char *)id; tmp.hash = (hash)?hash:simple_hash(tmp.id); - avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result); + avl_search(&(st->classes_index), (avl *) &tmp, tc_class_iterator, (avl **) &result); return result; } // ---------------------------------------------------------------------------- -static void tc_class_free(struct tc_device *n, struct tc_class *c) { +static inline void tc_class_free(struct tc_device *n, struct tc_class *c) { debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', seen=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->seen); if(c->next) c->next->prev = c->prev; @@ -161,7 +154,7 @@ static void tc_class_free(struct tc_device *n, struct tc_class *c) { free(c); } -static void tc_device_classes_cleanup(struct tc_device *d) { +static inline void tc_device_classes_cleanup(struct tc_device *d) { static int cleanup_every = 999; if(cleanup_every > 0) { @@ -186,7 +179,7 @@ static void tc_device_classes_cleanup(struct tc_device *d) { } } -static void tc_device_commit(struct tc_device *d) +static inline void tc_device_commit(struct tc_device *d) { static int enable_new_interfaces = -1; @@ -296,7 +289,7 @@ static void tc_device_commit(struct tc_device *d) tc_device_classes_cleanup(d); } -static void tc_device_set_class_name(struct tc_device *d, char *id, char *name) +static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name) { struct tc_class *c = tc_class_index_find(d, id, 0); if(c) { @@ -310,7 +303,7 @@ static void tc_device_set_class_name(struct tc_device *d, char *id, char *name) } } -static void tc_device_set_device_name(struct tc_device *d, char *name) { +static inline void tc_device_set_device_name(struct tc_device *d, char *name) { if(d->name) free(d->name); d->name = NULL; @@ -320,7 +313,7 @@ static void tc_device_set_device_name(struct tc_device *d, char *name) { } } -static void tc_device_set_device_family(struct tc_device *d, char *family) { +static inline void tc_device_set_device_family(struct tc_device *d, char *family) { if(d->family) free(d->family); d->family = NULL; @@ -331,7 +324,7 @@ static void tc_device_set_device_family(struct tc_device *d, char *family) { // no need for null termination - it is already null } -static struct tc_device *tc_device_create(char *id) +static inline struct tc_device *tc_device_create(char *id) { struct tc_device *d = tc_device_index_find(id, 0); @@ -347,20 +340,7 @@ static struct tc_device *tc_device_create(char *id) d->id = strdup(id); d->hash = simple_hash(d->id); - d->classes_index.root = NULL; - d->classes_index.compar = tc_class_compare; - - int lock; -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - lock = pthread_mutex_init(&d->classes_index.mutex, NULL); -#else - lock = pthread_rwlock_init(&d->classes_index.rwlock, NULL); -#endif -#endif - if(lock != 0) - fatal("Failed to initialize plugin_tc mutex/rwlock, return code %d.", lock); - + avl_init(&d->classes_index, tc_class_compare); tc_device_index_add(d); if(!tc_device_root) { @@ -376,7 +356,7 @@ static struct tc_device *tc_device_create(char *id) return(d); } -static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid) +static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid) { struct tc_class *c = tc_class_index_find(n, id, 0); @@ -418,7 +398,7 @@ static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parent return(c); } -static void tc_device_free(struct tc_device *n) +static inline void tc_device_free(struct tc_device *n) { if(n->next) n->next->prev = n->prev; if(n->prev) n->prev->next = n->next; @@ -438,7 +418,7 @@ static void tc_device_free(struct tc_device *n) free(n); } -static void tc_device_free_all() +static inline void tc_device_free_all() { while(tc_device_root) tc_device_free(tc_device_root); @@ -459,7 +439,7 @@ static inline int tc_space(char c) { } } -static void tc_split_words(char *str, char **words, int max_words) { +static inline void tc_split_words(char *str, char **words, int max_words) { char *s = str; int i = 0; diff --git a/src/rrd.c b/src/rrd.c index 909533dc..379b1c8b 100644 --- a/src/rrd.c +++ b/src/rrd.c @@ -50,20 +50,13 @@ static int rrdset_compare(void* a, void* b) { else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id); } -avl_tree rrdset_root_index = { - NULL, - rrdset_compare, -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -#endif +avl_tree_lock rrdset_root_index = { + { NULL, rrdset_compare }, + AVL_LOCK_INITIALIZER }; -#define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st)) -#define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st)) +#define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st)) +#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; @@ -71,7 +64,7 @@ static RRDSET *rrdset_index_find(const char *id, uint32_t hash) { tmp.id[RRD_ID_LENGTH_MAX] = '\0'; tmp.hash = (hash)?hash:simple_hash(tmp.id); - avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result); + avl_search_lock(&(rrdset_root_index), (avl *) &tmp, rrdset_iterator, (avl **) &result); return result; } @@ -93,24 +86,17 @@ static int rrdset_compare_name(void* a, void* b) { else return strcmp(A->name, B->name); } -avl_tree rrdset_root_index_name = { - NULL, - rrdset_compare_name, -#ifndef AVL_WITHOUT_PTHREADS -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -#endif +avl_tree_lock rrdset_root_index_name = { + { NULL, rrdset_compare_name }, + AVL_LOCK_INITIALIZER }; int rrdset_index_add_name(RRDSET *st) { // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name); - return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname)); + return avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname)); } -#define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname)) +#define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname)) static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) { void *result = NULL; @@ -119,7 +105,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(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result); + avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)), rrdset_iterator_name, (avl **) &result); if(result) { RRDSET *st = rrdset_from_avlname(result); if(strcmp(st->magic, RRDSET_MAGIC)) @@ -144,8 +130,8 @@ static int rrddim_compare(void* a, void* b) { else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id); } -#define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd)) -#define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd)) +#define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd)) +#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; @@ -153,7 +139,7 @@ static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) { tmp.id[RRD_ID_LENGTH_MAX] = '\0'; tmp.hash = (hash)?hash:simple_hash(tmp.id); - avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result); + avl_search_lock(&(st->dimensions_index), (avl *) &tmp, rrddim_iterator, (avl **) &result); return result; } @@ -457,7 +443,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const st->gap_when_lost_iterations_above = (int) ( config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2); - avl_init(&st->dimensions_index, rrddim_compare); + avl_init_lock(&st->dimensions_index, rrddim_compare); pthread_rwlock_init(&st->rwlock, NULL); pthread_rwlock_wrlock(&rrdset_root_rwlock); diff --git a/src/rrd.h b/src/rrd.h index 4211b7d5..60eeeb04 100644 --- a/src/rrd.h +++ b/src/rrd.h @@ -246,7 +246,7 @@ struct rrdset { // ------------------------------------------------------------------------ // the dimensions - avl_tree dimensions_index; // the root of the dimensions index + avl_tree_lock dimensions_index; // the root of the dimensions index RRDDIM *dimensions; // the actual data for every dimension }; typedef struct rrdset RRDSET;