Switch to package slices from the golang 1.21 default library

This commit is contained in:
Holger Obermaier 2023-09-06 09:45:01 +02:00
parent 013aa9ec92
commit 0cf32d5988

View File

@ -10,24 +10,16 @@ import (
"strings" "strings"
cclogger "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger" cclogger "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
"golang.org/x/exp/slices"
) )
const SYSFS_NUMABASE = `/sys/devices/system/node` const (
const SYSFS_CPUBASE = `/sys/devices/system/cpu` SYSFS_NUMABASE = `/sys/devices/system/node`
const PROCFS_CPUINFO = `/proc/cpuinfo` SYSFS_CPUBASE = `/sys/devices/system/cpu`
PROCFS_CPUINFO = `/proc/cpuinfo`
// intArrayContains scans an array of ints if the value str is present in the array )
// If the specified value is found, the corresponding array index is returned.
// The bool value is used to signal success or failure
func intArrayContains(array []int, str int) (int, bool) {
for i, a := range array {
if a == str {
return i, true
}
}
return -1, false
}
// fileToInt reads an integer value from a file
// Used internally for sysfs file reads // Used internally for sysfs file reads
func fileToInt(path string) int { func fileToInt(path string) int {
buffer, err := os.ReadFile(path) buffer, err := os.ReadFile(path)
@ -36,10 +28,8 @@ func fileToInt(path string) int {
cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error()) cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error())
return -1 return -1
} }
sbuffer := strings.Replace(string(buffer), "\n", "", -1) sbuffer := strings.TrimSpace(string(buffer))
var id int64 id, err := strconv.ParseInt(sbuffer, 10, 32)
//_, err = fmt.Scanf("%d", sbuffer, &id)
id, err = strconv.ParseInt(sbuffer, 10, 32)
if err != nil { if err != nil {
cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error()) cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error())
return -1 return -1
@ -64,7 +54,7 @@ func SocketList() []int {
log.Print(err) log.Print(err)
return packs return packs
} }
_, found := intArrayContains(packs, int(id)) found := slices.Contains(packs, int(id))
if !found { if !found {
packs = append(packs, int(id)) packs = append(packs, int(id))
} }
@ -90,7 +80,7 @@ func HwthreadList() []int {
log.Print(err) log.Print(err)
return cpulist return cpulist
} }
_, found := intArrayContains(cpulist, int(id)) found := slices.Contains(cpulist, int(id))
if !found { if !found {
cpulist = append(cpulist, int(id)) cpulist = append(cpulist, int(id))
} }
@ -122,7 +112,7 @@ func CoreList() []int {
log.Print(err) log.Print(err)
return corelist return corelist
} }
_, found := intArrayContains(corelist, int(id)) found := slices.Contains(corelist, int(id))
if !found { if !found {
corelist = append(corelist, int(id)) corelist = append(corelist, int(id))
} }
@ -156,7 +146,7 @@ func NumaNodeList() []int {
if len(matches) == 2 { if len(matches) == 2 {
id, err := strconv.Atoi(matches[1]) id, err := strconv.Atoi(matches[1])
if err == nil { if err == nil {
if _, found := intArrayContains(numaList, id); !found { if found := slices.Contains(numaList, id); !found {
numaList = append(numaList, id) numaList = append(numaList, id)
} }
} }
@ -174,8 +164,7 @@ func DieList() []int {
diepath := filepath.Join(string(SYSFS_CPUBASE), fmt.Sprintf("cpu%d", c), "topology/die_id") diepath := filepath.Join(string(SYSFS_CPUBASE), fmt.Sprintf("cpu%d", c), "topology/die_id")
dieid := fileToInt(diepath) dieid := fileToInt(diepath)
if dieid > 0 { if dieid > 0 {
_, found := intArrayContains(dielist, int(dieid)) if found := slices.Contains(dielist, int(dieid)); !found {
if !found {
dielist = append(dielist, int(dieid)) dielist = append(dielist, int(dieid))
} }
} }
@ -341,7 +330,6 @@ type CpuInformation struct {
// Get basic information about the CPU // Get basic information about the CPU
func CpuInfo() CpuInformation { func CpuInfo() CpuInformation {
var c CpuInformation
smtList := make([]int, 0) smtList := make([]int, 0)
numaList := make([]int, 0) numaList := make([]int, 0)
@ -350,29 +338,30 @@ func CpuInfo() CpuInformation {
coreList := make([]int, 0) coreList := make([]int, 0)
cdata := CpuData() cdata := CpuData()
for _, d := range cdata { for _, d := range cdata {
if _, ok := intArrayContains(smtList, d.SMT); !ok { if ok := slices.Contains(smtList, d.SMT); !ok {
smtList = append(smtList, d.SMT) smtList = append(smtList, d.SMT)
} }
if _, ok := intArrayContains(numaList, d.Numadomain); !ok { if ok := slices.Contains(numaList, d.Numadomain); !ok {
numaList = append(numaList, d.Numadomain) numaList = append(numaList, d.Numadomain)
} }
if _, ok := intArrayContains(dieList, d.Die); !ok { if ok := slices.Contains(dieList, d.Die); !ok {
dieList = append(dieList, d.Die) dieList = append(dieList, d.Die)
} }
if _, ok := intArrayContains(socketList, d.Socket); !ok { if ok := slices.Contains(socketList, d.Socket); !ok {
socketList = append(socketList, d.Socket) socketList = append(socketList, d.Socket)
} }
if _, ok := intArrayContains(coreList, d.Core); !ok { if ok := slices.Contains(coreList, d.Core); !ok {
coreList = append(coreList, d.Core) coreList = append(coreList, d.Core)
} }
} }
c.NumNumaDomains = len(numaList) return CpuInformation{
c.SMTWidth = len(smtList) NumNumaDomains: len(numaList),
c.NumDies = len(dieList) SMTWidth: len(smtList),
c.NumCores = len(coreList) NumDies: len(dieList),
c.NumSockets = len(socketList) NumCores: len(coreList),
c.NumHWthreads = len(cdata) NumSockets: len(socketList),
return c NumHWthreads: len(cdata),
}
} }
// Get the CPU socket ID for a given hardware thread ID // Get the CPU socket ID for a given hardware thread ID