blob: 7d006b1f86288bcb05acf1ca3f97cb83879fb58b [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
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000027#include <time.h>
mostang.com!davidm12876ef2003-09-24 05:02:14 +000028
29#include <libunwind.h>
30
31#define panic(args...) \
32 do { fprintf (stderr, args); exit (-1); } while (0)
33
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000034static long iterations = 10000;
hp.com!davidm5e446b22003-11-27 06:52:54 +000035static int maxlevel = 100;
36
mostang.com!davidm12876ef2003-09-24 05:02:14 +000037static inline double
38gettime (void)
39{
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000040 struct timespec ts;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000041
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000042 clock_gettime(CLOCK_REALTIME, &ts);
43 return ts.tv_sec + 1e-9*ts.tv_nsec;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000044}
45
46static int
hp.com!davidm5e446b22003-11-27 06:52:54 +000047measure_unwind (int maxlevel, double *init, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000048{
49 double stop, mid, start;
50 unw_cursor_t cursor;
51 unw_context_t uc;
52 int ret, level = 0;
53
54 start = gettime ();
55
56 unw_getcontext (&uc);
57 if (unw_init_local (&cursor, &uc) < 0)
58 panic ("unw_init_local() failed\n");
59
60 mid = gettime ();
61
62 do
63 {
64 ret = unw_step (&cursor);
65 if (ret < 0)
66 panic ("unw_step() failed\n");
67 ++level;
68 }
69 while (ret > 0);
70
71 stop = gettime ();
72
73 if (level <= maxlevel)
74 panic ("Unwound only %d levels, expected at least %d levels",
75 level, maxlevel);
76
hp.com!davidm5e446b22003-11-27 06:52:54 +000077 *init = mid - start;
78 *step = (stop - mid) / (double) level;
hp.com!davidm29f2f892003-09-25 05:29:14 +000079 return 0;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000080}
81
82static int
hp.com!davidm5e446b22003-11-27 06:52:54 +000083f1 (int level, int maxlevel, double *init, double *step)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000084{
85 if (level == maxlevel)
hp.com!davidm5e446b22003-11-27 06:52:54 +000086 return measure_unwind (maxlevel, init, step);
mostang.com!davidm12876ef2003-09-24 05:02:14 +000087 else
88 /* defeat last-call/sibcall optimization */
hp.com!davidm5e446b22003-11-27 06:52:54 +000089 return f1 (level + 1, maxlevel, init, step) + level;
90}
91
92static void
93doit (const char *label)
94{
mostang.com!davidmc7fdc722004-03-20 09:54:28 +000095 double init, step, min_init, first_init, min_step, first_step;
96 double sum_init, sum_step;
hp.com!davidm5e446b22003-11-27 06:52:54 +000097 int i;
98
99 sum_init = sum_step = first_init = first_step = 0.0;
100 min_init = min_step = 1e99;
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000101 for (i = 0; i < iterations; ++i)
hp.com!davidm5e446b22003-11-27 06:52:54 +0000102 {
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"
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000120 " unw_{getcontext+init_local}: 1st=%9.3f min=%9.3f avg=%9.3f nsec\n"
121 " unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n",
hp.com!davidm5e446b22003-11-27 06:52:54 +0000122 label,
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000123 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)
mostang.com!davidmc7fdc722004-03-20 09:54:28 +0000131 {
132 maxlevel = atol (argv[1]);
133 if (argc > 2)
134 iterations = atol (argv[2]);
135 }
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000136
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000137 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000138 doit ("Caching: none");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000139
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000140 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000141 doit ("Caching: global");
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000142
mostang.com!davidm7a346fb2003-11-20 01:10:03 +0000143 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
hp.com!davidm5e446b22003-11-27 06:52:54 +0000144 doit ("Caching: per-thread");
145
mostang.com!davidm12876ef2003-09-24 05:02:14 +0000146 return 0;
147}