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

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (499.7 kB view details)

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

codegreen-0.4.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (470.4 kB view details)

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

codegreen-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (303.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.0-cp312-cp312-win_amd64.whl (324.2 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (499.7 kB view details)

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

codegreen-0.4.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (470.4 kB view details)

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

codegreen-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (303.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.0-cp311-cp311-win_amd64.whl (324.2 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (499.7 kB view details)

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

codegreen-0.4.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (470.4 kB view details)

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

codegreen-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (303.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.0-cp310-cp310-win_amd64.whl (324.2 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (499.7 kB view details)

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

codegreen-0.4.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (470.4 kB view details)

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

codegreen-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (303.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (499.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.4.0.tar.gz.

File metadata

  • Download URL: codegreen-0.4.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c2e7b15951632075411e266f9b1d621f946ba78cc2c1def08a3ea3ac6db9b814
MD5 3d488465d08b70747bbbea29aa5f9f82
BLAKE2b-256 1b036f084b509e65c2d2a6fe774541229e6125dac5676ec068ab58b85d883245

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 324.2 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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 77b22ddecedbdf459fc1e7ef2bab0e560e3f51ac2218882b250a9af0f3248d1f
MD5 b23f98834f1b064b3963c50a7a222569
BLAKE2b-256 d7269878df9e09a8a0018982c94aa5d21b2e9659ee9052e167ba2aefdfc1c486

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d6b17b9ff22b3b4c09313ed2894255e1c0cc36b1d3e41f4a6a9fecacacc7f909
MD5 1548ea8439f70e5fcf129a8edb2a934e
BLAKE2b-256 38cdef4b17fa5e34fb8a8ca2afe488b71e98f2358d6348b2195c7f45268fab74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2e1012e16bb15deda88642bfd869ac8f3e5ad6e96fcdb509d9df564787efd632
MD5 c39902fc35ccf6e5603333299f553ec1
BLAKE2b-256 f9e76a4f71283954884a0952058e022c1924e83fc0d13cf7c8c11adbd9732e62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d285a358b7e36d41ac260b14adf827e01603cad8ab2e963cd114913a5dcf466
MD5 aa14f8d4d2cf5015e669d365fd11873c
BLAKE2b-256 112d6ee7673407be94a55afd1415ef74add43dc7ba99ece3256d1d0e8b39cbe8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 324.2 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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3b4ed091afbb422025f7595b1a78ec29b25556a0b9dff5eddc0a4d7be7fff89d
MD5 d3ea53c47c4dded2ecc8d3e44ba30564
BLAKE2b-256 d530da54b670c3b26b7a1d9567cbc6fe7f9867b6a540412f9d97df5962394538

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bdd0502f3394c96153a62814145305807f9048126bd63095d65d2dbe9b9647d1
MD5 8a3cc5ad344b98cbf6d01f459a143705
BLAKE2b-256 d03c2750eb4c84b1f337091443947c8fb61e77ab29c4081c1e6d2e46c19db604

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6c0347b65c9bcf9df479edfc485e5f67c56d10f1782510b1b1e855a50638b40e
MD5 f3d7e4458c6d90ee22731706bd4cbd4a
BLAKE2b-256 ab9247a6b2dedefeba9be07441966f4d7739f327e9db5536f938a0b437ab237a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c7d66968ab97166d6eb465ab3627e8f86ad6e2b9d92142d9ba870a043c56596b
MD5 17811527be0a4ffaa4d398061170029c
BLAKE2b-256 e535ec2856fcb54e970ad6887da4a6d34ad877d0829c42a3b433a39d1b413710

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 324.2 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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 54bc20a42e23d1234913863f3426fff590473550242d89533e5fb2330a7efc4a
MD5 241fe3f11c13697cbed5f48dfa88694f
BLAKE2b-256 b05c309f145e97771cc6aea424e8d6cafeff6f0c0f0f2e21654ee1ba1d030311

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d1759df9d1bcc71eabd6a8510391503c255f4e6ca5dcf4d0e26f53fa0575096e
MD5 9c5c80a4a405e200a234291c63c04acc
BLAKE2b-256 38f2d626f8b04b828369c1c625a52498f9bf0ffd42ed3501653ac7246a4cf01d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 47641c00d6e0b37b1527662b48fcd137cec956096c8cd261cc58ab08a39494c3
MD5 84dad08ffd6f68d342a1c4c813e5c848
BLAKE2b-256 32012a5ef16065b0e54551ace9bc593fc4beb82330f5db3f3369b662fee6bdd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 13c056bfb715a374d90e4d8f1ecf1be813d20bad10f209f5e3386761fbb606b0
MD5 06b7e32fc787250e6753ebd832933b29
BLAKE2b-256 b1d434648f6d2637884adb894ed02f6e7843edd10be30edefc9e32cbec48147c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 324.2 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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 80ce0e90e3893fff37f7a44933c22d32642ea72111d3bc96a2692cd30030db78
MD5 83fa85741fdfdf0810e82877b18ea94c
BLAKE2b-256 cb39677c52630bd986c652ce564810066627818422262b496a82ba15b543becb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d99dcea33f5a96f572c8fa3c583fb6a86f982ee7da0dc0ec970024ffa531b6f2
MD5 9d0a9721d29e2a13bfbe8a51cbfd0898
BLAKE2b-256 fa05e6d322f07b8a34872fe7ba57709d6a556f6e3859dcaa5922f3abc1fcc326

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 75f8211595e7fffd16fc6a762ef42ca4e1f9fee4c2a1f672ca7e594f816a82eb
MD5 1e1ff18f53c0e106502d248005f87aab
BLAKE2b-256 fd7c0ef63e7786d82dd5d2f868e1417ce92bdae5f2edcc225abcdec34c0db347

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 addf1b9ee8c08f5789dd45e85b857abc6232de2de8a7f81b40eedc08aec11818
MD5 71f792f6c6c62ee08f6145e6e2f9bb1a
BLAKE2b-256 3141da34f79a85dc90e48712c0c063c7c3788f5c5820431d1670ef4e751ad0cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ec19668a753bad3ddbb2f14df2db0dd974f50822d8ad49b00c8155a569ad7280
MD5 24f94be2039ad63ab5850e931f2b4a98
BLAKE2b-256 3350e44076399ca40c6f6a85e65f84bd07b536a22ae3699d9480fe0326250262

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