18 #include <rapidsmpf/config.hpp>
19 #include <rapidsmpf/memory/memory_type.hpp>
20 #include <rapidsmpf/rmm_resource_adaptor.hpp>
21 #include <rapidsmpf/utils/misc.hpp>
25 class StreamOrderedTiming;
118 stats_{std::move(o.stats_)},
119 formatters_{std::move(o.formatters_)} {}
128 enabled_ = o.enabled();
129 stats_ = std::move(o.stats_);
130 formatters_ = std::move(o.formatters_);
140 return enabled_.load(std::memory_order_acquire);
147 enabled_.store(
true, std::memory_order_release);
154 enabled_.store(
false, std::memory_order_release);
173 std::string
report(std::string
const& header =
"Statistics:")
const;
196 void write_json(std::filesystem::path
const& filepath)
const;
225 max_ = std::max(max_,
value);
233 [[nodiscard]] std::size_t
count() const noexcept {
242 [[nodiscard]]
double value() const noexcept {
252 [[nodiscard]]
double max() const noexcept {
257 std::size_t count_{0};
259 double max_{-std::numeric_limits<double>::infinity()};
267 using Formatter = std::function<void(std::ostream&, std::vector<Stat>
const&)>;
285 void add_stat(std::string
const& name,
double value);
328 std::string
const& report_entry_name,
329 std::vector<std::string>
const& stat_names,
494 struct FormatterEntry {
495 std::vector<std::string> stat_names;
499 mutable std::mutex mutex_;
500 std::atomic<bool> enabled_;
501 std::map<std::string, Stat> stats_;
502 std::map<std::string, FormatterEntry> formatters_;
503 std::unordered_map<std::string, MemoryRecord> memory_records_;
530 #define RAPIDSMPF_MEMORY_PROFILE(...) \
531 RAPIDSMPF_OVERLOAD_BY_ARG_COUNT( \
532 __VA_ARGS__, RAPIDSMPF_MEMORY_PROFILE_2, RAPIDSMPF_MEMORY_PROFILE_1 \
537 #define RAPIDSMPF_MEMORY_PROFILE_1(stats) RAPIDSMPF_MEMORY_PROFILE_2(stats, __func__)
540 #define RAPIDSMPF_MEMORY_PROFILE_2(stats, funcname) \
541 auto&& RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__) = (stats); \
542 auto const RAPIDSMPF_CONCAT(_rapidsmpf_memory_recorder_, __LINE__) = \
543 ((rapidsmpf::detail::to_pointer(RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__)) \
544 && rapidsmpf::detail::to_pointer( \
545 RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__) \
546 ) -> is_memory_profiling_enabled()) \
547 ? rapidsmpf::detail::to_pointer( \
548 RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__) \
550 ->create_memory_recorder( \
551 std::string(__FILE__) + ":" + RAPIDSMPF_STRINGIFY(__LINE__) + "(" \
552 + std::string(funcname) + ")" \
554 : rapidsmpf::Statistics::MemoryRecorder{})
A RMM memory resource adaptor tailored to RapidsMPF.
RAII-style object for scoped memory usage tracking.
MemoryRecorder(Statistics *stats, RmmResourceAdaptor *mr, std::string name)
Constructs an active MemoryRecorder.
MemoryRecorder(MemoryRecorder const &)=delete
Deleted copy and move constructors/assignments.
~MemoryRecorder()
Destructor.
MemoryRecorder()=default
Constructs a no-op MemoryRecorder (disabled state).
Represents a single tracked statistic.
void add(double value)
Adds a value to this statistic.
std::size_t count() const noexcept
Returns the number of updates applied to this statistic.
double max() const noexcept
Returns the maximum value seen across all add() calls.
double value() const noexcept
Returns the total accumulated value.
auto operator<=>(Stat const &) const noexcept=default
Three-way comparison operator.
Stat()=default
Default-constructs a Stat.
Tracks statistics across rapidsmpf operations.
std::string report(std::string const &header="Statistics:") const
Generates a formatted report of all collected statistics.
bool is_memory_profiling_enabled() const
Checks whether memory profiling is enabled.
Stat get_stat(std::string const &name) const
Retrieves a statistic by name.
void clear()
Clears all statistics.
MemoryRecorder create_memory_recorder(std::string name)
Creates a scoped memory recorder for the given name.
static std::shared_ptr< Statistics > disabled()
Returns a shared pointer to a disabled (no-op) Statistics instance.
bool exist_report_entry_name(std::string const &name) const
Check whether a report entry name already has a formatter registered.
void register_formatter(std::string const &report_entry_name, std::vector< std::string > const &stat_names, Formatter formatter)
Register a formatter that takes multiple named statistics.
void write_json(std::ostream &os) const
Writes a JSON representation of all collected statistics to a stream.
static std::shared_ptr< Statistics > from_options(RmmResourceAdaptor *mr, config::Options options)
Construct from configuration options.
void add_bytes_stat(std::string const &name, std::size_t nbytes)
Adds a byte count to the named statistic.
void enable() noexcept
Enable statistics tracking for this instance.
void record_alloc(MemoryType mem_type, std::size_t nbytes, StreamOrderedTiming &&timing)
Record size and wall-clock duration for a buffer allocation.
void add_duration_stat(std::string const &name, Duration seconds)
Adds a duration to the named statistic.
Statistics(RmmResourceAdaptor *mr)
Constructs a Statistics object with memory profiling enabled.
std::vector< std::string > list_stat_names() const
Get the names of all statistics.
Statistics(bool enabled=true)
Constructs a Statistics object without memory profiling.
void add_stat(std::string const &name, double value)
Adds a numeric value to the named statistic.
std::function< void(std::ostream &, std::vector< Stat > const &)> Formatter
Type alias for a statistics formatting function.
void register_formatter(std::string const &name, Formatter formatter)
Register a formatter for a single named statistic.
void record_copy(MemoryType src, MemoryType dst, std::size_t nbytes, StreamOrderedTiming &&timing)
Record byte count and wall-clock duration for a memory copy operation.
bool enabled() const noexcept
Checks if statistics tracking is enabled.
void write_json(std::filesystem::path const &filepath) const
Writes a JSON report of all collected statistics to a file.
void disable() noexcept
Disable statistics tracking for this instance.
Statistics & operator=(Statistics &&o) noexcept
Move assignment operator.
std::unordered_map< std::string, MemoryRecord > const & get_memory_records() const
Retrieves all memory profiling records stored by this instance.
Stream-ordered wall-clock timer that records its result into Statistics.
Manages configuration options for RapidsMPF operations.
RAPIDS Multi-Processor interfaces.
std::chrono::duration< double > Duration
Alias for a duration type representing time in seconds as a double.
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Memory statistics for a specific scope.
Holds memory profiling information for a named scope.
std::int64_t global_peak
Peak global memory usage during the scope.
ScopedMemoryRecord scoped
Scoped memory stats.
std::uint64_t num_calls
Number of times the scope was invoked.