statistics_resource_adaptor.hpp
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 
18 #include <rmm/mr/device/device_memory_resource.hpp>
19 
20 #include <cstddef>
21 #include <mutex>
22 #include <shared_mutex>
23 
24 namespace rmm::mr {
39 template <typename Upstream>
41  public:
42  // can be a std::shared_mutex once C++17 is adopted
43  using read_lock_t = std::shared_lock<std::shared_timed_mutex>;
44  using write_lock_t = std::unique_lock<std::shared_timed_mutex>;
45 
49  struct counter {
50  int64_t value{0}; // Current value
51  int64_t peak{0}; // Max value of `value`
52  int64_t total{0}; // Sum of all added values
53 
54  counter& operator+=(int64_t val)
55  {
56  value += val;
57  total += val;
58  peak = std::max(value, peak);
59  return *this;
60  }
61 
62  counter& operator-=(int64_t val)
63  {
64  value -= val;
65  return *this;
66  }
67  };
68 
77  statistics_resource_adaptor(Upstream* upstream) : upstream_{upstream}
78  {
79  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
80  }
81 
82  statistics_resource_adaptor() = delete;
83  ~statistics_resource_adaptor() override = default;
84  statistics_resource_adaptor(statistics_resource_adaptor const&) = delete;
85  statistics_resource_adaptor& operator=(statistics_resource_adaptor const&) = delete;
86  statistics_resource_adaptor(statistics_resource_adaptor&&) noexcept = default;
87  statistics_resource_adaptor& operator=(statistics_resource_adaptor&&) noexcept = default;
88 
94  Upstream* get_upstream() const noexcept { return upstream_; }
95 
102  bool supports_streams() const noexcept override { return upstream_->supports_streams(); }
103 
109  bool supports_get_mem_info() const noexcept override
110  {
111  return upstream_->supports_get_mem_info();
112  }
113 
121  counter get_bytes_counter() const noexcept
122  {
123  read_lock_t lock(mtx_);
124 
125  return bytes_;
126  }
127 
136  {
137  read_lock_t lock(mtx_);
138 
139  return allocations_;
140  }
141 
142  private:
156  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
157  {
158  void* ptr = upstream_->allocate(bytes, stream);
159 
160  // increment the stats
161  {
162  write_lock_t lock(mtx_);
163 
164  // Increment the allocation_count_ while we have the lock
165  bytes_ += bytes;
166  allocations_ += 1;
167  }
168 
169  return ptr;
170  }
171 
181  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
182  {
183  upstream_->deallocate(ptr, bytes, stream);
184 
185  {
186  write_lock_t lock(mtx_);
187 
188  // Decrement the current allocated counts.
189  bytes_ -= bytes;
190  allocations_ -= 1;
191  }
192  }
193 
203  bool do_is_equal(device_memory_resource const& other) const noexcept override
204  {
205  if (this == &other) { return true; }
206  auto cast = dynamic_cast<statistics_resource_adaptor<Upstream> const*>(&other);
207  return cast != nullptr ? upstream_->is_equal(*cast->get_upstream())
208  : upstream_->is_equal(other);
209  }
210 
219  std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const override
220  {
221  return upstream_->get_mem_info(stream);
222  }
223 
224  counter bytes_; // peak, current and total allocated bytes
225  counter allocations_; // peak, current and total allocation count
226  std::shared_timed_mutex mutable mtx_; // mutex for thread safe access to allocations_
227  Upstream* upstream_; // the upstream resource used for satisfying allocation requests
228 };
229 
237 template <typename Upstream>
238 statistics_resource_adaptor<Upstream> make_statistics_adaptor(Upstream* upstream)
239 {
240  return statistics_resource_adaptor<Upstream>{upstream};
241 }
242 
243 } // namespace rmm::mr
rmm::mr::statistics_resource_adaptor::get_allocations_counter
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:135
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::statistics_resource_adaptor::get_upstream
Upstream * get_upstream() const noexcept
Return pointer to the upstream resource.
Definition: statistics_resource_adaptor.hpp:94
rmm::mr::statistics_resource_adaptor::counter
Utility struct for counting the current, peak, and total value of a number.
Definition: statistics_resource_adaptor.hpp:49
rmm::mr::statistics_resource_adaptor::get_bytes_counter
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:121
rmm::mr::statistics_resource_adaptor::statistics_resource_adaptor
statistics_resource_adaptor(Upstream *upstream)
Construct a new statistics resource adaptor using upstream to satisfy allocation requests.
Definition: statistics_resource_adaptor.hpp:77
rmm::mr::statistics_resource_adaptor
Resource that uses Upstream to allocate memory and tracks statistics on memory allocations.
Definition: statistics_resource_adaptor.hpp:40
rmm::mr::statistics_resource_adaptor::supports_streams
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: statistics_resource_adaptor.hpp:102
rmm::mr::statistics_resource_adaptor::supports_get_mem_info
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: statistics_resource_adaptor.hpp:109
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82