blob: 93c95250f2af0c243a83c87f194b87005af1476a [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!davidme3eae742004-03-31 01:53:04 +000051 struct timeval tv;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000052
mostang.com!davidme3eae742004-03-31 01:53:04 +000053 gettimeofday (&tv, NULL);
54 return tv.tv_sec + 1e-6*tv.tv_usec;
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
mostang.com!davidme3eae742004-03-31 01:53:04 +0000140# define M 1000000 /* 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!davidme3eae742004-03-31 01:53:04 +0000156#if 0
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000157 /* Run each test M times and take the minimum to filter out noise
158 such dynamic linker resolving overhead, context-switches,
159 page-in, cache, and TLB effects. */
mostang.com!davidm96c62502004-03-27 09:25:58 +0000160
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000161 get_cold = 1e99;
162 for (j = 0; j < M; ++j)
163 {
hp.com!davidm3be31f52004-03-30 22:57:06 +0000164 dummy += sum (big, sizeof (big)); /* flush the cache */
165 for (i = 0; i < N; ++i)
166 uc[i].padding[511] = i; /* warm up the TLB */
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000167 start = gettime ();
168 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000169 unw_getcontext (&uc[i].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000170 stop = gettime ();
171 delta = (stop - start) / N;
172 if (delta < get_cold)
173 get_cold = delta;
174 }
hp.com!davidm3be31f52004-03-30 22:57:06 +0000175
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000176 init_cold = 1e99;
177 for (j = 0; j < M; ++j)
178 {
179 dummy += sum (big, sizeof (big)); /* flush cache */
hp.com!davidm3be31f52004-03-30 22:57:06 +0000180 for (i = 0; i < N; ++i)
181 uc[i].padding[511] = i; /* warm up the TLB */
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000182 start = gettime ();
183 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000184 unw_init_local (&cursor[i].c, &uc[i].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000185 stop = gettime ();
186 delta = (stop - start) / N;
187 if (delta < init_cold)
188 init_cold = delta;
189 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000190
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000191 get_warm = 1e99;
192 for (j = 0; j < M; ++j)
193 {
194 start = gettime ();
195 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000196 unw_getcontext (&uc[0].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000197 stop = gettime ();
198 delta = (stop - start) / N;
199 if (delta < get_warm)
200 get_warm = delta;
201 }
mostang.com!davidme3eae742004-03-31 01:53:04 +0000202#else
203 unw_getcontext (&uc[0].uc);
204#endif
mostang.com!davidm96c62502004-03-27 09:25:58 +0000205
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000206 init_warm = 1e99;
207 for (j = 0; j < M; ++j)
208 {
209 start = gettime ();
210 for (i = 0; i < N; ++i)
hp.com!davidm3be31f52004-03-30 22:57:06 +0000211 unw_init_local (&cursor[0].c, &uc[0].uc);
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000212 stop = gettime ();
213 delta = (stop - start) / N;
214 if (delta < init_warm)
215 init_warm = delta;
216 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000217
218 printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
mostang.com!davidmfc7b49b2004-03-28 00:24:33 +0000219 1e9 * get_cold, 1e9 * get_warm);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000220 printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
221 1e9 * init_cold, 1e9 * init_warm);
mostang.com!davidme3eae742004-03-31 01:53:04 +0000222 exit (0);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000223}
224
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000225int
226main (int argc, char **argv)
227{
hp.com!davidm3be31f52004-03-30 22:57:06 +0000228 struct rlimit rlim;
229
230 rlim.rlim_cur = RLIM_INFINITY;
231 rlim.rlim_max = RLIM_INFINITY;
232 setrlimit (RLIMIT_STACK, &rlim);
233
234 memset (big, 0xaa, sizeof (big));
235
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000236 if (argc > 1)
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000237 {
238 maxlevel = atol (argv[1]);
239 if (argc > 2)
240 iterations = atol (argv[2]);
241 }
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000242
mostang.com!davidm96c62502004-03-27 09:25:58 +0000243 measure_init ();
244
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000245 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000246 doit ("no cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000247
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000248 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000249 doit ("global cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000250
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000251 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000252 doit ("per-thread cache");
hp.com!davidm5e446b22003-11-27 06:52:54 +0000253
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000254 return 0;
255}