Skip to main content

Know the true energy cost of your code -- hardware-level measurement via RAPL, NVML, IOReport, EMI

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++, Java. JavaScript config exists but is not yet exposed via CLI.

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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.16-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (482.9 kB view details)

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

codegreen-0.3.16-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (453.7 kB view details)

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

codegreen-0.3.16-cp313-cp313-macosx_11_0_arm64.whl (288.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.16-cp312-cp312-win_amd64.whl (309.2 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.16-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (482.9 kB view details)

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

codegreen-0.3.16-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (453.7 kB view details)

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

codegreen-0.3.16-cp312-cp312-macosx_11_0_arm64.whl (288.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.16-cp311-cp311-win_amd64.whl (309.2 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.16-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (482.9 kB view details)

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

codegreen-0.3.16-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (453.7 kB view details)

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

codegreen-0.3.16-cp311-cp311-macosx_11_0_arm64.whl (288.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.16-cp310-cp310-win_amd64.whl (309.2 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.16-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (482.9 kB view details)

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

codegreen-0.3.16-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (453.7 kB view details)

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

codegreen-0.3.16-cp310-cp310-macosx_11_0_arm64.whl (288.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.16-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (482.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.16.tar.gz.

File metadata

  • Download URL: codegreen-0.3.16.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.16.tar.gz
Algorithm Hash digest
SHA256 cee2e05ab3308877f31447ec31d11d42966112a5dea00682017d854544b84ea2
MD5 5da7fc518ee979bad81fee9a141cd4f7
BLAKE2b-256 ee95397f0663ef3d1df94c689fcb22ecb7ff3df10c3e9e1f07208bf9ee144724

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.16-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 309.2 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.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3a66c03dae7f1de55c3addf02530d410dea3606b8ae897eac6c0a9aeab99c242
MD5 ae68b5b5616ba39b5e63cad2e1db65b8
BLAKE2b-256 e5943ac9a43b2dffe480357b680b8789573058d01ea6b430f120ab119e5f69a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5fed0344f5697fad7eb58911813d53ff9d385b4c64f93f0a6d4b11498def5fa7
MD5 8c82a871b2f344d8c57e8edf13da9809
BLAKE2b-256 fd23e3d0d59becb9dc7eee92a68d78132a9e20b7dc2922197a082152ac631b3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 813eba68b7f78f236ea2a28543c6a446eab5489565e73c5717adea997d479625
MD5 1c034a34484955140dddd93ceea86c27
BLAKE2b-256 6d7885f3372761787f489c920523d4a81be2ac8ddc38c0822c5e4cd4bfca320a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e22c3e33fb2261636e6007253e12f2ee1f2b922bcff5f9117a76d7fbb562e4b4
MD5 8ed7c337d410f7a1bb38c3bf1a8a86de
BLAKE2b-256 d1724feaee65a420ac227d2b1245e642f2466766533d534f22afdd9e30146d1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.16-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 309.2 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.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fad1732adf1ed72b16866516a3d99ed34ebf212c811af5f9e7aa6a29804f0fa5
MD5 0c52597618610896a0e980b44b5a146d
BLAKE2b-256 c1b72a3c2ce2ec08e350308112bd96d1a1696362d2c013108da6eb3eecad7ef9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 14675b80ebb59e9abb9169d55d9053ca867691c4ca1f0c96f84cc063e128bcb5
MD5 80a62dbc3f39a7283a4e695489156bb9
BLAKE2b-256 80cda02a35b086b6e66a646a1cab6d893f033fa037c9c07cf98b5b7c413f1800

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bc272a13c8562a8dd8b1dff6723dbd740ddf0f7bb1c6d4b6958f16af564fce46
MD5 2e717dc8c1933848ef6718882ffc8ce1
BLAKE2b-256 66acfd54d56a9e76368576b2296f2bac98f12899a37858eb7fd4a02f4647bfc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e482d88054c0e0a4e90780e4812669e409d5892360e12ff991617d9abc3eefc3
MD5 3f93e68684aa151f196f907b710e778f
BLAKE2b-256 215ba74035fd12bdf91c43100cbf483486ac5eab24d6dae2c41cb8b16fbdf1f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.16-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 309.2 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.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d361f2974f17d1349b967e0995fce681efbdbe4496135e99ca73cfeeb3c2d963
MD5 6f3a9087aef049854f2d6d6a60ef3086
BLAKE2b-256 55d55d53b2bc053acecf5604ae921a3650875d0bd92577a2e78b3e7232595ecd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 258ed4a10a5d780e47ca35da689f2dca41bc7d895666c28e1e063cde1f4a9f05
MD5 955c3276f01021a25eec98c3a86da52a
BLAKE2b-256 cd4acd595ceed09cd3ecc7184b9bfaa64a4bfcf39e11794cd6e6b0e2f81fcc95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 33008ed0d122a81180a1230c0b43deee35e9a39112cb134f7ba686e863d864c9
MD5 99f0d316889f65889d3619c0dde9eca7
BLAKE2b-256 87e64a6a57a3195e69e36f039021a861eb32fe61f601391591836193134235d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c4f85f5c0b9fc18c1c367b0895d02468b502518f571cacc2a984f54efd3445b0
MD5 998568075c049a96948c944399788126
BLAKE2b-256 d83158683526fe6f2776447cbd5cc3c05d73cee3152ca86df8b0164276b7c6cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.16-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 309.2 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.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 711ce259d2b7af0fb6a9b4f39f333ae1b591b9e75faa9c09d9083f3eabe66793
MD5 28f9cfd179cd0a6532c06d346aa363d5
BLAKE2b-256 6dd2a8efab01424b8493856693b73e7b56d22f13af733aa423fa965a8448268a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f2be9bf3becc1df803624744f8c1fdd288de9015c0ef3812cc73019881e4ed7e
MD5 d2cb527766132196c39e1e1975113a2f
BLAKE2b-256 1416e795051a96006e71269e953bebdb0ea979fbecacdb7a290989744f1d6deb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 aef9d0e89fe0917ef2b440febdb3b18bc66f765abbfe253e818d22ec72e94767
MD5 032309edff008a9151d51812834d0ca3
BLAKE2b-256 ce76abad94174f50924bd54afeae7edb47fa554f9ffc58d693cff7d49c2e3abe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 426bdd2bc206afa3c21e7dab749c4be3399c7443ed25778a1d43e843ef971831
MD5 b4cd5fcd13ae5b1740e2713116032c98
BLAKE2b-256 fa27136925b411e5fa6716efdbefe95b99bfa06b9b2ba7f01e37f47b000a8570

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.16-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 53fa175808229b8dc49ee831be22eeff4107c951f91b638cbbd97b138b7587be
MD5 82bb58096c8135c2d883d65c1209e16d
BLAKE2b-256 8aaa29123a4fe6fbcccfd05c86decbb6460665fdffab8c87b8a67c664aee84d9

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