diff --git a/include/live_allocation-c.hpp b/include/live_allocation-c.hpp new file mode 100644 index 000000000..01fb0053d --- /dev/null +++ b/include/live_allocation-c.hpp @@ -0,0 +1,12 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. This product includes software +// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present +// Datadog, Inc. + +#pragma once + +namespace ddprof { +namespace liveallocation { +static constexpr auto kMaxTracked = 200000; +} +} diff --git a/include/live_allocation.hpp b/include/live_allocation.hpp index 821545c27..4f717fe82 100644 --- a/include/live_allocation.hpp +++ b/include/live_allocation.hpp @@ -1,8 +1,14 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. This product includes software +// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present +// Datadog, Inc. + #pragma once #include "ddprof_defs.hpp" #include "logger.hpp" #include "unwind_output.hpp" +#include "live_allocation-c.hpp" #include @@ -10,7 +16,6 @@ namespace ddprof { class LiveAllocation { public: - static constexpr auto kMaxTracked = 200000; void register_allocation(const UnwindOutput &stack, uintptr_t addr, size_t size, int watcher_pos, pid_t pid) { StackMap &stack_map = _pid_map[pid]; diff --git a/src/ddprof_worker.cc b/src/ddprof_worker.cc index e7dab83fb..a07d447d1 100644 --- a/src/ddprof_worker.cc +++ b/src/ddprof_worker.cc @@ -386,7 +386,7 @@ static DDRes aggregate_live_allocations(DDProfContext *ctx) { LG_NTC("Number of Live allocations for PID%d = %lu ", stack_map.first, stack_map.second.size()); // Safety to avoid spending all the time reporting allocations - if (stack_map.second.size() >= LiveAllocation::kMaxTracked) { + if (stack_map.second.size() >= liveallocation::kMaxTracked) { stack_map.second.clear(); } } diff --git a/src/lib/allocation_tracker.cc b/src/lib/allocation_tracker.cc index 095c05dcb..33ebf7f4c 100644 --- a/src/lib/allocation_tracker.cc +++ b/src/lib/allocation_tracker.cc @@ -14,6 +14,7 @@ #include "ringbuffer_utils.hpp" #include "savecontext.hpp" #include "syscalls.hpp" +#include "live_allocation-c.hpp" #include #include @@ -225,6 +226,10 @@ void AllocationTracker::track_deallocation(uintptr_t addr, // Inserting / Erasing addresses is done within the lock if (_address_set.erase(addr)) { + if (_address_set.size() >= liveallocation::kMaxTracked) { + // avoid unbounded growth + _address_set.clear(); + } bool success = IsDDResOK(push_dealloc_sample(addr, tl_state)); free_on_consecutive_failures(success); }