forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintel_powerstat_test.go
494 lines (412 loc) · 17.2 KB
/
intel_powerstat_test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// +build linux
package intel_powerstat
import (
"errors"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil"
)
func TestInitPlugin(t *testing.T) {
cores := []string{"cpu0", "cpu1", "cpu2", "cpu3"}
power, fsMock, _, _ := getPowerWithMockedServices()
fsMock.On("getCPUInfoStats", mock.Anything).
Return(nil, errors.New("error getting cpu stats")).Once()
require.Error(t, power.Init())
fsMock.On("getCPUInfoStats", mock.Anything).
Return(make(map[string]*cpuInfo), nil).Once()
require.Error(t, power.Init())
fsMock.On("getCPUInfoStats", mock.Anything).
Return(map[string]*cpuInfo{"0": {
vendorID: "GenuineIntel",
cpuFamily: "test",
}}, nil).Once()
require.Error(t, power.Init())
fsMock.On("getStringsMatchingPatternOnPath", mock.Anything).
Return(cores, nil).Once().
On("getCPUInfoStats", mock.Anything).
Return(map[string]*cpuInfo{"0": {
vendorID: "GenuineIntel",
cpuFamily: "6",
}}, nil)
// Verify MSR service initialization.
power.cpuFrequency = true
require.NoError(t, power.Init())
fsMock.AssertCalled(t, "getStringsMatchingPatternOnPath", mock.Anything)
require.Equal(t, len(cores), len(power.msr.getCPUCoresData()))
fsMock.On("getStringsMatchingPatternOnPath", mock.Anything).
Return(nil, errors.New("error during getStringsMatchingPatternOnPath")).Once()
// In case of an error when fetching cpu cores plugin should proceed with execution.
require.NoError(t, power.Init())
fsMock.AssertCalled(t, "getStringsMatchingPatternOnPath", mock.Anything)
require.Equal(t, 0, len(power.msr.getCPUCoresData()))
}
func TestParseCPUMetricsConfig(t *testing.T) {
power, _, _, _ := getPowerWithMockedServices()
disableCoreMetrics(power)
power.CPUMetrics = []string{
"cpu_frequency", "cpu_c1_state_residency", "cpu_c6_state_residency", "cpu_busy_cycles", "cpu_temperature",
"cpu_busy_frequency",
}
power.parseCPUMetricsConfig()
verifyCoreMetrics(t, power, true)
disableCoreMetrics(power)
verifyCoreMetrics(t, power, false)
power.CPUMetrics = []string{}
power.parseCPUMetricsConfig()
power.CPUMetrics = []string{"cpu_c6_state_residency", "#@$sdkjdfsdf3@", "1pu_c1_state_residency"}
power.parseCPUMetricsConfig()
require.Equal(t, false, power.cpuC1StateResidency)
require.Equal(t, true, power.cpuC6StateResidency)
disableCoreMetrics(power)
verifyCoreMetrics(t, power, false)
power.CPUMetrics = []string{"#@$sdkjdfsdf3@", "1pu_c1_state_residency", "123"}
power.parseCPUMetricsConfig()
verifyCoreMetrics(t, power, false)
}
func verifyCoreMetrics(t *testing.T, power *PowerStat, enabled bool) {
require.Equal(t, enabled, power.cpuFrequency)
require.Equal(t, enabled, power.cpuC1StateResidency)
require.Equal(t, enabled, power.cpuC6StateResidency)
require.Equal(t, enabled, power.cpuBusyCycles)
require.Equal(t, enabled, power.cpuBusyFrequency)
require.Equal(t, enabled, power.cpuTemperature)
}
func TestGather(t *testing.T) {
var acc testutil.Accumulator
packageIDs := []string{"0", "1"}
coreIDs := []string{"0", "1", "2", "3"}
socketCurrentEnergy := 13213852.2
dramCurrentEnergy := 784552.0
preparedCPUData := getPreparedCPUData(coreIDs)
raplDataMap := prepareRaplDataMap(packageIDs, socketCurrentEnergy, dramCurrentEnergy)
power, _, raplMock, msrMock := getPowerWithMockedServices()
prepareCPUInfo(power, coreIDs, packageIDs)
enableCoreMetrics(power)
power.skipFirstIteration = false
raplMock.On("initializeRaplData", mock.Anything).
On("getRaplData").Return(raplDataMap).
On("retrieveAndCalculateData", mock.Anything).Return(nil).Times(len(raplDataMap)).
On("getConstraintMaxPowerWatts", mock.Anything).Return(546783852.3, nil)
msrMock.On("getCPUCoresData").Return(preparedCPUData).
On("openAndReadMsr", mock.Anything).Return(nil).
On("retrieveCPUFrequencyForCore", mock.Anything).Return(1200000.2, nil)
require.NoError(t, power.Gather(&acc))
// Number of global metrics : 3
// Number of per core metrics : 6
require.Equal(t, 3*len(packageIDs)+6*len(coreIDs), len(acc.GetTelegrafMetrics()))
}
func TestAddGlobalMetricsNegative(t *testing.T) {
var acc testutil.Accumulator
socketCurrentEnergy := 13213852.2
dramCurrentEnergy := 784552.0
raplDataMap := prepareRaplDataMap([]string{"0", "1"}, socketCurrentEnergy, dramCurrentEnergy)
power, _, raplMock, _ := getPowerWithMockedServices()
power.skipFirstIteration = false
raplMock.On("initializeRaplData", mock.Anything).Once().
On("getRaplData").Return(raplDataMap).Once().
On("retrieveAndCalculateData", mock.Anything).Return(errors.New("error while calculating data")).Times(len(raplDataMap))
power.addGlobalMetrics(&acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
raplMock.AssertNumberOfCalls(t, "retrieveAndCalculateData", len(raplDataMap))
raplMock.On("initializeRaplData", mock.Anything).Once().
On("getRaplData").Return(make(map[string]*raplData)).Once()
power.addGlobalMetrics(&acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
raplMock.AssertNotCalled(t, "retrieveAndCalculateData")
raplMock.On("initializeRaplData", mock.Anything).Once().
On("getRaplData").Return(raplDataMap).
On("retrieveAndCalculateData", mock.Anything).Return(nil).Once().
On("retrieveAndCalculateData", mock.Anything).Return(errors.New("error while calculating data")).Once().
On("getConstraintMaxPowerWatts", mock.Anything).Return(12313851.5, nil).Twice()
power.addGlobalMetrics(&acc)
require.Equal(t, 3, len(acc.GetTelegrafMetrics()))
}
func TestAddGlobalMetricsPositive(t *testing.T) {
var acc testutil.Accumulator
socketCurrentEnergy := 3644574.4
dramCurrentEnergy := 124234872.5
raplDataMap := prepareRaplDataMap([]string{"0", "1"}, socketCurrentEnergy, dramCurrentEnergy)
maxPower := 546783852.9
power, _, raplMock, _ := getPowerWithMockedServices()
power.skipFirstIteration = false
raplMock.On("initializeRaplData", mock.Anything).
On("getRaplData").Return(raplDataMap).
On("retrieveAndCalculateData", mock.Anything).Return(nil).Times(len(raplDataMap)).
On("getConstraintMaxPowerWatts", mock.Anything).Return(maxPower, nil).Twice().
On("getCurrentDramPowerConsumption", mock.Anything).Return(dramCurrentEnergy)
power.addGlobalMetrics(&acc)
require.Equal(t, 6, len(acc.GetTelegrafMetrics()))
expectedResults := getGlobalMetrics(maxPower, socketCurrentEnergy, dramCurrentEnergy)
for _, test := range expectedResults {
acc.AssertContainsTaggedFields(t, "powerstat_package", test.fields, test.tags)
}
}
func TestAddMetricsForSingleCoreNegative(t *testing.T) {
var wg sync.WaitGroup
var acc testutil.Accumulator
core := "0"
power, _, _, msrMock := getPowerWithMockedServices()
msrMock.On("openAndReadMsr", core).Return(errors.New("error reading MSR file")).Once()
// Skip generating metric for CPU frequency.
power.cpuFrequency = false
wg.Add(1)
power.addMetricsForSingleCore(core, &acc, &wg)
wg.Wait()
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
}
func TestAddCPUFrequencyMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
frequency := 1200000.2
power, _, _, msrMock := getPowerWithMockedServices()
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
msrMock.On("retrieveCPUFrequencyForCore", mock.Anything).
Return(float64(0), errors.New("error on reading file")).Once()
power.addCPUFrequencyMetric(cpuID, &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
msrMock.On("retrieveCPUFrequencyForCore", mock.Anything).Return(frequency, nil).Once()
power.addCPUFrequencyMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedFrequency := roundFloatToNearestTwoDecimalPlaces(frequency)
expectedMetric := getPowerCoreMetric("cpu_frequency_mhz", expectedFrequency, coreID, packageID, cpuID)
acc.AssertContainsTaggedFields(t, "powerstat_core", expectedMetric.fields, expectedMetric.tags)
}
func TestAddCoreCPUTemperatureMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
power, _, _, msrMock := getPowerWithMockedServices()
preparedData := getPreparedCPUData([]string{cpuID})
expectedTemp := preparedData[cpuID].throttleTemp - preparedData[cpuID].temp
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
msrMock.On("getCPUCoresData").Return(preparedData).Once()
power.addCPUTemperatureMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedMetric := getPowerCoreMetric("cpu_temperature_celsius", expectedTemp, coreID, packageID, cpuID)
acc.AssertContainsTaggedFields(t, "powerstat_core", expectedMetric.fields, expectedMetric.tags)
}
func TestAddC6StateResidencyMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
power, _, _, msrMock := getPowerWithMockedServices()
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
preparedData := getPreparedCPUData([]string{cpuID})
expectedC6 := roundFloatToNearestTwoDecimalPlaces(percentageMultiplier *
float64(preparedData[cpuID].c6Delta) / float64(preparedData[cpuID].timeStampCounterDelta))
msrMock.On("getCPUCoresData").Return(preparedData).Twice()
power.addCPUC6StateResidencyMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedMetric := getPowerCoreMetric("cpu_c6_state_residency_percent", expectedC6, coreID, packageID, cpuID)
acc.AssertContainsTaggedFields(t, "powerstat_core", expectedMetric.fields, expectedMetric.tags)
acc.ClearMetrics()
preparedData[cpuID].timeStampCounterDelta = 0
power.addCPUC6StateResidencyMetric(cpuID, &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
}
func TestAddProcessorBusyCyclesMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
power, _, _, msrMock := getPowerWithMockedServices()
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
preparedData := getPreparedCPUData([]string{cpuID})
expectedBusyCycles := roundFloatToNearestTwoDecimalPlaces(percentageMultiplier * float64(preparedData[cpuID].mperfDelta) /
float64(preparedData[cpuID].timeStampCounterDelta))
msrMock.On("getCPUCoresData").Return(preparedData).Twice()
power.addCPUBusyCyclesMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedMetric := getPowerCoreMetric("cpu_busy_cycles_percent", expectedBusyCycles, coreID, packageID, cpuID)
acc.AssertContainsTaggedFields(t, "powerstat_core", expectedMetric.fields, expectedMetric.tags)
acc.ClearMetrics()
preparedData[cpuID].timeStampCounterDelta = 0
power.addCPUBusyCyclesMetric(cpuID, &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
}
func TestAddProcessorBusyFrequencyMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
power, _, _, msrMock := getPowerWithMockedServices()
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
preparedData := getPreparedCPUData([]string{cpuID})
power.skipFirstIteration = false
msrMock.On("getCPUCoresData").Return(preparedData).Twice()
power.addCPUBusyFrequencyMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
acc.ClearMetrics()
preparedData[cpuID].mperfDelta = 0
power.addCPUBusyFrequencyMetric(cpuID, &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
}
func TestAddC1StateResidencyMetric(t *testing.T) {
var acc testutil.Accumulator
cpuID := "0"
coreID := "2"
packageID := "1"
power, _, _, msrMock := getPowerWithMockedServices()
prepareCPUInfoForSingleCPU(power, cpuID, coreID, packageID)
preparedData := getPreparedCPUData([]string{cpuID})
c1 := preparedData[cpuID].timeStampCounterDelta - preparedData[cpuID].mperfDelta - preparedData[cpuID].c3Delta -
preparedData[cpuID].c6Delta - preparedData[cpuID].c7Delta
expectedC1 := roundFloatToNearestTwoDecimalPlaces(percentageMultiplier * float64(c1) / float64(preparedData[cpuID].timeStampCounterDelta))
msrMock.On("getCPUCoresData").Return(preparedData).Twice()
power.addCPUC1StateResidencyMetric(cpuID, &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedMetric := getPowerCoreMetric("cpu_c1_state_residency_percent", expectedC1, coreID, packageID, cpuID)
acc.AssertContainsTaggedFields(t, "powerstat_core", expectedMetric.fields, expectedMetric.tags)
acc.ClearMetrics()
preparedData[cpuID].timeStampCounterDelta = 0
power.addCPUC1StateResidencyMetric(cpuID, &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
}
func TestAddThermalDesignPowerMetric(t *testing.T) {
var acc testutil.Accumulator
sockets := []string{"0"}
maxPower := 195720672.1
power, _, raplMock, _ := getPowerWithMockedServices()
raplMock.On("getConstraintMaxPowerWatts", mock.Anything).
Return(float64(0), errors.New("getConstraintMaxPowerWatts error")).Once().
On("getConstraintMaxPowerWatts", mock.Anything).Return(maxPower, nil).Once()
power.addThermalDesignPowerMetric(sockets[0], &acc)
require.Equal(t, 0, len(acc.GetTelegrafMetrics()))
power.addThermalDesignPowerMetric(sockets[0], &acc)
require.Equal(t, 1, len(acc.GetTelegrafMetrics()))
expectedTDP := roundFloatToNearestTwoDecimalPlaces(maxPower)
expectedMetric := getPowerGlobalMetric("thermal_design_power_watts", expectedTDP, sockets[0])
acc.AssertContainsTaggedFields(t, "powerstat_package", expectedMetric.fields, expectedMetric.tags)
}
func getPreparedCPUData(cores []string) map[string]*msrData {
msrDataMap := make(map[string]*msrData)
for _, core := range cores {
msrDataMap[core] = &msrData{
mperf: 43079,
aperf: 82001,
timeStampCounter: 15514,
c3: 52829,
c6: 86930,
c7: 25340,
throttleTemp: 88150,
temp: 40827,
mperfDelta: 23515,
aperfDelta: 33866,
timeStampCounterDelta: 13686000,
c3Delta: 20003,
c6Delta: 44518,
c7Delta: 20979,
}
}
return msrDataMap
}
func getGlobalMetrics(maxPower float64, socketCurrentEnergy float64, dramCurrentEnergy float64) []struct {
fields map[string]interface{}
tags map[string]string
} {
return []struct {
fields map[string]interface{}
tags map[string]string
}{
getPowerGlobalMetric("thermal_design_power_watts", roundFloatToNearestTwoDecimalPlaces(maxPower), "0"),
getPowerGlobalMetric("thermal_design_power_watts", roundFloatToNearestTwoDecimalPlaces(maxPower), "1"),
getPowerGlobalMetric("current_power_consumption_watts", roundFloatToNearestTwoDecimalPlaces(socketCurrentEnergy), "0"),
getPowerGlobalMetric("current_power_consumption_watts", roundFloatToNearestTwoDecimalPlaces(socketCurrentEnergy), "1"),
getPowerGlobalMetric("current_dram_power_consumption_watts", roundFloatToNearestTwoDecimalPlaces(dramCurrentEnergy), "0"),
getPowerGlobalMetric("current_dram_power_consumption_watts", roundFloatToNearestTwoDecimalPlaces(dramCurrentEnergy), "1"),
}
}
func getPowerCoreMetric(name string, value interface{}, coreID string, packageID string, cpuID string) struct {
fields map[string]interface{}
tags map[string]string
} {
return getPowerMetric(name, value, map[string]string{"package_id": packageID, "core_id": coreID, "cpu_id": cpuID})
}
func getPowerGlobalMetric(name string, value interface{}, socketID string) struct {
fields map[string]interface{}
tags map[string]string
} {
return getPowerMetric(name, value, map[string]string{"package_id": socketID})
}
func getPowerMetric(name string, value interface{}, tags map[string]string) struct {
fields map[string]interface{}
tags map[string]string
} {
return struct {
fields map[string]interface{}
tags map[string]string
}{
map[string]interface{}{
name: value,
},
tags,
}
}
func prepareCPUInfoForSingleCPU(power *PowerStat, cpuID string, coreID string, packageID string) {
power.cpuInfo = make(map[string]*cpuInfo)
power.cpuInfo[cpuID] = &cpuInfo{
physicalID: packageID,
coreID: coreID,
cpuID: cpuID,
}
}
func prepareCPUInfo(power *PowerStat, coreIDs []string, packageIDs []string) {
power.cpuInfo = make(map[string]*cpuInfo)
currentCPU := 0
for _, packageID := range packageIDs {
for _, coreID := range coreIDs {
cpuID := strconv.Itoa(currentCPU)
power.cpuInfo[cpuID] = &cpuInfo{
physicalID: packageID,
cpuID: cpuID,
coreID: coreID,
}
currentCPU++
}
}
}
func enableCoreMetrics(power *PowerStat) {
power.cpuC1StateResidency = true
power.cpuC6StateResidency = true
power.cpuTemperature = true
power.cpuBusyFrequency = true
power.cpuFrequency = true
power.cpuBusyCycles = true
}
func disableCoreMetrics(power *PowerStat) {
power.cpuC1StateResidency = false
power.cpuC6StateResidency = false
power.cpuTemperature = false
power.cpuBusyFrequency = false
power.cpuFrequency = false
power.cpuBusyCycles = false
}
func prepareRaplDataMap(socketIDs []string, socketCurrentEnergy float64, dramCurrentEnergy float64) map[string]*raplData {
raplDataMap := make(map[string]*raplData, len(socketIDs))
for _, socketID := range socketIDs {
raplDataMap[socketID] = &raplData{
socketCurrentEnergy: socketCurrentEnergy,
dramCurrentEnergy: dramCurrentEnergy,
}
}
return raplDataMap
}
func getPowerWithMockedServices() (*PowerStat, *mockFileService, *mockRaplService, *mockMsrService) {
fsMock := &mockFileService{}
msrMock := &mockMsrService{}
raplMock := &mockRaplService{}
logger := testutil.Logger{Name: "PowerPluginTest"}
p := newPowerStat(fsMock)
p.Log = logger
p.fs = fsMock
p.rapl = raplMock
p.msr = msrMock
return p, fsMock, raplMock, msrMock
}