CodeGreen¶
-
Precise Energy Measurement
Hardware energy counters via RAPL (Linux), IOReport (macOS), EMI (Windows), NVML and ROCm, with a validity verdict on every result.
-
Multi-Language Support
Python, C, C++, and Java instrumentation via Tree-sitter with config-driven extensibility.
-
Continuous Monitoring
codegreen monitorsamples an already-running service, with cgroup_v2 per-PID attribution and a UDS annotation channel for per-request energy. -
Interactive Visualization
Energy timeline plots with per-function breakdown, hotspot detection, and zoom/pan via Plotly.
-
Easy Integration
JSON output for every command, CI energy gating with
--budget, and exit code 3 for invalid measurements.
What is CodeGreen?¶
CodeGreen measures the energy consumption of software from hardware energy counters, down to individual functions. It instruments source code with Tree-sitter and reads the counters through a native C++ backend (NEMB, the Native Energy Measurement Backend).
Key Features¶
- Per-function energy:
codegreen measureinstruments a file;codegreen projectinstruments a multi-file project and runs your own build and run commands (Maven, Gradle, Make, plain Python). - Whole-command energy:
codegreen runmeasures any command over repeated runs, with confidence intervals and--budgetgating. - In-process API:
codegreen.Sessionbrackets regions of a Python program. - Continuous monitoring:
codegreen monitorsamples running services, with per-PID attribution (cgroup v2) and per-request annotations over a Unix socket. - Validity: every result carries a verdict with reason codes;
measure,runandprojectexit 3 on an invalid measurement. - Visualization:
--export-plotwrites an interactive Plotly timeline or a static image.
Quick Start¶
Where to start¶
| I want to ... | Start here | Then |
|---|---|---|
| Measure a one-off Python or shell script | Quickstart | 01 Quickstart measure |
| Get a stable mean across N runs | Examples | 02 Run with repeats |
| Bracket regions inside a long script | Python API | 03 Session API |
| Find per-method hotspots in a Java/Python/C++ project | Project Profiling | 04 Java / 09 Python |
| Watch a running daemon or webapp | Continuous Monitoring | 06 host / 07 PID / 08 annotations |
| Gate energy in CI | CI/CD integration | codegreen run --budget 10.0 ... |
Cross-validate against perf |
Energy Measurement | 10 CodeGreen vs perf |
| Add a language or contribute | Architecture | Contributing |
What's new in 0.5.0¶
- Every
measure,runandprojectresult carries avalidityverdict with reason codes, and the command exits 3 on an invalid measurement. See Exit codes and measurement validity. --sensors(andmeasurement.default_sensors) selects the providers that are summed; a total that mixes GPU and CPU energy is flaggedGPU_IN_TOTAL.- Per-function energy replays every call:
inclusive_j,exclusive_jand a per-functionqualityverdict.projectpercentages are shares of the attributed (exclusive) energy. - NEMB result block ABI 4: per-marker reading index, clamp flag, per-provider energy and sampler statistics.
Earlier releases are described in the Changelog.
Supported Platforms¶
-
Linux
Intel RAPL, NVIDIA NVML, and AMD ROCm/RAPL energy monitoring.
-
macOS
Apple Silicon energy via IOReport (CPU/GPU/ANE/DRAM) + kpc hardware perf counters. Pre-built ARM64 wheels on PyPI.
-
Windows 11
RAPL energy via EMI (inbox intelpep.sys driver). PKG, cores, iGPU, DRAM domains. Zero driver install.
PyPI wheels: Linux x86_64 (CPython 3.9-3.13), Linux aarch64, macOS arm64 and Windows x86_64 (CPython 3.10-3.13). Other platforms install from source.
Citing CodeGreen¶
If you use CodeGreen in your research, please cite:
Rajput, S., & Sharma, T. (2026). CodeGreen: Towards Improving Precision and Portability in Software Energy Measurement. arXiv preprint arXiv:2603.17924.
Community¶
- GitHub - Source code and issues
- Discussions - Community discussions
License¶
CodeGreen is released under the MPL 2.0 License. See the License page for details.