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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.5-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.5-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.5-cp313-cp313-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.5-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.5-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.5-cp312-cp312-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.5-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.5-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.5-cp311-cp311-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.5-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.5-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.5-cp310-cp310-macosx_11_0_arm64.whl (275.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.5-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.5.tar.gz.

File metadata

  • Download URL: codegreen-0.3.5.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.5.tar.gz
Algorithm Hash digest
SHA256 009360488a7e89fea9097fa275f2a9dddcf146ed21db47aa21c618142ef33188
MD5 8bac8376f889dc3508ea2e2f2bf15577
BLAKE2b-256 6f7a1ca5deab493220181ba42eb3ded964425085d0dd9f316137d396e92eb47c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.5-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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e9e791cb89ad4c2614b77cf940b4b20cf44f839e03b3e2e91dc55d76635efe1f
MD5 fd3b70ada6dd2d0e46e219b6aa364b75
BLAKE2b-256 209cdf4f4905c6a194d9d9d5ada6dcb0a7f917f6191b26cccf0ceeba4a39bf5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 70c103a7ce9ddddcd6fdd32ebb9734596c9b433f5c07e246eb81b6c36cd01b3c
MD5 61752ed6f733f8f69c0b13118a511760
BLAKE2b-256 ec8d501f50b386d635b0460cdaea25f2638cb4fed7c7b89742c6c9e0e9870c3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ed1f085c84bd65c7776f288c03574efa102232d5705d4d675bf8a91b09ec2830
MD5 c95f96b5931d0f57cd27f64395fd155f
BLAKE2b-256 6b6bded455818b422a37faea59ef043f87e2be347f7de0c6afff7b39b9440e6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f9cb96077e0c8791b149c4427d8eb41ced51bc8fc229e5ae6ca2245b0d4f50e
MD5 d2cb271665268e01086e0bf5ad647564
BLAKE2b-256 04d3839270b1dc4f1a2d9f263a7457b0129a22463d30731612dfd1bce137cf67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.5-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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 687112ad268979a0e52df3b1af642e2898600581112942889790656ad1e6f33a
MD5 0574818ab60452b5ae73fc73eb82d36e
BLAKE2b-256 efff0fea18b59a6d5f5b976d1578fab3c79dec954a984388c6aabc081a8b961c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2a3861f35a628a80fb5b8afd29d49be7bdaa0f3bd72eefa49d95c47f8cfb626f
MD5 e9ab2bf7fe0802292c09bc9f8e18089a
BLAKE2b-256 51c0d797dd36b7ff1ed99b212dd3dba4178bbdc69dcdaff0e2e04aac8fc3647b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ec0e18cbf67d0620ea9206e03e11332430250a7d8a3611cb340ca67c8508ad01
MD5 02c121d3c1a70d53a5c084104baf935b
BLAKE2b-256 66b8f85eeeda1d8b7c43d4edbadadf64cbfac7561fcc661b2c8c5f936c27d81d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e055e761285179226fe4cacf7d8d844090689f45ec8c6e7bd43d2e3fda3d70b2
MD5 fa0e345a3d78bb12490cbff2be64ffd5
BLAKE2b-256 57bb9a6a82ced1c4d242a3b8b92f84a403d894faedc796295ad6e3baef294149

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.5-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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c72ad828902bb97108ed6ac9ef396179892f5295e595b58b2bf5493dc4ddec75
MD5 75270be5b21430d3670b07bc32837bff
BLAKE2b-256 255022a4c8c495d6b6ead252fb42ca8c771b24d6b7a975a98ee6de5d60ea5bbd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4681dabcd834a378c72dd793410dc681982b3993d608b8d51879583bbc0ebc2f
MD5 9f519ff36bc6923e3509f7a99ca376e1
BLAKE2b-256 1d447e934f8232199fc7adf649b4fc624c2e80e31afa8cfebbd80fbf13e47d21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f0ddc7cd56bdf3e7a9aa8be88a3bc278cec83bfa751461bd09351f956e6e2889
MD5 6f16f1ddf0b7bbac27b43993ccc9f255
BLAKE2b-256 8506d65b4bb6d0480c009c1b2a229a86fee541bdbca3564a48f2993f55577293

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a3ab2e9ef2bd0d95935e0d88af837dc337de9b622dcbb98a05bb50d3dec3c856
MD5 d6f0ee861f9d3ca7b11d211e4c088c69
BLAKE2b-256 50a6509c546b17aa87f0226a0aede83066ca53cdab81303cb63543b59f79cb1a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.5-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.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 798e204244941503399ba784514ff634a894c39d90d0f85b4102a1acb114acbb
MD5 09d20c0b9b324129afc42cf95f7ce782
BLAKE2b-256 96d854475720beb750184ca7199bce609660c69485edd22eca764ec802bba2b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3c749c37c6530e0043324d2451a2e80cf7088c0e900786dfede84904f207da62
MD5 36a277733f4a594fcc442dea83d79a35
BLAKE2b-256 54d3ad530019a122997099733decf21d98951834a1f39867239df399e53eefd4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 248051665594daabcaab161e16b3984d70b5cb780ae49e125718d445cabf72d6
MD5 d97fcac764ee75af417cf56f46cde072
BLAKE2b-256 9a855a6b6d9847590d99e8f073cac7dfd9c72cd3a4a87282731ef660b2ddb961

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e8d8a77ae635badde0e5accec82d1af9029c4af9c6916e786bb0d42650a6e44
MD5 9f14b27b42b139fac919ce847b68522a
BLAKE2b-256 f93f1663863c74de2581071e0151963a761b39522245f6bf506885fe074012f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.5-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9378d10ed9237a000e10b738c3555a5785745d773dc87797a3d023e7cbd46bff
MD5 76daaddb1994d632771d8a3881263997
BLAKE2b-256 e6dee2acaf0f40985c2794e95e8aed2dcebec98cf5962de0ed7ca896dac85317

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