2022-01-21 09:59:57 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-09 17:03:38 +02:00
|
|
|
"os"
|
2022-01-21 09:59:57 +01:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-27 17:43:00 +01:00
|
|
|
|
2022-10-10 11:53:11 +02:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
2023-10-02 10:57:50 +02:00
|
|
|
"github.com/ClusterCockpit/cc-metric-collector/pkg/ccTopology"
|
2022-01-21 09:59:57 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
type CPUFreqCollectorTopology struct {
|
2023-10-02 10:57:50 +02: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)
|
2022-11-16 14:58:11 +01:00
|
|
|
// Only measure on the first hyper-thread
|
2022-01-21 09:59:57 +01:00
|
|
|
//
|
|
|
|
// 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-01-24 20:22:08 +01:00
|
|
|
topology []CPUFreqCollectorTopology
|
|
|
|
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()
|
2022-05-13 14:10:39 +02:00
|
|
|
m.parallel = true
|
2022-01-21 09:59:57 +01:00
|
|
|
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",
|
2022-07-12 11:58:37 +02:00
|
|
|
"unit": "Hz",
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-01-21 09:59:57 +01:00
|
|
|
|
2023-10-02 10:57:50 +02:00
|
|
|
m.topology = make([]CPUFreqCollectorTopology, 0)
|
|
|
|
for _, c := range ccTopology.CpuData() {
|
2022-01-24 20:22:08 +01:00
|
|
|
|
2023-10-02 10:57:50 +02:00
|
|
|
// Skip hyper threading CPUs
|
|
|
|
if c.CpuID != c.CoreCPUsList[0] {
|
|
|
|
continue
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:22:08 +01:00
|
|
|
// Check access to current frequency file
|
2023-10-02 10:57:50 +02:00
|
|
|
scalingCurFreqFile := filepath.Join("/sys/devices/system/cpu", fmt.Sprintf("cpu%d", c.CpuID), "cpufreq/scaling_cur_freq")
|
|
|
|
err := unix.Access(scalingCurFreqFile, unix.R_OK)
|
2022-01-24 20:22:08 +01:00
|
|
|
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
|
|
|
|
2023-10-02 10:57:50 +02:00
|
|
|
m.topology = append(m.topology,
|
|
|
|
CPUFreqCollectorTopology{
|
|
|
|
tagSet: map[string]string{
|
|
|
|
"type": "hwthread",
|
|
|
|
"type-id": fmt.Sprint(c.CpuID),
|
|
|
|
"package_id": fmt.Sprint(c.Socket),
|
|
|
|
},
|
|
|
|
scalingCurFreqFile: scalingCurFreqFile,
|
|
|
|
},
|
|
|
|
)
|
2022-01-24 20:22:08 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:58:11 +01:00
|
|
|
// Initialized
|
|
|
|
cclog.ComponentDebug(
|
|
|
|
m.name,
|
|
|
|
"initialized",
|
2023-10-02 10:57:50 +02:00
|
|
|
len(m.topology), "non-hyper-threading CPUs")
|
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]
|
|
|
|
|
2022-01-21 09:59:57 +01:00
|
|
|
// Read current frequency
|
2022-10-09 17:03:38 +02:00
|
|
|
line, err := os.ReadFile(t.scalingCurFreqFile)
|
2022-02-10 09:28:06 +01:00
|
|
|
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-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2022-01-21 09:59:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CPUFreqCollector) Close() {
|
|
|
|
m.init = false
|
|
|
|
}
|