Skip to content

CodeGreen

PyPI PyPI Downloads GitHub stars License Platform DOI

  • Precise Energy Measurement


    Hardware energy counters via RAPL (Linux), IOReport (macOS), EMI (Windows), NVML and ROCm, with a validity verdict on every result.

    Getting Started

  • Multi-Language Support


    Python, C, C++, and Java instrumentation via Tree-sitter with config-driven extensibility.

    Examples

  • Continuous Monitoring


    codegreen monitor samples an already-running service, with cgroup_v2 per-PID attribution and a UDS annotation channel for per-request energy.

    Continuous Monitoring

  • Interactive Visualization


    Energy timeline plots with per-function breakdown, hotspot detection, and zoom/pan via Plotly.

    Reports & Visualization

  • Easy Integration


    JSON output for every command, CI energy gating with --budget, and exit code 3 for invalid measurements.

    Installation

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 measure instruments a file; codegreen project instruments a multi-file project and runs your own build and run commands (Maven, Gradle, Make, plain Python).
  • Whole-command energy: codegreen run measures any command over repeated runs, with confidence intervals and --budget gating.
  • In-process API: codegreen.Session brackets regions of a Python program.
  • Continuous monitoring: codegreen monitor samples 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, run and project exit 3 on an invalid measurement.
  • Visualization: --export-plot writes an interactive Plotly timeline or a static image.

Quick Start

pip install codegreen
git clone https://github.com/SMART-Dal/codegreen.git
cd codegreen
./install.sh
sudo codegreen init-sensors
codegreen measure python my_script.py --sensors rapl
codegreen run --repeat 10 --sensors rapl -- python3 my_script.py
codegreen measure python my_script.py -g fine --export-plot energy.html
codegreen monitor --pid $(pgrep -f gunicorn | head -1) -d 60 -o energy.jsonl
codegreen project python ./src -r "python main.py" -g fine

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, run and project result carries a validity verdict with reason codes, and the command exits 3 on an invalid measurement. See Exit codes and measurement validity.
  • --sensors (and measurement.default_sensors) selects the providers that are summed; a total that mixes GPU and CPU energy is flagged GPU_IN_TOTAL.
  • Per-function energy replays every call: inclusive_j, exclusive_j and a per-function quality verdict. project percentages 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.

Full citation & BibTeX

Community

License

CodeGreen is released under the MPL 2.0 License. See the License page for details.