hardware-query 0.2.1

Cross-platform Rust library for comprehensive hardware detection, real-time monitoring, power management, and AI/ML optimization
Documentation
//! Power management and efficiency tracking module
//!
//! This module provides comprehensive power monitoring capabilities including
//! power consumption tracking, efficiency analysis, and battery life estimation.

use crate::{BatteryInfo, Result};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Power consumption profile for the system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerProfile {
    /// Total system power draw in watts
    pub total_power_draw: Option<f32>,
    /// CPU power consumption in watts
    pub cpu_power: Option<f32>,
    /// GPU power consumption in watts  
    pub gpu_power: Option<f32>,
    /// Memory power consumption in watts
    pub memory_power: Option<f32>,
    /// Storage power consumption in watts
    pub storage_power: Option<f32>,
    /// Network interfaces power consumption in watts
    pub network_power: Option<f32>,
    /// Other components power consumption in watts
    pub other_power: Option<f32>,
    /// Performance per watt efficiency score (0.0 to 1.0)
    pub efficiency_score: f64,
    /// Risk level of thermal throttling
    pub thermal_throttling_risk: ThrottlingRisk,
    /// Current power management state
    pub power_state: PowerState,
    /// Available power saving modes
    pub available_power_modes: Vec<PowerMode>,
}

/// Risk level for thermal throttling
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ThrottlingRisk {
    /// No risk of throttling
    None,
    /// Low risk of throttling under sustained load
    Low,
    /// Moderate risk, throttling may occur
    Moderate,
    /// High risk, throttling likely
    High,
    /// Critical, throttling is occurring
    Critical,
}

/// Current system power state
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PowerState {
    /// Maximum performance mode
    HighPerformance,
    /// Balanced mode
    Balanced,
    /// Power saving mode
    PowerSaver,
    /// Battery optimization mode
    BatteryOptimized,
    /// Custom power profile
    Custom(String),
    /// Unknown state
    Unknown,
}

/// Available power management modes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PowerMode {
    /// Mode name
    pub name: String,
    /// Mode description
    pub description: String,
    /// Is this mode currently active
    pub is_active: bool,
    /// Expected power savings percentage
    pub power_savings_percent: Option<f32>,
    /// Expected performance impact percentage
    pub performance_impact_percent: Option<f32>,
}

/// Power optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerOptimization {
    /// Recommendation category
    pub category: OptimizationCategory,
    /// Human-readable recommendation
    pub recommendation: String,
    /// Expected power savings in watts
    pub expected_savings_watts: Option<f32>,
    /// Expected performance impact (0.0 to 1.0, where 1.0 is no impact)
    pub performance_impact: f64,
    /// Priority level of this optimization
    pub priority: OptimizationPriority,
}

/// Category of power optimization
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationCategory {
    /// CPU frequency scaling
    CPUScaling,
    /// GPU power limiting
    GPUPowerLimit,
    /// Display brightness
    DisplayBrightness,
    /// Background processes
    BackgroundProcesses,
    /// Network interfaces
    NetworkInterfaces,
    /// Storage devices
    StorageDevices,
    /// Thermal management
    ThermalManagement,
    /// System settings
    SystemSettings,
}

/// Priority level for optimizations
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationPriority {
    /// Low impact, minor savings
    Low,
    /// Medium impact, moderate savings
    Medium,
    /// High impact, significant savings
    High,
    /// Critical for system stability
    Critical,
}

impl PowerProfile {
    /// Query current power profile
    pub fn query() -> Result<Self> {
        let total_power_draw = Self::query_total_power_draw()?;
        let cpu_power = Self::query_cpu_power()?;
        let gpu_power = Self::query_gpu_power()?;
        let memory_power = Self::query_memory_power()?;
        let storage_power = Self::query_storage_power()?;
        let network_power = Self::query_network_power()?;
        let other_power = Self::query_other_power()?;
        
        let efficiency_score = Self::calculate_efficiency_score(total_power_draw);
        let thermal_throttling_risk = Self::assess_throttling_risk()?;
        let power_state = Self::query_power_state()?;
        let available_power_modes = Self::query_available_power_modes()?;

        Ok(Self {
            total_power_draw,
            cpu_power,
            gpu_power,
            memory_power,
            storage_power,
            network_power,
            other_power,
            efficiency_score,
            thermal_throttling_risk,
            power_state,
            available_power_modes,
        })
    }

    /// Estimate battery life based on current power consumption
    pub fn estimate_battery_life(&self, battery: &BatteryInfo) -> Option<Duration> {
        if let (Some(power_draw), Some(capacity_wh)) = (self.total_power_draw, battery.capacity_wh()) {
            if power_draw > 0.0 {
                // Calculate remaining capacity in wh
                let remaining_wh = capacity_wh * (battery.charge_percent() as f32 / 100.0);
                
                // Estimate hours remaining
                let hours_remaining = remaining_wh / power_draw;
                
                // Convert to Duration
                let seconds = (hours_remaining * 3600.0) as u64;
                return Some(Duration::from_secs(seconds));
            }
        }
        None
    }

