blob: 4a6d4654dd51c0b4f5dbcb36b90020fd06f685f1 [file] [log] [blame]
mostang.com!davidme1da7182004-01-21 06:36:35 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2003-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
mostang.com!davidm96c62502004-03-27 09:25:58 +000024#include <memory.h>
mostang.com!davidm12876ef2003-09-24 05:02:14 +000025#include <stdio.h>
26#include <stdlib.h>
hp.com!davidm3be31f52004-03-30 22:57:06 +000027#include <unistd.h>
mostang.com!davidm12876ef2003-09-24 05:02:14 +000028
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000029#include <time.h>
mostang.com!davidm12876ef2003-09-24 05:02:14 +000030
31#include <libunwind.h>
32
hp.com!davidm3be31f52004-03-30 22:57:06 +000033#include <sys/resource.h>
34
mostang.com!davidm12876ef2003-09-24 05:02:14 +000035#define panic(args...) \
36 do { fprintf (stderr, args); exit (-1); } while (0)
37
mostang.com!davidm96c62502004-03-27 09:25:58 +000038long dummy;
39
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000040static long iterations = 10000;
hp.com!davidm5e446b22003-11-27 06:52:54 +000041static int maxlevel = 100;
42
hp.com!davidm3be31f52004-03-30 22:57:06 +000043#define KB 1024
mostang.com!davidm96c62502004-03-27 09:25:58 +000044#define MB (1024*1024)
45
hp.com!davidm3be31f52004-03-30 22:57:06 +000046static char big[64*MB]; /* should be >> max. cache size */
mostang.com!davidm96c62502004-03-27 09:25:58 +000047
mostang.com!davidm12876ef2003-09-24 05:02:14 +000048static inline double
49gettime (void)
50{
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000051 struct timespec ts;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000052
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000053 clock_gettime(CLOCK_REALTIME, &ts);
54 return ts.tv_sec + 1e-9*ts.tv_nsec;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000055}
56
57static int
mostang.com!davidm96c62502004-03-27 09:25:58 +000058measure_unwind (int maxlevel, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000059{
mostang.com!davidm96c62502004-03-27 09:25:58 +000060 double stop, start;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000061 unw_cursor_t cursor;
62 unw_context_t uc;
63 int ret, level = 0;
64
mostang.com!davidm12876ef2003-09-24 05:02:14 +000065 unw_getcontext (&uc);
66 if (unw_init_local (&cursor, &uc) < 0)
67 panic ("unw_init_local() failed\n");
68
mostang.com!davidm96c62502004-03-27 09:25:58 +000069 start = gettime ();
mostang.com!davidm12876ef2003-09-24 05:02:14 +000070
71 do
72 {
73 ret = unw_step (&cursor);
74 if (ret < 0)
75 panic ("unw_step() failed\n");
76 ++level;
77 }
78 while (ret > 0);
79
80 stop = gettime ();
81
82 if (level <= maxlevel)
83 panic ("Unwound only %d levels, expected at least %d levels",
84 level, maxlevel);
85
mostang.com!davidm96c62502004-03-27 09:25:58 +000086 *step = (stop - start) / (double) level;
hp.com!davidm29f2f892003-09-25 05:29:14 +000087 return 0;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000088}
89
90static int
mostang.com!davidm96c62502004-03-27 09:25:58 +000091f1 (int level, int maxlevel, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000092{
93 if (level == maxlevel)
mostang.com!davidm96c62502004-03-27 09:25:58 +000094 return measure_unwind (maxlevel, step);
mostang.com!davidm12876ef2003-09-24 05:02:14 +000095 else
96 /* defeat last-call/sibcall optimization */
mostang.com!davidm96c62502004-03-27 09:25:58 +000097 return f1 (level + 1, maxlevel, step) + level;
hp.com!davidm5e446b22003-11-27 06:52:54 +000098}
99
100static void
101doit (const char *label)
102{
mostang.com!davidm96c62502004-03-27 09:25:58 +0000103 double step, min_step, first_step, sum_step;
hp.com!davidm5e446b22003-11-27 06:52:54 +0000104 int i;
105
mostang.com!davidm96c62502004-03-27 09:25:58 +0000106 sum_step = first_step = 0.0;
107 min_step = 1e99;
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000108 for (i = 0; i < iterations; ++i)
hp.com!davidm5e446b22003-11-27 06:52:54 +0000109 {
mostang.com!davidm96c62502004-03-27 09:25:58 +0000110 f1 (0, maxlevel, &step);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000111
hp.com!davidm5e446b22003-11-27 06:52:54 +0000112 sum_step += step;
113
hp.com!davidm5e446b22003-11-27 06:52:54 +0000114 if (step < min_step)
115 min_step = step;
116
117 if (i == 0)
mostang.com!davidm96c62502004-03-27 09:25:58 +0000118 first_step = step;
hp.com!davidm5e446b22003-11-27 06:52:54 +0000119 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000120 printf ("%s: unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n", label,
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000121 1e9*first_step, 1e9*min_step, 1e9*sum_step/iterations);
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000122}
123
mostang.com!davidm96c62502004-03-27 09:25:58 +0000124static long
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000125sum (void *buf, size_t size)
mostang.com!davidm96c62502004-03-27 09:25:58 +0000126{
127 long s = 0;
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000128 char *cp = buf;
mostang.com!davidm96c62502004-03-27 09:25:58 +0000129 size_t i;
130
hp.com!davidm3be31f52004-03-30 22:57:06 +0000131 for (i = 0; i < size; i += 8)
132 s += cp[i];
mostang.com!davidm96c62502004-03-27 09:25:58 +0000133 return s;
134}
135
136static void
137measure_init (void)
138{
hp.com!davidm3be31f52004-03-30 22:57:06 +0000139# define N 100
140# define M 10 /* must be at least 2 to get steady-state */
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000141 double stop, start, get_cold, get_warm, init_cold, init_warm, delta;
hp.com!davidm3be31f52004-03-30 22:57:06 +0000142 struct
143 {
144 unw_cursor_t c;
145 char padding[1024]; /* should be > 2 * max. cacheline size */
146 }
147 cursor[N];
148 struct
149 {
150 unw_context_t uc;
151 char padding[1024]; /* should be > 2 * max. cacheline size */
152 }
153 uc[N];
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000154 int i, j;
mostang.com!davidm96c62502004-03-27 09:25:58 +0000155
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000156 /* Run each test M times and take the minimum to filter out noise
157 such dynamic linker resolving overhead, context-switches,
158 page-in, cache, and TLB effects. */
mostang.com!davidm96c62502004-03-27 09:25:58 +0000159
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000160 get_cold = 1e99;
161 for (j = 0; j < M; ++j)
162 {
hp.com!davidm3be31f52004-03-30 22:57:06 +0000163 dummy += sum (big, sizeof (big)); /* flush the cache */
164 for (i = 0; i < N; ++i)
165 uc[i].padding[511] = i; /* warm up the TLB */
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000166 start = gettime ();
167 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000168 unw_getcontext (&uc[i].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000169 stop = gettime ();
170 delta = (stop - start) / N;
171 if (delta < get_cold)
172 get_cold = delta;
173 }
hp.com!davidm3be31f52004-03-30 22:57:06 +0000174
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000175 init_cold = 1e99;
176 for (j = 0; j < M; ++j)
177 {
178 dummy += sum (big, sizeof (big)); /* flush cache */
hp.com!davidm3be31f52004-03-30 22:57:06 +0000179 for (i = 0; i < N; ++i)
180 uc[i].padding[511] = i; /* warm up the TLB */
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000181 start = gettime ();
182 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000183 unw_init_local (&cursor[i].c, &uc[i].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000184 stop = gettime ();
185 delta = (stop - start) / N;
186 if (delta < init_cold)
187 init_cold = delta;
188 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000189
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000190 get_warm = 1e99;
191 for (j = 0; j < M; ++j)
192 {
193 start = gettime ();
194 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000195 unw_getcontext (&uc[0].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000196 stop = gettime ();
197 delta = (stop - start) / N;
198 if (delta < get_warm)
199 get_warm = delta;
200 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000201
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000202 init_warm = 1e99;
203 for (j = 0; j < M; ++j)
204 {
205 start = gettime ();
206 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000207 unw_init_local (&cursor[0].c, &uc[0].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000208 stop = gettime ();
209 delta = (stop - start) / N;
210 if (delta < init_warm)
211 init_warm = delta;
212 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000213
214 printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000215 1e9 * get_cold, 1e9 * get_warm);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000216 printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
217 1e9 * init_cold, 1e9 * init_warm);
218}
219
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000220int
221main (int argc, char **argv)
222{
hp.com!davidm3be31f52004-03-30 22:57:06 +0000223 struct rlimit rlim;
224
225 rlim.rlim_cur = RLIM_INFINITY;
226 rlim.rlim_max = RLIM_INFINITY;
227 setrlimit (RLIMIT_STACK, &rlim);
228
229 memset (big, 0xaa, sizeof (big));
230
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000231 if (argc > 1)
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000232 {
233 maxlevel = atol (argv[1]);
234 if (argc > 2)
235 iterations = atol (argv[2]);
236 }
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000237
mostang.com!davidm96c62502004-03-27 09:25:58 +0000238 measure_init ();
239
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000240 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000241 doit ("no cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000242
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000243 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000244 doit ("global cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000245
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000246 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000247 doit ("per-thread cache");
hp.com!davidm5e446b22003-11-27 06:52:54 +0000248
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000249 return 0;
250}