dictionary.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_DICTIONARY_H
  3. #define NETDATA_DICTIONARY_H 1
  4. #include "../libnetdata.h"
  5. /*
  6. * Netdata DICTIONARY features:
  7. *
  8. * CLONE or LINK
  9. * Names and Values in the dictionary can be cloned or linked.
  10. * In clone mode, the dictionary does all the memory management.
  11. * The default is clone for both names and values.
  12. * Set DICT_OPTION_NAME_LINK_DONT_CLONE to link names.
  13. * Set DICT_OPTION_VALUE_LINK_DONT_CLONE to link names.
  14. *
  15. * ORDERED
  16. * Items are ordered in the order they are added (new items are appended at the end).
  17. * You may reverse the order by setting the flag DICT_OPTION_ADD_IN_FRONT.
  18. *
  19. * LOOKUP
  20. * The dictionary uses JudyHS to maintain a very fast randomly accessible hash table.
  21. *
  22. * MULTI-THREADED and SINGLE-THREADED
  23. * Each dictionary may be single threaded (no locks), or multi-threaded (multiple readers or one writer).
  24. * The default is multi-threaded. Add the flag DICT_OPTION_SINGLE_THREADED for single-threaded.
  25. *
  26. * WALK-THROUGH and FOREACH traversal
  27. * The dictionary can be traversed on read or write mode, either with a callback (walkthrough) or with
  28. * a loop (foreach).
  29. *
  30. * In write mode traversal, the caller may delete only the current item, but may add as many items as needed.
  31. *
  32. */
  33. #ifdef DICTIONARY_INTERNALS
  34. #define DICTFE_CONST
  35. #define DICT_ITEM_CONST
  36. #else
  37. #define DICTFE_CONST const
  38. #define DICT_ITEM_CONST const
  39. #endif
  40. typedef struct dictionary DICTIONARY;
  41. typedef struct dictionary_item DICTIONARY_ITEM;
  42. typedef enum __attribute__((packed)) dictionary_options {
  43. DICT_OPTION_NONE = 0, // the default is the opposite of all below
  44. DICT_OPTION_SINGLE_THREADED = (1 << 0), // don't use any locks (default: use locks)
  45. DICT_OPTION_VALUE_LINK_DONT_CLONE = (1 << 1), // don't copy the value, just point to the one provided (default: copy)
  46. DICT_OPTION_NAME_LINK_DONT_CLONE = (1 << 2), // don't copy the name, just point to the one provided (default: copy)
  47. DICT_OPTION_DONT_OVERWRITE_VALUE = (1 << 3), // don't overwrite values of dictionary items (default: overwrite)
  48. DICT_OPTION_ADD_IN_FRONT = (1 << 4), // add dictionary items at the front of the linked list (default: at the end)
  49. DICT_OPTION_FIXED_SIZE = (1 << 5), // the items of the dictionary have a fixed size
  50. } DICT_OPTIONS;
  51. struct dictionary_stats {
  52. const char *name; // the name of the category
  53. struct {
  54. size_t active; // the number of active dictionaries
  55. size_t deleted; // the number of dictionaries queued for destruction
  56. } dictionaries;
  57. struct {
  58. long entries; // active items in the dictionary
  59. long pending_deletion; // pending deletion items in the dictionary
  60. long referenced; // referenced items in the dictionary
  61. } items;
  62. struct {
  63. size_t creations; // dictionary creations
  64. size_t destructions; // dictionary destructions
  65. size_t flushes; // dictionary flushes
  66. size_t traversals; // dictionary foreach
  67. size_t walkthroughs; // dictionary walkthrough
  68. size_t garbage_collections; // dictionary garbage collections
  69. size_t searches; // item searches
  70. size_t inserts; // item inserts
  71. size_t resets; // item resets
  72. size_t deletes; // item deletes
  73. } ops;
  74. struct {
  75. size_t inserts; // number of times the insert callback is called
  76. size_t conflicts; // number of times the conflict callback is called
  77. size_t reacts; // number of times the react callback is called
  78. size_t deletes; // number of times the delete callback is called
  79. } callbacks;
  80. // memory
  81. struct {
  82. long index; // bytes of keys indexed (indication of the index size)
  83. long values; // bytes of caller structures
  84. long dict; // bytes of the structures dictionary needs
  85. } memory;
  86. // spin locks
  87. struct {
  88. size_t use_spins; // number of times a reference to item had to spin to acquire it or ignore it
  89. size_t search_spins; // number of times a successful search result had to be thrown away
  90. size_t insert_spins; // number of times an insertion to the hash table had to be repeated
  91. size_t delete_spins; // number of times a deletion had to spin to get a decision
  92. } spin_locks;
  93. };
  94. // Create a dictionary
  95. #ifdef NETDATA_INTERNAL_CHECKS
  96. #define dictionary_create(options) dictionary_create_advanced_with_trace(options, NULL, 0, __FUNCTION__, __LINE__, __FILE__)
  97. #define dictionary_create_advanced(options, stats, fixed_size) dictionary_create_advanced_with_trace(options, stats, fixed_size, __FUNCTION__, __LINE__, __FILE__)
  98. DICTIONARY *dictionary_create_advanced_with_trace(DICT_OPTIONS options, struct dictionary_stats *stats, size_t fixed_size, const char *function, size_t line, const char *file);
  99. #else
  100. #define dictionary_create(options) dictionary_create_advanced(options, NULL, 0)
  101. DICTIONARY *dictionary_create_advanced(DICT_OPTIONS options, struct dictionary_stats *stats, size_t fixed_size);
  102. #endif
  103. // Create a view on a dictionary
  104. #ifdef NETDATA_INTERNAL_CHECKS
  105. #define dictionary_create_view(master) dictionary_create_view_with_trace(master, __FUNCTION__, __LINE__, __FILE__)
  106. DICTIONARY *dictionary_create_view_with_trace(DICTIONARY *master, const char *function, size_t line, const char *file);
  107. #else
  108. DICTIONARY *dictionary_create_view(DICTIONARY *master);
  109. #endif
  110. // an insert callback to be called just after an item is added to the dictionary
  111. // this callback is called while the dictionary is write locked!
  112. void dictionary_register_insert_callback(DICTIONARY *dict, void (*ins_callback)(const DICTIONARY_ITEM *item, void *value, void *data), void *data);
  113. // a delete callback to be called just before an item is deleted forever
  114. // this callback is called while the dictionary is write locked!
  115. void dictionary_register_delete_callback(DICTIONARY *dict, void (*del_callback)(const DICTIONARY_ITEM *item, void *value, void *data), void *data);
  116. // a merge callback to be called when DICT_OPTION_DONT_OVERWRITE_VALUE
  117. // and an item is already found in the dictionary - the dictionary does nothing else in this case
  118. // the old_value will remain in the dictionary - the new_value is ignored
  119. // The callback should return true if the value has been updated (it increases the dictionary version).
  120. void dictionary_register_conflict_callback(DICTIONARY *dict, bool (*conflict_callback)(const DICTIONARY_ITEM *item, void *old_value, void *new_value, void *data), void *data);
  121. // a reaction callback to be called after every item insertion or conflict
  122. // after the constructors have finished and the items are fully available for use
  123. // and the dictionary is not write locked anymore
  124. void dictionary_register_react_callback(DICTIONARY *dict, void (*react_callback)(const DICTIONARY_ITEM *item, void *value, void *data), void *data);
  125. // Destroy a dictionary
  126. // Returns the number of bytes freed
  127. // The returned value will not include name/key sizes
  128. // Registered delete callbacks will be run for each item in the dictionary.
  129. size_t dictionary_destroy(DICTIONARY *dict);
  130. // Empties a dictionary
  131. // Referenced items will survive, but are not offered anymore.
  132. // Registered delete callbacks will be run for each item in the dictionary.
  133. void dictionary_flush(DICTIONARY *dict);
  134. void dictionary_version_increment(DICTIONARY *dict);
  135. void dictionary_garbage_collect(DICTIONARY *dict);
  136. // ----------------------------------------------------------------------------
  137. // Set an item in the dictionary
  138. //
  139. // - if an item with the same name does not exist, create one
  140. // - if an item with the same name exists, then:
  141. // a) if DICT_OPTION_DONT_OVERWRITE_VALUE is set, just return the existing value (ignore the new value)
  142. // else b) reset the value to the new value passed at the call
  143. //
  144. // When DICT_OPTION_VALUE_LINK_DONT_CLONE is set, the value is linked, otherwise it is copied
  145. // When DICT_OPTION_NAME_LINK_DONT_CLONE is set, the name is linked, otherwise it is copied
  146. //
  147. // When neither DICT_OPTION_VALUE_LINK_DONT_CLONE nor DICT_OPTION_NAME_LINK_DONT_CLONE are set, all the
  148. // memory management for names and values is done by the dictionary.
  149. //
  150. // Passing NULL as value, the dictionary will callocz() the newly allocated value, otherwise it will copy it.
  151. // Passing 0 as value_len, the dictionary will set the value to NULL (no allocations for value will be made).
  152. #define dictionary_set(dict, name, value, value_len) dictionary_set_advanced(dict, name, -1, value, value_len, NULL)
  153. void *dictionary_set_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, void *value, size_t value_len, void *constructor_data);
  154. #define dictionary_set_and_acquire_item(dict, name, value, value_len) dictionary_set_and_acquire_item_advanced(dict, name, -1, value, value_len, NULL)
  155. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_set_and_acquire_item_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, void *value, size_t value_len, void *constructor_data);
  156. // set an item in a dictionary view
  157. #define dictionary_view_set_and_acquire_item(dict, name, master_item) dictionary_view_set_and_acquire_item_advanced(dict, name, -1, master_item)
  158. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_view_set_and_acquire_item_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, DICTIONARY_ITEM *master_item);
  159. #define dictionary_view_set(dict, name, master_item) dictionary_view_set_advanced(dict, name, -1, master_item)
  160. void *dictionary_view_set_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, DICT_ITEM_CONST DICTIONARY_ITEM *master_item);
  161. // ----------------------------------------------------------------------------
  162. // Get an item from the dictionary
  163. // If it returns NULL, the item is not found
  164. #define dictionary_get(dict, name) dictionary_get_advanced(dict, name, -1)
  165. void *dictionary_get_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  166. #define dictionary_get_and_acquire_item(dict, name) dictionary_get_and_acquire_item_advanced(dict, name, -1)
  167. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_get_and_acquire_item_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  168. // ----------------------------------------------------------------------------
  169. // Delete an item from the dictionary
  170. // returns true if the item was found and has been deleted
  171. // returns false if the item was not found in the index
  172. #define dictionary_del(dict, name) dictionary_del_advanced(dict, name, -1)
  173. bool dictionary_del_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  174. // ----------------------------------------------------------------------------
  175. // reference counters management
  176. void dictionary_acquired_item_release(DICTIONARY *dict, DICT_ITEM_CONST DICTIONARY_ITEM *item);
  177. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_acquired_item_dup(DICTIONARY *dict, DICT_ITEM_CONST DICTIONARY_ITEM *item);
  178. const char *dictionary_acquired_item_name(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  179. void *dictionary_acquired_item_value(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  180. size_t dictionary_acquired_item_references(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  181. // ----------------------------------------------------------------------------
  182. // Traverse (walk through) the items of the dictionary.
  183. // The order of traversal is currently the order of insertion.
  184. //
  185. // The callback function may return a negative number to stop the traversal,
  186. // in which case that negative value is returned to the caller.
  187. //
  188. // If all callback calls return zero or positive numbers, the sum of all of
  189. // them is returned to the caller.
  190. //
  191. // You cannot alter the dictionary from inside a dictionary_walkthrough_read() - deadlock!
  192. // You can only delete the current item from inside a dictionary_walkthrough_write() - you can add as many as you want.
  193. //
  194. #define dictionary_walkthrough_read(dict, callback, data) dictionary_walkthrough_rw(dict, 'r', callback, data)
  195. #define dictionary_walkthrough_write(dict, callback, data) dictionary_walkthrough_rw(dict, 'w', callback, data)
  196. int dictionary_walkthrough_rw(DICTIONARY *dict, char rw, int (*callback)(const DICTIONARY_ITEM *item, void *value, void *data), void *data);
  197. typedef int (*dictionary_sorted_compar)(const DICTIONARY_ITEM **item1, const DICTIONARY_ITEM **item2);
  198. #define dictionary_sorted_walkthrough_read(dict, callback, data) dictionary_sorted_walkthrough_rw(dict, 'r', callback, data, NULL)
  199. #define dictionary_sorted_walkthrough_write(dict, callback, data) dictionary_sorted_walkthrough_rw(dict, 'w', callback, data, NULL)
  200. int dictionary_sorted_walkthrough_rw(DICTIONARY *dict, char rw, int (*callback)(const DICTIONARY_ITEM *item, void *entry, void *data), void *data, dictionary_sorted_compar compar);
  201. // ----------------------------------------------------------------------------
  202. // Traverse with foreach
  203. //
  204. // Use like this:
  205. //
  206. // DICTFE dfe = {};
  207. // for(MY_ITEM *item = dfe_start_read(&dfe, dict); item ; item = dfe_next(&dfe)) {
  208. // // do things with the item and its dfe.name
  209. // }
  210. // dfe_done(&dfe);
  211. //
  212. // You cannot alter the dictionary from within a dfe_read_start() - deadlock!
  213. // You can only delete the current item from inside a dfe_start_write() - you can add as many as you want.
  214. //
  215. #define DICTIONARY_LOCK_READ 'r'
  216. #define DICTIONARY_LOCK_WRITE 'w'
  217. #define DICTIONARY_LOCK_REENTRANT 'z'
  218. void dictionary_write_lock(DICTIONARY *dict);
  219. void dictionary_write_unlock(DICTIONARY *dict);
  220. typedef DICTFE_CONST struct dictionary_foreach {
  221. DICTIONARY *dict; // the dictionary upon we work
  222. DICTIONARY_ITEM *item; // the item we work on, to remember the position we are at
  223. // this can be used with dictionary_acquired_item_dup() to
  224. // acquire the currently working item.
  225. const char *name; // the dictionary name of the last item used
  226. void *value; // the dictionary value of the last item used
  227. // same as the return value of dictfe_start() and dictfe_next()
  228. size_t counter; // counts the number of iterations made, starting from zero
  229. char rw; // the lock mode 'r' or 'w'
  230. bool locked; // true when the dictionary is locked
  231. } DICTFE;
  232. #define dfe_start_read(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_READ)
  233. #define dfe_start_write(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_WRITE)
  234. #define dfe_start_reentrant(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_REENTRANT)
  235. #define dfe_start_rw(dict, value, mode) \
  236. do { \
  237. DICTFE value ## _dfe = {}; \
  238. (void)(value); /* needed to avoid warning when looping without using this */ \
  239. for((value) = dictionary_foreach_start_rw(&value ## _dfe, (dict), (mode)); \
  240. (value ## _dfe.item) ; \
  241. (value) = dictionary_foreach_next(&value ## _dfe)) \
  242. {
  243. #define dfe_done(value) \
  244. } \
  245. dictionary_foreach_done(&value ## _dfe); \
  246. } while(0)
  247. #define dfe_unlock(value) dictionary_foreach_unlock(&value ## _dfe);
  248. void *dictionary_foreach_start_rw(DICTFE *dfe, DICTIONARY *dict, char rw);
  249. void *dictionary_foreach_next(DICTFE *dfe);
  250. void dictionary_foreach_done(DICTFE *dfe);
  251. void dictionary_foreach_unlock(DICTFE *dfe);
  252. // ----------------------------------------------------------------------------
  253. // Get statistics about the dictionary
  254. size_t dictionary_version(DICTIONARY *dict);
  255. size_t dictionary_entries(DICTIONARY *dict);
  256. size_t dictionary_referenced_items(DICTIONARY *dict);
  257. // for all cases that the caller does not provide a stats structure, this is where they are accumulated.
  258. extern struct dictionary_stats dictionary_stats_category_other;
  259. int dictionary_unittest(size_t entries);
  260. // ----------------------------------------------------------------------------
  261. // THREAD CACHE
  262. void *thread_cache_entry_get_or_set(void *key,
  263. ssize_t key_length,
  264. void *value,
  265. void *(*transform_the_value_before_insert)(void *key, size_t key_length, void *value));
  266. void thread_cache_destroy(void);
  267. #endif /* NETDATA_DICTIONARY_H */