2021-03-25 14:47:10 +01:00
|
|
|
package collectors
|
2021-03-25 17:47:08 +01:00
|
|
|
|
2021-03-25 14:47:10 +01:00
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I./likwid
|
2022-03-03 13:03:58 +01:00
|
|
|
#cgo LDFLAGS: -Wl,--unresolved-symbols=ignore-in-object-files
|
2021-03-25 14:47:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <likwid.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-03-25 14:47:10 +01:00
|
|
|
"errors"
|
2021-03-25 17:47:08 +01:00
|
|
|
"fmt"
|
2021-11-25 15:11:39 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
2022-02-01 16:01:31 +01:00
|
|
|
"regexp"
|
2021-11-25 15:11:39 +01:00
|
|
|
"strconv"
|
2021-03-25 14:47:10 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
2022-02-01 16:01:31 +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-02-01 16:01:31 +01:00
|
|
|
topo "github.com/ClusterCockpit/cc-metric-collector/internal/ccTopology"
|
2022-02-03 16:52:55 +01:00
|
|
|
agg "github.com/ClusterCockpit/cc-metric-collector/internal/metricAggregator"
|
2022-02-21 13:29:33 +01:00
|
|
|
"github.com/NVIDIA/go-nvml/pkg/dl"
|
2021-03-25 14:47:10 +01:00
|
|
|
)
|
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
type MetricScope string
|
2022-01-25 15:37:43 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
METRIC_SCOPE_HWTHREAD = iota
|
2022-02-01 16:01:31 +01:00
|
|
|
METRIC_SCOPE_CORE
|
|
|
|
METRIC_SCOPE_LLC
|
2022-01-25 15:37:43 +01:00
|
|
|
METRIC_SCOPE_NUMA
|
2022-02-01 16:01:31 +01:00
|
|
|
METRIC_SCOPE_DIE
|
|
|
|
METRIC_SCOPE_SOCKET
|
2022-01-25 15:37:43 +01:00
|
|
|
METRIC_SCOPE_NODE
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ms MetricScope) String() string {
|
2022-02-01 16:01:31 +01:00
|
|
|
return string(ms)
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
func (ms MetricScope) Likwid() string {
|
|
|
|
LikwidDomains := map[string]string{
|
2022-02-08 13:39:58 +01:00
|
|
|
"cpu": "",
|
2022-02-02 14:52:07 +01:00
|
|
|
"core": "",
|
|
|
|
"llc": "C",
|
|
|
|
"numadomain": "M",
|
|
|
|
"die": "D",
|
|
|
|
"socket": "S",
|
|
|
|
"node": "N",
|
|
|
|
}
|
|
|
|
return LikwidDomains[string(ms)]
|
|
|
|
}
|
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
func (ms MetricScope) Granularity() int {
|
2022-02-02 14:52:07 +01:00
|
|
|
for i, g := range GetAllMetricScopes() {
|
|
|
|
if ms == g {
|
2022-02-01 16:01:31 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
func GetAllMetricScopes() []MetricScope {
|
2022-02-08 13:39:58 +01:00
|
|
|
return []MetricScope{"cpu" /*, "core", "llc", "numadomain", "die",*/, "socket", "node"}
|
2022-02-02 14:52:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:29:33 +01:00
|
|
|
const (
|
2022-03-03 13:03:58 +01:00
|
|
|
LIKWID_LIB_NAME = "liblikwid.so"
|
|
|
|
LIKWID_LIB_DL_FLAGS = dl.RTLD_LAZY | dl.RTLD_GLOBAL
|
|
|
|
LIKWID_DEF_ACCESSMODE = "direct"
|
2022-02-21 13:29:33 +01:00
|
|
|
)
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type LikwidCollectorMetricConfig struct {
|
2022-02-03 16:52:55 +01:00
|
|
|
Name string `json:"name"` // Name of the metric
|
|
|
|
Calc string `json:"calc"` // Calculation for the metric using
|
|
|
|
//Aggr string `json:"aggregation"` // if scope unequal to LIKWID metric scope, the values are combined (sum, min, max, mean or avg, median)
|
|
|
|
Scope MetricScope `json:"scope"` // scope for calculation. subscopes are aggregated using the 'aggregation' function
|
2022-02-01 16:01:31 +01:00
|
|
|
Publish bool `json:"publish"`
|
|
|
|
granulatity MetricScope
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type LikwidCollectorEventsetConfig struct {
|
2022-02-01 16:01:31 +01:00
|
|
|
Events map[string]string `json:"events"`
|
|
|
|
granulatity map[string]MetricScope
|
|
|
|
Metrics []LikwidCollectorMetricConfig `json:"metrics"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type LikwidCollectorConfig struct {
|
2021-11-25 15:11:39 +01:00
|
|
|
Eventsets []LikwidCollectorEventsetConfig `json:"eventsets"`
|
2022-02-07 18:35:08 +01:00
|
|
|
Metrics []LikwidCollectorMetricConfig `json:"globalmetrics,omitempty"`
|
|
|
|
ForceOverwrite bool `json:"force_overwrite,omitempty"`
|
2022-02-21 13:29:33 +01:00
|
|
|
InvalidToZero bool `json:"invalid_to_zero,omitempty"`
|
2022-03-03 13:03:58 +01:00
|
|
|
AccessMode string `json:"access_mode,omitempty"`
|
|
|
|
DaemonPath string `json:"accessdaemon_path,omitempty"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:47:10 +01:00
|
|
|
type LikwidCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2022-02-02 14:52:07 +01:00
|
|
|
cpulist []C.int
|
|
|
|
cpu2tid map[int]int
|
|
|
|
sock2tid map[int]int
|
|
|
|
scopeRespTids map[MetricScope]map[int]int
|
|
|
|
metrics map[C.int]map[string]int
|
|
|
|
groups []C.int
|
|
|
|
config LikwidCollectorConfig
|
|
|
|
results map[int]map[int]map[string]interface{}
|
|
|
|
mresults map[int]map[int]map[string]float64
|
|
|
|
gmresults map[int]map[string]float64
|
|
|
|
basefreq float64
|
|
|
|
running bool
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type LikwidMetric struct {
|
2022-02-01 16:01:31 +01:00
|
|
|
name string
|
|
|
|
search string
|
|
|
|
scope MetricScope
|
|
|
|
group_idx int
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
func eventsToEventStr(events map[string]string) string {
|
2021-11-25 15:11:39 +01:00
|
|
|
elist := make([]string, 0)
|
|
|
|
for k, v := range events {
|
|
|
|
elist = append(elist, fmt.Sprintf("%s:%s", v, k))
|
|
|
|
}
|
|
|
|
return strings.Join(elist, ",")
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
func getGranularity(counter, event string) MetricScope {
|
|
|
|
if strings.HasPrefix(counter, "PMC") || strings.HasPrefix(counter, "FIXC") {
|
2022-02-08 13:39:58 +01:00
|
|
|
return "cpu"
|
2022-02-01 16:01:31 +01:00
|
|
|
} else if strings.Contains(counter, "BOX") || strings.Contains(counter, "DEV") {
|
|
|
|
return "socket"
|
|
|
|
} else if strings.HasPrefix(counter, "PWR") {
|
|
|
|
if event == "RAPL_CORE_ENERGY" {
|
2022-02-08 13:39:58 +01:00
|
|
|
return "cpu"
|
2022-02-01 16:01:31 +01:00
|
|
|
} else {
|
|
|
|
return "socket"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
func getBaseFreq() float64 {
|
2021-11-25 15:11:39 +01:00
|
|
|
var freq float64 = math.NaN()
|
|
|
|
C.power_init(0)
|
|
|
|
info := C.get_powerInfo()
|
|
|
|
if float64(info.baseFrequency) != 0 {
|
2022-02-08 13:39:58 +01:00
|
|
|
freq = float64(info.baseFrequency) * 1e3
|
2021-11-25 15:11:39 +01:00
|
|
|
} else {
|
|
|
|
buffer, err := ioutil.ReadFile("/sys/devices/system/cpu/cpu0/cpufreq/bios_limit")
|
|
|
|
if err == nil {
|
|
|
|
data := strings.Replace(string(buffer), "\n", "", -1)
|
|
|
|
x, err := strconv.ParseInt(data, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
freq = float64(x) * 1e3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return freq
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
func (m *LikwidCollector) initGranularity() {
|
2022-02-02 14:52:07 +01:00
|
|
|
splitRegex := regexp.MustCompile("[+-/*()]")
|
2022-02-01 16:01:31 +01:00
|
|
|
for _, evset := range m.config.Eventsets {
|
|
|
|
evset.granulatity = make(map[string]MetricScope)
|
|
|
|
for counter, event := range evset.Events {
|
|
|
|
gran := getGranularity(counter, event)
|
|
|
|
if gran.Granularity() >= 0 {
|
|
|
|
evset.granulatity[counter] = gran
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, metric := range evset.Metrics {
|
2022-02-02 14:52:07 +01:00
|
|
|
s := splitRegex.Split(metric.Calc, -1)
|
2022-02-08 13:39:58 +01:00
|
|
|
gran := MetricScope("cpu")
|
2022-02-01 16:01:31 +01:00
|
|
|
evset.Metrics[i].granulatity = gran
|
|
|
|
for _, x := range s {
|
|
|
|
if _, ok := evset.Events[x]; ok {
|
|
|
|
if evset.granulatity[x].Granularity() > gran.Granularity() {
|
|
|
|
gran = evset.granulatity[x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
evset.Metrics[i].granulatity = gran
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, metric := range m.config.Metrics {
|
2022-02-02 14:52:07 +01:00
|
|
|
s := splitRegex.Split(metric.Calc, -1)
|
2022-02-08 13:39:58 +01:00
|
|
|
gran := MetricScope("cpu")
|
2022-02-01 16:01:31 +01:00
|
|
|
m.config.Metrics[i].granulatity = gran
|
|
|
|
for _, x := range s {
|
|
|
|
for _, evset := range m.config.Eventsets {
|
|
|
|
for _, m := range evset.Metrics {
|
|
|
|
if m.Name == x && m.granulatity.Granularity() > gran.Granularity() {
|
|
|
|
gran = m.granulatity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.config.Metrics[i].granulatity = gran
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
type TopoResolveFunc func(cpuid int) int
|
|
|
|
|
|
|
|
func (m *LikwidCollector) getResponsiblities() map[MetricScope]map[int]int {
|
|
|
|
get_cpus := func(scope MetricScope) map[int]int {
|
|
|
|
var slist []int
|
|
|
|
var cpu C.int
|
|
|
|
var input func(index int) string
|
|
|
|
switch scope {
|
|
|
|
case "node":
|
|
|
|
slist = []int{0}
|
|
|
|
input = func(index int) string { return "N:0" }
|
|
|
|
case "socket":
|
|
|
|
input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) }
|
|
|
|
slist = topo.SocketList()
|
|
|
|
// case "numadomain":
|
|
|
|
// input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) }
|
|
|
|
// slist = topo.NumaNodeList()
|
|
|
|
// cclog.Debug(scope, " ", input(0), " ", slist)
|
|
|
|
// case "die":
|
|
|
|
// input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) }
|
|
|
|
// slist = topo.DieList()
|
|
|
|
// case "llc":
|
|
|
|
// input = fmt.Sprintf("%s%d:0", scope.Likwid(), s)
|
|
|
|
// slist = topo.LLCacheList()
|
2022-02-08 13:39:58 +01:00
|
|
|
case "cpu":
|
|
|
|
input = func(index int) string { return fmt.Sprintf("%d", index) }
|
|
|
|
slist = topo.CpuList()
|
2022-02-02 14:52:07 +01:00
|
|
|
case "hwthread":
|
|
|
|
input = func(index int) string { return fmt.Sprintf("%d", index) }
|
|
|
|
slist = topo.CpuList()
|
|
|
|
}
|
|
|
|
outmap := make(map[int]int)
|
|
|
|
for _, s := range slist {
|
|
|
|
t := C.CString(input(s))
|
|
|
|
clen := C.cpustr_to_cpulist(t, &cpu, 1)
|
|
|
|
if int(clen) == 1 {
|
|
|
|
outmap[s] = m.cpu2tid[int(cpu)]
|
|
|
|
} else {
|
|
|
|
cclog.Error(fmt.Sprintf("Cannot determine responsible CPU for %s", input(s)))
|
|
|
|
outmap[s] = -1
|
|
|
|
}
|
|
|
|
C.free(unsafe.Pointer(t))
|
|
|
|
}
|
|
|
|
return outmap
|
|
|
|
}
|
|
|
|
|
|
|
|
scopes := GetAllMetricScopes()
|
|
|
|
complete := make(map[MetricScope]map[int]int)
|
|
|
|
for _, s := range scopes {
|
|
|
|
complete[s] = get_cpus(s)
|
|
|
|
}
|
|
|
|
return complete
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *LikwidCollector) Init(config json.RawMessage) error {
|
2021-05-18 15:14:11 +02:00
|
|
|
var ret C.int
|
2021-03-25 17:47:08 +01:00
|
|
|
m.name = "LikwidCollector"
|
2022-03-03 13:03:58 +01:00
|
|
|
m.config.AccessMode = LIKWID_DEF_ACCESSMODE
|
2021-11-25 14:04:03 +01:00
|
|
|
if len(config) > 0 {
|
2021-11-25 15:11:39 +01:00
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-02-21 13:29:33 +01:00
|
|
|
lib := dl.New(LIKWID_LIB_NAME, LIKWID_LIB_DL_FLAGS)
|
|
|
|
if lib == nil {
|
|
|
|
return fmt.Errorf("error instantiating DynamicLibrary for %s", LIKWID_LIB_NAME)
|
|
|
|
}
|
2022-03-03 13:03:58 +01:00
|
|
|
err := lib.Open()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening %s: %v", LIKWID_LIB_NAME, err)
|
|
|
|
}
|
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
if m.config.ForceOverwrite {
|
2022-02-02 14:52:07 +01:00
|
|
|
cclog.ComponentDebug(m.name, "Set LIKWID_FORCE=1")
|
2022-02-01 16:01:31 +01:00
|
|
|
os.Setenv("LIKWID_FORCE", "1")
|
|
|
|
}
|
2021-03-25 14:47:10 +01:00
|
|
|
m.setup()
|
2022-02-01 16:01:31 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "PerfCounter"}
|
2022-02-02 14:52:07 +01:00
|
|
|
cclog.ComponentDebug(m.name, "Get cpulist and init maps and lists")
|
2022-02-01 16:01:31 +01:00
|
|
|
cpulist := topo.CpuList()
|
2021-03-25 14:47:10 +01:00
|
|
|
m.cpulist = make([]C.int, len(cpulist))
|
2022-02-02 14:52:07 +01:00
|
|
|
m.cpu2tid = make(map[int]int)
|
2021-03-25 14:47:10 +01:00
|
|
|
for i, c := range cpulist {
|
2021-03-25 17:47:08 +01:00
|
|
|
m.cpulist[i] = C.int(c)
|
2022-02-02 14:52:07 +01:00
|
|
|
m.cpu2tid[c] = i
|
|
|
|
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
m.results = make(map[int]map[int]map[string]interface{})
|
|
|
|
m.mresults = make(map[int]map[int]map[string]float64)
|
|
|
|
m.gmresults = make(map[int]map[string]float64)
|
2022-02-02 14:52:07 +01:00
|
|
|
cclog.ComponentDebug(m.name, "initialize LIKWID topology")
|
2021-05-18 15:14:11 +02:00
|
|
|
ret = C.topology_init()
|
|
|
|
if ret != 0 {
|
2022-02-01 16:01:31 +01:00
|
|
|
err := errors.New("failed to initialize LIKWID topology")
|
|
|
|
cclog.ComponentError(m.name, err.Error())
|
|
|
|
return err
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
|
2022-02-08 13:39:58 +01:00
|
|
|
// Determine which counter works at which level. PMC*: cpu, *BOX*: socket, ...
|
2022-02-02 14:52:07 +01:00
|
|
|
m.initGranularity()
|
|
|
|
// Generate map for MetricScope -> scope_id (like socket id) -> responsible id (offset in cpulist)
|
|
|
|
m.scopeRespTids = m.getResponsiblities()
|
2022-03-03 13:03:58 +01:00
|
|
|
switch m.config.AccessMode {
|
|
|
|
case "direct":
|
|
|
|
C.HPMmode(0)
|
|
|
|
case "accessdaemon":
|
|
|
|
if len(m.config.DaemonPath) > 0 {
|
|
|
|
p := os.Getenv("PATH")
|
|
|
|
os.Setenv("PATH", m.config.DaemonPath+":"+p)
|
|
|
|
}
|
|
|
|
C.HPMmode(1)
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
|
|
|
|
cclog.ComponentDebug(m.name, "initialize LIKWID perfmon module")
|
2021-05-18 15:14:11 +02:00
|
|
|
ret = C.perfmon_init(C.int(len(m.cpulist)), &m.cpulist[0])
|
|
|
|
if ret != 0 {
|
|
|
|
C.topology_finalize()
|
2022-02-01 16:01:31 +01:00
|
|
|
err := errors.New("failed to initialize LIKWID topology")
|
|
|
|
cclog.ComponentError(m.name, err.Error())
|
|
|
|
return err
|
2021-05-18 15:14:11 +02:00
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// This is for the global metrics computation test
|
2022-02-01 16:01:31 +01:00
|
|
|
globalParams := make(map[string]interface{})
|
|
|
|
globalParams["time"] = float64(1.0)
|
|
|
|
globalParams["inverseClock"] = float64(1.0)
|
2022-02-02 14:52:07 +01:00
|
|
|
// While adding the events, we test the metrics whether they can be computed at all
|
2021-11-25 14:04:03 +01:00
|
|
|
for i, evset := range m.config.Eventsets {
|
2021-11-25 15:11:39 +01:00
|
|
|
estr := eventsToEventStr(evset.Events)
|
2022-02-02 14:52:07 +01:00
|
|
|
// Generate parameter list for the metric computing test
|
2022-02-01 16:01:31 +01:00
|
|
|
params := make(map[string]interface{})
|
|
|
|
params["time"] = float64(1.0)
|
|
|
|
params["inverseClock"] = float64(1.0)
|
2022-02-02 14:52:07 +01:00
|
|
|
for counter := range evset.Events {
|
2022-02-01 16:01:31 +01:00
|
|
|
params[counter] = float64(1.0)
|
|
|
|
}
|
|
|
|
for _, metric := range evset.Metrics {
|
2022-02-02 14:52:07 +01:00
|
|
|
// Try to evaluate the metric
|
2022-02-03 16:52:55 +01:00
|
|
|
_, err := agg.EvalFloat64Condition(metric.Calc, params)
|
2022-02-01 16:01:31 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// If the metric is not in the parameter list for the global metrics, add it
|
2022-02-01 16:01:31 +01:00
|
|
|
if _, ok := globalParams[metric.Name]; !ok {
|
|
|
|
globalParams[metric.Name] = float64(1.0)
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// Now we add the list of events to likwid
|
2021-11-25 15:11:39 +01:00
|
|
|
cstr := C.CString(estr)
|
|
|
|
gid := C.perfmon_addEventSet(cstr)
|
|
|
|
if gid >= 0 {
|
|
|
|
m.groups = append(m.groups, gid)
|
|
|
|
}
|
|
|
|
C.free(unsafe.Pointer(cstr))
|
|
|
|
m.results[i] = make(map[int]map[string]interface{})
|
|
|
|
m.mresults[i] = make(map[int]map[string]float64)
|
2022-01-21 15:20:53 +01:00
|
|
|
for tid := range m.cpulist {
|
2021-11-25 15:11:39 +01:00
|
|
|
m.results[i][tid] = make(map[string]interface{})
|
|
|
|
m.mresults[i][tid] = make(map[string]float64)
|
2022-02-02 14:52:07 +01:00
|
|
|
if i == 0 {
|
|
|
|
m.gmresults[tid] = make(map[string]float64)
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2021-03-25 17:47:08 +01:00
|
|
|
}
|
2022-02-01 16:01:31 +01:00
|
|
|
for _, metric := range m.config.Metrics {
|
2022-02-02 14:52:07 +01:00
|
|
|
// Try to evaluate the global metric
|
2022-02-03 16:52:55 +01:00
|
|
|
_, err := agg.EvalFloat64Condition(metric.Calc, globalParams)
|
2022-02-01 16:01:31 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// If no event set could be added, shut down LikwidCollector
|
2021-05-18 15:14:11 +02:00
|
|
|
if len(m.groups) == 0 {
|
|
|
|
C.perfmon_finalize()
|
|
|
|
C.topology_finalize()
|
2022-02-01 16:01:31 +01:00
|
|
|
err := errors.New("no LIKWID performance group initialized")
|
|
|
|
cclog.ComponentError(m.name, err.Error())
|
|
|
|
return err
|
2021-05-18 15:14:11 +02:00
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
m.basefreq = getBaseFreq()
|
2022-02-08 13:39:58 +01:00
|
|
|
cclog.ComponentDebug(m.name, "BaseFreq", m.basefreq)
|
2021-05-18 15:14:11 +02:00
|
|
|
m.init = true
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// take a measurement for 'interval' seconds of event set index 'group'
|
2022-02-01 16:01:31 +01:00
|
|
|
func (m *LikwidCollector) takeMeasurement(group int, interval time.Duration) error {
|
2021-11-25 14:04:03 +01:00
|
|
|
var ret C.int
|
2022-02-01 16:01:31 +01:00
|
|
|
gid := m.groups[group]
|
|
|
|
ret = C.perfmon_setupCounters(gid)
|
|
|
|
if ret != 0 {
|
|
|
|
gctr := C.GoString(C.perfmon_getGroupName(gid))
|
2022-02-21 13:29:33 +01:00
|
|
|
err := fmt.Errorf("failed to setup performance group %d (%s)", gid, gctr)
|
2022-02-01 16:01:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
ret = C.perfmon_startCounters()
|
|
|
|
if ret != 0 {
|
|
|
|
gctr := C.GoString(C.perfmon_getGroupName(gid))
|
2022-02-21 13:29:33 +01:00
|
|
|
err := fmt.Errorf("failed to start performance group %d (%s)", gid, gctr)
|
2022-02-01 16:01:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.running = true
|
|
|
|
time.Sleep(interval)
|
|
|
|
m.running = false
|
|
|
|
ret = C.perfmon_stopCounters()
|
|
|
|
if ret != 0 {
|
|
|
|
gctr := C.GoString(C.perfmon_getGroupName(gid))
|
2022-02-21 13:29:33 +01:00
|
|
|
err := fmt.Errorf("failed to stop performance group %d (%s)", gid, gctr)
|
2022-02-01 16:01:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// Get all measurement results for an event set, derive the metric values out of the measurement results and send it
|
|
|
|
func (m *LikwidCollector) calcEventsetMetrics(group int, interval time.Duration, output chan lp.CCMetric) error {
|
2022-02-01 16:01:31 +01:00
|
|
|
var eidx C.int
|
|
|
|
evset := m.config.Eventsets[group]
|
|
|
|
gid := m.groups[group]
|
2022-02-08 13:39:58 +01:00
|
|
|
invClock := float64(1.0 / m.basefreq)
|
2022-02-02 14:52:07 +01:00
|
|
|
|
|
|
|
// Go over events and get the results
|
|
|
|
for eidx = 0; int(eidx) < len(evset.Events); eidx++ {
|
|
|
|
ctr := C.perfmon_getCounterName(gid, eidx)
|
|
|
|
ev := C.perfmon_getEventName(gid, eidx)
|
|
|
|
gctr := C.GoString(ctr)
|
|
|
|
gev := C.GoString(ev)
|
|
|
|
// MetricScope for the counter (and if needed the event)
|
|
|
|
scope := getGranularity(gctr, gev)
|
|
|
|
// Get the map scope-id -> tids
|
|
|
|
// This way we read less counters like only the responsible hardware thread for a socket
|
|
|
|
scopemap := m.scopeRespTids[scope]
|
|
|
|
for _, tid := range scopemap {
|
|
|
|
if tid >= 0 {
|
|
|
|
m.results[group][tid]["time"] = interval.Seconds()
|
2022-02-08 13:39:58 +01:00
|
|
|
m.results[group][tid]["inverseClock"] = invClock
|
2022-02-02 14:52:07 +01:00
|
|
|
res := C.perfmon_getLastResult(gid, eidx, C.int(tid))
|
|
|
|
m.results[group][tid][gctr] = float64(res)
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-01 16:01:31 +01:00
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go over the event set metrics, derive the value out of the event:counter values and send it
|
|
|
|
for _, metric := range evset.Metrics {
|
|
|
|
// The metric scope is determined in the Init() function
|
|
|
|
// Get the map scope-id -> tids
|
|
|
|
scopemap := m.scopeRespTids[metric.Scope]
|
|
|
|
for domain, tid := range scopemap {
|
|
|
|
if tid >= 0 {
|
2022-02-03 16:52:55 +01:00
|
|
|
value, err := agg.EvalFloat64Condition(metric.Calc, m.results[group][tid])
|
2022-02-02 14:52:07 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.mresults[group][tid][metric.Name] = value
|
2022-02-21 13:29:33 +01:00
|
|
|
if m.config.InvalidToZero && math.IsNaN(value) {
|
|
|
|
value = 0.0
|
|
|
|
}
|
|
|
|
if m.config.InvalidToZero && math.IsInf(value, 0) {
|
2022-02-07 18:35:08 +01:00
|
|
|
value = 0.0
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// Now we have the result, send it with the proper tags
|
2022-02-08 13:39:58 +01:00
|
|
|
if !math.IsNaN(value) {
|
|
|
|
if metric.Publish {
|
|
|
|
tags := map[string]string{"type": metric.Scope.String()}
|
|
|
|
if metric.Scope != "node" {
|
|
|
|
tags["type-id"] = fmt.Sprintf("%d", domain)
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{"value": value}
|
|
|
|
y, err := lp.New(metric.Name, tags, m.meta, fields, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
2022-02-07 17:41:35 +01:00
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
|
2022-02-01 16:01:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// Go over the global metrics, derive the value out of the event sets' metric values and send it
|
|
|
|
func (m *LikwidCollector) calcGlobalMetrics(interval time.Duration, output chan lp.CCMetric) error {
|
2021-11-25 14:04:03 +01:00
|
|
|
for _, metric := range m.config.Metrics {
|
2022-02-02 14:52:07 +01:00
|
|
|
scopemap := m.scopeRespTids[metric.Scope]
|
|
|
|
for domain, tid := range scopemap {
|
|
|
|
if tid >= 0 {
|
|
|
|
// Here we generate parameter list
|
|
|
|
params := make(map[string]interface{})
|
|
|
|
for j := range m.groups {
|
|
|
|
for mname, mres := range m.mresults[j][tid] {
|
|
|
|
params[mname] = mres
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Evaluate the metric
|
2022-02-03 16:52:55 +01:00
|
|
|
value, err := agg.EvalFloat64Condition(metric.Calc, params)
|
2022-02-02 14:52:07 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.gmresults[tid][metric.Name] = value
|
2022-02-21 13:29:33 +01:00
|
|
|
if m.config.InvalidToZero && math.IsNaN(value) {
|
|
|
|
value = 0.0
|
|
|
|
}
|
|
|
|
if m.config.InvalidToZero && math.IsInf(value, 0) {
|
2022-02-07 18:35:08 +01:00
|
|
|
value = 0.0
|
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// Now we have the result, send it with the proper tags
|
2022-02-08 13:39:58 +01:00
|
|
|
if !math.IsNaN(value) {
|
|
|
|
if metric.Publish {
|
|
|
|
tags := map[string]string{"type": metric.Scope.String()}
|
|
|
|
if metric.Scope != "node" {
|
|
|
|
tags["type-id"] = fmt.Sprintf("%d", domain)
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{"value": value}
|
|
|
|
y, err := lp.New(metric.Name, tags, m.meta, fields, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
output <- y
|
|
|
|
}
|
2022-02-07 17:41:35 +01:00
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-02-01 16:01:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:52:07 +01:00
|
|
|
// main read function taking multiple measurement rounds, each 'interval' seconds long
|
2022-02-01 16:01:31 +01:00
|
|
|
func (m *LikwidCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-02 16:55:15 +01:00
|
|
|
for i := range m.groups {
|
2022-02-01 16:01:31 +01:00
|
|
|
// measure event set 'i' for 'interval' seconds
|
|
|
|
err := m.takeMeasurement(i, interval)
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(m.name, err.Error())
|
2022-02-21 13:29:33 +01:00
|
|
|
return
|
2022-02-01 16:01:31 +01:00
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// read measurements and derive event set metrics
|
|
|
|
m.calcEventsetMetrics(i, interval, output)
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-02 14:52:07 +01:00
|
|
|
// use the event set metrics to derive the global metrics
|
|
|
|
m.calcGlobalMetrics(interval, output)
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LikwidCollector) Close() {
|
2021-05-18 15:14:11 +02:00
|
|
|
if m.init {
|
2022-02-01 16:01:31 +01:00
|
|
|
cclog.ComponentDebug(m.name, "Closing ...")
|
2021-11-25 15:11:39 +01:00
|
|
|
m.init = false
|
2022-02-01 16:01:31 +01:00
|
|
|
if m.running {
|
|
|
|
cclog.ComponentDebug(m.name, "Stopping counters")
|
|
|
|
C.perfmon_stopCounters()
|
|
|
|
}
|
|
|
|
cclog.ComponentDebug(m.name, "Finalize LIKWID perfmon module")
|
2021-05-18 15:14:11 +02:00
|
|
|
C.perfmon_finalize()
|
2022-02-01 16:01:31 +01:00
|
|
|
cclog.ComponentDebug(m.name, "Finalize LIKWID topology module")
|
2021-05-18 15:14:11 +02:00
|
|
|
C.topology_finalize()
|
2022-02-01 16:01:31 +01:00
|
|
|
cclog.ComponentDebug(m.name, "Closing done")
|
2021-05-18 15:14:11 +02:00
|
|
|
}
|
2021-03-25 14:47:10 +01:00
|
|
|
}
|