cc-metric-collector/collectors/cpufreqMetric.go

215 lines
5.8 KiB
Go
Raw Normal View History

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
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
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 {
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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"`
}
}
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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()
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
}
}
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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": "Hz",
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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-10-09 17:03:38 +02:00
line, err := os.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
}
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-10-09 17:03:38 +02:00
line, err = os.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
}
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{
"type": "hwthread",
2022-02-18 16:59:59 +01:00
"type-id": t.processor,
"package_id": t.physicalPackageID,
2022-01-24 20:22:08 +01:00
}
}
2022-01-21 09:59:57 +01:00
m.init = true
return nil
}
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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-10-09 17:03:38 +02:00
line, err := os.ReadFile(t.scalingCurFreqFile)
if err != nil {
cclog.ComponentError(
m.name,
fmt.Sprintf("Read(): Failed to read file '%s': %v", t.scalingCurFreqFile, err))
2022-01-21 09:59:57 +01:00
continue
}
cpuFreq, err := strconv.ParseInt(strings.TrimSpace(string(line)), 10, 64)
2022-01-21 09:59:57 +01:00
if err != nil {
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 {
Modularize the whole thing (#16) * Use channels, add a metric router, split up configuration and use extended version of Influx line protocol internally * Use central timer for collectors and router. Add expressions to router * Add expression to router config * Update entry points * Start with README * Update README for CCMetric * Formatting * Update README.md * Add README for MultiChanTicker * Add README for MultiChanTicker * Update README.md * Add README to metric router * Update main README * Remove SinkEntity type * Update README for sinks * Update go files * Update README for receivers * Update collectors README * Update collectors README * Use seperate page per collector * Fix for tempstat page * Add docs for customcmd collector * Add docs for ipmistat collector * Add docs for topprocs collector * Update customCmdMetric.md * Use seconds when calculating LIKWID metrics * Add IB metrics ib_recv_pkts and ib_xmit_pkts * Drop domain part of host name * Updated to latest stable version of likwid * Define source code dependencies in Makefile * Add GPFS / IBM Spectrum Scale collector * Add vet and staticcheck make targets * Add vet and staticcheck make targets * Avoid go vet warning: struct field tag `json:"..., omitempty"` not compatible with reflect.StructTag.Get: suspicious space in struct tag value struct field tag `json:"...", omitempty` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces * Add sample collector to README.md * Add CPU frequency collector * Avoid staticcheck warning: redundant return statement * Avoid staticcheck warning: unnecessary assignment to the blank identifier * Simplified code * Add CPUFreqCollectorCpuinfo a metric collector to measure the current frequency of the CPUs as obtained from /proc/cpuinfo Only measure on the first hyperthread * Add collector for NFS clients * Move publication of metrics into Flush() for NatsSink * Update GitHub actions * Refactoring * Avoid vet warning: Println arg list ends with redundant newline * Avoid vet warning struct field commands has json tag but is not exported * Avoid vet warning: return copies lock value. * Corrected typo * Refactoring * Add go sources in internal/... * Bad separator in Makefile * Fix Infiniband collector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
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
}