test_hashtab_thrash.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 manipulatings 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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include <pthread.h>
  36. #include "asterisk/hashtab.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/test.h"
  40. #include "asterisk/time.h"
  41. #include "asterisk/utils.h"
  42. #define MAX_HASH_ENTRIES 30000
  43. #define MAX_TEST_SECONDS 60
  44. struct hash_test {
  45. /*! Unit under test */
  46. struct ast_hashtab *to_be_thrashed;
  47. /*! Number of entries to insert in the grow thread. */
  48. int max_grow;
  49. /*! Number of enteries added by the grow thread. */
  50. int grow_count;
  51. /*! Entries preloaded into the hashtab; to be deleted by the shrink thread */
  52. int preload;
  53. /*! When to give up on the tests */
  54. struct timeval deadline;
  55. /*! The actual test object */
  56. struct ast_test *test;
  57. };
  58. static int is_timed_out(struct hash_test const *data) {
  59. struct timeval now = ast_tvnow();
  60. int val = ast_tvdiff_us(data->deadline, now) < 0;
  61. if (val) {
  62. /* tv_usec is suseconds_t, which could be int or long */
  63. ast_test_status_update(data->test, "Now: %ld.%06ld Deadline: %ld.%06ld\n",
  64. now.tv_sec, (long)now.tv_usec,
  65. data->deadline.tv_sec, (long)data->deadline.tv_usec);
  66. }
  67. return val;
  68. }
  69. /*! /brief Create test element */
  70. static char *ht_new(int i)
  71. {
  72. const int buflen = 12;
  73. char *keybuf = ast_malloc(buflen);
  74. int needed;
  75. if (keybuf == NULL) {
  76. return NULL;
  77. }
  78. needed = snprintf(keybuf, buflen, "key%08x", (unsigned)i);
  79. ast_assert(needed + 1 <= buflen);
  80. return keybuf;
  81. }
  82. /*! /brief Free test element */
  83. static void ht_delete(void *obj)
  84. {
  85. ast_free(obj);
  86. }
  87. /*! /brief Grow the hash data as specified */
  88. static void *hash_test_grow(void *d)
  89. {
  90. struct hash_test *data = d;
  91. int i;
  92. for (i = 0; i < data->max_grow; ++i) {
  93. char *obj;
  94. if (is_timed_out(data)) {
  95. return "Growth timed out";
  96. }
  97. obj = ht_new(i);
  98. if (obj == NULL) {
  99. return "Allocation failed";
  100. }
  101. ast_hashtab_insert_immediate(data->to_be_thrashed, obj);
  102. ast_atomic_fetchadd_int(&data->grow_count, 1);
  103. }
  104. return NULL;
  105. }
  106. /*! Randomly lookup data in the hash */
  107. static void *hash_test_lookup(void *d)
  108. {
  109. struct hash_test *data = d;
  110. int max;
  111. unsigned seed = time(NULL);
  112. /* ast_atomic_fetchadd_int provide a memory fence so that the optimizer doesn't
  113. * optimize away reads.
  114. */
  115. while ((max = ast_atomic_fetchadd_int(&data->grow_count, 0)) < data->max_grow) {
  116. int i;
  117. char *obj;
  118. int is_in_hashtab;
  119. if (is_timed_out(data)) {
  120. return "Lookup timed out";
  121. }
  122. if (max == 0) {
  123. /* No data yet; yield and try again */
  124. sched_yield();
  125. continue;
  126. }
  127. /* Randomly lookup one object from the hash */
  128. i = rand_r(&seed) % max;
  129. obj = ht_new(i);
  130. if (obj == NULL) {
  131. return "Allocation failed.";
  132. }
  133. is_in_hashtab = (ast_hashtab_lookup(data->to_be_thrashed, obj) != NULL);
  134. ht_delete(obj);
  135. if (!is_in_hashtab) {
  136. return "key unexpectedly missing";
  137. }
  138. }
  139. return NULL;
  140. }
  141. /*! Delete entries from the hash */
  142. static void *hash_test_shrink(void *d)
  143. {
  144. const struct hash_test *data = d;
  145. int i;
  146. for (i = 1; i < data->preload; ++i) {
  147. char *obj = ht_new(-i);
  148. char *from_hashtab;
  149. int deleted;
  150. if (obj == NULL) {
  151. return "Allocation failed";
  152. }
  153. from_hashtab = ast_hashtab_remove_object_via_lookup(data->to_be_thrashed, obj);
  154. deleted = from_hashtab != NULL;
  155. ht_delete(obj);
  156. ht_delete(from_hashtab);
  157. if (!deleted) {
  158. return "could not delete object";
  159. }
  160. if (is_timed_out(data)) {
  161. return "Shrink timed out";
  162. }
  163. }
  164. return NULL;
  165. }
  166. /*! Continuously iterate through all the entries in the hash */
  167. static void *hash_test_count(void *d)
  168. {
  169. const struct hash_test *data = d;
  170. int count = 0;
  171. int last_count = 0;
  172. while (count < data->max_grow) {
  173. struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(data->to_be_thrashed);
  174. char *ht = ast_hashtab_next(it);
  175. last_count = count;
  176. count = 0;
  177. while (ht) {
  178. /* only count keys added by grow thread */
  179. if (strncmp(ht, "key0", 4) == 0) {
  180. ++count;
  181. }
  182. ht = ast_hashtab_next(it);
  183. }
  184. ast_hashtab_end_traversal(it);
  185. if (last_count == count) {
  186. /* Give other threads ample chance to run, note that using sched_yield here does not
  187. * provide enough of a chance and can cause this thread to starve others.
  188. */
  189. usleep(1);
  190. } else if (last_count > count) {
  191. /* Make sure the hashtable never shrinks */
  192. return "hashtab unexpectedly shrank";
  193. }
  194. if (is_timed_out(data)) {
  195. return "Count timed out";
  196. }
  197. }
  198. /* Successfully iterated over all of the expected elements */
  199. return NULL;
  200. }
  201. AST_TEST_DEFINE(hash_test)
  202. {
  203. enum ast_test_result_state res = AST_TEST_PASS;
  204. struct hash_test data = {};
  205. pthread_t grow_thread, count_thread, lookup_thread, shrink_thread;
  206. void *thread_results;
  207. int i;
  208. switch (cmd) {
  209. case TEST_INIT:
  210. info->name = "thrash";
  211. info->category = "/main/hashtab/";
  212. info->summary = "Testing hashtab concurrency";
  213. info->description = "Test hashtab concurrency correctness.";
  214. return AST_TEST_NOT_RUN;
  215. case TEST_EXECUTE:
  216. break;
  217. }
  218. ast_test_status_update(test, "Executing hash concurrency test...\n");
  219. data.test = test;
  220. data.preload = MAX_HASH_ENTRIES / 2;
  221. data.max_grow = MAX_HASH_ENTRIES - data.preload;
  222. data.deadline = ast_tvadd(ast_tvnow(), ast_tv(MAX_TEST_SECONDS, 0));
  223. data.to_be_thrashed = ast_hashtab_create(MAX_HASH_ENTRIES / 100,
  224. ast_hashtab_compare_strings_nocase, ast_hashtab_resize_java,
  225. ast_hashtab_newsize_java, ast_hashtab_hash_string_nocase, 1);
  226. if (data.to_be_thrashed == NULL) {
  227. ast_test_status_update(test, "Allocation failed\n");
  228. /* Nothing needs to be freed; early return is fine */
  229. return AST_TEST_FAIL;
  230. }
  231. /* preload with data to delete */
  232. for (i = 1; i < data.preload; ++i) {
  233. char *obj = ht_new(-i);
  234. if (obj == NULL) {
  235. ast_test_status_update(test, "Allocation failed\n");
  236. ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
  237. return AST_TEST_FAIL;
  238. }
  239. ast_hashtab_insert_immediate(data.to_be_thrashed, obj);
  240. }
  241. /* add data.max_grow entries to the hashtab */
  242. ast_pthread_create(&grow_thread, NULL, hash_test_grow, &data);
  243. /* continually count the keys added by the grow thread */
  244. ast_pthread_create(&count_thread, NULL, hash_test_count, &data);
  245. /* continually lookup keys added by the grow thread */
  246. ast_pthread_create(&lookup_thread, NULL, hash_test_lookup, &data);
  247. /* delete all keys preloaded into the hashtab */
  248. ast_pthread_create(&shrink_thread, NULL, hash_test_shrink, &data);
  249. pthread_join(grow_thread, &thread_results);
  250. if (thread_results != NULL) {
  251. ast_test_status_update(test, "Growth thread failed: %s\n",
  252. (char *)thread_results);
  253. res = AST_TEST_FAIL;
  254. }
  255. pthread_join(count_thread, &thread_results);
  256. if (thread_results != NULL) {
  257. ast_test_status_update(test, "Count thread failed: %s\n",
  258. (char *)thread_results);
  259. res = AST_TEST_FAIL;
  260. }
  261. pthread_join(lookup_thread, &thread_results);
  262. if (thread_results != NULL) {
  263. ast_test_status_update(test, "Lookup thread failed: %s\n",
  264. (char *)thread_results);
  265. res = AST_TEST_FAIL;
  266. }
  267. pthread_join(shrink_thread, &thread_results);
  268. if (thread_results != NULL) {
  269. ast_test_status_update(test, "Shrink thread failed: %s\n",
  270. (char *)thread_results);
  271. res = AST_TEST_FAIL;
  272. }
  273. if (ast_hashtab_size(data.to_be_thrashed) != data.max_grow) {
  274. ast_test_status_update(test,
  275. "Invalid hashtab size. Expected: %d, Actual: %d\n",
  276. data.max_grow, ast_hashtab_size(data.to_be_thrashed));
  277. res = AST_TEST_FAIL;
  278. }
  279. ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
  280. return res;
  281. }
  282. static int unload_module(void)
  283. {
  284. AST_TEST_UNREGISTER(hash_test);
  285. return 0;
  286. }
  287. static int load_module(void)
  288. {
  289. AST_TEST_REGISTER(hash_test);
  290. return AST_MODULE_LOAD_SUCCESS;
  291. }
  292. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hash test");