(closes issue #10695)
[asterisk/asterisk.git] / utils / hashtest2.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Steve Murphy
5  *
6  * Steve Murphy <murf@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 /*! \file
19  *
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.
27  *
28  *  \author Steve Murphy <murf@digium.com>
29  */
30
31 #include "asterisk.h"
32
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <alloca.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include <sys/stat.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <unistd.h>
44
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"
50
51 int testno = 2;
52
53 /* stuff we need to make this work with the astobj2 stuff */
54
55 void ast_cli(int *fd, char *str, ...)
56 {
57 }
58
59 int64_t ast_mark(int prof_id, int x)
60 {
61 }
62
63 /* my OBJECT */
64 struct ht_element 
65 {
66         char *key;
67         char *val;
68 };
69
70
71 static int hash_string(const void *obj, const int flags)
72 {
73         unsigned char *str = ((struct ht_element*)obj)->key;
74         int total;
75
76         for (total=0; *str; str++)
77         {
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 */
83         
84                 total += ((unsigned int)(*str));
85         }
86         if (total < 0)
87                 total = -total;
88         return total;
89 }
90
91 static int hashtab_compare_strings(void *a, void *b, int flags)
92 {
93         const struct ht_element *ae = a, *be = b;
94         return !strcmp(ae->key, be->key) ? CMP_MATCH : 0;
95 }
96
97 /* random numbers */
98
99 my_rand(int incl_low, int incl_high, unsigned int *seedp)
100 {
101         if (incl_high == 0)
102                 return 0;
103         
104         return incl_low + (rand_r(seedp) % incl_high);
105 }
106
107
108
109
110 /* the testing routines */
111
112 static int glob_highwater = 0;
113 struct ao2_container *glob_hashtab = 0;
114 unsigned int glob_seed = 0;
115 int els_removed = 0;
116 int els_added = 0;
117 int els_lookedup = 0;
118 int els_found = 0;
119 int els_traversals = 0;
120
121 /* all the operations to perform on the hashtab */
122
123 void ht_destroy(void *obj)
124 {
125         const struct ht_element *o = obj;
126         if (o->key)
127                 free(o->key);
128         if (o->val)
129                 free(o->val);
130 }
131
132
133 static void add_element(void)
134 {
135         char keybuf[100];
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");
140 #ifdef DEBUG
141         printf("+ %s\n", keybuf);
142 #endif  
143         ao2_link(glob_hashtab, x);
144         
145         els_added++; /* unprotected, sometimes off, but, not really important, either */
146 }
147
148 static void traverse_elements(void)
149 {
150         struct ht_element *el;
151         struct ao2_iterator it = ao2_iterator_init(glob_hashtab, 0);
152 #ifdef DEBUG
153         printf("Traverse hashtab\n");
154 #endif
155         while ((el = ao2_iterator_next(&it))) {
156                 ao2_ref(el,-1);
157         }
158         els_traversals++; /* unprotected, sometimes off, but, not really important, either */
159 }
160
161 static void * del_element(unsigned int *seedp)
162 {
163         char keybuf[100];
164         struct ht_element *el, lookup;
165         int x;
166         
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);
170 #ifdef DEBUG
171         printf("- %s", keybuf);
172 #endif
173         lookup.key = keybuf;
174         el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
175         if (el) {
176 #ifdef DEBUG
177                 printf("...YES (el=%x)\n", (unsigned long)el);
178 #endif
179                 ao2_unlink(glob_hashtab, el); /* mistakenly tried to use ao2_ref(c,-2) here to unlink. Bad Boy! */
180                 els_removed++;
181         } else {
182 #ifdef DEBUG
183                 printf("...NO.\n");
184 #endif
185                 return 0;
186         }
187         return el;
188 }
189
190 static int lookup_element(unsigned int *seedp)
191 {
192         char keybuf[100];
193         struct ht_element *el, lookup;
194         int x;
195         
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);
199         lookup.key = keybuf;
200         el = ao2_find(glob_hashtab, &lookup, OBJ_POINTER);
201         els_lookedup++;
202         if (el) {
203                 els_found++;
204                 ao2_ref(el, -1); /* toss out this ref, no longer needed */
205                 return 1;
206         } else {
207                 return 0;
208         }
209 }
210
211
212 static void *hashtest(void *data)
213 {
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++;
220         
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");
224         
225         /* main test routine-- a global hashtab exists, pound it like crazy  */
226         int its;
227         for(its=0;its<100000;its++)
228         {
229                 void *seed2 = &seed;
230                 int op = my_rand(0,100, seed2);
231                 if (op<60) {
232                         my_els_lookedup++;
233 #ifdef DEBUG
234                         printf("%d[%d]: LOOKUP\n", my_testno, its);
235 #endif                  
236                         if ((my_els_lookedup%1000)==0) {
237                                 printf(".");
238                                 fflush(stdout);
239                         }
240                         if (lookup_element(seed2))
241                                 my_els_found++;
242                 } else if (op < 61) { /* make this 61 and it'll take 16 minutes to run */
243 #ifdef DEBUG
244                         printf("%d[%d]: TRAVERSE\n", my_testno, its);
245 #endif
246                         traverse_elements();
247                         my_els_traversals++;
248                         
249                 } else if (op < 80) {
250 #ifdef DEBUG
251                         printf("%d[%d]: REMOVE\n", my_testno, its);
252 #endif
253                         if (del_element(seed2))
254                                 my_els_removed++;
255                 } else {
256                         my_els_added++;
257 #ifdef DEBUG
258                         printf("%d[%d]: ADD\n", my_testno, its);
259 #endif
260                         add_element();
261                 }
262         }
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);
267         pthread_exit(0);
268 }
269
270 void run_hashtest(int numthr)
271 {
272         pthread_t thr[numthr];
273         void *thrres[numthr];
274         int i;
275         
276         /* init a single global hashtab, then... */
277         glob_hashtab = ao2_container_alloc(180000, hash_string, hashtab_compare_strings);
278
279         /* set a random seed  */
280         glob_seed = (unsigned int)time(0);
281         srand(glob_seed);
282         
283         /* create threads, each running hashtest */
284         for(i=0;i<numthr;i++)
285         {
286                 unsigned long z = rand();
287                 
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);
291                 }
292                 printf("hashtest thread spawned.... \n");
293         }
294         /* collect threads, each running hashtest */
295         for(i=0;i<numthr;i++)
296         {
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);
300                 }
301                 printf("hashtest thread %d done.... \n",i+1);
302         }
303         /* user has to kill/intr the process to stop the test? */
304 }
305
306
307 int main(int argc,char **argv)
308 {
309         if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
310         {
311                 printf("Usage: hashtest <number of threads>\n");
312                 exit(1);
313         }
314         
315         /* one arg == number of threads to create */
316         run_hashtest(atoi(argv[1]));
317         
318         return 0;
319 }
320
321
322 struct ast_app *pbx_findapp(const char *app)
323 {
324         return (struct ast_app*)1; /* so as not to trigger an error */
325 }
326
327 int  ast_add_profile(const char *x, uint64_t scale)
328 {
329 }
330
331 int ast_loader_register(int (*updater)(void))
332 {
333         return 1;
334 }
335
336 int ast_loader_unregister(int (*updater)(void))
337 {
338         return 1;
339 }
340 void ast_module_register(const struct ast_module_info *x)
341 {
342 }
343
344 void ast_module_unregister(const struct ast_module_info *x)
345 {
346 }
347
348
349 void ast_cli_register_multiple(void)
350 {
351 }
352
353 void ast_register_file_version(const char *file, const char *version)
354 {
355 }
356
357 void ast_unregister_file_version(const char *file)
358 {
359
360 }
361
362 void ast_cli_unregister_multiple(void)
363 {
364 }
365
366 void ast_context_destroy(void)
367 {
368 }
369
370 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
371 {
372         va_list vars;
373         va_start(vars,fmt);
374         printf("LOG: lev:%d file:%s  line:%d func: %s  ",
375                    level, file, line, function);
376         vprintf(fmt, vars);
377         fflush(stdout);
378         va_end(vars);
379 }
380
381 void ast_verbose(const char *fmt, ...)
382 {
383         va_list vars;
384         va_start(vars,fmt);
385
386         printf("VERBOSE: ");
387         vprintf(fmt, vars);
388         fflush(stdout);
389         va_end(vars);
390 }
391
392 void ast_register_thread(char *name)
393 {
394
395 }
396
397 void ast_unregister_thread(void *id)
398 {
399 }