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.8.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.8-cp313-cp313-win_amd64.whl (298.6 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.8-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.2 kB view details)

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

codegreen-0.3.8-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.6 kB view details)

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

codegreen-0.3.8-cp313-cp313-macosx_11_0_arm64.whl (277.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.8-cp312-cp312-win_amd64.whl (298.6 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.8-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.2 kB view details)

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

codegreen-0.3.8-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.6 kB view details)

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

codegreen-0.3.8-cp312-cp312-macosx_11_0_arm64.whl (277.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.8-cp311-cp311-win_amd64.whl (298.6 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.8-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.2 kB view details)

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

codegreen-0.3.8-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.6 kB view details)

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

codegreen-0.3.8-cp311-cp311-macosx_11_0_arm64.whl (277.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.8-cp310-cp310-win_amd64.whl (298.6 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.8-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.2 kB view details)

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

codegreen-0.3.8-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.6 kB view details)

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

codegreen-0.3.8-cp310-cp310-macosx_11_0_arm64.whl (277.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.8-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.2 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.8.tar.gz.

File metadata

  • Download URL: codegreen-0.3.8.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.8.tar.gz
Algorithm Hash digest
SHA256 e501025bd3abfd71aa29631454269b0f0172a21af862e47066ce8aea30f7f7b6
MD5 ebcfae0a1b15dfdef384d33f80bc4bce
BLAKE2b-256 59f14f6a19f335113e6e33c111a71eaae8bc74f3c5c517991cb63b9a6745470d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 298.6 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.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2b3ebc3ace56c6e876084f1588c9e08d6c5412f970a9ae5a745ec7b8bfb645af
MD5 f4ae0613636d61bae934fa3c45846411
BLAKE2b-256 97745b6423ed5d50aff198340908a7d62f93a1314f4d24cb105f20d0027d870c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c0ccc081467279c8f14275e67f86ccdf7f272c3c0b2083869cf183f202468c02
MD5 c37444d5a78a1e902af4b66d5eaf09b4
BLAKE2b-256 fde5e2614b34c9209367a82a3654728ca0bfabd8df18016a2cf19ad10e4bfaf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f0970bed9d2c0c0a6cb7507e2051830846f2c4f97517772b1dd3357a96b8898d
MD5 b3b44d595177990ea29c2b1bfd4473fc
BLAKE2b-256 25fc80219fde24b7aaee89cf664f45010382af14af20ccc6ea0c726d48cac79a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8f53ae55bc34976453f0351294caf2db90218f5c38ecac56c7bc126ca34e2c86
MD5 397acbc26ce77747c940bc9c5f783019
BLAKE2b-256 c6d393a6957700abfe30bcf2dba67ddd3a454797b2fcffa76768e57243b77da2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 298.6 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.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 047bb537d8ccd98c8e7bbaddaf2214ba88c6f5819f712d46e6668ba302579716
MD5 8a81b1847bf9d609f56357bc1cb24a78
BLAKE2b-256 68e02559b6576042ab3d06aa75542ec16946cb36dfa4e1a24810e562b2027756

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3f9482a799b522126d7cdcbe682c0afa30f9ada0cdb7b2b6d32f61f9755b4ea9
MD5 a36cdf6933f920a3f1bd4d8ad183536f
BLAKE2b-256 3314ab325f9fee10939d379137db4eb777b34664abde843dc15da121deb0cf39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e3289303db19b17f0c8a1c9f92c322b32f884bd8d06dec1635505ea3b232331c
MD5 d8d9f4e0060c850becfb852a49a44373
BLAKE2b-256 e1f8700ee0d96b5e5a05ed52d1cc67d896071dbc8a14ad074656fa41a30446e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 74d005a5d4c9719764347240606262e50abf29bcf30db74c003d96559d437969
MD5 46ba84a9be155bd831453553b793f668
BLAKE2b-256 af67506ba7c52accfe466145061d3271f1468b4f3672dd7bb629f176664b5ced

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 298.6 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.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ac6854c48cb36b7d3b7747e0fc6c4f7ad630a6718023979c62017a0d6d535449
MD5 6e734e839582cf4e08b9ce40842c98bc
BLAKE2b-256 94141de912ae99e7e3b8d5db33fb00f7b9648cc5a2c809a703bdd7c736566112

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c57567b346502e4dc550eb708a9153d78fcbc7531e8bc05e35f3200f909fef31
MD5 9c60c1c25a19787a53cad75a3621bf60
BLAKE2b-256 1a0d824eefae17eef6c0dde9e77626b1dae4314cff1111237920c2832b95e632

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6ee60990ecd07c817ed303a6981ff79ce80203cc00bcc573e164a139316ac3ee
MD5 676d28ac7bd924dc4a194292cff0e02b
BLAKE2b-256 da625ae5ba4f7399b9f298ebece84ca2aa3f79a2f593ba0d31ca906033e5cb38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b04539b988948d65b4c0bd454f42f28e1bc3545d0a407a4ac9fd40ad69de2607
MD5 c9de82091d2d8442f22f83f04f1af4d6
BLAKE2b-256 e89af28f9f4d72da3150e32039fd594747b00387b819ed075239b4ec05e18b55

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 298.6 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.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4dab4434be77b6c6285ff151fb77b6b0c9ec49d86b1bd2ced1c87d771daa8aaa
MD5 06471c5959a15bd64d7454af87fd7305
BLAKE2b-256 e36f184c266c682649f765e99dda723bd7776dd7868a1a432eb68db335f844aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cf728ef25e5dc193a55c70d32a414c1a71e30c5e1003cc6c8dcc83f808424090
MD5 a18bad399a683df24c44eb5673c11ffa
BLAKE2b-256 05212f794794d586651be15068eb31e1f45e5ccb54817f1e3f3435add9118478

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 def475641706f882f81214a4c4543a24f7f5a977cc0c4902096d2b666f093b96
MD5 d49c242c3d6325ee20dedeb4174eac1e
BLAKE2b-256 65dde9559feec92f791cbf88c93a52fd35f7942c8f5c09a2189da17acc2b0431

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b7c78adf1a968aa4d3872325a58f20ba258a4209545554cdc65279e040d35aaf
MD5 a7ac4d163ba9869eaab532502e08773d
BLAKE2b-256 8f91e36cb59ec2a8b42cdc06da6f231401fdb3391452ded41df4ec02c516ba75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.8-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3a6e097eea8b7d25bb2e82ad2e0015f2e646d2e1d0a8552c060a290607829388
MD5 5e1fab5e7260edd7acad803509fd6675
BLAKE2b-256 9ed72531f53197f2ac39e7a75e5caf819a213d933bd377087d4c6a76e9791cdc

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