string.c 23 KB

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