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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <sys/types.h>
47 #include "asterisk/lock.h"
48 #include "asterisk/hashtab.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/module.h"
54 /* stuff we need to make this work with the hashtab stuff */
56 int64_t ast_mark(int prof_id, int x)
66 static int hashtab_compare_strings_nocase(const void *a, const void *b)
68 const struct ht_element *ae = a, *be = b;
69 return ast_hashtab_compare_strings_nocase(ae->key, be->key);
72 static int hashtab_compare_strings(const void *a, const void *b)
74 const struct ht_element *ae = a, *be = b;
75 return ast_hashtab_compare_strings(ae->key, be->key);
78 static unsigned int hashtab_hash_string(const void *obj)
80 const struct ht_element *o = obj;
81 return ast_hashtab_hash_string((const void *)o->key);
84 static unsigned int hashtab_hash_string_nocase(const void *obj)
86 const struct ht_element *o = obj;
87 return ast_hashtab_hash_string_nocase((const void*)o->key);
92 my_rand(int incl_low, int incl_high, unsigned int *seedp)
97 return incl_low + (rand_r(seedp) % incl_high);
103 /* the testing routines */
105 static int glob_highwater = 0;
106 struct ast_hashtab *glob_hashtab = 0;
107 unsigned int glob_seed = 0;
110 int els_lookedup = 0;
112 int els_traversals = 0;
114 /* all the operations to perform on the hashtab */
116 static void add_element(void)
119 struct ht_element *x = malloc(sizeof(struct ht_element));
120 sprintf(keybuf,"key%08d", glob_highwater++);
121 x->key = strdup(keybuf);
122 x->val = strdup("interesting data");
123 ast_hashtab_insert_immediate(glob_hashtab, x);
127 static void traverse_elements(void)
129 struct ht_element *el;
131 struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(glob_hashtab);
133 printf("Traverse hashtab\n");
135 while ((el = ast_hashtab_next(it))) {
138 ast_hashtab_end_traversal(it);
139 els_traversals++; /* unprotected, sometimes off, but, not really important, either */
142 static void * del_element(unsigned int *seedp)
145 struct ht_element *el, lookup;
148 /* pick a random element from 0 to highwater-1 */
149 x = my_rand(0,glob_highwater-1,seedp);
150 sprintf(keybuf, "key%08d", x);
152 printf("Removing %s", keybuf);
155 el = ast_hashtab_remove_object_via_lookup(glob_hashtab, &lookup);
159 printf("...YES (el=%x)\n", (unsigned long)el);
176 static int lookup_element(unsigned int *seedp)
179 struct ht_element *el, lookup;
182 /* pick a random element from 0 to highwater-1 */
183 x = my_rand(0,glob_highwater-1,seedp);
184 sprintf(keybuf, "key%08d", x);
186 el = ast_hashtab_lookup(glob_hashtab, &lookup);
196 static void *hashtest(void *data)
198 int my_els_removed = 0;
199 int my_els_added = 0;
200 int my_els_lookedup = 0;
201 int my_els_found = 0;
202 int my_els_traversals = 0;
203 int my_testno = testno++;
205 /* data will be a random number == use as a seed for random numbers */
206 unsigned long seed = (unsigned long)data;
207 printf("hashtest thread created... test beginning\n");
209 /* main test routine-- a global hashtab exists, pound it like crazy */
211 for(its=0;its<100000;its++)
214 int op = my_rand(0,100, seed2);
218 printf("%d[%d]: LOOKUP\n", my_testno, its);
220 if ((my_els_lookedup%1000)==0) {
224 if (lookup_element(seed2))
226 } else if (op < 61) { /* make this 61 and it'll take 15 minutes to run */
228 printf("%d[%d]: TRAVERSE\n", my_testno, its);
233 } else if (op < 80) {
235 printf("%d[%d]: REMOVE\n", my_testno, its);
237 if (del_element(seed2))
242 printf("%d[%d]: ADD\n", my_testno, its);
247 printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
248 my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals);
249 printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
250 els_found, els_lookedup, els_added, els_removed,els_traversals);
254 void run_hashtest(int numthr)
256 pthread_t thr[numthr];
257 void *thrres[numthr];
258 int i, biggest, resize_cnt, numobjs, numbuckets;
260 /* init a single global hashtab, then... */
261 glob_hashtab = ast_hashtab_create(180000, hashtab_compare_strings_nocase, ast_hashtab_resize_java, ast_hashtab_newsize_java, hashtab_hash_string_nocase, 1);
262 printf("starting with %d elements in the hashtable...\n", ast_hashtab_capacity(glob_hashtab));
263 /* set a random seed */
264 glob_seed = (unsigned int)time(0);
267 /* create threads, each running hashtest */
268 for(i=0;i<numthr;i++)
270 unsigned long z = rand();
272 printf("starting hashtest thread %d....\n",i+1);
273 if (ast_pthread_create(&thr[i], NULL, hashtest, (void*)z)) {
274 printf("Sorry, couldn't create thread #%d\n", i+1);
276 printf("hashtest thread spawned.... \n");
278 /* collect threads, each running hashtest */
279 for(i=0;i<numthr;i++)
281 printf("waiting for thread %d....\n", i+1);
282 if (pthread_join(thr[i], &thrres[i])) {
283 printf("Sorry, couldn't join thread #%d\n", i+1);
285 printf("hashtest thread %d done.... \n",i+1);
287 /* user has to kill/intr the process to stop the test? */
288 ast_hashtab_get_stats(glob_hashtab, &biggest, &resize_cnt, &numobjs, &numbuckets);
289 printf("Some stats: longest bucket chain: %d; number of resizes: %d; number of objects: %d; capacity: %d\n",
290 biggest, resize_cnt, numobjs, numbuckets);
294 int main(int argc,char **argv)
296 if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
298 printf("Usage: hashtest <number of threads>\n");
302 /* one arg == number of threads to create */
303 run_hashtest(atoi(argv[1]));
309 struct ast_app *pbx_findapp(const char *app)
311 return (struct ast_app*)1; /* so as not to trigger an error */
314 int ast_add_profile(const char *x, uint64_t scale)
318 int ast_loader_register(int (*updater)(void))
323 int ast_loader_unregister(int (*updater)(void))
327 void ast_module_register(const struct ast_module_info *x)
331 void ast_module_unregister(const struct ast_module_info *x)
336 void ast_register_file_version(const char *file, const char *version)
340 void ast_unregister_file_version(const char *file)
345 void ast_context_destroy(void)
349 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
353 printf("LOG: lev:%d file:%s line:%d func: %s ",
354 level, file, line, function);
360 void ast_verbose(const char *fmt, ...)
371 void ast_register_thread(char *name)
376 void ast_unregister_thread(void *id)