Skip to main content

Know the true energy cost of your code

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

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

Manual measurement from Python (Session API)

For span-based measurement of arbitrary code regions, import codegreen.Session directly — no CLI, no AST instrumentation:

import codegreen

with codegreen.Session("training-run") as s:
    with s.task("data_load"):
        load_data()
    with s.task("train"):
        model.fit(...)
    with s.task("eval"):
        score = model.evaluate(...)
# writes codegreen_<pid>.json with per-task energy + per-domain breakdown

Time-series sampling for power-vs-time plots (area under curve = energy):

with codegreen.Session("infer", record_time_series=True) as s:
    with s.task("batch1"):
        run_inference()
    s.export_plot("infer.html")   # interactive Plotly chart

The Session API uses the same NEMB backend as the CLI (RAPL/NVML/IOReport), supports nested + concurrent tasks, and degrades gracefully if the native library is unavailable. See API → Python for the full reference.

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.4.7.tar.gz (2.0 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.4.7-cp313-cp313-win_amd64.whl (336.0 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.7-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (511.7 kB view details)

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

codegreen-0.4.7-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.3 kB view details)

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

codegreen-0.4.7-cp313-cp313-macosx_11_0_arm64.whl (315.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.7-cp312-cp312-win_amd64.whl (336.0 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.7-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (511.7 kB view details)

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

codegreen-0.4.7-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.3 kB view details)

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

codegreen-0.4.7-cp312-cp312-macosx_11_0_arm64.whl (315.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.7-cp311-cp311-win_amd64.whl (336.0 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.7-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (511.7 kB view details)

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

codegreen-0.4.7-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.3 kB view details)

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

codegreen-0.4.7-cp311-cp311-macosx_11_0_arm64.whl (315.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.7-cp310-cp310-win_amd64.whl (336.0 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.7-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (511.7 kB view details)

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

codegreen-0.4.7-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.3 kB view details)

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

codegreen-0.4.7-cp310-cp310-macosx_11_0_arm64.whl (315.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.7-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (511.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.4.7.tar.gz.

File metadata

  • Download URL: codegreen-0.4.7.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.7.tar.gz
Algorithm Hash digest
SHA256 45b64b5e9237e27f2ba45725032f61431b21593d1ddf383a0c401d7f28269e45
MD5 655dced397c9a672bc95bb700f55e8d8
BLAKE2b-256 3f95821ea948bfd08e3390265c335244b73b2ed694929ebd49b81c75c325b40e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.7-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 336.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dd8b87bfa8effbd7e686019b57da895793aed4b25111b443f48740c56140e5c3
MD5 d1fef24dcc473ab7d9f7d915e7c26859
BLAKE2b-256 cf6aa66907ab0e8463512ea7c86831946cd8b8da616ff7f025576eef8487489e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 200056383f8f8765ae6b40e9766e8e0ffb1696b5adcb016c0b7d950842fc56c1
MD5 84f074a936b2af8ddba8e3b518f717c2
BLAKE2b-256 b3a212855949896e6473ffbd1d17eb4c865ef9504e156590f159455c35becbda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 70a8974e05cd4e7ac905e3a117a6ff8f7b0be6d35095307eb56bd292d2bb0014
MD5 d0f9550444b3cb4c280828d83cccc113
BLAKE2b-256 7fbdb4764f81b3c7fd0e9d590873557a72312b913876a839cafb74b82b656652

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b583e00b14c8792bb1e27795a1c5e6f0e02ad4c5bd7bb970844a6c5cc8b4c9a1
MD5 c43a86443c41d7d3e42caf4e7d23a232
BLAKE2b-256 2c5e816510511e815f49063fac6417149d02258f480b41a76353648d0bb3c67c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 336.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1eb1908ac44d8e0283ca61cf44aa73b3e5cdc0ff74f3c2668e7771de735245da
MD5 552300908ca9f653d274d557af118a8f
BLAKE2b-256 0afa5f31c4ae893394c3b55a444f16e7f82948adc47ec5860f087b242e2e70fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7f89d2ebcba45edb4687d43b1683f1dc4e9a06255368d6f729d34346a709da17
MD5 7e173ec18553ccc8669e5b5ea59ea22c
BLAKE2b-256 73f170a7f827a04e05e4632e99f7c55dedb355a18e4b17ba0db5dea4df888751

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 499dbd93983dcc2e4d9e2bfb3e1e189bce66f1cf03ab880556877fb04b396fe6
MD5 e3bb4987a65ea5f65109c8ce715b852a
BLAKE2b-256 6a45edf8a804e6c89abc9205fa7eb20b67cf94ad4a07a6998962284819dc9333

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86b4d2efb56ee5126ff295a4fb5670e0c9716b51cd9f20013c6ddd7fa2abb0da
MD5 e162fb67a238595c8ce57ea3b5616daf
BLAKE2b-256 105e4514d6668dd22677a358ba37afa11414cdab274eb2a2dc7e63e3a440c162

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 336.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2b7a2b7910b28f282b3dca98b28cb7cdb9a239f75ff074b1f67b4f85b8649ac5
MD5 69774f0b2ee24c0ad189a4396dc53330
BLAKE2b-256 c2ef141a60fbfe67b732bc3a63e40d0fc328da32572e3c62b46b2733f5964eb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f61a7f24a53fa6796fc783cf606ff1fa603d229088fd400db7944501026f26f9
MD5 03039ef396f227cec0cdc12a6fb59a3f
BLAKE2b-256 3759ae5e77c39b1bdcb4a33804199611d38529670f84be9a0727119ab8fa9f83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 acc20d1137f14f1b99850ac8ed4e144c9a989d07ddcc40be29917250314898aa
MD5 e73f29df0f18c2a67e90b19cc1998ac7
BLAKE2b-256 429cc8903c8c159daa06b7ecf10ec8be0cbe9059967e6b5e51d50d1359f89afd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d08fbd5e2626772fd1ad1c58ce4f827c1feb73a900913e6b18748c4b78d56ef0
MD5 100a68837af1a6a2a018948bfacc554e
BLAKE2b-256 6990f44f0a3c93bc27fbdcb811206aa2ecd70107e4bde13e905c418352deee41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 336.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d2d0231e3c7a0b4f8e7d1705c876f2a97992991af3a58e7ba6e1a822f050fbe0
MD5 7db39a238e4830c980c4cd78479a1d9d
BLAKE2b-256 46948eeb2f903f1da54260c990471f9b4515a9bd64e55e0772ad8c0c190f120f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1acd284964811f6bd5abc8911f0d9fa284e604565b00f1882e184ca22ad403d8
MD5 fe7fb3a7e2d37a61426cb4be0fa9d181
BLAKE2b-256 9b07db53bd8de7e9802bf3217faa311265ac103c5f67849c779945054602623f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b81df78fb43d6d30277b3251c898a1690e61dd0fca3fe7408fa4677b18eadbd3
MD5 e982791bc7e10250b0149d020479dde9
BLAKE2b-256 00c072ccb7528e9e2e94f68e4ee5fbe19006205421eb5432418eb002b968ecc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 28b16cc7ee756eb06ab502a3468ea0476d9760d2fbb97a46186e3306759f947e
MD5 ec84b4638450c0893e454674e59b8078
BLAKE2b-256 2dbf615f3d4ec4fb36f043e6d30b3fd32821245ea62525ec8bf4a884069c4187

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.7-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d99d73b7cc1a7b8b0d8a12b715ab234e8b915294e620c441cd8684871515d234
MD5 66210af2df53fd4674d343f3c183eed7
BLAKE2b-256 1073e1d9e07fc1082b15ffc81d01308e0d981f6da1247a27a0515cecf28686ac

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