powersample

package
v0.6.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package powersample measures SoC subsystem energy on Apple silicon.

A Meter measures for the duration of a region and integrates the CPU, GPU, and ANE energy into joules:

m, err := powersample.Start(500 * time.Millisecond)
if err != nil {
	log.Fatal(err) // the error says how to grant access
}
work()
r, err := m.Stop()
fmt.Printf("GPU: %.2f J over %s\n", r.Energy.GPU, r.Duration)

Start needs no privileges: it reads the SoC's cumulative energy counters (the "Energy Model" group in the private libIOReport.dylib — the same source powermetrics aggregates) directly. Where IOReport is unavailable it falls back to running powermetrics, which requires root: the fallback runs it directly when the process is root, otherwise through sudo -n, and when neither works the returned error contains the exact command that grants passwordless access to powermetrics alone. TestLivePowermetricsAgreement cross-checks the two backends against each other when run with root.

The numbers are Apple's own estimates and are documented as unsuitable for comparing devices; they are for comparing configurations on one machine. Both backends are version-brittle in their own way — the IOReport channel names ("CPU Energy", "GPU Energy", "ANE") and the powermetrics text format, whose parser is pinned by hand-authored testdata matching the output shape observed on macOS 26.6.1 (25G76), not a captured live run. Report.Samples lets a caller detect a silent change in either (zero samples over a nonzero duration), and Stop returns an error when no energy was seen at all. TestLiveMeter exercises the IOReport path unprivileged.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Power

type Power struct {
	CPU      float64
	GPU      float64
	ANE      float64
	Combined float64
}

Power holds one estimate per SoC rail, in the caller's unit of the moment: watts in a Sample, joules in Report.Energy.

type Sample

type Sample struct {
	Elapsed time.Duration
	Power   Power // watts
}

Sample is one powermetrics sample window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL