ruvector_memopt/platform/
mod.rs1#[derive(Debug, Clone)]
7pub struct MemoryStatus {
8 pub total_physical_mb: f64,
9 pub available_physical_mb: f64,
10 pub memory_load_percent: u32,
11}
12
13impl MemoryStatus {
14 pub fn used_physical_mb(&self) -> f64 {
15 self.total_physical_mb - self.available_physical_mb
16 }
17
18 pub fn is_high_pressure(&self) -> bool {
19 self.memory_load_percent > 80
20 }
21
22 pub fn is_critical(&self) -> bool {
23 self.memory_load_percent > 95
24 }
25}
26
27#[derive(Debug, Clone)]
29pub struct OptimizationResult {
30 pub freed_mb: f64,
31 pub before_available_mb: f64,
32 pub after_available_mb: f64,
33 pub processes_affected: usize,
34 pub duration_ms: u64,
35}
36
37pub trait MemoryOptimizer: Send + Sync {
39 fn get_memory_status(&self) -> Result<MemoryStatus, String>;
41
42 fn optimize(&self, aggressive: bool) -> Result<OptimizationResult, String>;
44
45 fn has_elevated_privileges(&self) -> bool;
47
48 fn platform_name(&self) -> &'static str;
50}
51
52pub fn create_optimizer() -> Box<dyn MemoryOptimizer> {
54 #[cfg(target_os = "windows")]
55 {
56 Box::new(WindowsOptimizer::new())
57 }
58
59 #[cfg(target_os = "macos")]
60 {
61 Box::new(MacOptimizer::new())
62 }
63
64 #[cfg(not(any(target_os = "windows", target_os = "macos")))]
65 {
66 Box::new(StubOptimizer::new())
67 }
68}
69
70#[cfg(target_os = "windows")]
75pub struct WindowsOptimizer {
76 inner: crate::windows::memory::WindowsMemoryOptimizer,
77}
78
79#[cfg(target_os = "windows")]
80impl WindowsOptimizer {
81 pub fn new() -> Self {
82 Self {
83 inner: crate::windows::memory::WindowsMemoryOptimizer::new(),
84 }
85 }
86}
87
88#[cfg(target_os = "windows")]
89impl MemoryOptimizer for WindowsOptimizer {
90 fn get_memory_status(&self) -> Result<MemoryStatus, String> {
91 let status = crate::windows::memory::WindowsMemoryOptimizer::get_memory_status()?;
92 Ok(MemoryStatus {
93 total_physical_mb: status.total_physical_mb,
94 available_physical_mb: status.available_physical_mb,
95 memory_load_percent: status.memory_load_percent,
96 })
97 }
98
99 fn optimize(&self, aggressive: bool) -> Result<OptimizationResult, String> {
100 let result = self.inner.optimize(aggressive)?;
101 Ok(OptimizationResult {
102 freed_mb: result.freed_mb,
103 before_available_mb: result.before_available_mb,
104 after_available_mb: result.after_available_mb,
105 processes_affected: result.processes_trimmed,
106 duration_ms: result.duration_ms,
107 })
108 }
109
110 fn has_elevated_privileges(&self) -> bool {
111 self.inner.has_admin_privileges()
112 }
113
114 fn platform_name(&self) -> &'static str {
115 "Windows"
116 }
117}
118
119#[cfg(target_os = "macos")]
124pub struct MacOptimizer {
125 inner: crate::macos::memory::MacMemoryOptimizer,
126}
127
128#[cfg(target_os = "macos")]
129impl MacOptimizer {
130 pub fn new() -> Self {
131 Self {
132 inner: crate::macos::memory::MacMemoryOptimizer::new(),
133 }
134 }
135}
136
137#[cfg(target_os = "macos")]
138impl MemoryOptimizer for MacOptimizer {
139 fn get_memory_status(&self) -> Result<MemoryStatus, String> {
140 let status = crate::macos::memory::MacMemoryOptimizer::get_memory_status()?;
141 Ok(MemoryStatus {
142 total_physical_mb: status.total_physical_mb,
143 available_physical_mb: status.available_physical_mb,
144 memory_load_percent: status.memory_load_percent,
145 })
146 }
147
148 fn optimize(&self, aggressive: bool) -> Result<OptimizationResult, String> {
149 let result = self.inner.optimize(aggressive)?;
150 Ok(OptimizationResult {
151 freed_mb: result.freed_mb,
152 before_available_mb: result.before_available_mb,
153 after_available_mb: result.after_available_mb,
154 processes_affected: result.processes_affected,
155 duration_ms: result.duration_ms,
156 })
157 }
158
159 fn has_elevated_privileges(&self) -> bool {
160 self.inner.has_sudo_privileges()
161 }
162
163 fn platform_name(&self) -> &'static str {
164 if self.inner.is_apple_silicon() {
165 "macOS (Apple Silicon)"
166 } else {
167 "macOS (Intel)"
168 }
169 }
170}
171
172#[cfg(not(any(target_os = "windows", target_os = "macos")))]
177pub struct StubOptimizer;
178
179#[cfg(not(any(target_os = "windows", target_os = "macos")))]
180impl StubOptimizer {
181 pub fn new() -> Self {
182 Self
183 }
184}
185
186#[cfg(not(any(target_os = "windows", target_os = "macos")))]
187impl MemoryOptimizer for StubOptimizer {
188 fn get_memory_status(&self) -> Result<MemoryStatus, String> {
189 use sysinfo::System;
190
191 let mut sys = System::new();
192 sys.refresh_memory();
193
194 let total = sys.total_memory() as f64 / 1024.0 / 1024.0;
195 let available = sys.available_memory() as f64 / 1024.0 / 1024.0;
196 let load = if total > 0.0 {
197 (((total - available) / total) * 100.0) as u32
198 } else {
199 0
200 };
201
202 Ok(MemoryStatus {
203 total_physical_mb: total,
204 available_physical_mb: available,
205 memory_load_percent: load,
206 })
207 }
208
209 fn optimize(&self, _aggressive: bool) -> Result<OptimizationResult, String> {
210 Err("Memory optimization not supported on this platform".into())
211 }
212
213 fn has_elevated_privileges(&self) -> bool {
214 unsafe { libc::geteuid() == 0 }
215 }
216
217 fn platform_name(&self) -> &'static str {
218 "Linux (Limited Support)"
219 }
220}