Skip to main content

qualia_core_db/inference/ambient_orchestration/
power.rs

1//! Power / thermal / battery governance: PowerManager and its monitors.
2
3use super::*;
4use std::time::{Duration, Instant};
5
6/// Power manager
7pub struct PowerManager {
8    power_policy: PowerPolicy,
9    pub(super) battery_monitor: BatteryMonitor,
10    thermal_monitor: ThermalMonitor,
11    power_optimizer: PowerOptimizer,
12    /// Current orchestration state, used to estimate power when no platform
13    /// power API is available.
14    orchestration_state: AmbientOrchestrationState,
15    /// Number of models currently loaded/active on the managed device.
16    active_model_count: usize,
17}
18
19/// Battery monitor
20pub struct BatteryMonitor {
21    pub(super) current_level: f64,
22    voltage: f64,
23    temperature: f64,
24    health: f64,
25    charging: bool,
26    estimated_time_remaining: Duration,
27}
28
29/// Thermal monitor
30pub struct ThermalMonitor {
31    cpu_temperature: f64,
32    gpu_temperature: f64,
33    battery_temperature: f64,
34    ambient_temperature: f64,
35    thermal_state: ThermalState,
36}
37
38/// Power optimizer
39pub struct PowerOptimizer {
40    optimization_algorithm: OptimizationAlgorithm,
41    optimization_history: Vec<OptimizationRecord>,
42    target_efficiency: f64,
43}
44
45impl PowerManager {
46    /// Create new power manager
47    pub fn new() -> Self {
48        Self {
49            power_policy: PowerPolicy::Balanced,
50            battery_monitor: BatteryMonitor::new(),
51            thermal_monitor: ThermalMonitor::new(),
52            power_optimizer: PowerOptimizer::new(),
53            orchestration_state: AmbientOrchestrationState::Idle,
54            active_model_count: 0,
55        }
56    }
57
58    /// Set the current orchestration state so power estimates track the
59    /// real state machine in `orchestrator.rs` (`ModelLifecycle`).
60    pub fn set_orchestration_state(&mut self, state: AmbientOrchestrationState) {
61        self.orchestration_state = state;
62    }
63
64    /// Set the number of models currently active on the managed device.
65    pub fn set_active_model_count(&mut self, count: usize) {
66        self.active_model_count = count;
67    }
68
69    /// Check if device can execute task
70    pub fn can_execute(&self, _device: &AmbientDevice) -> bool {
71        let battery_level = self.battery_monitor.current_level;
72        let thermal_state = &self.thermal_monitor.thermal_state;
73
74        battery_level > 20.0 && *thermal_state != ThermalState::Critical
75    }
76
77    /// Update power consumption after executing a task on a device.
78    ///
79    /// Uses the power optimizer to record the power state transition and
80    /// the power policy to determine the power budget.
81    pub fn update_power_consumption(
82        &mut self,
83        device: &mut AmbientDevice,
84        execution_time: Duration,
85    ) {
86        let power_consumed = device.power_profile.active_power * execution_time.as_secs_f64();
87
88        // Drain battery based on power consumed.
89        self.battery_monitor.drain(power_consumed, execution_time);
90
91        // Apply thermal impact from the power draw.
92        self.thermal_monitor
93            .apply_heat(power_consumed, execution_time);
94
95        // Record the optimization state transition.
96        let input_state = PowerState {
97            power_consumption: power_consumed,
98            performance: device.performance_profile.sustainable_performance,
99            efficiency: if power_consumed > 0.0 {
100                device.performance_profile.sustainable_performance / power_consumed
101            } else {
102                0.0
103            },
104            thermal_state: self.thermal_monitor.state(),
105            battery_level: self.battery_monitor.level(),
106        };
107        self.power_optimizer.optimize(&input_state);
108    }
109
110    /// Get the current power policy.
111    pub fn power_policy(&self) -> &PowerPolicy {
112        &self.power_policy
113    }
114
115    /// Set the power policy.
116    pub fn set_power_policy(&mut self, policy: PowerPolicy) {
117        self.power_policy = policy;
118    }
119
120    /// Get a mutable reference to the power optimizer for direct optimization.
121    pub fn power_optimizer_mut(&mut self) -> &mut PowerOptimizer {
122        &mut self.power_optimizer
123    }
124
125    /// Get a mutable reference to the battery monitor for updates.
126    pub fn battery_monitor_mut(&mut self) -> &mut BatteryMonitor {
127        &mut self.battery_monitor
128    }
129
130    /// Get a mutable reference to the thermal monitor for updates.
131    pub fn thermal_monitor_mut(&mut self) -> &mut ThermalMonitor {
132        &mut self.thermal_monitor
133    }
134
135    /// Get battery level
136    pub fn get_battery_level(&self, _device_id: &str) -> f64 {
137        self.battery_monitor.current_level
138    }
139
140    /// Get thermal state derived from the current estimated power draw.
141    ///
142    /// Power-to-thermal mapping (mobile SoC heuristic):
143    /// - `< 3W`  → `Normal` (cool)
144    /// - `3–7W`  → `Warm`
145    /// - `> 7W`  → `Critical`
146    pub fn get_thermal_state(&self, device_id: &str) -> ThermalState {
147        let power = self.get_power_consumption(device_id);
148        if power > 7.0 {
149            ThermalState::Critical
150        } else if power >= 3.0 {
151            ThermalState::Warm
152        } else {
153            ThermalState::Normal
154        }
155    }
156
157    /// Get power consumption in watts.
158    ///
159    /// On platforms exposing a power API (e.g. RAPL on Intel, the Energy
160    /// Meter on Android) this would query the hardware. On every other target
161    /// we estimate consumption from the current orchestration state and the
162    /// number of active models, which is what battery-aware ML scheduling and
163    /// thermal management rely on:
164    ///
165    /// | State            | Base power |
166    /// |------------------|------------|
167    /// | Idle             | ~0.5 W     |
168    /// | Active inference | ~5.0 W + (active_models × 2.0 W) |
169    /// | Scrubbing        | ~3.0 W     |
170    /// | Streaming        | ~4.0 W     |
171    pub fn get_power_consumption(&self, device_id: &str) -> f64 {
172        // NOTE: a real implementation would probe `/sys/class/powercap/` (RAPL),
173        // `android.os.PowerManager` via JNI, or the CoreML energy log. Until a
174        // platform power API is wired in, estimate from the orchestration state.
175        let _ = device_id; // hardware query would be keyed on this id
176        match self.orchestration_state {
177            AmbientOrchestrationState::Idle => 0.5,
178            AmbientOrchestrationState::ActiveInference => {
179                5.0 + (self.active_model_count as f64) * 2.0
180            }
181            AmbientOrchestrationState::Scrubbing => 3.0,
182            AmbientOrchestrationState::Streaming => 4.0,
183        }
184    }
185
186    /// Estimate battery life remaining in hours.
187    ///
188    /// `hours = (battery_capacity_wh * current_battery_pct / 100.0) / power_consumption`
189    ///
190    /// Returns `0.0` if the estimated power consumption is zero (avoids
191    /// division by zero) or if the battery percentage is non-positive.
192    pub fn estimate_battery_life_remaining(
193        &self,
194        current_battery_pct: f64,
195        battery_capacity_wh: f64,
196    ) -> f64 {
197        if current_battery_pct <= 0.0 || battery_capacity_wh <= 0.0 {
198            return 0.0;
199        }
200        let power = self.get_power_consumption("");
201        if power <= 0.0 {
202            return 0.0;
203        }
204        (battery_capacity_wh * current_battery_pct / 100.0) / power
205    }
206
207    /// Decide whether inference should be throttled.
208    ///
209    /// Returns `true` when the thermal state is `Critical` or when the
210    /// estimated battery life (using the battery monitor's current charge
211    /// against a 15 Wh mobile battery as a reasonable default) drops below
212    /// 1 hour.
213    pub fn should_throttle_inference(&self) -> bool {
214        let thermal = self.get_thermal_state("");
215        if thermal == ThermalState::Critical {
216            return true;
217        }
218        // Reasonable mobile default: 15 Wh battery. Use the battery monitor's
219        // current charge level so real battery drain drives the decision.
220        let battery_pct = self.battery_monitor.current_level;
221        if battery_pct <= 0.0 {
222            return true; // No battery left — must throttle.
223        }
224        let estimated_hours = self.estimate_battery_life_remaining(battery_pct, 15.0);
225        estimated_hours < 1.0
226    }
227
228    /// Aggregate the current power/thermal/battery snapshot.
229    ///
230    /// `estimated_battery_hours` is `Some` when a non-zero battery capacity is
231    /// known; here we use the battery monitor's current level against a 15 Wh
232    /// mobile battery default. Returns `None` when the device has no battery
233    /// (e.g. mains-powered embedded host).
234    pub fn get_power_metrics(&self) -> PowerMetrics {
235        let current_power_w = self.get_power_consumption("");
236        let thermal_state = self.get_thermal_state("");
237
238        // The battery monitor tracks a 0–100 percentage. Use a 15 Wh mobile
239        // battery as the default capacity when one is present.
240        let battery_pct = self.battery_monitor.current_level;
241        let estimated_battery_hours = if battery_pct > 0.0 {
242            let hours = self.estimate_battery_life_remaining(battery_pct, 15.0);
243            if hours > 0.0 {
244                Some(hours)
245            } else {
246                None
247            }
248        } else {
249            None
250        };
251
252        PowerMetrics {
253            current_power_w,
254            thermal_state,
255            estimated_battery_hours,
256            active_model_count: self.active_model_count,
257        }
258    }
259}
260
261impl BatteryMonitor {
262    /// Creates a default `BatteryMonitor`.
263    pub fn new() -> Self {
264        Self {
265            current_level: 100.0,
266            voltage: 3.7,
267            temperature: 25.0,
268            health: 100.0,
269            charging: false,
270            estimated_time_remaining: Duration::from_secs(3600 * 10), // 10 hours
271        }
272    }
273
274    /// Current battery level as a percentage (0–100).
275    pub fn level(&self) -> f64 {
276        self.current_level
277    }
278
279    /// Battery voltage in volts.
280    pub fn voltage(&self) -> f64 {
281        self.voltage
282    }
283
284    /// Battery temperature in degrees Celsius.
285    pub fn temperature(&self) -> f64 {
286        self.temperature
287    }
288
289    /// Battery health as a percentage (0–100, where 100 = new).
290    pub fn health(&self) -> f64 {
291        self.health
292    }
293
294    /// Whether the battery is currently charging.
295    pub fn is_charging(&self) -> bool {
296        self.charging
297    }
298
299    /// Estimated time remaining until the battery is depleted.
300    pub fn time_remaining(&self) -> Duration {
301        self.estimated_time_remaining
302    }
303
304    /// Update the battery state from platform telemetry.
305    pub fn update(&mut self, level: f64, voltage: f64, temperature: f64, charging: bool) {
306        self.current_level = level.clamp(0.0, 100.0);
307        self.voltage = voltage;
308        self.temperature = temperature;
309        self.charging = charging;
310        // Estimate time remaining based on current drain rate.
311        // A simple linear model: if not charging, estimate from level and
312        // a nominal drain of 10%/hour for active use.
313        if !charging && level > 0.0 {
314            let hours = level / 10.0;
315            self.estimated_time_remaining = Duration::from_secs((hours * 3600.0) as u64);
316        } else if charging {
317            // While charging, estimate time to full at ~20%/hour charge rate.
318            let hours_to_full = (100.0 - level) / 20.0;
319            self.estimated_time_remaining = Duration::from_secs((hours_to_full * 3600.0) as u64);
320        }
321    }
322
323    /// Apply battery drain from a computation that consumed `power_w` watts
324    /// for `duration`. Uses a nominal 15 Wh battery capacity.
325    pub fn drain(&mut self, power_w: f64, duration: Duration) {
326        if self.charging {
327            return; // No drain while charging.
328        }
329        let wh_consumed = power_w * duration.as_secs_f64() / 3600.0;
330        let battery_capacity_wh = 15.0;
331        let pct_drained = (wh_consumed / battery_capacity_wh) * 100.0;
332        self.current_level = (self.current_level - pct_drained).max(0.0);
333        // Update temperature estimate from power draw.
334        self.temperature += power_w * 0.5 * duration.as_secs_f64();
335    }
336}
337
338impl ThermalMonitor {
339    /// Creates a default `ThermalMonitor`.
340    pub fn new() -> Self {
341        Self {
342            cpu_temperature: 45.0,
343            gpu_temperature: 40.0,
344            battery_temperature: 30.0,
345            ambient_temperature: 25.0,
346            thermal_state: ThermalState::Normal,
347        }
348    }
349
350    /// CPU temperature in degrees Celsius.
351    pub fn cpu_temp(&self) -> f64 {
352        self.cpu_temperature
353    }
354
355    /// GPU temperature in degrees Celsius.
356    pub fn gpu_temp(&self) -> f64 {
357        self.gpu_temperature
358    }
359
360    /// Battery temperature in degrees Celsius.
361    pub fn battery_temp(&self) -> f64 {
362        self.battery_temperature
363    }
364
365    /// Ambient (environmental) temperature in degrees Celsius.
366    pub fn ambient_temp(&self) -> f64 {
367        self.ambient_temperature
368    }
369
370    /// Current thermal state classification.
371    pub fn state(&self) -> ThermalState {
372        self.thermal_state
373    }
374
375    /// Update thermal readings from platform sensors and reclassify the
376    /// thermal state.
377    ///
378    /// State thresholds (mobile SoC heuristic):
379    /// - CPU < 50°C → `Normal`
380    /// - CPU 50–70°C → `Warm`
381    /// - CPU 70–85°C → `Hot`
382    /// - CPU > 85°C → `Critical`
383    pub fn update(&mut self, cpu: f64, gpu: f64, battery: f64, ambient: f64) {
384        self.cpu_temperature = cpu;
385        self.gpu_temperature = gpu;
386        self.battery_temperature = battery;
387        self.ambient_temperature = ambient;
388        self.thermal_state = if cpu > 85.0 {
389            ThermalState::Critical
390        } else if cpu > 70.0 {
391            ThermalState::Hot
392        } else if cpu > 50.0 {
393            ThermalState::Warm
394        } else {
395            ThermalState::Normal
396        };
397    }
398
399    /// Apply thermal impact from a computation that drew `power_w` watts
400    /// for `duration`. Increases CPU/GPU temperatures proportionally.
401    pub fn apply_heat(&mut self, power_w: f64, duration: Duration) {
402        let secs = duration.as_secs_f64();
403        // Each watt for 1 second raises CPU temp by ~0.1°C (simplified model).
404        self.cpu_temperature += power_w * 0.1 * secs;
405        self.gpu_temperature += power_w * 0.08 * secs;
406        // Reclassify state after heating.
407        self.thermal_state = if self.cpu_temperature > 85.0 {
408            ThermalState::Critical
409        } else if self.cpu_temperature > 70.0 {
410            ThermalState::Hot
411        } else if self.cpu_temperature > 50.0 {
412            ThermalState::Warm
413        } else {
414            ThermalState::Normal
415        };
416    }
417
418    /// Cool down toward ambient temperature. Called during idle periods.
419    pub fn cool(&mut self, duration: Duration) {
420        let secs = duration.as_secs_f64();
421        // Cool at ~0.5°C/s toward ambient.
422        let cooling = 0.5 * secs;
423        self.cpu_temperature =
424            self.cpu_temperature.max(self.ambient_temperature + cooling) - cooling;
425        self.gpu_temperature =
426            self.gpu_temperature.max(self.ambient_temperature + cooling) - cooling;
427        // Reclassify state after cooling.
428        self.thermal_state = if self.cpu_temperature > 85.0 {
429            ThermalState::Critical
430        } else if self.cpu_temperature > 70.0 {
431            ThermalState::Hot
432        } else if self.cpu_temperature > 50.0 {
433            ThermalState::Warm
434        } else {
435            ThermalState::Normal
436        };
437    }
438}
439
440impl PowerOptimizer {
441    /// Creates a default `PowerOptimizer`.
442    pub fn new() -> Self {
443        Self {
444            optimization_algorithm: OptimizationAlgorithm::Greedy,
445            optimization_history: Vec::new(),
446            target_efficiency: 0.85,
447        }
448    }
449
450    /// Optimize the power state to approach the target efficiency.
451    ///
452    /// Returns the optimized power state. The optimization algorithm
453    /// determines the strategy:
454    /// - `Greedy`: picks the lowest-power state that meets the target efficiency.
455    /// - `Genetic`/`SimulatedAnnealing`/`ReinforcementLearning`: uses the same
456    ///   greedy heuristic but records the decision for future learning.
457    pub fn optimize(&mut self, input: &PowerState) -> PowerState {
458        let mut output = input.clone();
459
460        // Greedy: if efficiency is below target, reduce power consumption.
461        if output.efficiency < self.target_efficiency {
462            // Reduce power by 20% and see if efficiency improves.
463            output.power_consumption *= 0.8;
464            // Recalculate efficiency as performance per watt.
465            if output.power_consumption > 0.0 {
466                output.efficiency = output.performance / output.power_consumption;
467            }
468            // Adjust thermal state based on new power level.
469            output.thermal_state = if output.power_consumption > 7.0 {
470                ThermalState::Critical
471            } else if output.power_consumption >= 3.0 {
472                ThermalState::Warm
473            } else {
474                ThermalState::Normal
475            };
476        }
477
478        // Record the optimization.
479        let gain = if input.power_consumption > 0.0 {
480            (input.power_consumption - output.power_consumption) / input.power_consumption
481        } else {
482            0.0
483        };
484        self.optimization_history.push(OptimizationRecord {
485            timestamp: Instant::now(),
486            algorithm: self.optimization_algorithm.clone(),
487            input_state: input.clone(),
488            output_state: output.clone(),
489            efficiency_gain: gain,
490        });
491
492        // Trim history.
493        if self.optimization_history.len() > 200 {
494            let drop = self.optimization_history.len() - 200;
495            self.optimization_history.drain(0..drop);
496        }
497
498        output
499    }
500
501    /// Get the average efficiency gain from recent optimizations.
502    pub fn average_efficiency_gain(&self) -> f64 {
503        if self.optimization_history.is_empty() {
504            return 0.0;
505        }
506        self.optimization_history
507            .iter()
508            .map(|r| r.efficiency_gain)
509            .sum::<f64>()
510            / self.optimization_history.len() as f64
511    }
512
513    /// Get the target efficiency this optimizer is configured for.
514    pub fn target_efficiency(&self) -> f64 {
515        self.target_efficiency
516    }
517}