Index: MicroBenchmarks/XRay/CMakeLists.txt =================================================================== --- MicroBenchmarks/XRay/CMakeLists.txt +++ MicroBenchmarks/XRay/CMakeLists.txt @@ -1,23 +1,2 @@ -check_cxx_compiler_flag(-fxray-instrument COMPILER_HAS_FXRAY_INSTRUMENT) -if(ARCH STREQUAL "x86" AND COMPILER_HAS_FXRAY_INSTRUMENT) - file(COPY lit.local.cfg DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - - list(APPEND CPPFLAGS -std=c++11 -Wl,--gc-sections -fxray-instrument) - list(APPEND LDFLAGS -fxray-instrument) - llvm_test_run(--benchmark_filter=dummy_skip_ignore) - llvm_test_executable(retref-bench retref-bench.cc) - target_link_libraries(retref-bench benchmark) - - file(COPY retref-bench_BM_ReturnNeverInstrumented.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedUnPatched.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatchedThenUnpatched.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatched.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_RDTSCP_Cost.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatchedWithLogHandler.test - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -endif() +add_subdirectory(ReturnReference) +add_subdirectory(FDRMode) Index: MicroBenchmarks/XRay/FDRMode/CMakeLists.txt =================================================================== --- MicroBenchmarks/XRay/FDRMode/CMakeLists.txt +++ MicroBenchmarks/XRay/FDRMode/CMakeLists.txt @@ -5,19 +5,19 @@ list(APPEND CPPFLAGS -std=c++11 -Wl,--gc-sections -fxray-instrument) list(APPEND LDFLAGS -fxray-instrument) llvm_test_run(--benchmark_filter=dummy_skip_ignore) - llvm_test_executable(retref-bench retref-bench.cc) - target_link_libraries(retref-bench benchmark) + llvm_test_executable(fdrmode-bench fdrmode-bench.cc) + target_link_libraries(fdrmode-bench benchmark) - file(COPY retref-bench_BM_ReturnNeverInstrumented.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_1_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedUnPatched.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_2_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatchedThenUnpatched.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_4_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatched.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_8_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_RDTSCP_Cost.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_16_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY retref-bench_BM_ReturnInstrumentedPatchedWithLogHandler.test + file(COPY fdrmode-bench_BM_XRayFDRMultiThreaded_32_thread.test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) endif() Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench.cc =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench.cc @@ -0,0 +1,86 @@ +//===- fdrmode-bench.cc - XRay Instrumentation Benchmarks ------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Define benchmarks for measuring the cost of XRay instrumentation when using +// flight data recorder mode. +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include "benchmark/benchmark.h" +#include "xray/xray_interface.h" +#include "xray/xray_log_interface.h" + +namespace { + +// This needs to be a global value so that the compiler doesn't end up writing +// to data on the stack. We also mark it volatile to preserve all the loads and +// stores performed on this variable. +volatile int val = 0; + +__xray::FDRLoggingOptions Options; + +} // namespace + +// We define a multi-threaded benchmark which measures the overall costs +// introduced by the XRay handlers upstream. This will exercise a tight loop +// calling a single function that practically does nothing. +[[clang::xray_always_instrument]] __attribute__((noinline)) __attribute__((weak)) int +EmptyFunction() { + return 0; +} + +volatile bool log_initialized = false; + +// Force installation of the FDR logging implementation upstream, with the +// caveat that we're writing the log out to /dev/null. +[[clang::xray_never_instrument]] void SetUpXRayFDRMultiThreaded( + benchmark::State& state) { + if (!log_initialized) { + Options.ReportErrors = true; + Options.Fd = open("/dev/null", O_WRONLY); + assert((Options.Fd != -1) && "Cannot open /dev/null!"); + __xray_log_init(getpagesize(), 1 << 16, &Options, + sizeof(__xray::FDRLoggingOptions)); + __xray_remove_customevent_handler(); + __xray_patch(); + log_initialized = true; + } +} + +[[clang::xray_never_instrument]] void TearDownXRayFDRMultiThreaded( + benchmark::State& state) { + if (log_initialized) { + log_initialized = false; + __xray_log_finalize(); + __xray_log_flushLog(); + __xray_unpatch(); + } +} + +[[clang::xray_never_instrument]] static void BM_XRayFDRMultiThreaded( + benchmark::State& state) { + if (state.thread_index == 0) { + SetUpXRayFDRMultiThreaded(state); + } + while (state.KeepRunning()) { + val = EmptyFunction(); + benchmark::DoNotOptimize(val); + } + if (state.thread_index == 0) { + TearDownXRayFDRMultiThreaded(state); + } +} + +BENCHMARK(BM_XRayFDRMultiThreaded) + ->ThreadRange(1, 32); // Number of threads + +BENCHMARK_MAIN(); Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_16_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_16_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:16$ Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_1_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_1_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:1$ Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_2_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_2_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:2$ Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_32_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_32_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:32$ Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_4_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_4_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:4$ Index: MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_8_thread.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/FDRMode/fdrmode-bench_BM_XRayFDRMultiThreaded_8_thread.test @@ -0,0 +1 @@ +RUN: %S/fdrmode-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_XRayFDRMultiThreaded/threads:8$ Index: MicroBenchmarks/XRay/FDRMode/lit.local.cfg =================================================================== --- MicroBenchmarks/XRay/FDRMode/lit.local.cfg +++ MicroBenchmarks/XRay/FDRMode/lit.local.cfg @@ -1,4 +1,4 @@ -config.environment['XRAY_OPTIONS'] = 'patch_premain=false xray_naive_log=false' +config.environment['XRAY_OPTIONS'] = 'patch_premain=false xray_naive_log=false xray_fdr_log=true' test_modules = config.test_modules if 'run' in test_modules: # Insert microbenchmark module behind 'run' Index: =================================================================== --- /dev/null +++ /dev/null @@ -1,8 +0,0 @@ -config.environment['XRAY_OPTIONS'] = 'patch_premain=false xray_naive_log=false' -test_modules = config.test_modules -if 'run' in test_modules: - # Insert microbenchmark module behind 'run' - test_modules.insert(test_modules.index('run')+1, 'microbenchmark') - # Timeit results are not useful for microbenchmarks - if 'timeit' in test_modules: - test_modules.remove('timeit') Index: MicroBenchmarks/XRay/retref-bench.cc =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench.cc @@ -1,117 +0,0 @@ -//===- retref-bench.cc - XRay Instrumentation Benchmarks ------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Define benchmarks for measuring the cost of XRay instrumentation (the sleds, -// the trampolines). -// -//===----------------------------------------------------------------------===// - -#include - -#include "benchmark/benchmark.h" -#include "xray/xray_interface.h" - -[[clang::xray_never_instrument]] __attribute__((noinline)) int -neverInstrumented() { - benchmark::ClobberMemory(); - return 0; -} - -[[clang::xray_never_instrument]] static void BM_ReturnNeverInstrumented( - benchmark::State& state) { - while (state.KeepRunning()) { - benchmark::DoNotOptimize(neverInstrumented()); - } -} - -BENCHMARK(BM_ReturnNeverInstrumented); - -[[clang::xray_always_instrument]] __attribute__((noinline)) int -alwaysInstrumented() { - benchmark::ClobberMemory(); - return 0; -} - -[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedUnPatched( - benchmark::State& state) { - __xray_unpatch(); - while (state.KeepRunning()) { - int x; - benchmark::DoNotOptimize(x = alwaysInstrumented()); - benchmark::ClobberMemory(); - } -} - -BENCHMARK(BM_ReturnInstrumentedUnPatched); - - -[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedPatchedThenUnpatched( - benchmark::State& state) { - __xray_patch(); - __xray_unpatch(); - while (state.KeepRunning()) { - int x; - benchmark::DoNotOptimize(x = alwaysInstrumented()); - benchmark::ClobberMemory(); - } -} - -BENCHMARK(BM_ReturnInstrumentedPatchedThenUnpatched); - - -[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedPatched( - benchmark::State& state) { - __xray_patch(); - while (state.KeepRunning()) { - int x; - benchmark::DoNotOptimize(alwaysInstrumented()); - benchmark::ClobberMemory(); - } -} - -BENCHMARK(BM_ReturnInstrumentedPatched); - -[[clang::xray_never_instrument]] static void BM_RDTSCP_Cost( - benchmark::State& state) { - while (state.KeepRunning()) { - unsigned cpu; - unsigned tsc; - benchmark::DoNotOptimize(tsc = __rdtscp(&cpu)); - benchmark::ClobberMemory(); - } -} - -volatile unsigned global_cpu; -volatile unsigned tsc; -[[clang::xray_never_instrument]] void benchmark_handler(int32_t, - XRayEntryType) { - unsigned cpu; - benchmark::DoNotOptimize(tsc = __rdtscp(&cpu)); - global_cpu = cpu; - benchmark::ClobberMemory(); -} - -BENCHMARK(BM_RDTSCP_Cost); - -[[clang::xray_never_instrument]] static void -BM_ReturnInstrumentedPatchedWithLogHandler(benchmark::State& state) { - __xray_set_handler(benchmark_handler); - __xray_patch(); - benchmark::ClobberMemory(); - while (state.KeepRunning()) { - int x; - benchmark::DoNotOptimize(x = alwaysInstrumented()); - benchmark::ClobberMemory(); - } - __xray_remove_handler(); -} - -BENCHMARK(BM_ReturnInstrumentedPatchedWithLogHandler); - -BENCHMARK_MAIN(); Index: MicroBenchmarks/XRay/retref-bench_BM_RDTSCP_Cost.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_RDTSCP_Cost.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_RDTSCP_Cost Index: MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatched.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatched.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_ReturnInstrumentedPatched Index: MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatchedThenUnpatched.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatchedThenUnpatched.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_ReturnInstrumentedPatchedThenUnpatched Index: MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatchedWithLogHandler.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedPatchedWithLogHandler.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_ReturnInstrumentedPatchedWithLogHandler Index: MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedUnPatched.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_ReturnInstrumentedUnPatched.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_ReturnInstrumentedUnPatched Index: MicroBenchmarks/XRay/retref-bench_BM_ReturnNeverInstrumented.test =================================================================== --- /dev/null +++ MicroBenchmarks/XRay/retref-bench_BM_ReturnNeverInstrumented.test @@ -1 +0,0 @@ -RUN: %S/retref-bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=BM_ReturnNeverInstrumented