Power Consumption Monitoring System
Overview
The Power Monitoring system provides a generic, reusable interface for measuring and reporting power consumption of processes using PowerJoular. It can be integrated into any script to track energy usage during execution.
24/7 mode (recommended for 20H12): PowerJoular can run as a systemd service (powerjoular.service) and write continuously to /var/lib/powerjoular/power_24h.csv. The 20H12 process then uses the last 24 hours of this CSV to generate the daily power report (full-day consumption). See 24/7 systemd service below.
Important: Files are stored in /tmp/ by default (not ~/.zen/tmp/) to avoid cleanup by scripts like 20h12.process.sh which clean ~/.zen/tmp/ during execution. This ensures monitoring data persists throughout script execution.
Components
1. admin/monitor/power_monitor.sh - Generic Monitoring Wrapper
Main script that provides a unified interface for power monitoring operations.
Commands:
start [csv_file] [pid_file]- Start power monitoring (one-off)stop [pid_file]- Stop power monitoringstatus [pid_file]- Get monitoring statusreport <csv_file> <output_html> [title] [log_file] [hostname] [duration]- Generate HTML report from a CSVreport-from-24h <output_html> [title] [log_file] [hostname] [duration]- Generate report from last 24h of 24/7 CSV (usesPOWER_24H_CSV, default/var/lib/powerjoular/power_24h.csv)trim-24h-csv [csv_path]- Trim 24/7 CSV to last 24h only (stops powerjoular.service, overwrites CSV, restarts). Call after report-from-24h in 20H12 to avoid filling disk.
2. admin/monitor/generate_powerjoular_graph.py - Graph Generator
Python script that generates power consumption graphs from CSV data using matplotlib.
3. admin/monitor/generate_power_report.sh - HTML Report Generator
Generates comprehensive HTML reports with embedded graphs and statistics. Generic version that accepts a custom report title.
4. 24/7 systemd service (powerjoular.service)
Installed by install/install_powerjoular.sh from tools/systemd/powerjoular.service. Runs PowerJoular continuously and appends to /var/lib/powerjoular/power_24h.csv. The 20H12 script uses this file to report full-day power consumption (last 24h).
To avoid filling disk, 20H12 trims the CSV to last 24h after generating the report (power_monitor.sh trim-24h-csv): it stops the service, overwrites the CSV with only the last 24h of data, then restarts the service.
Enable/start:
sudo systemctl enable --now powerjoularStatus:
sudo systemctl status powerjoularCSV path:
/var/lib/powerjoular/power_24h.csv(override with envPOWER_24H_CSV)
Usage
Basic Usage
Advanced Usage with Custom CSV File
Note:
PID files are automatically derived from CSV files (same path with
.pidextension). No need to manage them manually.
Integration in Existing Scripts
Example 1: Simple Integration
Example 2: With Error Handling
CSV Format
PowerJoular CSV format (with -f append mode):
Fields:
timestamp: ISO format timestamp (YYYY-MM-DD HH:MM:SS)power: Total power consumption (Watts)cpu_power: CPU power consumption (Watts)gpu_power: GPU power consumption (Watts) - may be 0 if no GPU
Report Format
The generated HTML report includes:
Power Statistics Box:
Average Power (W)
Maximum Power (W)
Minimum Power (W)
Duration (if provided)
Power Consumption Graph:
Total power over time (line chart with fill)
CPU/GPU breakdown (if available)
Statistics overlay
Execution Log:
Process log file content (if provided)
Formatted for readability
Requirements
System Requirements
PowerJoular: Must be installed and available in PATH
Installation:
~/.zen/Astroport.ONE/install/install_powerjoular.shRequires: GNAT compiler, gprbuild
Supports: Intel RAPL, AMD Ryzen/EPYC, Raspberry Pi, Asus Tinker Board
Python Requirements
Python 3 with:
matplotlib(for graph generation)Standard library:
csv,datetime,os,sys
Install dependencies:
Or via Astroport.ONE install.sh (already included).
Permissions
PowerJoular requires root/sudo access for RAPL monitoring on Linux kernels 5.10+:
The power_monitor.sh script handles sudo automatically.
API Reference
power_monitor.sh start [csv_file]
Starts power monitoring. PID file is automatically derived from CSV file.
Parameters:
csv_file(optional): Path to CSV output file (default:/tmp/power_monitor_$$.csv)If relative path, stored in
/tmp/(to avoid cleanup by scripts that clean~/.zen/tmp/)PID file automatically created as
{csv_file%.csv}.pid
Returns:
Exit code: 0 on success, 1 on failure
Stdout: PID on first line, CSV path on second line (if successful)
Example:
power_monitor.sh stop [csv_file]
Stops power monitoring. Can use CSV file (PID file auto-detected) or omit for default.
Parameters:
csv_file(optional): CSV file path (PID file auto-derived) or omit for default
Returns:
Exit code: 0 on success
Example:
power_monitor.sh status [csv_file]
Gets monitoring status. Can use CSV file (PID file auto-detected) or omit for default.
Parameters:
csv_file(optional): CSV file path (PID file auto-derived) or omit for default
Returns:
RUNNING:PIDif monitoring is activeNOT_RUNNINGif not active
Example:
power_monitor.sh report <csv_file> <output_html> [title] [log_file] [hostname] [duration]
Generates HTML report with power consumption graph.
Parameters:
csv_file(required): Path to PowerJoular CSV fileoutput_html(required): Path to output HTML filetitle(optional): Report title (default: "Power Consumption Report")log_file(optional): Path to process log file to includehostname(optional): Hostname for report (default:$(hostname -f))duration(optional): Process duration string (e.g., "1h 30m 15s")
Returns:
Exit code: 0 on success, 1 on failure
Example:
Integration Examples
In 20h12.process.sh
The 20h12.process.sh script uses the 24/7 PowerJoular service to report full-day (last 24h) power consumption. No start/stop in the script; the systemd service writes continuously to /var/lib/powerjoular/power_24h.csv.
For one-off monitoring (start/stop within a script), use start, stop, and report as in the Custom Scripts example below.
In Custom Scripts
Troubleshooting
PowerJoular Not Found
Error: PowerJoular is not available
Solution:
Permission Denied
Error: sudo: a password is required
Solution: Ensure the script has sudo access or run with appropriate permissions. PowerJoular requires root for RAPL access.
No Data in CSV
Issue: CSV file exists but has only 1 line or is empty
Possible causes:
Monitoring started but stopped too quickly
PowerJoular failed to initialize
Insufficient permissions
Solution: Check PowerJoular logs and ensure monitoring runs for at least a few seconds.
Graph Generation Fails
Error: Python3 or generate_powerjoular_graph.py not available
Solution:
Or ensure matplotlib is installed via Astroport.ONE install.sh.
Report Generation Fails
Error: generate_power_report.sh not found
Solution: Ensure all scripts are in admin/monitor/ directory and paths are correct.
Best Practices
Always use cleanup traps:
Use CSV file for stop/status (PID file auto-detected):
Check monitoring status before generating reports:
Use descriptive titles for reports:
Include log files when available:
Handle monitoring failures gracefully:
No need to manage PID files manually:
PID files are automatically derived from CSV files
All files stored in
/tmp/by default to avoid cleanup by scripts that clean~/.zen/tmp/Just use the CSV file for all operations
References
PowerJoular Documentation: https://www.noureddine.org/research/joular/powerjoular
PowerJoular GitHub: https://github.com/papiche/powerjoular
Installation Script:
install/install_powerjoular.shExample Integration:
20h12.process.sh
License
AGPL-3.0 (same as Astroport.ONE)
Last updated