Skip to content

Commit f97f100

Browse files
ckennellycopybara-github
authored andcommitted
Add microbenchmark covering hooked new/delete.
PiperOrigin-RevId: 770235127 Change-Id: I2c2063336fb80dfa097355295d0cbbbf61f43e0b
1 parent d892274 commit f97f100

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

tcmalloc/testing/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ create_tcmalloc_benchmark_suite(
9393
],
9494
deps = [
9595
"//tcmalloc:malloc_extension",
96+
"//tcmalloc:malloc_hook",
9697
"//tcmalloc/internal:declarations",
9798
"@com_github_google_benchmark//:benchmark",
9899
"@com_google_absl//absl/base:core_headers",
100+
"@com_google_absl//absl/log:check",
99101
"@com_google_absl//absl/random",
100102
"@com_google_absl//absl/synchronization",
101103
"@com_google_absl//absl/time",

tcmalloc/testing/tcmalloc_benchmark.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <vector>
2727

2828
#include "absl/base/attributes.h"
29+
#include "absl/log/check.h"
2930
#include "absl/random/random.h"
3031
#include "absl/synchronization/notification.h"
3132
#include "absl/time/clock.h"
@@ -34,6 +35,8 @@
3435
#include "tcmalloc/common.h"
3536
#include "tcmalloc/internal/declarations.h"
3637
#include "tcmalloc/malloc_extension.h"
38+
#include "tcmalloc/malloc_hook.h"
39+
#include "tcmalloc/malloc_hook_invoke.h"
3740

3841
extern "C" ABSL_ATTRIBUTE_WEAK void MallocExtension_Internal_GetStats(
3942
std::string* ret);
@@ -70,13 +73,49 @@ BENCHMARK_TEMPLATE(BM_new_delete_fixed, 4096);
7073
static void BM_new_sized_delete(benchmark::State& state) {
7174
const int arg = state.range(0);
7275

76+
CHECK_EQ(tcmalloc_internal::new_hooks_.size(), 0);
77+
CHECK_EQ(tcmalloc_internal::delete_hooks_.size(), 0);
7378
for (auto s : state) {
7479
void* ptr = ::operator new(arg);
7580
::operator delete(ptr, arg);
7681
}
7782
}
7883
BENCHMARK(BM_new_sized_delete)->Range(1, 1 << 20);
7984

85+
static void BM_hooked_new_sized_delete(benchmark::State& state) {
86+
const int arg = state.range(0);
87+
88+
auto new_hook = [](const MallocHook::NewInfo& info) {
89+
benchmark::DoNotOptimize(info.ptr);
90+
};
91+
CHECK(MallocHook::AddNewHook(new_hook));
92+
93+
for (auto s : state) {
94+
void* ptr = ::operator new(arg);
95+
::operator delete(ptr, arg);
96+
}
97+
98+
CHECK(MallocHook::RemoveNewHook(new_hook));
99+
}
100+
BENCHMARK(BM_hooked_new_sized_delete)->Range(1, 1 << 20);
101+
102+
static void BM_new_hooked_sized_delete(benchmark::State& state) {
103+
const int arg = state.range(0);
104+
105+
auto delete_hook = [](const MallocHook::DeleteInfo& info) {
106+
benchmark::DoNotOptimize(info.ptr);
107+
};
108+
CHECK(MallocHook::AddDeleteHook(delete_hook));
109+
110+
for (auto s : state) {
111+
void* ptr = ::operator new(arg);
112+
::operator delete(ptr, arg);
113+
}
114+
115+
CHECK(MallocHook::RemoveDeleteHook(delete_hook));
116+
}
117+
BENCHMARK(BM_new_hooked_sized_delete)->Range(1, 1 << 20);
118+
80119
static void BM_size_returning_new_delete(benchmark::State& state) {
81120
const int arg = state.range(0);
82121

0 commit comments

Comments
 (0)