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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.7 kB view details)

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

codegreen-0.3.12-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.12-cp313-cp313-macosx_11_0_arm64.whl (278.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.12-cp312-cp312-win_amd64.whl (299.9 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.7 kB view details)

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

codegreen-0.3.12-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.9 kB view details)

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

codegreen-0.3.12-cp312-cp312-macosx_11_0_arm64.whl (278.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.12-cp311-cp311-win_amd64.whl (299.9 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.12-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.7 kB view details)

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

codegreen-0.3.12-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.9 kB view details)

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

codegreen-0.3.12-cp311-cp311-macosx_11_0_arm64.whl (278.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.12-cp310-cp310-win_amd64.whl (299.9 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.12-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.7 kB view details)

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

codegreen-0.3.12-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.9 kB view details)

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

codegreen-0.3.12-cp310-cp310-macosx_11_0_arm64.whl (278.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.12-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (474.7 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.12.tar.gz.

File metadata

  • Download URL: codegreen-0.3.12.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.12.tar.gz
Algorithm Hash digest
SHA256 b7c3f008b3deb164499cebd4a739a7b1c2f84feaafb3f62ca37bba94ad14de31
MD5 bac6ce52af59f0d1893d9d6a5a6e6ebd
BLAKE2b-256 1b9dc8cdb5a7a6d601b6710ab119b3dab92b99fc062df82c33fefac3056a5bf2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.12-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 299.9 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.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ed7785c2124d826a45050195113b6746f51ed8f0fd8ab691ec703b8b4c65b595
MD5 e95afeedbce8df98b2fdcc6ae8ab65af
BLAKE2b-256 306552081725d617bddea106a07bb11e46fb5cd9e7230c1e1be06f55f5284dbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 32f71858aa30a0e5201848cfcb272a02c9342632f95309ca6d42cb1013df8aa4
MD5 1af0ab74fd65495a232504c364b51c0f
BLAKE2b-256 1bf97ef02ae867cbb572bce43587b7bc1821046a95cd029d1ecc2f9c67a74062

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b1111c009a60ca1e30be93832727a2374ffdebc4df3810609cfb0335d29f62a0
MD5 0c9e293746a84277bd03c2b27a619031
BLAKE2b-256 991c921d06c1f86b2b6acf4a71991172e526c6bf7c62e60bfd98e2e8889e21a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 887c59bd955e35395a71594938558d86c243d10cfbd6adfbfcecafe41e8669a4
MD5 69b968d53e50fa5b8e4d540d765bebe2
BLAKE2b-256 d751ff7354264ee69342a32e7391075970f5fafa152a9a2ce2e86131e51c8103

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.12-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 299.9 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.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f98e8aaf3e53a4f7bccd74a29513c442fca71b1793067e855ff42ccf21533d90
MD5 bfad1513e6646d87d54aff801816000c
BLAKE2b-256 b5ff87fa0911163f6b3e1a1f5eb0c504a70fcfc9d9f0a9c3b10702171e07eccc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 43d4038ee599f4c1435f0e0c813c8b21b1d307a8b3a1c5ef935353a459241ba2
MD5 d410b217e8c7710be0b99cee3ab70cc5
BLAKE2b-256 96881cfecad4d19c8dd336e02fafad756e4f60bd7fbc5d1b5838624e3bf215ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9cce1b57b99c41b83077827529c4ecea3beafdea25e3071bd06bcc8764911811
MD5 e57a9594e88ac519465f8c857361ca0e
BLAKE2b-256 416822aeb9eee3183816e1412a2b9c8259644194640217a12f560d1d0e51f3b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b998b2e032f9a31b20ab38792c0fde2447c68f934098f97177a5d08b4daae5c
MD5 5ee7adc05153091dd55e0feae92298f5
BLAKE2b-256 438bac79e8c25d566f39409ebdaf6816ddd0f21372712b7b7bddb9ce16d5731f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.12-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 299.9 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.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 268b5c04bb178560fad097aeebfe6e5e78efe249ffd321cbdea2797131ea3d72
MD5 4744824813f45833d5a635dcd1293f2a
BLAKE2b-256 6ddb8772094a3ae046fdacaf7310da0c1d3fa2f15b4f7a195536e83bd093b092

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b172297daaaae46aa9446b1f47c2232cdec83d910f751bc5b94befd197b62472
MD5 55aedfc3cddaaeb347950a652ed4bc0e
BLAKE2b-256 354b74c1abd696b0c6246864a1a12c4a1438a46eab08e7e4d4fe6d68ae02228c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ecd3e11d8bbc7541d7d1c375c88f2779226df260bbb36d5d21ab6ab21c4202ad
MD5 50c344fed8a43be5eb3763dfd71e3245
BLAKE2b-256 3f5eca2d384986862d4dabe7d73e3016ac35164da2579a1fe645ae9e037b4f3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a4bb15356a1e219c1c3b2f7d10821f1f068c2f1a6bc19ac7372ce6b8fcc78c6
MD5 8b5b3b79d9cf3cc741719208db347de6
BLAKE2b-256 d4aac0cdf947f207e5e19b002a0291b396482577d85816afdcd1f6350c6b6604

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.12-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 299.9 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.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5fc9e85944c7ba2b1a7951e00dc2c234c6d862607522b30e731d5f2bb41f3e90
MD5 47e84c93b810151145aa1c009aee06e3
BLAKE2b-256 cdf36a7cc25a3c5770bef25e9f534ac1f4be650a4d38ec1d505d1372f4d7ab83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a74d630b45c5048b54b1d648f16f470ceef6ccc2fadd87276a724d4c24b22efb
MD5 59bac5000995019ad576596b9043249b
BLAKE2b-256 f69e890031eb90933f0e76f5807ebf309f3e34fb71e8d67432acf9fbb5e37477

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7065b89a1fb3e29dc323cd6c27bcb9e80755e93f54c183c36c11fbd0cb910018
MD5 8573cab425b2e83dac64254d4e112cb3
BLAKE2b-256 f81f41a3132011f2d29c43e68c47decc8df79992fb51cfd8034e8fbde3620985

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72d8f5774c3ec6f0b287de7b6718d9680e4becad4f1aa4bcfdcd45e515ab0513
MD5 da60042a03e63fb48bfe639dbc1899fa
BLAKE2b-256 9a3dddbaea733f0df570ae693056584f85412e7720ea6d5f92020e783bce2ed5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.12-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2ec5d25a90ea9046f64ea957e124d2e2d9de1de37a5cb414d49ebd369af8adc4
MD5 0a5756c954b60f0ad67dcee65ff35257
BLAKE2b-256 083c224ecb1967291ca7717751f61cc0a9136d31966650875cd7a425b276bf35

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