RMM  23.12
RAPIDS Memory Manager
statistics_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021, 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 
19 
20 #include <cstddef>
21 #include <mutex>
22 #include <shared_mutex>
23 
24 namespace rmm::mr {
44 template <typename Upstream>
46  public:
47  // can be a std::shared_mutex once C++17 is adopted
48  using read_lock_t =
49  std::shared_lock<std::shared_timed_mutex>;
50  using write_lock_t =
51  std::unique_lock<std::shared_timed_mutex>;
55  struct counter {
56  int64_t value{0};
57  int64_t peak{0};
58  int64_t total{0};
59 
66  counter& operator+=(int64_t val)
67  {
68  value += val;
69  total += val;
70  peak = std::max(value, peak);
71  return *this;
72  }
73 
80  counter& operator-=(int64_t val)
81  {
82  value -= val;
83  return *this;
84  }
85  };
86 
95  statistics_resource_adaptor(Upstream* upstream) : upstream_{upstream}
96  {
97  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
98  }
99 
100  statistics_resource_adaptor() = delete;
101  ~statistics_resource_adaptor() override = default;
103  statistics_resource_adaptor& operator=(statistics_resource_adaptor const&) = delete;
105  default;
107  default;
108 
112  Upstream* get_upstream() const noexcept { return upstream_; }
113 
120  bool supports_streams() const noexcept override { return upstream_->supports_streams(); }
121 
127  bool supports_get_mem_info() const noexcept override
128  {
129  return upstream_->supports_get_mem_info();
130  }
131 
139  counter get_bytes_counter() const noexcept
140  {
141  read_lock_t lock(mtx_);
142 
143  return bytes_;
144  }
145 
154  {
155  read_lock_t lock(mtx_);
156 
157  return allocations_;
158  }
159 
160  private:
174  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
175  {
176  void* ptr = upstream_->allocate(bytes, stream);
177 
178  // increment the stats
179  {
180  write_lock_t lock(mtx_);
181 
182  // Increment the allocation_count_ while we have the lock
183  bytes_ += bytes;
184  allocations_ += 1;
185  }
186 
187  return ptr;
188  }
189 
197  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
198  {
199  upstream_->deallocate(ptr, bytes, stream);
200 
201  {
202  write_lock_t lock(mtx_);
203 
204  // Decrement the current allocated counts.
205  bytes_ -= bytes;
206  allocations_ -= 1;
207  }
208  }
209 
217  bool do_is_equal(device_memory_resource const& other) const noexcept override
218  {
219  if (this == &other) { return true; }
220  auto cast = dynamic_cast<statistics_resource_adaptor<Upstream> const*>(&other);
221  return cast != nullptr ? upstream_->is_equal(*cast->get_upstream())
222  : upstream_->is_equal(other);
223  }
224 
233  std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const override
234  {
235  return upstream_->get_mem_info(stream);
236  }
237 
238  counter bytes_; // peak, current and total allocated bytes
239  counter allocations_; // peak, current and total allocation count
240  std::shared_timed_mutex mutable mtx_; // mutex for thread safe access to allocations_
241  Upstream* upstream_; // the upstream resource used for satisfying allocation requests
242 };
243 
252 template <typename Upstream>
254 {
255  return statistics_resource_adaptor<Upstream>{upstream};
256 }
257  // end of group
259 } // 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 statistics on memory allocations.
Definition: statistics_resource_adaptor.hpp:45
std::shared_lock< std::shared_timed_mutex > read_lock_t
Type of lock used to synchronize read access.
Definition: statistics_resource_adaptor.hpp:49
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: statistics_resource_adaptor.hpp:120
statistics_resource_adaptor(statistics_resource_adaptor &&) noexcept=default
Default move constructor.
std::unique_lock< std::shared_timed_mutex > write_lock_t
Type of lock used to synchronize write access.
Definition: statistics_resource_adaptor.hpp:51
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: statistics_resource_adaptor.hpp:127
statistics_resource_adaptor(Upstream *upstream)
Construct a new statistics resource adaptor using upstream to satisfy allocation requests.
Definition: statistics_resource_adaptor.hpp:95
counter get_allocations_counter() const noexcept
Returns a counter struct for this adaptor containing the current, peak, and total number of allocatio...
Definition: statistics_resource_adaptor.hpp:153
counter get_bytes_counter() const noexcept
Returns a counter struct for this adaptor containing the current, peak, and total number of allocated...
Definition: statistics_resource_adaptor.hpp:139
Upstream * get_upstream() const noexcept
Pointer to the upstream resource.
Definition: statistics_resource_adaptor.hpp:112
statistics_resource_adaptor< Upstream > make_statistics_adaptor(Upstream *upstream)
Convenience factory to return a statistics_resource_adaptor around the upstream resource upstream.
Definition: statistics_resource_adaptor.hpp:253
Utility struct for counting the current, peak, and total value of a number.
Definition: statistics_resource_adaptor.hpp:55
counter & operator-=(int64_t val)
Subtract val from the current value and update the peak value if necessary.
Definition: statistics_resource_adaptor.hpp:80
int64_t value
Current value.
Definition: statistics_resource_adaptor.hpp:56
int64_t peak
Max value of value
Definition: statistics_resource_adaptor.hpp:57
counter & operator+=(int64_t val)
Add val to the current value and update the peak value if necessary.
Definition: statistics_resource_adaptor.hpp:66
int64_t total
Sum of all added values.
Definition: statistics_resource_adaptor.hpp:58