dictionary.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. // ----------------------------------------------------------------------------
  4. // dictionary statistics
  5. static inline void NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(DICTIONARY *dict) {
  6. if(likely(dict->stats))
  7. dict->stats->inserts++;
  8. }
  9. static inline void NETDATA_DICTIONARY_STATS_DELETES_PLUS1(DICTIONARY *dict) {
  10. if(likely(dict->stats))
  11. dict->stats->deletes++;
  12. }
  13. static inline void NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
  14. if(likely(dict->stats))
  15. dict->stats->searches++;
  16. }
  17. static inline void NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict) {
  18. if(likely(dict->stats))
  19. dict->stats->entries++;
  20. }
  21. static inline void NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict) {
  22. if(likely(dict->stats))
  23. dict->stats->entries--;
  24. }
  25. // ----------------------------------------------------------------------------
  26. // dictionary locks
  27. static inline void dictionary_read_lock(DICTIONARY *dict) {
  28. if(likely(dict->rwlock)) {
  29. // debug(D_DICTIONARY, "Dictionary READ lock");
  30. netdata_rwlock_rdlock(dict->rwlock);
  31. }
  32. }
  33. static inline void dictionary_write_lock(DICTIONARY *dict) {
  34. if(likely(dict->rwlock)) {
  35. // debug(D_DICTIONARY, "Dictionary WRITE lock");
  36. netdata_rwlock_wrlock(dict->rwlock);
  37. }
  38. }
  39. static inline void dictionary_unlock(DICTIONARY *dict) {
  40. if(likely(dict->rwlock)) {
  41. // debug(D_DICTIONARY, "Dictionary UNLOCK lock");
  42. netdata_rwlock_unlock(dict->rwlock);
  43. }
  44. }
  45. // ----------------------------------------------------------------------------
  46. // avl index
  47. static int name_value_compare(void* a, void* b) {
  48. if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
  49. else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
  50. else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
  51. }
  52. static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
  53. NAME_VALUE tmp;
  54. tmp.hash = (hash)?hash:simple_hash(name);
  55. tmp.name = (char *)name;
  56. NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
  57. return (NAME_VALUE *)avl_search(&(dict->values_index), (avl_t *) &tmp);
  58. }
  59. // ----------------------------------------------------------------------------
  60. // internal methods
  61. static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
  62. debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
  63. NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
  64. if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
  65. nv->name = (char *)name;
  66. else {
  67. nv->name = strdupz(name);
  68. }
  69. nv->hash = (hash)?hash:simple_hash(nv->name);
  70. if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
  71. nv->value = value;
  72. else {
  73. nv->value = mallocz(value_len);
  74. memcpy(nv->value, value, value_len);
  75. }
  76. // index it
  77. NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict);
  78. if(unlikely(avl_insert(&((dict)->values_index), (avl_t *)(nv)) != (avl_t *)nv))
  79. error("dictionary: INTERNAL ERROR: duplicate insertion to dictionary.");
  80. NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict);
  81. return nv;
  82. }
  83. static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
  84. debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
  85. NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict);
  86. if(unlikely(avl_remove(&(dict->values_index), (avl_t *)(nv)) != (avl_t *)nv))
  87. error("dictionary: INTERNAL ERROR: dictionary invalid removal of node.");
  88. NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
  89. if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
  90. debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
  91. freez(nv->value);
  92. }
  93. if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
  94. debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
  95. freez(nv->name);
  96. }
  97. freez(nv);
  98. }
  99. // ----------------------------------------------------------------------------
  100. // API - basic methods
  101. DICTIONARY *dictionary_create(uint8_t flags) {
  102. debug(D_DICTIONARY, "Creating dictionary.");
  103. DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
  104. if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
  105. dict->stats = callocz(1, sizeof(struct dictionary_stats));
  106. if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
  107. dict->rwlock = callocz(1, sizeof(netdata_rwlock_t));
  108. netdata_rwlock_init(dict->rwlock);
  109. }
  110. avl_init(&dict->values_index, name_value_compare);
  111. dict->flags = flags;
  112. return dict;
  113. }
  114. void dictionary_destroy(DICTIONARY *dict) {
  115. debug(D_DICTIONARY, "Destroying dictionary.");
  116. dictionary_write_lock(dict);
  117. while(dict->values_index.root)
  118. dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
  119. dictionary_unlock(dict);
  120. if(dict->stats)
  121. freez(dict->stats);
  122. if(dict->rwlock) {
  123. netdata_rwlock_destroy(dict->rwlock);
  124. freez(dict->rwlock);
  125. }
  126. freez(dict);
  127. }
  128. // ----------------------------------------------------------------------------
  129. void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
  130. debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
  131. uint32_t hash = simple_hash(name);
  132. dictionary_write_lock(dict);
  133. NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
  134. if(unlikely(!nv)) {
  135. debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
  136. nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
  137. if(unlikely(!nv))
  138. fatal("Cannot create name_value.");
  139. }
  140. else {
  141. debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
  142. if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
  143. debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
  144. nv->value = value;
  145. }
  146. else {
  147. debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
  148. // copy the new value without breaking
  149. // any other thread accessing the same entry
  150. void *new = mallocz(value_len),
  151. *old = nv->value;
  152. memcpy(new, value, value_len);
  153. nv->value = new;
  154. debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
  155. freez(old);
  156. }
  157. }
  158. dictionary_unlock(dict);
  159. return nv->value;
  160. }
  161. void *dictionary_get(DICTIONARY *dict, const char *name) {
  162. debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
  163. dictionary_read_lock(dict);
  164. NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
  165. dictionary_unlock(dict);
  166. if(unlikely(!nv)) {
  167. debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
  168. return NULL;
  169. }
  170. debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
  171. return nv->value;
  172. }
  173. int dictionary_del(DICTIONARY *dict, const char *name) {
  174. int ret;
  175. debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
  176. dictionary_write_lock(dict);
  177. NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
  178. if(unlikely(!nv)) {
  179. debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
  180. ret = -1;
  181. }
  182. else {
  183. debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
  184. dictionary_name_value_destroy_nolock(dict, nv);
  185. ret = 0;
  186. }
  187. dictionary_unlock(dict);
  188. return ret;
  189. }
  190. // ----------------------------------------------------------------------------
  191. // API - walk through the dictionary
  192. // the dictionary is locked for reading while this happens
  193. // do not user other dictionary calls while walking the dictionary - deadlock!
  194. static int dictionary_walker(avl_t *a, int (*callback)(void *entry, void *data), void *data) {
  195. int total = 0, ret = 0;
  196. if(a->avl_link[0]) {
  197. ret = dictionary_walker(a->avl_link[0], callback, data);
  198. if(ret < 0) return ret;
  199. total += ret;
  200. }
  201. ret = callback(((NAME_VALUE *)a)->value, data);
  202. if(ret < 0) return ret;
  203. total += ret;
  204. if(a->avl_link[1]) {
  205. ret = dictionary_walker(a->avl_link[1], callback, data);
  206. if (ret < 0) return ret;
  207. total += ret;
  208. }
  209. return total;
  210. }
  211. int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
  212. int ret = 0;
  213. dictionary_read_lock(dict);
  214. if(likely(dict->values_index.root))
  215. ret = dictionary_walker(dict->values_index.root, callback, data);
  216. dictionary_unlock(dict);
  217. return ret;
  218. }
  219. static int dictionary_walker_name_value(avl_t *a, int (*callback)(char *name, void *entry, void *data), void *data) {
  220. int total = 0, ret = 0;
  221. if(a->avl_link[0]) {
  222. ret = dictionary_walker_name_value(a->avl_link[0], callback, data);
  223. if(ret < 0) return ret;
  224. total += ret;
  225. }
  226. ret = callback(((NAME_VALUE *)a)->name, ((NAME_VALUE *)a)->value, data);
  227. if(ret < 0) return ret;
  228. total += ret;
  229. if(a->avl_link[1]) {
  230. ret = dictionary_walker_name_value(a->avl_link[1], callback, data);
  231. if (ret < 0) return ret;
  232. total += ret;
  233. }
  234. return total;
  235. }
  236. int dictionary_get_all_name_value(DICTIONARY *dict, int (*callback)(char *name, void *entry, void *data), void *data) {
  237. int ret = 0;
  238. dictionary_read_lock(dict);
  239. if(likely(dict->values_index.root))
  240. ret = dictionary_walker_name_value(dict->values_index.root, callback, data);
  241. dictionary_unlock(dict);
  242. return ret;
  243. }