2022-01-21 09:59:57 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-02-10 09:28:06 +01:00
|
|
|
"io/ioutil"
|
2022-01-21 09:59:57 +01:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-27 17:43:00 +01:00
|
|
|
|
2022-02-04 14:42:42 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-25 15:37:43 +01:00
|
|
|
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-21 09:59:57 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
type CPUFreqCollectorTopology struct {
|
2022-01-24 22:03:13 +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-24 22:03:13 +01:00
|
|
|
physicalPackageID string // socket / package ID
|
2022-02-07 11:12:03 +01:00
|
|
|
physicalPackageID_int int64
|
2022-01-24 22:03:13 +01:00
|
|
|
numPhysicalPackages string // number of sockets / packages
|
2022-02-07 11:12:03 +01:00
|
|
|
numPhysicalPackages_int int64
|
2022-01-24 22:03:13 +01:00
|
|
|
isHT bool
|
|
|
|
numNonHT string // number of non hyperthreading processors
|
2022-02-07 11:12:03 +01:00
|
|
|
numNonHT_int int64
|
2022-01-24 22:03:13 +01:00
|
|
|
scalingCurFreqFile string
|
|
|
|
tagSet map[string]string
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CPUFreqCollector
|
|
|
|
// a metric collector to measure the current frequency of the CPUs
|
|
|
|
// as obtained from the hardware (in KHz)
|
|
|
|
// Only measure on the first hyper thread
|
|
|
|
//
|
|
|
|
// See: https://www.kernel.org/doc/html/latest/admin-guide/pm/cpufreq.html
|
|
|
|
//
|
|
|
|
type CPUFreqCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-04-02 16:05:52 +02:00
|
|
|
topology []CPUFreqCollectorTopology
|
|
|
|
statsProcessedMetrics int64
|
|
|
|
config struct {
|
2022-01-21 09:59:57 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
2022-02-07 11:12:03 +01:00
|
|
|
// Check if already initialized
|
|
|
|
if m.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:59:57 +01:00
|
|
|
m.name = "CPUFreqCollector"
|
|
|
|
m.setup()
|
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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-21 09:59:57 +01:00
|
|
|
|
|
|
|
// Loop for all CPU directories
|
|
|
|
baseDir := "/sys/devices/system/cpu"
|
|
|
|
globPattern := filepath.Join(baseDir, "cpu[0-9]*")
|
|
|
|
cpuDirs, err := filepath.Glob(globPattern)
|
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err)
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
if cpuDirs == nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
// Initialize CPU topology
|
|
|
|
m.topology = make([]CPUFreqCollectorTopology, len(cpuDirs))
|
2022-01-21 09:59:57 +01:00
|
|
|
for _, cpuDir := range cpuDirs {
|
2022-01-24 20:22:08 +01:00
|
|
|
processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
|
2022-02-07 11:12:03 +01:00
|
|
|
processor_int, err := strconv.ParseInt(processor, 10, 64)
|
2022-01-24 20:22:08 +01:00
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to convert cpuID '%s' to int64: %v", processor, err)
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read package ID
|
2022-01-24 22:03:13 +01:00
|
|
|
physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
|
2022-02-10 09:28:06 +01:00
|
|
|
line, err := ioutil.ReadFile(physicalPackageIDFile)
|
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err)
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
2022-02-10 09:28:06 +01:00
|
|
|
physicalPackageID := strings.TrimSpace(string(line))
|
2022-02-07 11:12:03 +01:00
|
|
|
physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64)
|
2022-01-24 20:22:08 +01:00
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to convert packageID '%s' to int64: %v", physicalPackageID, err)
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
2022-01-21 09:59:57 +01:00
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
// Read core ID
|
|
|
|
coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
|
2022-02-10 09:28:06 +01:00
|
|
|
line, err = ioutil.ReadFile(coreIDFile)
|
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to read core ID from file '%s': %v", coreIDFile, err)
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
2022-02-10 09:28:06 +01:00
|
|
|
coreID := strings.TrimSpace(string(line))
|
2022-02-07 11:12:03 +01:00
|
|
|
coreID_int, err := strconv.ParseInt(coreID, 10, 64)
|
2022-01-24 20:22:08 +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-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
// Check access to current frequency file
|
|
|
|
scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
|
|
|
|
err = unix.Access(scalingCurFreqFile, unix.R_OK)
|
|
|
|
if err != nil {
|
2022-03-15 16:38:20 +01:00
|
|
|
return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err)
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
2022-01-24 20:22:08 +01:00
|
|
|
|
|
|
|
t := &m.topology[processor_int]
|
|
|
|
t.processor = processor
|
2022-01-24 22:03:13 +01:00
|
|
|
t.physicalPackageID = physicalPackageID
|
|
|
|
t.physicalPackageID_int = physicalPackageID_int
|
2022-01-24 20:22:08 +01:00
|
|
|
t.coreID = coreID
|
|
|
|
t.coreID_int = coreID_int
|
|
|
|
t.scalingCurFreqFile = scalingCurFreqFile
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
// is processor a hyperthread?
|
|
|
|
coreSeenBefore := make(map[string]bool)
|
|
|
|
for i := range m.topology {
|
|
|
|
t := &m.topology[i]
|
|
|
|
|
2022-01-24 22:03:13 +01:00
|
|
|
globalID := t.physicalPackageID + ":" + t.coreID
|
2022-01-24 20:22:08 +01:00
|
|
|
t.isHT = coreSeenBefore[globalID]
|
|
|
|
coreSeenBefore[globalID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// number of non hyper thread cores and packages / sockets
|
2022-02-07 11:12:03 +01:00
|
|
|
var numNonHT_int int64 = 0
|
|
|
|
var maxPhysicalPackageID int64 = 0
|
2022-01-24 20:22:08 +01:00
|
|
|
for i := range m.topology {
|
|
|
|
t := &m.topology[i]
|
|
|
|
|
|
|
|
// Update maxPackageID
|
2022-01-25 09:47:24 +01:00
|
|
|
if t.physicalPackageID_int > maxPhysicalPackageID {
|
|
|
|
maxPhysicalPackageID = t.physicalPackageID_int
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !t.isHT {
|
|
|
|
numNonHT_int++
|
|
|
|
}
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 09:47:24 +01:00
|
|
|
numPhysicalPackageID_int := maxPhysicalPackageID + 1
|
|
|
|
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
|
2022-01-24 20:22:08 +01:00
|
|
|
numNonHT := fmt.Sprint(numNonHT_int)
|
|
|
|
for i := range m.topology {
|
|
|
|
t := &m.topology[i]
|
2022-01-25 09:47:24 +01:00
|
|
|
t.numPhysicalPackages = numPhysicalPackageID
|
|
|
|
t.numPhysicalPackages_int = numPhysicalPackageID_int
|
2022-01-24 20:22:08 +01:00
|
|
|
t.numNonHT = numNonHT
|
|
|
|
t.numNonHT_int = numNonHT_int
|
|
|
|
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 20:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 16:05:52 +02:00
|
|
|
m.statsProcessedMetrics = 0
|
2022-01-21 09:59:57 +01:00
|
|
|
m.init = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2022-02-07 11:12:03 +01:00
|
|
|
// Check if already initialized
|
2022-01-21 09:59:57 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:31:45 +01:00
|
|
|
now := time.Now()
|
2022-01-24 20:22:08 +01:00
|
|
|
for i := range m.topology {
|
|
|
|
t := &m.topology[i]
|
|
|
|
|
|
|
|
// skip hyperthreads
|
|
|
|
if t.isHT {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-24 11:31:45 +01:00
|
|
|
|
2022-01-21 09:59:57 +01:00
|
|
|
// Read current frequency
|
2022-02-10 09:28:06 +01:00
|
|
|
line, err := ioutil.ReadFile(t.scalingCurFreqFile)
|
|
|
|
if err != nil {
|
2022-02-04 14:42:42 +01:00
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
2022-02-10 09:28:06 +01:00
|
|
|
fmt.Sprintf("Read(): Failed to read file '%s': %v", t.scalingCurFreqFile, err))
|
2022-01-21 09:59:57 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-02-10 09:28:06 +01:00
|
|
|
cpuFreq, err := strconv.ParseInt(strings.TrimSpace(string(line)), 10, 64)
|
2022-01-21 09:59:57 +01:00
|
|
|
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 frequency '%s' to int64: %v", line, err))
|
2022-01-21 09:59:57 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-07 11:12:03 +01:00
|
|
|
if y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, 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-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 16:05:52 +02:00
|
|
|
stats.ComponentStatInt(m.name, "processed_metrics", m.statsProcessedMetrics)
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CPUFreqCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|