blob: dfc66d9516df0f27891cd2c56cdb4c8ba7b96f21 [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>
27
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000028#include <time.h>
mostang.com!davidm12876ef2003-09-24 05:02:14 +000029
30#include <libunwind.h>
31
32#define panic(args...) \
33 do { fprintf (stderr, args); exit (-1); } while (0)
34
mostang.com!davidm96c62502004-03-27 09:25:58 +000035long dummy;
36
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000037static long iterations = 10000;
hp.com!davidm5e446b22003-11-27 06:52:54 +000038static int maxlevel = 100;
39
mostang.com!davidm96c62502004-03-27 09:25:58 +000040#define MB (1024*1024)
41
42static char big[128*MB];
43
mostang.com!davidm12876ef2003-09-24 05:02:14 +000044static inline double
45gettime (void)
46{
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000047 struct timespec ts;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000048
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000049 clock_gettime(CLOCK_REALTIME, &ts);
50 return ts.tv_sec + 1e-9*ts.tv_nsec;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000051}
52
53static int
mostang.com!davidm96c62502004-03-27 09:25:58 +000054measure_unwind (int maxlevel, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000055{
mostang.com!davidm96c62502004-03-27 09:25:58 +000056 double stop, start;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000057 unw_cursor_t cursor;
58 unw_context_t uc;
59 int ret, level = 0;
60
mostang.com!davidm12876ef2003-09-24 05:02:14 +000061 unw_getcontext (&uc);
62 if (unw_init_local (&cursor, &uc) < 0)
63 panic ("unw_init_local() failed\n");
64
mostang.com!davidm96c62502004-03-27 09:25:58 +000065 start = gettime ();
mostang.com!davidm12876ef2003-09-24 05:02:14 +000066
67 do
68 {
69 ret = unw_step (&cursor);
70 if (ret < 0)
71 panic ("unw_step() failed\n");
72 ++level;
73 }
74 while (ret > 0);
75
76 stop = gettime ();
77
78 if (level <= maxlevel)
79 panic ("Unwound only %d levels, expected at least %d levels",
80 level, maxlevel);
81
mostang.com!davidm96c62502004-03-27 09:25:58 +000082 *step = (stop - start) / (double) level;
hp.com!davidm29f2f892003-09-25 05:29:14 +000083 return 0;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000084}
85
86static int
mostang.com!davidm96c62502004-03-27 09:25:58 +000087f1 (int level, int maxlevel, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000088{
89 if (level == maxlevel)
mostang.com!davidm96c62502004-03-27 09:25:58 +000090 return measure_unwind (maxlevel, step);
mostang.com!davidm12876ef2003-09-24 05:02:14 +000091 else
92 /* defeat last-call/sibcall optimization */
mostang.com!davidm96c62502004-03-27 09:25:58 +000093 return f1 (level + 1, maxlevel, step) + level;
hp.com!davidm5e446b22003-11-27 06:52:54 +000094}
95
96static void
97doit (const char *label)
98{
mostang.com!davidm96c62502004-03-27 09:25:58 +000099 double step, min_step, first_step, sum_step;
hp.com!davidm5e446b22003-11-27 06:52:54 +0000100 int i;
101
mostang.com!davidm96c62502004-03-27 09:25:58 +0000102 sum_step = first_step = 0.0;
103 min_step = 1e99;
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000104 for (i = 0; i < iterations; ++i)
hp.com!davidm5e446b22003-11-27 06:52:54 +0000105 {
mostang.com!davidm96c62502004-03-27 09:25:58 +0000106 f1 (0, maxlevel, &step);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000107
hp.com!davidm5e446b22003-11-27 06:52:54 +0000108 sum_step += step;
109
hp.com!davidm5e446b22003-11-27 06:52:54 +0000110 if (step < min_step)
111 min_step = step;
112
113 if (i == 0)
mostang.com!davidm96c62502004-03-27 09:25:58 +0000114 first_step = step;
hp.com!davidm5e446b22003-11-27 06:52:54 +0000115 }
mostang.com!davidm96c62502004-03-27 09:25:58 +0000116 printf ("%s: unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n", label,
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000117 1e9*first_step, 1e9*min_step, 1e9*sum_step/iterations);
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000118}
119
mostang.com!davidm96c62502004-03-27 09:25:58 +0000120static long
121sum (char *buf, size_t size)
122{
123 long s = 0;
124 size_t i;
125
126 for (i = 0; i < size; ++i)
127 s += *buf++;
128 return s;
129}
130
131static void
132measure_init (void)
133{
134# define N 1000
135 double stop, start, getcontext_cold, getcontext_warm, init_cold, init_warm;
136 unw_cursor_t cursor[N];
137 unw_context_t uc[N];
138 int i;
139
140 /* Ensure memory is paged in but not in the cache: */
141 memset (cursor, 0, sizeof (cursor));
142 memset (uc, 0, sizeof (uc));
143 dummy = sum (big, sizeof (big));
144
145 start = gettime ();
146 for (i = 0; i < N; ++i)
147 unw_getcontext (&uc[i]);
148 stop = gettime ();
149 getcontext_cold = (stop - start) / N;
150
151 start = gettime ();
152 for (i = 0; i < N; ++i)
153 unw_init_local (&cursor[i], &uc[i]);
154 stop = gettime ();
155 init_cold = (stop - start) / N;
156
157 start = gettime ();
158 for (i = 0; i < N; ++i)
159 unw_getcontext (&uc[0]);
160 stop = gettime ();
161 getcontext_warm = (stop - start) / N;
162
163 start = gettime ();
164 for (i = 0; i < N; ++i)
165 unw_init_local (&cursor[0], &uc[0]);
166 stop = gettime ();
167 init_warm = (stop - start) / N;
168
169 printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
170 1e9 * getcontext_cold, 1e9 * getcontext_warm);
171 printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
172 1e9 * init_cold, 1e9 * init_warm);
173}
174
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000175int
176main (int argc, char **argv)
177{
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000178 if (argc > 1)
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000179 {
180 maxlevel = atol (argv[1]);
181 if (argc > 2)
182 iterations = atol (argv[2]);
183 }
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000184
mostang.com!davidm96c62502004-03-27 09:25:58 +0000185 measure_init ();
186
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000187 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000188 doit ("no cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000189
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000190 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000191 doit ("global cache ");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000192
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000193 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
mostang.com!davidm96c62502004-03-27 09:25:58 +0000194 doit ("per-thread cache");
hp.com!davidm5e446b22003-11-27 06:52:54 +0000195
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000196 return 0;
197}