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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.9-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.6 kB view details)

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

codegreen-0.3.9-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.9 kB view details)

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

codegreen-0.3.9-cp313-cp313-macosx_11_0_arm64.whl (277.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.9-cp312-cp312-win_amd64.whl (298.8 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.9-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.6 kB view details)

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

codegreen-0.3.9-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.9 kB view details)

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

codegreen-0.3.9-cp312-cp312-macosx_11_0_arm64.whl (277.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.9-cp311-cp311-win_amd64.whl (298.8 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.9-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.6 kB view details)

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

codegreen-0.3.9-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.9 kB view details)

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

codegreen-0.3.9-cp311-cp311-macosx_11_0_arm64.whl (277.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.9-cp310-cp310-win_amd64.whl (298.9 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.9-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.6 kB view details)

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

codegreen-0.3.9-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (444.9 kB view details)

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

codegreen-0.3.9-cp310-cp310-macosx_11_0_arm64.whl (277.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.9-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.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.9.tar.gz.

File metadata

  • Download URL: codegreen-0.3.9.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.9.tar.gz
Algorithm Hash digest
SHA256 7385c05df8207d27f0c15fd446a3f5c4d248fef69d8662b922f723fbe56bce0b
MD5 55b487d8ff1fbadd712cc6e510ce9c95
BLAKE2b-256 3f70a5769f70b0b23e0718965e643bd7aef1e2af58b272c80bafbee913b9c331

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.9-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 298.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.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 941f2307d539a45c6006b216e2cadd5437610e20fa7ca782673a38293c05ae21
MD5 5c669e4230dd0bedfc795636e355388a
BLAKE2b-256 17c6737cd978fd2df8454a9f3db200a83207f4cb20ea38d398847f09aa34b8f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8cdfd9a8ad682390bcab2f9cb9c7b7afc5085b5aee1517cef459a50e43262014
MD5 5c14f2ba3a382bd316f66f4e48f67a7a
BLAKE2b-256 2c8ec8fbf5539f52d668898191c395ee52f785f5290deac9fffb0e73c06c8e57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 370dea99c6e51d32bd93113b9b7d564b4ad75e023b84dca2ebd3cd0cb9cf0051
MD5 5587fb087d65def2ce59006eb909d81a
BLAKE2b-256 9e66d1f9095b7573ea746bc5917a1899a905b9a7652645d260c567d09342c0a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c6f38edc016c77ede3b1f6b53ebef5036dbc169769e983e9d46a77fdb9f1850b
MD5 6e9822979fcbb3b9dfb25bc390d0970a
BLAKE2b-256 9dc3dcbdc1f238af85c09f4265665fcf534f75505440f88e0e9fc7cc5a5fb568

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.9-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 298.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.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 48e9b19b72b672edc1935813dab1b9b9ef7b165b1886f40c6d4087e2b17d2a27
MD5 ef12d594c8248d6a2bfbbbe2efbbf484
BLAKE2b-256 6fc14e33fa9d747ea7251e132deeda094d7f2a31330e36c560a8934c84d6a054

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ed2278d095d6e95aff3a79d40a8b455b02a1ddfc57917872849063e556c1634e
MD5 23aec9783446a737387de59351906a16
BLAKE2b-256 069ce277ffd0040f0c199d8df8ace0edaece4b408bd26670d86758cf2a6fe9bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fd701414f83b4427d2b32a59be7195a2caeb1574d5f391271237dc5c7c304bc7
MD5 47295494adc69684208989d1c952b463
BLAKE2b-256 ecf1f1fc3bbd658931955c0fc00fd5a7b44e4445071ed0279a0d8010f24daa45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0942b089a53ebb003d6676052f513004808c2ba5ec98423449b9124ec1e22e36
MD5 8779f346dbd76c2a62116b5374b044a8
BLAKE2b-256 5c354d13f07a2832360115a02ddcb06d277d91125b8383c559797ef6e8bed46f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.9-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 298.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.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 992475c40e0d671e32afbcd60e512baad2c0e95a1c70263452b96695836fe89c
MD5 5c0a6ec6f6e3204238a2e3b87a5623bd
BLAKE2b-256 dd5ae623d2322232d83b568b2a07ae0c9d60255e10552eb1f4bc3a5af364b4cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ebe65db2f386be443f03521fbc28cc34a2ff7915d08641db85491e78e3f07e35
MD5 6a913542392339db37c525e084a6949c
BLAKE2b-256 d47bc836f68c0881f3c76a56fe4e2254e86131b7b48f0186699ba9cdc763fd08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b4ea48aff1f568619a676221d2c0c2467ce365d1d74705aa5747adbe9b8d5641
MD5 d89aa6e0d449b58bc2c14e892c289a1b
BLAKE2b-256 168543096ab7bc7bdebaa010c839e0bba1a1ca1e43dcb75f68a82635d477c36f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a59108449fc22be6772db047f9dd9c92d7c84c53b4d18c8ee085b3c64b45f86
MD5 1651823a0e257b7bb3b6dbb618f6d97e
BLAKE2b-256 c380bb32cc38be3eb569d0cf1c5f48c4863e0dceaef995111bd0c0b00fb50022

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.9-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 298.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.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dbf6d9e2ad898d14e5221c5285f0fd9338f38d3bf273b8d44cb7ff1b2e8f68c4
MD5 ccfcb1464fd125a6ff1ca77d8bcb65fa
BLAKE2b-256 fbeffaf3f54a451932df46737e08f3c3fa40fcf068be113c27b53c8afe44b091

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 828aff3f9b6969bdaf0047160222c99ff165b651746d8002b72e561af6b0dc2b
MD5 baa002b283f9d4eb325b6621b8a3cfb3
BLAKE2b-256 e16ec822f8900685728adf5b2c6bc8fc71019080010cf83dc4020cec8bbc230b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b3f988fcbeb966eacb77c52460456ae4949ec8ef3e7dcf0688fa55108feada29
MD5 0206c04c28ecfb0d33d6f1c65482e720
BLAKE2b-256 39150273005e7587b132c5f93cd8968d3f5d08326bb9c65bd60e140bf23d8d3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 45bf419c44c0a38b240e1187325398c1bdb1a2df9c54403904e954a631f3b28a
MD5 12e32b1f7f9da2e30969bca7f51a26c1
BLAKE2b-256 e16d60b3f277875edc5f102b518ed497ae4b04039ea611faaed051dec0b4bd36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.9-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ecd2b19030ff00414de36f41dd921b2cb1b067eef72f4f862870a74ff1a012e6
MD5 48201bccc7a2f8c979833c981d28669e
BLAKE2b-256 e297c4a4ffba9721647d0347f326309b7ffa438177a162fd4b1d8f409f7066a8

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