Refactoring

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

View File

@ -19,13 +19,17 @@ import (
// Only measure on the first hyperthread // Only measure on the first hyperthread
// //
type CPUFreqCpuInfoCollectorTopology struct { type CPUFreqCpuInfoCollectorTopology struct {
processor string // logical processor number (continuous, starting at 0) processor string // logical processor number (continuous, starting at 0)
coreID string // socket local core ID coreID string // socket local core ID
physicalID string // socket / package ID coreID_int int
numPhysicalID string // number of sockets / packages physicalPackageID string // socket / package ID
isHT bool physicalPackageID_int int
numNonHT string // number of non hyperthreading processors numPhysicalPackages string // number of sockets / packages
tagSet map[string]string numPhysicalPackages_int int
isHT bool
numNonHT string // number of non hyperthreading processors
numNonHT_int int
tagSet map[string]string
} }
type CPUFreqCpuInfoCollector struct { type CPUFreqCpuInfoCollector struct {
@ -46,10 +50,10 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
// Collect topology information from file cpuinfo // Collect topology information from file cpuinfo
foundFreq := false foundFreq := false
processor := "" processor := ""
numNonHT := 0 numNonHT_int := 0
coreID := "" coreID := ""
physicalID := "" physicalPackageID := ""
maxPhysicalID := 0 maxPhysicalPackageID := 0
m.topology = make([]CPUFreqCpuInfoCollectorTopology, 0) m.topology = make([]CPUFreqCpuInfoCollectorTopology, 0)
coreSeenBefore := make(map[string]bool) coreSeenBefore := make(map[string]bool)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
@ -67,7 +71,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
case "core id": case "core id":
coreID = value coreID = value
case "physical id": case "physical id":
physicalID = value physicalPackageID = value
} }
} }
@ -75,55 +79,65 @@ func (m *CPUFreqCpuInfoCollector) Init(config []byte) error {
if foundFreq && if foundFreq &&
len(processor) > 0 && len(processor) > 0 &&
len(coreID) > 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] isHT := coreSeenBefore[globalID]
coreSeenBefore[globalID] = true coreSeenBefore[globalID] = true
if !isHT { if !isHT {
// increase number on non hyper thread cores // increase number on non hyper thread cores
numNonHT++ numNonHT_int++
// 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
}
} }
// store collected topology information // store collected topology information
m.topology = append( m.topology = append(
m.topology, m.topology,
CPUFreqCpuInfoCollectorTopology{ CPUFreqCpuInfoCollectorTopology{
processor: processor, processor: processor,
coreID: coreID, coreID: coreID,
physicalID: physicalID, coreID_int: coreID_int,
isHT: isHT, physicalPackageID: physicalPackageID,
physicalPackageID_int: physicalPackageID_int,
isHT: isHT,
}) })
// reset topology information // reset topology information
foundFreq = false foundFreq = false
processor = "" processor = ""
coreID = "" coreID = ""
physicalID = "" physicalPackageID = ""
} }
} }
numPhysicalID := fmt.Sprint(maxPhysicalID + 1) numPhysicalPackageID_int := maxPhysicalPackageID + 1
numNonHTString := fmt.Sprint(numNonHT) numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
numNonHT := fmt.Sprint(numNonHT_int)
for i := range m.topology { for i := range m.topology {
t := &m.topology[i] t := &m.topology[i]
t.numPhysicalID = numPhysicalID t.numPhysicalPackages = numPhysicalPackageID
t.numNonHT = numNonHTString t.numPhysicalPackages_int = numPhysicalPackageID_int
t.numNonHT = numNonHT
t.numNonHT_int = numNonHT_int
t.tagSet = map[string]string{ t.tagSet = map[string]string{
"type": "cpu", "type": "cpu",
"type-id": t.processor, "type-id": t.processor,
"num_core": t.numNonHT, "num_core": t.numNonHT,
"package_id": t.physicalID, "package_id": t.physicalPackageID,
"num_package": t.numPhysicalID, "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 // number of non hyper thread cores and packages / sockets
numNonHT_int := 0 numNonHT_int := 0
maxPhysicalID := 0 maxPhysicalPackageID := 0
for i := range m.topology { for i := range m.topology {
t := &m.topology[i] t := &m.topology[i]
// Update maxPackageID // Update maxPackageID
if t.physicalPackageID_int > maxPhysicalID { if t.physicalPackageID_int > maxPhysicalPackageID {
maxPhysicalID = t.physicalPackageID_int maxPhysicalPackageID = t.physicalPackageID_int
} }
if !t.isHT { if !t.isHT {
@ -157,13 +157,13 @@ func (m *CPUFreqCollector) Init(config []byte) error {
} }
} }
numPhysicalID_int := maxPhysicalID + 1 numPhysicalPackageID_int := maxPhysicalPackageID + 1
numPhysicalID := fmt.Sprint(numPhysicalID_int) numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
numNonHT := fmt.Sprint(numNonHT_int) numNonHT := fmt.Sprint(numNonHT_int)
for i := range m.topology { for i := range m.topology {
t := &m.topology[i] t := &m.topology[i]
t.numPhysicalPackages = numPhysicalID t.numPhysicalPackages = numPhysicalPackageID
t.numPhysicalPackages_int = numPhysicalID_int t.numPhysicalPackages_int = numPhysicalPackageID_int
t.numNonHT = numNonHT t.numNonHT = numNonHT
t.numNonHT_int = numNonHT_int t.numNonHT_int = numNonHT_int
t.tagSet = map[string]string{ t.tagSet = map[string]string{