blob: 7dd1db7cc609e6e1608f0c3688e79b27578866e4 [file] [log] [blame]
mostang.com!davidm12876ef2003-09-24 05:02:14 +00001#include <stdio.h>
2#include <stdlib.h>
3
4#include <sys/time.h>
5
6#include <libunwind.h>
7
8#define panic(args...) \
9 do { fprintf (stderr, args); exit (-1); } while (0)
10
11static inline double
12gettime (void)
13{
14 struct timeval tv;
15
16 gettimeofday(&tv, NULL);
17 return tv.tv_sec + 1e-6*tv.tv_usec;
18}
19
20static int
21measure_unwind (int maxlevel)
22{
23 double stop, mid, start;
24 unw_cursor_t cursor;
25 unw_context_t uc;
26 int ret, level = 0;
27
28 start = gettime ();
29
30 unw_getcontext (&uc);
31 if (unw_init_local (&cursor, &uc) < 0)
32 panic ("unw_init_local() failed\n");
33
34 mid = gettime ();
35
36 do
37 {
38 ret = unw_step (&cursor);
39 if (ret < 0)
40 panic ("unw_step() failed\n");
41 ++level;
42 }
43 while (ret > 0);
44
45 stop = gettime ();
46
47 if (level <= maxlevel)
48 panic ("Unwound only %d levels, expected at least %d levels",
49 level, maxlevel);
50
51 printf ("initialization time = %gnsec, time per unwind-step = %gnsec\n",
52 1e9*(mid - start), 1e9*(stop - mid)/level);
53}
54
55static int
56f1 (int level, int maxlevel)
57{
58 if (level == maxlevel)
59 return measure_unwind (maxlevel);
60 else
61 /* defeat last-call/sibcall optimization */
62 return f1 (level + 1, maxlevel) + level;
63}
64
65int
66main (int argc, char **argv)
67{
68 int i, maxlevel = 100;
69
70 if (argc > 1)
71 maxlevel = atol (argv[1]);
72
hp.com!davidm048d4232003-09-24 21:56:42 +000073 for (i = 0; i < 20; ++i)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000074 f1 (0, maxlevel);
75 return 0;
76}