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/AMD) Pre-built wheel Full RAPL via NEMB
macOS ARM64 (Apple Silicon) Pre-built wheel Full IOReport + kpc via NEMB
macOS Intel From source Full IOReport via NEMB
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
  • 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), 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.0.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.0-cp313-cp313-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

codegreen-0.3.0-cp313-cp313-macosx_11_0_arm64.whl (301.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.0-cp312-cp312-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

codegreen-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (301.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

codegreen-0.3.0-cp311-cp311-macosx_11_0_arm64.whl (301.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

codegreen-0.3.0-cp310-cp310-macosx_11_0_arm64.whl (301.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.0-cp39-cp39-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

File details

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

File metadata

  • Download URL: codegreen-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7704272e0c927e1711ad6ad2741234e333911ea48750a152ebcfb5ea240bf3d8
MD5 0b257b3c33a8550280e861a379686cda
BLAKE2b-256 9658ffa787f8827b8afaf5c847b3e60e36568cbaab9fc67e13dc8f11d8c388b4

See more details on using hashes here.

File details

Details for the file codegreen-0.3.0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6fc957ad34d4bf63ce116bd760e2a15823d529f06e1193eec9d8ec4230c43dd0
MD5 02f7ff004d1439506b2d75fd7bb066dc
BLAKE2b-256 f981e2203082ca88081907d46e216cf098cc211b37f003506b21faab4f5379cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d76d486e31aaa34c829e961a8191a7c6d058894bfdbb4eab76d8f19dfab2d1bc
MD5 09cf377a1501684443a5db6ffca3b4ed
BLAKE2b-256 1fc1e7fd2503c0fe5e217e1fe15311b4f6029976dbf1be9c8cf6103cc9ac63e3

See more details on using hashes here.

File details

Details for the file codegreen-0.3.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dbfc94f60e9537b08dc50a45e5862c1ffaabfaa899a9755110fa16d09fdb1452
MD5 2ffca2f96eda39c29e00580c78384049
BLAKE2b-256 8caa49c149ec19dc81090098f97b1a4cbbadf75d10245b0cdbbf5b4f1724b1e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 99b9865ef3e28ec628ca979c21792bde5a3ae706ee0b88977f7fbd1886f558df
MD5 67166281181fa5f3365bbfba372fe7c8
BLAKE2b-256 945b1fd9e40c88c954a5ef75ca193cc1351366e25a3a73cc6b0214f75c0be872

See more details on using hashes here.

File details

Details for the file codegreen-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 33343846e802279780df4dd85eafc616a64633255e606fca44e6b4342199c654
MD5 59094406dca43c9394bfe7e0e5633c36
BLAKE2b-256 b3199b18e60ed53658fc29a87761164ad502d62cd017c4ac42a70e225297dbce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8dc24272325858a1f3532cf0ef04afc28c5790e1c927000f0806d96bb021b299
MD5 7151f4f79991b0a2b6186d48175cca5a
BLAKE2b-256 8ac84c4c12032ffc87f60f25a07e6af78a08bbecdf516ce853119b61a292a2fc

See more details on using hashes here.

File details

Details for the file codegreen-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a3eb68d933195b39bde681fd788d66eea4afba4aeaafeb19e776f609a3cba0bb
MD5 1861102365d97d6b16baae156f6fa950
BLAKE2b-256 bc453f71841fc5887963cf3ae3f24f7ade5049ad034362090b651bb29218b324

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1edd1075e4934f69b80a6c9381be62663e30ba15f0e194628958b1b419d813d1
MD5 29f4013e5a5769f90b6d5d828ba35257
BLAKE2b-256 edaefff50458987b0b7b7c28b60748c764a674f2b891da084f964073590cbc46

See more details on using hashes here.

File details

Details for the file codegreen-0.3.0-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for codegreen-0.3.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 93c29ee6f08400c667c2ad91074ea8a9dc93084b32c4442d5727a1644484005e
MD5 171311a5ced3e66979d189359a1c3bf8
BLAKE2b-256 d73e62fdc1327a574e116ff7179cea0a9cd6c28f9a96482f27fc6e58b4e08e3b

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