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

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.2 kB view details)

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

codegreen-0.4.2-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.2-cp313-cp313-macosx_11_0_arm64.whl (305.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.2-cp312-cp312-win_amd64.whl (326.7 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.2 kB view details)

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

codegreen-0.4.2-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.2-cp312-cp312-macosx_11_0_arm64.whl (305.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.2-cp311-cp311-win_amd64.whl (326.7 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.2 kB view details)

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

codegreen-0.4.2-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.2-cp311-cp311-macosx_11_0_arm64.whl (305.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.2-cp310-cp310-win_amd64.whl (326.7 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.2 kB view details)

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

codegreen-0.4.2-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.2-cp310-cp310-macosx_11_0_arm64.whl (305.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.2 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.2.tar.gz.

File metadata

  • Download URL: codegreen-0.4.2.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.2.tar.gz
Algorithm Hash digest
SHA256 4d1ee1a15569faadc90b4d8fa8b4508cde17bafe216a92d996fd0de086e3a34f
MD5 39657f96d4772badb2f74321c70dba93
BLAKE2b-256 bc10539d712b5e2d94a8c8264ac894d17f6397ec272875de8e54c199e178a3a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 326.7 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bd9c279c201a8fb0e3d988e657ab25a5e1f693344a55e5f1217fc796994f9f14
MD5 487fefc2b5bb3858b4a5d9da3e242e8b
BLAKE2b-256 61c6f1ea54dc62625079ddfece7d0f41ecee3c463d46883eb3440fdd0b4febe3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bd395b48bd8824c56154e05041c61d9491ca4bf6e6bd4ddfbb49b512338d1f58
MD5 787cc62204d94f6751c7a2d7424911f2
BLAKE2b-256 f2bd6c4b261f26cf6975756d1a378dc462098170cdf274f4c81cb87a1a6328ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 16e9cffdbf839e67793530dcb2375783e37e591ef02497533d8c9cce60e83e04
MD5 d1fd6ec3384263cd9616ca72fa9395cc
BLAKE2b-256 ffd8e9a9d5827906c9d8f67542e52d116069dc3ee40736a670d006a74c427d58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33227642e289aacdd8aa843cb230d09fc0065c1baf4e54d787bb5b5ce62e8ae2
MD5 c70b44795fe71be4c220440c24ff8e10
BLAKE2b-256 b61e447d425efd93275dd3a3c4f3103f433ee2153b095ee592ca2088f9c8b6a4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 326.7 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4947e218557bc2c9743a3c075f3a0df226c8fe3b9d1263bbe5f55afc83245ffe
MD5 548cf3ba8a0f8d98e63dac42e3f0e304
BLAKE2b-256 8b2ff5248e06419653e23cb0bf2c2fee81d3c4db384f08fe8d394f12d9555d39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 814a42a04f153822d4a2a8581ff9afd2ea2ca6ac2307f0d9899b8bd190d05c6b
MD5 2adc35c061fb8002076804889c95b7df
BLAKE2b-256 2988458116a44f057b673a2db122cdb0e660662c2c3fd8f5f863268d714bd3e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ea025e2c8487403d57d4e35867ea3bc58ebaa30f13b27d5a954e4cededb9b3ec
MD5 4f71f6c8c9679dda8f24c3aa61389738
BLAKE2b-256 3b77034aafe185c119cf29971d5712d3e21d0fec349a898097be7e62202b74e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b9e47e459136a61dda9c2d2ff6cd14b9f286b9a3741edd6b971232dd7e2c3bfe
MD5 62609851eda668d487f6c34d94b1e569
BLAKE2b-256 28a34da31b53a781bfa11dea2ebd102ae6c2d5f8304df0f3541c75f19d534dd7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 326.7 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e10ab95243a94ba375992a4156fa08d01db0c24f3abae0253948dd173864f999
MD5 631e88b069dd8bdc37d21c621ffc7344
BLAKE2b-256 21919347016eb45a3c4ffb0146fb772c704e9ed47b0ebe50f0809765d69a9651

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eaff371cb75b0ea3bccc28a976db6313d6bac62ed17ae286ce36f5cd9b066d33
MD5 c2e3f0b791d7843b20768a9f7fb4437f
BLAKE2b-256 39c05bbdc2eb85345be6d6722ad8295059edb43242ba80367ae76fc75bb45353

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 539798cf9fb607cdcea031d6795c5d022c2b092e14616b8a76c72def1aad1132
MD5 3b50912ac20f38381a863c1bbd6b1679
BLAKE2b-256 65335345b5c8cc67367ecf42574bd9c59b7a1a35834795fb9781d1b11e2a0f1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37b6092278e840460fb73e94b29b532b21ec70cdc8f2d64e45500aab81766888
MD5 d422706a4e22a3a7f993bcd348f33b2f
BLAKE2b-256 6d955fb1bc29b27edc3e957c85b04f9337b8c517cb64ebac8b749a5004ef15d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 326.7 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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1f606dd0ccd23682bccde44b2ae0324ae4e91f4e2cb9fe904d0be704aba2ff2a
MD5 b487c79a9005c5be30e39faec2bce7b0
BLAKE2b-256 db64e31b9b5fa4a60e4bf9dc3004c812d55bbb9a8a58ee4a6471b98bf2a27b68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 419d79513c30febb4c8bf24304940e5cc066a300d3f9c0eb96856329693de616
MD5 607b4acf3d652713415b4b192d47815e
BLAKE2b-256 7bc4ec6c567fcaa64fb71892c9dac6957b8d9bd4b3e4cab0a6feafbbfbc70b5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3b039756172d8448497a550359bc88071719499c0abd4565d6d3147db998fbf7
MD5 5726ae5082c0943db9c447f77637c826
BLAKE2b-256 612029d6ed97abb6766f074796f9ba077bb5bdd2a111b3b5628cc105a84671e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4e30b5a7b015aaaea96dbeb4156a36b00abd5a6452421bd472eec1102b8897b8
MD5 3f276a1cf7c930df317cc166c7168cb5
BLAKE2b-256 08e967bbe75a9a3b985dd9b2544a58340fe8157ea63ee2623968c3b92ca70e7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5b0905ba105da98fef06367e0600cb8e4374771f6899c5df8eabb7b5d40a3e26
MD5 e7206b530c09676c374a9fedf091737e
BLAKE2b-256 6ab05d2b6d788ac08030b6fbbd811441ef539018c9fd9ddb824c2b92e97d7e50

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