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

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.1-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.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.8 kB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.1-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.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.8 kB view details)

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

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.1-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.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.8 kB view details)

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

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.1-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.1-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.8 kB view details)

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

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

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.1-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.1.tar.gz.

File metadata

  • Download URL: codegreen-0.4.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e4e1464a5d993fdff1236e569ea9ab69e6924e470a3137e1f847185e4f9b9f3c
MD5 2c106d025150de1e3eafa499dcb569c9
BLAKE2b-256 296776c950c89f0c380c4485bf324f9332e27c94b1de5f3b8f05b6568a18b106

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.1-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.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3c22165798bc6a112c50bb962ccb489151488f68078892a9d75e9bb57e90992d
MD5 a004cc78848daddad7aea6198bc15842
BLAKE2b-256 e7efbbfcefb96ec8d2238b8d637e53b1a1e339dc7cd626b08dbe30c6445c4346

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b4672ec9420bb8d731053817ddc7c3ad0e0578c23df61acd24f2d246e6a9d9ef
MD5 a5c0f7f34ed2521a8eb231240b07ab46
BLAKE2b-256 7a415411ab16bfc12fa60417ff86d6d946e745eac8e690b48f565b18e5bfda02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 661ac4b4212b50ea1b622e33d18c0ac293a1e534ec1db4ab6e7db6f07c54d826
MD5 c6ba3c7daf09fd446402a08324bd00e9
BLAKE2b-256 4f917860bbefea346d8fac7280d01e4f89ab5019cd4cd2109e36814b8e4bf01d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb7bd47a69415af3eae5f12032d9502fd0d5864aae1c6428076a14896550d11f
MD5 2ef8ca6955cbd5f409b2a4f5a154fd9f
BLAKE2b-256 acf236f1f0e3b28cd855c6226feaa5ecb28901781ef624bb1bb10c321e964d94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.1-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.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ef2eb337ce78e0c8785b9ce3c57d3c7082d198ff2cf81629b56afaa8a95e1d30
MD5 4002a9adc65d083748a53562cf3e7ec3
BLAKE2b-256 748fe5bf2f2683f794e48b5c074636e2ba9c72cab75f507a6c0a1692e31598bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ef75d8e698d42e6c738cc09ff46a1e1d66a5b7d6fcc1c037bc2e4bb17ac821ea
MD5 98a7285a72d7936938097f0aa9fce5e2
BLAKE2b-256 842d3ae85b5fad8dea81017fcf0b50702181d74cfb6c77ded4de50fd043de4b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ec39d47604f5797c982a97a787d837e3cb07fbf1fd0d4d7a5b66b19257fed5cf
MD5 9a58c9948f21a9941427eace0234e7e1
BLAKE2b-256 8abf5cdc90034af76866f0e2a288395304649f88c49c98a0cdec98e547fca82d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ee97244fc9e9b048447f7bc9ceb779c68c2c4c3c0a064bce575949c3b5af7a67
MD5 077203e328ff377f55597d9e39c77eab
BLAKE2b-256 861248ea12590cbb796de080e3b20cbabc65d0ba4ac372839089f0a3847133f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.1-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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 49143a2b7caf7877c8d91452fe1f56832d39ed66f27f7bd49f4713d02f4df621
MD5 123c1191c73609a87071292b1f0644df
BLAKE2b-256 a3b779abe400d9f03f83eb8d3df89a58bbf9aed91752e79a6f62150468af6967

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 05b66b9a423c268ec0cd81ba33c747f0517b1aa732865ee90e0aa602f1f901e4
MD5 303a3afe347a24bacebf10f8ebfe1a43
BLAKE2b-256 9eade60ab402178d108ad9eaa5ae81c27d7ba1f101916c49e3e5aaec6fa6259d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ec5c33e263e21ca99f051e06362f982e12d9fcf2f531ceb1c65c46856fb5d210
MD5 e0a81b6c8d5d8e9eb1b9301022935670
BLAKE2b-256 875d598763f4aac221f1e3e4070160b1e772fd86d7bdb622a4ff7eac10137323

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f15d8a588397da385ff1f94a6521475d1bb141422e209951fe30a63fdc837cb0
MD5 241f9d7fe71f0ea8eb39c9843f47b484
BLAKE2b-256 25570e7049229f03cba07a25dbbbb8ceff277d70db5f7c82c2983c90a9e9019d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.1-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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c196e9a8262abf2a1c4eddc3aca0d6fffe69e6119fabe9efc42dcd74cc30a9e9
MD5 6ad9601bb0a37444f0a82d2319cd10cc
BLAKE2b-256 b57d75850394c4222216efa904aa9effa772d612ff8b91bb44010519172538da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 caff57b6ce76d79eb86d77fe21d35436793e98b7f553f76af165222fb4e8e3a8
MD5 9e362fa7dba6304ff4ca8d98d1bdb8d1
BLAKE2b-256 6b407d6c85215a1f1aa5221d1db40986892c814988713d9491ea93ebfe58d54b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2d44c4e9c984f3efe569a3e13545eca7054684725fde440a00cbfe3e1effa4fb
MD5 a4c8a0e67db8e853081e08afe44a20d7
BLAKE2b-256 450e3c5e82015560ad1d4beb66fa30c9e02bb167fb5ccfc01d2ea9b299096b8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 02664aa4a115778a3a4c3c7412f123b354b2c3c110a7d5e94032e37160bae29f
MD5 696de81ca68674cabbc4c840dd579336
BLAKE2b-256 6dae5ab0ce9ef9ec823a1249f2f86aac4c93bb58fcbc6d035869303a460bfd4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 285f918f89e3468b384b8d60f06ad345fe0555fbd9f0026bca00ddb37c5fd059
MD5 d21d2b9a75bb58b91118f3811f91658b
BLAKE2b-256 31b2b9d3b915bee4e4b33ba53c36999d950fa13c581c288977d718a0873c1792

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