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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.7-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.3 kB view details)

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

codegreen-0.3.7-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.2 kB view details)

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

codegreen-0.3.7-cp313-cp313-macosx_11_0_arm64.whl (275.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.7-cp312-cp312-win_amd64.whl (294.2 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.7-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.3 kB view details)

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

codegreen-0.3.7-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.2 kB view details)

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

codegreen-0.3.7-cp312-cp312-macosx_11_0_arm64.whl (275.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.7-cp311-cp311-win_amd64.whl (294.2 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.7-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.3 kB view details)

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

codegreen-0.3.7-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.2 kB view details)

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

codegreen-0.3.7-cp311-cp311-macosx_11_0_arm64.whl (275.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.7-cp310-cp310-win_amd64.whl (294.2 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.7-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.3 kB view details)

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

codegreen-0.3.7-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (440.2 kB view details)

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

codegreen-0.3.7-cp310-cp310-macosx_11_0_arm64.whl (275.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.7-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (468.3 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.7.tar.gz.

File metadata

  • Download URL: codegreen-0.3.7.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.7.tar.gz
Algorithm Hash digest
SHA256 822f8a752aa45f343fd32670eb28cb40c0b33dea2373050c32161e7a705d5678
MD5 60817e433ad5df374d4434f79254f35b
BLAKE2b-256 e061e7137383b0d5d3b240b3bd02529673f1121dbb8c4339955886fc997099bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.7-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 294.2 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.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e66e327970113ef3cc46c2c5649124be20456ab78b42bc0c720c852fb2e5f8f3
MD5 0b5fe20faa22563500669e4fdf58a006
BLAKE2b-256 045ee2fcc0ef4eb3cd7d0699b38a1d128ce4f9efd9bf6102398a1f0c3de500ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ecdfd524e13a089f420865da640dd8b25273ae2d60ba0dd6874a04f3c0bab32
MD5 e82953554b9edd4dcf13d2eb2f8028bf
BLAKE2b-256 ca0c715e4e1ca32a60170fec40d311fb764631a0acc1a2a0dcfb40bdb1cb003c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3339aa864ac9987ad38a2f64b9132a200cf5c4b6777e0c3071c971bb4a431c0d
MD5 facf29b09662fa1937e1396adbf083b4
BLAKE2b-256 121daa49277f2cef28ad71d4439d2d65fa383af38f9c09fa86c76a028da827b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7427a8390f1b6e809686d01bed27ca01962a6cba836cc07f1b23778c5e4d9994
MD5 73d9c0563ce4afa9f5e66f0d10608100
BLAKE2b-256 c930e78d4754f6584c8cc054508145ec10fcb89da48e494b56baffe39ae30c6e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 294.2 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.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2dfc92a520e40cdef9ae0e7c097ead16fed520ea6be770044d549578bb2d24a5
MD5 e7512b68da579020864bbde3d53769c9
BLAKE2b-256 f9db4cb1d99902c0fa01e9289011ff516733c7ca36c5287c8629af79c67b929c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5bf7d4f4b8a15703625f2c21196f08f856755cdae7f4c91aefb7e179010a4bba
MD5 780e81618ee06d396f5eb35bfcc7e69c
BLAKE2b-256 e562f0f93972bf9fbca8ce4816a865fa6f58a192345910ef359f466606b35e2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 36a76a824913131a8e23a1589dcc177f2a21560d495e813381978af87b4df45c
MD5 ac496afd4e5b25152bb428155308c495
BLAKE2b-256 c6e6267c5dae1cf3bf73dedaf7be8b50c961ce4c920be5566106f7b17c998311

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 692d66dda24b3b0598e86aa4797d2ddd64685aa6815672f8120fd1a1c91e039a
MD5 dd6894643f6ee925836b4e8179dafef2
BLAKE2b-256 5596f8bf0709a3b6184d68bd8c81c06985258d2f3a67c03ab4e73d9f584fe3bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 294.2 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.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0a52a348d699923596b1b74c36e4e9ca30c85d4aca60e06952662d611d9d2439
MD5 c343c968519fa3222dca3d82e28a316b
BLAKE2b-256 2599418941880f81b99f6d2cbb8128a998563d4b352c7a738ee0ff64abcb33e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 789fda2c8df71493cd005df18f704da8aba7d2895291aafea8a9655dca25a4db
MD5 a07ddb81f1c510d48229c9df40288713
BLAKE2b-256 6b00468ef2dca15dea6a72ccc793a25f572d9234ed68ef3d8af77ccde91ec465

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e5d50d683058ddf09059eb4e22a2b35d888471490e6939f4ea378549c731db91
MD5 739352cfbe7ebdedd08a08fa4efe4b7e
BLAKE2b-256 86b956ddd65501ecab0f7aa9041b7db2d6788885cb2fb2279dc21f3783782f1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 001fc45a345f7e6950a5d7dcc9e7344b0cec374c53ff5f3b99d9f1e3de0faece
MD5 7a5544f819246a9376c17b0abe64a121
BLAKE2b-256 3e606cb93b55f38e956889020ccb45617a442272169d03d8e88f6d07407c1d8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 294.2 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.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a396ca3040bed4b99e5144aebdfcfbb0a69d62659667d88f5d42882ca8934424
MD5 2ced38a680536e3bf21fba5d243d34b4
BLAKE2b-256 45e5a94efa4918f1610a784f22ba4438a87c9bd0288fab8e19fd5f067a548c9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6039c9560a43d26589c121a544cc7b437911a21c5fee7b76587ace630fe5c7ca
MD5 57ad91acce2e5585e31fc8441e8c3b1f
BLAKE2b-256 b1c50dcd5cf8dffdcef7bc553e4e7b1354bb0ea2fd50e9c5906836a8ae559efc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e5d843d0f3dfd226d27c4d6b57e176ba8cb68b6dcfe38fe3a442b19d2428650d
MD5 3b7b4fd52682c9ba24a0fd446a8decc2
BLAKE2b-256 1633bf6ad9252daa3ef8f07c076034578713c08799739c09f707f525a861192b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 03a0a523c1e2ea36313d4f7c67b6fc2ab8f743c1bb34fd0368eda4f874500a04
MD5 d6e6dee86194767aef11eaf2b9bfd47e
BLAKE2b-256 2709f73cb60833623d78a29cd56d16c8a43cbe3100d457ad813aaaeb10b5dced

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.7-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d2f1c1321c74da8e41ee88773e6afae6d656e3e5b015ec2f22704cc0f99c125e
MD5 2d4dbbed7fef69a4086df9d54fdaa7a6
BLAKE2b-256 eb9906a8632bfb43df8912f2f0280eea12a21b5ffd0315c11625c9dc2121a80f

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