2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012, David M. Lee, II
6 * David M. Lee, II <dlee@digium.com>
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.
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.
20 *! \file \brief Thrash a hash table, for fun and profit.
22 * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
24 * Inspired by the original hashtest.c by Steve Murphy <murf@digium.com>. This test runs
25 * several threads manipulatings a concurrent hastab to see if they maintain
26 * consistency. While the tests attempt to check consistency and error normally, threading
27 * errors often result in segfaults.
32 <depend>TEST_FRAMEWORK</depend>
33 <support_level>core</support_level>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/hashtab.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/module.h"
43 #include "asterisk/test.h"
44 #include "asterisk/time.h"
45 #include "asterisk/utils.h"
47 #define MAX_HASH_ENTRIES 30000
48 #define MAX_TEST_SECONDS 60
51 /*! Unit under test */
52 struct ast_hashtab *to_be_thrashed;
53 /*! Number of entries to insert in the grow thread. */
55 /*! Number of enteries added by the grow thread. */
57 /*! Entries preloaded into the hashtab; to be deleted by the shrink thread */
59 /*! When to give up on the tests */
60 struct timeval deadline;
61 /*! The actual test object */
62 struct ast_test *test;
65 static int is_timed_out(struct hash_test const *data) {
66 struct timeval now = ast_tvnow();
67 int val = ast_tvdiff_us(data->deadline, now) < 0;
69 /* tv_usec is suseconds_t, which could be int or long */
70 ast_test_status_update(data->test, "Now: %ld.%06ld Deadline: %ld.%06ld\n",
71 now.tv_sec, (long)now.tv_usec,
72 data->deadline.tv_sec, (long)data->deadline.tv_usec);
77 /*! /brief Create test element */
78 static char *ht_new(int i)
80 const int buflen = 12;
81 char *keybuf = ast_malloc(buflen);
86 needed = snprintf(keybuf, buflen, "key%08x", i);
87 ast_assert(needed + 1 <= buflen);
91 /*! /brief Free test element */
92 static void ht_delete(void *obj)
97 /*! /brief Grow the hash data as specified */
98 static void *hash_test_grow(void *d)
100 struct hash_test *data = d;
103 for (i = 0; i < data->max_grow; ++i) {
105 if (is_timed_out(data)) {
106 return "Growth timed out";
110 return "Allocation failed";
112 ast_hashtab_insert_immediate(data->to_be_thrashed, obj);
113 ast_atomic_fetchadd_int(&data->grow_count, 1);
118 /*! Randomly lookup data in the hash */
119 static void *hash_test_lookup(void *d)
121 struct hash_test *data = d;
123 unsigned seed = time(NULL);
125 /* ast_atomic_fetchadd_int provide a memory fence so that the optimizer doesn't
126 * optimize away reads.
128 while ((max = ast_atomic_fetchadd_int(&data->grow_count, 0)) < data->max_grow) {
133 if (is_timed_out(data)) {
134 return "Lookup timed out";
138 /* No data yet; yield and try again */
143 /* Randomly lookup one object from the hash */
144 i = rand_r(&seed) % max;
147 return "Allocation failed.";
149 is_in_hashtab = (ast_hashtab_lookup(data->to_be_thrashed, obj) != NULL);
151 if (!is_in_hashtab) {
152 return "key unexpectedly missing";
159 /*! Delete entries from the hash */
160 static void *hash_test_shrink(void *d)
162 const struct hash_test *data = d;
165 for (i = 1; i < data->preload; ++i) {
166 char *obj = ht_new(-i);
171 return "Allocation failed";
173 from_hashtab = ast_hashtab_remove_object_via_lookup(data->to_be_thrashed, obj);
174 deleted = from_hashtab != NULL;
177 ht_delete(from_hashtab);
179 return "could not delete object";
181 if (is_timed_out(data)) {
182 return "Shrink timed out";
188 /*! Continuously iterate through all the entries in the hash */
189 static void *hash_test_count(void *d)
191 const struct hash_test *data = d;
195 while (count < data->max_grow) {
196 struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(data->to_be_thrashed);
197 char *ht = ast_hashtab_next(it);
201 /* only count keys added by grow thread */
202 if (strncmp(ht, "key0", 4) == 0) {
205 ht = ast_hashtab_next(it);
207 ast_hashtab_end_traversal(it);
209 if (last_count == count) {
210 /* Allow other threads to run. */
212 } else if (last_count > count) {
213 /* Make sure the hashtable never shrinks */
214 return "hashtab unexpectedly shrank";
217 if (is_timed_out(data)) {
218 return "Count timed out";
222 /* Successfully iterated over all of the expected elements */
226 AST_TEST_DEFINE(hash_test)
228 enum ast_test_result_state res = AST_TEST_PASS;
229 struct hash_test data = {};
230 pthread_t grow_thread, count_thread, lookup_thread, shrink_thread;
231 void *thread_results;
236 info->name = "thrash";
237 info->category = "/main/hashtab/";
238 info->summary = "Testing hashtab concurrency";
239 info->description = "Test hashtab concurrency correctness.";
240 return AST_TEST_NOT_RUN;
245 ast_test_status_update(test, "Executing hash concurrency test...\n");
247 data.preload = MAX_HASH_ENTRIES / 2;
248 data.max_grow = MAX_HASH_ENTRIES - data.preload;
249 data.deadline = ast_tvadd(ast_tvnow(), ast_tv(MAX_TEST_SECONDS, 0));
250 data.to_be_thrashed = ast_hashtab_create(MAX_HASH_ENTRIES / 100,
251 ast_hashtab_compare_strings_nocase, ast_hashtab_resize_java,
252 ast_hashtab_newsize_java, ast_hashtab_hash_string_nocase, 1);
254 if (data.to_be_thrashed == NULL) {
255 ast_test_status_update(test, "Allocation failed\n");
256 /* Nothing needs to be freed; early return is fine */
257 return AST_TEST_FAIL;
261 /* preload with data to delete */
262 for (i = 1; i < data.preload; ++i) {
263 char *obj = ht_new(-i);
265 ast_test_status_update(test, "Allocation failed\n");
266 ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
267 return AST_TEST_FAIL;
269 ast_hashtab_insert_immediate(data.to_be_thrashed, obj);
272 /* add data.max_grow entries to the hashtab */
273 ast_pthread_create(&grow_thread, NULL, hash_test_grow, &data);
274 /* continually count the keys added by the grow thread */
275 ast_pthread_create(&count_thread, NULL, hash_test_count, &data);
276 /* continually lookup keys added by the grow thread */
277 ast_pthread_create(&lookup_thread, NULL, hash_test_lookup, &data);
278 /* delete all keys preloaded into the hashtab */
279 ast_pthread_create(&shrink_thread, NULL, hash_test_shrink, &data);
281 pthread_join(grow_thread, &thread_results);
282 if (thread_results != NULL) {
283 ast_test_status_update(test, "Growth thread failed: %s\n",
284 (char *)thread_results);
288 pthread_join(count_thread, &thread_results);
289 if (thread_results != NULL) {
290 ast_test_status_update(test, "Count thread failed: %s\n",
291 (char *)thread_results);
295 pthread_join(lookup_thread, &thread_results);
296 if (thread_results != NULL) {
297 ast_test_status_update(test, "Lookup thread failed: %s\n",
298 (char *)thread_results);
302 pthread_join(shrink_thread, &thread_results);
303 if (thread_results != NULL) {
304 ast_test_status_update(test, "Shrink thread failed: %s\n",
305 (char *)thread_results);
309 if (ast_hashtab_size(data.to_be_thrashed) != data.max_grow) {
310 ast_test_status_update(test,
311 "Invalid hashtab size. Expected: %d, Actual: %d\n",
312 data.max_grow, ast_hashtab_size(data.to_be_thrashed));
316 ast_hashtab_destroy(data.to_be_thrashed, ht_delete);
320 static int unload_module(void)
322 AST_TEST_UNREGISTER(hash_test);
326 static int load_module(void)
328 AST_TEST_REGISTER(hash_test);
329 return AST_MODULE_LOAD_SUCCESS;
332 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hash test");