added stacktrace capturing benchmark

This commit is contained in:
Aliaksey Kandratsenka 2017-05-29 13:07:39 -07:00
parent c571ae2fc9
commit 5ac82ec5b9
4 changed files with 157 additions and 0 deletions

2
.gitignore vendored
View File

@ -143,3 +143,5 @@
/test-driver
/thread_dealloc_unittest
/thread_dealloc_unittest.exe
/unwind_bench
/unwind_bench.exe

View File

@ -874,6 +874,12 @@ malloc_bench_shared_full_CXXFLAGS = $(PTHREAD_CFLAGS) $(AM_CXXFLAGS) $(NO_BUILTI
malloc_bench_shared_full_LDFLAGS = $(PTHREAD_CFLAGS) $(TCMALLOC_FLAGS)
malloc_bench_shared_full_LDADD = librun_benchmark.la libtcmalloc.la $(PTHREAD_LIBS)
noinst_PROGRAMS += unwind_bench
unwind_bench_SOURCES = benchmark/unwind_bench.cc benchmark/getcontext_light.cc
unwind_bench_CXXFLAGS = $(PTHREAD_CFLAGS) $(AM_CXXFLAGS) $(NO_BUILTIN_CXXFLAGS)
unwind_bench_LDFLAGS = $(PTHREAD_CFLAGS) $(TCMALLOC_FLAGS)
unwind_bench_LDADD = librun_benchmark.la libtcmalloc.la $(PTHREAD_LIBS)
endif WITH_HEAP_PROFILER_OR_CHECKER
binary_trees_SOURCES = benchmark/binary_trees.cc

View File

@ -0,0 +1,60 @@
#include <ucontext.h>
#include <stddef.h>
extern "C" void getcontext_light(ucontext_t *ctx);
// clang's built-in asm cannot handle .set directives
#if defined(__GNUC__) && !defined(__llvm__) && defined(__x86_64) && defined(_LP64)
#define R(r) offsetof(ucontext_t, uc_mcontext.gregs[r])
static __attribute__((used))
void getcontext_tramp(ucontext_t *ctx) {
__asm__ __volatile__(".set oRBX, %c0\n"
".set oRBP, %c1\n"
".set oR12, %c2\n"
".set oR13, %c3\n"
".set oR14, %c4\n"
".set oR15, %c5\n"
".set oRIP, %c6\n"
".set oRSP, %c7\n"
: :
"p" (R(REG_RBX)),
"p" (R(REG_RBP)),
"p" (R(REG_R12)),
"p" (R(REG_R13)),
"p" (R(REG_R14)),
"p" (R(REG_R15)),
"p" (R(REG_RIP)),
"p" (R(REG_RSP)));
getcontext_light(ctx);
}
__asm__(".pushsection .text; .globl getcontext_light\n"
".type getcontext_light, @function\n"
"getcontext_light:\n"
"\t.cfi_startproc\n"
"\tmovq %rbx, oRBX(%rdi)\n"
"\tmovq %rbp, oRBP(%rdi)\n"
"\tmovq %r12, oR12(%rdi)\n"
"\tmovq %r13, oR13(%rdi)\n"
"\tmovq %r14, oR14(%rdi)\n"
"\tmovq %r15, oR14(%rdi)\n"
"\tmovq (%rsp), %rcx\n"
"\tmovq %rcx, oRIP(%rdi)\n"
"\tleaq 8(%rsp), %rcx\n" /* Exclude the return address. */
"\tmovq %rcx, oRSP(%rdi)\n"
"\tret\n"
".cfi_endproc\n"
".size getcontext_light, .-getcontext_light\n"
".popsection\n"
);
#else
extern "C" void getcontext_light(ucontext_t *ctx) {
getcontext(ctx);
}
#endif

89
benchmark/unwind_bench.cc Normal file
View File

@ -0,0 +1,89 @@
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
#include "config.h"
#include "base/basictypes.h"
#include "gperftools/stacktrace.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "sys/ucontext.h"
#if HAVE_LIBUNWIND_H
#include <libunwind.h>
#endif
#include "run_benchmark.h"
extern "C" void getcontext_light(ucontext_t *ctx);
#define MAX_FRAMES 1024
static void *frames[MAX_FRAMES];
enum measure_mode {
MODE_NOOP,
MODE_WITH_CONTEXT,
MODE_WITHOUT_CONTEXT
};
static int ATTRIBUTE_NOINLINE measure_unwind(int maxlevel, int mode) {
int n;
if (mode == MODE_NOOP)
return 0;
if (mode == MODE_WITH_CONTEXT) {
ucontext_t uc;
getcontext_light(&uc);
n = GetStackTraceWithContext(frames, MAX_FRAMES, 0, &uc);
} else {
n = GetStackTrace(frames, MAX_FRAMES, 0);
}
if (n < maxlevel) {
fprintf(stderr, "Expected at least %d frames, got %d\n", maxlevel, n);
abort();
}
return 0;
}
static int ATTRIBUTE_NOINLINE frame_forcer(int rv) {
return rv;
}
static int ATTRIBUTE_NOINLINE f1(int level, int maxlevel, int mode) {
if (level == maxlevel)
return frame_forcer(measure_unwind(maxlevel, mode));
return frame_forcer(f1(level + 1, maxlevel, mode));
}
static void bench_unwind_no_op(long iterations, uintptr_t param) {
do {
f1(0, param, MODE_NOOP);
iterations -= param;
} while (iterations > 0);
}
static void bench_unwind_context(long iterations, uintptr_t param) {
do {
f1(0, param, MODE_WITH_CONTEXT);
iterations -= param;
} while (iterations > 0);
}
static void bench_unwind_no_context(long iterations, uintptr_t param) {
do {
f1(0, param, MODE_WITHOUT_CONTEXT);
iterations -= param;
} while (iterations > 0);
}
int main(void) {
report_benchmark("unwind_no_op", bench_unwind_no_op, 100);
report_benchmark("unwind_context", bench_unwind_context, 100);
report_benchmark("unwind_no_context", bench_unwind_no_context, 100);
//// TODO: somehow this fails at linking step. Figure out why this is missing
// #if HAVE_LIBUNWIND_H
// unw_set_caching_policy(unw_local_addr_space, UNW_CACHE_PER_THREAD);
// #endif
return 0;
}