scoped_memory_record.hpp
1 
6 #pragma once
7 
8 #include <array>
9 #include <cstddef>
10 #include <cstdint>
11 #include <type_traits>
12 
13 namespace rapidsmpf {
14 
22  enum class AllocType : std::size_t {
23  PRIMARY = 0,
24  FALLBACK = 1,
25  ALL = 2
26  };
27 
29  using AllocTypeArray = std::array<std::int64_t, 2>;
30 
39  [[nodiscard]] std::int64_t num_total_allocs(
40  AllocType alloc_type = AllocType::ALL
41  ) const noexcept;
42 
53  [[nodiscard]] std::int64_t num_current_allocs(
54  AllocType alloc_type = AllocType::ALL
55  ) const noexcept;
56 
66  [[nodiscard]] std::int64_t current(
67  AllocType alloc_type = AllocType::ALL
68  ) const noexcept;
69 
79  [[nodiscard]] std::int64_t total(
80  AllocType alloc_type = AllocType::ALL
81  ) const noexcept;
82 
96  [[nodiscard]] std::int64_t peak(AllocType alloc_type = AllocType::ALL) const noexcept;
97 
108  [[nodiscard]] std::int64_t max(AllocType alloc_type = AllocType::ALL) const noexcept;
109 
121  void record_allocation(AllocType alloc_type, std::int64_t nbytes);
122 
133  void record_deallocation(AllocType alloc_type, std::int64_t nbytes);
134 
157 
174 
175  private:
176  AllocTypeArray num_current_allocs_{{0, 0}};
177  AllocTypeArray num_total_allocs_{{0, 0}};
178  AllocTypeArray current_{{0, 0}};
179  AllocTypeArray total_{{0, 0}};
180  AllocTypeArray peak_{{0, 0}};
181  AllocTypeArray max_{{0, 0}};
182  std::int64_t highest_peak_{0};
183 };
184 
185 static_assert(
186  std::is_trivially_copyable_v<ScopedMemoryRecord>,
187  "ScopedMemoryRecord must be trivially copyable"
188 );
189 
190 } // namespace rapidsmpf
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14
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).
std::int64_t max(AllocType alloc_type=AllocType::ALL) const noexcept
Returns the size of the largest single allocation (in bytes) for the specified allocator type.
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.