2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Steve Murphy
6 * Steve Murphy <murf@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 * \brief A program to thoroughly thrash a hash table, testing
21 * out locking safety, and making sure all functionality
22 * is functioning. Run with 5 or more threads to get that
23 * fully intense firestorm of activity. If your
24 * hash tables don't crash, lock up, or go weird, it must
25 * be good code! Even features some global counters
26 * that will get slightly behind because they aren't lock-protected.
28 * \author Steve Murphy <murf@digium.com>
32 <support_level>extended</support_level>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #include "asterisk/lock.h"
44 #include "asterisk/astobj2.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/module.h"
51 /* stuff we need to make this work with the astobj2 stuff */
52 #if !defined(LOW_MEMORY)
53 int64_t ast_mark(int prof_id, int x)
66 char *pbx_substitute_variables_helper_full(struct ast_channel *chan, struct varshead *head, const char *cp1, char *cp2, int maxlen, size_t *used);
67 char *pbx_substitute_variables_helper_full(struct ast_channel *chan, struct varshead *head, const char *cp1, char *cp2, int maxlen, size_t *used)
72 static int hash_string(const void *obj, const int flags)
74 char *str = ((struct ht_element*)obj)->key;
77 for (total=0; *str; str++)
79 unsigned int tmp = total;
80 total <<= 1; /* multiply by 2 */
81 total += tmp; /* multiply by 3 */
82 total <<= 2; /* multiply by 12 */
83 total += tmp; /* multiply by 13 */
85 total += ((unsigned int)(*str));
92 static int hashtab_compare_strings(void *a, void *b, int flags)
94 const struct ht_element *ae = a, *be = b;
95 return !strcmp(ae->key, be->key) ? CMP_MATCH | CMP_STOP : 0;
100 static int my_rand(int incl_low, int incl_high, unsigned int *seedp)
105 return incl_low + (rand_r(seedp) % incl_high);
111 /* the testing routines */
113 static int glob_highwater = 0;
114 struct ao2_container *glob_hashtab = 0;
115 unsigned int glob_seed = 0;
118 int els_lookedup = 0;
120 int els_traversals = 0;
122 /* all the operations to perform on the hashtab */
124 static void ht_destroy(void *obj)
126 const struct ht_element *o = obj;
134 static void add_element(void)
137 struct ht_element *x = ao2_alloc(sizeof(struct ht_element), ht_destroy);
138 sprintf(keybuf,"key%08d", glob_highwater++);
139 x->key = strdup(keybuf);
140 x->val = strdup("interesting data");
142 printf("+ %s\n", keybuf);
144 ao2_link(glob_hashtab, x);
146 els_added++; /* unprotected, sometimes off, but, not really important, either */
149 static int do_nothing_cb(void *obj, void *arg, int flags)
154 static void traverse_elements(void)
157 printf("Traverse hashtab\n");
159 ao2_callback(glob_hashtab, OBJ_NODATA, do_nothing_cb, NULL);
160 els_traversals++; /* unprotected, sometimes off, but, not really important, either */
163 static void * del_element(unsigned int *seedp)
166 struct ht_element *el, lookup;
169 /* pick a random element from 0 to highwater-1 */
170 x = my_rand(0,glob_highwater-1,seedp);
171 sprintf(keybuf, "key%08d", x);
173 printf("- %s", keybuf);
176 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
179 printf("...YES (el=%x)\n", (unsigned long)el);
181 ao2_unlink(glob_hashtab, el); /* mistakenly tried to use ao2_ref(c,-2) here to unlink. Bad Boy! */
192 static int lookup_element(unsigned int *seedp)
195 struct ht_element *el, lookup;
198 /* pick a random element from 0 to highwater-1 */
199 x = my_rand(0,glob_highwater-1,seedp);
200 sprintf(keybuf, "key%08d", x);
202 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
206 ao2_ref(el, -1); /* toss out this ref, no longer needed */
214 static void *hashtest(void *data)
216 int my_els_removed = 0;
217 int my_els_added = 0;
218 int my_els_lookedup = 0;
219 int my_els_found = 0;
220 int my_els_traversals = 0;
221 int my_testno = testno++;
224 /* data will be a random number == use as a seed for random numbers */
225 unsigned long seed = (unsigned long)data;
226 printf("hashtest thread created... test beginning\n");
228 /* main test routine-- a global hashtab exists, pound it like crazy */
229 for(its=0;its<100000;its++)
232 int op = my_rand(0,100, seed2);
236 printf("%d[%d]: LOOKUP\n", my_testno, its);
238 if ((my_els_lookedup%1000)==0) {
242 if (lookup_element(seed2))
244 } else if (op < 61) { /* make this 61 and it'll take 16 minutes to run */
246 printf("%d[%d]: TRAVERSE\n", my_testno, its);
251 } else if (op < 80) {
253 printf("%d[%d]: REMOVE\n", my_testno, its);
255 if (del_element(seed2))
260 printf("%d[%d]: ADD\n", my_testno, its);
265 printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
266 my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals);
267 printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d; traversals=%d\n",
268 els_found, els_lookedup, els_added, els_removed, els_traversals);
273 static void run_hashtest(int numthr)
275 pthread_t thr[numthr];
276 void *thrres[numthr];
279 /* init a single global hashtab, then... */
280 glob_hashtab = ao2_container_alloc(180000, hash_string, hashtab_compare_strings);
282 /* set a random seed */
283 glob_seed = (unsigned int)time(0);
286 /* create threads, each running hashtest */
287 for(i=0;i<numthr;i++)
289 unsigned long z = rand();
291 printf("starting hashtest thread %d....\n",i+1);
292 if (ast_pthread_create(&thr[i], NULL, hashtest, (void*)z)) {
293 printf("Sorry, couldn't create thread #%d\n", i+1);
295 printf("hashtest thread spawned.... \n");
297 /* collect threads, each running hashtest */
298 for(i=0;i<numthr;i++)
300 printf("waiting for thread %d....\n", i+1);
301 if (pthread_join(thr[i], &thrres[i])) {
302 printf("Sorry, couldn't join thread #%d\n", i+1);
304 printf("hashtest thread %d done.... \n",i+1);
306 /* user has to kill/intr the process to stop the test? */
310 int main(int argc,char **argv)
312 if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
314 printf("Usage: hashtest <number of threads>\n");
318 /* one arg == number of threads to create */
319 run_hashtest(atoi(argv[1]));
324 #if !defined(LOW_MEMORY)
325 int ast_add_profile(const char *x, uint64_t scale)
331 int ast_loader_register(int (*updater)(void))
336 int ast_loader_unregister(int (*updater)(void))
340 void ast_module_register(const struct ast_module_info *x)
344 void ast_module_unregister(const struct ast_module_info *x)
349 void ast_register_file_version(const char *file, const char *version);
350 void ast_register_file_version(const char *file, const char *version)
354 void ast_unregister_file_version(const char *file);
355 void ast_unregister_file_version(const char *file)
360 int ast_register_atexit(void (*func)(void));
361 int ast_register_atexit(void (*func)(void))
366 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
370 printf("LOG: lev:%d file:%s line:%d func: %s ",
371 level, file, line, function);
377 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
388 void ast_register_thread(char *name)
393 void ast_unregister_thread(void *id)
398 struct ast_bt* ast_bt_create(void)
403 int ast_bt_get_addresses(struct ast_bt *bt)
408 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
410 char **foo = calloc(num_frames, sizeof(char *) + 1);
413 for (i = 0; i < num_frames; i++) {
414 foo[i] = (char *) foo + sizeof(char *) * num_frames;
420 void *ast_bt_destroy(struct ast_bt *bt)