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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "asterisk/lock.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/module.h"
47 /* stuff we need to make this work with the astobj2 stuff */
48 #if !defined(LOW_MEMORY)
49 int64_t ast_mark(int prof_id, int x)
63 static int hash_string(const void *obj, const int flags)
65 char *str = ((struct ht_element*)obj)->key;
68 for (total=0; *str; str++)
70 unsigned int tmp = total;
71 total <<= 1; /* multiply by 2 */
72 total += tmp; /* multiply by 3 */
73 total <<= 2; /* multiply by 12 */
74 total += tmp; /* multiply by 13 */
76 total += ((unsigned int)(*str));
83 static int hashtab_compare_strings(void *a, void *b, int flags)
85 const struct ht_element *ae = a, *be = b;
86 return !strcmp(ae->key, be->key) ? CMP_MATCH : 0;
91 static int my_rand(int incl_low, int incl_high, unsigned int *seedp)
96 return incl_low + (rand_r(seedp) % incl_high);
102 /* the testing routines */
104 static int glob_highwater = 0;
105 struct ao2_container *glob_hashtab = 0;
106 unsigned int glob_seed = 0;
109 int els_lookedup = 0;
111 int els_traversals = 0;
113 /* all the operations to perform on the hashtab */
115 static void ht_destroy(void *obj)
117 const struct ht_element *o = obj;
125 static void add_element(void)
128 struct ht_element *x = ao2_alloc(sizeof(struct ht_element), ht_destroy);
129 sprintf(keybuf,"key%08d", glob_highwater++);
130 x->key = strdup(keybuf);
131 x->val = strdup("interesting data");
133 printf("+ %s\n", keybuf);
135 ao2_link(glob_hashtab, x);
137 els_added++; /* unprotected, sometimes off, but, not really important, either */
140 static int do_nothing_cb(void *obj, void *arg, int flags)
145 static void traverse_elements(void)
148 printf("Traverse hashtab\n");
150 ao2_callback(glob_hashtab, OBJ_NODATA, do_nothing_cb, NULL);
151 els_traversals++; /* unprotected, sometimes off, but, not really important, either */
154 static void * del_element(unsigned int *seedp)
157 struct ht_element *el, lookup;
160 /* pick a random element from 0 to highwater-1 */
161 x = my_rand(0,glob_highwater-1,seedp);
162 sprintf(keybuf, "key%08d", x);
164 printf("- %s", keybuf);
167 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
170 printf("...YES (el=%x)\n", (unsigned long)el);
172 ao2_unlink(glob_hashtab, el); /* mistakenly tried to use ao2_ref(c,-2) here to unlink. Bad Boy! */
183 static int lookup_element(unsigned int *seedp)
186 struct ht_element *el, lookup;
189 /* pick a random element from 0 to highwater-1 */
190 x = my_rand(0,glob_highwater-1,seedp);
191 sprintf(keybuf, "key%08d", x);
193 el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
197 ao2_ref(el, -1); /* toss out this ref, no longer needed */
205 static void *hashtest(void *data)
207 int my_els_removed = 0;
208 int my_els_added = 0;
209 int my_els_lookedup = 0;
210 int my_els_found = 0;
211 int my_els_traversals = 0;
212 int my_testno = testno++;
215 /* data will be a random number == use as a seed for random numbers */
216 unsigned long seed = (unsigned long)data;
217 printf("hashtest thread created... test beginning\n");
219 /* main test routine-- a global hashtab exists, pound it like crazy */
220 for(its=0;its<100000;its++)
223 int op = my_rand(0,100, seed2);
227 printf("%d[%d]: LOOKUP\n", my_testno, its);
229 if ((my_els_lookedup%1000)==0) {
233 if (lookup_element(seed2))
235 } else if (op < 61) { /* make this 61 and it'll take 16 minutes to run */
237 printf("%d[%d]: TRAVERSE\n", my_testno, its);
242 } else if (op < 80) {
244 printf("%d[%d]: REMOVE\n", my_testno, its);
246 if (del_element(seed2))
251 printf("%d[%d]: ADD\n", my_testno, its);
256 printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
257 my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals);
258 printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d; traversals=%d\n",
259 els_found, els_lookedup, els_added, els_removed, els_traversals);
264 static void run_hashtest(int numthr)
266 pthread_t thr[numthr];
267 void *thrres[numthr];
270 /* init a single global hashtab, then... */
271 glob_hashtab = ao2_container_alloc(180000, hash_string, hashtab_compare_strings);
273 /* set a random seed */
274 glob_seed = (unsigned int)time(0);
277 /* create threads, each running hashtest */
278 for(i=0;i<numthr;i++)
280 unsigned long z = rand();
282 printf("starting hashtest thread %d....\n",i+1);
283 if (ast_pthread_create(&thr[i], NULL, hashtest, (void*)z)) {
284 printf("Sorry, couldn't create thread #%d\n", i+1);
286 printf("hashtest thread spawned.... \n");
288 /* collect threads, each running hashtest */
289 for(i=0;i<numthr;i++)
291 printf("waiting for thread %d....\n", i+1);
292 if (pthread_join(thr[i], &thrres[i])) {
293 printf("Sorry, couldn't join thread #%d\n", i+1);
295 printf("hashtest thread %d done.... \n",i+1);
297 /* user has to kill/intr the process to stop the test? */
301 int main(int argc,char **argv)
303 if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
305 printf("Usage: hashtest <number of threads>\n");
309 /* one arg == number of threads to create */
310 run_hashtest(atoi(argv[1]));
315 #if !defined(LOW_MEMORY)
316 int ast_add_profile(const char *x, uint64_t scale)
322 int ast_loader_register(int (*updater)(void))
327 int ast_loader_unregister(int (*updater)(void))
331 void ast_module_register(const struct ast_module_info *x)
335 void ast_module_unregister(const struct ast_module_info *x)
340 void ast_register_file_version(const char *file, const char *version);
341 void ast_register_file_version(const char *file, const char *version)
345 void ast_unregister_file_version(const char *file);
346 void ast_unregister_file_version(const char *file)
351 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
355 printf("LOG: lev:%d file:%s line:%d func: %s ",
356 level, file, line, function);
362 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
373 void ast_register_thread(char *name)
378 void ast_unregister_thread(void *id)
383 struct ast_bt* ast_bt_create(void)
388 int ast_bt_get_addresses(struct ast_bt *bt)
393 void *ast_bt_destroy(struct ast_bt *bt)