1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2003-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 #include <libunwind.h>
32 #include <sys/resource.h>
35 #define panic(args...) \
36 do { fprintf (stderr, args); exit (-1); } while (0)
40 static long iterations = 10000;
41 static int maxlevel = 100;
44 #define MB (1024*1024)
46 static char big[64*MB]; /* should be >> max. cache size */
53 gettimeofday (&tv, NULL);
54 return tv.tv_sec + 1e-6*tv.tv_usec;
58 measure_unwind (int maxlevel, double *step)
66 if (unw_init_local (&cursor, &uc) < 0)
67 panic ("unw_init_local() failed\n");
73 ret = unw_step (&cursor);
75 panic ("unw_step() failed\n");
82 if (level <= maxlevel)
83 panic ("Unwound only %d levels, expected at least %d levels\n",
86 *step = (stop - start) / (double) level;
90 static int f1 (int, int, double *);
93 g1 (int level, int maxlevel, double *step)
95 if (level == maxlevel)
96 return measure_unwind (maxlevel, step);
98 /* defeat last-call/sibcall optimization */
99 return f1 (level + 1, maxlevel, step) + level;
103 f1 (int level, int maxlevel, double *step)
105 if (level == maxlevel)
106 return measure_unwind (maxlevel, step);
108 /* defeat last-call/sibcall optimization */
109 return g1 (level + 1, maxlevel, step) + level;
113 doit (const char *label)
115 double step, min_step, first_step, sum_step;
118 sum_step = first_step = 0.0;
120 for (i = 0; i < iterations; ++i)
122 f1 (0, maxlevel, &step);
132 printf ("%s: unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n", label,
133 1e9*first_step, 1e9*min_step, 1e9*sum_step/iterations);
137 sum (void *buf, size_t size)
143 for (i = 0; i < size; i += 8)
152 # define M 10 /* must be at least 2 to get steady-state */
153 double stop, start, get_cold, get_warm, init_cold, init_warm, delta;
157 char padding[1024]; /* should be > 2 * max. cacheline size */
163 char padding[1024]; /* should be > 2 * max. cacheline size */
168 /* Run each test M times and take the minimum to filter out noise
169 such dynamic linker resolving overhead, context-switches,
170 page-in, cache, and TLB effects. */
173 for (j = 0; j < M; ++j)
175 dummy += sum (big, sizeof (big)); /* flush the cache */
176 for (i = 0; i < N; ++i)
177 uc[i].padding[511] = i; /* warm up the TLB */
179 for (i = 0; i < N; ++i)
180 unw_getcontext (&uc[i].uc);
182 delta = (stop - start) / N;
183 if (delta < get_cold)
188 for (j = 0; j < M; ++j)
190 dummy += sum (big, sizeof (big)); /* flush cache */
191 for (i = 0; i < N; ++i)
192 uc[i].padding[511] = i; /* warm up the TLB */
194 for (i = 0; i < N; ++i)
195 unw_init_local (&cursor[i].c, &uc[i].uc);
197 delta = (stop - start) / N;
198 if (delta < init_cold)
203 for (j = 0; j < M; ++j)
206 for (i = 0; i < N; ++i)
207 unw_getcontext (&uc[0].uc);
209 delta = (stop - start) / N;
210 if (delta < get_warm)
215 for (j = 0; j < M; ++j)
218 for (i = 0; i < N; ++i)
219 unw_init_local (&cursor[0].c, &uc[0].uc);
221 delta = (stop - start) / N;
222 if (delta < init_warm)
226 printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
227 1e9 * get_cold, 1e9 * get_warm);
228 printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
229 1e9 * init_cold, 1e9 * init_warm);
233 main (int argc, char **argv)
237 rlim.rlim_cur = RLIM_INFINITY;
238 rlim.rlim_max = RLIM_INFINITY;
239 setrlimit (RLIMIT_STACK, &rlim);
241 memset (big, 0xaa, sizeof (big));
245 maxlevel = atol (argv[1]);
247 iterations = atol (argv[2]);
252 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
255 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
256 doit ("global cache ");
258 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
259 doit ("per-thread cache");