forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
168 lines (149 loc) · 4.46 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// +build !windows
package intel_rdt
import (
"context"
"strings"
"time"
"github.com/influxdata/telegraf"
)
// Publisher for publish new RDT metrics to telegraf accumulator
type Publisher struct {
acc telegraf.Accumulator
Log telegraf.Logger
shortenedMetrics bool
BufferChanProcess chan processMeasurement
BufferChanCores chan string
errChan chan error
}
func NewPublisher(acc telegraf.Accumulator, log telegraf.Logger, shortenedMetrics bool) Publisher {
return Publisher{
acc: acc,
Log: log,
shortenedMetrics: shortenedMetrics,
BufferChanProcess: make(chan processMeasurement),
BufferChanCores: make(chan string),
errChan: make(chan error),
}
}
func (p *Publisher) publish(ctx context.Context) {
go func() {
for {
select {
case newMeasurements := <-p.BufferChanCores:
p.publishCores(newMeasurements)
case newMeasurements := <-p.BufferChanProcess:
p.publishProcess(newMeasurements)
case err := <-p.errChan:
p.Log.Error(err)
case <-ctx.Done():
return
}
}
}()
}
func (p *Publisher) publishCores(measurement string) {
coresString, values, timestamp, err := parseCoresMeasurement(measurement)
if err != nil {
p.errChan <- err
}
p.addToAccumulatorCores(coresString, values, timestamp)
}
func (p *Publisher) publishProcess(measurement processMeasurement) {
process, coresString, values, timestamp, err := parseProcessesMeasurement(measurement)
if err != nil {
p.errChan <- err
}
p.addToAccumulatorProcesses(process, coresString, values, timestamp)
}
func parseCoresMeasurement(measurements string) (string, []float64, time.Time, error) {
var values []float64
timeValue, metricsValues, cores, err := splitCSVLineIntoValues(measurements)
if err != nil {
return "", nil, time.Time{}, err
}
timestamp, err := parseTime(timeValue)
if err != nil {
return "", nil, time.Time{}, err
}
// change string slice to one string and separate it by coma
coresString := strings.Join(cores, ",")
// trim unwanted quotes
coresString = strings.Trim(coresString, "\"")
for _, metric := range metricsValues {
parsedValue, err := parseFloat(metric)
if err != nil {
return "", nil, time.Time{}, err
}
values = append(values, parsedValue)
}
return coresString, values, timestamp, nil
}
func (p *Publisher) addToAccumulatorCores(cores string, metricsValues []float64, timestamp time.Time) {
for i, value := range metricsValues {
if p.shortenedMetrics {
//0: "IPC"
//1: "LLC_Misses"
if i == 0 || i == 1 {
continue
}
}
tags := map[string]string{}
fields := make(map[string]interface{})
tags["cores"] = cores
tags["name"] = pqosMetricOrder[i]
fields["value"] = value
p.acc.AddFields("rdt_metric", fields, tags, timestamp)
}
}
func parseProcessesMeasurement(measurement processMeasurement) (string, string, []float64, time.Time, error) {
var values []float64
timeValue, metricsValues, coreOrPidsValues, pids, err := parseProcessMeasurement(measurement.measurement)
if err != nil {
return "", "", nil, time.Time{}, err
}
timestamp, err := parseTime(timeValue)
if err != nil {
return "", "", nil, time.Time{}, err
}
actualProcess := measurement.name
lenOfPids := len(strings.Split(pids, ","))
cores := coreOrPidsValues[lenOfPids:]
coresString := strings.Trim(strings.Join(cores, ","), `"`)
for _, metric := range metricsValues {
parsedValue, err := parseFloat(metric)
if err != nil {
return "", "", nil, time.Time{}, err
}
values = append(values, parsedValue)
}
return actualProcess, coresString, values, timestamp, nil
}
func (p *Publisher) addToAccumulatorProcesses(process string, cores string, metricsValues []float64, timestamp time.Time) {
for i, value := range metricsValues {
if p.shortenedMetrics {
//0: "IPC"
//1: "LLC_Misses"
if i == 0 || i == 1 {
continue
}
}
tags := map[string]string{}
fields := make(map[string]interface{})
tags["process"] = process
tags["cores"] = cores
tags["name"] = pqosMetricOrder[i]
fields["value"] = value
p.acc.AddFields("rdt_metric", fields, tags, timestamp)
}
}
func parseProcessMeasurement(measurements string) (string, []string, []string, string, error) {
timeValue, metricsValues, coreOrPidsValues, err := splitCSVLineIntoValues(measurements)
if err != nil {
return "", nil, nil, "", err
}
pids, err := findPIDsInMeasurement(measurements)
if err != nil {
return "", nil, nil, "", err
}
return timeValue, metricsValues, coreOrPidsValues, pids, nil
}