Managing power consumption on Linux laptops has always been a balancing act between performance and battery life. Today, I'm excited to share Observer, a new open-source tool that intelligently manages CPU cores based on your system's power state and load.
The Problem
Modern laptops come with increasingly powerful CPUs, often featuring multiple cores and HyperThreading. While this provides excellent performance when needed, having all cores active can significantly impact battery life during lighter workloads.
Traditional power management tools often focus on CPU frequency scaling, but they don't always handle core management optimally. This is where Observer comes in.
How Observer Works
Observer takes a different approach by dynamically managing which CPU cores are active. Here's how it works:
Power State Detection: Observer continuously monitors whether your laptop is running on battery or AC power.
Smart Core Management:
- On battery power, it reduces the number of active cores to a configurable percentage (default 50%)
- When plugged in, all cores are available for maximum performance
- Cores are enabled/disabled in pairs to maintain proper HyperThreading relationships
Load-Based Scaling:
- Monitors real-time CPU load across active cores
- Dynamically scales up when load is high
- Scales down during idle periods
- Maintains a minimum number of cores for system responsiveness
Smooth Transitions:
- Gracefully handles core state changes
- Ensures system stability during transitions
- Properly manages core pairs for HyperThreading
Real-World Impact
Let's look at a practical example. On my laptop with an 8-core/16-thread CPU:
- On AC Power: All 16 threads available for maximum performance
- On Battery (Light Load): Runs with 4 cores (8 threads), significantly reducing power consumption
- On Battery (High Load): Dynamically scales up to use more cores as needed
- Battery Life Impact: Early testing shows 10-20% improvement in battery life during light workloads
Technical Implementation
Observer is written in Rust, chosen for its safety guarantees and performance and as my learning experience. Some key technical aspects:
// Core management example
impl CoreManager {
fn get_optimal_core_count(&mut self, on_battery: bool) -> usize {
let total_cores = self.available_cores.len();
// Calculate base cores based on power state
let base_cores = if on_battery {
(total_cores as f32 * (self.settings.battery_percentage as f32 / 100.0)) as usize
} else {
total_cores
};
// Adjust based on load
if self.current_load > self.settings.load_threshold {
base_cores.saturating_add(2)
} else {
base_cores
}
}
}
The project follows modern Rust best practices:
- Modular architecture for maintainability
- Error handling using Rust's Result type
- Configuration using TOML
- Systemd integration
- Comprehensive logging
Getting Started
Installation is straightforward. On Arch Linux:
yay -S observer
Or manually:
# Download and install
wget https://github.com/yourusername/observer/releases/latest/download/observer-linux-amd64.tar.gz
tar xzf observer-linux-amd64.tar.gz
sudo ./install.sh
# Start the service
sudo systemctl enable --now observer
Configure through /etc/observer/config.toml
:
# Percentage of cores to enable on battery
battery_core_percentage = 50
# CPU load threshold for scaling
cpu_load_threshold = 40.0
# Minimum cores to keep enabled
min_cores = 2
Future Plans
Observer is actively developed, with several exciting features planned:
- Temperature-Based Management: Adjusting core count based on CPU temperature
- Workload Profiles: Different configurations for different types of workloads
- GPU Integration: Coordinating with GPU power management
- Power Consumption Metrics: Built-in power usage monitoring
Contributing
Observer is open source and welcomes contributions! Whether it's code, documentation, or bug reports, every contribution helps. Check out my GitHub repository to get started.
Conclusion
Observer demonstrates how modern systems can be more energy-efficient without sacrificing the ability to perform when needed. By intelligently managing CPU cores, we can achieve better battery life while maintaining responsiveness.
Give Observer a try and let me know your experiences. I'm particularly interested in hearing about battery life improvements on different hardware configurations.
Remember to star the repository if you find it useful, and feel free to open issues or contribute to the project!
This post was written on January 5, 2024. Observer is under active development, and new features may have been added since this publication.