test_hashtab_thrash.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, David M. Lee, II
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*
  19. *! \file \brief Thrash a hash table, for fun and profit.
  20. *
  21. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  22. *
  23. * Inspired by the original hashtest.c by Steve Murphy <murf@digium.com>. This test runs
  24. * several threads manipulating a concurrent hastab to see if they maintain
  25. * consistency. While the tests attempt to check consistency and error normally, threading
  26. * errors often result in segfaults.
  27. * \ingroup tests
  28. */
  29. /*** MODULEINFO
  30. <depend>TEST_FRAMEWORK</depend>
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. #include <pthread.h>
  35. #include "asterisk/hashtab.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/test.h"
  39. #include "asterisk/time.h"
  40. #include "asterisk/utils.h"
  41. #define MAX_HASH_ENTRIES 30000
  42. #define MAX_TEST_SECONDS 60
  43. struct hash_test {
  44. /*! Unit under test */
  45. struct ast_hashtab *to_be_thrashed;
  46. /*! Number of entries to insert in the grow thread. */
  47. int max_grow;
  48. /*! Number of entries added by the grow thread. */
  49. int grow_count;
  50. /*! Entries preloaded into the hashtab; to be deleted by the shrink thread */
  51. int preload;
  52. /*! When to give up on the tests */
  53. struct timeval deadline;
  54. /*! The actual test object */
  55. struct ast_test *test;
  56. };
  57. static int is_timed_out(struct hash_test const *data) {
  58. struct timeval now = ast_tvnow();
  59. int val = ast_tvdiff_us(data->deadline, now) < 0;
  60. if (val) {
  61. /* tv_usec is suseconds_t, which could be int or long */
  62. ast_test_status_update(data->test, "Now: %ld.%06ld Deadline: %ld.%06ld\n",
  63. now.tv_sec, (long)now.tv_usec,
  64. data->deadline.tv_sec, (long)data->deadline.tv_usec);
  65. }
  66. return val;
  67. }
  68. /*! \brief Create test element */
  69. static char *ht_new(int i)
  70. {
  71. const int buflen = 12;
  72. char *keybuf = ast_malloc(buflen);
  73. int needed;
  74. if (keybuf == NULL) {
  75. return NULL;
  76. }
  77. needed = snprintf(keybuf, buflen, "key%08x", (unsigned)i);
  78. ast_assert(needed + 1 <= buflen);
  79. return keybuf;
  80. }
  81. /*! \brief Free test element */
  82. static void ht_delete(void *obj)
  83. {
  84. ast_free(obj);
  85. }
  86. /*! \brief Grow the hash data as specified */
  87. static void *hash_test_grow(void *d)
  88. {
  89. struct hash_test *data = d;
  90. int i;
  91. for (i = 0; i < data->max_grow; ++i) {
  92. char *obj;
  93. if (is_timed_out(data)) {
  94. return "Growth timed out";
  95. }
  96. obj = ht_new(i);
  97. if (obj == NULL) {
  98. return "Allocation failed";
  99. }
  100. ast_hashtab_insert_immediate(data->to_be_thrashed, obj);
  101. ast_atomic_fetchadd_int(&data->grow_count, 1);
  102. }
  103. return NULL;
  104. }
  105. /*! Randomly lookup data in the hash */
  106. static void *hash_test_lookup(void *d)
  107. {
  108. struct hash_test *data = d;
  109. int max;
  110. unsigned seed = time(NULL);
  111. /* ast_atomic_fetchadd_int provide a memory fence so that the optimizer doesn't
  112. * optimize away reads.
  113. */
  114. while ((max = ast_atomic_fetchadd_int(&data->grow_count, 0)) < data->max_grow) {
  115. int i;
  116. char *obj;
  117. int is_in_hashtab;
  118. if (is_timed_out(data)) {
  119. return "Lookup timed out";
  120. }
  121. if (max == 0) {
  122. /* No data yet; yield and try again */
  123. sched_yield();
  124. continue;
  125. }
  126. /* Randomly lookup one object from the hash */
  127. i = rand_r(&seed) % max;
  128. obj = ht_new(i);
  129. if (obj == NULL) {
  130. return "Allocation failed.";
  131. }
  132. is_in_hashtab = (ast_hashtab_lookup(data->to_be_thrashed, obj) != NULL);
  133. ht_delete(obj);
  134. if (!is_in_hashtab) {
  135. return "key unexpectedly missing";
  136. }
  137. }
  138. return NULL;
  139. }
  140. /*! Delete entries from the hash */
  141. static void *hash_test_shrink(void *d)
  142. {
  143. const struct hash_test *data = d;
  144. int i;
  145. for (i = 1; i < data->preload; ++i) {
  146. char *obj = ht_new(-i);
  147. char *from_hashtab;
  148. int deleted;
  149. if (obj == NULL) {
  150. return "Allocation failed";
  151. }
  152. from_hashtab = ast_hashtab_remove_object_via_lookup(data->to_be_thrashed, obj);
  153. deleted = from_hashtab != NULL;
  154. ht_delete(obj);
  155. ht_delete(from_hashtab);
  156. if (!deleted) {
  157. return "could not delete object";
  158. }
  159. if (is_timed_out(data)) {
  160. return "Shrink timed out";
  161. }
  162. }
  163. return NULL;
  164. }
  165. /*! Continuously iterate through all the entries in the hash */
  166. static void *hash_test_count(void *d)
  167. {
  168. const struct hash_test *data = d;
  169. int count = 0;
  170. int last_count = 0;
  171. while (count < data->max_grow) {
  172. struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(data->to_be_thrashed);
  173. char *ht = ast_hashtab_next(it);
  174. last_count = count;
  175. count = 0;
  176. while (ht) {
  177. /* only count keys added by grow thread */
  178. if (strncmp(ht, "key0", 4) == 0) {
  179. ++count;
  180. }
  181. ht = ast_hashtab_next(it);
  182. }
  183. ast_hashtab_end_traversal(it);
  184. if (last_count == count) {
  185. /* Give other threads ample chance to run, note that using sched_yield here does not
  186. * provide enough of a chance and can cause this thread to starve others.
  187. */
  188. usleep(1);
  189. } else if (last_count > count) {
  190. /* Make sure the hashtable never shrinks */
  191. return "hashtab unexpectedly shrank";
  192. }
  193. if (is_timed_out(data)) {
  194. return "Count timed out";
  195. }
  196. }
  197. /* Successfully iterated over all of the expected elements */
  198. return NULL;
  199. }
  200. AST_TEST_DEFINE(hash_test)
  201. {
  202. enum ast_test_result_state res = AST_TEST_PASS;
  203. struct hash_test data = {};
  204. pthread_t grow_thread, count_thread, lookup_thread, shrink_thread;
  205. void *thread_results;
  206. int i;
  207. switch (cmd) {
  208. case TEST_INIT:
  209. info->name = "thrash";
  210. info->category = "/main/hashtab/";
  211. info->summary = "Testing hashtab concurrency";
  212. info->description = "Test hashtab concurrency correctness.";
  213. return AST_TEST_NOT_RUN;
  214. case TEST_EXECUTE:
  215. break;
  216. }
  217. ast_test_status_update(test, "Executing hash concurrency test...\n");
  218. data.test = test;
  219. data.preload = MAX_HASH_ENTRIES / 2;
  220. data.max_grow = MAX_HASH_ENTRIES - data.preload;
  221. data.deadline = ast_tvadd(ast_tvnow(), ast_tv(MAX_TEST_SECONDS, 0));
  222. data.to_be_thrashed = ast_hashtab_create(MAX_HASH_ENTRIES / 100,
  223. ast_hashtab_compare_strings_nocase, ast_hashtab_resize_java,
  224. ast_hashtab_newsize_java, ast_hashtab_hash_string_nocase, 1);
  225. if (data.to_be_thrashed == NULL) {
  226. ast_test_status_update(test, "Allocation failed\n");
  227. /* Nothing needs to be freed; early return is fine */
  228. return AST_TEST_FAIL;
  229. }
  230. /* preload with data to delete */
  231. for (i = 1; i < data.preload; ++i) {
  232. char *obj = ht_new(-i);
  233. if (obj == NULL) {
  234. ast_test_status_update(test, "Allocation failed\n");
  235. ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
  236. return AST_TEST_FAIL;
  237. }
  238. ast_hashtab_insert_immediate(data.to_be_thrashed, obj);
  239. }
  240. /* add data.max_grow entries to the hashtab */
  241. ast_pthread_create(&grow_thread, NULL, hash_test_grow, &data);
  242. /* continually count the keys added by the grow thread */
  243. ast_pthread_create(&count_thread, NULL, hash_test_count, &data);
  244. /* continually lookup keys added by the grow thread */
  245. ast_pthread_create(&lookup_thread, NULL, hash_test_lookup, &data);
  246. /* delete all keys preloaded into the hashtab */
  247. ast_pthread_create(&shrink_thread, NULL, hash_test_shrink, &data);
  248. pthread_join(grow_thread, &thread_results);
  249. if (thread_results != NULL) {
  250. ast_test_status_update(test, "Growth thread failed: %s\n",
  251. (char *)thread_results);
  252. res = AST_TEST_FAIL;
  253. }
  254. pthread_join(count_thread, &thread_results);
  255. if (thread_results != NULL) {
  256. ast_test_status_update(test, "Count thread failed: %s\n",
  257. (char *)thread_results);
  258. res = AST_TEST_FAIL;
  259. }
  260. pthread_join(lookup_thread, &thread_results);
  261. if (thread_results != NULL) {
  262. ast_test_status_update(test, "Lookup thread failed: %s\n",
  263. (char *)thread_results);
  264. res = AST_TEST_FAIL;
  265. }
  266. pthread_join(shrink_thread, &thread_results);
  267. if (thread_results != NULL) {
  268. ast_test_status_update(test, "Shrink thread failed: %s\n",
  269. (char *)thread_results);
  270. res = AST_TEST_FAIL;
  271. }
  272. if (ast_hashtab_size(data.to_be_thrashed) != data.max_grow) {
  273. ast_test_status_update(test,
  274. "Invalid hashtab size. Expected: %d, Actual: %d\n",
  275. data.max_grow, ast_hashtab_size(data.to_be_thrashed));
  276. res = AST_TEST_FAIL;
  277. }
  278. ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
  279. return res;
  280. }
  281. static int unload_module(void)
  282. {
  283. AST_TEST_UNREGISTER(hash_test);
  284. return 0;
  285. }
  286. static int load_module(void)
  287. {
  288. AST_TEST_REGISTER(hash_test);
  289. return AST_MODULE_LOAD_SUCCESS;
  290. }
  291. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hash test");