string.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. STRING *string_strndupz(const char *str, size_t len) {
  244. if(unlikely(!str || !*str || !len)) return NULL;
  245. #ifdef NETDATA_INTERNAL_CHECKS
  246. uint8_t partition = string_partition_str(str);
  247. #endif
  248. char buf[len + 1];
  249. memcpy(buf, str, len);
  250. buf[len] = '\0';
  251. STRING *string = string_index_search(buf, len + 1);
  252. while(!string)
  253. string = string_index_insert(buf, len + 1);
  254. string_stats_atomic_increment(partition, active_references);
  255. return string;
  256. }
  257. void string_freez(STRING *string) {
  258. if(unlikely(!string)) return;
  259. #ifdef NETDATA_INTERNAL_CHECKS
  260. uint8_t partition = string_partition(string);
  261. #endif
  262. REFCOUNT refcount = string_entry_release(string);
  263. #ifdef NETDATA_INTERNAL_CHECKS
  264. if(unlikely(refcount < 0))
  265. fatal("STRING: tried to %s() a string that is already freed (it has %d references).", __FUNCTION__, string->refcount);
  266. #endif
  267. if(unlikely(refcount == 0))
  268. string_index_delete(string);
  269. // statistics
  270. string_stats_atomic_decrement(partition, active_references);
  271. string_stats_atomic_increment(partition, releases);
  272. }
  273. inline size_t string_strlen(STRING *string) {
  274. if(unlikely(!string)) return 0;
  275. return string->length - 1;
  276. }
  277. inline const char *string2str(STRING *string) {
  278. if(unlikely(!string)) return "";
  279. return string->str;
  280. }
  281. STRING *string_2way_merge(STRING *a, STRING *b) {
  282. static STRING *X = NULL;
  283. if(unlikely(!X)) {
  284. X = string_strdupz("[x]");
  285. }
  286. if(unlikely(a == b)) return string_dup(a);
  287. if(unlikely(a == X)) return string_dup(a);
  288. if(unlikely(b == X)) return string_dup(b);
  289. if(unlikely(!a)) return string_dup(X);
  290. if(unlikely(!b)) return string_dup(X);
  291. size_t alen = string_strlen(a);
  292. size_t blen = string_strlen(b);
  293. size_t length = alen + blen + string_strlen(X) + 1;
  294. char buf1[length + 1], buf2[length + 1], *dst1;
  295. const char *s1, *s2;
  296. s1 = string2str(a);
  297. s2 = string2str(b);
  298. dst1 = buf1;
  299. for( ; *s1 && *s2 && *s1 == *s2 ;s1++, s2++)
  300. *dst1++ = *s1;
  301. *dst1 = '\0';
  302. if(*s1 != '\0' || *s2 != '\0') {
  303. *dst1++ = '[';
  304. *dst1++ = 'x';
  305. *dst1++ = ']';
  306. s1 = &(string2str(a))[alen - 1];
  307. s2 = &(string2str(b))[blen - 1];
  308. char *dst2 = &buf2[length];
  309. *dst2 = '\0';
  310. for (; *s1 && *s2 && *s1 == *s2; s1--, s2--)
  311. *(--dst2) = *s1;
  312. strcpy(dst1, dst2);
  313. }
  314. return string_strdupz(buf1);
  315. }
  316. // ----------------------------------------------------------------------------
  317. // STRING unit test
  318. struct thread_unittest {
  319. int join;
  320. int dups;
  321. };
  322. static void *string_thread(void *arg) {
  323. struct thread_unittest *tu = arg;
  324. for(; 1 ;) {
  325. if(__atomic_load_n(&tu->join, __ATOMIC_RELAXED))
  326. break;
  327. STRING *s = string_strdupz("string thread checking 1234567890");
  328. for(int i = 0; i < tu->dups ; i++)
  329. string_dup(s);
  330. for(int i = 0; i < tu->dups ; i++)
  331. string_freez(s);
  332. string_freez(s);
  333. }
  334. return arg;
  335. }
  336. static char **string_unittest_generate_names(size_t entries) {
  337. char **names = mallocz(sizeof(char *) * entries);
  338. for(size_t i = 0; i < entries ;i++) {
  339. char buf[25 + 1] = "";
  340. snprintfz(buf, sizeof(buf) - 1, "name.%zu.0123456789.%zu \t !@#$%%^&*(),./[]{}\\|~`", i, entries / 2 + i);
  341. names[i] = strdupz(buf);
  342. }
  343. return names;
  344. }
  345. static void string_unittest_free_char_pp(char **pp, size_t entries) {
  346. for(size_t i = 0; i < entries ;i++)
  347. freez(pp[i]);
  348. freez(pp);
  349. }
  350. static long unittest_string_entries(void) {
  351. long entries = 0;
  352. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  353. entries += string_base[p].entries;
  354. return entries;
  355. }
  356. #ifdef NETDATA_INTERNAL_CHECKS
  357. static size_t unittest_string_found_deleted_on_search(void) {
  358. size_t entries = 0;
  359. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  360. entries += string_base[p].found_deleted_on_search;
  361. return entries;
  362. }
  363. static size_t unittest_string_found_available_on_search(void) {
  364. size_t entries = 0;
  365. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  366. entries += string_base[p].found_available_on_search;
  367. return entries;
  368. }
  369. static size_t unittest_string_found_deleted_on_insert(void) {
  370. size_t entries = 0;
  371. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  372. entries += string_base[p].found_deleted_on_insert;
  373. return entries;
  374. }
  375. static size_t unittest_string_found_available_on_insert(void) {
  376. size_t entries = 0;
  377. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  378. entries += string_base[p].found_available_on_insert;
  379. return entries;
  380. }
  381. static size_t unittest_string_spins(void) {
  382. size_t entries = 0;
  383. for(size_t p = 0; p < STRING_PARTITIONS ;p++)
  384. entries += string_base[p].spins;
  385. return entries;
  386. }
  387. #endif // NETDATA_INTERNAL_CHECKS
  388. int string_unittest(size_t entries) {
  389. size_t errors = 0;
  390. fprintf(stderr, "Generating %zu names and values...\n", entries);
  391. char **names = string_unittest_generate_names(entries);
  392. // check string
  393. {
  394. long entries_starting = unittest_string_entries();
  395. fprintf(stderr, "\nChecking strings...\n");
  396. STRING *s1 = string_strdupz("hello unittest");
  397. STRING *s2 = string_strdupz("hello unittest");
  398. if(s1 != s2) {
  399. errors++;
  400. fprintf(stderr, "ERROR: duplicating strings are not deduplicated\n");
  401. }
  402. else
  403. fprintf(stderr, "OK: duplicating string are deduplicated\n");
  404. STRING *s3 = string_dup(s1);
  405. if(s3 != s1) {
  406. errors++;
  407. fprintf(stderr, "ERROR: cloning strings are not deduplicated\n");
  408. }
  409. else
  410. fprintf(stderr, "OK: cloning string are deduplicated\n");
  411. if(s1->refcount != 3) {
  412. errors++;
  413. fprintf(stderr, "ERROR: string refcount is not 3\n");
  414. }
  415. else
  416. fprintf(stderr, "OK: string refcount is 3\n");
  417. STRING *s4 = string_strdupz("world unittest");
  418. if(s4 == s1) {
  419. errors++;
  420. fprintf(stderr, "ERROR: string is sharing pointers on different strings\n");
  421. }
  422. else
  423. fprintf(stderr, "OK: string is properly handling different strings\n");
  424. usec_t start_ut, end_ut;
  425. STRING **strings = mallocz(entries * sizeof(STRING *));
  426. start_ut = now_realtime_usec();
  427. for(size_t i = 0; i < entries ;i++) {
  428. strings[i] = string_strdupz(names[i]);
  429. }
  430. end_ut = now_realtime_usec();
  431. fprintf(stderr, "Created %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  432. start_ut = now_realtime_usec();
  433. for(size_t i = 0; i < entries ;i++) {
  434. strings[i] = string_dup(strings[i]);
  435. }
  436. end_ut = now_realtime_usec();
  437. fprintf(stderr, "Cloned %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  438. start_ut = now_realtime_usec();
  439. for(size_t i = 0; i < entries ;i++) {
  440. strings[i] = string_strdupz(string2str(strings[i]));
  441. }
  442. end_ut = now_realtime_usec();
  443. fprintf(stderr, "Found %zu existing strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  444. start_ut = now_realtime_usec();
  445. for(size_t i = 0; i < entries ;i++) {
  446. string_freez(strings[i]);
  447. }
  448. end_ut = now_realtime_usec();
  449. fprintf(stderr, "Released %zu referenced strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  450. start_ut = now_realtime_usec();
  451. for(size_t i = 0; i < entries ;i++) {
  452. string_freez(strings[i]);
  453. }
  454. end_ut = now_realtime_usec();
  455. fprintf(stderr, "Released (again) %zu referenced strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  456. start_ut = now_realtime_usec();
  457. for(size_t i = 0; i < entries ;i++) {
  458. string_freez(strings[i]);
  459. }
  460. end_ut = now_realtime_usec();
  461. fprintf(stderr, "Freed %zu strings in %"PRIu64" usecs\n", entries, end_ut - start_ut);
  462. freez(strings);
  463. if(unittest_string_entries() != entries_starting + 2) {
  464. errors++;
  465. fprintf(stderr, "ERROR: strings dictionary should have %ld items but it has %ld\n",
  466. entries_starting + 2, unittest_string_entries());
  467. }
  468. else
  469. fprintf(stderr, "OK: strings dictionary has 2 items\n");
  470. }
  471. // check 2-way merge
  472. {
  473. struct testcase {
  474. char *src1; char *src2; char *expected;
  475. } tests[] = {
  476. { "", "", ""},
  477. { "a", "", "[x]"},
  478. { "", "a", "[x]"},
  479. { "a", "a", "a"},
  480. { "abcd", "abcd", "abcd"},
  481. { "foo_cs", "bar_cs", "[x]_cs"},
  482. { "cp_UNIQUE_INFIX_cs", "cp_unique_infix_cs", "cp_[x]_cs"},
  483. { "cp_UNIQUE_INFIX_ci_unique_infix_cs", "cp_unique_infix_ci_UNIQUE_INFIX_cs", "cp_[x]_cs"},
  484. { "foo[1234]", "foo[4321]", "foo[[x]]"},
  485. { NULL, NULL, NULL },
  486. };
  487. for (struct testcase *tc = &tests[0]; tc->expected != NULL; tc++) {
  488. STRING *src1 = string_strdupz(tc->src1);
  489. STRING *src2 = string_strdupz(tc->src2);
  490. STRING *expected = string_strdupz(tc->expected);
  491. STRING *result = string_2way_merge(src1, src2);
  492. if (string_cmp(result, expected) != 0) {
  493. fprintf(stderr, "string_2way_merge(\"%s\", \"%s\") -> \"%s\" (expected=\"%s\")\n",
  494. string2str(src1),
  495. string2str(src2),
  496. string2str(result),
  497. string2str(expected));
  498. errors++;
  499. }
  500. string_freez(src1);
  501. string_freez(src2);
  502. string_freez(expected);
  503. string_freez(result);
  504. }
  505. }
  506. // threads testing of string
  507. {
  508. struct thread_unittest tu = {
  509. .dups = 1,
  510. .join = 0,
  511. };
  512. #ifdef NETDATA_INTERNAL_CHECKS
  513. size_t ofound_deleted_on_search = unittest_string_found_deleted_on_search(),
  514. ofound_available_on_search = unittest_string_found_available_on_search(),
  515. ofound_deleted_on_insert = unittest_string_found_deleted_on_insert(),
  516. ofound_available_on_insert = unittest_string_found_available_on_insert(),
  517. ospins = unittest_string_spins();
  518. #endif
  519. size_t oinserts, odeletes, osearches, oentries, oreferences, omemory, oduplications, oreleases;
  520. string_statistics(&oinserts, &odeletes, &osearches, &oentries, &oreferences, &omemory, &oduplications, &oreleases);
  521. time_t seconds_to_run = 5;
  522. int threads_to_create = 2;
  523. fprintf(
  524. stderr,
  525. "Checking string concurrency with %d threads for %lld seconds...\n",
  526. threads_to_create,
  527. (long long)seconds_to_run);
  528. // check string concurrency
  529. netdata_thread_t threads[threads_to_create];
  530. tu.join = 0;
  531. for (int i = 0; i < threads_to_create; i++) {
  532. char buf[100 + 1];
  533. snprintf(buf, 100, "string%d", i);
  534. netdata_thread_create(
  535. &threads[i], buf, NETDATA_THREAD_OPTION_DONT_LOG | NETDATA_THREAD_OPTION_JOINABLE, string_thread, &tu);
  536. }
  537. sleep_usec(seconds_to_run * USEC_PER_SEC);
  538. __atomic_store_n(&tu.join, 1, __ATOMIC_RELAXED);
  539. for (int i = 0; i < threads_to_create; i++) {
  540. void *retval;
  541. netdata_thread_join(threads[i], &retval);
  542. }
  543. size_t inserts, deletes, searches, sentries, references, memory, duplications, releases;
  544. string_statistics(&inserts, &deletes, &searches, &sentries, &references, &memory, &duplications, &releases);
  545. fprintf(stderr, "inserts %zu, deletes %zu, searches %zu, entries %zu, references %zu, memory %zu, duplications %zu, releases %zu\n",
  546. inserts - oinserts, deletes - odeletes, searches - osearches, sentries - oentries, references - oreferences, memory - omemory, duplications - oduplications, releases - oreleases);
  547. #ifdef NETDATA_INTERNAL_CHECKS
  548. size_t found_deleted_on_search = unittest_string_found_deleted_on_search(),
  549. found_available_on_search = unittest_string_found_available_on_search(),
  550. found_deleted_on_insert = unittest_string_found_deleted_on_insert(),
  551. found_available_on_insert = unittest_string_found_available_on_insert(),
  552. spins = unittest_string_spins();
  553. fprintf(stderr, "on insert: %zu ok + %zu deleted\non search: %zu ok + %zu deleted\nspins: %zu\n",
  554. found_available_on_insert - ofound_available_on_insert,
  555. found_deleted_on_insert - ofound_deleted_on_insert,
  556. found_available_on_search - ofound_available_on_search,
  557. found_deleted_on_search - ofound_deleted_on_search,
  558. spins - ospins
  559. );
  560. #endif
  561. }
  562. string_unittest_free_char_pp(names, entries);
  563. fprintf(stderr, "\n%zu errors found\n", errors);
  564. return errors ? 1 : 0;
  565. }