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

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (504.1 kB view details)

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

codegreen-0.4.4-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (474.7 kB view details)

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

codegreen-0.4.4-cp313-cp313-macosx_11_0_arm64.whl (307.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.4-cp312-cp312-win_amd64.whl (328.6 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (504.1 kB view details)

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

codegreen-0.4.4-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (474.7 kB view details)

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

codegreen-0.4.4-cp312-cp312-macosx_11_0_arm64.whl (307.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.4-cp311-cp311-win_amd64.whl (328.6 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (504.1 kB view details)

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

codegreen-0.4.4-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (474.7 kB view details)

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

codegreen-0.4.4-cp311-cp311-macosx_11_0_arm64.whl (307.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.4-cp310-cp310-win_amd64.whl (328.6 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.4-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (504.1 kB view details)

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

codegreen-0.4.4-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (474.8 kB view details)

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

codegreen-0.4.4-cp310-cp310-macosx_11_0_arm64.whl (307.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.4-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (504.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.4.tar.gz.

File metadata

  • Download URL: codegreen-0.4.4.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.4.tar.gz
Algorithm Hash digest
SHA256 1a19dc01547cb7f3737ef0d6689ed0cb86ba26c6372197f877d58bf568a7d3d0
MD5 504f7eafeee91ffff1318a8278bf4e36
BLAKE2b-256 168c10ead7229720223fccad88b21aee0fcad963406a1e4e2354bc167a515e61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 328.6 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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c8476413cdeed3f8668b4e6bad9eb6ab9b837b377c085c7cf4b66cc9551c85da
MD5 7756395a4f80ffa5c9a3a7af7eb7a93f
BLAKE2b-256 9c5ef17e3a770cf8f71fb5eff7050d683b93dfc10ead5e7b5e2ce684671ca567

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ac50ee30cebb0647a1cd8abde0d66b1f7d76ded73c52ccf5d5f212482a35c105
MD5 c8eb6a2bbd2a920dbcaabc0ccd14e444
BLAKE2b-256 3674de80601c8943d7dd0645790f6a47210050bd0bf15415c66933554332a4b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2054b902064721cef74c97c1a1551ee8b2b21c9b215ee15870f608238b828175
MD5 41d242542047297cc2535d63d64a81de
BLAKE2b-256 6d066259daabcdc237f29244dcf2145ea7354f767cdd60b7921fc0e7d1c38b75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 40856af4876f0aa420ce89415704b29c4c294ee9f10617f6da9be58729b1af70
MD5 1291b33b81e5aca1d3eaa04e3ad55706
BLAKE2b-256 5e7d4f6c8b935d0b5e2aeb67d8615c530ca8d8765df5c6c6e90ddc832de3f541

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 328.6 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 beab7504e8c3bf88018b3ad129d8d87cd3648e338241b07727fd0f26cab83b36
MD5 d90b1e7bbe554703c40ecc4725f540de
BLAKE2b-256 cbd0c82840e5b75a095244cb503d39f683b63121265f7556fcf7adaa1b379623

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4815e08fbfccfaff0c94d360ba36734b2f5a5c6cd205c6c894c065613dfe9781
MD5 ce4e06688c3cb8f0c96290a2b52283e4
BLAKE2b-256 3aadc06bae6664e08f68e208174d84419c67305a4b8938e4812be17a1ab1cedd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 06310faa22aab35c769193d501e4776c2505e93b4148b5d0fa10a7f0d55adc04
MD5 a311a0f5c53bcfc3f63efc0d50135803
BLAKE2b-256 2dbc00fd6d2580032b9407ae3a7adc541ea25efb4ab08b89b7dfdd0a6b9ffac6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e91129071f9ed68dfd9560c3cb740d9bc8adf9892ad10b1b2f5abdcbd318dea
MD5 ad3f18c3caa648b5acf580f2e83ea463
BLAKE2b-256 19b17503c68c30b8e1585ebab20028f36bd4793d73e57551d62d97e35bd86f99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 328.6 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5fc722c71909a55a7de603b004e636471b64609cfee02220061fa567fc6afb8c
MD5 d512d4665b8cae637025ef055111adce
BLAKE2b-256 30c82470725f86018a49fb4d3532319694b12a3eade818f69efc3db7277692c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6f8c2c45e8256a24c47efb0abb3f7986dac944542806440d00378ed876785c8a
MD5 602447f870d7e488a0159e5c2c082087
BLAKE2b-256 70b7ec842ecb514f4130f9e159674731dc2b3c926807d97b3b466d2c47c71f27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2a807efa894138e29143fac857ced7e0a1cdbfd233a33a41c6ca284786c926bd
MD5 16a11a9f4cd3530e179cf5bbc42f9bd6
BLAKE2b-256 3881f0ef1442c4b892a520fc42697da8804be31af13a3080aa348e026cffccbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0ca2f8d394f6e3ab18d16a9c7effa30c8600104a1929e6fbc66c77d9dd5492ff
MD5 62763b042a9d31a04fe24efbb902c1f5
BLAKE2b-256 a8aa8a62319638a80e0535faecbff1c0a640f260c2b72636402c8b8c025481a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 328.6 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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f375681e2aae4a2f0d2b9b117291b590a69ed85185b668dfe92788d78fe4d99f
MD5 e4e8a41fc6401e0e01319dc03a7dfdb3
BLAKE2b-256 709ed68a1f2b56cc5e38b235966948c20a2980125ff14d686f4c88d130f79850

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8571466041dcb1446028371122585b6886c43df1de98fb9401eeb334fd13ffd5
MD5 53ccdee5e2d3d4be93ac36fe8ce4a350
BLAKE2b-256 8d1c217d00133feec1179f6b2f1d22fa6bec793eb3cd04f8325f4bc66a246c1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4604e9076f298ab55854149071e92ee1014b25cef0a353c0b1ad8d8a9c14015e
MD5 aafc9e052b8e8e969a6e0fc275d24353
BLAKE2b-256 5a58e20a818fc0d3db693fb7fe0e375b197fbc6fa71c12eef34d8c873c932d2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 99898cdb0460046e2ccc2c9a4893ca98fb9f184e4c3d8ec720a5a5464b1ff7a6
MD5 d5549f108ea4cb935a8e320491788e73
BLAKE2b-256 2fe4f599b658a3cf07bd1f4db8d916c873b156f83a8d6ef70e5c5d61d537779c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.4-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 64eaea988dda801c22f424c58aa99a2147819da5b47a510d60be3761635290f6
MD5 19ec5bcd151c8a9a64c2f24f5c188b4e
BLAKE2b-256 7f20b5b7b73d84bbea7ff4514b1f756ec11eca4afb2f66d244fe7a12135301ac

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