test_astobj2_thrash.c 8.7 KB

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