Skip to main content

A library for programmatic, in-code energy measurement on Apple Silicon.

Project description

In-Code Energy Measurement on Apple Silicon  ⚡

A lightweight, header-only C++ library for precisely measuring energy consumed by arbitrary code snippets on Apple Silicon processors.

Also available in Python, installable via pip.

This library is used to provide macOS support for Zeus, a framework for deep learning energy measurement and optimization.

Installation

Python

Install the package using pip:

pip install zeus-apple-silicon

Then, import the necessary components in your Python script:

from zeus_apple_silicon import AppleEnergyMonitor, AppleEnergyMetrics
# or: import zeus_apple_silicon

C++

  1. Copy/move the header file apple_energy.hpp (found in the apple_energy/ directory of the source repository) into your project.
  2. Include the header in your C++ files:
    #include "apple_energy.hpp"
    
  3. Link your executable against Apple's CoreFoundation framework and the IOReport library (part of IOKit); both dependencies are available by default on nearly all macOS devices. The library requires C++17.
    • Manual Compilation (g++/clang++) - add necessary flags to your compile command:
      g++ your_code.cpp -o your_executable -std=c++17 -framework CoreFoundation -lIOReport
      # or
      clang++ your_code.cpp -o your_executable -std=c++17 -framework CoreFoundation -lIOReport
      
    • CMake - add the following to your CMakeLists.txt file, where your_target_name is the name of your executable or library target:
      target_link_libraries(your_target_name PRIVATE
          "-framework CoreFoundation"
          IOReport
      )
      

Usage Overview

The library operates by defining measurement windows. You mark the beginning and end of a code section you want to measure.

  1. Start a measurement window: use AppleEnergyMonitor::begin_window(label) to indicate you want energy measurement to start at that line of code. Each window needs a string label passed in as an argument.
  2. The code being measured: The start of the window should be followed by the code you want to measure energy for.
  3. End & Retrieve Results: indicate where you want your measurement window to end by using AppleEnergyMonitor::end_window(label) with the same label you used to mark the window's start. The AppleEnergyMonitor::end_window(label) function returns an AppleEnergyMetrics object containing energy data collected during the window.

Note about Measurement Windows:

  • You can have multiple windows active simultaneously (i.e., they can overlap), as long as each uses a distinct label.
  • Non-overlapping windows can re-use names. I.e., once a window is ended with end_window, its label is free to be reused.
  • Attempting to start a window with a label still currently in use (i.e., end_window not yet called for that label) will raise an exception.
  • Calling end_window with a label that doesn't belong to any currently active window will raise an exception.

Note about Results of Measurements:

  • Results are reported via an AppleEnergyMetrics struct, but depending on your processor, some metrics may not be available (e.g., DRAM may not be available on older machines). In such cases, fields that could not be measured will be presented as: None in Python, and an empty std::optional object in C++.
  • A more detailed explanation of results is provided later in this readme.

Usage Examples

The API is identical in C++ and Python. For available fields of a result object, read this section of the readme.

C++ Example

#include "apple_energy.hpp"

int main() {
    // Create a monitor instance.
    AppleEnergyMonitor monitor;

    // --- Basic Measurement ---
    monitor.begin_window("task_1"); // Indicating the measurement window starts here.

    // Do some work...

    // End the window and get results.
    AppleEnergyMetrics result1 = monitor.end_window("task_1");


    // --- Overlapping Measurements ---
    monitor.begin_window("outer_task");

    monitor.begin_window("inner_task");
    AppleEnergyMetrics inner_result = monitor.end_window("inner_task");

    AppleEnergyMetrics outer_result = monitor.end_window("outer_task");


    // --- Reusing a Label ---
    monitor.begin_window("task_1"); // This is okay because previous "task_1" window ended.
    AppleEnergyMetrics result = monitor.end_window("task_1");
}

Python Example

from zeus_apple_silicon import AppleEnergyMonitor, AppleEnergyMetrics

# Create a monitor instance.
monitor = AppleEnergyMonitor()

# --- Basic Measurement ---
monitor.begin_window("task_1") # Indicating the measurement window starts here.

# Do some work...

# End the window and get results.
result1 = monitor.end_window("task_1")


# --- Overlapping Measurements ---
monitor.begin_window("outer_task")

monitor.begin_window("inner_task")
inner_result = monitor.end_window("inner_task")

outer_result = monitor.end_window("outer_task")


# --- Reusing a Label ---
monitor.begin_window("task_1") # This is okay because previous "task_1" ended.
result = monitor.end_window("task_1")

API Reference

Class: AppleEnergyMonitor

The main class for getting energy measurements.

  • AppleEnergyMonitor(): Constructor. Initializes the monitoring system.
  • begin_window(label: str): Starts a new measurement window identified by label.
  • end_window(label: str) -> AppleEnergyMetrics: Ends the measurement window identified by label and returns an object containing the results.
  • get_cumulative_energy() -> AppleEnergyMetrics: Returns cumulative energy consumed from an unspecified point fixed over the lifetime of the energy monitor (e.g., from bootup).

Struct/Class: AppleEnergyMetrics

This struct/class is how results get reported, containing metrics for various different SoC subsystems.

All energy values are reported in mJ.

