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)
65 level = unw_backtrace(buffer, 128);
68 if (level <= maxlevel)
69 panic ("Unwound only %d levels, expected at least %d levels\n",
72 *step = (stop - start) / (double) level;
76 static int f1 (int, int, double *);
79 g1 (int level, int maxlevel, double *step)
81 if (level == maxlevel)
82 return measure_unwind (maxlevel, step);
84 /* defeat last-call/sibcall optimization */
85 return f1 (level + 1, maxlevel, step) + level;
89 f1 (int level, int maxlevel, double *step)
91 if (level == maxlevel)
92 return measure_unwind (maxlevel, step);
94 /* defeat last-call/sibcall optimization */
95 return g1 (level + 1, maxlevel, step) + level;
99 doit (const char *label)
101 double step, min_step, first_step, sum_step;
104 sum_step = first_step = 0.0;
106 for (i = 0; i < iterations; ++i)
108 f1 (0, maxlevel, &step);
118 printf ("%s: unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n", label,
119 1e9*first_step, 1e9*min_step, 1e9*sum_step/iterations);
123 sum (void *buf, size_t size)
129 for (i = 0; i < size; i += 8)
138 # define M 10 /* must be at least 2 to get steady-state */
139 double stop, start, get_cold, get_warm, init_cold, init_warm, delta;
143 char padding[1024]; /* should be > 2 * max. cacheline size */
149 char padding[1024]; /* should be > 2 * max. cacheline size */
154 /* Run each test M times and take the minimum to filter out noise
155 such dynamic linker resolving overhead, context-switches,
156 page-in, cache, and TLB effects. */
159 for (j = 0; j < M; ++j)
161 dummy += sum (big, sizeof (big)); /* flush the cache */
162 for (i = 0; i < N; ++i)
163 uc[i].padding[511] = i; /* warm up the TLB */
165 for (i = 0; i < N; ++i)
166 unw_getcontext (&uc[i].uc);
168 delta = (stop - start) / N;
169 if (delta < get_cold)
174 for (j = 0; j < M; ++j)
176 dummy += sum (big, sizeof (big)); /* flush cache */
177 for (i = 0; i < N; ++i)
178 uc[i].padding[511] = i; /* warm up the TLB */
180 for (i = 0; i < N; ++i)
181 unw_init_local (&cursor[i].c, &uc[i].uc);
183 delta = (stop - start) / N;
184 if (delta < init_cold)
189 for (j = 0; j < M; ++j)
192 for (i = 0; i < N; ++i)
193 unw_getcontext (&uc[0].uc);
195 delta = (stop - start) / N;
196 if (delta < get_warm)
201 for (j = 0; j < M; ++j)
204 for (i = 0; i < N; ++i)
205 unw_init_local (&cursor[0].c, &uc[0].uc);
207 delta = (stop - start) / N;
208 if (delta < init_warm)
212 printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
213 1e9 * get_cold, 1e9 * get_warm);
214 printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
215 1e9 * init_cold, 1e9 * init_warm);
219 main (int argc, char **argv)
223 rlim.rlim_cur = RLIM_INFINITY;
224 rlim.rlim_max = RLIM_INFINITY;
225 setrlimit (RLIMIT_STACK, &rlim);
227 memset (big, 0xaa, sizeof (big));
231 maxlevel = atol (argv[1]);
233 iterations = atol (argv[2]);
238 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
241 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
242 doit ("global cache ");
244 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
245 doit ("per-thread cache");