Skip to main content

Know the true energy cost of your code -- hardware-level measurement via RAPL, NVML, IOReport, EMI

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 (like hyperfine, but for energy)

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

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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.15-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (479.6 kB view details)

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

codegreen-0.3.15-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (450.0 kB view details)

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

codegreen-0.3.15-cp313-cp313-macosx_11_0_arm64.whl (283.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.15-cp312-cp312-win_amd64.whl (303.8 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.15-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (479.6 kB view details)

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

codegreen-0.3.15-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (450.0 kB view details)

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

codegreen-0.3.15-cp312-cp312-macosx_11_0_arm64.whl (283.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.15-cp311-cp311-win_amd64.whl (303.8 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.15-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (479.6 kB view details)

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

codegreen-0.3.15-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (450.0 kB view details)

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

codegreen-0.3.15-cp311-cp311-macosx_11_0_arm64.whl (283.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.15-cp310-cp310-win_amd64.whl (303.8 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.15-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (479.6 kB view details)

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

codegreen-0.3.15-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (450.0 kB view details)

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

codegreen-0.3.15-cp310-cp310-macosx_11_0_arm64.whl (283.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.15-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (479.6 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.3.15.tar.gz.

File metadata

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

File hashes

Hashes for codegreen-0.3.15.tar.gz
Algorithm Hash digest
SHA256 eb1ecb4661845522100162666ed3f4a2f8dc65ce505e9a220f593831f59ed1dc
MD5 aceb0b00a6b44baee578f912326136e5
BLAKE2b-256 bbcc28c3b77224a80d347c86d5a51572254e66fabb8ed04800450bca9bca5f55

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for codegreen-0.3.15-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2f69881cfa2c9c6b06f5fecc11bab518e8580417c00cecd6431a2d598acba669
MD5 47ae6af081168acd2d5d88d877987b58
BLAKE2b-256 3cd6cfed85954b44a8ff98be02ffc8cc781e608da99a280e661ddb3febd7d62d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 723efb5fc53f7a771f13cc00ae3ae8953bfc03e2261c978e4ec546f161392d34
MD5 ff80d8ecdf11ef7c23fe55af20b9f678
BLAKE2b-256 b83a258bd6eb0353f78e62e7da91fb4ea85dc86f540a79a1649503efd47e8512

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 86f2906bfd5117435c16f85dde6eaee112c9c228112f646062b844ddfb41ac6f
MD5 6c44064a7792c86e5b4cb33afa22e217
BLAKE2b-256 88493b2478439211dd17e794894b2ec65672452c70c7459006217b4b8e30fa15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87c5038276d2f8c982e24416314bd83b86aed5271343db349fee24f3ebe2d07e
MD5 b9cfb0705ad9a49ea421fa6f72ad7631
BLAKE2b-256 98ca32485c27771672085e4d2b25d3fc1d43e1f5cc48d8fd50644c9932d2ca3f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for codegreen-0.3.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8c28d3f17498cb514adc6e843c41dc07339dcdd21c059de91c25a4da70c6818d
MD5 138208f2a4aef0418cabf68e97655f71
BLAKE2b-256 008c938166179c0abf08a9bc6bebb682aa802e34b705d91a9a575978ed6d4a54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a639c8976c48e1e38fa79bc84b45d386339f7e2d909d83d8ee90b2d11718dc05
MD5 2247aaef580a5faf9ee35bf7990e3bb2
BLAKE2b-256 8744c42a341e733c04f56b62c20e05f6afdaa5aa644cc6a6bb8668430517e72c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7bccb4b7a003b8b1fdf42c8e1eb3e6b9a5acc69ee2979e47133528f7fb304935
MD5 7fc843371d2a41030006396224f242a7
BLAKE2b-256 1dfbd220a72d6314519e85a8ce7edac106ae9738ee7ce1d827e1065ed7b26b1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37a0958286073d04ad8046891452ee90c244734a273e460d7c8e7bdfc210eace
MD5 0c92083366b09501105bcff25424678a
BLAKE2b-256 d1b695ead162e7a9f1317c027b6249b0f54a9272ab8fc6e87a62e6692ad82ba1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for codegreen-0.3.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3c2408c0beaf35c576bfc3767fadec3f1d57ac402ddb408b8bd6f5f671ddf3a6
MD5 b27e2230394ef340fd086107ca056f06
BLAKE2b-256 5333a5870961b2b28978ca4028360d99d6340415fb6a28a22a1e12768fde4820

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 822ab876a31c5e44bbaf20a6bbf7fc6b5fb5b1829b1cb0202707beed503c0332
MD5 a767a0d75c48c807a19e1df98959c2ae
BLAKE2b-256 ab2016c7032cb20ac0649ea05514e78e419453ac58dfed59545c901c1f4f1568

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 86422b2a11a1db5a14db3f4551e7fa126f57181fd0cfd43272d032c042a13ea0
MD5 c9d6296de0926ceddb7818ea06b40f14
BLAKE2b-256 845459f09ab173dca834f6049a82960acf666cbfbdab1ba78d57cf89ce2cc273

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3ea36c506d202b9d660a429fa91df7bd45361ef5be4b27bedbeff9e926610d2c
MD5 bf4f5193dc1f3c70216e9fe63f061a64
BLAKE2b-256 d545b7cf06c1c6879873db93afee4241c85ec0151a99939d262c111ec3afb9f8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for codegreen-0.3.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dbf035a269083b67885c4537a6fca426de21802f22542daa7f940e247ba141e4
MD5 9ddbfe5ef694ceb8814d10730ec8ee85
BLAKE2b-256 68d29bd3656cdefa4b287359fdec47a0f0eb0c3de06683e156dd67205ecec546

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 81958aa0b944e582e3fb5b82f98fac027f50c801a7c47183ad5215b135512305
MD5 2b0eeb128c08771c054572d816322c81
BLAKE2b-256 0fd2f51def4bce08565537ba166291ceb10b47832b8db7b535c853f10582440d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d4582197a54c3898a2025cf7eea6aae129148c57032a4b255c61af4187342633
MD5 1f059a854a478455b4705bcaeda6ab20
BLAKE2b-256 592d8ccab9f2f683278e2873c3b13c6ee3e1e905d7d837d0415028a5886a565e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 51e00aa097b2be263364925824f53c5e13ed72fb81346e6910138929aea9cba5
MD5 c5eb11c82f94db996c816f9f9bc46c51
BLAKE2b-256 351771f921fe45fe7f7d28a2b0fb665c272ffc505b4d7bde085ca9796eaa036b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.15-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 74f4d15cfb85a0fab4edf87f79dd2a795855f3f4724d4e69f74b37951648bd11
MD5 c702d3e11792d94d7d1d4becc65791a0
BLAKE2b-256 5d2ded7ccd83a481ced7c52ffd912c15e741d6ad99ddce26e46d8fa0962c1ac0

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