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

Requires Linux with RAPL support. The wheel includes the pre-built C++ measurement backend.

From source (recommended for development)

git clone https://github.com/SMART-Dal/codegreen.git
cd codegreen
./install.sh

# For RAPL sensor access (one-time, requires sudo):
sudo ./install.sh   # or: sudo codegreen init-sensors
# Then log out and back in for group permissions

Requirements

  • Linux (kernel 5.0+, Ubuntu 20.04+, Debian 11+, Fedora 35+, MacOS)
  • Python 3.9+
  • CMake 3.16+, g++ 9+ (for source builds only)
  • Intel or AMD CPU with RAPL support
  • perf (for codegreen run and benchmark validation)

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 src/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 (src/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: RAPL energy reading with sub-microsecond timestamping, configurable polling interval
  • 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.2.2.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.2.2-cp313-cp313-manylinux_2_28_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

codegreen-0.2.2-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.2.2.tar.gz.

File metadata

  • Download URL: codegreen-0.2.2.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.2.2.tar.gz
Algorithm Hash digest
SHA256 7fbdc03f9d1e23e8b3aa4614b982f007b339b0df7302cae8b22646d1817ef32b
MD5 b077a1e63d3470437a61e8fc8d88e563
BLAKE2b-256 48dabf9986649cd23cda6dcea3a42dc472a192f88e9e0cd56695e1698d767451

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d5b680502eddec6aa2d54ddf320106553cc354aebcd048bf5de68062e50fb0d6
MD5 fa95ebbe5f98a90537560f195683b591
BLAKE2b-256 314f1a8bea7e514ce82ae1f11bb1a03e356e1b2dc6d868f8db986e4e6dd6f4ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 665a1f807e1ed225b54553338838358dbb17ec0a35ab8411a65855ba09effe3e
MD5 bddd4abd7ba1e84288dccfc4f01ff8a1
BLAKE2b-256 1afd0e3e11c8ecbe55b7d9415d8fcc4b3b21cc6ae41f1e976c081f78f5b59cf5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 96a2b67fd89098c59c7dfda48340b24b3e90aad3d71011277731ba436be68e09
MD5 d3c6a54315c9a6f36261ff4061968d92
BLAKE2b-256 ed40604e6c281a9a1db746fd87bd37ec65c70213d57894b37be3f144a0bd2662

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 48bbed7982d44b72034ab417420921d85d39b78451d31a602ceda24323b321dc
MD5 d796c464c3592a6c3d236eff0f4a545f
BLAKE2b-256 d8eaacaa4b674b61c6edea3fdf31cdaf8158f896b912f762d25ea57e2fbcb4cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d3ab385952bea343653394885db1c9bcdec71807c7560ef1362c32d802deb75d
MD5 22f4889cccd39cb81a93aa7b3fef618d
BLAKE2b-256 d2eafd2f827f02f89d2607f548e16ef8a6f78e50cdaf1b649c8ed637710bf8a1

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