string.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. #include <Judy.h>
  4. typedef int32_t REFCOUNT;
  5. // ----------------------------------------------------------------------------
  6. // STRING implementation - dedup all STRING
  7. #define STRING_PARTITION_SHIFTS (0)
  8. #define STRING_PARTITIONS (256 >> STRING_PARTITION_SHIFTS)
  9. #define string_partition_str(str) ((uint8_t)((str)[0]) >> STRING_PARTITION_SHIFTS)
  10. #define string_partition(string) (string_partition_str((string)->str))
  11. struct netdata_string {
  12. uint32_t length; // the string length including the terminating '\0'
  13. REFCOUNT refcount; // how many times this string is used
  14. // We use a signed number to be able to detect duplicate frees of a string.
  15. // If at any point this goes below zero, we have a duplicate free.
  16. const char str[]; // the string itself, is appended to this structure
  17. };
  18. static struct string_partition {
  19. RW_SPINLOCK spinlock; // the R/W spinlock to protect the Judy array
  20. Pvoid_t JudyHSArray; // the Judy array - hashtable
  21. size_t inserts; // the number of successful inserts to the index
  22. size_t deletes; // the number of successful deleted from the index
  23. long int entries; // the number of entries in the index
  24. long int memory; // the memory used, without the JudyHS index
  25. #ifdef NETDATA_INTERNAL_CHECKS
  26. // internal statistics
  27. struct {
  28. size_t searches; // the number of successful searches in the index
  29. size_t releases; // when a string is unreferenced
  30. size_t duplications; // when a string is referenced
  31. long int active_references; // the number of active references alive
  32. } atomic;
  33. size_t found_deleted_on_search;
  34. size_t found_available_on_search;
  35. size_t found_deleted_on_insert;
  36. size_t found_available_on_insert;
  37. size_t spins;
  38. #endif
  39. } string_base[STRING_PARTITIONS] = { 0 };
  40. #ifdef NETDATA_INTERNAL_CHECKS
  41. #define string_stats_atomic_increment(partition, var) __atomic_add_fetch(&string_base[partition].atomic.var, 1, __ATOMIC_RELAXED)
  42. #define string_stats_atomic_decrement(partition, var) __atomic_sub_fetch(&string_base[partition].atomic.var, 1, __ATOMIC_RELAXED)
  43. #define string_internal_stats_add(partition, var, val) __atomic_add_fetch(&string_base[partition].var, val, __ATOMIC_RELAXED)
  44. #else
  45. #define string_stats_atomic_increment(partition, var) do {;} while(0)
  46. #define string_stats_atomic_decrement(partition, var) do {;} while(0)
  47. #define string_internal_stats_add(partition, var, val) do {;} while(0)
  48. #endif
  49. void string_statistics(size_t *inserts, size_t *deletes, size_t *searches, size_t *entries, size_t *references, size_t *memory, size_t *duplications, size_t *releases) {
  50. if (inserts) *inserts = 0;
  51. if (deletes) *deletes = 0;
  52. if (searches) *searches = 0;
  53. if (entries) *entries = 0;
  54. if (references) *references = 0;
  55. if (memory) *memory = 0;
  56. if (duplications) *duplications = 0;
  57. if (releases) *releases = 0;
  58. for(size_t i = 0; i < STRING_PARTITIONS ;i++) {
  59. if (inserts) *inserts += string_base[i].inserts;
  60. if (deletes) *deletes += string_base[i].deletes;
  61. if (entries) *entries += (size_t) string_base[i].entries;
  62. if (memory) *memory += (size_t) string_base[i].memory;
  63. #ifdef NETDATA_INTERNAL_CHECKS
  64. if (searches) *searches += string_base[i].atomic.searches;
  65. if (references) *references += (size_t) string_base[i].atomic.active_references;
  66. if (duplications) *duplications += string_base[i].atomic.duplications;
  67. if (releases) *releases += string_base[i].atomic.releases;
  68. #endif
  69. }
  70. }
  71. #define string_entry_acquire(se) __atomic_add_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST);
  72. #define string_entry_release(se) __atomic_sub_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST);
  73. static inline bool string_entry_check_and_acquire(STRING *se) {
  74. #ifdef NETDATA_INTERNAL_CHECKS
  75. uint8_t partition = string_partition(se);
  76. #endif
  77. REFCOUNT expected, desired, count = 0;
  78. expected = __atomic_load_n(&se->refcount, __ATOMIC_SEQ_CST);
  79. do {
  80. count++;
  81. if(expected <= 0) {
  82. // We cannot use this.
  83. // The reference counter reached value zero,
  84. // so another thread is deleting this.
  85. string_internal_stats_add(partition, spins, count - 1);
  86. return false;
  87. }
  88. desired = expected + 1;
  89. } while(!__atomic_compare_exchange_n(&se->refcount, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
  90. string_internal_stats_add(partition, spins, count - 1);
  91. // statistics
  92. // string_base.active_references is altered at the in string_strdupz() and string_freez()
  93. string_stats_atomic_increment(partition, duplications);
  94. return true;
  95. }
  96. STRING *string_dup(STRING *string) {
  97. if(unlikely(!string)) return NULL;
  98. #ifdef NETDATA_INTERNAL_CHECKS
  99. if(unlikely(__atomic_load_n(&string->refcount, __ATOMIC_SEQ_CST) <= 0))
  100. fatal("STRING: tried to %s() a string that is freed (it has %d references).", __FUNCTION__, string->refcount);
  101. #endif
  102. string_entry_acquire(string);
  103. #ifdef NETDATA_INTERNAL_CHECKS
  104. uint8_t partition = string_partition(string);
  105. #endif
  106. // statistics
  107. string_stats_atomic_increment(partition, active_references);
  108. string_stats_atomic_increment(partition, duplications);
  109. return string;
  110. }
  111. // Search the index and return an ACQUIRED string entry, or NULL
  112. static inline STRING *string_index_search(const char *str, size_t length) {
  113. STRING *string;
  114. uint8_t partition = string_partition_str(str);
  115. // Find the string in the index
  116. // With a read-lock so that multiple readers can use the index concurrently.
  117. rw_spinlock_read_lock(&string_base[partition].spinlock);
  118. Pvoid_t *Rc;
  119. Rc = JudyHSGet(string_base[partition].JudyHSArray, (void *)str, length - 1);
  120. if(likely(Rc)) {
  121. // found in the hash table
  122. string = *Rc;
  123. if(string_entry_check_and_acquire(string)) {
  124. // we can use this entry
  125. string_internal_stats_add(partition, found_available_on_search, 1);
  126. }
  127. else {
  128. // this entry is about to be deleted by another thread
  129. // do not touch it, let it go...
  130. string = NULL;
  131. string_internal_stats_add(partition, found_deleted_on_search, 1);
  132. }
  133. }
  134. else {
  135. // not found in the hash table
  136. string = NULL;
  137. }
  138. string_stats_atomic_increment(partition, searches);
  139. rw_spinlock_read_unlock(&string_base[partition].spinlock);
  140. return string;
  141. }
  142. // Insert a string to the index and return an ACQUIRED string entry,
  143. // or NULL if the call needs to be retried (a deleted entry with the same key is still in the index)
  144. // The returned entry is ACQUIRED, and it can either be:
  145. // 1. a new item inserted, or
  146. // 2. an item found in the index that is not currently deleted
  147. static inline STRING *string_index_insert(const char *str, size_t length) {
  148. STRING *string;
  149. uint8_t partition = string_partition_str(str);
  150. rw_spinlock_write_lock(&string_base[partition].spinlock);
  151. STRING **ptr;
  152. {
  153. JError_t J_Error;
  154. Pvoid_t *Rc = JudyHSIns(&string_base[partition].JudyHSArray, (void *)str, length - 1, &J_Error);
  155. if (unlikely(Rc == PJERR)) {
  156. fatal(
  157. "STRING: Cannot insert entry with name '%s' to JudyHS, JU_ERRNO_* == %u, ID == %d",
  158. str,
  159. JU_ERRNO(&J_Error),
  160. JU_ERRID(&J_Error));
  161. }
  162. ptr = (STRING **)Rc;
  163. }
  164. if (likely(*ptr == 0)) {
  165. // a new item added to the index
  166. size_t mem_size = sizeof(STRING) + length;
  167. string = mallocz(mem_size);
  168. strcpy((char *)string->str, str);
  169. string->length = length;
  170. string->refcount = 1;
  171. *ptr = string;
  172. string_base[partition].inserts++;
  173. string_base[partition].entries++;
  174. string_base[partition].memory += (long)(mem_size + JUDYHS_INDEX_SIZE_ESTIMATE(length));
  175. }
  176. else {
  177. // the item is already in the index
  178. string = *ptr;
  179. if(string_entry_check_and_acquire(string)) {
  180. // we can use this entry
  181. string_internal_stats_add(partition, found_available_on_insert, 1);
  182. }
  183. else {
  184. // this entry is about to be deleted by another thread
  185. // do not touch it, let it go...
  186. string = NULL;
  187. string_internal_stats_add(partition, found_deleted_on_insert, 1);
  188. }
  189. string_stats_atomic_increment(partition, searches);
  190. }
  191. rw_spinlock_write_unlock(&string_base[partition].spinlock);
  192. return string;
  193. }
  194. // delete an entry from the index
  195. static inline void string_index_delete(STRING *string) {
  196. uint8_t partition = string_partition(string);
  197. rw_spinlock_write_lock(&string_base[partition].spinlock);
  198. #ifdef NETDATA_INTERNAL_CHECKS
  199. if(unlikely(__atomic_load_n(&string->refcount, __ATOMIC_SEQ_CST) != 0))
  200. fatal("STRING: tried to delete a string at %s() that is already freed (it has %d references).", __FUNCTION__, string->refcount);
  201. #endif
  202. bool deleted = false;
  203. if (likely(string_base[partition].JudyHSArray)) {
  204. JError_t J_Error;
  205. int ret = JudyHSDel(&string_base[partition].JudyHSArray, (void *)string->str, string->length - 1, &J_Error);
  206. if (unlikely(ret == JERR)) {
  207. netdata_log_error(
  208. "STRING: Cannot delete entry with name '%s' from JudyHS, JU_ERRNO_* == %u, ID == %d",
  209. string->str,
  210. JU_ERRNO(&J_Error),
  211. JU_ERRID(&J_Error));
  212. } else
  213. deleted = true;
  214. }
  215. if (unlikely(!deleted))
  216. netdata_log_error("STRING: tried to delete '%s' that is not in the index. Ignoring it.", string->str);
  217. else {
  218. size_t mem_size = sizeof(STRING) + string->length;
  219. string_base[partition].deletes++;
  220. string_base[partition].entries--;
  221. string_base[partition].memory -= (long)(mem_size + JUDYHS_INDEX_SIZE_ESTIMATE(string->length));
  222. freez(string);
  223. }
  224. rw_spinlock_write_unlock(&string_base[partition].spinlock);
  225. }
  226. STRING *string_strdupz(const char *str) {
  227. if(unlikely(!str || !*str)) return NULL;
  228. #ifdef NETDATA_INTERNAL_CHECKS
  229. uint8_t partition = string_partition_str(str);
  230. #endif
  231. size_t length = strlen(str) + 1;
  232. STRING *string = string_index_search(str, length);
  233. while(!string) {
  234. // The search above did not find anything,
  235. // We loop here, because during insert we may find an entry that is being deleted by another thread.
  236. // So, we have to let it go and retry to insert it again.
  237. string = string_index_insert(str, length);
  238. }
  239. // statistics
  240. string_stats_atomic_increment(partition, active_references);
  241. return string;
  242. }
  243. void string_freez(STRING *string) {
  244. if(unlikely(!string)) return;
  245. #ifdef NETDATA_INTERNAL_CHECKS
  246. uint8_t partition = string_partition(string);
  247. #endif
  248. REFCOUNT refcount = string_entry_release(string);
  249. #ifdef NETDATA_INTERNAL_CHECKS
  250. if(unlikely(refcount < 0))
  251. fatal("STRING: tried to %s() a string that is already freed (it has %d references).", __FUNCTION__, string->refcount);
  252. #endif
  253. if(unlikely(refcount == 0))
  254. string_index_delete(string);
  255. // statistics
  256. string_stats_atomic_decrement(partition, active_references);
  257. string_stats_atomic_increment(partition, releases);
  258. }
  259. inline size_t string_strlen(STRING *string) {
  260. if(unlikely(!string)) return 0;
  261. return string->length - 1;
  262. }
  263. inline const char *string2str(STRING *string) {
  264. if(unlikely(!string)) return "";
  265. return string->str;
  266. }
  267. int string_strcmp(STRING *string, const char *s) {
  268. return strcmp(string2str(string), s);
  269. }
  270. STRING *string_2way_merge(STRING *a, STRING *b) {
  271. static STRING *X = NULL;
  272. if(unlikely(!X)) {
  273. X = string_strdupz("[x]");
  274. }
  275. if(unlikely(a == b)) return string_dup(a);
  276. if(unlikely(a == X)) return string_dup(a);
  277. if(unlikely(b == X)) return string_dup(b);
  278. if(unlikely(!a)) return string_dup(X);
  279. if(unlikely(!b)) return string_dup(X);
  280. size_t alen = string_strlen(a);
  281. size_t blen = string_strlen(b);
  282. size_t length = alen + blen + string_strlen(X) + 1;
  283. char buf1[length + 1], buf2[length + 1], *dst1;
  284. const char *s1, *s2;
  285. s1 = string2str(a);
  286. s2 = string2str(b);
  287. dst1 = buf1;
  288. for( ; *s1 && *s2 && *s1 == *s2 ;s1++, s2++)
  289. *dst1++ = *s1;
  290. *dst1 = '\0';
  291. if(*s1 != '\0' || *s2 != '\0') {
  292. *dst1++ = '[';
  293. *dst1++ = 'x';
  294. *dst1++ = ']';
  295. s1 = &(string2str(a))[alen - 1];
  296. s2 = &(string2str(b))[blen - 1];
  297. char *dst2 = &buf2[length];
  298. *dst2 = '\0';
  299. for (; *s1 && *s2 && *s1 == *s2; s1--, s2--)
  300. *(--dst2) = *s1;
  301. strcpy(dst1, dst2);
  302. }
  303. return string_strdupz(buf1);
  304. }
  305. // ----------------------------------------------------------------------------
  306. // STRING unit test
  307. struct thread_unittest {
  308. int join;
  309. int dups;
  310. };
  311. static void *string_thread(void *arg) {
  312. struct thread_unittest *tu = arg;
  313. for(; 1 ;) {
  314. if(__atomic_load_n(&tu->join, __ATOMIC_RELAXED))
  315. break;
  316. STRING *s = string_strdupz("string thread checking 1234567890");
  317. for(int i = 0; i < tu->dups ; i++)
  318. string_dup(s);
  319. for(int i = 0; i < tu->dups ; i++)
  320. string_freez(s);
  321. string_freez(s);
  322. }
  323. return arg;
  324. }
  325. static char **string_unittest_generate_names(size_t entries) {
  326. char **names = mallocz(sizeof(char *) * entries);
  327. for(size_t i = 0; i < entries ;i++) {
  328. char buf[25 + 1] = "";
  329. snprintfz(buf, 25, "name.%zu.0123456789.%zu \t !@#$%%^&*(),./[]{}\\|~`", i, entries / 2 + i);
  330. names[i] = strdupz(buf);
  331. }
  332. return names;
  333. }
  334. static void string_unittest_free_char_pp(char **pp, size_t entries) {
  335. for(size_t i = 0; i < entries ;i++)
  336. freez(pp[i]);
  337. freez(pp);
  338. }
  339. static long unittest_string_entries(void) {
  340. long entries = 0;
  341. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  342. entries += string_base[p].entries;
  343. return entries;
  344. }
  345. #ifdef NETDATA_INTERNAL_CHECKS
  346. static size_t unittest_string_found_deleted_on_search(void) {
  347. size_t entries = 0;
  348. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  349. entries += string_base[p].found_deleted_on_search;
  350. return entries;
  351. }
  352. static size_t unittest_string_found_available_on_search(void) {
  353. size_t entries = 0;
  354. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  355. entries += string_base[p].found_available_on_search;
  356. return entries;
  357. }
  358. static size_t unittest_string_found_deleted_on_insert(void) {
  359. size_t entries = 0;
  360. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  361. entries += string_base[p].found_deleted_on_insert;
  362. return entries;
  363. }
  364. static size_t unittest_string_found_available_on_insert(void) {
  365. size_t entries = 0;
  366. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  367. entries += string_base[p].found_available_on_insert;
  368. return entries;
  369. }
  370. static size_t unittest_string_spins(void) {
  371. size_t entries = 0;
  372. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  373. entries += string_base[p].spins;
  374. return entries;
  375. }
  376. #endif // NETDATA_INTERNAL_CHECKS
  377. int string_unittest(size_t entries) {
  378. size_t errors = 0;
  379. fprintf(stderr, "Generating %zu names and values...\n", entries);
  380. char **names = string_unittest_generate_names(entries);
  381. // check string
  382. {
  383. long entries_starting = unittest_string_entries();
  384. fprintf(stderr, "\nChecking strings...\n");
  385. STRING *s1 = string_strdupz("hello unittest");
  386. STRING *s2 = string_strdupz("hello unittest");
  387. if(s1 != s2) {
  388. errors++;
  389. fprintf(stderr, "ERROR: duplicating strings are not deduplicated\n");
  390. }
  391. else
  392. fprintf(stderr, "OK: duplicating string are deduplicated\n");
  393. STRING *s3 = string_dup(s1);
  394. if(s3 != s1) {
  395. errors++;
  396. fprintf(stderr, "ERROR: cloning strings are not deduplicated\n");
  397. }
  398. else
  399. fprintf(stderr, "OK: cloning string are deduplicated\n");
  400. if(s1->refcount != 3) {
  401. errors++;
  402. fprintf(stderr, "ERROR: string refcount is not 3\n");
  403. }
  404. else
  405. fprintf(stderr, "OK: string refcount is 3\n");
  406. STRING *s4 = string_strdupz("world unittest");
  407. if(s4 == s1) {
  408. errors++;
  409. fprintf(stderr, "ERROR: string is sharing pointers on different strings\n");
  410. }
  411. else
  412. fprintf(stderr, "OK: string is properly handling different strings\n");
  413. usec_t start_ut, end_ut;
  414. STRING **strings = mallocz(entries * sizeof(STRING *));
  415. start_ut = now_realtime_usec();
  416. for(size_t i = 0; i < entries ;i++) {
  417. strings[i] = string_strdupz(names[i]);
  418. }
  419. end_ut = now_realtime_usec();
  420. fprintf(stderr, "Created %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  421. start_ut = now_realtime_usec();
  422. for(size_t i = 0; i < entries ;i++) {
  423. strings[i] = string_dup(strings[i]);
  424. }
  425. end_ut = now_realtime_usec();
  426. fprintf(stderr, "Cloned %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  427. start_ut = now_realtime_usec();
  428. for(size_t i = 0; i < entries ;i++) {
  429. strings[i] = string_strdupz(string2str(strings[i]));
  430. }
  431. end_ut = now_realtime_usec();
  432. fprintf(stderr, "Found %zu existing strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  433. start_ut = now_realtime_usec();
  434. for(size_t i = 0; i < entries ;i++) {
  435. string_freez(strings[i]);
  436. }
  437. end_ut = now_realtime_usec();
  438. fprintf(stderr, "Released %zu referenced strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  439. start_ut = now_realtime_usec();
  440. for(size_t i = 0; i < entries ;i++) {
  441. string_freez(strings[i]);
  442. }
  443. end_ut = now_realtime_usec();
  444. fprintf(stderr, "Released (again) %zu referenced strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  445. start_ut = now_realtime_usec();
  446. for(size_t i = 0; i < entries ;i++) {
  447. string_freez(strings[i]);
  448. }
  449. end_ut = now_realtime_usec();
  450. fprintf(stderr, "Freed %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  451. freez(strings);
  452. if(unittest_string_entries() != entries_starting + 2) {
  453. errors++;
  454. fprintf(stderr, "ERROR: strings dictionary should have %ld items but it has %ld\n",
  455. entries_starting + 2, unittest_string_entries());
  456. }
  457. else
  458. fprintf(stderr, "OK: strings dictionary has 2 items\n");
  459. }
  460. // check 2-way merge
  461. {
  462. struct testcase {
  463. char *src1; char *src2; char *expected;
  464. } tests[] = {
  465. { "", "", ""},
  466. { "a", "", "[x]"},
  467. { "", "a", "[x]"},
  468. { "a", "a", "a"},
  469. { "abcd", "abcd", "abcd"},
  470. { "foo_cs", "bar_cs", "[x]_cs"},
  471. { "cp_UNIQUE_INFIX_cs", "cp_unique_infix_cs", "cp_[x]_cs"},
  472. { "cp_UNIQUE_INFIX_ci_unique_infix_cs", "cp_unique_infix_ci_UNIQUE_INFIX_cs", "cp_[x]_cs"},
  473. { "foo[1234]", "foo[4321]", "foo[[x]]"},
  474. { NULL, NULL, NULL },
  475. };
  476. for (struct testcase *tc = &tests[0]; tc->expected != NULL; tc++) {
  477. STRING *src1 = string_strdupz(tc->src1);
  478. STRING *src2 = string_strdupz(tc->src2);
  479. STRING *expected = string_strdupz(tc->expected);
  480. STRING *result = string_2way_merge(src1, src2);
  481. if (string_cmp(result, expected) != 0) {
  482. fprintf(stderr, "string_2way_merge(\"%s\", \"%s\") -> \"%s\" (expected=\"%s\")\n",
  483. string2str(src1),
  484. string2str(src2),
  485. string2str(result),
  486. string2str(expected));
  487. errors++;
  488. }
  489. string_freez(src1);
  490. string_freez(src2);
  491. string_freez(expected);
  492. string_freez(result);
  493. }
  494. }
  495. // threads testing of string
  496. {
  497. struct thread_unittest tu = {
  498. .dups = 1,
  499. .join = 0,
  500. };
  501. #ifdef NETDATA_INTERNAL_CHECKS
  502. size_t ofound_deleted_on_search = unittest_string_found_deleted_on_search(),
  503. ofound_available_on_search = unittest_string_found_available_on_search(),
  504. ofound_deleted_on_insert = unittest_string_found_deleted_on_insert(),
  505. ofound_available_on_insert = unittest_string_found_available_on_insert(),
  506. ospins = unittest_string_spins();
  507. #endif
  508. size_t oinserts, odeletes, osearches, oentries, oreferences, omemory, oduplications, oreleases;
  509. string_statistics(&oinserts, &odeletes, &osearches, &oentries, &oreferences, &omemory, &oduplications, &oreleases);
  510. time_t seconds_to_run = 5;
  511. int threads_to_create = 2;
  512. fprintf(
  513. stderr,
  514. "Checking string concurrency with %d threads for %lld seconds...\n",
  515. threads_to_create,
  516. (long long)seconds_to_run);
  517. // check string concurrency
  518. netdata_thread_t threads[threads_to_create];
  519. tu.join = 0;
  520. for (int i = 0; i < threads_to_create; i++) {
  521. char buf[100 + 1];
  522. snprintf(buf, 100, "string%d", i);
  523. netdata_thread_create(
  524. &threads[i], buf, NETDATA_THREAD_OPTION_DONT_LOG | NETDATA_THREAD_OPTION_JOINABLE, string_thread, &tu);
  525. }
  526. sleep_usec(seconds_to_run * USEC_PER_SEC);
  527. __atomic_store_n(&tu.join, 1, __ATOMIC_RELAXED);
  528. for (int i = 0; i < threads_to_create; i++) {
  529. void *retval;
  530. netdata_thread_join(threads[i], &retval);
  531. }
  532. size_t inserts, deletes, searches, sentries, references, memory, duplications, releases;
  533. string_statistics(&inserts, &deletes, &searches, &sentries, &references, &memory, &duplications, &releases);
  534. fprintf(stderr, "inserts %zu, deletes %zu, searches %zu, entries %zu, references %zu, memory %zu, duplications %zu, releases %zu\n",
  535. inserts - oinserts, deletes - odeletes, searches - osearches, sentries - oentries, references - oreferences, memory - omemory, duplications - oduplications, releases - oreleases);
  536. #ifdef NETDATA_INTERNAL_CHECKS
  537. size_t found_deleted_on_search = unittest_string_found_deleted_on_search(),
  538. found_available_on_search = unittest_string_found_available_on_search(),
  539. found_deleted_on_insert = unittest_string_found_deleted_on_insert(),
  540. found_available_on_insert = unittest_string_found_available_on_insert(),
  541. spins = unittest_string_spins();
  542. fprintf(stderr, "on insert: %zu ok + %zu deleted\non search: %zu ok + %zu deleted\nspins: %zu\n",
  543. found_available_on_insert - ofound_available_on_insert,
  544. found_deleted_on_insert - ofound_deleted_on_insert,
  545. found_available_on_search - ofound_available_on_search,
  546. found_deleted_on_search - ofound_deleted_on_search,
  547. spins - ospins
  548. );
  549. #endif
  550. }
  551. string_unittest_free_char_pp(names, entries);
  552. fprintf(stderr, "\n%zu errors found\n", errors);
  553. return errors ? 1 : 0;
  554. }