19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/detail/format.hpp>
25 #include <spdlog/common.h>
26 #include <spdlog/sinks/basic_file_sink.h>
27 #include <spdlog/sinks/ostream_sink.h>
28 #include <spdlog/spdlog.h>
34 #include <string_view>
36 namespace RMM_NAMESPACE {
54 template <
typename Upstream>
79 std::string
const& filename = get_default_filename(),
80 bool auto_flush =
false)
83 init_logger(auto_flush);
103 init_logger(auto_flush);
121 spdlog::sinks_init_list sinks,
122 bool auto_flush =
false)
125 init_logger(auto_flush);
149 std::string
const& filename = get_default_filename(),
150 bool auto_flush =
false)
151 : logger_{make_logger(filename)}, upstream_{upstream}
153 init_logger(auto_flush);
169 std::ostream& stream,
170 bool auto_flush =
false)
171 : logger_{make_logger(stream)}, upstream_{upstream}
173 init_logger(auto_flush);
189 spdlog::sinks_init_list sinks,
190 bool auto_flush =
false)
191 : logger_{make_logger(sinks)}, upstream_{upstream}
193 init_logger(auto_flush);
225 return std::string{
"Thread,Time,Action,Pointer,Size,Stream"};
237 auto* filename = std::getenv(
"RMM_LOG_FILE");
238 RMM_EXPECTS(filename !=
nullptr,
239 "RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
240 return std::string{filename};
244 static auto make_logger(std::ostream& stream)
246 return std::make_shared<spdlog::logger>(
247 "RMM", std::make_shared<spdlog::sinks::ostream_sink_mt>(stream));
250 static auto make_logger(std::string
const& filename)
252 return std::make_shared<spdlog::logger>(
253 "RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename,
true ));
256 static auto make_logger(spdlog::sinks_init_list sinks)
258 return std::make_shared<spdlog::logger>(
"RMM", sinks);
264 void init_logger(
bool auto_flush)
266 if (auto_flush) { logger_->flush_on(spdlog::level::info); }
267 logger_->set_pattern(
"%v");
268 logger_->info(header());
269 logger_->set_pattern(
"%t,%H:%M:%S.%f,%v");
297 void* do_allocate(std::size_t bytes, cuda_stream_view stream)
override
300 auto const ptr = get_upstream_resource().allocate_async(bytes, stream);
301 logger_->info(rmm::detail::formatted_log(
302 "allocate,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream)));
305 logger_->info(rmm::detail::formatted_log(
306 "allocate failure,%p,%zu,%s",
nullptr, bytes, rmm::detail::format_stream(stream)));
325 void do_deallocate(
void* ptr, std::size_t bytes, cuda_stream_view stream)
override
328 rmm::detail::formatted_log(
"free,%p,%zu,%s", ptr, bytes, rmm::detail::format_stream(stream)));
329 get_upstream_resource().deallocate_async(ptr, bytes, stream);
339 [[nodiscard]]
bool do_is_equal(device_memory_resource
const& other)
const noexcept
override
341 if (
this == &other) {
return true; }
342 auto const* cast =
dynamic_cast<logging_resource_adaptor<Upstream> const*
>(&other);
343 if (cast ==
nullptr) {
return false; }
344 return get_upstream_resource() == cast->get_upstream_resource();
347 std::shared_ptr<spdlog::logger> logger_;
365 template <
typename Upstream>
367 "make_logging_adaptor is deprecated in RMM 24.10. Use the logging_resource_adaptor constructor "
372 bool auto_flush =
false)
388 template <
typename Upstream>
390 "make_logging_adaptor is deprecated in RMM 24.10. Use the logging_resource_adaptor constructor "
393 std::ostream& stream,
394 bool auto_flush =
false)
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:55
std::string header() const
Return the CSV header string.
Definition: logging_resource_adaptor.hpp:223
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:148
void flush()
Flush logger contents.
Definition: logging_resource_adaptor.hpp:216
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:78
static std::string get_default_filename()
Return the value of the environment variable RMM_LOG_FILE.
Definition: logging_resource_adaptor.hpp:235
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:100
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
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:168
logging_resource_adaptor(device_async_resource_ref 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:188
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:392
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