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: energy measurement requires sudo (IOReport access):
sudo codegreen run -- python script.py

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, sudo for energy measurement
  • 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

@software{Rajput_CodeGreen_2026,
  author = {Rajput, Saurabhsingh},
  doi = {10.5281/zenodo.18371772},
  title = {{CodeGreen: Per-Function Energy Measurement for Multi-Language Software}},
  url = {https://smart-dal.github.io/codegreen/},
  version = {v0.2.0},
  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.3.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.3-cp313-cp313-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.3-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.3-cp313-cp313-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.3-cp312-cp312-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.3-cp312-cp312-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.3-cp311-cp311-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.3-cp311-cp311-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.3-cp310-cp310-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.3-cp310-cp310-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 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.3.tar.gz.

File metadata

  • Download URL: codegreen-0.3.3.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.3.tar.gz
Algorithm Hash digest
SHA256 b53d47f7e09bcbf47f3085754e8f6a14c15e591ff1c194d109426fa602b913e3
MD5 607f0620799f78b06aaa23f6d25a8386
BLAKE2b-256 d362408c92f57c431c2f4ce524f4fa6536b7c9a66d62619d00709a2c1276dc32

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1b652ae180954504ad40f09eb76045ef7f5469ef43c5938c35c82297a50d2e0a
MD5 9f222156a90fd3e1f46dee7e04b00363
BLAKE2b-256 13e86297fbc3bd3460e82ab532ea26f74b8fc5766b62d9894a997d2b51d119a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a9fe80ef1e75e1397b01a1d1bc49c7b8a707feb387e0143feb8cc491b0773c03
MD5 03dbbc4537ddbc634831a668c8cbecd5
BLAKE2b-256 f4f727035757f3b5196a8cee94e381c046ab7cfaf30cd13d20a6002d369d0596

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 61d672f2ba094822578675a99e77aa641851d3bbb3304353f35aef3ca1dfff1e
MD5 819f0dc1583994eb394ebb4a840b8247
BLAKE2b-256 ba3ba026749fa89743b42784f621f2d528854f28619798ab7eda97f9baa07a20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 750d1452b303766f2887d7ccb6b53efc360d6c233a8d5f89d677f0eac2f1012d
MD5 ec2f19ebd96649d3882cad769d00e826
BLAKE2b-256 5f7351b5c03c940d1dd571d04a0dede1772216c421af85e64a40498cab29daba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c8a618ac4852cb64a77a05a5575b25f700566ca5e7a94b66a0958e028397748c
MD5 e7efba00413b225b305ad7ab3722f892
BLAKE2b-256 51d1a0d2fcda0b426b1117189870eefa7d5d60ee01be49d486581d23b0ed0797

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 05f3a222e9af95fa1d2d792634dda01d1ab8ae7e3aec2f5d0fdea721709e447f
MD5 0810807a50e957e903d790ffb65b6fff
BLAKE2b-256 e034f2d520e0bd04b6529e0fd1da71627bdce2c56979b32eb819e913224c1fc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9066727883857db5e1f264476483b311eb1933d0665a1b94d25eaa383dcb737f
MD5 8538fb990f7472b132fff4e26ea9d1ae
BLAKE2b-256 5e75b8720038a975fc0a64d96a9ed133bd5c3df95b00a889eaa316c09c1bf66c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a7af71f31a3d080a8db9c2c882465aa42caa130b6aa06ee8b473995877281fc6
MD5 e23f3255063b017af30fc01859328baa
BLAKE2b-256 da61fe2c2359d4cfa1e1ed6a680e4620d74fb908eac670786ed6b6c1eb88061d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a30b9b2faf6f242232f7358bcdf41bf5d071cdbf345860e649556ac84f35ab06
MD5 3d4122784449757a94f9f95eb42f9061
BLAKE2b-256 1793c139979236253e5b4b018af3912d915f72a145119359dd8be1edfc6ff8fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 605b1fdec98123118ad3c5ae57a2ba406fb69a72b5dc13922d905758850d76e5
MD5 9da8b154ddd1bd8c20a3724cf60dbdac
BLAKE2b-256 01a384a844e6b60978e8b42a0fc0707732ebab3e408c3543840a037f1c58b4a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ae44295cf26493bbc05709e6160391d995adb1c8a5dbebe4e1288c0eb706e42a
MD5 761a1cb8104138d2303f16165c959287
BLAKE2b-256 9fc2690c2016834e6b2a2336ed16c2f3f22f97b6c4563d47b83ed20f40110964

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 699d1bb5a3e2431c636a0959570d1a06191bab62a19a6da94c8aaff8d32f514e
MD5 94207ebc8ce3ac121b6b036a58b1d615
BLAKE2b-256 ddc0804a1bb9d632d10178391000ba2943f269e96131aab4644c06c133f43667

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 084ed7a225c7f622590bdf1a9cc92744faab5cc0ad19a01bcba16c3d02b169e8
MD5 e64e33ce1c9420709a971d52712f41f5
BLAKE2b-256 40684dea0efa84350bf39ac5442dd789b78befbdc6433ea85bd3a2e52553add5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f7d9ea41dfb3ea372758965705c40fa6221c8b83018d143e46914911357a82f7
MD5 486ccc159a868773d69547da47749517
BLAKE2b-256 a7b908590c611c1e4444f07b5bb3d0ed759d96da1d39df0a70ed1816e6957473

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c2ee3312b2076a171615d94b1587fe3e49a02f3c5ead3497d2bab84f0784739e
MD5 4d2961d6916db26b8e59c9a00e921bd6
BLAKE2b-256 1b8f85ab93033a9cbe816ca61fed4957710010ec21543481e1512555721e5855

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fe49ffd8f8374e4b952b46ee1ccd1d2a9662f121eca6874a0b7b31c1b6ed0d6
MD5 6c230c0d66da933d0b2996d2ff4a8f2e
BLAKE2b-256 82134afc741459996262c2c035bd5ad7735bccabcb88676014f350ff508f5d21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2025a80e97abecaf6e3b51bd5de8414a4df994c55e4a57ce8154f0bb74067dbd
MD5 7efe240f57140715dfc0b699f303f10e
BLAKE2b-256 66a4703254bcbf94b41bf33e5c048bf859b0fc1e67dbe8fb13db058d7d298d42

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