mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-26 15:29:04 +01:00
3f76947f54
* Update configuration.md Add an additional receiver to have better alignment of components * Change default GpfsCollector command to `mmpmon` (#53) * Set default cmd to 'mmpmon' * Reuse looked up path * Cast const to string * Just download LIKWID to get the headers (#54) * Just download LIKWID to get the headers * Remove perl-Data-Dumper from BuildRequires, only required by LIKWID build * Add HttpReceiver as counterpart to the HttpSink (#49) * Use GBytes as unit for large memory numbers * Make maxForward configurable, save old name in meta in rename metrics and make the hostname tag key configurable * Single release action (#55) Building all RPMs and releasing in a single workflow * Makefile target to build binary-only Debian packages (#61) * Add 'install' and 'DEB' make targets to build binary-only Debian packages * Add control file for DEB builds * Use a single line for bash loop in make clean * Add config options for retry intervals of InfluxDB clients (#59) * Refactoring of LikwidCollector and metric units (#62) * Reduce complexity of LikwidCollector and allow metric units * Add unit to LikwidCollector docu and fix some typos * Make library path configurable * Use old metric name in Ganglia if rename has happened in the router (#60) * Use old metric name if rename has happened in the router * Also check for Ganglia renames for the oldname * Derived metrics (#57) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Simplified iota usage * Move unit tag to meta data tags * Derived metrics (#65) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values * Update LustreCollector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Meta to tags list and map for sinks (#63) * Change ccMetric->Influx functions * Use a meta_as_tags string list in config but create a lookup map afterwards * Add meta as tag logic to sampleSink * Fix staticcheck warnings (#66) Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
214 lines
5.8 KiB
Go
214 lines
5.8 KiB
Go
package collectors
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
type CPUFreqCollectorTopology struct {
|
|
processor string // logical processor number (continuous, starting at 0)
|
|
coreID string // socket local core ID
|
|
coreID_int int64
|
|
physicalPackageID string // socket / package ID
|
|
physicalPackageID_int int64
|
|
numPhysicalPackages string // number of sockets / packages
|
|
numPhysicalPackages_int int64
|
|
isHT bool
|
|
numNonHT string // number of non hyperthreading processors
|
|
numNonHT_int int64
|
|
scalingCurFreqFile string
|
|
tagSet map[string]string
|
|
}
|
|
|
|
//
|
|
// 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 {
|
|
metricCollector
|
|
topology []CPUFreqCollectorTopology
|
|
config struct {
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
|
}
|
|
}
|
|
|
|
func (m *CPUFreqCollector) Init(config json.RawMessage) error {
|
|
// Check if already initialized
|
|
if m.init {
|
|
return nil
|
|
}
|
|
|
|
m.name = "CPUFreqCollector"
|
|
m.setup()
|
|
if len(config) > 0 {
|
|
err := json.Unmarshal(config, &m.config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
m.meta = map[string]string{
|
|
"source": m.name,
|
|
"group": "CPU",
|
|
"unit": "MHz",
|
|
}
|
|
|
|
// 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 {
|
|
return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err)
|
|
}
|
|
if cpuDirs == nil {
|
|
return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
|
|
}
|
|
|
|
// Initialize CPU topology
|
|
m.topology = make([]CPUFreqCollectorTopology, len(cpuDirs))
|
|
for _, cpuDir := range cpuDirs {
|
|
processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
|
|
processor_int, err := strconv.ParseInt(processor, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to convert cpuID '%s' to int64: %v", processor, err)
|
|
}
|
|
|
|
// Read package ID
|
|
physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
|
|
line, err := ioutil.ReadFile(physicalPackageIDFile)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err)
|
|
}
|
|
physicalPackageID := strings.TrimSpace(string(line))
|
|
physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to convert packageID '%s' to int64: %v", physicalPackageID, err)
|
|
}
|
|
|
|
// Read core ID
|
|
coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
|
|
line, err = ioutil.ReadFile(coreIDFile)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to read core ID from file '%s': %v", coreIDFile, err)
|
|
}
|
|
coreID := strings.TrimSpace(string(line))
|
|
coreID_int, err := strconv.ParseInt(coreID, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to convert coreID '%s' to int64: %v", coreID, err)
|
|
}
|
|
|
|
// Check access to current frequency file
|
|
scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
|
|
err = unix.Access(scalingCurFreqFile, unix.R_OK)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err)
|
|
}
|
|
|
|
t := &m.topology[processor_int]
|
|
t.processor = processor
|
|
t.physicalPackageID = physicalPackageID
|
|
t.physicalPackageID_int = physicalPackageID_int
|
|
t.coreID = coreID
|
|
t.coreID_int = coreID_int
|
|
t.scalingCurFreqFile = scalingCurFreqFile
|
|
}
|
|
|
|
// is processor a hyperthread?
|
|
coreSeenBefore := make(map[string]bool)
|
|
for i := range m.topology {
|
|
t := &m.topology[i]
|
|
|
|
globalID := t.physicalPackageID + ":" + t.coreID
|
|
t.isHT = coreSeenBefore[globalID]
|
|
coreSeenBefore[globalID] = true
|
|
}
|
|
|
|
// number of non hyper thread cores and packages / sockets
|
|
var numNonHT_int int64 = 0
|
|
var maxPhysicalPackageID int64 = 0
|
|
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++
|
|
}
|
|
}
|
|
|
|
numPhysicalPackageID_int := maxPhysicalPackageID + 1
|
|
numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
|
|
numNonHT := fmt.Sprint(numNonHT_int)
|
|
for i := range m.topology {
|
|
t := &m.topology[i]
|
|
t.numPhysicalPackages = numPhysicalPackageID
|
|
t.numPhysicalPackages_int = numPhysicalPackageID_int
|
|
t.numNonHT = numNonHT
|
|
t.numNonHT_int = numNonHT_int
|
|
t.tagSet = map[string]string{
|
|
"type": "cpu",
|
|
"type-id": t.processor,
|
|
"package_id": t.physicalPackageID,
|
|
}
|
|
}
|
|
|
|
m.init = true
|
|
return nil
|
|
}
|
|
|
|
func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
|
// Check if already initialized
|
|
if !m.init {
|
|
return
|
|
}
|
|
|
|
now := time.Now()
|
|
for i := range m.topology {
|
|
t := &m.topology[i]
|
|
|
|
// skip hyperthreads
|
|
if t.isHT {
|
|
continue
|
|
}
|
|
|
|
// Read current frequency
|
|
line, err := ioutil.ReadFile(t.scalingCurFreqFile)
|
|
if err != nil {
|
|
cclog.ComponentError(
|
|
m.name,
|
|
fmt.Sprintf("Read(): Failed to read file '%s': %v", t.scalingCurFreqFile, err))
|
|
continue
|
|
}
|
|
cpuFreq, err := strconv.ParseInt(strings.TrimSpace(string(line)), 10, 64)
|
|
if err != nil {
|
|
cclog.ComponentError(
|
|
m.name,
|
|
fmt.Sprintf("Read(): Failed to convert CPU frequency '%s' to int64: %v", line, err))
|
|
continue
|
|
}
|
|
|
|
if y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, now); err == nil {
|
|
output <- y
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *CPUFreqCollector) Close() {
|
|
m.init = false
|
|
}
|