Know the true energy cost of your code
Project description
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.
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
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 spectralnorm -l python -r 5 --profilers codegreen 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 config exists but is not yet exposed via CLI.
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/cfg/) that builds energy-annotated control flow graphs from source code:
from codegreen.analysis.cfg.builder import build_per_method_cfgs
from codegreen.analysis.cfg.energy_flow 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.
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codegreen-0.4.5.tar.gz.
File metadata
- Download URL: codegreen-0.4.5.tar.gz
- Upload date:
- Size: 1.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e1bf2722052c3cd820709a91daecc59dfefbb6bfa85820d244d37c74687de14
|
|
| MD5 |
dd223ece005ef555ee8ae00be7542a12
|
|
| BLAKE2b-256 |
bf1aae3561dc2ab3165f1f2552eec2ea7512350c5242c59a4510b9d23a684c03
|
File details
Details for the file codegreen-0.4.5-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 329.4 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c5bc52b510c5fb455f17d2bca2f00e8ef577d2a7f9bff7608ddce076afdb510
|
|
| MD5 |
1f101a2dbedcc2a64b01d2227efb60b1
|
|
| BLAKE2b-256 |
77c233a95b4f2c09f372eff4c14d9d4e1135498a8145b948421360219f1adcc8
|
File details
Details for the file codegreen-0.4.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 504.8 kB
- Tags: CPython 3.13, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60ca3f2afe81fe0911e06c6538596d6a682c39fdf967e32c2a41220b23c035fe
|
|
| MD5 |
9b0a11e8b914c7320b7c25c05a484a94
|
|
| BLAKE2b-256 |
9659dc45d94ae014e0b9c5c2ad65b2a6c3e8802301ac63f04fe535c5bacef650
|
File details
Details for the file codegreen-0.4.5-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 475.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.24+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64beac2266cf406328255cf891f2f4795c0609eb99de8f8d5a377d92876ad738
|
|
| MD5 |
cd1698f0cf3100f4643add6f7d2b1b8d
|
|
| BLAKE2b-256 |
31f07570725b80be88c730cc8d27fd443623a54c0d4df922286b1b1529e7aad9
|
File details
Details for the file codegreen-0.4.5-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 308.4 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcc081023743db54042ee40ea8edbefd8a06e57512d1ea11b68dc1485b73f3be
|
|
| MD5 |
d629354800cff3d9f8c461745e467423
|
|
| BLAKE2b-256 |
f8f478450871fe0d1bed2542ac905cf5261274389c21a12b5e9800098da22eca
|
File details
Details for the file codegreen-0.4.5-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 329.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b105509e78d751edf6ea5f22576d0f2a876e1c20351d94f0c059c9284434e721
|
|
| MD5 |
641c0462bbad9002c68a36c12985c672
|
|
| BLAKE2b-256 |
f42f99e5f230de53cb089d7e91832d9b099b16a2e3d3ab13b90818f1864beb73
|
File details
Details for the file codegreen-0.4.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 504.8 kB
- Tags: CPython 3.12, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c27d0b6c86123093bfeb8b64f73f6d00f2bdf65245090adeafecdd4a6e33255
|
|
| MD5 |
5567d83b2e369c5fe6cd7cacbc0cb983
|
|
| BLAKE2b-256 |
edc1337efb084372d4f968dbf1ce5226429d2ad036e6c8c4a2eea817bbfecd4c
|
File details
Details for the file codegreen-0.4.5-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 475.5 kB
- Tags: CPython 3.12, manylinux: glibc 2.24+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be62ac0d056855b5de25cb200ff0f363af420192e644f9b3762a98ec15dc6bfe
|
|
| MD5 |
5f5d515e84779be4e39b80215fda7c48
|
|
| BLAKE2b-256 |
a5372582460a33d3b5994bf8409b6831cee4e64890a622baf8ff872f775e8887
|
File details
Details for the file codegreen-0.4.5-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 308.4 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0944870f0d4b39bdb6e916961a970533239d7f816293764b0f6ddf90ec9feba4
|
|
| MD5 |
f38e3a6d9596a539a3a8de371e739cd6
|
|
| BLAKE2b-256 |
229bd812e310580771d74db9fbbc90b36ed93cda9538b61a3745b966e89631d6
|
File details
Details for the file codegreen-0.4.5-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 329.4 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4651324dddc0810a18cc4d200970640d8f7c0933ed20f1f0aa468cffd7afeab6
|
|
| MD5 |
d739a8eef5da9e6f6b7175e67a3736c9
|
|
| BLAKE2b-256 |
514e0ed88429aff07d259a75a32a7237bd0be08d275c66614522320af5255ae9
|
File details
Details for the file codegreen-0.4.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 504.8 kB
- Tags: CPython 3.11, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ebbbbb3b421ece453376a2b9353a2a12987a6333aca0945c8512f48582e37fe
|
|
| MD5 |
14b32dc28f2ae85f2cf97cf20b00c2a9
|
|
| BLAKE2b-256 |
bd50369238f782e137ec846730acc78b10ebdff3ffd2885127275401546f74e0
|
File details
Details for the file codegreen-0.4.5-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 475.5 kB
- Tags: CPython 3.11, manylinux: glibc 2.24+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4df4e5641f1ce97e560f7434afc4603144e00561aac5a1e9683ebb8b38c30732
|
|
| MD5 |
3ab232937c9d804ea6f6261429719b03
|
|
| BLAKE2b-256 |
86c52724d2045d2e8f4880af8767db99a0da1d7fac9065c018650b3b3e3c3ff0
|
File details
Details for the file codegreen-0.4.5-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 308.4 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f950d36fe4cba09836defa51df7c73783c5b0f800434aa38762456b4baac9d6
|
|
| MD5 |
32f3efcf46422cb647b260bef93471fd
|
|
| BLAKE2b-256 |
71ebaa3eb09c05c013d71b0dbf9836d8e1b04cb677f494059376c05c6891a3e8
|
File details
Details for the file codegreen-0.4.5-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 329.4 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35974dc0712a13a0ba7afe3ffbc883cb6faa1271d46c0a697767d86aab98a910
|
|
| MD5 |
992a7dbbfc896856e4dc21213c3205a0
|
|
| BLAKE2b-256 |
35ac5776c68b44fb89eed1fe5faf4bd05bbd42641cb6f53c5cb85d89a6c0942b
|
File details
Details for the file codegreen-0.4.5-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 504.8 kB
- Tags: CPython 3.10, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf7587a04e547a45a832d865ce481ad42aa6284f3f64e743e2cfb04218433546
|
|
| MD5 |
3358203b4c147695a561d0df95109e73
|
|
| BLAKE2b-256 |
a7d64334085a4d9bb95c14af1bdcd2e43e2228d78a3eca8e403bc5d5812b6d1d
|
File details
Details for the file codegreen-0.4.5-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 475.5 kB
- Tags: CPython 3.10, manylinux: glibc 2.24+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d969ac509c5677e4158641d09b458861b68c127a1fe98a92dc03490feb7eefd
|
|
| MD5 |
0a9a1da73f81a1387acb740385d4a890
|
|
| BLAKE2b-256 |
a760434847d2ccbe635745dfba2229a53be67794020f63d53f6eb40fc9815186
|
File details
Details for the file codegreen-0.4.5-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 308.4 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b28d31a9edfa2a75366977a552bf4c031814f9f041ff0a360ada153a38ff6106
|
|
| MD5 |
2997098643ec9611742724a928716914
|
|
| BLAKE2b-256 |
5d7e45a874be24012d11703158fbbdda6f518bec025d26b58310203814ae8b42
|
File details
Details for the file codegreen-0.4.5-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: codegreen-0.4.5-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 504.8 kB
- Tags: CPython 3.9, manylinux: glibc 2.24+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f86666258c842cc082ce48c02aaf5217ebb9d6bfbe1d4efdaeffd630e3bbe75d
|
|
| MD5 |
6a4e4440d183468842d94fe38a9fa4fa
|
|
| BLAKE2b-256 |
8f6f916990f89fb8a20cd772fd39c37dc333c1dc803d8cbcfabb26263d86c2c7
|