All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
logging_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2025, 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/cuda_stream_view.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/detail/format.hpp>
22 #include <rmm/logger.hpp>
24 #include <rmm/resource_ref.hpp>
25 
26 #include <cstddef>
27 #include <cstdio>
28 #include <initializer_list>
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 
33 namespace RMM_NAMESPACE {
34 namespace mr {
35 
52 template <typename Upstream>
54  public:
76  logging_resource_adaptor(Upstream* upstream,
77  std::string const& filename = get_default_filename(),
78  bool auto_flush = false)
79  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), filename, auto_flush)
80  {
81  }
82 
97  logging_resource_adaptor(Upstream* upstream, std::ostream& stream, bool auto_flush = false)
98  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), stream, auto_flush)
99  {
100  }
101 
116  logging_resource_adaptor(Upstream* upstream,
117  std::initializer_list<rapids_logger::sink_ptr> sinks,
118  bool auto_flush = false)
119  : logging_resource_adaptor{to_device_async_resource_ref_checked(upstream), sinks, auto_flush}
120  {
121  }
122 
144  std::string const& filename = get_default_filename(),
145  bool auto_flush = false)
146  : logging_resource_adaptor{make_logger(filename), upstream, auto_flush}
147  {
148  }
149 
163  std::ostream& stream,
164  bool auto_flush = false)
165  : logging_resource_adaptor{make_logger(stream), upstream, auto_flush}
166  {
167  }
168 
182  std::initializer_list<rapids_logger::sink_ptr> sinks,
183  bool auto_flush = false)
184  : logging_resource_adaptor{make_logger(sinks), upstream, auto_flush}
185  {
186  }
187 
188  logging_resource_adaptor() = delete;
189  ~logging_resource_adaptor() override = default;
191  logging_resource_adaptor& operator=(logging_resource_adaptor const&) = delete;
193  default;
195  default;
196 
200  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
201  {
202  return upstream_;
203  }
204 
208  void flush() { logger_->flush(); }
209 
215  [[nodiscard]] std::string header() const
216  {
217  return std::string{"Thread,Time,Action,Pointer,Size,Stream"};
218  }
219 
227  static std::string get_default_filename()
228  {
229  auto* filename = std::getenv("RMM_LOG_FILE");
230  RMM_EXPECTS(filename != nullptr,
231  "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
232  return std::string{filename};
233  }
234 
235  private:
236  static auto make_logger(std::ostream& stream)
237  {
238  return std::make_shared<rapids_logger::logger>("RMM", stream);
239  }
240 
241  static auto make_logger(std::string const& filename)
242  {
243  return std::make_shared<rapids_logger::logger>("RMM", filename);
244  }
245 
246  static auto make_logger(std::initializer_list<rapids_logger::sink_ptr> sinks)
247  {
248  return std::make_shared<rapids_logger::logger>("RMM", sinks);
249  }
250 
251  logging_resource_adaptor(std::shared_ptr<rapids_logger::logger> logger,
252  device_async_resource_ref upstream,
253  bool auto_flush)
254  : logger_{logger}, upstream_{upstream}
255  {
256  if (auto_flush) { logger_->flush_on(rapids_logger::level_enum::info); }
257  logger_->set_pattern("%v");
258  logger_->info(header());
259  logger_->set_pattern("%t,%H:%M:%S.%f,%v");
260  }
261 
287  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
288  {
289  try {
290  auto const ptr = get_upstream_resource().allocate_async(bytes, stream);
291  logger_->info("allocate,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
292  return ptr;
293  } catch (...) {
294  logger_->info(
295  "allocate failure,%p,%zu,%s", nullptr, bytes, rmm::detail::format_stream(stream));
296  throw;
297  }
298  }
299 
314  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
315  {
316  logger_->info("free,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
317  get_upstream_resource().deallocate_async(ptr, bytes, stream);
318  }
319 
327  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
328  {
329  if (this == &other) { return true; }
330  auto const* cast = dynamic_cast<logging_resource_adaptor<Upstream> const*>(&other);
331  if (cast == nullptr) { return false; }
332  return get_upstream_resource() == cast->get_upstream_resource();
333  }
334 
335  std::shared_ptr<rapids_logger::logger> logger_{};
336 
337  device_async_resource_ref upstream_;
339 };
340  // end of group
342 } // namespace mr
343 } // namespace RMM_NAMESPACE
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:93
Resource that uses Upstream to allocate memory and logs information about the requested allocation/de...
Definition: logging_resource_adaptor.hpp:53
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:215
logging_resource_adaptor(device_async_resource_ref upstream, std::string const &filename=get_default_filename(), bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:143
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:208
logging_resource_adaptor(Upstream *upstream, std::string const &filename=get_default_filename(), bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:76
static std::string get_default_filename()
Return the value of the environment variable RMM_LOG_FILE.
Definition: logging_resource_adaptor.hpp:227
logging_resource_adaptor(logging_resource_adaptor &&) noexcept=default
Default move constructor.
logging_resource_adaptor(Upstream *upstream, std::ostream &stream, bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:97
logging_resource_adaptor(Upstream *upstream, std::initializer_list< rapids_logger::sink_ptr > sinks, bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:116
logging_resource_adaptor(device_async_resource_ref upstream, std::ostream &stream, bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:162
logging_resource_adaptor(device_async_resource_ref upstream, std::initializer_list< rapids_logger::sink_ptr > sinks, bool auto_flush=false)
Construct a new logging resource adaptor using upstream to satisfy allocation requests and logging in...
Definition: logging_resource_adaptor.hpp:181
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