Refactoring

This commit is contained in:
Holger Obermaier 2022-01-25 09:47:24 +01:00
parent 3d377760b8
commit ae6ffd4974
2 changed files with 55 additions and 41 deletions

View File

@ -21,10 +21,14 @@ import (
type CPUFreqCpuInfoCollectorTopology struct {
processor string // logical processor number (continuous, starting at 0)
coreID string // socket local core ID
physicalID string // socket / package ID
numPhysicalID string // number of sockets / packages
coreID_int int
physicalPackageID string // socket / package ID
physicalPackageID_int int
numPhysicalPackages string // number of sockets / packages
numPhysicalPackages_int int
isHT bool
numNonHT string // number of non hyperthreading processors
numNonHT_int int
tagSet map[string]string
}
@ -46,10 +50,10 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
// Collect topology information from file cpuinfo
foundFreq := false
processor := ""
numNonHT := 0
numNonHT_int := 0
coreID := ""
physicalID := ""
maxPhysicalID := 0
physicalPackageID := ""
maxPhysicalPackageID := 0
m.topology = make([]CPUFreqCpuInfoCollectorTopology, 0)
coreSeenBefore := make(map[string]bool)
scanner := bufio.NewScanner(file)
@ -67,7 +71,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
case "core id":
coreID = value
case "physical id":
physicalID = value
physicalPackageID = value
}
}
@ -75,23 +79,28 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
if foundFreq &&
len(processor) > 0 &&
len(coreID) > 0 &&
len(physicalID) > 0 {
len(physicalPackageID) > 0 {
globalID := physicalID + ":" + coreID
coreID_int, err := strconv.Atoi(coreID)
if err != nil {
return fmt.Errorf("Unable to convert coreID to int: %v", err)
}
physicalPackageID_int, err := strconv.Atoi(physicalPackageID)
if err != nil {
return fmt.Errorf("Unable to convert physicalPackageID to int: %v", err)
}
// increase maximun socket / package ID, when required
if physicalPackageID_int > maxPhysicalPackageID {
maxPhysicalPackageID = physicalPackageID_int
}
globalID := physicalPackageID + ":" + coreID
isHT := coreSeenBefore[globalID]
coreSeenBefore[globalID] = true
if !isHT {
// increase number on non hyper thread cores
numNonHT++
// increase maximun socket / package ID, when required
physicalIDInt, err := strconv.Atoi(physicalID)
if err != nil {
return fmt.Errorf("Failed to convert physical id to int: %v", err)
}
if physicalIDInt > maxPhysicalID {
maxPhysicalID = physicalIDInt
}
numNonHT_int++
}
// store collected topology information
@ -100,7 +109,9 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
CPUFreqCpuInfoCollectorTopology{
processor: processor,
coreID: coreID,
physicalID: physicalID,
coreID_int: coreID_int,
physicalPackageID: physicalPackageID,
physicalPackageID_int: physicalPackageID_int,
isHT: isHT,
})
@ -108,22 +119,25 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
foundFreq = false
processor = ""
coreID = ""
physicalID = ""
physicalPackageID = ""
}
}
numPhysicalID := fmt.Sprint(maxPhysicalID + 1)
numNonHTString := fmt.Sprint(numNonHT)
numPhysicalPackageID_int := maxPhysicalPackageID + 1
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
numNonHT := fmt.Sprint(numNonHT_int)
for i := range m.topology {
t := &m.topology[i]
t.numPhysicalID = numPhysicalID
t.numNonHT = numNonHTString
t.numPhysicalPackages = numPhysicalPackageID
t.numPhysicalPackages_int = numPhysicalPackageID_int
t.numNonHT = numNonHT
t.numNonHT_int = numNonHT_int
t.tagSet = map[string]string{
"type": "cpu",
"type-id": t.processor,
"num_core": t.numNonHT,
"package_id": t.physicalID,
"num_package": t.numPhysicalID,
"package_id": t.physicalPackageID,
"num_package": t.numPhysicalPackages,
}
}

View File

@ -143,13 +143,13 @@ func (m *CPUFreqCollector) Init(config []byte) error {
// number of non hyper thread cores and packages / sockets
numNonHT_int := 0
maxPhysicalID := 0
maxPhysicalPackageID := 0
for i := range m.topology {
t := &m.topology[i]
// Update maxPackageID
if t.physicalPackageID_int > maxPhysicalID {
maxPhysicalID = t.physicalPackageID_int
if t.physicalPackageID_int > maxPhysicalPackageID {
maxPhysicalPackageID = t.physicalPackageID_int
}
if !t.isHT {
@ -157,13 +157,13 @@ func (m *CPUFreqCollector) Init(config []byte) error {
}
}
numPhysicalID_int := maxPhysicalID + 1
numPhysicalID := fmt.Sprint(numPhysicalID_int)
numPhysicalPackageID_int := maxPhysicalPackageID + 1
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
numNonHT := fmt.Sprint(numNonHT_int)
for i := range m.topology {
t := &m.topology[i]
t.numPhysicalPackages = numPhysicalID
t.numPhysicalPackages_int = numPhysicalID_int
t.numPhysicalPackages = numPhysicalPackageID
t.numPhysicalPackages_int = numPhysicalPackageID_int
t.numNonHT = numNonHT
t.numNonHT_int = numNonHT_int
t.tagSet = map[string]string{