From b6e5bc1e3b5d9f1df680f216cd535e555c2c2faf Mon Sep 17 00:00:00 2001 From: Feruzjon Muyassarov Date: Wed, 21 Aug 2024 15:56:56 +0300 Subject: [PATCH] utils: add functions for setting cpu frequency scaling governor Signed-off-by: Feruzjon Muyassarov --- pkg/utils/sysfs.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/utils/sysfs.go b/pkg/utils/sysfs.go index 0dfb70c..650fa9f 100644 --- a/pkg/utils/sysfs.go +++ b/pkg/utils/sysfs.go @@ -34,6 +34,10 @@ func setCPUFreqValue(cpu ID, setting string, value int) error { return writeFileInt(cpuFreqPath(cpu, setting), value) } +func setCPUScalingGovernor(cpu ID, setting string, value string) error { + return writeFileStr(cpuFreqPath(cpu, setting), value) +} + // GetCPUFreqValue returns information of the currently used CPU frequency func GetCPUFreqValue(cpu ID, setting string) (int, error) { raw, err := os.ReadFile(cpuFreqPath(cpu, setting)) @@ -63,6 +67,22 @@ func SetCPUScalingMaxFreq(cpu ID, freq int) error { return setCPUFreqValue(cpu, "scaling_max_freq", freq) } +// SetCPUScalingGovernor sets the governor mode of a given CPU +func SetCPUScalingGovernor(cpu ID, governor string) error { + return setCPUScalingGovernor(cpu, "scaling_governor", governor) +} + +// SetCPUsScalingGovernor sets the scaling_governor of a given set of CPUs +func SetCPUsScalingGovernor(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 { @@ -129,6 +149,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 {