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.8.tar.gz (2.0 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.8-cp313-cp313-win_amd64.whl (336.3 kB view details)

Uploaded CPython 3.13Windows x86-64

codegreen-0.4.8-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (512.0 kB view details)

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

codegreen-0.4.8-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.6 kB view details)

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

codegreen-0.4.8-cp313-cp313-macosx_11_0_arm64.whl (315.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

codegreen-0.4.8-cp312-cp312-win_amd64.whl (336.3 kB view details)

Uploaded CPython 3.12Windows x86-64

codegreen-0.4.8-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (512.0 kB view details)

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

codegreen-0.4.8-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.6 kB view details)

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

codegreen-0.4.8-cp312-cp312-macosx_11_0_arm64.whl (315.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

codegreen-0.4.8-cp311-cp311-win_amd64.whl (336.3 kB view details)

Uploaded CPython 3.11Windows x86-64

codegreen-0.4.8-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (512.0 kB view details)

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

codegreen-0.4.8-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.6 kB view details)

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

codegreen-0.4.8-cp311-cp311-macosx_11_0_arm64.whl (315.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

codegreen-0.4.8-cp310-cp310-win_amd64.whl (336.3 kB view details)

Uploaded CPython 3.10Windows x86-64

codegreen-0.4.8-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (512.0 kB view details)

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

codegreen-0.4.8-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (482.6 kB view details)

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

codegreen-0.4.8-cp310-cp310-macosx_11_0_arm64.whl (315.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

codegreen-0.4.8-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (512.0 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.8.tar.gz.

File metadata

  • Download URL: codegreen-0.4.8.tar.gz
  • Upload date:
  • Size: 2.0 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.8.tar.gz
Algorithm Hash digest
SHA256 1977ad9073195c3addc1536d21606ba4f8cfd624a6e5fa9fae9cd08a2c7a0719
MD5 9a58513111fd2525547f353effa5818e
BLAKE2b-256 3476855ceebbf7f0c920751a8e49630f3ca998920435af6eeb64407cb0332102

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 336.3 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.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 da197deef9cf49f5e86f8229c74413fbbe54ac2a4617636b42f7a11f3d05fc45
MD5 e6b19ca852c506f4bd98a52c6155073c
BLAKE2b-256 a716f8a374463f65a6ae5f3b39517cd45acf1eb3506beea4dd445db8f389bbe3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4f4a06df7383737b8ce8e35dc0994e7c24d123bb88b8cc5e461d4da90974b414
MD5 40d2610513e582d08e108bca1f7590f8
BLAKE2b-256 e5d753d80d9c44fbb8df1d51170ed19852b9c25d542042c45d7e20166125d389

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 eba02fbf6e7a01ac511cc6bc9142bcf963c486d93c127c51d6e1b0a511d3a00e
MD5 2fc6ee8435dbb9812e8a7b87524f0d33
BLAKE2b-256 fe4a3bfcbf4bd4628268ae9b971642b53baf49035a95d4868d0de01d1f9fa56d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4e6780650c0669413f13bc041a92b61e11273cdb62b3debc767cfeb0ca52878
MD5 eb375d8fbe7e915831c67398b8856070
BLAKE2b-256 2bc9f1ec2d26ef129886605f10d0343b38abf4d484b2fc543a55945082fefd59

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 336.3 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.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b99f74b2cc75cf4a7e51b2aeb5fbe29e31fcd6ece5a8f0de1498462eaa8ff6bd
MD5 d65d51007963bb7465f5a660e0008666
BLAKE2b-256 1400868169b43645a4e681a3b8561a70cd1f05599d135f71319b3b6f41410f71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 736bd9eb08b72b69cb2359790dafd07d92a8c8645fff69b79292ba87e367f0c7
MD5 bebbc0fc7e729ca43fccc02bf8fa0f5a
BLAKE2b-256 21dd405be08db5863da65725da0b75e0494c9eb0374c50ae3bd1e66382d326c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3443169330ead7ba5d7cea533a0d40c5efc85559447a19504858284b927e161f
MD5 d3a0daca878786e9b37fb0a7f8358d7c
BLAKE2b-256 e83c4e47159258a1d29f859ee348ade7eff517e3d8ffca0686ca999f6a0269f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0e9a574cd5bc07a9bd86ac8bb2baaae52d60fa7ca44c40c34c339c2cb14ddc8
MD5 bfbd1a47acda532f56238dc49fd044a4
BLAKE2b-256 b69c929e0de736441e754b9ba205af8ecbccd79c9852caff5105815c214142cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 336.3 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.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 420c72e28aa440c880c9951cf3444a3f47d02c965df46f5154b8fb06ff475b80
MD5 9418898edd2aed428f71c319119f3d44
BLAKE2b-256 9de9734ad7d542947c66b284fb5fb4ec0503328ca78630fd1bc35f2789c835c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3dbb17c1140b18cbfecb73f227594dcb24537e8e7a4870133f2f14b6f4050b1c
MD5 bbd5f2ec460643d1e44e92c289b48ef2
BLAKE2b-256 fbfa7b3ad8081f9aa0213c8bc854a0d691ba1306e656345802e48eb30076b12e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2497704a6845f6733b6efb736a192050059005e7c040dd9fe54f944ebfabc295
MD5 01da3033bf7d0e390a3b63215120633a
BLAKE2b-256 486c165960ca09b881d91e47e10314f80bfe2444bbc0646ea9bbe5ca05045bc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 96c1364956367ca5103c1dcf7293b22974269293f08b6006028236fe255b8ae4
MD5 018bd4c241d2498fe43930a9fae06c47
BLAKE2b-256 cfa7bae5f5f5b52c542644d39a86e5936f7391730b333b40cd85fb27bf5d89d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codegreen-0.4.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 336.3 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.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2b53956381b983f997af11b26f7d4afcea88526a51015535aa040adfc672fc2e
MD5 d39237f1c25ae86ec1fa6191d6c885d2
BLAKE2b-256 ba70f554f05afb654e134d45eac9d9fe538cee765c4ae78131856aa1eea5ff5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 771868be9f809865c3a23e2aa0c2a5fa39df495cebde8582d44a6e67a93df56b
MD5 03dd95afb82f102608ddc2c52b6f256c
BLAKE2b-256 a9a6b4b18ca2596ea7725e039e222086f6d191c14dfcbd36320fb5228875e41a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8fccc13134a63b82a19783561fb592167c177cc01c077ec9ca3a7f0802f2e850
MD5 0ea9fedc7894aae8e8bae00538e10cb2
BLAKE2b-256 38c270d4eb86305b5ce182b66eca104cc4022fd80353c4ea94282e518446db89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2993a0263aa26796fd62d0c0bce438385e64470e875b772c6dbe02f9939f2356
MD5 cccf91262feb24da84bc2b29eeee52ea
BLAKE2b-256 ca7fcd84fcb4cb5f2da5ba7e94c9bc0d08d7f0549b5916555052b5b4ec421aa3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codegreen-0.4.8-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f1203f7a4090aa52abbb08360c53d4c45ae7beb9200eb68c377c749f9613bf56
MD5 cbc82b90c4a0f54549e570498fe792a0
BLAKE2b-256 63664a5d04e096d840ecff8db58a058a068ac5a9cf8fb70ca48803d270a80793

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