Skip to content

Commit f328f8a

Browse files
committed
tune tcmalloc for benchmarks
1 parent 4e603ca commit f328f8a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/workerd/tests/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ load("//:build/wd_test.bzl", "wd_test")
66
wd_cc_library(
77
name = "bench-tools",
88
hdrs = ["bench-tools.h"],
9+
local_defines = select({
10+
"//src/workerd/server:really_use_tcmalloc": ["WD_USE_TCMALLOC"],
11+
"//conditions:default": [],
12+
}),
913
tags = ["workerd-benchmark"],
1014
visibility = ["//visibility:public"],
1115
deps = [
1216
"@capnp-cpp//src/kj:kj-test",
1317
"@google_benchmark//:benchmark",
14-
],
18+
] + select({
19+
# tcmalloc is only available on Linux and when use_tcmalloc is enabled.
20+
# We use the malloc_extension API to configure tcmalloc for deterministic benchmarks.
21+
"//src/workerd/server:really_use_tcmalloc": ["@tcmalloc//tcmalloc:malloc_extension"],
22+
"//conditions:default": [],
23+
}),
1524
)
1625

1726
wd_cc_library(

src/workerd/tests/bench-tools.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
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+
// WD_USE_TCMALLOC is defined when tcmalloc is enabled (Linux + use_tcmalloc flag).
16+
#if defined(WD_USE_TCMALLOC) && defined(WD_IS_BENCHMARK)
17+
#include "tcmalloc/malloc_extension.h"
18+
19+
namespace workerd::bench {
20+
21+
struct TcmallocBenchmarkConfig {
22+
TcmallocBenchmarkConfig() {
23+
// Disable heap profiling sampling by setting interval to max value.
24+
// Default is ~512KB which causes probabilistic sampling of allocations.
25+
tcmalloc::MallocExtension::SetProfileSamplingInterval(std::numeric_limits<int64_t>::max());
26+
27+
// Disable GWP-ASan guarded sampling. A negative value disables it.
28+
tcmalloc::MallocExtension::SetGuardedSamplingInterval(-1);
29+
30+
// Disable background memory release actions that can cause timing variance.
31+
tcmalloc::MallocExtension::SetBackgroundProcessActionsEnabled(false);
32+
}
33+
};
34+
35+
// Global instance ensures configuration runs before main().
36+
inline TcmallocBenchmarkConfig tcmallocBenchmarkConfig;
37+
38+
} // namespace workerd::bench
39+
#endif // defined(WD_USE_TCMALLOC) && defined(WD_IS_BENCHMARK)
40+
1341
// Define a benchmark. Use microseconds instead of nanoseconds by default, most tests run long
1442
// enough to not need ns precision.
1543
#define WD_BENCHMARK(X) BENCHMARK(X)->Unit(benchmark::kMicrosecond)

0 commit comments

Comments
 (0)