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.6.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.4.6-cp313-cp313-win_amd64.whl (329.4 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (505.1 kB view details)

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

codegreen-0.4.6-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (475.7 kB view details)

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

codegreen-0.4.6-cp313-cp313-macosx_11_0_arm64.whl (308.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.6-cp312-cp312-win_amd64.whl (329.4 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (505.1 kB view details)

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

codegreen-0.4.6-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (475.7 kB view details)

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

codegreen-0.4.6-cp312-cp312-macosx_11_0_arm64.whl (308.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.6-cp311-cp311-win_amd64.whl (329.4 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (505.1 kB view details)

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

codegreen-0.4.6-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (475.7 kB view details)

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

codegreen-0.4.6-cp311-cp311-macosx_11_0_arm64.whl (308.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.6-cp310-cp310-win_amd64.whl (329.4 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (505.1 kB view details)

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

codegreen-0.4.6-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (475.7 kB view details)

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

codegreen-0.4.6-cp310-cp310-macosx_11_0_arm64.whl (308.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.6-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (505.1 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.6.tar.gz.

File metadata

  • Download URL: codegreen-0.4.6.tar.gz
  • Upload date:
  • Size: 1.9 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.6.tar.gz
Algorithm Hash digest
SHA256 28c9b32ac3501ee0f5840a7cfbdc350d618d89411ee9a401a5b61b131a6ce7bf
MD5 0282758d5ad77cf0b075fa744add4c4a
BLAKE2b-256 5e771a27d085f5ad1278a4546d301ff91d53fafdf851085e2899b66f37fcd8dd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 329.4 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.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 78a261893ea4ef7f0aa0e31076fc1645f07cb96ee733d0f83e8fc22c5935a339
MD5 9884f0f7ff83245570a11aa94dc8fa8d
BLAKE2b-256 1ab058ec1507d564aad79eda87e1aa6e9750d83fb3c9abb06f176c73d59c9f65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f8df69efcb19d4800e93ae23637e8a47cc78cb750e53e978454217be1275816b
MD5 c45afbd3cccd6093107c520cc4144096
BLAKE2b-256 684ab93cdfd9b6e5af5de71ee9fd818a633681b2640c6449b669db91391c5c5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ea995fa6eb2c4d74c5bbc2f732920c0e6f81f7e1488519df0b14911d7e64a476
MD5 430bf24002dc59bf4caf264dd25b2c9d
BLAKE2b-256 066c49716f652e6471b50a7f5b6fe6c7c2d946c509954bea9b1573a2618d4645

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e5b72436500ffd1ecfd9c18a938862449e7c89bba85609701f9c2320c4a24ab
MD5 16e23bf861681362bda2e1b7e6e56ee9
BLAKE2b-256 2e07a7527ff4665ce99bc7e85f80cf482f3c2b637b517f076a97255f52d6386c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 329.4 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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f0400800239d09f94645e997c26a7f51566f3210f619536c0ca61fc87b66aa94
MD5 62a7e5e382cd41561f33116467fd3795
BLAKE2b-256 41457fd1d60cd0f24735e86be0fa27021ff24d870adde88668f1967cf5981fd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 74ef4cb5877bc9c0c0da45426125b8b7fcf3390e2a545a6135c619407380e168
MD5 5f91e1fafd4c6bbd27f86cdf9c074563
BLAKE2b-256 b7e4b2246836b06d02f078c35c077b887d0ebb1759213ec4f2bc21e5ab46d82d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 42a2d700ba96c288359cab2a139ac9a6c0c367d5a0e32f1021768310e5f898e7
MD5 6bdc381b2db66e1eb5b775da9c1578b3
BLAKE2b-256 88ea930c0f44119c07428100f779da45dbb3c4613bd9a4f158235ab215ae9251

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ec904baa28b2424bb9130d7dd8a7ee98f0b05c8ae3029665554e2d21cabb9354
MD5 a7a4cf2fd4e3b75954261599900cfcc0
BLAKE2b-256 4902cddbddcead2fdb728fce51bf75d64da03717e4164bf485074f3655165e5d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 329.4 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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 474444c2cbf437425e728003f1da61aedf9a919c8f94f89fe5612053e8a2e182
MD5 e4d38acf1f8d912be566c9785c42c1ee
BLAKE2b-256 04efc1c1b6c712db7e806e1a86131c7b9bf65ccb08c5a81d95ca91a31017e328

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2812f0cc6fc75b14d4d5bd10102f2c1e38c4424ad99f67cfabbe244cc73086e5
MD5 e627d6b405a8f8b4cb8ac8ac4e78e87e
BLAKE2b-256 ed545d8a0a0571ac7efdc30aa7cc056cfc44738f7aa238388297c29f0d6c8b5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 98d4d9ec62a1d2c813c70c23025cec5a3032f239e4e4db9501e4f20c461c5c45
MD5 ff9dbb68847d415c60743d9d92100e65
BLAKE2b-256 a279f5b275cdedb065159f2e8e4c8819f1767d4cb8ccbda86e7c85465fcd045d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2df16476abff9f91cc315120f501f1830ca0ee5bfa820718e2810e0bfb477e9c
MD5 625a68980f674238b71458790de2182e
BLAKE2b-256 db4cd4c200fb1decc56cc6d66d62940889f358cc5294f21f25a381ee48363a0a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.6-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 329.4 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.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 309f54a3e05738fddfc09c98fd7914c838c0ea12467214cf42e44ee297afe5bc
MD5 9d949859288268989a6d5b2de49e7241
BLAKE2b-256 6fc48e3d96eb4e8d8e069886b5f443e167e513d899adff89ab329028d4f6072f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2c1219437806efd918dbbe167b74ccf2d003856aabeaf7008eff3121d7694991
MD5 58d07af658ae994ecdb47223fdb6e7ec
BLAKE2b-256 b2de1e73ca28e489bb729e3a9a62c665d1e63601aafc63300ccfcf7a0e4a2a2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e49224820bc47a017b8b3b0fde5a4a031bc4f7078ad55c56ad685212a4c1d49b
MD5 ef835f84c0724b83486d63eb412d6767
BLAKE2b-256 8d383ab213e05993c9552c78fc09bafa6b2b9447e2151bfb00e53e932319e869

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0289b072b78fcc59361df512b835a46b1eb5199e0085b7d0760b9dfd776a5fe7
MD5 8c9ea41d6dc9f66e64d9395582dc231b
BLAKE2b-256 7116efbcfee87b9287a8bb3b33cefbfc3f89486132d24f03326d1b21690fc7ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.6-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 78ada4a7bb2fd000e42a953860dff5589a01019e1c5f8200fb973112c090ac77
MD5 cc338ce2c97926dc80b5d46569ea102b
BLAKE2b-256 8927f454865680968ec50ae8218b5ac39798b5abf77d5cefe541fb680b997cd0

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