Skip to content

Commit

Permalink
utils: add functions for setting cpu frequency scaling governor
Browse files Browse the repository at this point in the history
Signed-off-by: Feruzjon Muyassarov <[email protected]>
  • Loading branch information
fmuyassarov committed Sep 18, 2024
1 parent dc1742e commit f115c95
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/utils/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func setCPUFreqValue(cpu ID, setting string, value int) error {
return writeFileInt(cpuFreqPath(cpu, setting), value)
}

func SetCPUScalingGovernor(cpu ID, governor string) error {
return writeFileStr(cpuFreqPath(cpu, "scaling_governor"), governor)
}

// GetCPUFreqValue returns information of the currently used CPU frequency
func GetCPUFreqValue(cpu ID, setting string) (int, error) {
raw, err := os.ReadFile(cpuFreqPath(cpu, setting))
Expand Down Expand Up @@ -63,6 +67,17 @@ func SetCPUScalingMaxFreq(cpu ID, freq int) error {
return setCPUFreqValue(cpu, "scaling_max_freq", freq)
}

// SetScalingGovernorForCPUs sets the scaling_governor of a given set of CPUs
func SetScalingGovernorForCPUs(cpus []ID, governor string) error {
for _, cpu := range cpus {
if err := SetCPUScalingGovernor(cpu, governor); err != nil {
return err
}
}

return nil
}

// SetCPUsScalingMinFreq sets the scaling_min_freq value of a given set of CPUs
func SetCPUsScalingMinFreq(cpus []ID, freq int) error {
for _, cpu := range cpus {
Expand Down Expand Up @@ -129,6 +144,10 @@ func writeFileInt(path string, value int) error {
return os.WriteFile(path, []byte(strconv.Itoa(value)), 0644)
}

func writeFileStr(path string, value string) error {
return os.WriteFile(path, []byte(value), 0644)
}

func readFileInt(path string) (int, error) {
data, err := os.ReadFile(path)
if err != nil {
Expand Down

0 comments on commit f115c95

Please sign in to comment.