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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.13-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

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

codegreen-0.3.13-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

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

codegreen-0.3.13-cp313-cp313-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.13-cp312-cp312-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.13-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

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

codegreen-0.3.13-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

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

codegreen-0.3.13-cp312-cp312-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.13-cp311-cp311-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.13-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

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

codegreen-0.3.13-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

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

codegreen-0.3.13-cp311-cp311-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.13-cp310-cp310-win_amd64.whl (300.1 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 kB view details)

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

codegreen-0.3.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (446.0 kB view details)

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

codegreen-0.3.13-cp310-cp310-macosx_11_0_arm64.whl (278.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.13-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.9 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.13.tar.gz.

File metadata

  • Download URL: codegreen-0.3.13.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.13.tar.gz
Algorithm Hash digest
SHA256 b1380bf91dbf67bc4df2d225bd3f12277fee36a16f2068b864a51dbe906acb40
MD5 9d3b4ad90a43f64f142173545fa60781
BLAKE2b-256 b8577154486b2600ed011253cc8358abc209a0618e2f5ab7a440e5cf15430e95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.13-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 300.1 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.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 16cfc4ed56d04d4e94503221e416be5818c5f737a3a28e6a330235b3c56b6023
MD5 5adcb996854feace447a99d9a1b8fc0e
BLAKE2b-256 273610feee9ac271de93b1453351466629e734b3e89086cc89afb468183e3de8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c59cf7e415e0d667a5e9b0f68a42e6ed2d7e2fb6bbaf3f0c4d86e99729e70a6e
MD5 046e90282f845652604bab0ea143f11e
BLAKE2b-256 f2a98f3d0a22984afa743d57652712cc8685a6803ae2c1e629605d1123681157

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d7cce800403cc193de19bb746e63d33f20daa60f9001ab797f0ad197052c4a86
MD5 0ea6be993c5cab8e965144c2b33f833d
BLAKE2b-256 20d8ad78b184efcf60ccc90965de237465e9d76a57104ef558e9dbe88b0d845d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc08eea2a918909f3dd4992f6509d1381481a2829e312aaa4ca82257b7d5f4af
MD5 02e8ea1b5355934aac8af63fcef5d923
BLAKE2b-256 50d7a46aff76a30eb7f3ec94461676c5fdc6f5e84a0eff49659e9c31ad1fcc15

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.13-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 300.1 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.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 10d8ecd5137e55861b9cae58753c153fa9e16031fa692b9bebd8152366d2878c
MD5 47addbe8fc7b01ee51f8f76a81f6ff55
BLAKE2b-256 947d0b5c3ecefcb3b3f3788294661e8dd20e826a4bc853ec982cec5be301374b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6d8a413e9b6bf0538137e6c9b6723be077d48ebf6b0bf16a60cfc44e53c0c423
MD5 eaea17d48231c36bf10c6ae3d1b04a3a
BLAKE2b-256 2534db29007e384aff2ab24ed6408e5037404193043bf3b5e17840f336eff660

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 46910e524b8ee9b229be0f7c227e7bd944641a9b231e8bdcc45c090fd70e7ad5
MD5 0f750095b22b2c9b433891bebf0234b8
BLAKE2b-256 f8437e913f223edf14e7380aa266a75322f8810e8e73d048065ca4b2a36d496e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8686b029212a89f166ae77684137609c57eab637b35d20a440e2e9f987eba5c2
MD5 37a942b0b64c6cd71226ac583778ef8d
BLAKE2b-256 89f8dd7f1c2ef6143492e95628ab31b6211c6720320e90ecc2aea22e3f5c9aae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.13-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 300.1 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.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8ef13909811d5b8c95c7cb02079c8dc28b73789e7cefc3790196b53801447765
MD5 4e6f53d57bc41a2c40bcacc7e021becc
BLAKE2b-256 c27fbbe521a2136c768443334352009d50d6bd39a0d2618e23f39f5ccaa9377a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 428b5d4bd4e4c9d60503cafd7cde63fe515de4ef481a4554f9393ac6b5750072
MD5 55fbb6ed6d8730f6c986d2cadd79c92e
BLAKE2b-256 92523348c79f12eca55298160352ee20a51fef8c866753fbd45c43ec41ef2fd3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 47b1e64382d3bab621d7a517dc6f0865230727d9784a44ad2e6bb9a23bcb9652
MD5 accd9c94e33c6b43da93ad58752bb70f
BLAKE2b-256 2a88ec77c9f9c3773cab2566316c2bc728038d3b5382ddd46d384c5b89e34d4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d84066eb749211ca7e9ca7cdbcbd489573fcb3a34e94c1c000497301d9ea5e41
MD5 752e02b49053115fd6bf0c8aad348e4d
BLAKE2b-256 139b0dc5932f742a7e89139e042ebdac58a9b13c097d46c61f9803ff4c6d82df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.13-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 300.1 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.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 45dddbe4ee15fad9e505228cceab4433ea77e6541fb21f143b7df36e8f279bf5
MD5 d2e4473f7ca2889d587ca8c3b07dc790
BLAKE2b-256 fc15b38d5f5493bd691d011e6a5aef6b59b0ed39cfa5364ad48962c3bc2172d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b98fb169a4b36f83e66096be8f543159304bfa938f3a87cdb34ad8c593d45b60
MD5 d74b2cb53402da62ff26b6eda098ad41
BLAKE2b-256 a37852f0d043eabb5ab7f17d8f59da2bcab7e6ab3cf82476c97fdedc3faadf96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 747e96c8f24306675df9a06464df95f95ff553d8bb6b28f6aa625779954c153d
MD5 e31a81e29e01cc8ca81cc01be0171988
BLAKE2b-256 dde00b660ca891646263f068f32be6c7858f692022fd68999ba35525d93d505c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a043854a820310910552ff4e5fb948c528a12156c403b55e248c8bc5e6e92f2b
MD5 bf4d54f91c471e2b0c72787ce26a1b53
BLAKE2b-256 34b4e97dcccf0a6ec7ad724440bfcc3a05fe9c7236264b652133f493084d7cac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.13-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2d21887c600c94d66864ebc946d6e186a109bbd1313362d2164e9b33276bc9f0
MD5 ffc85cb1e08ddf4b2922f4f632fd606c
BLAKE2b-256 2e0862b7e0a536d58d6397b7fbe63d1bbb17b0be8dcf8b2cc0dcd5089b65e7b7

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