Skip to main content

Energy-aware software development tool for measuring and optimizing code energy consumption

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: energy measurement requires sudo (IOReport access):
sudo codegreen run -- python script.py

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, sudo for energy measurement
  • 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++, JavaScript, Java.

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

@software{Rajput_CodeGreen_2026,
  author = {Rajput, Saurabhsingh},
  doi = {10.5281/zenodo.18371772},
  title = {{CodeGreen: Per-Function Energy Measurement for Multi-Language Software}},
  url = {https://smart-dal.github.io/codegreen/},
  version = {v0.2.0},
  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.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.3.2-cp313-cp313-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.2-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.2-cp313-cp313-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.2-cp312-cp312-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.2-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.2-cp312-cp312-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.2-cp311-cp311-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.2-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.2-cp311-cp311-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.2-cp310-cp310-win_amd64.whl (252.4 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.4 kB view details)

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

codegreen-0.3.2-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.3 kB view details)

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

codegreen-0.3.2-cp310-cp310-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

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

File metadata

  • Download URL: codegreen-0.3.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.7

File hashes

Hashes for codegreen-0.3.2.tar.gz
Algorithm Hash digest
SHA256 3ef380c9fe0c697d22f2dd00ccf981fe4eb1f257bd07f6396bf29b3fc5f5b5eb
MD5 f6859009d3048b7d6485a83244438997
BLAKE2b-256 f0cef34f3e66f3063addf31573d42c992c64493641399b249ff905ebb1cf906c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 623b50a2e15692e40fdcb67776dc133d4ea554ff83ab5b94e9d9300071f547b2
MD5 7ea29fa4c0ed64a33988be3aef3bf4c3
BLAKE2b-256 150541bbc2955c7a948b9ea32d164d79fec532d12304bf7412ec08bc2c19d637

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9aeae2367afdf0981142134b26bf67096117c237b202433d7c554686dea3ae78
MD5 5f1a6108a8d43c60262f90d845bbcf6a
BLAKE2b-256 0c7a50fefef0baf31de181a467d24ea3367c6a167495dcdf5c596983323b7c74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bc05c026db09007f46722df8f8e8ba681cfffdce982e0135b7ca285687e30fd8
MD5 4e62b3456024916c838662a4d7e86958
BLAKE2b-256 f7a9c9a4449985f0d4c6bb4035c77ffe6331a5f00bde44c92555a70ef40168e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d1efaf1e877a76f8a91f8631671686abdbddeda50dd52dac24c903b8518f562
MD5 43de1241f3df1fa97c4a2244641f47f1
BLAKE2b-256 1dddbc7054ec1275e35bfeb1ecdfd5e5785052b07117838684e6319b070e1de4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d753706e10e5a5a604e8194dcd8839e7ffaed3bade0d2ccbf23f907ed177c00a
MD5 41e4a15737491ec1d3fc1c0fc5641109
BLAKE2b-256 95a1424d2b04ce6c0af288c8d1f055357d93fef9160632f0ea35dc1149fe0e82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4936b264c9c5bfe216fe742c586c5e8face85fe02c848181f8390b3c58ab6941
MD5 552c50e9efa1103f67aaf12051588d73
BLAKE2b-256 32657ebd1c245614750df848024020c70c62eea8bae5b80c89280509dc3498fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3bf0b566c255c779ad3b1a87024e7cf721e9422e78f946853d18d0c7d2c327ba
MD5 3153e7039c0aebeb453a4d0953390b91
BLAKE2b-256 a59dc0a89d50f98cd70e1d3cc12a2dac4c8a7c4ea39e93c5cd408b56034ccfb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 020d2aceedcbb28d198cbca6ad2d019166fbbf6759cfc8bad3043cc3953c861b
MD5 5b9a6bcc9885d9dcc431c4972c42b68a
BLAKE2b-256 0057732fa4599bef2bdc8dddd17b2374a7bea2ce1f69d7a9cbdf983a9e69dff4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 740a9f25f899e287fbb18dffe155f64a069547ee5a1fe9bd92308319b43d7632
MD5 b4609e8da0ac801cc3c55f6c877c5625
BLAKE2b-256 fcd0c74da6ffcbf66ee6864155babdb43c8cab93fc5f54755c67a83ed8614109

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f042a07a361c644416fa33d04af4af2034df40ef90f903222bed6d71f945388b
MD5 d51a7a706c81f6bd4af9ff3b900ee03b
BLAKE2b-256 1baf0b5648693b1ab5dcf8d5b070bcd3ba2d1b5d54e21290e755e927e7761536

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7ea81fe066cd228811fc135379539cc88f552ec4c3214372044d0e118fa4e68a
MD5 7ce578b625742b0406b151a2de0bb048
BLAKE2b-256 97f301837523a4e9fa72efcfc1c1c98c6bcd3942daf57b23850889c4bc7c9148

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1598ebf517f2123e35a57dae9f07e92088de2c5c73f93518697118eec746e218
MD5 60141c3e2632c7c9e82c6bf5680bdca4
BLAKE2b-256 52e627021f7fd689db73fc99b49e6e1b45352a60969c1a8824d4090facf29274

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 252.4 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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2c13d312baccf8b43c254f0a549effcf0d9283f802de66a0c15d575ea00f193f
MD5 e73239e327d2c7e65337acf25c2b7338
BLAKE2b-256 fe2441b36bfa34a1ef50e7ec261ce57865b020ebb0cd126abd2f1b5d40b864cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 21e428f21162f5077a87ad35ee1b87f7f6c49ecd0ee0cd9ed7bcc31e954a7ee5
MD5 6ddac155632d9aa2f9d662862e271d56
BLAKE2b-256 3f6e40289ea266fd41ea4ebc51a6bada92fb01bf94742b4fa035cae3a7610f89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ae753796ec0d92e498173c4431d387f3afb60d373c542db14b00126bef4b695a
MD5 afc5357f545db74de663ecf9a8ff8452
BLAKE2b-256 07ef84351242e235200067f885b1f97d980a245a35ecb3670711f19329088cdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f477a5dccdd61604a7b50e0d64611f59fcbae5e85f6fc0646eeee0c78a545bf1
MD5 1caa57c651d2f26b0bb65010f7e8013f
BLAKE2b-256 f7dfb4cf5805f882e96a9c56ac093653e4aebb04f99ebf0b512e19e4f0afb7b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a947cb57c0070e4bbe01f5c55c4e02f6a73902e5b1aefe053a8583c313622371
MD5 f95f99e213bc0ef10e3db8dc7cf59bc5
BLAKE2b-256 6d438b1d05d440f727e28a5cd64639606fe9e2c05f91e5773f5c6984a8a95975

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