19 #include <rmm/detail/error.hpp>
23 #include <spdlog/common.h>
24 #include <spdlog/sinks/basic_file_sink.h>
25 #include <spdlog/sinks/ostream_sink.h>
26 #include <spdlog/spdlog.h>
31 #include <string_view>
50 template <
typename Upstream>
76 bool auto_flush =
false)
77 : logger_{make_logger(filename)}, upstream_{upstream}
79 RMM_EXPECTS(
nullptr != upstream,
"Unexpected null upstream resource pointer.");
81 init_logger(auto_flush);
99 : logger_{make_logger(stream)}, upstream_{upstream}
101 RMM_EXPECTS(
nullptr != upstream,
"Unexpected null upstream resource pointer.");
103 init_logger(auto_flush);
121 spdlog::sinks_init_list sinks,
122 bool auto_flush =
false)
123 : logger_{make_logger(sinks)}, upstream_{upstream}
125 RMM_EXPECTS(
nullptr != upstream,
"Unexpected null upstream resource pointer.");
127 init_logger(auto_flush);
144 [[nodiscard]] Upstream*
get_upstream() const noexcept {
return upstream_; }
154 return upstream_->supports_streams();
164 return upstream_->supports_get_mem_info();
179 return std::string{
"Thread,Time,Action,Pointer,Size,Stream"};
191 auto* filename = std::getenv(
"RMM_LOG_FILE");
192 RMM_EXPECTS(filename !=
nullptr,
193 "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
194 return std::string{filename};
198 static auto make_logger(std::ostream& stream)
200 return std::make_shared<spdlog::logger>(
201 "RMM", std::make_shared<spdlog::sinks::ostream_sink_mt>(stream));
204 static auto make_logger(std::string
const& filename)
206 return std::make_shared<spdlog::logger>(
207 "RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename,
true ));
210 static auto make_logger(spdlog::sinks_init_list sinks)
212 return std::make_shared<spdlog::logger>(
"RMM", sinks);
218 void init_logger(
bool auto_flush)
220 if (auto_flush) { logger_->flush_on(spdlog::level::info); }
221 logger_->set_pattern(
"%v");
223 logger_->set_pattern(
"%t,%H:%M:%S.%f,%v");
251 void* do_allocate(std::size_t bytes, cuda_stream_view stream)
override
254 auto const ptr = upstream_->allocate(bytes, stream);
255 logger_->info(
"allocate,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
258 logger_->info(
"allocate failure,{},{},{}",
nullptr, bytes, fmt::ptr(stream.value()));
277 void do_deallocate(
void* ptr, std::size_t bytes, cuda_stream_view stream)
override
279 logger_->info(
"free,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
280 upstream_->deallocate(ptr, bytes, stream);
290 [[nodiscard]]
bool do_is_equal(device_memory_resource
const& other)
const noexcept
override
292 if (
this == &other) {
return true; }
293 auto const* cast =
dynamic_cast<logging_resource_adaptor<Upstream> const*
>(&other);
294 if (cast !=
nullptr) {
return upstream_->is_equal(*cast->get_upstream()); }
295 return upstream_->is_equal(other);
306 [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
307 cuda_stream_view stream)
const override
309 return upstream_->get_mem_info(stream);
313 template <
typename T>
315 friend logging_resource_adaptor<T> make_logging_adaptor(T* upstream,
316 std::string
const& filename,
319 std::shared_ptr<spdlog::logger> logger_;
337 template <
typename Upstream>
341 bool auto_flush =
false)
357 template <
typename Upstream>
359 std::ostream& stream,
360 bool auto_flush =
false)
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
Resource that uses Upstream to allocate memory and logs information about the requested allocation/de...
Definition: logging_resource_adaptor.hpp:51
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:177
Upstream * get_upstream() const noexcept
Return pointer to the upstream resource.
Definition: logging_resource_adaptor.hpp:144
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:170
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:74
static std::string get_default_filename()
Return the value of the environment variable RMM_LOG_FILE.
Definition: logging_resource_adaptor.hpp:189
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:98
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: logging_resource_adaptor.hpp:162
logging_resource_adaptor(Upstream *upstream, spdlog::sinks_init_list 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:120
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: logging_resource_adaptor.hpp:152
logging_resource_adaptor< Upstream > make_logging_adaptor(Upstream *upstream, std::ostream &stream, bool auto_flush=false)
Convenience factory to return a logging_resource_adaptor around the upstream resource upstream.
Definition: logging_resource_adaptor.hpp:358