cc-metric-collector/collectors/numastatsMetric.go

152 lines
3.2 KiB
Go
Raw Normal View History

2022-02-03 15:02:13 +01:00
package collectors
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
2022-02-03 15:02:13 +01:00
)
// Non-Uniform Memory Access (NUMA) policy hit/miss statistics
2022-02-03 15:02:13 +01:00
//
// numa_hit:
//
// A process wanted to allocate memory from this node, and succeeded.
//
2022-02-03 15:02:13 +01:00
// numa_miss:
//
// A process wanted to allocate memory from another node,
// but ended up with memory from this node.
//
2022-02-03 15:02:13 +01:00
// numa_foreign:
//
// A process wanted to allocate on this node,
// but ended up with memory from another node.
//
2022-02-03 15:02:13 +01:00
// local_node:
//
// A process ran on this node's CPU,
// and got memory from this node.
//
2022-02-03 15:02:13 +01:00
// other_node:
//
// A process ran on a different node's CPU
// and got memory from this node.
//
2022-02-03 15:02:13 +01:00
// interleave_hit:
//
// Interleaving wanted to allocate from this node
// and succeeded.
2022-02-03 15:02:13 +01:00
//
// See: https://www.kernel.org/doc/html/latest/admin-guide/numastat.html
2022-02-03 15:02:13 +01:00
type NUMAStatsCollectorTopolgy struct {
file string
tagSet map[string]string
}
type NUMAStatsCollector struct {
metricCollector
topology []NUMAStatsCollectorTopolgy
}
func (m *NUMAStatsCollector) Init(config json.RawMessage) error {
// Check if already initialized
if m.init {
return nil
}
m.name = "NUMAStatsCollector"
m.parallel = true
2022-02-03 15:02:13 +01:00
m.setup()
m.meta = map[string]string{
"source": m.name,
"group": "NUMA",
}
// Loop for all NUMA node directories
2022-02-14 22:14:06 +01:00
base := "/sys/devices/system/node/node"
globPattern := base + "[0-9]*"
2022-02-03 15:02:13 +01:00
dirs, err := filepath.Glob(globPattern)
if err != nil {
return fmt.Errorf("unable to glob files with pattern '%s'", globPattern)
2022-02-03 15:02:13 +01:00
}
if dirs == nil {
return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
2022-02-03 15:02:13 +01:00
}
m.topology = make([]NUMAStatsCollectorTopolgy, 0, len(dirs))
for _, dir := range dirs {
2022-02-14 22:14:06 +01:00
node := strings.TrimPrefix(dir, base)
2022-02-03 15:02:13 +01:00
file := filepath.Join(dir, "numastat")
m.topology = append(m.topology,
NUMAStatsCollectorTopolgy{
file: file,
2022-02-08 13:40:27 +01:00
tagSet: map[string]string{"memoryDomain": node},
2022-02-03 15:02:13 +01:00
})
}
// Initialized
cclog.ComponentDebug(m.name, "initialized", len(m.topology), "NUMA domains")
2022-02-03 15:02:13 +01:00
m.init = true
return nil
}
func (m *NUMAStatsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
if !m.init {
return
}
for i := range m.topology {
// Loop for all NUMA domains
t := &m.topology[i]
now := time.Now()
file, err := os.Open(t.file)
if err != nil {
cclog.ComponentError(
m.name,
fmt.Sprintf("Read(): Failed to open file '%s': %v", t.file, err))
2022-02-03 15:02:13 +01:00
return
}
scanner := bufio.NewScanner(file)
2022-02-14 22:14:06 +01:00
// Read line by line
2022-02-03 15:02:13 +01:00
for scanner.Scan() {
split := strings.Fields(scanner.Text())
if len(split) != 2 {
continue
}
key := split[0]
value, err := strconv.ParseInt(split[1], 10, 64)
if err != nil {
cclog.ComponentError(
m.name,
fmt.Sprintf("Read(): Failed to convert %s='%s' to int64: %v", key, split[1], err))
2022-02-03 15:02:13 +01:00
continue
}
2022-02-14 22:14:06 +01:00
y, err := lp.New(
"numastats_"+key,
t.tagSet,
m.meta,
map[string]interface{}{"value": value},
now,
)
2022-02-03 15:02:13 +01:00
if err == nil {
output <- y
}
}
file.Close()
}
}
func (m *NUMAStatsCollector) Close() {
m.init = false
}