Skip to content

Latest commit

 

History

History
327 lines (231 loc) · 8.83 KB

File metadata and controls

327 lines (231 loc) · 8.83 KB

CPU Power Management

Overview

The Bridge tool includes intelligent CPU power management that automatically switches between performance and power-saving modes based on workload demand. This feature uses the Linux Intel P-State driver or generic cpufreq subsystem to control CPU frequency scaling.

How It Works

Automatic Mode Switching

Performance Mode (Active State)

  • Triggered when: Remote trigger file exists
  • Action: Port forwarding is active, data transfer in progress
  • CPU Setting: performance governor or performance energy preference
  • Effect: Maximum CPU frequency, optimal throughput

Powersave Mode (Idle State)

  • Triggered when: Remote trigger file is absent
  • Action: Only periodic file checking (every 15 minutes by default)
  • User Check: Automatically detects logged-in users
  • CPU Setting: powersave governor or power energy preference (only if no users logged in)
  • Effect: Reduced CPU frequency, lower power consumption
  • Protection: Skips CPU power changes when interactive user sessions are detected

Intel P-State Driver Support

The tool automatically detects and uses the best available interface:

  1. Intel P-State with HWP (Hardware P-States)

    • Preferred method on modern Intel CPUs
    • Uses energy_performance_preference interface
    • Available preferences: default, performance, balance_performance, balance_power, power
  2. Generic CPUFreq

    • Fallback for older systems or non-Intel CPUs
    • Uses scaling_governor interface
    • Available governors: performance, powersave, ondemand, conservative, etc.

Requirements

System Requirements

  • OS: Linux kernel 3.9+ (for intel_pstate) or 2.6+ (for cpufreq)
  • CPU: Intel Core processors (Sandy Bridge or newer recommended for HWP)
  • Permissions: Root/sudo access to modify CPU governor settings

Checking Your System

# Check if intel_pstate is available
ls -la /sys/devices/system/cpu/intel_pstate/

# Check available governors
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

# Check energy performance preferences (intel_pstate with HWP)
cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_available_preferences

# Check current governor
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

# Check current energy preference
cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference

User Session Protection

Important: The tool automatically protects interactive user sessions from power management changes.

Behavior

When entering low power mode, the tool:

  1. Checks for logged-in users using system utilities (who, /var/run/utmp)
  2. If users are detected:
    • Skips CPU governor changes
    • Still closes SSH connections and stops port forwarding
    • Logs: Skipping low power mode: users are logged in to the system
  3. If no users are logged in:
    • Proceeds with full low power mode (including CPU governor change)

Why This Matters

  • Interactive Performance: Prevents sluggish response during user sessions
  • Background Operation: Full power saving when system is unattended
  • Automatic Detection: No manual configuration needed

Example Scenarios

Scenario 1: User Working on System

User logs in via SSH → Bridge sees active session → Skips CPU power change → User experiences normal performance

Scenario 2: Unattended Server

No users logged in → Bridge enters full low power mode → CPU frequency reduced → Power saved

Usage

Running with CPU Control

# Run as root to enable CPU power management
sudo ./bridge -config bridge.conf

# Or install as systemd service (runs as root automatically)
sudo systemctl start bridge

Without Root Privileges

The tool will work without root but will log warnings:

Warning: Failed to set CPU governor to powersave: permission denied
Note: Run with sudo/root privileges to control CPU governor

All other functionality (SSH, port forwarding) works normally.

Configuration

Enable/disable low power mode in your configuration file:

# Enable automatic CPU power management
LOW_POWER_MODE=true

# Disable automatic CPU power management
LOW_POWER_MODE=false

When LOW_POWER_MODE=false, the tool will not attempt to change CPU governors.

Testing

Test Script

Use the included test script to verify CPU governor support:

# Run the test script
./test-cpu-governor.sh

# Run with sudo to test actual governor changes
sudo ./test-cpu-governor.sh

Manual Testing

  1. Start the bridge with a trigger file present:

    touch bridge
    sudo ./bridge -config bridge.conf
  2. Monitor CPU governor in another terminal:

    watch -n 1 cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference
  3. Remove trigger file to enter low power mode:

    rm bridge
    # Wait for next check interval (default 15 minutes, or set CHECK_INTERVAL=30s for testing)
  4. Observe the governor change:

    • Should switch from performance to power
  5. Recreate trigger file to exit low power mode:

    touch bridge
    # Wait for next check interval
  6. Observe the governor change:

    • Should switch back to performance

Monitoring

View Bridge Logs

# If running directly
sudo ./bridge -config bridge.conf

# If running as systemd service
sudo journalctl -u bridge -f

Log Messages

You'll see messages like:

Loading configuration from bridge.conf
Bridge starting with config:
  Current CPU Governor: balance_power
...
Entering low power mode...
CPU governor set to powersave mode
Low power mode active - only file checking will continue
...
Exiting low power mode...
CPU governor set to performance mode
Low power mode deactivated - full functionality restored

Monitor CPU Frequency

# Watch CPU frequencies in real-time
watch -n 1 'grep MHz /proc/cpuinfo | head -4'

# Or use cpupower tool
sudo cpupower frequency-info

# Or check sysfs directly
watch -n 1 'cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq | head -4'

Power Savings

Expected Benefits

In Low Power Mode:

  • CPU frequency reduced to minimum (typically 800-1200 MHz)
  • Power consumption reduced by 30-60% (depending on workload)
  • Heat generation reduced
  • Fan noise reduced (on laptops)

Performance Impact:

  • Minimal during idle (file checking only)
  • Zero impact on port forwarding (switches to performance mode)

Real-World Scenario

Example: Gateway server that forwards ports to development environment

  • Active hours (8 hours/day): Performance mode, full CPU power
  • Idle hours (16 hours/day): Powersave mode, ~50% power reduction
  • Overall savings: ~30-35% daily power consumption

Troubleshooting

Governor Not Changing

Check permissions:

# Verify you're running as root
id

# Check file permissions
ls -la /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

Check if governor is available:

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

Intel P-State Not Available

If you see "Intel P-State not available", the system is using generic cpufreq. This is normal for:

  • Non-Intel CPUs (AMD, ARM, etc.)
  • Older Intel CPUs (pre-Sandy Bridge)
  • Systems with intel_pstate disabled in BIOS/kernel

The tool will automatically fall back to cpufreq governors.

Performance Issues

If you experience performance issues:

  1. Check current governor:

    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  2. Manually set to performance:

    echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
  3. Disable low power mode:

    LOW_POWER_MODE=false

Advanced Configuration

Custom Governor Selection

To use different governors, modify the code in main.go:

// In enterLowPowerMode()
setCPUGovernor("powersave")  // Change to: "conservative" or "ondemand"

// In exitLowPowerMode()
setCPUGovernor("performance")  // Keep as performance for best throughput

Frequency Limits

To set custom frequency limits (requires additional code):

# Manual frequency limits
echo 800000 | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_min_freq
echo 3500000 | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq

References

See Also