logging_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 
8 #include <rmm/detail/error.hpp>
9 #include <rmm/detail/export.hpp>
10 #include <rmm/detail/format.hpp>
11 #include <rmm/logger.hpp>
13 #include <rmm/resource_ref.hpp>
14 
15 #include <cstddef>
16 #include <cstdio>
17 #include <initializer_list>
18 #include <memory>
19 #include <ostream>
20 #include <string>
21 
22 namespace RMM_NAMESPACE {
23 namespace mr {
24 
41 template <typename Upstream>
43  public:
65  logging_resource_adaptor(Upstream* upstream,
66  std::string const& filename = get_default_filename(),
67  bool auto_flush = false)
68  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), filename, auto_flush)
69  {
70  }
71 
86  logging_resource_adaptor(Upstream* upstream, std::ostream& stream, bool auto_flush = false)
87  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), stream, auto_flush)
88  {
89  }
90 
105  logging_resource_adaptor(Upstream* upstream,
106  std::initializer_list<rapids_logger::sink_ptr> sinks,
107  bool auto_flush = false)
108  : logging_resource_adaptor{to_device_async_resource_ref_checked(upstream), sinks, auto_flush}
109  {
110  }
111 
133  std::string const& filename = get_default_filename(),
134  bool auto_flush = false)
135  : logging_resource_adaptor{make_logger(filename), upstream, auto_flush}
136  {
137  }
138 
152  std::ostream& stream,
153  bool auto_flush = false)
154  : logging_resource_adaptor{make_logger(stream), upstream, auto_flush}
155  {
156  }
157 
171  std::initializer_list<rapids_logger::sink_ptr> sinks,
172  bool auto_flush = false)
173  : logging_resource_adaptor{make_logger(sinks), upstream, auto_flush}
174  {
175  }
176 
177  logging_resource_adaptor() = delete;
178  ~logging_resource_adaptor() override = default;
180  logging_resource_adaptor& operator=(logging_resource_adaptor const&) = delete;
182  default;
184  default;
185 
189  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
190  {
191  return upstream_;
192  }
193 
197  void flush() { logger_->flush(); }
198 
204  [[nodiscard]] std::string header() const
205  {
206  return std::string{"Thread,Time,Action,Pointer,Size,Stream"};
207  }
208 
216  static std::string get_default_filename()
217  {
218  auto* filename = std::getenv("RMM_LOG_FILE");
219  RMM_EXPECTS(filename != nullptr,
220  "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
221  return std::string{filename};
222  }
223 
224  private:
225  static auto make_logger(std::ostream& stream)
226  {
227  return std::make_shared<rapids_logger::logger>("RMM", stream);
228  }
229 
230  static auto make_logger(std::string const& filename)
231  {
232  return std::make_shared<rapids_logger::logger>("RMM", filename);
233  }
234 
235  static auto make_logger(std::initializer_list<rapids_logger::sink_ptr> sinks)
236  {
237  return std::make_shared<rapids_logger::logger>("RMM", sinks);
238  }
239 
240  logging_resource_adaptor(std::shared_ptr<rapids_logger::logger> logger,
241  device_async_resource_ref upstream,
242  bool auto_flush)
243  : logger_{logger}, upstream_{upstream}
244  {
245  if (auto_flush) { logger_->flush_on(rapids_logger::level_enum::info); }
246  logger_->set_pattern("%v");
247  logger_->info(header());
248  logger_->set_pattern("%t,%H:%M:%S.%f,%v");
249  }
250 
276  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
277  {
278  try {
279  auto const ptr = get_upstream_resource().allocate(stream, bytes);
280  logger_->info("allocate,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
281  return ptr;
282  } catch (...) {
283  logger_->info(
284  "allocate failure,%p,%zu,%s", nullptr, bytes, rmm::detail::format_stream(stream));
285  throw;
286  }
287  }
288 
303  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
304  {
305  logger_->info("free,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
306  get_upstream_resource().deallocate(stream, ptr, bytes);
307  }
308 
316  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
317  {
318  if (this == &other) { return true; }
319  auto const* cast = dynamic_cast<logging_resource_adaptor<Upstream> const*>(&other);
320  if (cast == nullptr) { return false; }
321  return get_upstream_resource() == cast->get_upstream_resource();
322  }
323 
324  std::shared_ptr<rapids_logger::logger> logger_{};
325 
326  device_async_resource_ref upstream_;
328 };
329  // end of group
331 } // namespace mr
332 } // namespace RMM_NAMESPACE
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Resource that uses Upstream to allocate memory and logs information about the requested allocation/de...
Definition: logging_resource_adaptor.hpp:42
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:204
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:132
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:197
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:65
static std::string get_default_filename()
Return the value of the environment variable RMM_LOG_FILE.
Definition: logging_resource_adaptor.hpp:216
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:86
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:105
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:151
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:170
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