File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed
Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,12 @@ wd_cc_library(
1111 deps = [
1212 "@capnp-cpp//src/kj:kj-test" ,
1313 "@google_benchmark//:benchmark" ,
14- ],
14+ ] + select ({
15+ # tcmalloc is only available on Linux. We use the malloc_extension API
16+ # to configure tcmalloc for deterministic benchmarks.
17+ "@platforms//os:linux" : ["@tcmalloc//tcmalloc:malloc_extension" ],
18+ "//conditions:default" : [],
19+ }),
1520)
1621
1722wd_cc_library (
Original file line number Diff line number Diff line change 1010
1111#include < kj/test.h>
1212
13+ // Configure tcmalloc for deterministic benchmarks on Linux.
14+ // tcmalloc uses probabilistic heap sampling which can introduce variance in benchmark results.
15+ // We disable sampling and background actions to make benchmarks more reproducible.
16+ // WD_IS_BENCHMARK is defined by wd_cc_benchmark builds.
17+ #if defined(__linux__) && defined(WD_IS_BENCHMARK)
18+ #include " tcmalloc/malloc_extension.h"
19+
20+ namespace workerd ::bench {
21+
22+ struct TcmallocBenchmarkConfig {
23+ TcmallocBenchmarkConfig () {
24+ // Disable heap profiling sampling by setting interval to max value.
25+ // Default is ~512KB which causes probabilistic sampling of allocations.
26+ tcmalloc::MallocExtension::SetProfileSamplingInterval (std::numeric_limits<int64_t >::max ());
27+
28+ // Disable GWP-ASan guarded sampling. A negative value disables it.
29+ tcmalloc::MallocExtension::SetGuardedSamplingInterval (-1 );
30+
31+ // Disable background memory release actions that can cause timing variance.
32+ tcmalloc::MallocExtension::SetBackgroundProcessActionsEnabled (false );
33+ }
34+ };
35+
36+ // Global instance ensures configuration runs before main().
37+ inline TcmallocBenchmarkConfig tcmallocBenchmarkConfig;
38+
39+ } // namespace workerd::bench
40+ #endif // defined(__linux__) && defined(WD_IS_BENCHMARK)
41+
1342// Define a benchmark. Use microseconds instead of nanoseconds by default, most tests run long
1443// enough to not need ns precision.
1544#define WD_BENCHMARK (X ) BENCHMARK(X)->Unit (benchmark::kMicrosecond )
You can’t perform that action at this time.
0 commit comments