c06d4ac0adadd353054f36bd5f4affa249d8c157
[asterisk/asterisk.git] / utils / hashtest.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 /*** MODULEINFO
32         <support_level>extended</support_level>
33  ***/
34
35 #include "asterisk.h"
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <pthread.h>
39 #include <sys/stat.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include "asterisk/lock.h"
43 #include "asterisk/hashtab.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/module.h"
47 int testno = 1;
48
49 /* stuff we need to make this work with the hashtab stuff */
50 #if !defined(LOW_MEMORY)
51 int64_t ast_mark(int prof_id, int x)
52 {
53         return 0;
54 }
55 #endif
56
57 void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int cp2_size, size_t *used);
58 void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int cp2_size, size_t *used)
59 {
60 }
61
62 struct ht_element 
63 {
64         char *key;
65         char *val;
66 };
67
68 static int hashtab_compare_strings_nocase(const void *a, const void *b)
69 {
70         const struct ht_element *ae = a, *be = b;
71         return ast_hashtab_compare_strings_nocase(ae->key, be->key);
72 }
73
74 #if 0
75 static int hashtab_compare_strings(const void *a, const void *b)
76 {
77         const struct ht_element *ae = a, *be = b;
78         return ast_hashtab_compare_strings(ae->key, be->key);
79 }
80
81 static unsigned int hashtab_hash_string(const void *obj)
82 {
83         const struct ht_element *o = obj;
84         return ast_hashtab_hash_string((const void *)o->key);
85 }
86 #endif
87
88 static unsigned int hashtab_hash_string_nocase(const void *obj)
89 {
90         const struct ht_element *o = obj;
91         return ast_hashtab_hash_string_nocase((const void*)o->key);
92 }
93
94 /* random numbers */
95
96 static int my_rand(int incl_low, int incl_high, unsigned int *seedp)
97 {
98         if (incl_high == 0)
99                 return 0;
100         
101         return incl_low + (rand_r(seedp) % incl_high);
102 }
103
104
105
106
107 /* the testing routines */
108
109 static int glob_highwater = 0;
110 struct ast_hashtab *glob_hashtab = 0;
111 unsigned int glob_seed = 0;
112 int els_removed = 0;
113 int els_added = 0;
114 int els_lookedup = 0;
115 int els_found = 0;
116 int els_traversals = 0;
117
118 /* all the operations to perform on the hashtab */
119
120 static void add_element(void)
121 {
122         char keybuf[100];
123         struct ht_element *x = malloc(sizeof(struct ht_element));
124         sprintf(keybuf,"key%08d", glob_highwater++);
125         x->key = strdup(keybuf);
126         x->val = strdup("interesting data");
127         ast_hashtab_insert_immediate(glob_hashtab, x);
128         els_added++;
129 }
130
131 static void traverse_elements(void)
132 {
133         struct ht_element *el;
134         int c=0;
135         struct ast_hashtab_iter *it = ast_hashtab_start_write_traversal(glob_hashtab);
136 #ifdef DEBUG
137         printf("Traverse hashtab\n");
138 #endif
139         while ((el = ast_hashtab_next(it))) {
140                 c++;
141         }
142         ast_hashtab_end_traversal(it);
143         els_traversals++; /* unprotected, sometimes off, but, not really important, either */
144 }
145
146 static void * del_element(unsigned int *seedp)
147 {
148         char keybuf[100];
149         struct ht_element *el, lookup;
150         int x;
151         
152         /* pick a random element from 0 to highwater-1 */
153         x = my_rand(0,glob_highwater-1,seedp);
154         sprintf(keybuf, "key%08d", x);
155 #ifdef DEBUG
156         printf("Removing %s", keybuf);
157 #endif
158         lookup.key = keybuf;
159         el = ast_hashtab_remove_object_via_lookup(glob_hashtab, &lookup);
160         
161         if (el) {
162 #ifdef DEBUG
163                 printf("...YES (el=%x)\n", (unsigned long)el);
164 #endif
165                 free(el->key);
166                 free(el->val);
167                 free(el);
168                 els_removed++;
169         } else {
170 #ifdef DEBUG
171                 printf("...NO.\n");
172 #endif
173                 return 0;
174         }
175         
176         
177         return el;
178 }
179
180 static int lookup_element(unsigned int *seedp)
181 {
182         char keybuf[100];
183         struct ht_element *el, lookup;
184         int x;
185         
186         /* pick a random element from 0 to highwater-1 */
187         x = my_rand(0,glob_highwater-1,seedp);
188         sprintf(keybuf, "key%08d", x);
189         lookup.key = keybuf;
190         el = ast_hashtab_lookup(glob_hashtab, &lookup);
191         els_lookedup++;
192         if (el)
193                 els_found++;
194         if (el)
195                 return 1;
196         return 0;
197 }
198
199
200 static void *hashtest(void *data)
201 {
202         int my_els_removed = 0;
203         int my_els_added = 0;
204         int my_els_lookedup = 0;
205         int my_els_found = 0;
206         int my_els_traversals = 0;
207         int my_testno = testno++;
208         int its;
209         
210         /* data will be a random number == use as a seed for random numbers */
211         unsigned long seed = (unsigned long)data;
212
213         printf("hashtest thread created... test beginning\n");
214         
215         /* main test routine-- a global hashtab exists, pound it like crazy  */
216         for(its=0;its<100000;its++)
217         {
218                 void *seed2 = &seed;
219                 int op = my_rand(0,100, seed2);
220                 if (op<60) {
221                         my_els_lookedup++;
222 #ifdef DEBUG
223                         printf("%d[%d]: LOOKUP\n", my_testno, its);
224 #endif                  
225                         if ((my_els_lookedup%1000)==0) {
226                                 printf(".");
227                                 fflush(stdout);
228                         }
229                         if (lookup_element(seed2))
230                                 my_els_found++;
231                 } else if (op < 61) { /* make this 61 and it'll take 15 minutes to run */
232 #ifdef DEBUG
233                         printf("%d[%d]: TRAVERSE\n", my_testno, its);
234 #endif
235                         traverse_elements();
236                         my_els_traversals++;
237                         
238                 } else if (op < 80) {
239 #ifdef DEBUG
240                         printf("%d[%d]: REMOVE\n", my_testno, its);
241 #endif
242                         if (del_element(seed2))
243                                 my_els_removed++;
244                 } else {
245                         my_els_added++;
246 #ifdef DEBUG
247                         printf("%d[%d]: ADD\n", my_testno, its);
248 #endif
249                         add_element();
250                 }
251         }
252         printf("\nhashtest thread %d exiting.... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
253                    my_testno, my_els_found, my_els_lookedup, my_els_added, my_els_removed, my_els_traversals);
254         printf("\ntotals..................... lookups=%d/%d, added=%d, removed=%d, traversals=%d;\n",
255                    els_found, els_lookedup, els_added, els_removed,els_traversals);
256         pthread_exit(0);
257 }
258
259 static void run_hashtest(int numthr)
260 {
261         pthread_t thr[numthr];
262         void *thrres[numthr];
263         int i, biggest, resize_cnt, numobjs, numbuckets;
264         
265         /* init a single global hashtab, then... */
266         glob_hashtab = ast_hashtab_create(180000, hashtab_compare_strings_nocase, ast_hashtab_resize_java, ast_hashtab_newsize_java, hashtab_hash_string_nocase, 1);
267         printf("starting with %d elements in the hashtable...\n", ast_hashtab_capacity(glob_hashtab));
268         /* set a random seed  */
269         glob_seed = (unsigned int)time(0);
270         srand(glob_seed);
271         
272         /* create threads, each running hashtest */
273         for(i=0;i<numthr;i++)
274         {
275                 unsigned long z = rand();
276                 
277                 printf("starting hashtest thread %d....\n",i+1);
278                 if (ast_pthread_create(&thr[i], NULL, hashtest, (void*)z)) {
279                         printf("Sorry, couldn't create thread #%d\n", i+1);
280                 }
281                 printf("hashtest thread spawned.... \n");
282         }
283         /* collect threads, each running hashtest */
284         for(i=0;i<numthr;i++)
285         {
286                 printf("waiting for thread %d....\n", i+1);
287                 if (pthread_join(thr[i], &thrres[i])) {
288                         printf("Sorry, couldn't join thread #%d\n", i+1);
289                 }
290                 printf("hashtest thread %d done.... \n",i+1);
291         }
292         /* user has to kill/intr the process to stop the test? */
293         ast_hashtab_get_stats(glob_hashtab, &biggest, &resize_cnt, &numobjs, &numbuckets);
294         printf("Some stats: longest bucket chain: %d;  number of resizes: %d; number of objects: %d;  capacity: %d\n",
295                         biggest, resize_cnt, numobjs, numbuckets);
296 }
297
298
299 int main(int argc,char **argv)
300 {
301         if (argc < 2 || argc > 2 || atoi(argv[1]) < 1)
302         {
303                 printf("Usage: hashtest <number of threads>\n");
304                 exit(1);
305         }
306         
307         /* one arg == number of threads to create */
308         run_hashtest(atoi(argv[1]));
309         
310         return 0;
311 }
312 #if !defined(LOW_MEMORY)
313 int  ast_add_profile(const char *x, uint64_t scale)
314 {
315         return 0;
316 }
317 #endif
318
319 int ast_loader_register(int (*updater)(void))
320 {
321         return 1;
322 }
323
324 int ast_loader_unregister(int (*updater)(void))
325 {
326         return 1;
327 }
328 void ast_module_register(const struct ast_module_info *x)
329 {
330 }
331
332 void ast_module_unregister(const struct ast_module_info *x)
333 {
334 }
335
336
337 void ast_register_file_version(const char *file, const char *version);
338 void ast_register_file_version(const char *file, const char *version)
339 {
340 }
341
342 void ast_unregister_file_version(const char *file);
343 void ast_unregister_file_version(const char *file)
344 {
345
346 }
347
348 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
349 {
350         va_list vars;
351         va_start(vars,fmt);
352         printf("LOG: lev:%d file:%s  line:%d func: %s  ",
353                    level, file, line, function);
354         vprintf(fmt, vars);
355         fflush(stdout);
356         va_end(vars);
357 }
358
359 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
360 {
361         va_list vars;
362         va_start(vars,fmt);
363
364         printf("VERBOSE: ");
365         vprintf(fmt, vars);
366         fflush(stdout);
367         va_end(vars);
368 }
369
370 void ast_register_thread(char *name)
371 {
372
373 }
374
375 void ast_unregister_thread(void *id)
376 {
377 }
378
379 #ifdef HAVE_BKTR
380 struct ast_bt *ast_bt_create(void);
381 struct ast_bt *ast_bt_create(void) 
382 {
383         return NULL;
384 }
385
386 int ast_bt_get_addresses(struct ast_bt *bt);
387 int ast_bt_get_addresses(struct ast_bt *bt)
388 {
389         return 0;
390 }
391
392 char **ast_bt_get_symbols(void **addresses, size_t num_frames);
393 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
394 {
395         char **foo = calloc(num_frames, sizeof(char *) + 1);
396         if (foo) {
397                 int i;
398                 for (i = 0; i < num_frames; i++) {
399                         foo[i] = (char *) foo + sizeof(char *) * num_frames;
400                 }
401         }
402         return foo;
403 }
404
405 void *ast_bt_destroy(struct ast_bt *bt);
406 void *ast_bt_destroy(struct ast_bt *bt)
407 {
408         return NULL;
409 }
410 #endif