statistics.hpp
1 
5 #pragma once
6 #include <atomic>
7 #include <cstddef>
8 #include <filesystem>
9 #include <initializer_list>
10 #include <limits>
11 #include <map>
12 #include <memory>
13 #include <mutex>
14 #include <optional>
15 #include <ostream>
16 #include <span>
17 #include <string>
18 #include <string_view>
19 #include <utility>
20 #include <vector>
21 
22 #include <rapidsmpf/config.hpp>
23 #include <rapidsmpf/memory/memory_type.hpp>
24 #include <rapidsmpf/memory/pinned_memory_resource.hpp>
25 #include <rapidsmpf/memory/resource_types.hpp>
26 #include <rapidsmpf/rmm_resource_adaptor.hpp>
27 #include <rapidsmpf/utils/misc.hpp>
28 
29 namespace rapidsmpf {
30 
31 class StreamOrderedTiming;
32 
72 class Statistics {
73  public:
100  enum class Formatter : std::uint8_t {
101  Default = 0,
102  Bytes,
103  Duration,
104  HitRate,
105  MemoryThroughput,
106  _Count,
107  };
108 
115  Statistics(bool enabled = true);
116 
124  static std::shared_ptr<Statistics> from_options(config::Options options);
125 
126  ~Statistics() noexcept;
127 
128  // `Statistics` is owned exclusively through `std::shared_ptr` (see `disabled()` and
129  // `from_options()`).
130  Statistics(Statistics const&) = delete;
131  Statistics& operator=(Statistics const&) = delete;
132  Statistics(Statistics&&) = delete;
133  Statistics& operator=(Statistics&&) = delete;
134 
143  static std::shared_ptr<Statistics> disabled();
144 
150  bool enabled() const noexcept {
151  return enabled_.load(std::memory_order_acquire);
152  }
153 
157  void enable() noexcept {
158  enabled_.store(true, std::memory_order_release);
159  }
160 
164  void disable() noexcept {
165  enabled_.store(false, std::memory_order_release);
166  }
167 
177  struct ReportArgs {
181  std::optional<any_device_resource> mr = std::nullopt;
184  std::optional<any_host_device_resource> pinned_mr = std::nullopt;
186  std::string_view header = "Statistics:";
187  };
188 
207  std::string report(ReportArgs report_args) const;
208 
213  std::string report() const {
214  return report(ReportArgs{});
215  }
216 
229  void write_json(std::ostream& os) const;
230 
237  void write_json(std::filesystem::path const& filepath) const;
238 
246  [[nodiscard]] std::shared_ptr<Statistics> copy() const;
247 
255  [[nodiscard]] std::vector<std::uint8_t> serialize() const;
256 
266  [[nodiscard]] static std::shared_ptr<Statistics> deserialize(
267  std::span<std::uint8_t const> data
268  );
269 
291  [[nodiscard]] static std::shared_ptr<Statistics> merge(
292  std::span<std::shared_ptr<Statistics> const> stats
293  );
294 
301  class Stat {
302  public:
306  Stat() = default;
307 
315  Stat(std::size_t count, double value, double max);
316 
324  auto operator<=>(Stat const&) const noexcept = default;
325 
331  void add(double value);
332 
338  [[nodiscard]] std::size_t count() const noexcept;
339 
345  [[nodiscard]] double value() const noexcept;
346 
353  [[nodiscard]] double max() const noexcept;
354 
363  [[nodiscard]] static constexpr std::size_t serialized_size() noexcept {
364  return sizeof(std::uint64_t) + sizeof(double) + sizeof(double);
365  }
366 
374  std::uint8_t* serialize(std::uint8_t* out) const;
375 
385  [[nodiscard]] static std::pair<Stat, std::span<std::uint8_t const>> deserialize(
386  std::span<std::uint8_t const> data
387  );
388 
397  [[nodiscard]] Stat merge(Stat const& other) const;
398 
399  private:
400  std::size_t count_{0};
401  double value_{0};
402  double max_{-std::numeric_limits<double>::infinity()};
403  };
404 
411  Stat get_stat(std::string const& name) const;
412 
423  void add_stat(std::string const& name, double value);
424 
442  std::string const& report_entry_name,
443  std::initializer_list<std::string_view> stat_names,
444  Formatter formatter
445  );
446 
447  // clang-format off
453  // clang-format on
455  std::string const& report_entry_name,
456  std::vector<std::string> stat_names,
457  Formatter formatter
458  );
459 
470  void add_bytes_stat(std::string const& name, std::size_t nbytes);
471 
482  void add_duration_stat(std::string const& name, Duration seconds);
483 
505  MemoryType src, MemoryType dst, std::size_t nbytes, StreamOrderedTiming&& timing
506  );
507 
526  MemoryType mem_type, std::size_t nbytes, StreamOrderedTiming&& timing
527  );
528 
534  std::vector<std::string> list_stat_names() const;
535 
541  void clear();
542 
543  // TODO: move MemoryRecord and MemoryRecorder to RmmResourceAdaptor?
544 
548  struct MemoryRecord {
550  std::int64_t global_peak{0};
551  std::uint64_t num_calls{0};
552  };
553 
560  public:
564  MemoryRecorder() = default;
565 
573  MemoryRecorder(Statistics* stats, RmmResourceAdaptor mr, std::string name);
574 
581 
583  MemoryRecorder(MemoryRecorder const&) = delete;
584  MemoryRecorder& operator=(MemoryRecorder const&) = delete;
585  MemoryRecorder(MemoryRecorder&&) = delete;
586  MemoryRecorder& operator=(MemoryRecorder&&) = delete;
587 
588  private:
589  Statistics* stats_{
590  nullptr
591  }; // TODO: make this shared_ptr using make_shared_from_this
592  std::optional<RmmResourceAdaptor>
593  mr_; // optional because RmmResourceAdaptor is not default constructible
594  std::string name_;
595  };
596 
608  std::optional<any_device_resource> mr, std::string name
609  );
610 
616  std::unordered_map<std::string, MemoryRecord> const& get_memory_records() const;
617 
618  private:
622  struct ReportEntry {
623  std::vector<std::string> stat_names;
624  Formatter formatter;
625  };
626 
627  mutable std::mutex mutex_;
628  std::atomic<bool> enabled_;
629  std::map<std::string, Stat> stats_;
630  std::map<std::string, ReportEntry> report_entries_;
631  std::unordered_map<std::string, MemoryRecord> memory_records_;
632 };
633 
658 // clang-format off
659 // Picks between _2 (stats, mr) and _3 (stats, mr, funcname) forms.
660 #define RAPIDSMPF_MEMORY_PROFILE_PICK_(_1, _2, _3, NAME, ...) NAME
661 #define RAPIDSMPF_MEMORY_PROFILE(...) \
662  RAPIDSMPF_MEMORY_PROFILE_PICK_( \
663  __VA_ARGS__, RAPIDSMPF_MEMORY_PROFILE_3, RAPIDSMPF_MEMORY_PROFILE_2, ~ \
664  )(__VA_ARGS__)
665 // clang-format on
666 
667 // Version with default function name (__func__)
668 #define RAPIDSMPF_MEMORY_PROFILE_2(stats, mr) \
669  RAPIDSMPF_MEMORY_PROFILE_3(stats, mr, __func__)
670 
671 // Version with custom function name
672 #define RAPIDSMPF_MEMORY_PROFILE_3(stats, mr, funcname) \
673  auto&& RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__) = (stats); \
674  auto const RAPIDSMPF_CONCAT(_rapidsmpf_memory_recorder_, __LINE__) = \
675  (rapidsmpf::detail::to_pointer(RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__))) \
676  ? rapidsmpf::detail::to_pointer( \
677  RAPIDSMPF_CONCAT(_rapidsmpf_stats_, __LINE__) \
678  ) \
679  -> create_memory_recorder( \
680  (mr), \
681  std::string(__FILE__) + ":" + RAPIDSMPF_STRINGIFY(__LINE__) + "(" \
682  + std::string(funcname) + ")" \
683  ) \
684  : rapidsmpf::Statistics::MemoryRecorder {}
685 
686 } // namespace rapidsmpf
A RMM memory resource adaptor tailored to RapidsMPF.
RAII-style object for scoped memory usage tracking.
Definition: statistics.hpp:559
MemoryRecorder(MemoryRecorder const &)=delete
Deleted copy and move constructors/assignments.
MemoryRecorder()=default
Constructs a no-op MemoryRecorder (disabled state).
MemoryRecorder(Statistics *stats, RmmResourceAdaptor mr, std::string name)
Constructs an active MemoryRecorder.
Represents a single tracked statistic.
Definition: statistics.hpp:301
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.
std::uint8_t * serialize(std::uint8_t *out) const
Serializes this Stat to a byte buffer.
Stat merge(Stat const &other) const
Merges another Stat into this one, returning the combined result.
Stat(std::size_t count, double value, double max)
Constructs a Stat with explicit field values.
double value() const noexcept
Returns the total accumulated value.
auto operator<=>(Stat const &) const noexcept=default
Three-way comparison operator.
static std::pair< Stat, std::span< std::uint8_t const > > deserialize(std::span< std::uint8_t const > data)
Deserializes a Stat from a byte buffer.
Stat()=default
Default-constructs a Stat.
static constexpr std::size_t serialized_size() noexcept
Returns the serialized size of this Stat in bytes.
Definition: statistics.hpp:363
Tracks statistics across rapidsmpf operations.
Definition: statistics.hpp:72
static std::shared_ptr< Statistics > from_options(config::Options options)
Construct from configuration options.
Formatter
Identifies a predefined formatter used by report().
Definition: statistics.hpp:100
@ _Count
Sentinel; must remain last.
Stat get_stat(std::string const &name) const
Retrieves a statistic by name.
void clear()
Clears all statistics.
static std::shared_ptr< Statistics > disabled()
Returns a shared pointer to a disabled (no-op) Statistics instance.
void write_json(std::ostream &os) const
Writes a JSON representation of all collected statistics to a stream.
std::shared_ptr< Statistics > copy() const
Creates a deep copy of this Statistics object.
void add_bytes_stat(std::string const &name, std::size_t nbytes)
Adds a byte count to the named statistic.
std::string report() const
Overload with all-default options. Equivalent to report(ReportArgs{}).
Definition: statistics.hpp:213
void enable() noexcept
Enable statistics tracking for this instance.
Definition: statistics.hpp:157
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.
MemoryRecorder create_memory_recorder(std::optional< any_device_resource > mr, std::string name)
Creates a scoped memory recorder for the given name.
void add_report_entry(std::string const &report_entry_name, std::vector< std::string > stat_names, Formatter formatter)
Associate a formatter with one or more named statistics for report rendering.
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.
static std::shared_ptr< Statistics > merge(std::span< std::shared_ptr< Statistics > const > stats)
Merge a set of Statistics into a new instance.
std::vector< std::uint8_t > serialize() const
Serializes the stats and report entries to a binary byte vector.
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.
Definition: statistics.hpp:150
std::string report(ReportArgs report_args) const
Generates a formatted report of all collected statistics.
void write_json(std::filesystem::path const &filepath) const
Writes a JSON report of all collected statistics to a file.
static std::shared_ptr< Statistics > deserialize(std::span< std::uint8_t const > data)
Deserializes a Statistics object from a binary byte vector.
void disable() noexcept
Disable statistics tracking for this instance.
Definition: statistics.hpp:164
void add_report_entry(std::string const &report_entry_name, std::initializer_list< std::string_view > stat_names, Formatter formatter)
Associate a formatter with one or more named statistics for report rendering.
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.
Definition: config.hpp:140
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14
std::chrono::duration< double > Duration
Alias for a duration type representing time in seconds as a double.
Definition: misc.hpp:33
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Definition: memory_type.hpp:16
Memory statistics for a specific scope.
Holds memory profiling information for a named scope.
Definition: statistics.hpp:548
std::int64_t global_peak
Peak global memory usage during the scope.
Definition: statistics.hpp:550
ScopedMemoryRecord scoped
Scoped memory stats.
Definition: statistics.hpp:549
std::uint64_t num_calls
Number of times the scope was invoked.
Definition: statistics.hpp:551
Named-argument struct for report().
Definition: statistics.hpp:177
std::optional< any_device_resource > mr
Definition: statistics.hpp:181
std::string_view header
Header line prepended to the report.
Definition: statistics.hpp:186
std::optional< any_host_device_resource > pinned_mr
Definition: statistics.hpp:184