summaryrefslogtreecommitdiffstats
path: root/libnetdata
diff options
context:
space:
mode:
authorCosta Tsaousis <costa@netdata.cloud>2022-06-28 17:48:11 +0300
committerGitHub <noreply@github.com>2022-06-28 17:48:11 +0300
commitaa3be2f0647ace385e5ffb475a3e145f38458e6e (patch)
tree23ad01136e8ccf855483a91c4881dd0ca5a85830 /libnetdata
parentc3dfbe52a61dd0d1995bc420b0e0576cf058fd74 (diff)
Dictionaries with reference counters and full deletion support during traversal (#13195)
* dont use atomic operations when not needed; detect misuse of the the unsafe functions * use relaxed atomic operations for statistics * use relaxed atomic operations for statistics * dictionaries now use reference counters, allowing deletetions of any item while traversing it * added acquire/release interface to dictionaries * added unittest for reference counters * added NETDATA_INTERNAL_CHECKS logs to detect non-exclusive access to crusial parts of the dictionaries * dictionaries cannot be deleted while there are referenced items in them - they will be deleted once the last item gets unreferenced * cleanup * properly cleanup released items * maintain counters for readers and writers; defer all deletes on sorted walkthrough; cleaner internal_error(); * somewhat faster reference counters on single threaded dictionaries * minor optimizations; allow compiling without internal checks
Diffstat (limited to 'libnetdata')
-rw-r--r--libnetdata/dictionary/README.md74
-rw-r--r--libnetdata/dictionary/dictionary.c906
-rw-r--r--libnetdata/dictionary/dictionary.h21
-rw-r--r--libnetdata/log/log.c25
-rw-r--r--libnetdata/log/log.h6
5 files changed, 806 insertions, 226 deletions
diff --git a/libnetdata/dictionary/README.md b/libnetdata/dictionary/README.md
index 28d0cfbbd9..e5bf3ba6ee 100644
--- a/libnetdata/dictionary/README.md
+++ b/libnetdata/dictionary/README.md
@@ -22,7 +22,7 @@ Dictionaries are **ordered**, meaning that the order they have been added is pre
Dictionaries guarantee **uniqueness** of all items added to them, meaning that only one item with a given name can exist in the dictionary at any given time.
-Dictionaries are extremely fast in all operations. They are indexing the keys with `JudyHS` and they utilize a double-linked-list for the traversal operations. Deletion is the most expensive operation, usually somewhat slower than insertion.
+Dictionaries are extremely fast in all operations. They are indexing the keys with `JudyHS` (or `AVL` when `libJudy` is not available) and they utilize a double-linked-list for the traversal operations. Deletion is the most expensive operation, usually somewhat slower than insertion.
## Memory management
@@ -31,13 +31,13 @@ Dictionaries come with 2 memory management options:
- **Clone** (copy) the name and/or the value to memory allocated by the dictionary.
- **Link** the name and/or the value, without allocating any memory about them.
-In **clone** mode, the dictionary guarantees that all operations on the dictionary items will automatically take care of the memory used by the name and/or the value. In case the value is an object needs to have user allocated memory, the following callback functions can be registered:
+In **clone** mode, the dictionary guarantees that all operations on the dictionary items will automatically take care of the memory used by the name and/or the value. In case the value is an object that needs to have user allocated memory, the following callback functions can be registered:
1.`dictionary_register_insert_callback()` that will be called just after the insertion of an item to the dictionary, or after the replacement of the value of a dictionary item (but while the dictionary is write-locked - if locking is enabled).
2. `dictionary_register_delete_callback()` that will be called just prior to the deletion of an item from the dictionary, or prior to the replacement of the value of a dictionary item (but while the dictionary is write-locked - if locking is enabled).
3. `dictionary_register_conflict_callback()` that will be called when `DICTIONARY_FLAG_DONT_OVERWRITE_VALUE` is set and another value is attempted to be inserted for the same key.
-In **link** mode, the name and/or the value are just linked to the dictionary item, and it is the user's responsibility to free the memory used after an item is deleted from the dictionary.
+In **link** mode, the name and/or the value are just linked to the dictionary item, and it is the user's responsibility to free the memory they use after an item is deleted from the dictionary or when the dictionary is destroyed.
By default, **clone** mode is used for both the name and the value.
@@ -63,7 +63,7 @@ The dictionary supports the following operations supported by the hash table:
Use `dictionary_create()` to create a dictionary.
-Use `dictionary_destroy()` to destroy a dictionary. When destroyed, a dictionary frees all the memory it has allocated on its own. The exception is the registration of a deletion callback function that can be called on deletion of an item, which may free additional resources.
+Use `dictionary_destroy()` to destroy a dictionary. When destroyed, a dictionary frees all the memory it has allocated on its own. This can be complemented by the registration of a deletion callback function that can be called upon deletion of each item in the dictionary, which may free additional resources.
### dictionary_set()
@@ -72,7 +72,7 @@ This call is used to:
- **add** an item to the dictionary.
- **reset** the value of an existing item in the dictionary.
-If **resetting** is not desired, add `DICTIONARY_FLAG_DONT_OVERWRITE_VALUE` to the flags when creating the dictionary. In this case, `dictionary_set()` will return the value of the original item found in the dictionary instead of resetting it and the value passed to the call will be ignored.
+If **resetting** is not desired, add `DICTIONARY_FLAG_DONT_OVERWRITE_VALUE` to the flags when creating the dictionary. In this case, `dictionary_set()` will return the value of the original item found in the dictionary instead of resetting it and the value passed to the call will be ignored. Optionally a conflict callback function can be registered, to manipulate (probably merge or extend) the original value, based on the new value attempted to be added to the dictionary.
For **multi-threaded** operation, the `dictionary_set()` calls get an exclusive write lock on the dictionary.
@@ -89,14 +89,16 @@ Where:
* `value` is a pointer to the value associated with this item. In **clone** mode, if `value` is `NULL`, a new memory allocation will be made of `value_len` size and will be initialized to zero.
* `value_len` is the size of the `value` data. If `value_len` is zero, no allocation will be done and the dictionary item will permanently have the `NULL` value.
-> **IMPORTANT**<br/>There is also an **unsafe** version (without locks) of this call. This is to be used when traversing the dictionary. It should never be called without an active lock on the dictionary, which can only be acquired while traversing.
+> **IMPORTANT**<br/>There is also an **unsafe** version (without locks) of this call. This is to be used when traversing the dictionary in write mode. It should never be called without an active lock on the dictionary, which can only be acquired while traversing.
### dictionary_get()
-This call is used to get the value of an item, given its name. It utilizes the JudyHS hash table for making the lookup.
+This call is used to get the value of an item, given its name. It utilizes the `JudyHS` hash table for making the lookup.
For **multi-threaded** operation, the `dictionary_get()` call gets a shared read lock on the dictionary.
+In clone mode, the value returned is not guaranteed to be valid, as any other thread may delete the item from the dictionary at any time. To ensure the value will be available, use `dictionary_acquire_item()`, which uses a reference counter to defer deletes until the item is released.
+
The format is:
```c
@@ -114,7 +116,7 @@ Where:
This call is used to delete an item from the dictionary, given its name.
-If there is a delete callback registered to the dictionary (`dictionary_register_delete_callback()`), it is called prior to the actual deletion of the item.
+If there is a deletion callback registered to the dictionary (`dictionary_register_delete_callback()`), it is called prior to the actual deletion of the item.
For **multi-threaded** operation, the `dictionary_del()` calls get an exclusive write lock on the dictionary.
@@ -131,29 +133,65 @@ Where:
> **IMPORTANT**<br/>There is also an **unsafe** version (without locks) of this call. This is to be used when traversing the dictionary, to delete the current item. It should never be called without an active lock on the dictionary, which can only be acquired while traversing.
+### dictionary_acquire_item()
+
+This call can be used the search and get a dictionary item, while ensuring that it will be available for use, until `dictionary_acquired_item_release()` is called.
+
+This call **does not return the value** of the dictionary item. It returns an internal pointer to a structure that maintains the reference counter used to protect the actual value. To get the value of the item (the same value as returned by `dictionary_get()`), the function `dictionary_acquired_item_value()` has to be called.
+
+Example:
+
+```c
+// create the dictionary
+DICTIONARY *dict = dictionary_create(DICTIONARY_FLAGS_NONE);
+
+// add an item to it
+dictionary_set(dict, "name", "value", 6);
+
+// find the item we added and acquire it
+void *item = dictionary_acquire_item(dict, "name");
+
+// extract its value
+char *value = (char *)dictionary_acquired_item_value(dict, item);
+
+// now value points to the string "value"
+printf("I got value = '%s'\n", value);
+
+// release the item, so that it can deleted
+dictionary_acquired_item_release(dict, item);
+
+// destroy the dictionary
+dictionary_destroy(dict);
+```
+
+When items are acquired, a reference counter is maintained to keep track of how many users exist for it. If an item with a non-zero number of users is deleted, it is removed from the index, it can be added again to the index (without conflict), and although it exists in the linked-list, it is not offered during traversal. Garbage collection to actually delete the item happens every time a write-locked dictionary is unlocked (just before the unlock) and items are deleted only if no users are using them.
+
+If any item is still acquired when the dictionary is destroyed, the destruction of the dictionary is also deferred until all the acquired items are released. When the dictionary is destroyed like that, all operations on the dictionary fail (traversals do not traverse, insertions do not insert, deletions do not delete, searches do not find any items, etc). Once the last item in the dictionary is released, the dictionary is automatically destroyed too.
+
## Traversal
-Dictionaries offer 2 ways to traverse the entire dictionary:
+Dictionaries offer 3 ways to traverse the entire dictionary:
- **walkthrough**, implemented by setting a callback function to be called for every item.
+- **sorted walkthrough**, which first sorts the dictionary and then call a callback function for every item.
- **foreach**, a way to traverse the dictionary with a for-next loop.
-Both of these methods are available in **read** or **write** mode. In **read** mode only lookups are allowed to the dictionary. In **write** both lookups but also deletion of the currently working item is also allowed.
+All these methods are available in **read** or **write** mode. In **read** mode only lookups are allowed to the dictionary. In **write** lookups but also insertions and deletions are allowed.
-While traversing the dictionary with any of these methods, all calls to the dictionary have to use the `_unsafe` versions of the function calls, otherwise deadlock may arise.
+While traversing the dictionary with any of these methods, all calls to the dictionary have to use the `_unsafe` versions of the function calls, otherwise deadlocks may arise.
> **IMPORTANT**<br/>The dictionary itself does not check to ensure that a user is actually using the right lock mode (read or write) while traversing the dictionary for each of the unsafe calls.
### walkthrough (callback)
-There are 2 calls:
+There are 4 calls:
-- `dictionary_walkthrough_read()` that acquires a shared read lock, and it calls a callback function for every item of the dictionary. The callback function may use the unsafe versions of the `dictionary_get()` calls to lookup other items in the dictionary, but it should not add or remove item from the dictionary.
-- `dictionary_walkthrough_write()` that acquires an exclusive write lock, and it calls a callback function for every item of the dictionary. This is to be used when items need to be added to the dictionary, or when the current item may need to be deleted. If the callback function deletes any other items, the behavior may be undefined (actually, the item next to the one currently working should not be deleted - a pointer to it is held by the traversal function to move on traversing the dictionary).
+- `dictionary_walkthrough_read()` and `dictionary_sorted_walkthrough_read()` that acquire a shared read lock, and they call a callback function for every item of the dictionary. The callback function may use the unsafe versions of the `dictionary_get()` calls to lookup other items in the dictionary, but it should not attempt to add or remove items to/from the dictionary.
+- `dictionary_walkthrough_write()` and `dictionary_sorted_walkthrough_write()` that acquire an exclusive write lock, and they call a callback function for every item of the dictionary. This is to be used when items need to be added to or removed from the dictionary. The `write` versions can be used to delete any or all the items from the dictionary, including the currently working one. For the `sorted` version, all items in the dictionary maintain a reference counter, so all deletions are deferred until the sorted walkthrough finishes.**
-The items are traversed in the same order they have been added to the dictionary (or the reverse order if the flag `DICTIONARY_FLAG_ADD_IN_FRONT` is set during dictionary creation).
+The non sorted versions traverse the items in the same order they have been added to the dictionary (or the reverse order if the flag `DICTIONARY_FLAG_ADD_IN_FRONT` is set during dictionary creation). The sorted versions sort alphabetically the items based on their name, and then they traverse them in the sorted order.
-The callback function returns an `int`. If this value is negative, traversal of the dictionary is stopped immediately and the negative value is returned to the caller. If the returned value of all callbacks is zero or positive, the walkthrough functions return the sum of the return values of all callbacks. So, if you are just interested to know how many items fall into some condition, write a callback function that returns 1 when the item satisfies that condition and 0 when it does not and the walkthrough function will return how many tested positive.
+The callback function returns an `int`. If this value is negative, traversal of the dictionary is stopped immediately and the negative value is returned to the caller. If the returned value of all callback calls is zero or positive, the walkthrough functions return the sum of the return values of all callbacks. So, if you are just interested to know how many items fall into some condition, write a callback function that returns 1 when the item satisfies that condition and 0 when it does not and the walkthrough function will return how many tested positive.
### foreach (for-next loop)
@@ -186,14 +224,14 @@ else
something else;
```
-In the above, the `if(x)` condition will work as expected. It will do the foreach loop when x is 1, otherwise it will run `something else`.
+In the above, the `if(x == 1)` condition will work as expected. It will do the foreach loop when x is 1, otherwise it will run `something else`.
There are 2 versions of `dfe_start`:
- `dfe_start_read()` that acquires a shared read lock to the dictionary.
- `dfe_start_write()` that acquires an exclusive write lock to the dictionary.
-While in the loop, depending on the read or write versions of `dfe_start`, the caller may lookup or manipulate the dictionary. The rules are the same with the walkthrough callback functions.
+While in the loop, depending on the read or write versions of `dfe_start`, the caller may lookup or manipulate the dictionary using the unsafe functions. The rules are the same with the unsorted walkthrough callback functions.
PS: DFE is Dictionary For Each.
diff --git a/libnetdata/dictionary/dictionary.c b/libnetdata/dictionary/dictionary.c
index d999aab054..c15fb60184 100644
--- a/libnetdata/dictionary/dictionary.c
+++ b/libnetdata/dictionary/dictionary.c
@@ -1,7 +1,12 @@
// SPDX-License-Identifier: GPL-3.0-or-later
-// NOT TO BE USED BY USERS YET
-#define DICTIONARY_FLAG_REFERENCE_COUNTERS (1 << 5) // maintain reference counter in walkthrough and foreach
+// NOT TO BE USED BY USERS
+#define DICTIONARY_FLAG_EXCLUSIVE_ACCESS (1 << 29) // there is only one thread accessing the dictionary
+#define DICTIONARY_FLAG_DESTROYED (1 << 30) // this dictionary has been destroyed
+#define DICTIONARY_FLAG_DEFER_ALL_DELETIONS (1 << 31) // defer all deletions of items in the dictionary
+
+// our reserved flags that cannot be set by users
+#define DICTIONARY_FLAGS_RESERVED (DICTIONARY_FLAG_EXCLUSIVE_ACCESS|DICTIONARY_FLAG_DESTROYED|DICTIONARY_FLAG_DEFER_ALL_DELETIONS)
typedef struct dictionary DICTIONARY;
#define DICTIONARY_INTERNALS
@@ -19,56 +24,15 @@ typedef struct dictionary DICTIONARY;
#include <Judy.h>
#endif
-/*
- * This version uses JudyHS arrays to index the dictionary
- *
- * The following output is from the unit test, at the end of this file:
- *
- * This is the JudyHS version:
- *
- * 1000000 x dictionary_set() (dictionary size 0 entries, 0 KB)...
- * 1000000 x dictionary_get(existing) (dictionary size 1000000 entries, 74001 KB)...
- * 1000000 x dictionary_get(non-existing) (dictionary size 1000000 entries, 74001 KB)...
- * Walking through the dictionary (dictionary size 1000000 entries, 74001 KB)...
- * 1000000 x dictionary_del(existing) (dictionary size 1000000 entries, 74001 KB)...
- * 1000000 x dictionary_set() (dictionary size 0 entries, 0 KB)...
- * Destroying dictionary (dictionary size 1000000 entries, 74001 KB)...
- *
- * TIMINGS:
- * adding 316027 usec, positive search 156740 usec, negative search 84524, walk through 15036 usec, deleting 361444, destroy 107394 usec
- *
- * This is from the JudySL version:
- *
- * Creating dictionary of 1000000 entries...
- * Checking index of 1000000 entries...
- * Walking 1000000 entries and checking name-value pairs...
- * Created and checked 1000000 entries, found 0 errors - used 58376 KB of memory
- * Destroying dictionary of 1000000 entries...
- * Deleted 1000000 entries
- * create 338975 usec, check 156080 usec, walk 80764 usec, destroy 444569 usec
- *
- * This is the AVL version:
- *
- * Creating dictionary of 1000000 entries...
- * Checking index of 1000000 entries...
- * Walking 1000000 entries and checking name-value pairs...
- * Created and checked 1000000 entries, found 0 errors - used 89626 KB of memory
- * Destroying dictionary of 1000000 entries...
- * create 413892 usec, check 220006 usec, walk 34247 usec, destroy 98062 usec
- *
- * So, the JudySL is a lot slower to WALK and DESTROY (DESTROY does a WALK)
- * It is slower, because for every item, JudySL copies the KEY/NAME to a
- * caller supplied buffer (Index). So, by just walking over 1 million items,
- * JudySL does 1 million strcpy() !!!
- *
- * It also seems that somehow JudySLDel() is unbelievably slow too!
- *
- */
-
+typedef enum name_value_flags {
+ NAME_VALUE_FLAG_NONE = 0,
+ NAME_VALUE_FLAG_DELETED = (1 << 0), // this item is deleted
+} NAME_VALUE_FLAGS;
/*
* Every item in the dictionary has the following structure.
*/
+
typedef struct name_value {
#ifdef DICTIONARY_WITH_AVL
avl_t avl_node;
@@ -83,26 +47,10 @@ typedef struct name_value {
void *value; // the value of the dictionary item
char *name; // the name of the dictionary item
+ int refcount; // the reference counter
+ NAME_VALUE_FLAGS flags; // the flags for this item
} NAME_VALUE;
-/*
- * When DICTIONARY_FLAG_REFERENCE_COUNTERS is set, we need to keep track of all the memory
- * we allocate and free. So, we need to keep track of the sizes of all names and values.
- * We do this by overloading NAME_VALUE with the following additional fields.
- */
-
-typedef enum name_value_flags {
- NAME_VALUE_FLAG_NONE = 0,
- NAME_VALUE_FLAG_DELETED = (1 << 0), // this item is deleted
-} NAME_VALUE_FLAGS;
-
-typedef struct name_value_with_reference_counters {
- NAME_VALUE name_value_data_here; // never used - just to put the lengths at the right position
-
- size_t refcount; // the reference counter
- NAME_VALUE_FLAGS flags; // the flags for this item
-} NAME_VALUE_WITH_REFERENCE_COUNTERS;
-
struct dictionary {
DICTIONARY_FLAGS flags; // the flags of the dictionary
@@ -129,15 +77,25 @@ struct dictionary {
void (*conflict_callback)(const char *name, void *old_value, void *new_value, void *data);
void *conflict_callback_data;
- size_t inserts;
- size_t deletes;
- size_t searches;
- size_t resets;
- size_t entries;
- size_t walkthroughs;
- size_t memory;
+ size_t inserts; // how many index insertions have been performed
+ size_t deletes; // how many index deletions have been performed
+ size_t searches; // how many index searches have been performed
+ size_t resets; // how many times items have reset their values
+ size_t walkthroughs; // how many walkthroughs have been done
+ long int memory; // how much memory the dictionary has currently allocated
+ long int entries; // how many items are currently in the index (the linked list may have more)
+ long int referenced_items; // how many items of the dictionary are currently being used by 3rd parties
+ long int pending_deletion_items; // how many items of the dictionary have been deleted, but have not been removed yet
+ int readers; // how many readers are currently using the dictionary
+ int writers; // how many writers are currently using the dictionary
};
+static inline void linkedlist_namevalue_unlink_unsafe(DICTIONARY *dict, NAME_VALUE *nv);
+static size_t namevalue_destroy_unsafe(DICTIONARY *dict, NAME_VALUE *nv);
+
+// ----------------------------------------------------------------------------
+// callbacks registration
+
void dictionary_register_insert_callback(DICTIONARY *dict, void (*ins_callback)(const char *name, void *value, void *data), void *data) {
dict->ins_callback = ins_callback;
dict->ins_callback_data = data;
@@ -156,10 +114,10 @@ void dictionary_register_conflict_callback(DICTIONARY *dict, void (*conflict_cal
// ----------------------------------------------------------------------------
// dictionary statistics maintenance
-size_t dictionary_stats_allocated_memory(DICTIONARY *dict) {
+long int dictionary_stats_allocated_memory(DICTIONARY *dict) {
return dict->memory;
}
-size_t dictionary_stats_entries(DICTIONARY *dict) {
+long int dictionary_stats_entries(DICTIONARY *dict) {
return dict->entries;
}
size_t dictionary_stats_searches(DICTIONARY *dict) {
@@ -179,26 +137,114 @@ size_t dictionary_stats_walkthroughs(DICTIONARY *dict) {
}
static inline void DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
- __atomic_fetch_add(&dict->searches, 1, __ATOMIC_SEQ_CST);
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->searches++;
+ }
+ else {
+ __atomic_fetch_add(&dict->searches, 1, __ATOMIC_RELAXED);
+ }
}
static inline void DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict, size_t size) {
- __atomic_fetch_add(&dict->inserts, 1, __ATOMIC_SEQ_CST);
- __atomic_fetch_add(&dict->entries, 1, __ATOMIC_SEQ_CST);
- __atomic_fetch_add(&dict->memory, size, __ATOMIC_SEQ_CST);
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->inserts++;
+ dict->entries++;
+ dict->memory += (long)size;
+ }
+ else {
+ __atomic_fetch_add(&dict->inserts, 1, __ATOMIC_RELAXED);
+ __atomic_fetch_add(&dict->entries, 1, __ATOMIC_RELAXED);
+ __atomic_fetch_add(&dict->memory, (long)size, __ATOMIC_RELAXED);
+ }
+}
+static inline void DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict) {
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->deletes++;
+ dict->entries--;
+ }
+ else {
+ __atomic_fetch_add(&dict->deletes, 1, __ATOMIC_RELAXED);
+ __atomic_fetch_sub(&dict->entries, 1, __ATOMIC_RELAXED);
+ }
}
-static inline void DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict, size_t size) {
- __atomic_fetch_add(&dict->deletes, 1, __ATOMIC_SEQ_CST);
- __atomic_fetch_sub(&dict->entries, 1, __ATOMIC_SEQ_CST);
- __atomic_fetch_sub(&dict->memory, size, __ATOMIC_SEQ_CST);
+static inline void DICTIONARY_STATS_ENTRIES_MINUS_MEMORY(DICTIONARY *dict, size_t size) {
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->memory -= (long)size;
+ }
+ else {
+ __atomic_fetch_sub(&dict->memory, (long)size, __ATOMIC_RELAXED);
+ }
}
static inline void DICTIONARY_STATS_VALUE_RESETS_PLUS1(DICTIONARY *dict, size_t oldsize, size_t newsize) {
- __atomic_fetch_add(&dict->resets, 1, __ATOMIC_SEQ_CST);
- __atomic_fetch_add(&dict->memory, newsize, __ATOMIC_SEQ_CST);
- __atomic_fetch_sub(&dict->memory, oldsize, __ATOMIC_SEQ_CST);
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->resets++;
+ dict->memory += (long)newsize;
+ dict->memory -= (long)oldsize;
+ }
+ else {
+ __atomic_fetch_add(&dict->resets, 1, __ATOMIC_RELAXED);
+ __atomic_fetch_add(&dict->memory, (long)newsize, __ATOMIC_RELAXED);
+ __atomic_fetch_sub(&dict->memory, (long)oldsize, __ATOMIC_RELAXED);
+ }
}
static inline void DICTIONARY_STATS_WALKTHROUGHS_PLUS1(DICTIONARY *dict) {
- __atomic_fetch_add(&dict->walkthroughs, 1, __ATOMIC_SEQ_CST);
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ dict->walkthroughs++;
+ }
+ else {
+ __atomic_fetch_add(&dict->walkthroughs, 1, __ATOMIC_RELAXED);
+ }
+}
+
+static inline size_t DICTIONARY_STATS_REFERENCED_ITEMS_PLUS1(DICTIONARY *dict) {
+ return __atomic_add_fetch(&dict->referenced_items, 1, __ATOMIC_SEQ_CST);
+}
+
+static inline size_t DICTIONARY_STATS_REFERENCED_ITEMS_MINUS1(DICTIONARY *dict) {
+ return __atomic_sub_fetch(&dict->referenced_items, 1, __ATOMIC_SEQ_CST);
+}
+
+static inline size_t DICTIONARY_STATS_PENDING_DELETES_PLUS1(DICTIONARY *dict) {
+ return __atomic_add_fetch(&dict->pending_deletion_items, 1, __ATOMIC_SEQ_CST);
+}
+
+static inline size_t DICTIONARY_STATS_PENDING_DELETES_MINUS1(DICTIONARY *dict) {
+ return __atomic_sub_fetch(&dict->pending_deletion_items, 1, __ATOMIC_SEQ_CST);
+}
+
+static inline size_t DICTIONARY_STATS_PENDING_DELETES_GET(DICTIONARY *dict) {
+ return __atomic_load_n(&dict->pending_deletion_items, __ATOMIC_SEQ_CST);
+}
+
+static inline int DICTIONARY_NAME_VALUE_REFCOUNT_GET(NAME_VALUE *nv) {
+ return __atomic_load_n(&nv->refcount, __ATOMIC_SEQ_CST);
+}
+
+// ----------------------------------------------------------------------------
+// garbage collector
+// it is called every time someone gets a write lock to the dictionary
+
+static void garbage_collect_pending_deletes_unsafe(DICTIONARY *dict) {
+ if(!(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS)) return;
+
+ if(likely(!DICTIONARY_STATS_PENDING_DELETES_GET(dict))) return;
+
+ NAME_VALUE *nv = dict->first_item;
+ while(nv) {
+ if(nv->flags & NAME_VALUE_FLAG_DELETED && DICTIONARY_NAME_VALUE_REFCOUNT_GET(nv) == 0) {
+ NAME_VALUE *nv_next = nv->next;
+
+ linkedlist_namevalue_unlink_unsafe(dict, nv);
+ namevalue_destroy_unsafe(dict, nv);
+
+ size_t pending = DICTIONARY_STATS_PENDING_DELETES_MINUS1(dict);
+ if(!pending) break;
+
+ nv = nv_next;
+ }
+ else
+ nv = nv->next;
+ }
}
// ----------------------------------------------------------------------------
@@ -208,8 +254,15 @@ static inline size_t dictionary_lock_init(DICTIONARY *dict) {
if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
dict->rwlock = mallocz(sizeof(netdata_rwlock_t));
netdata_rwlock_init(dict->rwlock);
+
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS)
+ dict->flags &= ~DICTIONARY_FLAG_EXCLUSIVE_ACCESS;
+
return sizeof(netdata_rwlock_t);
}
+
+ // we are single threaded
+ dict->flags |= DICTIONARY_FLAG_EXCLUSIVE_ACCESS;
dict->rwlock = NULL;
return 0;
}
@@ -223,24 +276,79 @@ static inline size_t dictionary_lock_free(DICTIONARY *dict) {
return 0;
}
-static inline void dictionary_lock_rlock(DICTIONARY *dict) {
- if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
- // debug(D_DICTIONARY, "Dictionary READ lock");
+static void dictionary_lock(DICTIONARY *dict, char rw) {
+ if(rw == 'r' || rw == 'R') {
+ // read lock
+ __atomic_add_fetch(&dict->readers, 1, __ATOMIC_RELAXED);
+ }
+ else {
+ // write lock
+ __atomic_add_fetch(&dict->writers, 1, __ATOMIC_RELAXED);
+ }
+
+ if(likely(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))
+ return;
+
+ if(rw == 'r' || rw == 'R') {
+ // read lock
netdata_rwlock_rdlock(dict->rwlock);
+
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS) {
+ internal_error(true, "DICTIONARY: left-over exclusive access to dictionary found");
+ dict->flags &= ~DICTIONARY_FLAG_EXCLUSIVE_ACCESS;
+ }
+ }
+ else {
+ // write lock
+ netdata_rwlock_wrlock(dict->rwlock);
+
+ dict->flags |= DICTIONARY_FLAG_EXCLUSIVE_ACCESS;
}
}
-static inline void dictionary_lock_wrlock(DICTIONARY *dict) {
- if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
- // debug(D_DICTIONARY, "Dictionary WRITE lock");
- netdata_rwlock_wrlock(dict->rwlock);
+static void dictionary_unlock(DICTIONARY *dict, char rw) {
+ if(rw == 'r' || rw == 'R') {
+ // read unlock
+ __atomic_sub_fetch(&dict->readers, 1, __ATOMIC_RELAXED);
+ }
+ else {
+ // write unlock
+ garbage_collect_pending_deletes_unsafe(dict);
+ __atomic_sub_fetch(&dict->writers, 1, __ATOMIC_RELAXED);
+ }
+
+ if(likely(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))
+ return;
+
+ if(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS)
+ dict->flags &= ~DICTIONARY_FLAG_EXCLUSIVE_ACCESS;
+
+ netdata_rwlock_unlock(dict->rwlock);
+}
+
+// ----------------------------------------------------------------------------
+// deferred deletions
+
+void dictionary_defer_all_deletions_unsafe(DICTIONARY *dict, char rw) {
+ if(rw == 'r' || rw == 'R') {
+ // read locked - no need to defer deletions
+ ;
+ }
+ else {
+ // write locked - defer deletions
+ dict->flags |= DICTIONARY_FLAG_DEFER_ALL_DELETIONS;
}
}
-static inline void dictionary_unlock(DICTIONARY *dict) {
- if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
- // debug(D_DICTIONARY, "Dictionary UNLOCK lock");
- netdata_rwlock_unlock(dict->rwlock);
+void dictionary_restore_all_deletions_unsafe(DICTIONARY *dict, char rw) {
+ if(rw == 'r' || rw == 'R') {
+ // read locked - no need to defer deletions
+ internal_error(dict->flags & DICTIONARY_FLAG_DEFER_ALL_DELETIONS, "DICTIONARY: deletions are deferred on a read lock");
+ }
+ else {
+ // write locked - defer deletions
+ if(dict->flags & DICTIONARY_FLAG_DEFER_ALL_DELETIONS)
+ dict->flags &= ~DICTIONARY_FLAG_DEFER_ALL_DELETIONS;
}
}
@@ -263,27 +371,56 @@ static inline size_t reference_counter_free(DICTIONARY *dict) {
return 0;
}
-static void reference_counter_acquire(DICTIONARY *dict, NAME_VALUE *nv) {
- if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
- NAME_VALUE_WITH_REFERENCE_COUNTERS *nvs = (NAME_VALUE_WITH_REFERENCE_COUNTERS *)nv;
- __atomic_fetch_add(&nvs->refcount, 1, __ATOMIC_SEQ_CST);
+static int reference_counter_acquire(DICTIONARY *dict, NAME_VALUE *nv) {
+ int refcount;
+ if(likely(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))
+ refcount = ++nv->refcount;
+ else
+ refcount = __atomic_add_fetch(&nv->refcount, 1, __ATOMIC_SEQ_CST);
+
+ if(refcount == 1) {
+ // referenced items counts number of unique items referenced
+ // so, we increase it only when refcount == 1
+ DICTIONARY_STATS_REFERENCED_ITEMS_PLUS1(dict);
+
+ // if this is a deleted item, but the counter increased to 1
+ // we need to remove it from the pending items to delete
+ if (nv->flags & NAME_VALUE_FLAG_DELETED)
+ DICTIONARY_STATS_PENDING_DELETES_MINUS1(dict);
}
+
+ return refcount;
}
-static void reference_counter_release(DICTIONARY *dict, NAME_VALUE *nv) {
- if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
- NAME_VALUE_WITH_REFERENCE_COUNTERS *nvs = (NAME_VALUE_WITH_REFERENCE_COUNTERS *)nv;
- __atomic_fetch_sub(&nvs->refcount, 1, __ATOMIC_SEQ_CST);
+static int reference_counter_release(DICTIONARY *dict, NAME_VALUE *nv, bool can_get_write_lock) {
+ // this function may be called without any lock on the dictionary
+ // or even when someone else has a write lock on the dictionary
+ // so, we cannot check for EXCLUSIVE ACCESS
+
+ int refcount;
+ if(likely(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))
+ refcount = --nv->refcount;
+ else
+ refcount = __atomic_sub_fetch(&nv->refcount, 1, __ATOMIC_SEQ_CST);
+
+ if(refcount == 0) {
+ if((nv->flags & NAME_VALUE_FLAG_DELETED))
+ DICTIONARY_STATS_PENDING_DELETES_PLUS1(dict);
+
+ // referenced items counts number of unique items referenced
+ // so, we decrease it only when refcount == 0
+ DICTIONARY_STATS_REFERENCED_ITEMS_MINUS1(dict);
}
-}
-static int reference_counter_mark_deleted(DICTIONARY *dict, NAME_VALUE *nv) {
- if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
- NAME_VALUE_WITH_REFERENCE_COUNTERS *nvs = (NAME_VALUE_WITH_REFERENCE_COUNTERS *)nv;
- nvs->flags |= NAME_VALUE_FLAG_DELETED;
- return 1;
+ if(can_get_write_lock && DICTIONARY_STATS_PENDING_DELETES_GET(dict)) {
+ // we can garbage collect now
+
+ dictionary_lock(dict, 'w');
+ garbage_collect_pending_deletes_unsafe(dict);
+ dictionary_unlock(dict, 'w');
}
- return 0;
+
+ return refcount;
}
// ----------------------------------------------------------------------------
@@ -366,6 +503,8 @@ static size_t hashtable_destroy_unsafe(DICTIONARY *dict) {
}
static inline NAME_VALUE **hashtable_insert_unsafe(DICTIONARY *dict, const char *name, size_t name_len) {
+ internal_error(!(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS), "DICTIONARY: inserting to the index without exclusive access to the dictionary.");
+
JError_t J_Error;
Pvoid_t *Rc = JudyHSIns(&dict->JudyHSArray, (void *)name, name_len, &J_Error);
if (unlikely(Rc == PJERR)) {
@@ -384,8 +523,9 @@ static inline NAME_VALUE **hashtable_insert_unsafe(DICTIONARY *dict, const char
}
static inline int hashtable_delete_unsafe(DICTIONARY *dict, const char *name, size_t name_len, NAME_VALUE *nv) {
- (void)nv;
+ internal_error(!(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS), "DICTIONARY: deleting from the index without exclusive access to the dictionary.");
+ (void)nv;
if(unlikely(!dict->JudyHSArray)) return 0;
JError_t J_Error;
@@ -440,6 +580,8 @@ static inline void hashtable_inserted_name_value_unsafe(DICTIONARY *dict, const
// linked list management
static inline void linkedlist_namevalue_link_unsafe(DICTIONARY *dict, NAME_VALUE *nv) {
+ internal_error(!(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS), "DICTIONARY: adding item to the linked-list without exclusive access to the dictionary.");
+
if (unlikely(!dict->first_item)) {
// we are the only ones here
nv->next = NULL;
@@ -467,6 +609,8 @@ static inline void linkedlist_namevalue_link_unsafe(DICTIONARY *dict, NAME_VALUE
}
static inline void linkedlist_namevalue_unlink_unsafe(DICTIONARY *dict, NAME_VALUE *nv) {
+ internal_error(!(dict->flags & DICTIONARY_FLAG_EXCLUSIVE_ACCESS), "DICTIONARY: removing item from the linked-list without exclusive access to the dictionary.");
+
if(nv->next) nv->next->prev = nv->prev;
if(nv->prev) nv->prev->next = nv->next;
if(dict->first_item == nv) dict->first_item = nv->next;
@@ -476,17 +620,15 @@ static inline void linkedlist_namevalue_unlink_unsafe(DICTIONARY *dict, NAME_VAL
// ----------------------------------------------------------------------------
// NAME_VALUE methods
-static inline size_t namevalue_alloc_size(DICTIONARY *dict) {
- return (dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS) ? sizeof(NAME_VALUE_WITH_REFERENCE_COUNTERS) : sizeof(NAME_VALUE);
-}
-
static NAME_VALUE *namevalue_create_unsafe(DICTIONARY *dict, const char *name, size_t name_len, void *value, size_t value_len) {
debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
- size_t size = namevalue_alloc_size(dict);
+ size_t size = sizeof(NAME_VALUE);
NAME_VALUE *nv = mallocz(size);<