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>
33 #include <sys/types.h>
45 #include "asterisk/lock.h"
46 #include "asterisk/astobj2.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/module.h"
53 /* stuff we need to make this work with the astobj2 stuff */
55 void ast_cli(int *fd, char *str, ...)
59 int64_t ast_mark(int prof_id, int x)
71 static int hash_string(const void *obj, const int flags)
73 unsigned char *str = ((struct ht_element*)obj)->key;
76 for (total=0; *str; str++)
78 unsigned int tmp = total;
79 total <<= 1; /* multiply by 2 */
80 total += tmp; /* multiply by 3 */
81 total <<= 2; /* multiply by 12 */
82 total += tmp; /* multiply by 13 */
84 total += ((unsigned int)(*str));
91 static int hashtab_compare_strings(void *a, void *b, int flags)
93 const struct ht_element *ae = a, *be = b;
94 return !strcmp(ae->key, be->key) ? CMP_MATCH : 0;
99 my_rand(int incl_low, int incl_high, unsigned int *seedp)
104 return incl_low + (rand_r(seedp) % incl_high);
110 /* the testing routines */
112 static int glob_highwater = 0;
113 struct ao2_container *glob_hashtab = 0;
114 unsigned int glob_seed = 0;
117 int els_lookedup = 0;
119 int els_traversals = 0;
121 /* all the operations to perform on the hashtab */
123 void ht_destroy(void *obj)
125 const struct ht_element *o = obj;
133 static void add_element(void)
136 struct ht_element *x = ao2_alloc(sizeof(struct ht_element), ht_destroy);
137 sprintf(keybuf,"key%08d", glob_highwater++);
138 x->key = strdup(keybuf);
139 x->val = strdup("interesting data");
141 printf("+ %s\n", keybuf);
143 ao2_link(glob_hashtab, x);
145 els_added++; /* unprotected, sometimes off, but, not really important, either */
148 static void traverse_elements(void)
150 struct ht_element *el;
151 struct ao2_iterator it = ao2_iterator_init(glob_hashtab, 0);
153 printf("Traverse hashtab\n");
155 while ((el = ao2_iterator_next(&it))) {
158 els_traversals++; /* unprotected, sometimes off, but, not really important, either */
161 static void * del_element(unsigned int *seedp)
164 struct ht_element *el, lookup;
167 /* pick a random element from 0 to highwater-1 */
168 x = my_rand(0,glob_highwater-1,seedp);
169 sprintf(keybuf, "key%08d", x);
171 printf("- %s", keybuf);
174 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
177 printf("...YES (el=%x)\n", (unsigned long)el);
179 ao2_unlink(glob_hashtab, el); /* mistakenly tried to use ao2_ref(c,-2) here to unlink. Bad Boy! */
190 static int lookup_element(unsigned int *seedp)
193 struct ht_element *el, lookup;
196 /* pick a random element from 0 to highwater-1 */
197 x = my_rand(0,glob_highwater-1,seedp);
198 sprintf(keybuf, "key%08d", x);
200 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
204 ao2_ref(el, -1); /* toss out this ref, no longer needed */
212 static void *hashtest(void *data)
214 int my_els_removed = 0;
215 int my_els_added = 0;
216 int my_els_lookedup = 0;
217 int my_els_found = 0;
218 int my_els_traversals = 0;
219 int my_testno = testno++;
221 /* data will be a random number == use as a seed for random numbers */
222 unsigned long seed = (unsigned long)data;
223 printf("hashtest thread created... test beginning\n");
225 /* main test routine-- a global hashtab exists, pound it like crazy */
227 for(its=0;its<100000;its++)
230 int op = my_rand(0,100, seed2);
234 printf("%d[%d]: LOOKUP\n", my_testno, its);
236 if ((my_els_lookedup%1000)==0) {
240 if (lookup_element(seed2))
242 } else if (op < 61) { /* make this 61 and it'll take 16 minutes to run */
244 printf("%d[%d]: TRAVERSE\n", my_testno, its);
249 } else if (op < 80) {
251 printf("%d[%d]: REMOVE\n", my_testno, its);
253 if (del_element(seed2))
258 printf("%d[%d]: ADD\n", my_testno, its);
263 printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
264 my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals);
265 printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d; traversals=%d\n",
266 els_found, els_lookedup, els_added, els_removed, els_traversals);
270 void run_hashtest(int numthr)
272 pthread_t thr[numthr];
273 void *thrres[numthr];
276 /* init a single global hashtab, then... */
277 glob_hashtab = ao2_container_alloc(180000, hash_string, hashtab_compare_strings);
279 /* set a random seed */
280 glob_seed = (unsigned int)time(0);
283 /* create threads, each running hashtest */
284 for(i=0;i<numthr;i++)
286 unsigned long z = rand();
288 printf("starting hashtest thread %d....\n",i+1);
289 if (ast_pthread_create(&thr[i], NULL, hashtest, (void*)z)) {
290 printf("Sorry, couldn't create thread #%d\n", i+1);
292 printf("hashtest thread spawned.... \n");
294 /* collect threads, each running hashtest */
295 for(i=0;i<numthr;i++)
297 printf("waiting for thread %d....\n", i+1);
298 if (pthread_join(thr[i], &thrres[i])) {
299 printf("Sorry, couldn't join thread #%d\n", i+1);
301 printf("hashtest thread %d done.... \n",i+1);
303 /* user has to kill/intr the process to stop the test? */
307 int main(int argc,char **argv)
309 if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
311 printf("Usage: hashtest <number of threads>\n");
315 /* one arg == number of threads to create */
316 run_hashtest(atoi(argv[1]));
322 struct ast_app *pbx_findapp(const char *app)
324 return (struct ast_app*)1; /* so as not to trigger an error */
327 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_cli_register_multiple(void)
353 void ast_register_file_version(const char *file, const char *version)
357 void ast_unregister_file_version(const char *file)
362 void ast_cli_unregister_multiple(void)
366 void ast_context_destroy(void)
370 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
374 printf("LOG: lev:%d file:%s line:%d func: %s ",
375 level, file, line, function);
381 void ast_verbose(const char *fmt, ...)
392 void ast_register_thread(char *name)
397 void ast_unregister_thread(void *id)