68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package drivers
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type SysDriver struct{}
|
|
|
|
func NewSysDriver() *SysDriver {
|
|
return &SysDriver{}
|
|
}
|
|
|
|
func (r *SysDriver) ReadClockSensors() ([]ClockSensor, error) {
|
|
// Glob directly targets scaling_cur_freq for all numerical CPU core paths
|
|
freqFiles, err := filepath.Glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to glob cpufreq files: %w", err)
|
|
}
|
|
|
|
clockSensors := []ClockSensor{}
|
|
|
|
for _, path := range freqFiles {
|
|
// Example path: /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
|
|
// Extract core ID from path segments
|
|
parts := strings.Split(path, "/")
|
|
if len(parts) < 6 {
|
|
continue
|
|
}
|
|
|
|
// Parse "cpu0" -> 0
|
|
cpuDir := parts[5]
|
|
coreIDStr := strings.TrimPrefix(cpuDir, "cpu")
|
|
coreID, err := strconv.Atoi(coreIDStr)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
// Read frequency in kHz directly from the matched file path
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
khz, err := strconv.ParseFloat(strings.TrimSpace(string(data)), 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
clockSensors = append(clockSensors, ClockSensor{
|
|
path: path,
|
|
Name: fmt.Sprintf("core %d", coreID),
|
|
FrequencyGHz: khz / 1000000.0, // Convert KHz to GHz
|
|
})
|
|
}
|
|
|
|
// Guarantee numerical order by core ID
|
|
sort.Slice(clockSensors, func(i, j int) bool {
|
|
return clockSensors[i].Name < clockSensors[j].Name
|
|
})
|
|
|
|
return clockSensors, nil
|
|
}
|