Skip to main content

Energy-aware software development tool for measuring and optimizing code energy consumption

Project description

CodeGreen Logo CodeGreen Logo

DOI

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 (like hyperfine, but for energy)

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

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++, JavaScript, Java.

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.

Docs: smart-dal.github.io/codegreen

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codegreen-0.3.14.tar.gz (1.9 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

codegreen-0.3.14-cp313-cp313-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.14-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

codegreen-0.3.14-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

codegreen-0.3.14-cp313-cp313-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.14-cp312-cp312-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.14-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

codegreen-0.3.14-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

codegreen-0.3.14-cp312-cp312-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.14-cp311-cp311-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.14-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

codegreen-0.3.14-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

codegreen-0.3.14-cp311-cp311-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.14-cp310-cp310-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.14-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

codegreen-0.3.14-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

codegreen-0.3.14-cp310-cp310-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.14-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file codegreen-0.3.14.tar.gz.

File metadata

  • Download URL: codegreen-0.3.14.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codegreen-0.3.14.tar.gz
Algorithm Hash digest
SHA256 fd081f2d2d7b1552ffa4d08fe71fcc3d29c4cd537ef8058fd2ceb6de2ffe11b4
MD5 96ab3e89c84b0d71b4abd31e69f2d2e3
BLAKE2b-256 78b329c8be21fa27e6aff3c4b8c0e1193550d6718a7e5cc1fce4a3d461388886

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: codegreen-0.3.14-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 300.1 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codegreen-0.3.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 13bd13cb65902ac7ec1939f801572fb0ce451562b09630fd10f811ab7ffe6d41
MD5 ea2956e899ae24a3108f1704d5a2bdd0
BLAKE2b-256 235e011b5d714cee20c55cbe31b561885af5207a4ea83a39bdff18308fe3ba2a

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bd15b10a63eeafd16ccafd1debabc5785fa77d20062170e27658b794be0e940e
MD5 92bb163afe9313f9b22729aaef13a2c9
BLAKE2b-256 c2984566f6c3d52077f72ca71e8d608e67569146bff8806890ec1c26f327551e

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 56f7d5c64fc5fde6587cc9f6962bed33b46ccfdfc832f2400fb6111d64dc8a27
MD5 b29001bcef16c2f71bd0671aaf806725
BLAKE2b-256 302bd0d46d14593ca11e3610ddf795d6c4307c960f3cc72ebf74f3c357371c28

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 75da81946c0bb89a815cce2ed066e0e9d93eda8ec806ca4600f7da163c6cc39e
MD5 dc0d8dd9bbb109ca6bbb565f3e49b817
BLAKE2b-256 8453170a165b459272f3fc064d201f7e6937c26e553c6320e144ba9cac0fc5d6

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: codegreen-0.3.14-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 300.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codegreen-0.3.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 960ed3aaaaa204954ff0e2d4945e8708c80357311518c9fbeae4aa05f810fb94
MD5 adf66c85cd970fef708f8b628b29dcff
BLAKE2b-256 da8122292e3484c4d9c761782f59170b89bb7bf678c3de00be43758bdb8bbd6e

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c8d445a654976c26bca18b603174c3dbdbedca7e5f7a5b77f44b84c5a6490368
MD5 08b65d10018225f9b6f37e34d7e9f491
BLAKE2b-256 af1a34ea6aff8183248d1805b3df9002f9d5556a14dbf4e9e64ef8eb0e661e61

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6619444b8232b4d0473c5bd8e5a087362466763c27b0cf74409c1a1dadf18343
MD5 4b74b56a507d0fdebd13bafcf8823e23
BLAKE2b-256 a1b1c6f334e5de1aa3ca75bbf9eceddb50d67e807699bc9d62b0fac587bcf0ef

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 741b40742362658b82a7540b1f5ad6b488f5cc62e5c77c49ed34db228581fe31
MD5 6447cd7f9b110aeb604596a92ce618a3
BLAKE2b-256 c829584b26165d6c3af07199be350a9ea4d99846ff59d225195d66bc13228a4a

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: codegreen-0.3.14-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 300.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codegreen-0.3.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f7b80032f2a4e9c5dd05fe594ecc916c436ffd985d17f19cf3adc67eb90fc27d
MD5 f3e739c9111990ac40fca0f92d813f8f
BLAKE2b-256 6635620e9b706379cb6dae0d21594e292f1e156e3ee6b3bf0be93d7b38f03b7f

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d3cc7049e631236a726afef29b1792481abfbb0c66009af41f0b3fad2f9cb589
MD5 3024b56a6cb3ac47dde75ae550bda6ac
BLAKE2b-256 486b77f684f88905b2bf4004e5869308e3b19369fb5b784af3d95d6ddb9f1211

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 dbdafd08f417cc2a60a8b101b5f1051047066b0d59d0b2d1805a0825246d6e51
MD5 3a823d5395e9e560d7562dfed5a7b2fb
BLAKE2b-256 86ed2760cb757a065150bc37c44a9ffa813fd286e2be244cbac56136563d98a4

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 503b426ad06f6dbdee5b11e41b2a8ddb5a80bc764e247f549bf107df5810d4c8
MD5 7e8565490d131874e72c25924692716b
BLAKE2b-256 4a8c7e2e912b1cbbd458c7f03ff057e0760ca8da4d7aa344b4f23304d91d1bf8

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: codegreen-0.3.14-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 300.1 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codegreen-0.3.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a9d78982fe16f5314d7d9e19d8b215ea0acb49b8196ab160e9d83170620cb9b1
MD5 e9ffc38359217d53596bafd4ab39efeb
BLAKE2b-256 0a496ff9469ebabd608f0c90a08b60b6e91d66db6032be600e3f5579fe7568dc

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7f4721875569028fe39220c246e5b17bc431e9bdfdd66d44a5e87dedf709506a
MD5 f8200bd91ad2df96cbffec866198407c
BLAKE2b-256 93796e14c7be5d00d393d16b9ff1381c20b2008c8c311f87e17bdeebf8c57765

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 66ca5b7e36a22d646431833db20352b9ce256a18cfb235259b6777adc6298f74
MD5 c4f3084172babaaacd362ac52a8181e2
BLAKE2b-256 b2769691e24c4b4dd1878506f75aed391f73442f49685d22b149ee583de15f05

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d67413a49d511d405a23b589e48fd73d9883bf88e73b9d3e9c4fbbfbb285b68
MD5 1ed6366dc48f3ae0c9f942704ba72c9f
BLAKE2b-256 700cae76e5121e00a030412e40a4c148c839a9925398937a8f56db8730254ac0

See more details on using hashes here.

File details

Details for the file codegreen-0.3.14-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.14-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7d7ecdab0bcf681dd5a91c675afa3d6ab117136095e45baf198f3879424c99b5
MD5 37b959fb70fca21d395636a302e93252
BLAKE2b-256 a5dd87be8b61ead37e09f38489bdcc1f6c5f460d10c14c0de161ec982902f254

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page