CodeGreen - Garage for Energy Measurement and Optimization
CodeGreen is a comprehensive tool for fine-grained energy profiling and optimization of code. It provides real-time energy measurement during code execution, identifies energy hotspots, and offers optimization suggestions to reduce energy consumption.
Installation
From PyPI (recommended for users)
pip install codegreen
Pre-built wheels available for Linux x86_64 and macOS ARM64 (Apple Silicon). Includes the native NEMB energy measurement backend.
Optional telemetry exporters for codegreen monitor: pip install "codegreen[otlp]" (OpenTelemetry/OTLP) or pip install "codegreen[prometheus]".
From source (recommended for development)
git clone https://github.com/SMART-Dal/codegreen.git
cd codegreen
./install.sh
# Linux: RAPL sensor access (one-time, requires sudo):
sudo ./install.sh # or: sudo codegreen init-sensors
# macOS: no setup needed, energy measurement works out of the box
Platform support
| Platform | pip install | Energy measurement | Backend |
|---|---|---|---|
| Linux x86_64 (Intel) | Pre-built wheel | Full (PKG, core, iGPU, DRAM) | RAPL via NEMB |
| Linux x86_64 (AMD) | Pre-built wheel | Full (PKG, no DRAM counter) | RAPL via NEMB |
| macOS ARM64 (Apple Silicon) | Pre-built wheel | Full (CPU, GPU, ANE, DRAM) | IOReport + kpc via NEMB |
| macOS Intel | From source | Full | IOReport via NEMB |
| Windows 11 (Intel) | From source | Full (PKG, core, iGPU, DRAM) | EMI via intelpep.sys |
| Windows 11 (AMD) | From source | Unverified | EMI may not expose AMD RAPL |
| NVIDIA GPU (any OS) | Automatic | Full (cumulative mJ) | NVML |
| Other | From source | Time-only | Fallback |
Requirements
- Python 3.9+
- Linux: kernel 5.0+, Intel/AMD CPU with RAPL support
- macOS: Apple Silicon (M1-M5) or Intel, no setup needed
- Windows 11: Intel/AMD CPU (EMI via inbox intelpep.sys driver, zero install)
- Source builds: CMake 3.16+, C++17 compiler
Usage
Measure energy of any command
codegreen run -- python script.py
codegreen run --repeat 20 --warmup 3 -- ./my_binary arg1 arg2
codegreen run --budget 10.0 --json -- python train.py # CI/CD gate
Per-function energy profiling
# Coarse mode: total program energy
codegreen measure python script.py
# Fine mode: per-function energy breakdown
codegreen measure python script.py -g fine --json
# With energy timeline plot
codegreen measure python script.py -g fine --export-plot energy.html
Project profiling, monitoring and timelines
# Per-function hotspots for a whole project (build once, run, attribute)
codegreen project python ./src -r "python main.py" -s "**/*.py"
# GPU work attributed to the launching function (torch/CUDA)
codegreen project python ./src -r "python train.py" --sync-device cuda
# Continuous host or per-PID sampling, optional OTLP / Prometheus export
codegreen monitor -d 30 -o energy.jsonl
codegreen monitor --pid 1234 --exporter prometheus --prometheus-port 9464
# Power, stacks, GC and GPU kernels on one axis for ui.perfetto.dev
codegreen timeline --nemb nemb.json --jfr run.json --out trace.json
Manual measurement from Python (Session API)
For span-based measurement of arbitrary code regions, import codegreen.Session directly — no CLI, no AST instrumentation:
import codegreen
with codegreen.Session("training-run") as s:
with s.task("data_load"):
load_data()
with s.task("train"):
model.fit(...)
with s.task("eval"):
score = model.evaluate(...)
# writes codegreen_<pid>.json with per-task energy + per-domain breakdown
Time-series sampling for power-vs-time plots (area under curve = energy):
with codegreen.Session("infer", record_time_series=True) as s:
with s.task("batch1"):
run_inference()
s.export_plot("infer.html") # interactive Plotly chart
The Session API uses the same NEMB backend as the CLI (RAPL/NVML/IOReport), supports nested + concurrent tasks, and degrades gracefully if the native library is unavailable. See API → Python for the full reference.
Static analysis (no execution)
codegreen analyze python script.py --save-instrumented
Benchmark validation (against perf RAPL ground truth)
codegreen benchmark -p nbody -p spectralnorm -l python -r 5 --profiler codegreen --profiler perf
Output formats
JSON (default for --json), CSV, Markdown table, and text summary. The JSON output is a comprehensive single source of truth containing system state, per-function energy, instrumentation points with AST-stable identifiers, and statistical analysis.
Language support
Adding a new language requires only a JSON config file in codegreen/instrumentation/configs/ plus the tree-sitter grammar. No Python code changes needed.
Currently supported: Python, C, C++, Java, JavaScript. Functions that cannot be framed safely (async, generators, coroutines, C++ constexpr) are reported as skipped_functions rather than instrumented.
Benchmarking
# Compare CodeGreen accuracy against perf RAPL ground truth
codegreen benchmark --suite benchmarksgame -l python --reps 5
# Run PerfOpt Java benchmark suite (JMH, original vs patched comparison)
codegreen benchmark --suite perfopt --dataset-dir /path/to/PerfOpt --jars-dir /path/to/jars
# Generate comparison artifacts for documentation
bash scripts/generate_comparison_artifacts.sh docs/benchmarks/
Energy Flow Graph (EFG)
CodeGreen includes an Energy Flow Graph module (codegreen/analysis/efg/) that builds energy-annotated control flow graphs from source code:
from codegreen.analysis.cfg.builder import build_per_method_cfgs
from codegreen.analysis.efg import build_efg, efg_to_dot, efg_to_text
cfgs = build_per_method_cfgs(java_source_code)
efg = build_efg(cfg_nodes, cfg_edges, "ClassName.method", "File.java", codegreen_data)
print(efg_to_text(efg)) # compact format for LLM prompts
Features: Ball & Larus branch heuristics, SCC-based hot path computation, three-level accuracy annotations (MEASURED/ESTIMATED/INFERRED), configurable via EFGConfig.
See codegreen/analysis/efg/README.md for the module layout, a runnable example (python -m codegreen.analysis.efg.example.demo), and the current list of known issues. codegreen.analysis.cfg.energy_flow still resolves as a back-compat shim.
Architecture
- C++ NEMB backend: platform-aware energy measurement (RAPL on Linux, IOReport on macOS, EMI on Windows), sub-microsecond timestamping, background polling with lock-free ring buffers
- Python instrumentation: tree-sitter AST analysis, config-driven checkpoint insertion
- Energy Flow Graph: CFG + energy annotation for path-dependent analysis
- Benchmark harness: multi-suite support (benchmarksgame, PerfOpt), statistical analysis with t-distribution CI, IQR outlier detection, profiler comparison
CI/CD integration
# Fail pipeline if energy exceeds budget
codegreen run --budget 10.0 --json -- python tests/benchmark.py
# Exit code 1 if mean energy > 10 Joules, 3 if the measurement is invalid (see validity.reasons)
Upgrading
pip install --upgrade codegreen # PyPI
# or
cd codegreen && git pull && ./install.sh --upgrade # source
Citation
@article{rajput2026codegreen,
title={CodeGreen: Towards Improving Precision and Portability in Software Energy Measurement},
author={Rajput, Saurabhsingh and Sharma, Tushar},
journal={arXiv preprint arXiv:2603.17924},
year={2026}
}
License
MPL-2.0 License - see LICENSE file.
Metadata
Release files for codegreen 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| codegreen-0.5.0.tar.gz | 470.7 kB | Without provenance | Details for codegreen-0.5.0.tar.gz |
Built distributions (wheels)
Total release size: 10.9 MB
Release files / codegreen-0.5.0.tar.gz
| Download URL | codegreen-0.5.0.tar.gz |
|---|---|
| Size | 470.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f60c5eac9087e39d607600aff72e41fa12b4749bd04f10b6d7b87b282611bb5
|
|
BLAKE2b-256 checksum How to use checksums |
05da6025eefb54c5350912121b42dc9a5419858fa45d0721830cd96ec23f0e54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp313-cp313-win_amd64.whl
| Download URL | codegreen-0.5.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 589.9 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
786f1fb48bcab75812f20e00cf7529332744300c2a6d7e4e96577e47f4f94dc3
|
|
BLAKE2b-256 checksum How to use checksums |
880d180ebec59b2ea46d01ddfbb0c41e56308759c0b0fa7a9d9c9a3c99eeed7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | codegreen-0.5.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 714.6 kB |
| Tags | CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
407784e56c0b729053796ced12bbdd3312accf2ab7aa7118533e6f014faed7fe
|
|
BLAKE2b-256 checksum How to use checksums |
66d40d1e3cd86f9f071902e4aba2b1b60908c40fe04db705440f58eb1e69307f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | codegreen-0.5.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 677.3 kB |
| Tags | CPython 3.13 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
9f4804f64693352ff9c3c9a70e1c46ae21df6d251bf0bbe51035c48efb987add
|
|
BLAKE2b-256 checksum How to use checksums |
227c0095b3d6b60074652b652efb94e9b5f97d7764cc432d17589d2333fca202
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | codegreen-0.5.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 450.4 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c79756b6b558130c3edee8f83fe3ea2878aacb9fece10b0b8400d51b60c3eea2
|
|
BLAKE2b-256 checksum How to use checksums |
164bb5f5e67866132c3a86db5f0cbf0e7456b7744f7876eadf7efa117bedc5cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp312-cp312-win_amd64.whl
| Download URL | codegreen-0.5.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 589.9 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
33ebbd885f795415bfcda7fabdd435941adb1757e9e023d96482818392432bb3
|
|
BLAKE2b-256 checksum How to use checksums |
69fd6f9775086374dcefc6ed15c1c5f18e1af9fe36ca49d40198e433f0bf5b6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | codegreen-0.5.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 714.6 kB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
065b71da4f40cfddc657cc8efd66aa7bffc1be270014fe192deb6d0afce84627
|
|
BLAKE2b-256 checksum How to use checksums |
1a85ce63f88e188200f87b4470ec2391755a02ab3b1308d8df99a4fb27838255
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | codegreen-0.5.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 677.3 kB |
| Tags | CPython 3.12 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
35c1e0dd4e0efdc2e6284820dab2642908f2d30933e1b4aaa4e1e2694aa5095a
|
|
BLAKE2b-256 checksum How to use checksums |
d220ba772defb60509f5115d4862e2901519c0ff95442f2009fd1e631f1df8fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | codegreen-0.5.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 450.4 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c63a512b74202dc0ca3487b47fe5f0926aadcf674211ccc286a11052e2daa324
|
|
BLAKE2b-256 checksum How to use checksums |
652d5d350fdf9feafa7430c373e74257762a0a41f2453355549cd74b8331ed33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp311-cp311-win_amd64.whl
| Download URL | codegreen-0.5.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 589.9 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
fabd146aaa9e2b0e66137e088ecfd919b9cacbc75015d84adafbaa05568ffff0
|
|
BLAKE2b-256 checksum How to use checksums |
16e565ba0d4dac97b61df14cc673286af344bc3b634514f577482a772e7a4ed9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | codegreen-0.5.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 714.6 kB |
| Tags | CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
22246150fde1a042d53438f595928ab1a147a6e29304174a45061e89dadcd70a
|
|
BLAKE2b-256 checksum How to use checksums |
a5216d38fe2b137dbe4dccda57b83df3b2899ce9cea3ac9e12c42afd01d5f31c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | codegreen-0.5.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 677.3 kB |
| Tags | CPython 3.11 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
ec5c08d71cb69be2b2ad563ee3607a9c26048f9fae958601e8cb83fab9e37dda
|
|
BLAKE2b-256 checksum How to use checksums |
a06c1298e193fa48520703f3415f777806a95e09728291e339086b17df5645aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | codegreen-0.5.0-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 450.4 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5cb7af80571e509eb4b846f097a030b562a850019916e91e3c719d1e6afe25a3
|
|
BLAKE2b-256 checksum How to use checksums |
d1baa7b2883b7a4ce3202a611770b50e4a2604cb66c6b39c99b29c2fe697e8f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp310-cp310-win_amd64.whl
| Download URL | codegreen-0.5.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 589.9 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
9fd92da742f095349100fe96d0080c350baac6c1c1f14528605c02336dda8a1b
|
|
BLAKE2b-256 checksum How to use checksums |
e989869d52c28816335af6ab057d7b63f73c328a13d9e9e7efcb472455d66ecc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | codegreen-0.5.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 714.6 kB |
| Tags | CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
ba70e29ad41ebb51fcdc4839a209e9beab974c647d75b8da0784e25c5518ad25
|
|
BLAKE2b-256 checksum How to use checksums |
244f96a537dd2e7c69c8bf9248b3da5a701121eed9dbb728d2fc385c4e9c0576
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | codegreen-0.5.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 677.3 kB |
| Tags | CPython 3.10 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
a48c7d31e4929f853b08f37960450adf3157982bbb5ffd940ea1a20555b5681f
|
|
BLAKE2b-256 checksum How to use checksums |
e956b56049e29d3b5844482f1db5d5621ad004c3f66e93cf04ad8f9d2600db20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | codegreen-0.5.0-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 450.4 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5db5517966b1f52551638ed240e858ddd2c530b90852ca3097c199b58a7cdbda
|
|
BLAKE2b-256 checksum How to use checksums |
2e309f82f4d616ff9f22d65e6da15866cfeceed72237dce55fd5af927eb47e7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / codegreen-0.5.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | codegreen-0.5.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 714.6 kB |
| Tags | CPython 3.9 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
db94a07b3c27228947eefe55300069c8f2894917211468ce36de193cbe0634c8
|
|
BLAKE2b-256 checksum How to use checksums |
26f63925b664aa15821ead38764fc30261aa46d9b8116dfcc9b9a9216dd40d8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for codegreen 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.