scoped_memory_record.hpp
1 
6 #pragma once
7 
8 #include <array>
9 #include <cstddef>
10 #include <type_traits>
11 
12 #include <rmm/error.hpp>
14 #include <rmm/resource_ref.hpp>
15 
16 namespace rapidsmpf {
17 
25  enum class AllocType : std::size_t {
26  PRIMARY = 0,
27  FALLBACK = 1,
28  ALL = 2
29  };
30 
32  using AllocTypeArray = std::array<std::int64_t, 2>;
33 
42  [[nodiscard]] std::int64_t num_total_allocs(
43  AllocType alloc_type = AllocType::ALL
44  ) const noexcept;
45 
56  [[nodiscard]] std::int64_t num_current_allocs(
57  AllocType alloc_type = AllocType::ALL
58  ) const noexcept;
59 
69  [[nodiscard]] std::int64_t current(
70  AllocType alloc_type = AllocType::ALL
71  ) const noexcept;
72 
82  [[nodiscard]] std::int64_t total(
83  AllocType alloc_type = AllocType::ALL
84  ) const noexcept;
85 
99  [[nodiscard]] std::int64_t peak(AllocType alloc_type = AllocType::ALL) const noexcept;
100 
112  void record_allocation(AllocType alloc_type, std::int64_t nbytes);
113 
124  void record_deallocation(AllocType alloc_type, std::int64_t nbytes);
125 
148 
165 
166  private:
167  AllocTypeArray num_current_allocs_{{0, 0}};
168  AllocTypeArray num_total_allocs_{{0, 0}};
169  AllocTypeArray current_{{0, 0}};
170  AllocTypeArray total_{{0, 0}};
171  AllocTypeArray peak_{{0, 0}};
172  std::int64_t highest_peak_{0};
173 };
174 
175 static_assert(
176  std::is_trivially_copyable_v<ScopedMemoryRecord>,
177  "ScopedMemoryRecord must be trivially copyable"
178 );
179 
180 } // namespace rapidsmpf
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
Memory statistics for a specific scope.
ScopedMemoryRecord & add_subscope(ScopedMemoryRecord const &subscope)
Merge the memory statistics of a subscope into this record.
void record_allocation(AllocType alloc_type, std::int64_t nbytes)
Records a memory allocation event.
std::array< std::int64_t, 2 > AllocTypeArray
Array type for storing per-allocator statistics.
std::int64_t peak(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the peak memory usage (in bytes) for the specified allocator type.
void record_deallocation(AllocType alloc_type, std::int64_t nbytes)
Records a memory deallocation event.
AllocType
Allocation source types.
@ PRIMARY
The primary allocator (first-choice allocator).
@ ALL
Aggregated statistics from both primary and fallback allocators.
@ FALLBACK
The fallback allocator (used when the primary fails).
ScopedMemoryRecord & add_scope(ScopedMemoryRecord const &scope)
Merge the memory statistics of another scope into this one.
std::int64_t total(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the total number of bytes allocated.
std::int64_t num_current_allocs(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the number of currently active (non-deallocated) allocations for the specified allocator type...
std::int64_t current(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the current memory usage in bytes for the specified allocator type.
std::int64_t num_total_allocs(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the total number of allocations performed by the specified allocator type.