2004-01-21 07:36:35 +01:00
|
|
|
/* libunwind - a platform-independent unwind library
|
|
|
|
Copyright (C) 2003-2004 Hewlett-Packard Co
|
|
|
|
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
#include <memory.h>
|
2003-09-24 07:02:14 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-03-31 00:57:06 +02:00
|
|
|
#include <unistd.h>
|
2003-09-24 07:02:14 +02:00
|
|
|
|
|
|
|
#include <libunwind.h>
|
|
|
|
|
2004-03-31 00:57:06 +02:00
|
|
|
#include <sys/resource.h>
|
2004-03-31 09:29:27 +02:00
|
|
|
#include <sys/time.h>
|
2004-03-31 00:57:06 +02:00
|
|
|
|
2003-09-24 07:02:14 +02:00
|
|
|
#define panic(args...) \
|
|
|
|
do { fprintf (stderr, args); exit (-1); } while (0)
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
long dummy;
|
|
|
|
|
2004-03-20 10:54:28 +01:00
|
|
|
static long iterations = 10000;
|
2003-11-27 07:52:54 +01:00
|
|
|
static int maxlevel = 100;
|
|
|
|
|
2004-03-31 00:57:06 +02:00
|
|
|
#define KB 1024
|
2004-03-27 10:25:58 +01:00
|
|
|
#define MB (1024*1024)
|
|
|
|
|
2004-03-31 00:57:06 +02:00
|
|
|
static char big[64*MB]; /* should be >> max. cache size */
|
2004-03-27 10:25:58 +01:00
|
|
|
|
2003-09-24 07:02:14 +02:00
|
|
|
static inline double
|
|
|
|
gettime (void)
|
|
|
|
{
|
2004-03-31 03:53:04 +02:00
|
|
|
struct timeval tv;
|
2003-09-24 07:02:14 +02:00
|
|
|
|
2004-03-31 03:53:04 +02:00
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
return tv.tv_sec + 1e-6*tv.tv_usec;
|
2003-09-24 07:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-03-27 10:25:58 +01:00
|
|
|
measure_unwind (int maxlevel, double *step)
|
2003-09-24 07:02:14 +02:00
|
|
|
{
|
2004-03-27 10:25:58 +01:00
|
|
|
double stop, start;
|
2003-09-24 07:02:14 +02:00
|
|
|
unw_cursor_t cursor;
|
|
|
|
unw_context_t uc;
|
|
|
|
int ret, level = 0;
|
|
|
|
|
|
|
|
unw_getcontext (&uc);
|
|
|
|
if (unw_init_local (&cursor, &uc) < 0)
|
|
|
|
panic ("unw_init_local() failed\n");
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
start = gettime ();
|
2003-09-24 07:02:14 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = unw_step (&cursor);
|
|
|
|
if (ret < 0)
|
|
|
|
panic ("unw_step() failed\n");
|
|
|
|
++level;
|
|
|
|
}
|
|
|
|
while (ret > 0);
|
|
|
|
|
|
|
|
stop = gettime ();
|
|
|
|
|
|
|
|
if (level <= maxlevel)
|
|
|
|
panic ("Unwound only %d levels, expected at least %d levels",
|
|
|
|
level, maxlevel);
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
*step = (stop - start) / (double) level;
|
2003-09-25 07:29:14 +02:00
|
|
|
return 0;
|
2003-09-24 07:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-03-27 10:25:58 +01:00
|
|
|
f1 (int level, int maxlevel, double *step)
|
2003-09-24 07:02:14 +02:00
|
|
|
{
|
|
|
|
if (level == maxlevel)
|
2004-03-27 10:25:58 +01:00
|
|
|
return measure_unwind (maxlevel, step);
|
2003-09-24 07:02:14 +02:00
|
|
|
else
|
|
|
|
/* defeat last-call/sibcall optimization */
|
2004-03-27 10:25:58 +01:00
|
|
|
return f1 (level + 1, maxlevel, step) + level;
|
2003-11-27 07:52:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
doit (const char *label)
|
|
|
|
{
|
2004-03-27 10:25:58 +01:00
|
|
|
double step, min_step, first_step, sum_step;
|
2003-11-27 07:52:54 +01:00
|
|
|
int i;
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
sum_step = first_step = 0.0;
|
|
|
|
min_step = 1e99;
|
2004-03-20 10:54:28 +01:00
|
|
|
for (i = 0; i < iterations; ++i)
|
2003-11-27 07:52:54 +01:00
|
|
|
{
|
2004-03-27 10:25:58 +01:00
|
|
|
f1 (0, maxlevel, &step);
|
2003-11-27 07:52:54 +01:00
|
|
|
|
|
|
|
sum_step += step;
|
|
|
|
|
|
|
|
if (step < min_step)
|
|
|
|
min_step = step;
|
|
|
|
|
|
|
|
if (i == 0)
|
2004-03-27 10:25:58 +01:00
|
|
|
first_step = step;
|
2003-11-27 07:52:54 +01:00
|
|
|
}
|
2004-03-27 10:25:58 +01:00
|
|
|
printf ("%s: unw_step : 1st=%9.3f min=%9.3f avg=%9.3f nsec\n", label,
|
2004-03-20 10:54:28 +01:00
|
|
|
1e9*first_step, 1e9*min_step, 1e9*sum_step/iterations);
|
2003-09-24 07:02:14 +02:00
|
|
|
}
|
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
static long
|
2004-03-28 01:24:33 +01:00
|
|
|
sum (void *buf, size_t size)
|
2004-03-27 10:25:58 +01:00
|
|
|
{
|
|
|
|
long s = 0;
|
2004-03-28 01:24:33 +01:00
|
|
|
char *cp = buf;
|
2004-03-27 10:25:58 +01:00
|
|
|
size_t i;
|
|
|
|
|
2004-03-31 00:57:06 +02:00
|
|
|
for (i = 0; i < size; i += 8)
|
|
|
|
s += cp[i];
|
2004-03-27 10:25:58 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
measure_init (void)
|
|
|
|
{
|
2004-03-31 00:57:06 +02:00
|
|
|
# define N 100
|
2004-03-31 09:29:27 +02:00
|
|
|
# define M 10 /* must be at least 2 to get steady-state */
|
2004-03-28 01:24:33 +01:00
|
|
|
double stop, start, get_cold, get_warm, init_cold, init_warm, delta;
|
2004-03-31 00:57:06 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
unw_cursor_t c;
|
|
|
|
char padding[1024]; /* should be > 2 * max. cacheline size */
|
|
|
|
}
|
|
|
|
cursor[N];
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
unw_context_t uc;
|
|
|
|
char padding[1024]; /* should be > 2 * max. cacheline size */
|
|
|
|
}
|
|
|
|
uc[N];
|
2004-03-28 01:24:33 +01:00
|
|
|
int i, j;
|
2004-03-27 10:25:58 +01:00
|
|
|
|
2004-03-28 01:24:33 +01:00
|
|
|
/* Run each test M times and take the minimum to filter out noise
|
|
|
|
such dynamic linker resolving overhead, context-switches,
|
|
|
|
page-in, cache, and TLB effects. */
|
2004-03-27 10:25:58 +01:00
|
|
|
|
2004-03-28 01:24:33 +01:00
|
|
|
get_cold = 1e99;
|
|
|
|
for (j = 0; j < M; ++j)
|
|
|
|
{
|
2004-03-31 00:57:06 +02:00
|
|
|
dummy += sum (big, sizeof (big)); /* flush the cache */
|
|
|
|
for (i = 0; i < N; ++i)
|
|
|
|
uc[i].padding[511] = i; /* warm up the TLB */
|
2004-03-28 01:24:33 +01:00
|
|
|
start = gettime ();
|
|
|
|
for (i = 0; i < N; ++i)
|
2004-03-31 00:57:06 +02:00
|
|
|
unw_getcontext (&uc[i].uc);
|
2004-03-28 01:24:33 +01:00
|
|
|
stop = gettime ();
|
|
|
|
delta = (stop - start) / N;
|
|
|
|
if (delta < get_cold)
|
|
|
|
get_cold = delta;
|
|
|
|
}
|
2004-03-31 00:57:06 +02:00
|
|
|
|
2004-03-28 01:24:33 +01:00
|
|
|
init_cold = 1e99;
|
|
|
|
for (j = 0; j < M; ++j)
|
|
|
|
{
|
|
|
|
dummy += sum (big, sizeof (big)); /* flush cache */
|
2004-03-31 00:57:06 +02:00
|
|
|
for (i = 0; i < N; ++i)
|
|
|
|
uc[i].padding[511] = i; /* warm up the TLB */
|
2004-03-28 01:24:33 +01:00
|
|
|
start = gettime ();
|
|
|
|
for (i = 0; i < N; ++i)
|
2004-03-31 00:57:06 +02:00
|
|
|
unw_init_local (&cursor[i].c, &uc[i].uc);
|
2004-03-28 01:24:33 +01:00
|
|
|
stop = gettime ();
|
|
|
|
delta = (stop - start) / N;
|
|
|
|
if (delta < init_cold)
|
|
|
|
init_cold = delta;
|
|
|
|
}
|
2004-03-27 10:25:58 +01:00
|
|
|
|
2004-03-28 01:24:33 +01:00
|
|
|
get_warm = 1e99;
|
|
|
|
for (j = 0; j < M; ++j)
|
|
|
|
{
|
|
|
|
start = gettime ();
|
|
|
|
for (i = 0; i < N; ++i)
|
2004-03-31 00:57:06 +02:00
|
|
|
unw_getcontext (&uc[0].uc);
|
2004-03-28 01:24:33 +01:00
|
|
|
stop = gettime ();
|
|
|
|
delta = (stop - start) / N;
|
|
|
|
if (delta < get_warm)
|
|
|
|
get_warm = delta;
|
|
|
|
}
|
2004-03-27 10:25:58 +01:00
|
|
|
|
2004-03-28 01:24:33 +01:00
|
|
|
init_warm = 1e99;
|
|
|
|
for (j = 0; j < M; ++j)
|
|
|
|
{
|
|
|
|
start = gettime ();
|
|
|
|
for (i = 0; i < N; ++i)
|
2004-03-31 00:57:06 +02:00
|
|
|
unw_init_local (&cursor[0].c, &uc[0].uc);
|
2004-03-28 01:24:33 +01:00
|
|
|
stop = gettime ();
|
|
|
|
delta = (stop - start) / N;
|
|
|
|
if (delta < init_warm)
|
|
|
|
init_warm = delta;
|
|
|
|
}
|
2004-03-27 10:25:58 +01:00
|
|
|
|
|
|
|
printf ("unw_getcontext : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
|
2004-03-28 01:24:33 +01:00
|
|
|
1e9 * get_cold, 1e9 * get_warm);
|
2004-03-27 10:25:58 +01:00
|
|
|
printf ("unw_init_local : cold avg=%9.3f nsec, warm avg=%9.3f nsec\n",
|
|
|
|
1e9 * init_cold, 1e9 * init_warm);
|
|
|
|
}
|
|
|
|
|
2003-09-24 07:02:14 +02:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2004-03-31 00:57:06 +02:00
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
rlim.rlim_cur = RLIM_INFINITY;
|
|
|
|
rlim.rlim_max = RLIM_INFINITY;
|
|
|
|
setrlimit (RLIMIT_STACK, &rlim);
|
|
|
|
|
|
|
|
memset (big, 0xaa, sizeof (big));
|
|
|
|
|
2003-09-24 07:02:14 +02:00
|
|
|
if (argc > 1)
|
2004-03-20 10:54:28 +01:00
|
|
|
{
|
|
|
|
maxlevel = atol (argv[1]);
|
|
|
|
if (argc > 2)
|
|
|
|
iterations = atol (argv[2]);
|
|
|
|
}
|
2003-09-24 07:02:14 +02:00
|
|
|
|
2004-03-27 10:25:58 +01:00
|
|
|
measure_init ();
|
|
|
|
|
2003-11-20 02:10:03 +01:00
|
|
|
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
|
2004-03-27 10:25:58 +01:00
|
|
|
doit ("no cache ");
|
2003-11-20 02:10:03 +01:00
|
|
|
|
|
|
|
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
|
2004-03-27 10:25:58 +01:00
|
|
|
doit ("global cache ");
|
2003-11-20 02:10:03 +01:00
|
|
|
|
|
|
|
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
|
2004-03-27 10:25:58 +01:00
|
|
|
doit ("per-thread cache");
|
2003-11-27 07:52:54 +01:00
|
|
|
|
2003-09-24 07:02:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|