blob: a62c20a34663ff21c838164e6d93dfcd2c79222e [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!davidm12876ef2003-09-24 05:02:14 +000024#include <stdio.h>
25#include <stdlib.h>
26
27#include <sys/time.h>
28
29#include <libunwind.h>
30
hp.com!davidm5e446b22003-11-27 06:52:54 +000031#define ITERATIONS 10000
32
mostang.com!davidm12876ef2003-09-24 05:02:14 +000033#define panic(args...) \
34 do { fprintf (stderr, args); exit (-1); } while (0)
35
hp.com!davidm5e446b22003-11-27 06:52:54 +000036static int maxlevel = 100;
37
mostang.com!davidm12876ef2003-09-24 05:02:14 +000038static inline double
39gettime (void)
40{
41 struct timeval tv;
42
43 gettimeofday(&tv, NULL);
44 return tv.tv_sec + 1e-6*tv.tv_usec;
45}
46
47static int
hp.com!davidm5e446b22003-11-27 06:52:54 +000048measure_unwind (int maxlevel, double *init, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000049{
50 double stop, mid, start;
51 unw_cursor_t cursor;
52 unw_context_t uc;
53 int ret, level = 0;
54
55 start = gettime ();
56
57 unw_getcontext (&uc);
58 if (unw_init_local (&cursor, &uc) < 0)
59 panic ("unw_init_local() failed\n");
60
61 mid = gettime ();
62
63 do
64 {
65 ret = unw_step (&cursor);
66 if (ret < 0)
67 panic ("unw_step() failed\n");
68 ++level;
69 }
70 while (ret > 0);
71
72 stop = gettime ();
73
74 if (level <= maxlevel)
75 panic ("Unwound only %d levels, expected at least %d levels",
76 level, maxlevel);
77
hp.com!davidm5e446b22003-11-27 06:52:54 +000078 *init = mid - start;
79 *step = (stop - mid) / (double) level;
hp.com!davidm29f2f892003-09-25 05:29:14 +000080 return 0;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000081}
82
83static int
hp.com!davidm5e446b22003-11-27 06:52:54 +000084f1 (int level, int maxlevel, double *init, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000085{
86 if (level == maxlevel)
hp.com!davidm5e446b22003-11-27 06:52:54 +000087 return measure_unwind (maxlevel, init, step);
mostang.com!davidm12876ef2003-09-24 05:02:14 +000088 else
89 /* defeat last-call/sibcall optimization */
hp.com!davidm5e446b22003-11-27 06:52:54 +000090 return f1 (level + 1, maxlevel, init, step) + level;
91}
92
93static void
94doit (const char *label)
95{
96 double init, step, min_init, first_init, min_step, first_step, sum_init, sum_step;
97 int i;
98
99 sum_init = sum_step = first_init = first_step = 0.0;
100 min_init = min_step = 1e99;
101 for (i = 0; i < ITERATIONS; ++i)
102 {
103 f1 (0, maxlevel, &init, &step);
104
105 sum_init += init;
106 sum_step += step;
107
108 if (init < min_init)
109 min_init = init;
110 if (step < min_step)
111 min_step = step;
112
113 if (i == 0)
114 {
115 first_init = init;
116 first_step = step;
117 }
118 }
119 printf ("%s:\n"
120 " unw_{getcontext+init_local}: first=%9.3f min=%9.3f avg=%9.3f nsec\n"
121 " unw_step : first=%9.3f min=%9.3f avg=%9.3f nsec\n",
122 label,
123 1e9*first_init, 1e9*min_init, 1e9*sum_init/ITERATIONS,
124 1e9*first_step, 1e9*min_step, 1e9*sum_step/ITERATIONS);
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000125}
126
127int
128main (int argc, char **argv)
129{
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000130 if (argc > 1)
131 maxlevel = atol (argv[1]);
132
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000133 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000134 doit ("Caching: none");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000135
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000136 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000137 doit ("Caching: global");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000138
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000139 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000140 doit ("Caching: per-thread");
141
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000142 return 0;
143}