    /// Get power optimization recommendations
    pub fn suggest_power_optimizations(&self) -> Vec<PowerOptimization> {
        let mut optimizations = Vec::new();

        // CPU optimization recommendations
        if let Some(cpu_power) = self.cpu_power {
            if cpu_power > 50.0 {
                optimizations.push(PowerOptimization {
                    category: OptimizationCategory::CPUScaling,
                    recommendation: "Consider reducing CPU frequency or enabling power saving mode".to_string(),
                    expected_savings_watts: Some(cpu_power * 0.2),
                    performance_impact: 0.85,
                    priority: OptimizationPriority::Medium,
                });
            }
        }

        // GPU optimization recommendations
        if let Some(gpu_power) = self.gpu_power {
            if gpu_power > 100.0 {
                optimizations.push(PowerOptimization {
                    category: OptimizationCategory::GPUPowerLimit,
                    recommendation: "GPU power consumption is high. Consider lowering power limit or reducing graphics settings".to_string(),
                    expected_savings_watts: Some(gpu_power * 0.15),
                    performance_impact: 0.90,
                    priority: OptimizationPriority::Medium,
                });
            }
        }

        // Thermal throttling recommendations
        match self.thermal_throttling_risk {
            ThrottlingRisk::High | ThrottlingRisk::Critical => {
                optimizations.push(PowerOptimization {
                    category: OptimizationCategory::ThermalManagement,
                    recommendation: "High thermal throttling risk detected. Reduce workload or improve cooling".to_string(),
                    expected_savings_watts: None,
                    performance_impact: 1.0,
                    priority: OptimizationPriority::Critical,
                });
            }
            ThrottlingRisk::Moderate => {
                optimizations.push(PowerOptimization {
                    category: OptimizationCategory::ThermalManagement,
                    recommendation: "Consider improving system cooling or reducing sustained workloads".to_string(),
                    expected_savings_watts: None,
                    performance_impact: 0.95,
                    priority: OptimizationPriority::Medium,
                });
            }
            _ => {}
        }

        optimizations
    }

    /// Calculate power efficiency score based on performance and consumption
    pub fn calculate_efficiency_with_performance(&self, performance_score: f64) -> f64 {
        if let Some(power_draw) = self.total_power_draw {
            if power_draw > 0.0 {
                // Calculate performance per watt
                return (performance_score / power_draw as f64).min(1.0);
            }
        }
        0.0
    }

    fn query_total_power_draw() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        // For now, return placeholder
        Ok(None)
    }

    fn query_cpu_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn query_gpu_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn query_memory_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn query_storage_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn query_network_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn query_other_power() -> Result<Option<f32>> {
        // Platform-specific implementation would go here
        Ok(None)
    }

    fn calculate_efficiency_score(total_power_draw: Option<f32>) -> f64 {
        // Basic efficiency calculation - would be enhanced with actual performance metrics
        if let Some(power) = total_power_draw {
            if power < 50.0 {
                0.9
            } else if power < 100.0 {
                0.7
            } else if power < 200.0 {
                0.5
            } else {
                0.3
            }
        } else {
            0.0
        }
    }

    fn assess_throttling_risk() -> Result<ThrottlingRisk> {
        // This would integrate with thermal sensors to assess risk
        // For now, return a placeholder
        Ok(ThrottlingRisk::None)
    }

    fn query_power_state() -> Result<PowerState> {
        // Platform-specific implementation would go here
        Ok(PowerState::Unknown)
    }

    fn query_available_power_modes() -> Result<Vec<PowerMode>> {
        // Platform-specific implementation would go here
        Ok(vec![])
    }
}

impl std::fmt::Display for PowerState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PowerState::HighPerformance => write!(f, "High Performance"),
            PowerState::Balanced => write!(f, "Balanced"),
            PowerState::PowerSaver => write!(f, "Power Saver"),
            PowerState::BatteryOptimized => write!(f, "Battery Optimized"),
            PowerState::Custom(name) => write!(f, "Custom: {}", name),
            PowerState::Unknown => write!(f, "Unknown"),
        }
    }
}

impl std::fmt::Display for ThrottlingRisk {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ThrottlingRisk::None => write!(f, "None"),
            ThrottlingRisk::Low => write!(f, "Low"),
            ThrottlingRisk::Moderate => write!(f, "Moderate"),
            ThrottlingRisk::High => write!(f, "High"),
            ThrottlingRisk::Critical => write!(f, "Critical"),
        }
    }
}