dictionary.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 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. // ----------------------------------------------------------------------------
  136. // Set an item in the dictionary
  137. //
  138. // - if an item with the same name does not exist, create one
  139. // - if an item with the same name exists, then:
  140. // a) if DICT_OPTION_DONT_OVERWRITE_VALUE is set, just return the existing value (ignore the new value)
  141. // else b) reset the value to the new value passed at the call
  142. //
  143. // When DICT_OPTION_VALUE_LINK_DONT_CLONE is set, the value is linked, otherwise it is copied
  144. // When DICT_OPTION_NAME_LINK_DONT_CLONE is set, the name is linked, otherwise it is copied
  145. //
  146. // When neither DICT_OPTION_VALUE_LINK_DONT_CLONE nor DICT_OPTION_NAME_LINK_DONT_CLONE are set, all the
  147. // memory management for names and values is done by the dictionary.
  148. //
  149. // Passing NULL as value, the dictionary will callocz() the newly allocated value, otherwise it will copy it.
  150. // Passing 0 as value_len, the dictionary will set the value to NULL (no allocations for value will be made).
  151. #define dictionary_set(dict, name, value, value_len) dictionary_set_advanced(dict, name, -1, value, value_len, NULL)
  152. void *dictionary_set_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, void *value, size_t value_len, void *constructor_data);
  153. #define dictionary_set_and_acquire_item(dict, name, value, value_len) dictionary_set_and_acquire_item_advanced(dict, name, -1, value, value_len, NULL)
  154. 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);
  155. // set an item in a dictionary view
  156. #define dictionary_view_set_and_acquire_item(dict, name, master_item) dictionary_view_set_and_acquire_item_advanced(dict, name, -1, master_item)
  157. 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);
  158. #define dictionary_view_set(dict, name, master_item) dictionary_view_set_advanced(dict, name, -1, master_item)
  159. void *dictionary_view_set_advanced(DICTIONARY *dict, const char *name, ssize_t name_len, DICT_ITEM_CONST DICTIONARY_ITEM *master_item);
  160. // ----------------------------------------------------------------------------
  161. // Get an item from the dictionary
  162. // If it returns NULL, the item is not found
  163. #define dictionary_get(dict, name) dictionary_get_advanced(dict, name, -1)
  164. void *dictionary_get_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  165. #define dictionary_get_and_acquire_item(dict, name) dictionary_get_and_acquire_item_advanced(dict, name, -1)
  166. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_get_and_acquire_item_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  167. // ----------------------------------------------------------------------------
  168. // Delete an item from the dictionary
  169. // returns true if the item was found and has been deleted
  170. // returns false if the item was not found in the index
  171. #define dictionary_del(dict, name) dictionary_del_advanced(dict, name, -1)
  172. bool dictionary_del_advanced(DICTIONARY *dict, const char *name, ssize_t name_len);
  173. // ----------------------------------------------------------------------------
  174. // reference counters management
  175. void dictionary_acquired_item_release(DICTIONARY *dict, DICT_ITEM_CONST DICTIONARY_ITEM *item);
  176. DICT_ITEM_CONST DICTIONARY_ITEM *dictionary_acquired_item_dup(DICTIONARY *dict, DICT_ITEM_CONST DICTIONARY_ITEM *item);
  177. const char *dictionary_acquired_item_name(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  178. void *dictionary_acquired_item_value(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  179. size_t dictionary_acquired_item_references(DICT_ITEM_CONST DICTIONARY_ITEM *item);
  180. // ----------------------------------------------------------------------------
  181. // Traverse (walk through) the items of the dictionary.
  182. // The order of traversal is currently the order of insertion.
  183. //
  184. // The callback function may return a negative number to stop the traversal,
  185. // in which case that negative value is returned to the caller.
  186. //
  187. // If all callback calls return zero or positive numbers, the sum of all of
  188. // them is returned to the caller.
  189. //
  190. // You cannot alter the dictionary from inside a dictionary_walkthrough_read() - deadlock!
  191. // You can only delete the current item from inside a dictionary_walkthrough_write() - you can add as many as you want.
  192. //
  193. #define dictionary_walkthrough_read(dict, callback, data) dictionary_walkthrough_rw(dict, 'r', callback, data)
  194. #define dictionary_walkthrough_write(dict, callback, data) dictionary_walkthrough_rw(dict, 'w', callback, data)
  195. int dictionary_walkthrough_rw(DICTIONARY *dict, char rw, int (*callback)(const DICTIONARY_ITEM *item, void *value, void *data), void *data);
  196. typedef int (*dictionary_sorted_compar)(const DICTIONARY_ITEM **item1, const DICTIONARY_ITEM **item2);
  197. #define dictionary_sorted_walkthrough_read(dict, callback, data) dictionary_sorted_walkthrough_rw(dict, 'r', callback, data, NULL)
  198. #define dictionary_sorted_walkthrough_write(dict, callback, data) dictionary_sorted_walkthrough_rw(dict, 'w', callback, data, NULL)
  199. 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);
  200. // ----------------------------------------------------------------------------
  201. // Traverse with foreach
  202. //
  203. // Use like this:
  204. //
  205. // DICTFE dfe = {};
  206. // for(MY_ITEM *item = dfe_start_read(&dfe, dict); item ; item = dfe_next(&dfe)) {
  207. // // do things with the item and its dfe.name
  208. // }
  209. // dfe_done(&dfe);
  210. //
  211. // You cannot alter the dictionary from within a dfe_read_start() - deadlock!
  212. // You can only delete the current item from inside a dfe_start_write() - you can add as many as you want.
  213. //
  214. #define DICTIONARY_LOCK_READ 'r'
  215. #define DICTIONARY_LOCK_WRITE 'w'
  216. #define DICTIONARY_LOCK_REENTRANT 'z'
  217. void dictionary_write_lock(DICTIONARY *dict);
  218. void dictionary_write_unlock(DICTIONARY *dict);
  219. typedef DICTFE_CONST struct dictionary_foreach {
  220. DICTIONARY *dict; // the dictionary upon we work
  221. DICTIONARY_ITEM *item; // the item we work on, to remember the position we are at
  222. // this can be used with dictionary_acquired_item_dup() to
  223. // acquire the currently working item.
  224. DICTFE_CONST char *name; // the dictionary name of the last item used
  225. void *value; // the dictionary value of the last item used
  226. // same as the return value of dictfe_start() and dictfe_next()
  227. size_t counter; // counts the number of iterations made, starting from zero
  228. char rw; // the lock mode 'r' or 'w'
  229. } DICTFE;
  230. #define dfe_start_read(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_READ)
  231. #define dfe_start_write(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_WRITE)
  232. #define dfe_start_reentrant(dict, value) dfe_start_rw(dict, value, DICTIONARY_LOCK_REENTRANT)
  233. #define dfe_start_rw(dict, value, mode) \
  234. do { \
  235. DICTFE value ## _dfe = {}; \
  236. (void)(value); /* needed to avoid warning when looping without using this */ \
  237. for((value) = dictionary_foreach_start_rw(&value ## _dfe, (dict), (mode)); \
  238. (value ## _dfe.item) ; \
  239. (value) = dictionary_foreach_next(&value ## _dfe)) \
  240. {
  241. #define dfe_done(value) \
  242. } \
  243. dictionary_foreach_done(&value ## _dfe); \
  244. } while(0)
  245. void *dictionary_foreach_start_rw(DICTFE *dfe, DICTIONARY *dict, char rw);
  246. void *dictionary_foreach_next(DICTFE *dfe);
  247. void dictionary_foreach_done(DICTFE *dfe);
  248. // ----------------------------------------------------------------------------
  249. // Get statistics about the dictionary
  250. size_t dictionary_version(DICTIONARY *dict);
  251. size_t dictionary_entries(DICTIONARY *dict);
  252. size_t dictionary_referenced_items(DICTIONARY *dict);
  253. long int dictionary_stats_for_registry(DICTIONARY *dict);
  254. // for all cases that the caller does not provide a stats structure, this is where they are accumulated.
  255. extern struct dictionary_stats dictionary_stats_category_other;
  256. int dictionary_unittest(size_t entries);
  257. // ----------------------------------------------------------------------------
  258. // THREAD CACHE
  259. void *thread_cache_entry_get_or_set(void *key,
  260. ssize_t key_length,
  261. void *value,
  262. void *(*transform_the_value_before_insert)(void *key, size_t key_length, void *value));
  263. void thread_cache_destroy(void);
  264. #endif /* NETDATA_DICTIONARY_H */