mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-04-08 22:45:55 +02:00
Refactoring
This commit is contained in:
parent
75282b0731
commit
f532abdb4e
@ -20,13 +20,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 {
|
||||||
@ -51,10 +55,10 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) 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)
|
||||||
@ -72,7 +76,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error {
|
|||||||
case "core id":
|
case "core id":
|
||||||
coreID = value
|
coreID = value
|
||||||
case "physical id":
|
case "physical id":
|
||||||
physicalID = value
|
physicalPackageID = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,55 +84,65 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,10 +32,19 @@ func readOneLine(filename string) (text string, ok bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type CPUFreqCollectorCPU struct {
|
type CPUFreqCollectorTopology struct {
|
||||||
// coreID, packageID, num_cores, num_package
|
processor string // logical processor number (continuous, starting at 0)
|
||||||
tagSet map[string]string
|
coreID string // socket local core ID
|
||||||
scalingCurFreqFile string
|
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
|
||||||
|
scalingCurFreqFile string
|
||||||
|
tagSet map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -48,10 +57,10 @@ type CPUFreqCollectorCPU struct {
|
|||||||
//
|
//
|
||||||
type CPUFreqCollector struct {
|
type CPUFreqCollector struct {
|
||||||
metricCollector
|
metricCollector
|
||||||
config struct {
|
topology []CPUFreqCollectorTopology
|
||||||
|
config struct {
|
||||||
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
||||||
}
|
}
|
||||||
cpus []CPUFreqCollectorCPU
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
||||||
@ -68,9 +77,6 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
|||||||
"group": "CPU Frequency",
|
"group": "CPU Frequency",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize CPU list
|
|
||||||
m.cpus = make([]CPUFreqCollectorCPU, 0)
|
|
||||||
|
|
||||||
// Loop for all CPU directories
|
// Loop for all CPU directories
|
||||||
baseDir := "/sys/devices/system/cpu"
|
baseDir := "/sys/devices/system/cpu"
|
||||||
globPattern := filepath.Join(baseDir, "cpu[0-9]*")
|
globPattern := filepath.Join(baseDir, "cpu[0-9]*")
|
||||||
@ -82,80 +88,95 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
|||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to find any files with pattern %s", globPattern)
|
return fmt.Errorf("CPUFreqCollector.Init() unable to find any files with pattern %s", globPattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
maxPackageID := 0
|
// Initialize CPU topology
|
||||||
maxCoreID := 0
|
m.topology = make([]CPUFreqCollectorTopology, len(cpuDirs))
|
||||||
for _, cpuDir := range cpuDirs {
|
for _, cpuDir := range cpuDirs {
|
||||||
cpuID := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
|
processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
|
||||||
|
processor_int, err := strconv.Atoi(processor)
|
||||||
// Read thread sibling list
|
if err != nil {
|
||||||
threadSiblingListFile := filepath.Join(cpuDir, "topology", "thread_siblings_list")
|
return fmt.Errorf("CPUFreqCollector.Init() unable to convert cpuID to int: %v", err)
|
||||||
threadSiblingList, ok := readOneLine(threadSiblingListFile)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to read thread siblings list from %s", threadSiblingListFile)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read frequency only from first hardware thread
|
// Read package ID
|
||||||
// Ignore Simultaneous Multithreading (SMT) / Hyper-Threading
|
physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
|
||||||
if strings.Split(threadSiblingList, ",")[0] == cpuID {
|
physicalPackageID, ok := readOneLine(physicalPackageIDFile)
|
||||||
// Read package ID
|
if !ok {
|
||||||
packageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
|
return fmt.Errorf("CPUFreqCollector.Init() unable to read physical package ID from %s", physicalPackageIDFile)
|
||||||
packageID, ok := readOneLine(packageIDFile)
|
}
|
||||||
if !ok {
|
physicalPackageID_int, err := strconv.Atoi(physicalPackageID)
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to read physical package ID from %s", packageIDFile)
|
if err != nil {
|
||||||
}
|
return fmt.Errorf("CPUFreqCollector.Init() unable to convert packageID to int: %v", err)
|
||||||
packageID_int, err := strconv.Atoi(packageID)
|
}
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to convert packageID to int: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update maxPackageID
|
// Read core ID
|
||||||
if packageID_int > maxPackageID {
|
coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
|
||||||
maxPackageID = packageID_int
|
coreID, ok := readOneLine(coreIDFile)
|
||||||
}
|
if !ok {
|
||||||
|
return fmt.Errorf("CPUFreqCollector.Init() unable to read core ID from %s", coreIDFile)
|
||||||
|
}
|
||||||
|
coreID_int, err := strconv.Atoi(coreID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("CPUFreqCollector.Init() unable to convert coreID to int: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Read core ID
|
// Check access to current frequency file
|
||||||
coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
|
scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
|
||||||
coreID, ok := readOneLine(coreIDFile)
|
err = unix.Access(scalingCurFreqFile, unix.R_OK)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to read core ID from %s", coreIDFile)
|
return fmt.Errorf("CPUFreqCollector.Init() unable to access %s: %v", scalingCurFreqFile, err)
|
||||||
}
|
}
|
||||||
coreID_int, err := strconv.Atoi(coreID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to convert coreID to int: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update maxCoreID
|
t := &m.topology[processor_int]
|
||||||
if coreID_int > maxCoreID {
|
t.processor = processor
|
||||||
maxCoreID = coreID_int
|
t.physicalPackageID = physicalPackageID
|
||||||
}
|
t.physicalPackageID_int = physicalPackageID_int
|
||||||
|
t.coreID = coreID
|
||||||
|
t.coreID_int = coreID_int
|
||||||
|
t.scalingCurFreqFile = scalingCurFreqFile
|
||||||
|
}
|
||||||
|
|
||||||
// Check access to current frequency file
|
// is processor a hyperthread?
|
||||||
scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
|
coreSeenBefore := make(map[string]bool)
|
||||||
err = unix.Access(scalingCurFreqFile, unix.R_OK)
|
for i := range m.topology {
|
||||||
if err != nil {
|
t := &m.topology[i]
|
||||||
return fmt.Errorf("CPUFreqCollector.Init() unable to access %s: %v", scalingCurFreqFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.cpus = append(
|
globalID := t.physicalPackageID + ":" + t.coreID
|
||||||
m.cpus,
|
t.isHT = coreSeenBefore[globalID]
|
||||||
CPUFreqCollectorCPU{
|
coreSeenBefore[globalID] = true
|
||||||
tagSet: map[string]string{
|
}
|
||||||
"type": "cpu",
|
|
||||||
"type-id": strings.TrimSpace(coreID),
|
// number of non hyper thread cores and packages / sockets
|
||||||
"packageID": strings.TrimSpace(packageID),
|
numNonHT_int := 0
|
||||||
},
|
maxPhysicalPackageID := 0
|
||||||
scalingCurFreqFile: scalingCurFreqFile,
|
for i := range m.topology {
|
||||||
})
|
t := &m.topology[i]
|
||||||
|
|
||||||
|
// Update maxPackageID
|
||||||
|
if t.physicalPackageID_int > maxPhysicalPackageID {
|
||||||
|
maxPhysicalPackageID = t.physicalPackageID_int
|
||||||
|
}
|
||||||
|
|
||||||
|
if !t.isHT {
|
||||||
|
numNonHT_int++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add num packages and num cores as tags
|
numPhysicalPackageID_int := maxPhysicalPackageID + 1
|
||||||
numPackages := strconv.Itoa(maxPackageID + 1)
|
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
|
||||||
numCores := strconv.Itoa(maxCoreID + 1)
|
numNonHT := fmt.Sprint(numNonHT_int)
|
||||||
for i := range m.cpus {
|
for i := range m.topology {
|
||||||
c := &m.cpus[i]
|
t := &m.topology[i]
|
||||||
c.tagSet["num_core"] = numCores
|
t.numPhysicalPackages = numPhysicalPackageID
|
||||||
c.tagSet["num_package"] = numPackages
|
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.physicalPackageID,
|
||||||
|
"num_package": t.numPhysicalPackages,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.init = true
|
m.init = true
|
||||||
@ -168,13 +189,18 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric)
|
|||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for i := range m.cpus {
|
for i := range m.topology {
|
||||||
cpu := &m.cpus[i]
|
t := &m.topology[i]
|
||||||
|
|
||||||
|
// skip hyperthreads
|
||||||
|
if t.isHT {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Read current frequency
|
// Read current frequency
|
||||||
line, ok := readOneLine(cpu.scalingCurFreqFile)
|
line, ok := readOneLine(t.scalingCurFreqFile)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("CPUFreqCollector.Read(): Failed to read one line from file '%s'", cpu.scalingCurFreqFile)
|
log.Printf("CPUFreqCollector.Read(): Failed to read one line from file '%s'", t.scalingCurFreqFile)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cpuFreq, err := strconv.Atoi(line)
|
cpuFreq, err := strconv.Atoi(line)
|
||||||
@ -183,7 +209,7 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
y, err := lp.New("cpufreq", cpu.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, now)
|
y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, now)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
output <- y
|
output <- y
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user