Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a benchmark for the allocation tracker #282

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ add_benchmark(savecontext-bench savecontext-bench.cc ../src/lib/pthread_fixes.cc

add_benchmark(timer-bench timer-bench.cc ../src/timer.cc ../src/perf.cc)

add_benchmark(
allocation_tracker-bench
allocation_tracker-bench.cc
../src/lib/allocation_tracker.cc
../src/pevent_lib.cc
../src/perf.cc
../src/perf_ringbuffer.cc
../src/perf_watcher.cc
../src/ringbuffer_utils.cc
../src/lib/pthread_fixes.cc
../src/lib/savecontext.cc
../src/lib/saveregisters.cc
../src/procutils.cc
../src/user_override.cc
../src/sys_utils.cc
LIBRARIES ${ELFUTILS_LIBRARIES} llvm-demangle DDProf::Parser
DEFINITIONS ${DDPROF_DEFINITION_LIST} KMAX_TRACKED_ALLOCATIONS=10000)

if(NOT CMAKE_BUILD_TYPE STREQUAL "SanitizedDebug")
add_exe(
simple_malloc-static simple_malloc.cc
Expand Down
94 changes: 94 additions & 0 deletions test/allocation_tracker-bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <benchmark/benchmark.h>

#include "allocation_tracker.hpp"
#include "ringbuffer_holder.hpp"
#include <thread>

DDPROF_NOINLINE void my_malloc(size_t size, uintptr_t addr = 0xdeadbeef) {
ddprof::AllocationTracker::track_allocation(addr, size);
// prevent tail call optimization
getpid();
}

DDPROF_NOINLINE void my_free(uintptr_t addr) {
ddprof::AllocationTracker::track_deallocation(addr);
// prevent tail call optimization
getpid();
}
// Function to perform allocations and deallocations
void perform_memory_operations(bool track_allocations,
benchmark::State &state) {
const uint64_t rate = 1;
const size_t buf_size_order = 5;
ddprof::RingBufferHolder ring_buffer{buf_size_order,
RingBufferType::kMPSCRingBuffer};

if (track_allocations) {
ddprof::AllocationTracker::allocation_tracking_init(
rate,
ddprof::AllocationTracker::kDeterministicSampling |
ddprof::AllocationTracker::kTrackDeallocations,
ring_buffer.get_buffer_info());
}

int nb_threads = 4;
std::vector<std::thread> threads;
int num_allocations = 1000;
size_t page_size = 0x1000;
std::random_device rd;
std::mt19937 gen(rd());

for (auto _ : state) {
state.PauseTiming();

// Initialize threads and clear addresses
threads.clear();
std::vector<std::vector<uintptr_t>> thread_addresses(nb_threads);

for (int i = 0; i < nb_threads; ++i) {
threads.emplace_back([&, i] {
std::uniform_int_distribution<> dis(i * page_size,
(i + 1) * page_size - 1);

for (int j = 0; j < num_allocations; ++j) {
uintptr_t addr = dis(gen);
my_malloc(1024, addr);
thread_addresses[i].push_back(addr);
}
});
}

for (auto &t : threads) {
t.join();
}

state.ResumeTiming();

threads.clear();
for (int i = 0; i < nb_threads; ++i) {
threads.emplace_back([&, i] {
for (auto addr : thread_addresses[i]) {
my_free(addr);
}
});
}

for (auto &t : threads) {
t.join();
}
}
ddprof::AllocationTracker::allocation_tracking_free();
}

// Benchmark without allocation tracking
static void BM_ThreadedAllocations_NoTracking(benchmark::State &state) {
perform_memory_operations(false, state);
}

// Benchmark with allocation tracking
static void BM_ThreadedAllocations_Tracking(benchmark::State &state) {
perform_memory_operations(true, state);
}

BENCHMARK(BM_ThreadedAllocations_NoTracking);
BENCHMARK(BM_ThreadedAllocations_Tracking);
4 changes: 0 additions & 4 deletions test/allocation_tracker-ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
// Datadog, Inc.
#include "allocation_tracker.hpp"

#include "ddprof_base.hpp"
#include "ddprof_perf_event.hpp"
#include "ipc.hpp"
#include "live_allocation-c.hpp"
#include "loghandle.hpp"
#include "perf_watcher.hpp"
#include "pevent_lib.hpp"
#include "ringbuffer_holder.hpp"
#include "ringbuffer_utils.hpp"
#include "syscalls.hpp"
#include "unwind.hpp"
#include "unwind_state.hpp"

#include <gtest/gtest.h>
#include <sys/syscall.h>
#include <unistd.h>

DDPROF_NOINLINE void my_malloc(size_t size, uintptr_t addr = 0xdeadbeef) {
Expand Down