blob: 003c0fcf83c70429b27c079ba990078ac50e283c [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
mostang.com!davidm7a346fb2003-11-20 01:10:03 +000051 printf (" unw_{getcontext+init_local}: %9.3f nsec, unw_step: %9.3f nsec\n",
mostang.com!davidm12876ef2003-09-24 05:02:14 +000052 1e9*(mid - start), 1e9*(stop - mid)/level);
hp.com!davidm29f2f892003-09-25 05:29:14 +000053 return 0;
mostang.com!davidm12876ef2003-09-24 05:02:14 +000054}
55
56static int
57f1 (int level, int maxlevel)
58{
59 if (level == maxlevel)
60 return measure_unwind (maxlevel);
61 else
62 /* defeat last-call/sibcall optimization */
63 return f1 (level + 1, maxlevel) + level;
64}
65
66int
67main (int argc, char **argv)
68{
69 int i, maxlevel = 100;
70
71 if (argc > 1)
72 maxlevel = atol (argv[1]);
73
mostang.com!davidm7a346fb2003-11-20 01:10:03 +000074 printf ("Caching: none\n");
75 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
76 for (i = 0; i < 20; ++i)
77 f1 (0, maxlevel);
78
79 printf ("Caching: global\n");
80 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
81 for (i = 0; i < 20; ++i)
82 f1 (0, maxlevel);
83
84 printf ("Caching: per-thread\n");
85 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
hp.com!davidm048d4232003-09-24 21:56:42 +000086 for (i = 0; i < 20; ++i)
mostang.com!davidm12876ef2003-09-24 05:02:14 +000087 f1 (0, maxlevel);
88 return 0;
89}