Skip to main content

Know the true energy cost of your code

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

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

Manual measurement from Python (Session API)

For span-based measurement of arbitrary code regions, import codegreen.Session directly — no CLI, no AST instrumentation:

import codegreen

with codegreen.Session("training-run") as s:
    with s.task("data_load"):
        load_data()
    with s.task("train"):
        model.fit(...)
    with s.task("eval"):
        score = model.evaluate(...)
# writes codegreen_<pid>.json with per-task energy + per-domain breakdown

Time-series sampling for power-vs-time plots (area under curve = energy):

with codegreen.Session("infer", record_time_series=True) as s:
    with s.task("batch1"):
        run_inference()
    s.export_plot("infer.html")   # interactive Plotly chart

The Session API uses the same NEMB backend as the CLI (RAPL/NVML/IOReport), supports nested + concurrent tasks, and degrades gracefully if the native library is unavailable. See API → Python for the full reference.

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++, Java. JavaScript config exists but is not yet exposed via CLI.

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

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.3 kB view details)

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

codegreen-0.4.3-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.3-cp313-cp313-macosx_11_0_arm64.whl (305.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.3-cp312-cp312-win_amd64.whl (326.8 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.3 kB view details)

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

codegreen-0.4.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.3-cp312-cp312-macosx_11_0_arm64.whl (305.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.3-cp311-cp311-win_amd64.whl (326.8 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.3 kB view details)

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

codegreen-0.4.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (473.0 kB view details)

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

codegreen-0.4.3-cp311-cp311-macosx_11_0_arm64.whl (305.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.3-cp310-cp310-win_amd64.whl (326.8 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.3 kB view details)

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

codegreen-0.4.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (472.9 kB view details)

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

codegreen-0.4.3-cp310-cp310-macosx_11_0_arm64.whl (305.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (502.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.4.3.tar.gz.

File metadata

  • Download URL: codegreen-0.4.3.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.3.tar.gz
Algorithm Hash digest
SHA256 138c96b711084342f52f469a2030f11af8a1d7b78448719a3a2a08b3511f00bc
MD5 847d73250fe64428a28d6acf634e8dfa
BLAKE2b-256 dceb6f1ee377f9c1cb6c4e9975c3cb60054f66da25b18672cc4a302a153ee235

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 326.8 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b5a69c1c2c3e98398d49a65461201fc4e567a39054c85a242e2c6a5fdf3b29a6
MD5 fced7e4fa70d7af28a85d6aa7f6f11d7
BLAKE2b-256 5eb2b58308094af05bd4a7251fe3f56175fad257219633a9a2cd77814061534d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8be2ef1a389af8ad3c0dcd50c44abe6fb12b237f2991db351f10150c1a8ee086
MD5 a921dc774ca3a8fa9a7c6d206443ca64
BLAKE2b-256 64852d9aabbe7b48640211b260f5393f061679afabf68c3bc9dcca5d4cc84eec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 add5d4926e85fa842d04d17d74ad173e2dbd79947bed1e910e848447cdbd2986
MD5 d3fe896d2e9f783809ecb77eb14ac094
BLAKE2b-256 9312288215487de075e73a89f4c933165760a39c22da23535c8177efec757e0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 994425f71493a7b624049658e5ab2ca4e709d0d17669b69002e14385600e3a67
MD5 b05d4b7591a1513b29ec8d2a5d1bd4e4
BLAKE2b-256 79c4c2fbf315dc9fa607ff6fd9a5045b998d2e154e978f4dedcb074e91109ec8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 326.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7f0bce2a60c4bc3c66d1733412823f9f6f0fa2f09071613c353d237688525a3e
MD5 414e123c52a34540679ffbd873542f18
BLAKE2b-256 e9c2c1033e8ff175d60c97dd6aa576999a90c4822f61175018661d71d2aead51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7bfc3e6fd303955b2de412d56a198094348980d5134edd6f2090cc8c1285f5a6
MD5 ae38239888ea819cd45b111dd2340aec
BLAKE2b-256 bc8410ebdd8d3a1a213eb79a630c9ce83f152cd39a5fbd7c28851541c02e2fa9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6af3511a0b3ef1cca33dc0844a0db914505ac6a57572182b1024c0f24bbda8c2
MD5 4a92a73d150144e220227e4cc33cfe1c
BLAKE2b-256 f07850ae80456fb69ac77ba2b91f942e71b3e8427219e154e2c2968cca43151c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9271fab05f1162264c8d2ec2b36b43423ef4cf89f55fe9285a397d050232fba6
MD5 48b957c7841fa10cb7409c4ab3b3b29e
BLAKE2b-256 c4ee9d523513a245ea24dbefb3d6d5734f9b06d7d19d6ee0a426797ca701d0fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 326.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 65e6c9eb4af760ebb2143f822a96488e4a8a8eeaed450aa9cc2031bfa60c68c9
MD5 d415a94e4fe5d0e0bf4d54d23c03fffe
BLAKE2b-256 24a4de4dee28c0d993dcade39c879ac93ba975619287553ed804680be0e733ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 977bddbe5319b163ea90ecfcb0cb4d949d32046bd1b74be035ec540d48db072f
MD5 0abd5f84ac68db25acd096dab93c48df
BLAKE2b-256 579e131ea07c5d4a83c04b3f8f01b83d02e2c1d14444c5d9ad702c330cca6437

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 eed4550150fe1526011919b05e03cc56ccbd022c9bd5815f94704659358cd371
MD5 91b35d8f001f8e3b9331555746ef369e
BLAKE2b-256 35b43b77c86207fcfe459d0a4ec6f4ac66d1a3903010d5a79ccf69014d410c36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dbcef961551ae42daf097de7c02e4f208aad75cdbaac0e179c363e0a6aa87961
MD5 1c9d3b704dc602040e714441892d733d
BLAKE2b-256 258d8011f09030ccf2c4aae492610490ac37cc5863445a324cfdadac66edb6c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 326.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codegreen-0.4.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1a0f018b81af6940a4b5c67108b00e730ee7fc73b3b7103af5262b1943ea56a2
MD5 9db043cad24551d135a9c46d059b35cd
BLAKE2b-256 a4c81ad35d2dbfc8b19a5609a113dc1a1b32b21204aa5ab15425246126a9a423

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d2a8f9774f8a6c421e8097e969bef04e470377953c69fbf70fb8fbebcecfa16a
MD5 c3dbed812feaf8401468af6fe6be5f29
BLAKE2b-256 0de4bc7b3df1cad9e9af6d29ecc795f8d1456adadaf24572c799b599d6f94ea0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c7f1eea9d5c7d6ba9db02e8144364386643212da2f31e038e8859dfe1a641a04
MD5 aa8acfa171ada051849cf95269def867
BLAKE2b-256 cede1f0466619bd74523d42074625b33d884f034767604dd71aa40517aba931b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 27cb45dfc618e574561cf0f2e590cd2dbd77bca9803022b2e3cd7558bde679c1
MD5 25390b860100f62c86babf468fa38837
BLAKE2b-256 236eeef27845817bf3917e36446b534c923cbef7908faa698c4f06b92bcf4d1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b17b71159d6e11e9f253d145574f3b001bfa8eacebafe3216694104123661038
MD5 41cf404fd7e87881477bfded7c09d10c
BLAKE2b-256 b25678c962fcb4cbfb51eda0f925f5b556507b408c5b7b337bb4e9836760d66d

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