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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.3.6-cp312-cp312-win_amd64.whl (294.3 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.3.6-cp311-cp311-win_amd64.whl (294.3 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.3.6-cp310-cp310-win_amd64.whl (294.3 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.3.6-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.6.tar.gz.

File metadata

  • Download URL: codegreen-0.3.6.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.6.tar.gz
Algorithm Hash digest
SHA256 491b721f2a3a5a3bab5aabc46d47409cac352dea58bb6fe7b741ac68e7be3da8
MD5 f5ce474607aabdfc1273ead530def07a
BLAKE2b-256 6dc9b2ba6151bca0c00b8242726878d3d5632c486a733789ec4631e106835177

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 294.3 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.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a6c573644efc1ff3711763eb167769deba0479cbe28c52caf40b4710fd4bbadb
MD5 4f240ad1abfcb2f4fb82c6a835c9d84f
BLAKE2b-256 a8166cc9a36d672fe17fec8a1ede7e319e3957ae62d9540df672fd3bc1f96e8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 23d323815345271dfd1354589c8347f04acd976f2684aff99176f9ca1a844298
MD5 4ef280b01416bcb354f0d6ece7c90013
BLAKE2b-256 3e29fea68a17f3c72e35abf48aa4f9d4eee7ddb08c6518d4c4b787b701bef23d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 79bb200ef14f7dd891989c5a86c4bea286fddc740627af3752f2a14ef4ae723a
MD5 2603a9a34fad20b4b38d74721a40bad2
BLAKE2b-256 cc3e5d1acfa5d68944c80fdf0e66281e92f1cf9af0adf90b0b866fee3fdd164c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e6f982d6b7baaf7e817abee990a7516c5aa535341cdcf13019638df8ea4e5b17
MD5 53eb5e60638e2ba880dd3f839f731131
BLAKE2b-256 47bf66bdf468a9d5ba4bec9c3bf22349d0268d9d80d5284109eebb7b93cdfdc5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 294.3 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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1bc0c81ce0380904efab7e2265daec4c288011681b2d99be6492c2431bc0cac7
MD5 35b77c0efb3355eb76403edee163ec76
BLAKE2b-256 658dc3b4b5c5b419f9a46ae0ac224039d3e810a5caf2289b8b3a12898fe5f28f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6cecac165338ad8b3de40711c20f0218e866b3b72df5e60bf6ee9f6616850dfb
MD5 154895a732f9fd5ea01097199906f74c
BLAKE2b-256 277f9c4c37c08f45b06a5f1f731584b93672970136f9eaa994a761945c8d6b10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 084b998323f51988cd83a29627e8e157db65ad4f5bbe193dea89a98dd01e5fa4
MD5 ac5dff91f847a1cb6d695835ffb7cf14
BLAKE2b-256 707129e9dd9c8ace02770dd651ff9fac0412884a12306e44fc14e434cc96bb94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ff1d8adaae1d8ad6636221e0876d5215815f3f46b847665acf097c4cd3ab03fe
MD5 0c7eb4684c61cbff51f973859a9a95e4
BLAKE2b-256 21bfe5c740f6d026a3aaec64e56049935c65ee4c628a3752dd0924725df170b4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 294.3 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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 94d790cd95b48f6c10b3eef8187f93b2f39ac5aa9f7996be9a559038367eee1e
MD5 c1f4ca886a5aad4ba7a777030d595f9c
BLAKE2b-256 d2d066fbf15f55eb0f8cae8da6b43e27068cdef1c49c971012a5ada07c599eda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 be3e020ba4e3d2d8f65685989e74da3cf22e5e9f5ce219b160f9174bb11e6d2e
MD5 5c9e16d88069ff7aaf43800b766c95c9
BLAKE2b-256 6ae780f4794101056c923d03367bf4334888024165389609f7e16b9123779ef8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d510174a26fc47f6a83e0508f1909cab9bb713da3f591bb8cbe2207c2bcd2fe2
MD5 b045ee7393c2744040d88776bef84d62
BLAKE2b-256 bfd7f7d52ef4bf9e681350b2dd3700c6a084582825609a46a4d13121662adcf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ef372cabdecbb4af150e716a510eb139e3df8f5513161c736cb3e26ee68898e1
MD5 904ae1fb00b4ab3460a29e39d920e27f
BLAKE2b-256 a379c992417c760935692cf63f0442a0746a557242ed77a2a2aa4861b6420db5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.3.6-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 294.3 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.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6e737e3f597c736445121ed02d5c6a05a3e2a540a74c0f2085b89a5536b8b798
MD5 a29740a62c1468d26bb6e2176e129254
BLAKE2b-256 b3e0a8a5bfbb3b2227b30cdd6b18003b5e12cb0f8048a70b2e3066c52282abb6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e64afc55df11b5046596c398cd00d20887c3ab8e1259f3211440f9626e811492
MD5 4a23a248f9f93be1aafa27de1988f8d0
BLAKE2b-256 357e61ec0e27138f8cca25260a5e637b524271f9e2b78122c78bdd335366d355

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 432a994e1bb60a7fb02e55c2f6a7629af09c11c268e59279ccc8741c4243cc55
MD5 97004f7d07b34d3fc9d64e00270fa078
BLAKE2b-256 9e90b7a4dee1036b1aacafba1062bd73ffddbb4e104c4a23c3464f132244bb5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6add3130b3a20bf01451d9c53179f469b223768f84d69671291fa0862402c9a9
MD5 924ff70f4a790dada307f7f96339af95
BLAKE2b-256 ecefe05cbe4d5e17dfa6f8a509f8c5ea902f1e00e3e7d0915940e62c6c59c24d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.3.6-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 835efd8b8564d98e3de7fb2694ae716c0e49c129572af42f3189ceb8cd8ba667
MD5 51251f4bb37ad2cec82375a10597752f
BLAKE2b-256 c2ee185d2147bf6049a72e3c744139257fb21536a09055d7fa9f80609bd9d99e

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