Skip to content

Commit 4d831f6

Browse files
committed
tune tcmalloc for benchmarks
1 parent a39e4c4 commit 4d831f6

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/workerd/tests/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff 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

1722
wd_cc_library(

src/workerd/tests/bench-tools.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@
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)

0 commit comments

Comments
 (0)