cc-metric-collector/collectors/likwidMetric.go

308 lines
7.8 KiB
Go
Raw Normal View History

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
#cgo LDFLAGS: -L./likwid -llikwid -llikwid-hwloc -lm
#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-10-04 15:23:43 +02:00
lp "github.com/influxdata/line-protocol"
2021-11-25 15:11:39 +01:00
"gopkg.in/Knetic/govaluate.v2"
"io/ioutil"
2021-03-25 17:47:08 +01:00
"log"
2021-11-25 15:11:39 +01:00
"math"
"os"
"strconv"
2021-03-25 14:47:10 +01:00
"strings"
"time"
"unsafe"
)
type LikwidCollectorMetricConfig struct {
2021-11-25 15:11:39 +01:00
Name string `json:"name"`
Calc string `json:"calc"`
Socket_scope bool `json:"socket_scope"`
Publish bool `json:"publish"`
}
type LikwidCollectorEventsetConfig struct {
2021-11-25 15:11:39 +01:00
Events map[string]string `json:"events"`
Metrics []LikwidCollectorMetricConfig `json:"metrics"`
}
type LikwidCollectorConfig struct {
2021-11-25 15:11:39 +01:00
Eventsets []LikwidCollectorEventsetConfig `json:"eventsets"`
Metrics []LikwidCollectorMetricConfig `json:"globalmetrics"`
ExcludeMetrics []string `json:"exclude_metrics"`
ForceOverwrite bool `json:"force_overwrite"`
}
2021-03-25 14:47:10 +01:00
type LikwidCollector struct {
MetricCollector
2021-11-25 15:11:39 +01:00
cpulist []C.int
sock2tid 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
2021-11-25 15:11:39 +01:00
basefreq float64
2021-03-25 14:47:10 +01:00
}
type LikwidMetric struct {
2021-03-25 17:47:08 +01:00
name string
search string
socket_scope bool
group_idx int
2021-03-25 14:47:10 +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, ",")
}
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 {
freq = float64(info.baseFrequency)
} 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-03-25 14:47:10 +01:00
func getSocketCpus() map[C.int]int {
2021-03-25 17:47:08 +01:00
slist := SocketList()
var cpu C.int
outmap := make(map[C.int]int)
for _, s := range slist {
t := C.CString(fmt.Sprintf("S%d", s))
clen := C.cpustr_to_cpulist(t, &cpu, 1)
if int(clen) == 1 {
outmap[cpu] = s
}
}
return outmap
2021-03-25 14:47:10 +01:00
}
func (m *LikwidCollector) Init(config []byte) error {
var ret C.int
2021-03-25 17:47:08 +01:00
m.name = "LikwidCollector"
if len(config) > 0 {
2021-11-25 15:11:39 +01:00
err := json.Unmarshal(config, &m.config)
if err != nil {
return err
}
}
2021-03-25 14:47:10 +01:00
m.setup()
cpulist := CpuList()
m.cpulist = make([]C.int, len(cpulist))
slist := getSocketCpus()
2021-03-25 17:47:08 +01:00
2021-03-25 14:47:10 +01:00
m.sock2tid = make(map[int]int)
for i, c := range cpulist {
2021-03-25 17:47:08 +01:00
m.cpulist[i] = C.int(c)
if sid, found := slist[m.cpulist[i]]; found {
m.sock2tid[sid] = i
}
2021-03-25 14:47:10 +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)
ret = C.topology_init()
if ret != 0 {
return errors.New("Failed to initialize LIKWID topology")
}
2021-11-25 15:11:39 +01:00
if m.config.ForceOverwrite {
os.Setenv("LIKWID_FORCE", "1")
}
ret = C.perfmon_init(C.int(len(m.cpulist)), &m.cpulist[0])
if ret != 0 {
C.topology_finalize()
return errors.New("Failed to initialize LIKWID topology")
}
2021-11-25 15:11:39 +01:00
for i, evset := range m.config.Eventsets {
2021-11-25 15:11:39 +01:00
estr := eventsToEventStr(evset.Events)
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)
for tid, _ := range m.cpulist {
m.results[i][tid] = make(map[string]interface{})
m.mresults[i][tid] = make(map[string]float64)
m.gmresults[tid] = make(map[string]float64)
}
2021-03-25 17:47:08 +01:00
}
if len(m.groups) == 0 {
C.perfmon_finalize()
C.topology_finalize()
return errors.New("No LIKWID performance group initialized")
}
m.basefreq = getBaseFreq()
m.init = true
return nil
2021-03-25 14:47:10 +01:00
}
2021-10-04 15:23:43 +02:00
func (m *LikwidCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
if !m.init {
2021-11-25 15:11:39 +01:00
return
}
var ret C.int
2021-11-25 15:11:39 +01:00
for i, gid := range m.groups {
2021-11-25 15:11:39 +01:00
evset := m.config.Eventsets[i]
ret = C.perfmon_setupCounters(gid)
if ret != 0 {
log.Print("Failed to setup performance group ", C.perfmon_getGroupName(gid))
continue
2021-10-04 15:23:43 +02:00
}
ret = C.perfmon_startCounters()
if ret != 0 {
log.Print("Failed to start performance group ", C.perfmon_getGroupName(gid))
continue
}
time.Sleep(interval)
ret = C.perfmon_stopCounters()
if ret != 0 {
log.Print("Failed to stop performance group ", C.perfmon_getGroupName(gid))
continue
2021-03-25 17:47:08 +01:00
}
var eidx C.int
for tid, _ := range m.cpulist {
2021-11-25 15:11:39 +01:00
for eidx = 0; int(eidx) < len(evset.Events); eidx++ {
ctr := C.perfmon_getCounterName(gid, eidx)
gctr := C.GoString(ctr)
res := C.perfmon_getLastResult(gid, eidx, C.int(tid))
m.results[i][tid][gctr] = float64(res)
}
m.results[i][tid]["time"] = float64(interval)
m.results[i][tid]["inverseClock"] = float64(1.0 / m.basefreq)
for _, metric := range evset.Metrics {
expression, err := govaluate.NewEvaluableExpression(metric.Calc)
if err != nil {
log.Print(err.Error())
continue
}
result, err := expression.Evaluate(m.results[i][tid])
if err != nil {
log.Print(err.Error())
continue
}
m.mresults[i][tid][metric.Name] = float64(result.(float64))
}
}
}
2021-11-25 15:11:39 +01:00
for _, metric := range m.config.Metrics {
2021-11-25 15:11:39 +01:00
for tid, _ := range m.cpulist {
var params map[string]interface{}
expression, err := govaluate.NewEvaluableExpression(metric.Calc)
if err != nil {
log.Print(err.Error())
continue
}
params = make(map[string]interface{})
for j, _ := range m.groups {
for mname, mres := range m.mresults[j][tid] {
params[mname] = mres
}
}
result, err := expression.Evaluate(params)
if err != nil {
log.Print(err.Error())
continue
}
m.gmresults[tid][metric.Name] = float64(result.(float64))
}
}
for i, _ := range m.groups {
2021-11-25 15:11:39 +01:00
evset := m.config.Eventsets[i]
for _, metric := range evset.Metrics {
_, skip := stringArrayContains(m.config.ExcludeMetrics, metric.Name)
if metric.Publish && !skip {
if metric.Socket_scope {
for sid, tid := range m.sock2tid {
y, err := lp.New(metric.Name,
map[string]string{"type": "socket", "type-id": fmt.Sprintf("%d", int(sid))},
map[string]interface{}{"value": m.mresults[i][tid][metric.Name]},
time.Now())
if err == nil {
2021-11-25 15:11:39 +01:00
*out = append(*out, y)
}
}
} else {
for tid, cpu := range m.cpulist {
y, err := lp.New(metric.Name,
map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", int(cpu))},
map[string]interface{}{"value": m.mresults[i][tid][metric.Name]},
time.Now())
if err == nil {
*out = append(*out, y)
}
}
}
}
}
2021-03-25 14:47:10 +01:00
}
for _, metric := range m.config.Metrics {
2021-11-25 15:11:39 +01:00
_, skip := stringArrayContains(m.config.ExcludeMetrics, metric.Name)
if metric.Publish && !skip {
if metric.Socket_scope {
for sid, tid := range m.sock2tid {
y, err := lp.New(metric.Name,
map[string]string{"type": "socket", "type-id": fmt.Sprintf("%d", int(sid))},
map[string]interface{}{"value": m.gmresults[tid][metric.Name]},
time.Now())
if err == nil {
2021-11-25 15:11:39 +01:00
*out = append(*out, y)
}
}
} else {
for tid, cpu := range m.cpulist {
y, err := lp.New(metric.Name,
map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", int(cpu))},
map[string]interface{}{"value": m.gmresults[tid][metric.Name]},
time.Now())
if err == nil {
*out = append(*out, y)
}
}
}
}
}
2021-03-25 14:47:10 +01:00
}
func (m *LikwidCollector) Close() {
if m.init {
2021-11-25 15:11:39 +01:00
m.init = false
C.perfmon_finalize()
C.topology_finalize()
}
2021-03-25 17:47:08 +01:00
return
2021-03-25 14:47:10 +01:00
}