Skip to main content

ruvector_memopt/apps/
mod.rs

1//! Application-specific optimization module
2//!
3//! Provides intelligent memory management for common resource-heavy applications:
4//! - Browsers (Chrome, Firefox, Safari, Edge, Arc, Brave)
5//! - Electron apps (VS Code, Discord, Slack, Teams, etc.)
6//! - Docker containers
7//! - Development tools
8//! - AI/ML workloads
9
10pub mod browser;
11pub mod electron;
12pub mod docker;
13pub mod leaks;
14pub mod suggestions;
15
16pub use browser::BrowserOptimizer;
17pub use electron::ElectronManager;
18pub use docker::DockerManager;
19pub use leaks::LeakDetector;
20pub use suggestions::SmartSuggestions;
21
22use serde::{Deserialize, Serialize};
23
24/// Common app categories for optimization
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26pub enum AppCategory {
27    Browser,
28    Electron,
29    Development,
30    Creative,
31    Communication,
32    Media,
33    System,
34    Container,
35    AI,
36    Other,
37}
38
39/// Process info with app categorization
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct AppProcess {
42    pub pid: u32,
43    pub name: String,
44    pub category: AppCategory,
45    pub memory_mb: f64,
46    pub cpu_percent: f32,
47    pub parent_app: Option<String>,
48    pub is_main_process: bool,
49}
50
51/// Aggregated app info (groups related processes)
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct AppInfo {
54    pub name: String,
55    pub category: AppCategory,
56    pub process_count: usize,
57    pub total_memory_mb: f64,
58    pub total_cpu_percent: f32,
59    pub main_pid: Option<u32>,
60    pub pids: Vec<u32>,
61    pub is_idle: bool,
62    pub idle_duration_secs: u64,
63}
64
65impl AppInfo {
66    /// Check if this app is a memory hog (>500MB)
67    pub fn is_memory_hog(&self) -> bool {
68        self.total_memory_mb > 500.0
69    }
70
71    /// Check if this app is using significant CPU (>10%)
72    pub fn is_cpu_intensive(&self) -> bool {
73        self.total_cpu_percent > 10.0
74    }
75
76    /// Get optimization priority (higher = optimize first)
77    pub fn optimization_priority(&self) -> f64 {
78        let mut priority = 0.0;
79
80        // Memory weight
81        priority += self.total_memory_mb / 100.0;
82
83        // CPU weight (less important than memory for optimization)
84        priority += self.total_cpu_percent as f64 * 0.5;
85
86        // Idle apps get higher priority for optimization
87        if self.is_idle {
88            priority *= 1.5;
89        }
90
91        // More processes = more overhead
92        priority += self.process_count as f64 * 2.0;
93
94        priority
95    }
96}
97
98/// Optimization action for an app
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100pub enum OptimizationAction {
101    /// Suggest closing the app
102    Close,
103    /// Suggest suspending/pausing
104    Suspend,
105    /// Trim working set / release memory
106    TrimMemory,
107    /// Restart to clear memory leaks
108    Restart,
109    /// Reduce tab count (browsers)
110    ReduceTabs { suggested_count: usize },
111    /// Unload inactive tabs
112    SuspendTabs,
113    /// Stop container
114    StopContainer,
115    /// Pause container
116    PauseContainer,
117    /// Clear cache
118    ClearCache,
119    /// No action needed
120    None,
121}
122
123/// Result of an optimization operation
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub struct OptimizationResult {
126    pub app_name: String,
127    pub action: OptimizationAction,
128    pub success: bool,
129    pub memory_freed_mb: f64,
130    pub message: String,
131}