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: energy measurement requires sudo (IOReport access):
sudo codegreen run -- python script.py

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, sudo for energy measurement
  • 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

@software{Rajput_CodeGreen_2026,
  author = {Rajput, Saurabhsingh},
  doi = {10.5281/zenodo.18371772},
  title = {{CodeGreen: Per-Function Energy Measurement for Multi-Language Software}},
  url = {https://smart-dal.github.io/codegreen/},
  version = {v0.2.0},
  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.1.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.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (461.7 kB view details)

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

codegreen-0.3.1-cp313-cp313-macosx_11_0_arm64.whl (302.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (461.7 kB view details)

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

codegreen-0.3.1-cp312-cp312-macosx_11_0_arm64.whl (302.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (461.7 kB view details)

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

codegreen-0.3.1-cp311-cp311-macosx_11_0_arm64.whl (302.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (461.7 kB view details)

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

codegreen-0.3.1-cp310-cp310-macosx_11_0_arm64.whl (302.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (461.7 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.1.tar.gz.

File metadata

  • Download URL: codegreen-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 cf4944bb60c8b664506284682961bb5afe0f45ef479436192d9c225c7ae5a248
MD5 8d1acdde47a53332cbe66e1e4c011c01
BLAKE2b-256 12a5b5b576ff4991c9dcc2f9d9f2ab572d18ed946c3d9102209600e0c5957141

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 49743b6d11e181437ac77d9870dcb62af7870afdfe8e31978e2b097fcc8aad82
MD5 0dd6ed1371071f1b0071651754f81f38
BLAKE2b-256 05c378858bfbc65490a4673a8489df78d387b6ec1a6542df2a43707a143e591d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4e9eade9d9cc9f0aa1383727e69107f89735b6c0d341b655735399193e90ce26
MD5 b983fc6ed1a8aa9566a65ab4b4899ea8
BLAKE2b-256 e06181daeeb205af1f637cf051c8d442910c5679b891f164b7ad320c2e21538e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 779757a61451d69881bc6eea2947ab6bc4104a4b61d59f7d467e4a9f0773d02c
MD5 0e0a4890980167110e7db14f998c071d
BLAKE2b-256 e64be7ae564f7a0f2da0154cfe473311d5e8e380606ed9cb953045225e8830dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a1568429e1958b9c600b712619bffb925b20fd3cbe1ed41ff6238c27e9d07a60
MD5 4b8153bda0c86345b91d5c1db1847820
BLAKE2b-256 dc24721db67e6900c8aa2c950263c8965b38e81ed4636d7b1e52f6a1e7ef8d0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 36112cc36aef22266d51cd69135b4cde8c91801bcd3e1fc9f221aa37cd7b8913
MD5 6de2fddee72a7dcaebb35c94f6873934
BLAKE2b-256 c225e17026b378cffcf615439d6255996eb5f065d0a8b0181b5fa79ec9a076c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b0072d06b4e2bb84c6099602e84d9cf399cab92fa1aa1231d4cf4d4b1aa31eb4
MD5 314b5abce69d7b1c0ac51c6c6fde4028
BLAKE2b-256 1d4fcd8166c9151cc7405782a886a1f355a96ede9bb98c3760d77441b347fbe6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d81fb837ff98e4bf119c2cb71138fb8b4fdf19ba3918b8a248b0f51940b7d4f3
MD5 ee01d4321e71ace9e491ac54a26d2777
BLAKE2b-256 170bec293a74d31fa89c4f60d843c6dc4588c1a8d08e5056dc0f3dfc309da4f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70fad30a42b472d5fbfe3f16bf75e33f3d51ede9a4c4af16885859e08e78921d
MD5 afcf9a85ae68eabe97d1cc38b76396fe
BLAKE2b-256 1fcf78fdb619eefeb32f2f3f631a22825071625628051aa9b234f2546ff6d6d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 def43e2b8e384c7f5a21f39dd38e782bb2c3683ea4124f8c4b611d6198f28308
MD5 a826ba0be656668e7fcb2cb7c1deabf3
BLAKE2b-256 e18578056dbdcfc687cd7bfcb4c5e4773dec3ae17f807815b4806596ed58609b

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