18 #include <rmm/detail/error.hpp>
19 #include <rmm/detail/export.hpp>
20 #include <rmm/detail/stack_trace.hpp>
21 #include <rmm/logger.hpp>
30 #include <shared_mutex>
33 namespace RMM_NAMESPACE {
55 template <
typename Upstream>
59 std::shared_lock<std::shared_mutex>;
61 std::unique_lock<std::shared_mutex>;
69 std::unique_ptr<rmm::detail::stack_trace>
strace;
81 return capture_stack ? std::make_unique<rmm::detail::stack_trace>() :
nullptr;
83 allocation_size{size} {};
94 : capture_stacks_{capture_stacks}, allocated_bytes_{0}, upstream_{upstream}
108 : capture_stacks_{capture_stacks},
168 std::ostringstream oss;
170 if (!allocations_.empty()) {
171 for (
auto const& alloc : allocations_) {
172 oss << alloc.first <<
": " << alloc.second.allocation_size <<
" B";
173 if (alloc.second.strace !=
nullptr) {
174 oss <<
" : callstack:" << std::endl << *alloc.second.strace;
189 #if RMM_LOG_ACTIVE_LEVEL <= RMM_LOG_LEVEL_DEBUG
190 RMM_LOG_DEBUG(
"Outstanding Allocations: %s", get_outstanding_allocations_str());
210 void* ptr = get_upstream_resource().allocate_async(bytes, stream);
213 write_lock_t lock(mtx_);
214 allocations_.emplace(ptr, allocation_info{bytes, capture_stacks_});
216 allocated_bytes_ += bytes;
228 void do_deallocate(
void* ptr, std::size_t bytes, cuda_stream_view stream)
override
230 get_upstream_resource().deallocate_async(ptr, bytes, stream);
232 write_lock_t lock(mtx_);
234 const auto found = allocations_.find(ptr);
237 if (found == allocations_.end()) {
241 "Deallocating a pointer that was not tracked. Ptr: %p [%zuB], Current Num. Allocations: "
245 this->allocations_.size());
247 allocations_.erase(found);
249 auto allocated_bytes = found->second.allocation_size;
251 if (allocated_bytes != bytes) {
255 "Alloc bytes (%zu) and Dealloc bytes (%zu) do not match", allocated_bytes, bytes);
257 bytes = allocated_bytes;
261 allocated_bytes_ -= bytes;
271 bool do_is_equal(device_memory_resource
const& other)
const noexcept
override
273 if (
this == &other) {
return true; }
274 auto cast =
dynamic_cast<tracking_resource_adaptor<Upstream> const*
>(&other);
275 if (cast ==
nullptr) {
return false; }
276 return get_upstream_resource() == cast->get_upstream_resource();
279 bool capture_stacks_;
280 std::map<void*, allocation_info> allocations_;
281 std::atomic<std::size_t> allocated_bytes_;
282 std::shared_mutex
mutable mtx_;
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:93
Resource that uses Upstream to allocate memory and tracks allocations.
Definition: tracking_resource_adaptor.hpp:56
tracking_resource_adaptor(Upstream *upstream, bool capture_stacks=false)
Construct a new tracking resource adaptor using upstream to satisfy allocation requests.
Definition: tracking_resource_adaptor.hpp:107
tracking_resource_adaptor(device_async_resource_ref upstream, bool capture_stacks=false)
Construct a new tracking resource adaptor using upstream to satisfy allocation requests.
Definition: tracking_resource_adaptor.hpp:93
std::size_t get_allocated_bytes() const noexcept
Query the number of bytes that have been allocated. Note that this can not be used to know how large ...
Definition: tracking_resource_adaptor.hpp:152
std::unique_lock< std::shared_mutex > write_lock_t
Type of lock used to synchronize write access.
Definition: tracking_resource_adaptor.hpp:61
tracking_resource_adaptor(tracking_resource_adaptor &&) noexcept=default
Default move constructor.
std::string get_outstanding_allocations_str() const
Gets a string containing the outstanding allocation pointers, their size, and optionally the stack tr...
Definition: tracking_resource_adaptor.hpp:164
std::map< void *, allocation_info > const & get_outstanding_allocations() const noexcept
Get the outstanding allocations map.
Definition: tracking_resource_adaptor.hpp:138
void log_outstanding_allocations() const
Log any outstanding allocations via RMM_LOG_DEBUG.
Definition: tracking_resource_adaptor.hpp:187
std::shared_lock< std::shared_mutex > read_lock_t
Type of lock used to synchronize read access.
Definition: tracking_resource_adaptor.hpp:59
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:41
device_async_resource_ref to_device_async_resource_ref_checked(Resource *res)
Convert pointer to memory resource into device_async_resource_ref, checking for nullptr
Definition: resource_ref.hpp:79
Management of per-device device_memory_resources.
Information stored about an allocation. Includes the size and a stack trace if the tracking_resource_...
Definition: tracking_resource_adaptor.hpp:68
std::unique_ptr< rmm::detail::stack_trace > strace
Stack trace of the allocation.
Definition: tracking_resource_adaptor.hpp:69
std::size_t allocation_size
Size of the allocation.
Definition: tracking_resource_adaptor.hpp:70
allocation_info(std::size_t size, bool capture_stack)
Construct a new allocation info object.
Definition: tracking_resource_adaptor.hpp:79