test_astobj2_thrash.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 astobj2 container, for fun and profit.
  20. *
  21. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  22. *
  23. * Inspired by the original hashtest2.c by Steve Murphy <murf@digium.com>. This test runs
  24. * several threads manipulating a concurrent astobj2 container 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/astobj2.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 15000
  43. /*
  44. * Use one of the online calculators to find the first prime number
  45. * greater than MAX_HASH_ENTRIES / 100.
  46. */
  47. #define HASH_BUCKETS 151
  48. #define COUNT_SLEEP_US 500
  49. #define MAX_TEST_SECONDS 60
  50. struct hash_test {
  51. /*! Unit under test */
  52. struct ao2_container *to_be_thrashed;
  53. /*! Number of entries to insert in the grow thread. */
  54. int max_grow;
  55. /*! Number of entries added by the grow thread. */
  56. int grow_count;
  57. /*! Entries preloaded into the hashtab; to be deleted by the shrink thread */
  58. int preload;
  59. /*! When to give up on the tests */
  60. struct timeval deadline;
  61. };
  62. static int alloc_count = 0;
  63. static int is_timed_out(struct hash_test const *data) {
  64. return ast_tvdiff_us(data->deadline, ast_tvnow()) < 0;
  65. }
  66. /*! \brief Free test element */
  67. static void ht_delete(void *obj)
  68. {
  69. ast_atomic_fetchadd_int(&alloc_count, -1);
  70. }
  71. /*! \brief Create test element */
  72. static char *ht_new(int i)
  73. {
  74. const int buflen = 12;
  75. char *keybuf = ao2_alloc(buflen, ht_delete);
  76. int needed;
  77. if (keybuf == NULL) {
  78. return NULL;
  79. }
  80. needed = snprintf(keybuf, buflen, "key%08x", (unsigned)i);
  81. ast_atomic_fetchadd_int(&alloc_count, 1);
  82. ast_assert(needed + 1 <= buflen);
  83. return keybuf;
  84. }
  85. /*! \brief Grow the hash data as specified */
  86. static void *hash_test_grow(void *d)
  87. {
  88. struct hash_test *data = d;
  89. int i;
  90. for (i = 0; i < data->max_grow; ++i) {
  91. char *ht;
  92. if (is_timed_out(data)) {
  93. printf("Growth timed out at %d\n", i);
  94. return "Growth timed out";
  95. }
  96. ht = ht_new(i);
  97. if (ht == NULL) {
  98. return "Allocation failed";
  99. }
  100. ao2_link(data->to_be_thrashed, ht);
  101. ao2_ref(ht, -1);
  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. char *from_ao2;
  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. from_ao2 = ao2_find(data->to_be_thrashed, obj, OBJ_POINTER);
  134. ao2_ref(obj, -1);
  135. ao2_ref(from_ao2, -1);
  136. if (from_ao2 == NULL) {
  137. return "Key unexpectedly missing";
  138. }
  139. }
  140. return NULL;
  141. }
  142. /*! Delete entries from the hash */
  143. static void *hash_test_shrink(void *d)
  144. {
  145. const struct hash_test *data = d;
  146. int i;
  147. for (i = 1; i < data->preload; ++i) {
  148. char *obj = ht_new(-i);
  149. char *from_ao2;
  150. if (obj == NULL) {
  151. return "Allocation failed";
  152. }
  153. from_ao2 = ao2_find(data->to_be_thrashed, obj, OBJ_UNLINK | OBJ_POINTER);
  154. ao2_ref(obj, -1);
  155. if (from_ao2) {
  156. ao2_ref(from_ao2, -1);
  157. } else {
  158. return "Could not find object to delete";
  159. }
  160. if (is_timed_out(data)) {
  161. return "Shrink timed out";
  162. }
  163. }
  164. return NULL;
  165. }
  166. /*! ao2_callback for hash_test_count */
  167. static int increment_count(void *obj, void *arg, int flags) {
  168. char *ht = obj;
  169. int *count = arg;
  170. if (strncmp(ht, "key0", 4) == 0) {
  171. ++(*count);
  172. }
  173. return 0;
  174. }
  175. /*! Continuously iterate through all the entries in the hash */
  176. static void *hash_test_count(void *d)
  177. {
  178. const struct hash_test *data = d;
  179. int count = 0;
  180. int last_count = 0;
  181. while (count < data->max_grow) {
  182. last_count = count;
  183. count = 0;
  184. ao2_callback(data->to_be_thrashed, OBJ_MULTIPLE, increment_count, &count);
  185. if (last_count == count) {
  186. /* Allow other threads to run. */
  187. usleep(COUNT_SLEEP_US);
  188. } else if (last_count > count) {
  189. /* Make sure the ao2 container never shrinks */
  190. return "ao2 container unexpectedly shrank";
  191. }
  192. if (is_timed_out(data)) {
  193. return "Count timed out";
  194. }
  195. }
  196. /* Successfully iterated over all of the expected elements */
  197. return NULL;
  198. }
  199. static int hash_string(const void *obj, const int flags)
  200. {
  201. return ast_hashtab_hash_string_nocase(obj);
  202. }
  203. static int compare_strings(void *lhs, void *rhs, int flags)
  204. {
  205. const char *lhs_str = lhs;
  206. const char *rhs_str = rhs;
  207. if (strcasecmp(lhs_str, rhs_str) == 0) {
  208. return CMP_MATCH | CMP_STOP;
  209. } else {
  210. return 0;
  211. }
  212. }
  213. AST_TEST_DEFINE(hash_test)
  214. {
  215. enum ast_test_result_state res = AST_TEST_PASS;
  216. struct hash_test data = {};
  217. pthread_t grow_thread, count_thread, lookup_thread, shrink_thread;
  218. void *thread_results;
  219. int i;
  220. switch (cmd) {
  221. case TEST_INIT:
  222. info->name = "thrash";
  223. info->category = "/main/astobj2/";
  224. info->summary = "Testing astobj2 container concurrency";
  225. info->description = "Test astobj2 container concurrency correctness.";
  226. return AST_TEST_NOT_RUN;
  227. case TEST_EXECUTE:
  228. break;
  229. }
  230. ast_test_status_update(test, "Executing hash concurrency test...\n");
  231. data.preload = MAX_HASH_ENTRIES / 2;
  232. data.max_grow = MAX_HASH_ENTRIES - data.preload;
  233. data.deadline = ast_tvadd(ast_tvnow(), ast_tv(MAX_TEST_SECONDS, 0));
  234. data.to_be_thrashed = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  235. HASH_BUCKETS, hash_string, NULL, compare_strings);
  236. if (data.to_be_thrashed == NULL) {
  237. ast_test_status_update(test, "Allocation failed\n");
  238. /* Nothing needs to be freed; early return is fine */
  239. return AST_TEST_FAIL;
  240. }
  241. /* preload with data to delete */
  242. for (i = 1; i < data.preload; ++i) {
  243. char *ht = ht_new(-i);
  244. if (ht == NULL) {
  245. ast_test_status_update(test, "Allocation failed\n");
  246. ao2_ref(data.to_be_thrashed, -1);
  247. return AST_TEST_FAIL;
  248. }
  249. ao2_link(data.to_be_thrashed, ht);
  250. ao2_ref(ht, -1);
  251. }
  252. /* add data.max_grow entries to the ao2 container */
  253. ast_pthread_create(&grow_thread, NULL, hash_test_grow, &data);
  254. /* continually count the keys added by the grow thread */
  255. ast_pthread_create(&count_thread, NULL, hash_test_count, &data);
  256. /* continually lookup keys added by the grow thread */
  257. ast_pthread_create(&lookup_thread, NULL, hash_test_lookup, &data);
  258. /* delete all keys preloaded into the ao2 container */
  259. ast_pthread_create(&shrink_thread, NULL, hash_test_shrink, &data);
  260. pthread_join(grow_thread, &thread_results);
  261. if (thread_results != NULL) {
  262. ast_test_status_update(test, "Growth thread failed: %s\n",
  263. (char *)thread_results);
  264. res = AST_TEST_FAIL;
  265. }
  266. pthread_join(count_thread, &thread_results);
  267. if (thread_results != NULL) {
  268. ast_test_status_update(test, "Count thread failed: %s\n",
  269. (char *)thread_results);
  270. res = AST_TEST_FAIL;
  271. }
  272. pthread_join(lookup_thread, &thread_results);
  273. if (thread_results != NULL) {
  274. ast_test_status_update(test, "Lookup thread failed: %s\n",
  275. (char *)thread_results);
  276. res = AST_TEST_FAIL;
  277. }
  278. pthread_join(shrink_thread, &thread_results);
  279. if (thread_results != NULL) {
  280. ast_test_status_update(test, "Shrink thread failed: %s\n",
  281. (char *)thread_results);
  282. res = AST_TEST_FAIL;
  283. }
  284. if (ao2_container_count(data.to_be_thrashed) != data.max_grow) {
  285. ast_test_status_update(test,
  286. "Invalid ao2 container size. Expected: %d, Actual: %d\n",
  287. data.max_grow, ao2_container_count(data.to_be_thrashed));
  288. res = AST_TEST_FAIL;
  289. }
  290. ao2_ref(data.to_be_thrashed, -1);
  291. /* check for object leaks */
  292. if (ast_atomic_fetchadd_int(&alloc_count, 0) != 0) {
  293. ast_test_status_update(test, "Leaked %d objects!\n",
  294. ast_atomic_fetchadd_int(&alloc_count, 0));
  295. res = AST_TEST_FAIL;
  296. }
  297. return res;
  298. }
  299. static int unload_module(void)
  300. {
  301. AST_TEST_UNREGISTER(hash_test);
  302. return 0;
  303. }
  304. static int load_module(void)
  305. {
  306. AST_TEST_REGISTER(hash_test);
  307. return AST_MODULE_LOAD_SUCCESS;
  308. }
  309. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "astobj2 container thrash test");