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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.11-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.6 kB view details)

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

codegreen-0.3.11-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.8 kB view details)

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

codegreen-0.3.11-cp313-cp313-macosx_11_0_arm64.whl (278.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.11-cp312-cp312-win_amd64.whl (299.8 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.11-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.6 kB view details)

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

codegreen-0.3.11-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.8 kB view details)

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

codegreen-0.3.11-cp312-cp312-macosx_11_0_arm64.whl (278.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.11-cp311-cp311-win_amd64.whl (299.8 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.11-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.6 kB view details)

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

codegreen-0.3.11-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.8 kB view details)

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

codegreen-0.3.11-cp311-cp311-macosx_11_0_arm64.whl (278.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.11-cp310-cp310-win_amd64.whl (299.8 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.11-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.6 kB view details)

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

codegreen-0.3.11-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.8 kB view details)

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

codegreen-0.3.11-cp310-cp310-macosx_11_0_arm64.whl (278.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.11-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.6 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.11.tar.gz.

File metadata

  • Download URL: codegreen-0.3.11.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.11.tar.gz
Algorithm Hash digest
SHA256 065e60d32c7945f77ca296aeee692889d3a30eaac2fa162c76c364c6adec1285
MD5 6a5d90a8e5b2e4b89325b44fbb7c19c1
BLAKE2b-256 c9a5d2ddd866695569af6d7b0a9f7961bb3a3e8e68d0dc01c6deb59e1e8de58c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.11-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 299.8 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.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e61340665ea08851867a378e23d41bba0714d847699c554175c5dcd1efb02b0c
MD5 629644e79acada8fc3c9f0f0a68e562f
BLAKE2b-256 946e1d8866ca9b4f988ca36e0d0ce3de6ea2ccaa8743ec398aa32380ab478e38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d30ad3e2f5b8306339431a0399947f45bd9d3f575b800f04965555f18799c29a
MD5 6dc3fd6f439035df7157ffd52e3133b8
BLAKE2b-256 a2e43f286bca8842a4ac4c5131c7d26677c529104f7a6e062532883154f86b44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5139a3bb22e929eeb818953b9a4a47f6907a479f94259c4d4a8efad6abb57f37
MD5 28fc6d0c1f7ab1f988b952293ea33207
BLAKE2b-256 100b4c6b1ad15cf1288ec722a5a39e8fa7b090c28055d55bcad67b2a61990764

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ca6556fddc526e379bacf0f335bfecbe028d7d40e7e58b0d5f4e9fba0b29b49
MD5 1f608621545943f2cca384d1d29b068a
BLAKE2b-256 3a5b155c78154928fc5e2106113560af32a276346faa645a14e70c3a51c8a82a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.11-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 299.8 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.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8eb9b4275b64fc36acfcfd6b79d615d0c6b337ffbcbbd430b1b6c58c148795da
MD5 84506bf114690b6d6542b3d7072bba22
BLAKE2b-256 6782e59db885eb755a2a1ff9d6533dce248e4fe9bdfbb4fac7696f73faf3e4db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 00c16d4d4d67204d354e8b86194cf013583579b6bfe2d41c1181dad650247d20
MD5 3d15e3903b2996c6beef38fb29cfac24
BLAKE2b-256 79552e87516a8f3ddb4b5d18e8d504e70e09081bfc35ef73aa262e03dab80564

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 482b90d4385ec350efbb1a0f4c446d67b9eeef30143a4b994e72615cdc14ccca
MD5 4ba5081374d411ff5a9d678504e9ef31
BLAKE2b-256 bcb5607582fdfba2752a42dfb27b92d215a32c17fb86984d209c91161f66583b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc25fe3456c940ddb8d3fc2fbcc33a16e3377aa55598cdbe05cf01673f8eccb1
MD5 7a7af36517c6c3759afb47910d89798b
BLAKE2b-256 f804e947d56d0f377107b7f9e4ea6bbb3aa8361997a71357830fdf35d401f732

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.11-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 299.8 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.11-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a2150d3715a50dad499a6ae494598a2e859302482aaf254b5d8e65ae0e71fbbc
MD5 a52dc8615fc4e92b364f8ab41c2e2baa
BLAKE2b-256 05e228ad1d3ea6a1db4da2b0837d3297498b6fa48eb4ce2fdd73a6e7a5498a65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 deb1142c7a32a9b0493dc797f60b6ccfc0d9631bcefc22391252db5f1ce103d8
MD5 e144e4ce18d2fbc45e8d8c1513156b62
BLAKE2b-256 a6620d149ed699db546a0aae0f510b9b36e45d240dd9e24f467494841fdf1cd4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d16bf5827aceff52841572fe5b98568d0aedf611652aa1cde15f94266370774a
MD5 7730f4e1e94dadf0583449e8dfc74b9e
BLAKE2b-256 77a61d2e077e1b444c4a373d3d7e5496735ae5f725da514643829b6416c97e65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2740fe8fe1abc335e70db310104361c27d0a5f03010d070d0e51271c7f5112b9
MD5 916d49cf0f572ae2e8b5b4d27804409c
BLAKE2b-256 3056f7c7f048f8b95771dee5dd2bef0cdd80c540d36eebcd177070b896a614c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.11-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 299.8 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.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a6a82792cd35c0eab1f0f5cdf5008b8909fb01e853383f04c2b770b2f93540c3
MD5 73699a48527592cca10e357e7c2c7d63
BLAKE2b-256 7a09c413bf64f6bcce474027f629a2410d541bec024bea07415ae85cbe78f464

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 46c70c81746bf6ddc23210dff68c02001726e90993e67ad083281959bfab4369
MD5 39a9c3f43f735308fce398a3eb7a8d22
BLAKE2b-256 c24b35d18097ff683e49f40e53723e271ae84a27ad4023a85b28d93591f8fb5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6fd3129809a14cddef30c01a4d84fd3305ad134095a61cf391d16cdadbb70cc8
MD5 3103cf899800e37adf185669cd6dd5e3
BLAKE2b-256 2ce285940194ecbdaa920367e1e1ede57e64d3ecf735b8fd32d5edffe638c13c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 775d0abe41fa2046b9d65a41ce4ec370cda67980ded753dcf10c8ca56cf32d7f
MD5 e35af7fad471604d8fa17589d48b4f7d
BLAKE2b-256 c83327c3b129a412e5cd27a31082b04df9d14230524801c5dfd2487f9a9a0552

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.11-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 aef3140ec1f22349533743ce3024af0d5c3eddc9f394116e9a8d0560ef59ecca
MD5 9c1538556b15e728f57339820fc56c23
BLAKE2b-256 b862259fb49d4fd1dfcaad5e643e583b768b892ecf314f3f7308d9a0e4b61154

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