RMM  23.12
RAPIDS Memory Manager
tracking_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2023, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/detail/error.hpp>
19 #include <rmm/detail/stack_trace.hpp>
20 #include <rmm/logger.hpp>
22 
23 #include <fmt/core.h>
24 
25 #include <cstddef>
26 #include <map>
27 #include <mutex>
28 #include <shared_mutex>
29 #include <sstream>
30 
31 namespace rmm::mr {
52 template <typename Upstream>
54  public:
55  // can be a std::shared_mutex once C++17 is adopted
56  using read_lock_t =
57  std::shared_lock<std::shared_timed_mutex>;
58  using write_lock_t =
59  std::unique_lock<std::shared_timed_mutex>;
66  struct allocation_info {
67  std::unique_ptr<rmm::detail::stack_trace> strace;
68  std::size_t allocation_size;
69 
70  allocation_info() = delete;
77  allocation_info(std::size_t size, bool capture_stack)
78  : strace{[&]() {
79  return capture_stack ? std::make_unique<rmm::detail::stack_trace>() : nullptr;
80  }()},
81  allocation_size{size} {};
82  };
83 
93  tracking_resource_adaptor(Upstream* upstream, bool capture_stacks = false)
94  : capture_stacks_{capture_stacks}, allocated_bytes_{0}, upstream_{upstream}
95  {
96  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
97  }
98 
99  tracking_resource_adaptor() = delete;
100  ~tracking_resource_adaptor() override = default;
103  default;
104  tracking_resource_adaptor& operator=(tracking_resource_adaptor const&) = delete;
106  default;
107 
111  Upstream* get_upstream() const noexcept { return upstream_; }
112 
119  bool supports_streams() const noexcept override { return upstream_->supports_streams(); }
120 
126  bool supports_get_mem_info() const noexcept override
127  {
128  return upstream_->supports_get_mem_info();
129  }
130 
138  std::map<void*, allocation_info> const& get_outstanding_allocations() const noexcept
139  {
140  return allocations_;
141  }
142 
152  std::size_t get_allocated_bytes() const noexcept { return allocated_bytes_; }
153 
165  {
166  read_lock_t lock(mtx_);
167 
168  std::ostringstream oss;
169 
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;
175  }
176  oss << std::endl;
177  }
178  }
179 
180  return oss.str();
181  }
182 
188  {
189 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
190  RMM_LOG_DEBUG("Outstanding Allocations: {}", get_outstanding_allocations_str());
191 #endif // SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
192  }
193 
194  private:
208  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
209  {
210  void* ptr = upstream_->allocate(bytes, stream);
211 
212  // track it.
213  {
214  write_lock_t lock(mtx_);
215  allocations_.emplace(ptr, allocation_info{bytes, capture_stacks_});
216  }
217  allocated_bytes_ += bytes;
218 
219  return ptr;
220  }
221 
229  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
230  {
231  upstream_->deallocate(ptr, bytes, stream);
232  {
233  write_lock_t lock(mtx_);
234 
235  const auto found = allocations_.find(ptr);
236 
237  // Ensure the allocation is found and the number of bytes match
238  if (found == allocations_.end()) {
239  // Don't throw but log an error. Throwing in a descructor (or any noexcept) will call
240  // std::terminate
241  RMM_LOG_ERROR(
242  "Deallocating a pointer that was not tracked. Ptr: {:p} [{}B], Current Num. Allocations: "
243  "{}",
244  fmt::ptr(ptr),
245  bytes,
246  this->allocations_.size());
247  } else {
248  allocations_.erase(found);
249 
250  auto allocated_bytes = found->second.allocation_size;
251 
252  if (allocated_bytes != bytes) {
253  // Don't throw but log an error. Throwing in a descructor (or any noexcept) will call
254  // std::terminate
255  RMM_LOG_ERROR(
256  "Alloc bytes ({}) and Dealloc bytes ({}) do not match", allocated_bytes, bytes);
257 
258  bytes = allocated_bytes;
259  }
260  }
261  }
262  allocated_bytes_ -= bytes;
263  }
264 
272  bool do_is_equal(device_memory_resource const& other) const noexcept override
273  {
274  if (this == &other) { return true; }
275  auto cast = dynamic_cast<tracking_resource_adaptor<Upstream> const*>(&other);
276  return cast != nullptr ? upstream_->is_equal(*cast->get_upstream())
277  : upstream_->is_equal(other);
278  }
279 
288  std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const override
289  {
290  return upstream_->get_mem_info(stream);
291  }
292 
293  bool capture_stacks_; // whether or not to capture call stacks
294  std::map<void*, allocation_info> allocations_; // map of active allocations
295  std::atomic<std::size_t> allocated_bytes_; // number of bytes currently allocated
296  std::shared_timed_mutex mutable mtx_; // mutex for thread safe access to allocations_
297  Upstream* upstream_; // the upstream resource used for satisfying allocation requests
298 };
299 
308 template <typename Upstream>
310 {
311  return tracking_resource_adaptor<Upstream>{upstream};
312 }
313  // end of group
315 } // namespace rmm::mr
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
Resource that uses Upstream to allocate memory and tracks allocations.
Definition: tracking_resource_adaptor.hpp:53
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:93
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: tracking_resource_adaptor.hpp:126
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: tracking_resource_adaptor.hpp:119
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::shared_lock< std::shared_timed_mutex > read_lock_t
Type of lock used to synchronize read access.
Definition: tracking_resource_adaptor.hpp:57
std::unique_lock< std::shared_timed_mutex > write_lock_t
Type of lock used to synchronize write access.
Definition: tracking_resource_adaptor.hpp:59
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
tracking_resource_adaptor< Upstream > make_tracking_adaptor(Upstream *upstream)
Convenience factory to return a tracking_resource_adaptor around the upstream resource upstream.
Definition: tracking_resource_adaptor.hpp:309
Information stored about an allocation. Includes the size and a stack trace if the tracking_resource_...
Definition: tracking_resource_adaptor.hpp:66
std::unique_ptr< rmm::detail::stack_trace > strace
Stack trace of the allocation.
Definition: tracking_resource_adaptor.hpp:67
std::size_t allocation_size
Size of the allocation.
Definition: tracking_resource_adaptor.hpp:68
allocation_info(std::size_t size, bool capture_stack)
Construct a new allocation info object.
Definition: tracking_resource_adaptor.hpp:77