grafeo_core/execution/
memory.rs1use grafeo_common::memory::buffer::{BufferManager, MemoryGrant, MemoryRegion, PressureLevel};
4use std::sync::Arc;
5
6pub const DEFAULT_CHUNK_SIZE: usize = 2048;
8
9pub const MODERATE_PRESSURE_CHUNK_SIZE: usize = 1024;
11
12pub const HIGH_PRESSURE_CHUNK_SIZE: usize = 512;
14
15pub const CRITICAL_PRESSURE_CHUNK_SIZE: usize = 256;
17
18pub struct ExecutionMemoryContext {
23 manager: Arc<BufferManager>,
25 allocated: usize,
27 grants: Vec<MemoryGrant>,
29}
30
31impl ExecutionMemoryContext {
32 #[must_use]
34 pub fn new(manager: Arc<BufferManager>) -> Self {
35 Self {
36 manager,
37 allocated: 0,
38 grants: Vec::new(),
39 }
40 }
41
42 pub fn allocate(&mut self, size: usize) -> Option<MemoryGrant> {
46 let grant = self
47 .manager
48 .try_allocate(size, MemoryRegion::ExecutionBuffers)?;
49 self.allocated += size;
50 Some(grant)
51 }
52
53 pub fn allocate_tracked(&mut self, size: usize) -> bool {
57 if let Some(grant) = self
58 .manager
59 .try_allocate(size, MemoryRegion::ExecutionBuffers)
60 {
61 self.allocated += size;
62 self.grants.push(grant);
63 true
64 } else {
65 false
66 }
67 }
68
69 #[must_use]
71 pub fn pressure_level(&self) -> PressureLevel {
72 self.manager.pressure_level()
73 }
74
75 #[must_use]
77 pub fn should_reduce_chunk_size(&self) -> bool {
78 matches!(
79 self.pressure_level(),
80 PressureLevel::High | PressureLevel::Critical
81 )
82 }
83
84 #[must_use]
86 pub fn adjusted_chunk_size(&self, requested: usize) -> usize {
87 match self.pressure_level() {
88 PressureLevel::Normal => requested,
89 PressureLevel::Moderate => requested.min(MODERATE_PRESSURE_CHUNK_SIZE),
90 PressureLevel::High => requested.min(HIGH_PRESSURE_CHUNK_SIZE),
91 PressureLevel::Critical => requested.min(CRITICAL_PRESSURE_CHUNK_SIZE),
92 _ => requested.min(CRITICAL_PRESSURE_CHUNK_SIZE),
93 }
94 }
95
96 #[must_use]
98 pub fn optimal_chunk_size(&self) -> usize {
99 self.adjusted_chunk_size(DEFAULT_CHUNK_SIZE)
100 }
101
102 #[must_use]
104 pub fn total_allocated(&self) -> usize {
105 self.allocated
106 }
107
108 #[must_use]
110 pub fn manager(&self) -> &Arc<BufferManager> {
111 &self.manager
112 }
113
114 pub fn release_all(&mut self) {
116 self.grants.clear();
117 self.allocated = 0;
118 }
119}
120
121impl Drop for ExecutionMemoryContext {
122 fn drop(&mut self) {
123 self.grants.clear();
125 }
126}
127
128pub struct ExecutionMemoryContextBuilder {
130 manager: Arc<BufferManager>,
131 initial_allocation: usize,
132}
133
134impl ExecutionMemoryContextBuilder {
135 #[must_use]
137 pub fn new(manager: Arc<BufferManager>) -> Self {
138 Self {
139 manager,
140 initial_allocation: 0,
141 }
142 }
143
144 #[must_use]
146 pub fn with_initial_allocation(mut self, size: usize) -> Self {
147 self.initial_allocation = size;
148 self
149 }
150
151 pub fn build(self) -> Option<ExecutionMemoryContext> {
155 let mut ctx = ExecutionMemoryContext::new(self.manager);
156
157 if self.initial_allocation > 0 && !ctx.allocate_tracked(self.initial_allocation) {
158 return None;
159 }
160
161 Some(ctx)
162 }
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168 use grafeo_common::memory::buffer::BufferManagerConfig;
169
170 #[test]
171 fn test_execution_context_creation() {
172 let manager = BufferManager::with_budget(1024 * 1024);
173 let ctx = ExecutionMemoryContext::new(manager);
174
175 assert_eq!(ctx.total_allocated(), 0);
176 assert_eq!(ctx.pressure_level(), PressureLevel::Normal);
177 }
178
179 #[test]
180 fn test_execution_context_allocation() {
181 let manager = BufferManager::with_budget(1024 * 1024);
182 let mut ctx = ExecutionMemoryContext::new(manager);
183
184 let grant = ctx.allocate(1024);
185 assert!(grant.is_some());
186 assert_eq!(ctx.total_allocated(), 1024);
187 }
188
189 #[test]
190 fn test_execution_context_tracked_allocation() {
191 let manager = BufferManager::with_budget(1024 * 1024);
192 let mut ctx = ExecutionMemoryContext::new(manager);
193
194 assert!(ctx.allocate_tracked(1024));
195 assert_eq!(ctx.total_allocated(), 1024);
196
197 ctx.release_all();
198 assert_eq!(ctx.total_allocated(), 0);
199 }
200
201 #[test]
202 fn test_adjusted_chunk_size_normal() {
203 let manager = BufferManager::with_budget(1024 * 1024);
204 let ctx = ExecutionMemoryContext::new(manager);
205
206 assert_eq!(ctx.adjusted_chunk_size(2048), 2048);
207 assert_eq!(ctx.optimal_chunk_size(), 2048);
208 }
209
210 #[test]
211 fn test_adjusted_chunk_size_under_pressure() {
212 let config = BufferManagerConfig {
213 budget: 1000,
214 soft_limit_fraction: 0.70,
215 evict_limit_fraction: 0.85,
216 hard_limit_fraction: 0.95,
217 background_eviction: false,
218 spill_path: None,
219 };
220 let manager = BufferManager::new(config);
221
222 let _g = manager.try_allocate(860, MemoryRegion::ExecutionBuffers);
224
225 let ctx = ExecutionMemoryContext::new(manager);
226 assert_eq!(ctx.pressure_level(), PressureLevel::High);
227 assert_eq!(ctx.adjusted_chunk_size(2048), HIGH_PRESSURE_CHUNK_SIZE);
228 assert!(ctx.should_reduce_chunk_size());
229 }
230
231 #[test]
232 fn test_builder() {
233 let manager = BufferManager::with_budget(1024 * 1024);
234
235 let ctx = ExecutionMemoryContextBuilder::new(manager)
236 .with_initial_allocation(4096)
237 .build();
238
239 assert!(ctx.is_some());
240 let ctx = ctx.unwrap();
241 assert_eq!(ctx.total_allocated(), 4096);
242 }
243
244 #[test]
245 fn test_builder_insufficient_memory() {
246 let manager = BufferManager::with_budget(1000);
247
248 let ctx = ExecutionMemoryContextBuilder::new(manager)
250 .with_initial_allocation(10000)
251 .build();
252
253 assert!(ctx.is_none());
254 }
255}