Skip to main content

cudf/core/memory/
usage.rs

1//! Usage-counter snapshots for resources and query domains.
2
3use crate::DeviceId;
4
5/// Usage counters reported by a production budget-tracking memory resource.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct DeviceMemoryResourceUsage {
8    pub allocation_limit_bytes: u64,
9    pub current_bytes: u64,
10    pub peak_bytes: u64,
11    pub total_bytes: u64,
12    pub current_aligned_bytes: u64,
13    pub peak_aligned_bytes: u64,
14    pub total_allocations: u64,
15    pub current_allocations: u64,
16    pub peak_allocations: u64,
17    pub failed_allocation_count: u64,
18    pub largest_failed_allocation_bytes: u64,
19}
20
21/// Physical CUDA async-pool counters.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct DeviceMemoryPoolUsage {
24    pub reserved_current_bytes: u64,
25    pub reserved_high_bytes: u64,
26    pub used_current_bytes: u64,
27    pub used_high_bytes: u64,
28}
29
30impl From<cudf_sys::cudf_memory_resource_usage_t> for DeviceMemoryResourceUsage {
31    fn from(value: cudf_sys::cudf_memory_resource_usage_t) -> Self {
32        Self {
33            allocation_limit_bytes: value.allocation_limit_bytes,
34            current_bytes: value.current_bytes,
35            peak_bytes: value.peak_bytes,
36            total_bytes: value.total_bytes,
37            current_aligned_bytes: value.current_aligned_bytes,
38            peak_aligned_bytes: value.peak_aligned_bytes,
39            total_allocations: value.total_allocations,
40            current_allocations: value.current_allocations,
41            peak_allocations: value.peak_allocations,
42            failed_allocation_count: value.failed_allocation_count,
43            largest_failed_allocation_bytes: value.largest_failed_allocation_bytes,
44        }
45    }
46}
47
48impl From<cudf_sys::cudf_memory_pool_usage_t> for DeviceMemoryPoolUsage {
49    fn from(value: cudf_sys::cudf_memory_pool_usage_t) -> Self {
50        Self {
51            reserved_current_bytes: value.reserved_current_bytes,
52            reserved_high_bytes: value.reserved_high_bytes,
53            used_current_bytes: value.used_current_bytes,
54            used_high_bytes: value.used_high_bytes,
55        }
56    }
57}
58
59/// Usage counters for a query-domain stream member.
60///
61/// These counters are recorded by the device-level reservation-aware memory
62/// manager. `current_*` counters include retained allocations that were
63/// created while the reservation was attached, even after the reservation has
64/// been detached from its stream.
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct ReservationUsage {
67    /// Native identity of this query-domain stream member.
68    pub reservation_id: u64,
69    /// Whether the member remains attached to its stream; detached allocations can remain retained.
70    pub attached: bool,
71    /// Allocations made through a query-domain member's direct resource.
72    pub direct_total_allocations: u64,
73    /// Allocations routed from the device-current resource into a query-domain
74    /// member.
75    pub routed_total_allocations: u64,
76    /// Enforced live allocation limit in bytes, charged using aligned allocation sizes.
77    pub allocation_limit_bytes: u64,
78    /// Requested bytes in allocations still retained at this snapshot.
79    pub current_bytes: u64,
80    /// Largest observed sum of requested bytes in simultaneously retained allocations.
81    pub peak_bytes: u64,
82    /// Cumulative requested bytes across successful allocations, including allocations later released.
83    pub total_bytes: u64,
84    /// Live allocation charge in bytes after alignment.
85    pub current_aligned_bytes: u64,
86    /// Largest observed live allocation charge in bytes after alignment.
87    pub peak_aligned_bytes: u64,
88    /// Cumulative count of successful allocations, including allocations later released.
89    pub total_allocations: u64,
90    /// Number of allocations still retained at this snapshot.
91    pub current_allocations: u64,
92    /// Largest observed number of simultaneously retained allocations.
93    pub peak_allocations: u64,
94    /// Number of allocation requests rejected by a limit or the upstream allocator.
95    pub failed_allocation_count: u64,
96    /// Largest failed allocation request in bytes; zero when no allocation failure was recorded.
97    pub largest_failed_allocation_bytes: u64,
98}
99
100/// Stable native identity for one query-memory-domain instance.
101///
102/// `generation` is distinct from the caller-supplied domain id and changes for
103/// every native domain creation. Consumers must retain both values when
104/// correlating asynchronous allocator events.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub struct QueryMemoryDomainIdentity {
107    pub device: DeviceId,
108    pub domain_id: u64,
109    pub generation: u64,
110}
111
112/// Usage counters for an opt-in query memory domain.
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub struct QueryMemoryDomainUsage {
115    /// Caller-supplied query-memory-domain identity.
116    pub domain_id: u64,
117    /// Whether the domain remains active for allocation.
118    pub active: bool,
119    /// Number of stream members currently attached to the domain.
120    pub active_members: u64,
121    /// Immutable logical ceiling declared when this domain was created, in bytes.
122    pub ceiling_bytes: u64,
123    /// Enforced live allocation limit in bytes, charged using aligned allocation sizes.
124    pub allocation_limit_bytes: u64,
125    /// Requested bytes in allocations still retained at this snapshot.
126    pub current_bytes: u64,
127    /// Largest observed sum of requested bytes in simultaneously retained allocations.
128    pub peak_bytes: u64,
129    /// Cumulative requested bytes across successful allocations, including allocations later released.
130    pub total_bytes: u64,
131    /// Live allocation charge in bytes after alignment.
132    pub current_aligned_bytes: u64,
133    /// Largest observed live allocation charge in bytes after alignment.
134    pub peak_aligned_bytes: u64,
135    /// Cumulative count of successful allocations, including allocations later released.
136    pub total_allocations: u64,
137    /// Number of allocations still retained at this snapshot.
138    pub current_allocations: u64,
139    /// Largest observed number of simultaneously retained allocations.
140    pub peak_allocations: u64,
141    /// Number of allocation requests rejected by a limit or the upstream allocator.
142    pub failed_allocation_count: u64,
143    /// Largest failed allocation request in bytes; zero when no allocation failure was recorded.
144    pub largest_failed_allocation_bytes: u64,
145}
146
147impl From<cudf_sys::cudf_memory_resource_usage_t> for ReservationUsage {
148    fn from(value: cudf_sys::cudf_memory_resource_usage_t) -> Self {
149        Self {
150            reservation_id: value.reserved[0],
151            attached: value.reserved[1] != 0,
152            direct_total_allocations: value.reserved[2],
153            routed_total_allocations: value.reserved[3],
154            allocation_limit_bytes: value.allocation_limit_bytes,
155            current_bytes: value.current_bytes,
156            peak_bytes: value.peak_bytes,
157            total_bytes: value.total_bytes,
158            current_aligned_bytes: value.current_aligned_bytes,
159            peak_aligned_bytes: value.peak_aligned_bytes,
160            total_allocations: value.total_allocations,
161            current_allocations: value.current_allocations,
162            peak_allocations: value.peak_allocations,
163            failed_allocation_count: value.failed_allocation_count,
164            largest_failed_allocation_bytes: value.largest_failed_allocation_bytes,
165        }
166    }
167}
168
169impl From<cudf_sys::cudf_memory_resource_usage_t> for QueryMemoryDomainUsage {
170    fn from(value: cudf_sys::cudf_memory_resource_usage_t) -> Self {
171        Self {
172            domain_id: value.reserved[0],
173            active: value.reserved[1] != 0,
174            active_members: value.reserved[2],
175            ceiling_bytes: value.reserved[3],
176            allocation_limit_bytes: value.allocation_limit_bytes,
177            current_bytes: value.current_bytes,
178            peak_bytes: value.peak_bytes,
179            total_bytes: value.total_bytes,
180            current_aligned_bytes: value.current_aligned_bytes,
181            peak_aligned_bytes: value.peak_aligned_bytes,
182            total_allocations: value.total_allocations,
183            current_allocations: value.current_allocations,
184            peak_allocations: value.peak_allocations,
185            failed_allocation_count: value.failed_allocation_count,
186            largest_failed_allocation_bytes: value.largest_failed_allocation_bytes,
187        }
188    }
189}
190
191impl From<ReservationUsage> for DeviceMemoryResourceUsage {
192    fn from(value: ReservationUsage) -> Self {
193        Self {
194            allocation_limit_bytes: value.allocation_limit_bytes,
195            current_bytes: value.current_bytes,
196            peak_bytes: value.peak_bytes,
197            total_bytes: value.total_bytes,
198            current_aligned_bytes: value.current_aligned_bytes,
199            peak_aligned_bytes: value.peak_aligned_bytes,
200            total_allocations: value.total_allocations,
201            current_allocations: value.current_allocations,
202            peak_allocations: value.peak_allocations,
203            failed_allocation_count: value.failed_allocation_count,
204            largest_failed_allocation_bytes: value.largest_failed_allocation_bytes,
205        }
206    }
207}
208
209/// One allocation a query memory domain's registry still owns.
210#[derive(Debug, Clone, Copy, PartialEq, Eq)]
211pub struct QueryMemoryDomainAllocation {
212    /// Base device pointer the allocator registered.
213    pub device_pointer: usize,
214    /// Requested bytes for this allocation.
215    pub bytes: u64,
216    /// Charged bytes after alignment.
217    pub aligned_bytes: u64,
218    /// Alignment requested for this allocation.
219    pub alignment: u64,
220}
221
222impl From<cudf_sys::cudf_query_memory_domain_allocation_t> for QueryMemoryDomainAllocation {
223    fn from(value: cudf_sys::cudf_query_memory_domain_allocation_t) -> Self {
224        Self {
225            device_pointer: value.device_pointer as usize,
226            bytes: value.bytes as u64,
227            aligned_bytes: value.aligned_bytes as u64,
228            alignment: value.alignment as u64,
229        }
230    }
231}