Files
Alexandru-Victor Macocian 87b0d2dbbf Disk usage tweaks
2025-10-30 17:54:54 +01:00

123 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
LOGFILE=/var/log/power_usage.log
OS_TYPE=$(DetectOS)
# Create log file with proper error handling
if ! touch "$LOGFILE" 2>/dev/null; then
LogError "Cannot create log file $LOGFILE"
exit 1
fi
chmod 644 "$LOGFILE" 2>/dev/null
# Check if bc is available
if ! command -v bc >/dev/null 2>&1; then
LogError "bc command not found. Please install bc:"
if [[ "$OS_TYPE" == "macos" ]]; then
LogError " macOS: brew install bc"
else
LogError " Ubuntu/Debian: sudo apt-get install bc"
fi
exit 1
fi
# Check power monitoring support
if ! IsPowerMonitoringSupported; then
if [[ "$OS_TYPE" == "macos" ]]; then
LogError "powermetrics not available at /usr/bin/powermetrics"
LogError "Power monitoring requires macOS with powermetrics support"
else
LogError "Intel RAPL interface not available"
LogError "This may be due to:"
LogError "1. Non-Intel CPU"
LogError "2. RAPL not supported by kernel"
LogError "3. Insufficient permissions"
LogError "4. Module not loaded (try: sudo modprobe intel_rapl_common)"
fi
exit 1
fi
LogInformation "Starting power usage monitoring (OS: $OS_TYPE)"
# Run 6 iterations with 10-second intervals = 60 seconds total (no gaps)
for i in {1..6}; do
timestamp=$(GetTimestamp)
if [[ "$OS_TYPE" == "macos" ]]; then
# Use powermetrics for macOS (requires sudo)
# Sample for 1 second and extract CPU power
power_data=$(sudo powermetrics -n 1 -i 1000 --samplers cpu_power 2>/dev/null | grep -i "power" | head -1)
if [[ -n "$power_data" ]]; then
# Try to extract power value in watts (format: "Intel energy model derived package power (CPUs+GT+SA): 4.55W")
power_watts=$(echo "$power_data" | grep -oE '[0-9]+\.[0-9]+W' | grep -oE '[0-9]+\.[0-9]+')
# If watts format not found, try milliwatts format (format: "CPU Power: 1234 mW")
if [[ -z "$power_watts" ]]; then
power_mw=$(echo "$power_data" | grep -oE '[0-9]+ mW' | grep -oE '[0-9]+')
if [[ -n "$power_mw" ]]; then
# Convert milliwatts to watts
power_watts=$(echo "scale=6; $power_mw / 1000" | bc 2>/dev/null)
fi
fi
if [[ -n "$power_watts" && $? -eq 0 ]]; then
echo "$timestamp $power_watts" >> "$LOGFILE"
LogInformation "Power usage recorded: ${power_watts}W (iteration $i/6)"
else
LogError "Failed to extract or calculate power value"
fi
else
LogWarning "Could not retrieve power data from powermetrics"
fi
else
# Linux RAPL interface
RAPL_PATH="/sys/class/powercap/intel-rapl:0/energy_uj"
# Read initial energy value
if ! energy_1=$(cat "$RAPL_PATH" 2>/dev/null); then
LogError "Failed to read energy value from $RAPL_PATH"
exit 1
fi
sleep 1
# Read second energy value
if ! energy_2=$(cat "$RAPL_PATH" 2>/dev/null); then
LogError "Failed to read energy value from $RAPL_PATH"
exit 1
fi
energy_diff=$((energy_2 - energy_1))
# Convert microjoules to joules (1 J = 1,000,000 µJ)
energy_joules=$(echo "scale=6; $energy_diff / 1000000" | bc 2>/dev/null)
if [[ $? -ne 0 ]]; then
LogError "Failed to calculate energy in joules"
continue
fi
# Since we waited 1 second, the power usage is simply the energy difference in joules (W)
power_watts=$(echo "scale=6; $energy_joules / 1" | bc 2>/dev/null)
if [[ $? -ne 0 ]]; then
LogError "Failed to calculate power in watts"
continue
fi
# Write power usage to log file
echo "$timestamp $power_watts" >> "$LOGFILE"
LogInformation "Power usage recorded: ${power_watts}W (iteration $i/6)"
fi
if [ $i -lt 6 ]; then
sleep 10
fi
done
LogInformation "Power usage monitoring completed"