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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.4-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.4.tar.gz.

File metadata

  • Download URL: codegreen-0.3.4.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.4.tar.gz
Algorithm Hash digest
SHA256 800a5fc2b57edd2266707a57fb98dfc03c2899377a7f07c8863f52409c37d5ca
MD5 66f49b7b366d8616256b6551a2203749
BLAKE2b-256 ebdb053ac2aab99f53230fd8387915fffefaf833568f7358ec453f70cc913072

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.4-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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 df4c0fa564bb516f78f98c71f0eaffbc3132462a6161bc76c05ad78e1b8614c1
MD5 a0b49090928f0326f1f47cacf2cfe66b
BLAKE2b-256 9e72d493c710e1185e72bf3cdab7fbe1a6479c58c148c57d7f3e418d0c35a6ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2c24fb4b00f6b076cb15f936099b06b9c6dede1000c90b2da968a9d46e61b65f
MD5 064e5245920e80689ab91800a5f1d622
BLAKE2b-256 da2e5fafe878c838794ccccc4daaade0526c7f88772b8dd98541bae63352b82d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1e9a3b5235e5271b6ad7cb473f97a64ad5dfea0ecf3f13fd9500c279687fca3d
MD5 f6361df26322c22e526c5bf0fd891984
BLAKE2b-256 9dc53ec3913aaa9f61070f1ccfc7af2d90d9568f89e3ea4512297dc1f84ddf61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b88039d6797ef208229f408392670bddcc378d47234a965f3a2c265c49ec860
MD5 f7048983ab7cd9df5d3ed7a45441ba14
BLAKE2b-256 cc595ad062ce9c11d7d8edfbb933254bcb00ecfe02c3975c968228cb8e4c5138

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.4-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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 74107a11538fb7f0cf2cce36a738ee20d28ae753b5d2967efc6a02f73fd35fa5
MD5 1983683383fc5612d8ab100fcfbf3f33
BLAKE2b-256 ff2b6957b969ae5d7798306d7d169156be78303130f7038373888d362dcc4d72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bda550bd39867d654f913ccb76d38ada07fa2aa26ad02c49e62611c5a6d60eab
MD5 d7401de7aa1abf6fb5240e3f27fcb50c
BLAKE2b-256 7192bcf4e3e68c0dd42fee677b58cc84cd9ae954c329a32a2fd4eef57438a695

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f6beeef7b6662ff8f3e9f5e770a614b010690239bb62ae364f60e715c1243d2a
MD5 07a9475e6e3d5e417fe021b720f579ce
BLAKE2b-256 58a6408b4fb5f7889d40aca93c4cf91b49504becbf5247d0e9de4f032c68b553

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 54230aefdced49f012a895c092471eb9b05a8b811fc2979768953e5d5bd5d401
MD5 7aa3f9e71490fb3d4bbcb7ea3c893684
BLAKE2b-256 8602f35a9f66098999b2b938bda50ed9d5b75719b648ecf391a60acf2fc67923

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.4-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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a66c03f091a7ce5218901df44f942e1dba06512a0f9031ad8fe12ef5ce1d0d2e
MD5 e513f9571bfb49782a337ca2b1510c77
BLAKE2b-256 a1f341110875326de7d4f3a5d6970f5cbaff328552cee948facb2d3ff434810e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 66658207a690aeaf6deb696296df1c12f2aa6b075541838cdfb932b823d7d7f0
MD5 fbe34d09df208dd901ad95c6da3951cb
BLAKE2b-256 b98d9c8ca622fa45ce4cae22e1301e8c095632970777aecaa1148bed410e81ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 558724b31125a8490f9386474cfc061411fdcecb85a1729d3eb98830a0c36dc1
MD5 9aaffc86d9e3ab7bdb6ff689a1366db7
BLAKE2b-256 83564d96c425038cb6629071eef267dfda06606b869b3138ba3f570c3878449e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79b559607d6edbc7049e6ffb6fcda7f5b817c910f09ca1e2dedc500913383011
MD5 ea3e9375ac08d3c672ce9080a05ae8c6
BLAKE2b-256 e6e5310cf4b4b4d8d2b2faf2b5f770db4b53f99c2de4828e10ceeeecb4ffc753

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.4-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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dcc3a6990c8ea456b54a75e10e69f4b4d1366c4679c7b3d5779240b719efe6f0
MD5 89dd6926f85b1b7e96a09fd6b00dc56c
BLAKE2b-256 dfbed0c1c043778b7a42a294f763afdcf05b7777a21da45a99606e4a209aabf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 68af30434daab65d278b660a74c2d266f8ad5ea1d9fb95aa2dc1a0bee81422c7
MD5 2cf2f3953cf8b6f62e5f0bc1d64a655a
BLAKE2b-256 87c9ebd05d913b188b806d464f54c1a11835ceeefdd83ba9a969a4b9097f5f64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7a7ec3fab0264afb3f68acda471f19f840bc5b30c991eea95ff8d2fca9c40d74
MD5 d021f9aaa7f61cf62a69f5bd5814765d
BLAKE2b-256 a2712eac57cfd4c94c5220faa44e2c727478608fef2fc82d1a3a502a152debd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eed6864e119c73d571bf92eb2f10411fb60b3299ae76870953f551cee727abc2
MD5 413c217214151ba7b7a3c2d67ff9a3de
BLAKE2b-256 ae12e4b0da563b14a960fe867fac449758805260e80b781f0282e31b4e3dbfb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.4-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f28012c78e5ca2a708c605379b14b9a7209b6f5808fdec766dc1323eec3d934
MD5 3d564a4741f877897ca99bad97e12151
BLAKE2b-256 e78eed785a82a242f52a7f7769e3db34163749f8280f216fa38663cf163c24dd

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