powertune

package
v0.4.140 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 13 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 powertune lets a person turn on, one by one, the power savings the kernel leaves off, and take them back (CasaOS#221).

These are the settings PowerTOP calls "tunables": a file of sysfs to write. Nothing here runs PowerTOP: the versions the distributions package cannot list a tunable without applying all of them, nor leave a device out. And nothing is turned on by itself: `powertop --auto-tune` suspends every USB device and network card, which loses keystrokes, drops links and disconnects dongles on many machines.

What is offered is a short list of what is safe to try and easy to take back:

  • runtime power management (power/control = auto) of a PCI device of a few classes: USB controllers, audio, SMBus, communication and signal processing controllers. Never one that carries a disk or a network interface. A USB controller only sleeps once every device plugged into it does, so a keyboard or a Zigbee dongle left as it is keeps it awake;
  • the same for a SATA port nothing is plugged into;
  • two settings of the kernel PowerTOP also changes: the NMI watchdog off, and dirty pages written every 15 seconds instead of 5;
  • the processor's energy/performance preference, on machines that have one.

A choice is kept in a file and written again at each start, for the same device only (its vendor and device ids are kept with it). Taking a choice back writes what the file held before.

A setting that freezes a machine must not come back at every boot. While settings are being tried (two minutes after one is turned on, or after the start wrote them again) a marker file exists; a start that finds it writes nothing and says so, until the person turns something on again or takes everything back.

Index

Constants

View Source
const Trial = 2 * time.Minute

Trial is how long a setting is watched after it is written: a box that stops within it does not get it back by itself.

Variables

View Source
var (
	// ErrUnknown is an id that names nothing offered on this machine.
	ErrUnknown = errors.New("no such power setting on this machine")
	// ErrPreference is a processor preference the machine does not list.
	ErrPreference = errors.New("this machine does not offer that processor preference")
)
View Source
var (
	ErrGovernor = errors.New("this machine does not offer that processor governor")
	ErrTurbo    = errors.New("the turbo of this machine cannot be set from here")
)

ErrGovernor is a governor the machine does not list; ErrTurbo a machine whose turbo cannot be set.

Functions

This section is empty.

Types

type CPU

type CPU struct {
	Preference string   `json:"preference"`
	Available  []string `json:"available"`
	Chosen     bool     `json:"chosen"`
	// Governor is the cpufreq governor, with the ones the machine offers; absent when it offers one only.
	Governor       string   `json:"governor,omitempty"`
	Governors      []string `json:"governors,omitempty"`
	GovernorChosen bool     `json:"governor_chosen"`
	// Turbo says whether the processor may go past its base frequency; absent when the machine does not say.
	Turbo       *bool `json:"turbo,omitempty"`
	TurboChosen bool  `json:"turbo_chosen"`
}

CPU is the processor's energy/performance preference, absent on machines without one.

type Item

type Item struct {
	ID string `json:"id"`
	// Kind is usb-controller, sata-port, pci or system.
	Kind string `json:"kind"`
	// Name says what it is, in the kernel's words: a class, an address, vendor:device.
	Name string `json:"name"`
	// Saving: the device may sleep when idle (power/control is auto).
	Saving bool `json:"saving"`
	// Chosen: turned on here, and written again at each start.
	Chosen bool `json:"chosen"`
	// Status is what the kernel says of it now: active, suspended, or nothing when it does not say.
	Status string `json:"status,omitempty"`
}

Item is one device whose runtime power management can be turned on.

type Monitor added in v0.4.139

type Monitor struct {
	History []Reading `json:"history"`
	// Cores is how long the cores spent in each of their idle states, a mean over the cores; the rest is time at work.
	Cores []Share `json:"cores,omitempty"`
	// Package is how long the whole package spent in each of its states; only where the kernel's msr device is there.
	Package []Share `json:"package,omitempty"`
	// Asleep and Allowed count the parts of the list: allowed to sleep, and asleep right now.
	Asleep  int `json:"asleep"`
	Allowed int `json:"allowed"`
}

Monitor is the last readings, and for the last one where the time went.

type Reading added in v0.4.139

type Reading struct {
	At time.Time `json:"at"`
	// Watts is what the processor package drew, when the machine counts it.
	Watts *float64 `json:"watts,omitempty"`
}

Reading is what the machine did over about a minute.

type Share added in v0.4.139

type Share struct {
	Name  string  `json:"name"`
	Share float64 `json:"share"`
}

Share is how much of a moment was spent in one idle state, from 0 to 1.

type Status

type Status struct {
	Items []Item `json:"items"`
	CPU   *CPU   `json:"cpu,omitempty"`
	// PackageWatts is what the processor package drew over a short moment, when the machine counts it.
	PackageWatts *float64 `json:"package_watts,omitempty"`
	// Held: the choices were not written again at this start, because the box stopped while they were being tried.
	Held bool `json:"held"`
	// Conflicts are other tools set up on this box that write the same settings at each start: powertop, tlp, tuned.
	Conflicts []string `json:"conflicts,omitempty"`
	// Monitor is what the machine really did: its power over the day, and where the time of the last minute went.
	Monitor *Monitor `json:"monitor,omitempty"`
}

Status is what the dashboard shows.

type Tuner

type Tuner struct {
	Sys   string
	Proc  string
	Etc   string
	Dev   string
	State string
	// Sleep waits between the two readings of the energy counter; After runs f once d has passed. Tests replace both.
	Sleep func(time.Duration)
	After func(d time.Duration, f func())
	// contains filtered or unexported fields
}

Tuner reads and writes the settings under Sys (the root of sysfs) and Proc, and keeps the choices in State. Etc is where the other tools that do the same are looked for.

func New

func New(state string) *Tuner

New is the tuner of this machine.

func (*Tuner) Apply

func (t *Tuner) Apply() []error

Apply writes the choices again, at the start of the box: the kernel forgets them at each boot. A choice whose device is no longer the same one (another card at that address) is left alone, and kept in case the device comes back. Nothing is written when the last trial did not end: the box stopped while settings were being tried, and one of them may be why.

func (*Tuner) Restore

func (t *Tuner) Restore() error

Restore takes every choice back, and what was held with them.

func (*Tuner) Sample added in v0.4.139

func (t *Tuner) Sample(now time.Time)

Sample takes a reading: called once a minute. What happened since the last one becomes a line of the history.

func (*Tuner) Set

func (t *Tuner) Set(id string, on bool) error

Set turns the saving of one device on, or takes it back to what it was before.

func (*Tuner) SetAll

func (t *Tuner) SetAll() error

SetAll turns on everything offered that is not on yet: the list is already only what is safe to try.

func (*Tuner) SetGovernor added in v0.4.139

func (t *Tuner) SetGovernor(governor string) error

SetGovernor sets the governor on every core; an empty one takes it back to what it was.

func (*Tuner) SetPreference

func (t *Tuner) SetPreference(preference string) error

SetPreference sets the processor's preference on every core; an empty one takes it back to what it was.

func (*Tuner) SetTurbo added in v0.4.139

func (t *Tuner) SetTurbo(turbo string) error

SetTurbo turns the turbo "off" or "on"; an empty word takes it back to what it was.

func (*Tuner) Status

func (t *Tuner) Status() Status

Status lists what can be turned on and how each stands.

Jump to

Keyboard shortcuts

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