2022-01-24 13:10:33 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-01-25 15:37:43 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
2022-01-24 13:10:33 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-02-04 14:42:42 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2022-04-02 16:05:52 +02:00
|
|
|
stats "github.com/ClusterCockpit/cc-metric-collector/internal/metricRouter"
|
2022-01-24 13:10:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
// CPUFreqCollector
|
|
|
|
// a metric collector to measure the current frequency of the CPUs
|
|
|
|
// as obtained from /proc/cpuinfo
|
|
|
|
// Only measure on the first hyperthread
|
|
|
|
//
|
|
|
|
type CPUFreqCpuInfoCollectorTopology struct {
|
2022-01-25 09:47:24 +01:00
|
|
|
processor string // logical processor number (continuous, starting at 0)
|
|
|
|
coreID string // socket local core ID
|
2022-02-07 11:12:03 +01:00
|
|
|
coreID_int int64
|
2022-01-25 09:47:24 +01:00
|
|
|
physicalPackageID string // socket / package ID
|
2022-02-07 11:12:03 +01:00
|
|
|
physicalPackageID_int int64
|
2022-01-25 09:47:24 +01:00
|
|
|
numPhysicalPackages string // number of sockets / packages
|
2022-02-07 11:12:03 +01:00
|
|
|
numPhysicalPackages_int int64
|
2022-01-25 09:47:24 +01:00
|
|
|
isHT bool
|
|
|
|
numNonHT string // number of non hyperthreading processors
|
2022-02-07 11:12:03 +01:00
|
|
|
numNonHT_int int64
|
2022-01-25 09:47:24 +01:00
|
|
|
tagSet map[string]string
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type CPUFreqCpuInfoCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-04-02 16:05:52 +02:00
|
|
|
topology []*CPUFreqCpuInfoCollectorTopology
|
|
|
|
statsProcessedMetrics int64
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error {
|
2022-02-07 11:12:03 +01:00
|
|
|
// Check if already initialized
|
|
|
|
if m.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-14 11:19:19 +01:00
|
|
|
m.setup()
|
|
|
|
|
2022-01-24 13:10:33 +01:00
|
|
|
m.name = "CPUFreqCpuInfoCollector"
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{
|
|
|
|
"source": m.name,
|
2022-02-07 11:12:03 +01:00
|
|
|
"group": "CPU",
|
|
|
|
"unit": "MHz",
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-01-24 13:10:33 +01:00
|
|
|
|
|
|
|
const cpuInfoFile = "/proc/cpuinfo"
|
|
|
|
file, err := os.Open(cpuInfoFile)
|
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("failed to open file '%s': %v", cpuInfoFile, err)
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Collect topology information from file cpuinfo
|
|
|
|
foundFreq := false
|
|
|
|
processor := ""
|
2022-02-07 11:12:03 +01:00
|
|
|
var numNonHT_int int64 = 0
|
2022-01-24 13:10:33 +01:00
|
|
|
coreID := ""
|
2022-01-25 09:47:24 +01:00
|
|
|
physicalPackageID := ""
|
2022-02-07 11:12:03 +01:00
|
|
|
var maxPhysicalPackageID int64 = 0
|
2022-02-14 11:19:19 +01:00
|
|
|
m.topology = make([]*CPUFreqCpuInfoCollectorTopology, 0)
|
2022-01-24 13:10:33 +01:00
|
|
|
coreSeenBefore := make(map[string]bool)
|
2022-02-14 11:19:19 +01:00
|
|
|
|
|
|
|
// Read cpuinfo file, line by line
|
2022-01-24 13:10:33 +01:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lineSplit := strings.Split(scanner.Text(), ":")
|
|
|
|
if len(lineSplit) == 2 {
|
|
|
|
key := strings.TrimSpace(lineSplit[0])
|
|
|
|
value := strings.TrimSpace(lineSplit[1])
|
|
|
|
switch key {
|
|
|
|
case "cpu MHz":
|
|
|
|
// frequency
|
|
|
|
foundFreq = true
|
|
|
|
case "processor":
|
|
|
|
processor = value
|
|
|
|
case "core id":
|
|
|
|
coreID = value
|
|
|
|
case "physical id":
|
2022-01-25 09:47:24 +01:00
|
|
|
physicalPackageID = value
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// were all topology information collected?
|
|
|
|
if foundFreq &&
|
|
|
|
len(processor) > 0 &&
|
|
|
|
len(coreID) > 0 &&
|
2022-01-25 09:47:24 +01:00
|
|
|
len(physicalPackageID) > 0 {
|
2022-01-24 13:10:33 +01:00
|
|
|
|
2022-02-14 11:19:19 +01:00
|
|
|
topology := new(CPUFreqCpuInfoCollectorTopology)
|
|
|
|
|
|
|
|
// Processor
|
|
|
|
topology.processor = processor
|
|
|
|
|
|
|
|
// Core ID
|
|
|
|
topology.coreID = coreID
|
|
|
|
topology.coreID_int, err = strconv.ParseInt(coreID, 10, 64)
|
2022-01-25 09:47:24 +01:00
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to convert coreID '%s' to int64: %v", coreID, err)
|
2022-01-25 09:47:24 +01:00
|
|
|
}
|
2022-02-14 11:19:19 +01:00
|
|
|
|
|
|
|
// Physical package ID
|
|
|
|
topology.physicalPackageID = physicalPackageID
|
|
|
|
topology.physicalPackageID_int, err = strconv.ParseInt(physicalPackageID, 10, 64)
|
2022-01-25 09:47:24 +01:00
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err)
|
2022-01-25 09:47:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase maximun socket / package ID, when required
|
2022-02-14 11:19:19 +01:00
|
|
|
if topology.physicalPackageID_int > maxPhysicalPackageID {
|
|
|
|
maxPhysicalPackageID = topology.physicalPackageID_int
|
2022-01-25 09:47:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 11:19:19 +01:00
|
|
|
// is hyperthread?
|
2022-01-25 09:47:24 +01:00
|
|
|
globalID := physicalPackageID + ":" + coreID
|
2022-02-14 11:19:19 +01:00
|
|
|
topology.isHT = coreSeenBefore[globalID]
|
2022-01-24 13:10:33 +01:00
|
|
|
coreSeenBefore[globalID] = true
|
2022-02-14 11:19:19 +01:00
|
|
|
if !topology.isHT {
|
2022-01-24 13:10:33 +01:00
|
|
|
// increase number on non hyper thread cores
|
2022-01-25 09:47:24 +01:00
|
|
|
numNonHT_int++
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// store collected topology information
|
2022-02-14 11:19:19 +01:00
|
|
|
m.topology = append(m.topology, topology)
|
2022-01-24 13:10:33 +01:00
|
|
|
|
|
|
|
// reset topology information
|
|
|
|
foundFreq = false
|
|
|
|
processor = ""
|
|
|
|
coreID = ""
|
2022-01-25 09:47:24 +01:00
|
|
|
physicalPackageID = ""
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 09:47:24 +01:00
|
|
|
numPhysicalPackageID_int := maxPhysicalPackageID + 1
|
|
|
|
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
|
|
|
|
numNonHT := fmt.Sprint(numNonHT_int)
|
2022-02-14 11:19:19 +01:00
|
|
|
for _, t := range m.topology {
|
2022-01-25 09:47:24 +01:00
|
|
|
t.numPhysicalPackages = numPhysicalPackageID
|
|
|
|
t.numPhysicalPackages_int = numPhysicalPackageID_int
|
|
|
|
t.numNonHT = numNonHT
|
|
|
|
t.numNonHT_int = numNonHT_int
|
2022-01-24 13:10:33 +01:00
|
|
|
t.tagSet = map[string]string{
|
2022-02-18 16:59:59 +01:00
|
|
|
"type": "cpu",
|
|
|
|
"type-id": t.processor,
|
|
|
|
"package_id": t.physicalPackageID,
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 16:05:52 +02:00
|
|
|
m.statsProcessedMetrics = 0
|
2022-01-24 13:10:33 +01:00
|
|
|
m.init = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2022-02-07 11:12:03 +01:00
|
|
|
// Check if already initialized
|
2022-01-24 13:10:33 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2022-02-07 11:12:03 +01:00
|
|
|
|
2022-01-24 13:10:33 +01:00
|
|
|
const cpuInfoFile = "/proc/cpuinfo"
|
|
|
|
file, err := os.Open(cpuInfoFile)
|
|
|
|
if err != nil {
|
2022-02-04 14:42:42 +01:00
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
2022-02-07 11:12:03 +01:00
|
|
|
fmt.Sprintf("Read(): Failed to open file '%s': %v", cpuInfoFile, err))
|
2022-01-24 13:10:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
processorCounter := 0
|
|
|
|
now := time.Now()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lineSplit := strings.Split(scanner.Text(), ":")
|
|
|
|
if len(lineSplit) == 2 {
|
|
|
|
key := strings.TrimSpace(lineSplit[0])
|
|
|
|
|
|
|
|
// frequency
|
|
|
|
if key == "cpu MHz" {
|
2022-02-14 11:19:19 +01:00
|
|
|
t := m.topology[processorCounter]
|
2022-01-24 13:10:33 +01:00
|
|
|
if !t.isHT {
|
|
|
|
value, err := strconv.ParseFloat(strings.TrimSpace(lineSplit[1]), 64)
|
|
|
|
if err != nil {
|
2022-02-04 14:42:42 +01:00
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
2022-02-07 11:12:03 +01:00
|
|
|
fmt.Sprintf("Read(): Failed to convert cpu MHz '%s' to float64: %v", lineSplit[1], err))
|
2022-01-24 13:10:33 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-07 11:12:03 +01:00
|
|
|
if y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now); err == nil {
|
2022-04-02 16:05:52 +02:00
|
|
|
m.statsProcessedMetrics++
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
processorCounter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 16:05:52 +02:00
|
|
|
stats.ComponentStatInt(m.name, "processed_metrics", m.statsProcessedMetrics)
|
2022-01-24 13:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CPUFreqCpuInfoCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|