cpufreq

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSysfsCPUPath is the default sysfs path for CPU frequency control.
	DefaultSysfsCPUPath = "/sys/devices/system/cpu"
)

Variables

This section is empty.

Functions

func CleanupOrphanedCPUFreqState

func CleanupOrphanedCPUFreqState(
	ctx context.Context,
	log logrus.FieldLogger,
	stateFiles []StateFile,
	sysfsBasePath string,
) error

CleanupOrphanedCPUFreqState restores settings from all orphaned state files. sysfsBasePath is the base path for CPU sysfs files (e.g. "/sys/devices/system/cpu").

func FormatFrequency

func FormatFrequency(kHz uint64) string

FormatFrequency formats a frequency in kHz to a human-readable string.

func HasWriteAccess

func HasWriteAccess(basePath string) error

HasWriteAccess checks if we have write access to CPU frequency sysfs files.

func IsCPUFreqSupported

func IsCPUFreqSupported(basePath string) bool

IsCPUFreqSupported checks if CPU frequency control is supported on this system.

func ParseFrequency

func ParseFrequency(freq string) (uint64, error)

ParseFrequency parses a frequency string and returns the value in kHz. Supported formats: "2000MHz", "2.4GHz", "2400000KHz", "2400000", "MAX".

func RemoveStateFile

func RemoveStateFile(statePath string) error

RemoveStateFile removes a state file.

func RestoreFromStateFile

func RestoreFromStateFile(
	ctx context.Context,
	log logrus.FieldLogger,
	statePath, sysfsBasePath string,
) error

RestoreFromStateFile restores CPU frequency settings from a state file and removes it. sysfsBasePath is the base path for CPU sysfs files (e.g. "/sys/devices/system/cpu").

func SaveState

func SaveState(cacheDir string, settings *OriginalSettings) (string, error)

SaveState saves the original CPU frequency settings to a state file.

func ValidateFrequency

func ValidateFrequency(basePath string, freqKHz uint64) error

ValidateFrequency validates that the specified frequency is within system bounds.

func ValidateGovernor

func ValidateGovernor(basePath string, governor string) error

ValidateGovernor checks if the specified governor is available on the system.

Types

type CPUInfo

type CPUInfo struct {
	ID             int
	MinFreqKHz     uint64
	MaxFreqKHz     uint64
	CurrentFreqKHz uint64
	Governor       string
	AvailGovernors []string
	ScalingMinKHz  uint64
	ScalingMaxKHz  uint64
}

CPUInfo contains frequency information for a single CPU.

type CPUSettings

type CPUSettings struct {
	ScalingMaxKHz uint64 `json:"scaling_max_khz"`
	ScalingMinKHz uint64 `json:"scaling_min_khz"`
	Governor      string `json:"governor"`
}

CPUSettings stores settings for a single CPU.

type Config

type Config struct {
	Frequency  string // "2000MHz", "2.4GHz", "MAX", or empty (unchanged)
	TurboBoost *bool  // nil=unchanged, true=enable, false=disable
	Governor   string // Governor name, defaults to "performance" if Frequency is set
}

Config holds CPU frequency configuration.

type Manager

type Manager interface {
	Start(ctx context.Context) error
	Stop() error
	// Apply applies CPU frequency settings to the specified CPUs.
	// If cpus is empty, settings are applied to all online CPUs.
	Apply(ctx context.Context, cfg *Config, cpus []int) error
	// Restore restores original CPU frequency settings.
	Restore(ctx context.Context) error
	// GetCPUInfo returns CPU frequency info for all online CPUs.
	GetCPUInfo() ([]CPUInfo, error)
}

Manager controls CPU frequency settings for benchmark CPUs.

func NewManager

func NewManager(log logrus.FieldLogger, cacheDir, sysfsBasePath string) Manager

NewManager creates a new CPU frequency manager. sysfsBasePath is the base path for CPU sysfs files (e.g. "/sys/devices/system/cpu").

type OriginalSettings

type OriginalSettings struct {
	CPUs       map[int]*CPUSettings `json:"cpus"`
	TurboBoost *TurboBoostSettings  `json:"turbo_boost,omitempty"`
}

OriginalSettings stores the original CPU frequency settings before modification.

func LoadState

func LoadState(statePath string) (*OriginalSettings, error)

LoadState loads CPU frequency settings from a state file.

type StateFile

type StateFile struct {
	Path      string
	Timestamp time.Time
}

StateFile represents a CPU frequency state file for orphan detection.

func ListOrphanedStateFiles

func ListOrphanedStateFiles(cacheDir string) ([]StateFile, error)

ListOrphanedStateFiles finds state files left behind by interrupted runs.

type TurboBoostSettings

type TurboBoostSettings struct {
	Type  string `json:"type"`  // "intel" or "amd"
	Value int    `json:"value"` // Original sysfs value
}

TurboBoostSettings stores turbo boost settings.

type TurboBoostType

type TurboBoostType string

TurboBoostType represents the type of turbo boost control available.

const (
	TurboBoostIntel TurboBoostType = "intel"
	TurboBoostAMD   TurboBoostType = "amd"
	TurboBoostNone  TurboBoostType = "none"
)

func GetTurboBoostEnabled

func GetTurboBoostEnabled(basePath string) (bool, TurboBoostType, error)

GetTurboBoostEnabled returns whether turbo boost is currently enabled.

Jump to

Keyboard shortcuts

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