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