logging_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/cuda_stream_view.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/mr/device/device_memory_resource.hpp>
21 
22 #include <spdlog/common.h>
23 #include <spdlog/sinks/basic_file_sink.h>
24 #include <spdlog/sinks/ostream_sink.h>
25 #include <spdlog/spdlog.h>
26 
27 #include <cstddef>
28 #include <memory>
29 #include <sstream>
30 #include <string_view>
31 
32 namespace rmm::mr {
44 template <typename Upstream>
46  public:
68  logging_resource_adaptor(Upstream* upstream,
69  std::string const& filename = get_default_filename(),
70  bool auto_flush = false)
71  : logger_{make_logger(filename)}, upstream_{upstream}
72  {
73  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
74 
75  init_logger(auto_flush);
76  }
77 
92  logging_resource_adaptor(Upstream* upstream, std::ostream& stream, bool auto_flush = false)
93  : logger_{make_logger(stream)}, upstream_{upstream}
94  {
95  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
96 
97  init_logger(auto_flush);
98  }
99 
100  logging_resource_adaptor(Upstream* upstream,
101  spdlog::sinks_init_list sinks,
102  bool auto_flush = false)
103  : logger_{make_logger(sinks)}, upstream_{upstream}
104  {
105  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
106 
107  init_logger(auto_flush);
108  }
109 
110  logging_resource_adaptor() = delete;
111  ~logging_resource_adaptor() override = default;
112  logging_resource_adaptor(logging_resource_adaptor const&) = delete;
113  logging_resource_adaptor& operator=(logging_resource_adaptor const&) = delete;
114  logging_resource_adaptor(logging_resource_adaptor&&) noexcept = default;
115  logging_resource_adaptor& operator=(logging_resource_adaptor&&) noexcept = default;
116 
122  [[nodiscard]] Upstream* get_upstream() const noexcept { return upstream_; }
123 
130  [[nodiscard]] bool supports_streams() const noexcept override
131  {
132  return upstream_->supports_streams();
133  }
134 
140  [[nodiscard]] bool supports_get_mem_info() const noexcept override
141  {
142  return upstream_->supports_get_mem_info();
143  }
144 
148  void flush() { logger_->flush(); }
149 
155  [[nodiscard]] std::string header() const
156  {
157  return std::string{"Thread,Time,Action,Pointer,Size,Stream"};
158  }
159 
160  private:
168  static std::string get_default_filename()
169  {
170  auto* filename = std::getenv("RMM_LOG_FILE");
171  RMM_EXPECTS(filename != nullptr,
172  "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
173  return std::string{filename};
174  }
175 
176  static auto make_logger(std::ostream& stream)
177  {
178  return std::make_shared<spdlog::logger>(
179  "RMM", std::make_shared<spdlog::sinks::ostream_sink_mt>(stream));
180  }
181 
182  static auto make_logger(std::string const& filename)
183  {
184  return std::make_shared<spdlog::logger>(
185  "RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true /*truncate file*/));
186  }
187 
188  static auto make_logger(spdlog::sinks_init_list sinks)
189  {
190  return std::make_shared<spdlog::logger>("RMM", sinks);
191  }
192 
196  void init_logger(bool auto_flush)
197  {
198  if (auto_flush) { logger_->flush_on(spdlog::level::info); }
199  logger_->set_pattern("%v");
200  logger_->info(header());
201  logger_->set_pattern("%t,%H:%M:%S.%f,%v");
202  }
203 
229  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
230  {
231  try {
232  auto const ptr = upstream_->allocate(bytes, stream);
233  logger_->info("allocate,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
234  return ptr;
235  } catch (...) {
236  logger_->info("allocate failure,{},{},{}", nullptr, bytes, fmt::ptr(stream.value()));
237  throw;
238  }
239  }
240 
257  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
258  {
259  logger_->info("free,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
260  upstream_->deallocate(ptr, bytes, stream);
261  }
262 
272  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
273  {
274  if (this == &other) { return true; }
275  auto const* cast = dynamic_cast<logging_resource_adaptor<Upstream> const*>(&other);
276  if (cast != nullptr) { return upstream_->is_equal(*cast->get_upstream()); }
277  return upstream_->is_equal(other);
278  }
279 
288  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
289  cuda_stream_view stream) const override
290  {
291  return upstream_->get_mem_info(stream);
292  }
293 
294  // make_logging_adaptor needs access to private get_default_filename
295  template <typename T>
296  // NOLINTNEXTLINE(readability-redundant-declaration)
297  friend logging_resource_adaptor<T> make_logging_adaptor(T* upstream,
298  std::string const& filename,
299  bool auto_flush);
300 
301  std::shared_ptr<spdlog::logger> logger_;
302 
303  Upstream* upstream_;
304 };
306 
316 template <typename Upstream>
317 logging_resource_adaptor<Upstream> make_logging_adaptor(
318  Upstream* upstream,
319  std::string const& filename = logging_resource_adaptor<Upstream>::get_default_filename(),
320  bool auto_flush = false)
321 {
322  return logging_resource_adaptor<Upstream>{upstream, filename, auto_flush};
323 }
324 
333 template <typename Upstream>
334 logging_resource_adaptor<Upstream> make_logging_adaptor(Upstream* upstream,
335  std::ostream& stream,
336  bool auto_flush = false)
337 {
338  return logging_resource_adaptor<Upstream>{upstream, stream, auto_flush};
339 }
340 
341 } // namespace rmm::mr
rmm::mr::logging_resource_adaptor::header
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:155
rmm::mr::logging_resource_adaptor::supports_streams
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: logging_resource_adaptor.hpp:130
rmm::mr::logging_resource_adaptor::logging_resource_adaptor
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:68
rmm::mr::logging_resource_adaptor
Resource that uses Upstream to allocate memory and logs information about the requested allocation/de...
Definition: logging_resource_adaptor.hpp:45
rmm::mr::logging_resource_adaptor::logging_resource_adaptor
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:92
rmm::mr::logging_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: logging_resource_adaptor.hpp:140
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82
rmm::mr::logging_resource_adaptor::get_upstream
Upstream * get_upstream() const noexcept
Return pointer to the upstream resource.
Definition: logging_resource_adaptor.hpp:122
rmm::mr::logging_resource_adaptor::flush
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:148