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-2024, 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 <memory>
29 #include <sstream>
30 #include <string_view>
31 
32 namespace RMM_NAMESPACE {
33 namespace mr {
34 
51 template <typename Upstream>
53  public:
75  logging_resource_adaptor(Upstream* upstream,
76  std::string const& filename = get_default_filename(),
77  bool auto_flush = false)
78  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), filename, auto_flush)
79  {
80  }
81 
96  logging_resource_adaptor(Upstream* upstream, std::ostream& stream, bool auto_flush = false)
97  : logging_resource_adaptor(to_device_async_resource_ref_checked(upstream), stream, auto_flush)
98  {
99  }
100 
115  logging_resource_adaptor(Upstream* upstream,
116  std::initializer_list<sink_ptr> sinks,
117  bool auto_flush = false)
118  : logging_resource_adaptor{to_device_async_resource_ref_checked(upstream), sinks, auto_flush}
119  {
120  }
121 
143  std::string const& filename = get_default_filename(),
144  bool auto_flush = false)
145  : logging_resource_adaptor{make_logger(filename), upstream, auto_flush}
146  {
147  }
148 
162  std::ostream& stream,
163  bool auto_flush = false)
164  : logging_resource_adaptor{make_logger(stream), upstream, auto_flush}
165  {
166  }
167 
181  std::initializer_list<sink_ptr> sinks,
182  bool auto_flush = false)
183  : logging_resource_adaptor{make_logger(sinks), upstream, auto_flush}
184  {
185  }
186 
187  logging_resource_adaptor() = delete;
188  ~logging_resource_adaptor() override = default;
190  logging_resource_adaptor& operator=(logging_resource_adaptor const&) = delete;
192  default;
194  default;
195 
199  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
200  {
201  return upstream_;
202  }
203 
207  void flush() { logger_->flush(); }
208 
214  [[nodiscard]] std::string header() const
215  {
216  return std::string{"Thread,Time,Action,Pointer,Size,Stream"};
217  }
218 
226  static std::string get_default_filename()
227  {
228  auto* filename = std::getenv("RMM_LOG_FILE");
229  RMM_EXPECTS(filename != nullptr,
230  "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
231  return std::string{filename};
232  }
233 
234  private:
235  static auto make_logger(std::ostream& stream) { return std::make_shared<logger>("RMM", stream); }
236 
237  static auto make_logger(std::string const& filename)
238  {
239  return std::make_shared<logger>("RMM", filename);
240  }
241 
242  static auto make_logger(std::initializer_list<sink_ptr> sinks)
243  {
244  return std::make_shared<logger>("RMM", sinks);
245  }
246 
247  logging_resource_adaptor(std::shared_ptr<logger> logger,
248  device_async_resource_ref upstream,
249  bool auto_flush)
250  : logger_{logger}, upstream_{upstream}
251  {
252  if (auto_flush) { logger_->flush_on(level_enum::info); }
253  logger_->set_pattern("%v");
254  logger_->info(header());
255  logger_->set_pattern("%t,%H:%M:%S.%f,%v");
256  }
257 
283  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
284  {
285  try {
286  auto const ptr = get_upstream_resource().allocate_async(bytes, stream);
287  logger_->info("allocate,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
288  return ptr;
289  } catch (...) {
290  logger_->info(
291  "allocate failure,%p,%zu,%s", nullptr, bytes, rmm::detail::format_stream(stream));
292  throw;
293  }
294  }
295 
310  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
311  {
312  logger_->info("free,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream));
313  get_upstream_resource().deallocate_async(ptr, bytes, stream);
314  }
315 
323  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
324  {
325  if (this == &other) { return true; }
326  auto const* cast = dynamic_cast<logging_resource_adaptor<Upstream> const*>(&other);
327  if (cast == nullptr) { return false; }
328  return get_upstream_resource() == cast->get_upstream_resource();
329  }
330 
331  std::shared_ptr<logger> logger_{};
332 
333  device_async_resource_ref upstream_;
335 };
336  // end of group
338 } // namespace mr
339 } // namespace RMM_NAMESPACE
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:94
Resource that uses Upstream to allocate memory and logs information about the requested allocation/de...
Definition: logging_resource_adaptor.hpp:52
logging_resource_adaptor(Upstream *upstream, std::initializer_list< 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:115
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:214
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:142
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:207
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:75
static std::string get_default_filename()
Return the value of the environment variable RMM_LOG_FILE.
Definition: logging_resource_adaptor.hpp:226
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:96
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:161
logging_resource_adaptor(device_async_resource_ref upstream, std::initializer_list< 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:180
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