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: 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++, 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

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

Uploaded CPython 3.13Windows x86-64

codegreen-0.3.10-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.7 kB view details)

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

codegreen-0.3.10-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.0 kB view details)

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

codegreen-0.3.10-cp313-cp313-macosx_11_0_arm64.whl (277.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.10-cp312-cp312-win_amd64.whl (298.9 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.3.10-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.7 kB view details)

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

codegreen-0.3.10-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.0 kB view details)

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

codegreen-0.3.10-cp312-cp312-macosx_11_0_arm64.whl (277.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.10-cp311-cp311-win_amd64.whl (298.9 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.3.10-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.7 kB view details)

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

codegreen-0.3.10-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.0 kB view details)

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

codegreen-0.3.10-cp311-cp311-macosx_11_0_arm64.whl (277.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.10-cp310-cp310-win_amd64.whl (298.9 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.3.10-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.7 kB view details)

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

codegreen-0.3.10-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (445.0 kB view details)

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

codegreen-0.3.10-cp310-cp310-macosx_11_0_arm64.whl (277.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.10-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (473.7 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.10.tar.gz.

File metadata

  • Download URL: codegreen-0.3.10.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.10.tar.gz
Algorithm Hash digest
SHA256 bfae971a5ce0c987dd0f8da2843364cc8be1469fe2e6a335772e9b3fc7b95990
MD5 90c087f668f9b050acc1460513d78846
BLAKE2b-256 ed37e6d448c8014ce156a13eedfeb0f8306591cf475cd69af7a7e5dbdbe266f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.10-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 298.9 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.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3c1cfb4969b9da2ba8997123296c6aff8b47cac6b61b9a240bc78f3f6deaa756
MD5 25eb45b0a4639a13e5b23a8ddd7489dc
BLAKE2b-256 489903188a91f0dbd016e8a2a954af6c5caea7d0cd1176d084320e778fb96439

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4cbe3e4f224ce13f7e6c312a165327d8b8113c384a8b64b93b686972fae73314
MD5 ef2a6009287aba28f0058a5e765428fe
BLAKE2b-256 6e47ed85689b9ea778e039992825146269920514986e73e0e5eef4f1cce486b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 255719c515357449e2743160fcad2b4c237afed438936997ab035f6daa240cd6
MD5 5bb331ad7bbf57a871079ee0b1639614
BLAKE2b-256 f66628a79bd71e0a8117b93c2499f4f1cbc36800b49502580e7c08ff98c4363d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c8c3f42529dcbc3b32a229affc21af29024f53379322867ff5b3572b2698cfd2
MD5 6e33c75eca25bdf1dc23af668061a520
BLAKE2b-256 359b44bb1c245f273a1a1fb8385f8f465c677a2fe3784a804dd2cc104416feae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.10-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 298.9 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.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 34ed82991eb90f33cadfe9c4267bd7b52efc13893c93b559b0bdceeca27c4abf
MD5 ba48c5f97bdbb8bfa20054d3a0904c3b
BLAKE2b-256 71cc38de5767fe603a88d8b17b05c2df158ff97a08b94cafd50862f217f6b197

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1df09dc88d86e494f3031db0f978e19938a90c3325dc63c54a550e89baa4408e
MD5 8ec73eae130af6a375429b4d539a5c9e
BLAKE2b-256 749085f9788da9400ca14ae1124e4f171b74480e5a0037e2e95a9cea277d8fe4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 43524df382005c41c9dfca455fb7e263eda09af97c7f08ae8c26fb4aaaf95dc3
MD5 dc3da86a96baa86bea088660bd42d4d8
BLAKE2b-256 ba5029146afc4456f789814e0dcef169085e1e0b3d07b50f98c7cb8e7fc19857

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 53fc93368dccc3401fedf5520255ab9917985bdcb2b5df1df380339110bf469d
MD5 10c02ed85ded3e47eb83e3c5bc4533bf
BLAKE2b-256 c08271ea0eb516a77b4add7a51eb773d9599063bdd561a0ee83654a6ee09fe74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.10-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 298.9 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.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a2bb3f8d195eab8d5631ab40ea4c53a2a971adb4f21e8cb88e8ea27a3b8199a9
MD5 2659467d606d9329e913092b0fc6f973
BLAKE2b-256 c077b83ff2f111329679d05767a6a76800b661ae332c7671881918eb82ad8b83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 684b66ff4ed39a56366143629c2bbf0827b17aa8e7b5ba481353358d32ea807e
MD5 0799ef535e35a1b16742b9dc213e4898
BLAKE2b-256 1f402b048b0e158b0d6f47834beb627451b58baf573bcacd2ae3ebc6bbac12d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 559ef43129889e145201bb63cc144f910cf9499004459396f9e626f07d2725ac
MD5 79b17ee21b257a12f5f1591f42e919e3
BLAKE2b-256 f0397264625c7381b5715518975c66d79d94259a716a6ea1c6355a2d3eaaa215

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2efcdd3d686eae2db91b47069ad380fb2503bf35ec8e292e9c23fd5f936c7b45
MD5 5a0fed106d60019ac3ddbee6a4ae1744
BLAKE2b-256 b61e4efd366a8a769d1897979bf43bb8996435ca465fe6530f6a0b70c447204e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.10-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 298.9 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.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c61493770c98f64067b6a06c9aa3efbfad25c312ebef5152b4db7f1e504e94c5
MD5 a318e681606ff6b3842de3610123e679
BLAKE2b-256 9ba775f7e93afb209f78d0a30dd1311268273f239f71becf71ce9e54cbae22ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dc60861db5b9dcc96208af123ff9ae17cfd3a876fee3f87e904055062647668a
MD5 f76c67b640b8c8fc6518368cf36bf13b
BLAKE2b-256 c4323367cddfea23de9a1ae45d6f07a40c30047e05a67d1a653401b8737de479

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9021eb1a465f628811f498ac59802fce74ffdc95b3a387226483483b5f73afc6
MD5 76e22d3b9b5adb8b2174a7f7f14b23e5
BLAKE2b-256 38abc447997216f047a914beecd382190bdc1614832d01fff3ff4f7eae0d565f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c21f210661e25bb01af0f1121a2a49ea950457634f86ac133beaf7e87d1ed593
MD5 b9c71fb774eddf7cb663b2b49607047c
BLAKE2b-256 f6078058a0ef697e0c8994ed9cffd5d4a328d9962350aff5240b55b1f801fdc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.10-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6891744c1db68990f43402cd6f13719a1eb780b643e74ce7f64487a5a679f708
MD5 d128d45103e5c36bd49c152a9bd846aa
BLAKE2b-256 bb9a286bb46837db556c44d240815ad55903049ebe820e95559e53b299eff653

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