Add caching

This commit is contained in:
Holger Obermaier 2023-09-06 16:19:16 +02:00
parent 2dc78ee0aa
commit 3ac1ada204

View File

@ -20,6 +20,12 @@ const (
PROCFS_CPUINFO = `/proc/cpuinfo` PROCFS_CPUINFO = `/proc/cpuinfo`
) )
var cache struct {
SocketList []int
HwthreadList []int
CoreList []int
}
// fileToInt reads an integer value from a file // fileToInt reads an integer value from a file
// In case of an error -1 is returned // In case of an error -1 is returned
// Used internally for sysfs file reads // Used internally for sysfs file reads
@ -39,61 +45,68 @@ func fileToInt(path string) int {
return id return id
} }
// SocketList gets the list of CPU socket IDs func initSocketHwthreadCoreList() {
func SocketList() []int {
file, err := os.Open(string(PROCFS_CPUINFO)) file, err := os.Open(string(PROCFS_CPUINFO))
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return nil return
} }
defer file.Close() defer file.Close()
packs := make([]int, 0)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() lineSplit := strings.Split(scanner.Text(), ":")
if strings.HasPrefix(line, "physical id") { if len(lineSplit) == 2 {
lv := strings.Fields(line) key := strings.TrimSpace(lineSplit[0])
id, err := strconv.Atoi(lv[3]) value := strings.TrimSpace(lineSplit[1])
switch key {
case "physical id":
id, err := strconv.Atoi(value)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return nil return
} }
if found := slices.Contains(packs, id); !found { if found := slices.Contains(cache.SocketList, id); !found {
packs = append(packs, id) cache.SocketList = append(cache.SocketList, id)
}
case "processor":
id, err := strconv.Atoi(value)
if err != nil {
log.Print(err)
return
}
if found := slices.Contains(cache.HwthreadList, id); !found {
cache.HwthreadList = append(cache.HwthreadList, id)
}
case "core id":
id, err := strconv.Atoi(value)
if err != nil {
log.Print(err)
return
}
if found := slices.Contains(cache.CoreList, id); !found {
cache.CoreList = append(cache.CoreList, id)
} }
} }
} }
return packs }
}
// SocketList gets the list of CPU socket IDs
func SocketList() []int {
if cache.SocketList == nil {
initSocketHwthreadCoreList()
}
return slices.Clone(cache.SocketList)
} }
// HwthreadList gets the list of hardware thread IDs in the order of listing in /proc/cpuinfo // HwthreadList gets the list of hardware thread IDs in the order of listing in /proc/cpuinfo
func HwthreadList() []int { func HwthreadList() []int {
file, err := os.Open(string(PROCFS_CPUINFO)) if cache.HwthreadList == nil {
if err != nil { initSocketHwthreadCoreList()
log.Print(err)
return nil
} }
defer file.Close() return slices.Clone(cache.HwthreadList)
cpuList := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "processor") {
lv := strings.Fields(line)
id, err := strconv.Atoi(lv[2])
if err != nil {
log.Print(err)
return nil
}
if found := slices.Contains(cpuList, id); !found {
cpuList = append(cpuList, id)
}
}
}
return cpuList
} }
// Get list of hardware thread IDs in the order of listing in /proc/cpuinfo // Get list of hardware thread IDs in the order of listing in /proc/cpuinfo
@ -104,30 +117,10 @@ func CpuList() []int {
// CoreList gets the list of CPU core IDs in the order of listing in /proc/cpuinfo // CoreList gets the list of CPU core IDs in the order of listing in /proc/cpuinfo
func CoreList() []int { func CoreList() []int {
file, err := os.Open(string(PROCFS_CPUINFO)) if cache.CoreList == nil {
if err != nil { initSocketHwthreadCoreList()
log.Print(err)
return nil
} }
defer file.Close() return slices.Clone(cache.CoreList)
coreList := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "core id") {
lv := strings.Fields(line)
id, err := strconv.Atoi(lv[3])
if err != nil {
log.Print(err)
return nil
}
if found := slices.Contains(coreList, id); !found {
coreList = append(coreList, id)
}
}
}
return coreList
} }
// Get list of NUMA node IDs // Get list of NUMA node IDs