tracking_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/error.hpp>
8 #include <rmm/detail/export.hpp>
9 #include <rmm/detail/stack_trace.hpp>
10 #include <rmm/logger.hpp>
13 #include <rmm/resource_ref.hpp>
14 
15 #include <atomic>
16 #include <cstddef>
17 #include <map>
18 #include <mutex>
19 #include <shared_mutex>
20 #include <sstream>
21 
22 namespace RMM_NAMESPACE {
23 namespace mr {
44 template <typename Upstream>
46  public:
47  using read_lock_t =
48  std::shared_lock<std::shared_mutex>;
49  using write_lock_t =
50  std::unique_lock<std::shared_mutex>;
57  struct allocation_info {
58  std::unique_ptr<rmm::detail::stack_trace> strace;
59  std::size_t allocation_size;
60 
61  allocation_info() = delete;
68  allocation_info(std::size_t size, bool capture_stack)
69  : strace{[&]() {
70  return capture_stack ? std::make_unique<rmm::detail::stack_trace>() : nullptr;
71  }()},
72  allocation_size{size} {};
73  };
74 
82  tracking_resource_adaptor(device_async_resource_ref upstream, bool capture_stacks = false)
83  : capture_stacks_{capture_stacks}, allocated_bytes_{0}, upstream_{upstream}
84  {
85  }
86 
96  tracking_resource_adaptor(Upstream* upstream, bool capture_stacks = false)
97  : capture_stacks_{capture_stacks},
98  allocated_bytes_{0},
99  upstream_{to_device_async_resource_ref_checked(upstream)}
100  {
101  }
102 
103  tracking_resource_adaptor() = delete;
104  ~tracking_resource_adaptor() override = default;
107  default;
108  tracking_resource_adaptor& operator=(tracking_resource_adaptor const&) = delete;
110  default;
111 
115  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
116  {
117  return upstream_;
118  }
119 
127  std::map<void*, allocation_info> const& get_outstanding_allocations() const noexcept
128  {
129  return allocations_;
130  }
131 
141  std::size_t get_allocated_bytes() const noexcept { return allocated_bytes_; }
142 
154  {
155  read_lock_t lock(mtx_);
156 
157  std::ostringstream oss;
158 
159  if (!allocations_.empty()) {
160  for (auto const& alloc : allocations_) {
161  oss << alloc.first << ": " << alloc.second.allocation_size << " B";
162  if (alloc.second.strace != nullptr) {
163  oss << " : callstack:" << std::endl << *alloc.second.strace;
164  }
165  oss << std::endl;
166  }
167  }
168 
169  return oss.str();
170  }
171 
177  {
178 #if RMM_LOG_ACTIVE_LEVEL <= RMM_LOG_LEVEL_DEBUG
179  RMM_LOG_DEBUG("Outstanding Allocations: %s", get_outstanding_allocations_str());
180 #endif // RMM_LOG_ACTIVE_LEVEL <= RMM_LOG_LEVEL_DEBUG
181  }
182 
183  private:
197  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
198  {
199  void* ptr = get_upstream_resource().allocate(stream, bytes);
200  // track it.
201  {
202  write_lock_t lock(mtx_);
203  allocations_.emplace(ptr, allocation_info{bytes, capture_stacks_});
204  }
205  allocated_bytes_ += bytes;
206 
207  return ptr;
208  }
209 
217  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
218  {
219  get_upstream_resource().deallocate(stream, ptr, bytes);
220  {
221  write_lock_t lock(mtx_);
222 
223  const auto found = allocations_.find(ptr);
224 
225  // Ensure the allocation is found and the number of bytes match
226  if (found == allocations_.end()) {
227  // Don't throw but log an error. Throwing in a destructor (or any noexcept) will call
228  // std::terminate
229  RMM_LOG_ERROR(
230  "Deallocating a pointer that was not tracked. Ptr: %p [%zuB], Current Num. Allocations: "
231  "%zu",
232  ptr,
233  bytes,
234  this->allocations_.size());
235  } else {
236  auto const allocated_bytes = found->second.allocation_size;
237 
238  allocations_.erase(found);
239 
240  if (allocated_bytes != bytes) {
241  // Don't throw but log an error. Throwing in a destructor (or any noexcept) will call
242  // std::terminate
243  RMM_LOG_ERROR(
244  "Alloc bytes (%zu) and Dealloc bytes (%zu) do not match", allocated_bytes, bytes);
245 
246  bytes = allocated_bytes;
247  }
248  }
249  }
250  allocated_bytes_ -= bytes;
251  }
252 
260  bool do_is_equal(device_memory_resource const& other) const noexcept override
261  {
262  if (this == &other) { return true; }
263  auto cast = dynamic_cast<tracking_resource_adaptor<Upstream> const*>(&other);
264  if (cast == nullptr) { return false; }
265  return get_upstream_resource() == cast->get_upstream_resource();
266  }
267 
268  bool capture_stacks_; // whether or not to capture call stacks
269  std::map<void*, allocation_info> allocations_; // map of active allocations
270  std::atomic<std::size_t> allocated_bytes_; // number of bytes currently allocated
271  std::shared_mutex mutable mtx_; // mutex for thread safe access to allocations_
272  device_async_resource_ref upstream_; // the upstream resource used for satisfying
273  // allocation requests
274 };
275  // end of group
277 } // namespace mr
278 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Resource that uses Upstream to allocate memory and tracks allocations.
Definition: tracking_resource_adaptor.hpp:45
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:96
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:82
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:141
std::unique_lock< std::shared_mutex > write_lock_t
Type of lock used to synchronize write access.
Definition: tracking_resource_adaptor.hpp:50
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:153
std::map< void *, allocation_info > const & get_outstanding_allocations() const noexcept
Get the outstanding allocations map.
Definition: tracking_resource_adaptor.hpp:127
void log_outstanding_allocations() const
Log any outstanding allocations via RMM_LOG_DEBUG.
Definition: tracking_resource_adaptor.hpp:176
std::shared_lock< std::shared_mutex > read_lock_t
Type of lock used to synchronize read access.
Definition: tracking_resource_adaptor.hpp:48
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:72
detail::cccl_async_resource_ref< cuda::mr::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:32
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:57
std::unique_ptr< rmm::detail::stack_trace > strace
Stack trace of the allocation.
Definition: tracking_resource_adaptor.hpp:58
std::size_t allocation_size
Size of the allocation.
Definition: tracking_resource_adaptor.hpp:59
allocation_info(std::size_t size, bool capture_stack)
Construct a new allocation info object.
Definition: tracking_resource_adaptor.hpp:68