Note: on some hardware configurations or macOS versions, certain metrics might not be available or reportable by the underlying system. In such cases, the corresponding attribute will be:

  • C++: An empty std::optional. You should check .has_value() before accessing .value().
  • Python: None. You should check for None before using the value.

Fields Reported in AppleEnergyMetrics:

  • CPU Related Metrics:

    • cpu_total_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Total energy consumed by all CPU related subsystems combined.
    • efficiency_cores_mj: std::optional<std::vector<int64_t>> (C++) / Optional[list[int]] (Python)
      • Energy consumed by each efficiency core individually. Returns a list where each element corresponds to an efficiency core.
    • performance_cores_mj: std::optional<std::vector<int64_t>> (C++) / Optional[list[int]] (Python)
      • Energy consumed by each performance core individually. Returns a list where each element corresponds to a performance core.
    • efficiency_core_manager_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy attributed to the efficiency core cluster's management logic.
    • performance_core_manager_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy attributed to the performance core cluster's management logic.
  • DRAM Metrics:

    • dram_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy consumed by DRAM.
  • GPU Related Metrics:

    • gpu_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy consumed by the on-chip GPU.
    • gpu_sram_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy consumed by the GPU's SRAM.
  • ANE Metrics:

    • ane_mj: std::optional<int64_t> (C++) / Optional[int] (Python)
      • Energy consumed by the Apple Neural Engine (ANE).

Source Code Directory Structure

  • apple_energy/: Contains the core C++ header library file (apple_energy.hpp).
  • bindings/: Contains nanobind bindings to create the Python package from the C++ library.
  • examples/: Contains sample usage and compilation examples (like the ones above).
  • scripts/: Utility scripts for development and CI.
  • tests/: Contains tests, which use mocked data.

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

zeus_apple_silicon-1.0.4.tar.gz (25.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

zeus_apple_silicon-1.0.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl (55.7 kB view details)

Uploaded PyPymacOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl (55.7 kB view details)

Uploaded PyPymacOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-cp313-cp313-macosx_11_0_arm64.whl (57.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-cp312-cp312-macosx_11_0_arm64.whl (57.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-cp311-cp311-macosx_11_0_arm64.whl (58.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-cp310-cp310-macosx_11_0_arm64.whl (58.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

zeus_apple_silicon-1.0.4-cp39-cp39-macosx_11_0_arm64.whl (58.6 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file zeus_apple_silicon-1.0.4.tar.gz.

File metadata

  • Download URL: zeus_apple_silicon-1.0.4.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zeus_apple_silicon-1.0.4.tar.gz
Algorithm Hash digest
SHA256 b0e962581bcf9c3e2f78ebae7089e9a7ec9d720bdf84b96b97fd1df9fb0038b2
MD5 99d1006f4d8dfabfa0dffa6b496ceff7
BLAKE2b-256 424f5c44fc2fc12430a3e88a1a92d53552ba653ff452bf4b82bcd7bbc2991025

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4.tar.gz:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 603bc4f001fabff4c26d0fd2e80b62a034c34663752d27ab7dff333078e9001f
MD5 1ecdc337427c587b9daf350a62c326ac
BLAKE2b-256 5b71f1dbebd3ebea09c9c5df81650b88e74213869166dffeb86ce3427ccb45b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e5f28df40e0a8bb4aa0c15cdb3929f83b4ca0c21a33561834245b038bad8599
MD5 bb671086c7a3fa7b84971369b7ded90f
BLAKE2b-256 4a790a78366d88b0ca2b70e271d2354436c13d21d924eccde2452d848a347d2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 66181452bdcf959fc08ebae4420093af73adb070ae2dd01dcea675cb6a98ed06
MD5 419801b385cf95e9c0db9c9b2e123f9a
BLAKE2b-256 71db110b4d9c40f489ded7f71b4bf403e019e6709645fa806ee1ed7662803456

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5f86c2fc6a5d94def678d3fa6b9733d1121b21410ccbe4d6e2eae84399213155
MD5 a985ffec879c98025d058ac41c7a2ae6
BLAKE2b-256 8c9d8259efa557552da370c126e2251a5f23de96b704c18298c8a8120b557d0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0242e5f44cc44b65433ea2cf4e976c3ce49034616039ba55d40f72926af5c7b
MD5 fc980123c36d87a75ff6dc1bb53c987a
BLAKE2b-256 542e399e9c17ca2997387b1bb123ac8373251ae26845c6c9640a91e95170ddc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 df4503b7d9b6535120dd4563432922e8581df4ff9bac04ad92f900527c174890
MD5 e66e3fd0c46436f25048e4fd98bc3e3a
BLAKE2b-256 223597c6d104f7eb11eff0d4f6a76caa933d3e5f8daebb19f6433044571ebca8

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zeus_apple_silicon-1.0.4-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zeus_apple_silicon-1.0.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c10719ec0c5795b611a68a706cdd5e8c6e96698b8f7877b73feb964f808c8cba
MD5 3633c4b51dde19ef9dee57ce34cfbced
BLAKE2b-256 c0574be3b2e3352c09508488fd0993e953bb5c90a065942952f98455d0910290

See more details on using hashes here.

Provenance

The following attestation bundles were made for zeus_apple_silicon-1.0.4-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yaml on ml-energy/zeus-apple-silicon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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