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.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.2.1-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.1-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.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: codegreen-0.2.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.2.1.tar.gz
Algorithm Hash digest
SHA256 165611b740341b4ca0879016ef8af118ccea18547c33725b7b3c3e86f1070ee3
MD5 05423cb89c469fb9c27bdcd88030dbc9
BLAKE2b-256 513ac63e5ba8ac4bfbf7be5ade41ab143207939eecccd0638461cce376164c03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.1-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 53ae8825dac6238029deeb09b7a2da80a8e36e516d01ac0bc48d8d12086efee6
MD5 fb41b3ae340d630c1c84dc5cba754baf
BLAKE2b-256 690718cb6b3c6c2efac0127d60636a87aedb4f3e6a9f06a800579f79103f3723

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f842236340fbd9a7a9bb2b2150b0c31c3d94183a32d876267624c07c88df608f
MD5 b110673ddbed0a7fe554e4a45d240c5e
BLAKE2b-256 e0850483f583bc88e250305a75fa6b0d4a68ddffb388a322ec105b829c52c275

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 60d3d9c03f356af8bdb08258f6c3be34fd215387c5ff0750141c21a878644e8a
MD5 cd56a8afc6e84a4f94b28296d4733e9b
BLAKE2b-256 39c967a6bf9279aa1173ad5d2ccc4aab70df7c16fc98775443c1cd2db9d97a3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8bef686817c8c475c8d9ac79d875f23d1918463cbdc5ff3aa058997db3f89817
MD5 11c61a94c551605aec85a715dc3030b3
BLAKE2b-256 71bf15c443f736f86f2503b7ccdb26ebbc709eb59ddb5069e546a952feb65abc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.2.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1ee8538fe4b502c1bc3c5d82903c91d1a7178d9b337bacc83f6116473cf9862c
MD5 7d062ea9c1cd45efbcdbca3d1284f2f9
BLAKE2b-256 99b8f7ebdcb4c658959deeb1eb7895025e3ddeed03a3b169686c490469d276a4

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