Skip to content

Commit

Permalink
Deallocation code path optimisations
Browse files Browse the repository at this point in the history
- Removal of the lock
- Minor fixes around the allocation code paths
  • Loading branch information
r1viollet committed Aug 30, 2023
1 parent fc9ae2d commit c182378
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/lib/address_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AddressBitset {
// returns true if the element was removed
bool unset(uintptr_t addr);
void clear() {
for (auto& element : _address_bitset) {
for (auto &element : _address_bitset) {
element.store(0, std::memory_order_relaxed);
}
_nb_elements.store(0);
Expand Down
7 changes: 2 additions & 5 deletions src/lib/allocation_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,8 @@ void AllocationTracker::track_deallocation(uintptr_t addr,
return;
}

{ // Grab the lock to check if this allocation was stored in the set
std::lock_guard lock{_state.mutex};
if (!_state.track_deallocations || !_dealloc_bitset.unset(addr)) {
return;
}
if (!_state.track_deallocations || !_dealloc_bitset.unset(addr)) {
return;
}

bool success = IsDDResOK(push_dealloc_sample(addr, tl_state));
Expand Down
6 changes: 3 additions & 3 deletions test/address_bitset-ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ TEST(address_bitset, many_addresses) {
AddressBitset address_bitset;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uintptr_t> dis(0,
std::numeric_limits<uintptr_t>::max());
std::uniform_int_distribution<uintptr_t> dis(
0, std::numeric_limits<uintptr_t>::max());

std::vector<uintptr_t> addresses;
unsigned nb_elements = 100000;
Expand All @@ -34,4 +34,4 @@ TEST(address_bitset, many_addresses) {
}
EXPECT_EQ(0, address_bitset.nb_elements());
}
}
} // namespace ddprof
15 changes: 5 additions & 10 deletions test/allocation_tracker-bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
// Sampling rate: default rate is 524288
static constexpr uint64_t k_rate = 200000;

#define READER_THREAD
// The reader thread is interesting, though it starts dominating the CPU
// the benchmark focuses on the capture of allocation events.
// #define READER_THREAD
std::atomic<bool> reader_continue{true};
std::atomic<bool> error_in_reader{false};

Expand All @@ -27,17 +29,14 @@ void read_buffer(ddprof::RingBufferHolder &holder) {
error_in_reader = false;
while (reader_continue) {
ddprof::MPSCRingBufferReader reader(holder.get_ring_buffer());
// fprintf(stderr, "size = %lu! \n", reader.available_size());
// fprintf(stderr, "size = %lu! \n", reader.available_size());
auto buf = reader.read_sample();
if (!buf.empty()) {
++nb_samples;
// fprintf(stderr, "Yep, got sample ! \n");
}
std::chrono::microseconds(10000);
}
#ifdef DEBUG
fprintf(stderr, "Reader thread exit, nb_samples=%d\n", nb_samples);
#endif
if (nb_samples == 0) {
error_in_reader = true;
}
Expand Down Expand Up @@ -70,7 +69,6 @@ void perform_memory_operations(bool track_allocations,
ddprof::AllocationTracker::kTrackDeallocations;
#endif


int nb_threads = 4;
std::vector<std::thread> threads;
int num_allocations = 1000;
Expand Down Expand Up @@ -100,7 +98,7 @@ void perform_memory_operations(bool track_allocations,
threads.emplace_back([&, i] {
ddprof::AllocationTracker::init_tl_state();
std::uniform_int_distribution<uintptr_t> dis(i * page_size,
(i + 1) * page_size - 1);
(i + 1) * page_size - 1);

for (int j = 0; j < num_allocations; ++j) {
uintptr_t addr = dis(gen);
Expand All @@ -116,7 +114,6 @@ void perform_memory_operations(bool track_allocations,

threads.clear();
for (int i = 0; i < nb_threads; ++i) {
ddprof::AllocationTracker::init_tl_state();
threads.emplace_back([&, i] {
ddprof::AllocationTracker::init_tl_state();
for (auto addr : thread_addresses[i]) {
Expand All @@ -130,8 +127,6 @@ void perform_memory_operations(bool track_allocations,
}
}



#ifdef READER_THREAD
reader_continue = false;
reader_thread.join();
Expand Down
3 changes: 1 addition & 2 deletions test/allocation_tracker-ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ TEST(allocation_tracker, max_tracked_allocs) {
ASSERT_EQ(sample->pid, getpid());
ASSERT_EQ(sample->tid, ddprof::gettid());
ASSERT_EQ(sample->addr, 0x1000 + i);
}
else {
} else {
if (hdr->type == PERF_CUSTOM_EVENT_CLEAR_LIVE_ALLOCATION) {
printf("Clear was found at iteration %d\n", i);
clear_found = true;
Expand Down

0 comments on commit c182378

Please sign in to comment.