forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rapl.go
238 lines (204 loc) · 7.94 KB
/
rapl.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
// +build linux
package intel_powerstat
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/influxdata/telegraf"
)
const (
intelRaplPath = "/sys/devices/virtual/powercap/intel-rapl"
intelRaplSocketPartialPath = "%s/intel-rapl:%s"
energyUjPartialPath = "%s/energy_uj"
maxEnergyRangeUjPartialPath = "%s/max_energy_range_uj"
maxPowerUwPartialPath = "%s/constraint_0_max_power_uw"
intelRaplDramPartialPath = "%s/intel-rapl:%s/%s"
intelRaplDramNamePartialPath = "%s/name"
)
// raplService is responsible for interactions with RAPL.
type raplService interface {
initializeRaplData()
getRaplData() map[string]*raplData
retrieveAndCalculateData(socketID string) error
getConstraintMaxPowerWatts(socketID string) (float64, error)
}
type raplServiceImpl struct {
log telegraf.Logger
data map[string]*raplData
dramFolders map[string]string
fs fileService
}
// initializeRaplData looks for RAPL folders and initializes data map with fetched information.
func (r *raplServiceImpl) initializeRaplData() {
r.prepareData()
r.findDramFolders()
}
func (r *raplServiceImpl) getRaplData() map[string]*raplData {
return r.data
}
func (r *raplServiceImpl) retrieveAndCalculateData(socketID string) error {
socketRaplPath := fmt.Sprintf(intelRaplSocketPartialPath, intelRaplPath, socketID)
socketEnergyUjPath := fmt.Sprintf(energyUjPartialPath, socketRaplPath)
socketEnergyUjFile, err := os.Open(socketEnergyUjPath)
if err != nil {
return fmt.Errorf("error opening socket energy_uj file on path %s, err: %v", socketEnergyUjPath, err)
}
defer socketEnergyUjFile.Close()
dramRaplPath := fmt.Sprintf(intelRaplDramPartialPath, intelRaplPath, socketID, r.dramFolders[socketID])
dramEnergyUjPath := fmt.Sprintf(energyUjPartialPath, dramRaplPath)
dramEnergyUjFile, err := os.Open(dramEnergyUjPath)
if err != nil {
return fmt.Errorf("error opening dram energy_uj file on path %s, err: %v", dramEnergyUjPath, err)
}
defer dramEnergyUjFile.Close()
socketMaxEnergyUjPath := fmt.Sprintf(maxEnergyRangeUjPartialPath, socketRaplPath)
socketMaxEnergyUjFile, err := os.Open(socketMaxEnergyUjPath)
if err != nil {
return fmt.Errorf("error opening socket max_energy_range_uj file on path %s, err: %v", socketMaxEnergyUjPath, err)
}
defer socketMaxEnergyUjFile.Close()
dramMaxEnergyUjPath := fmt.Sprintf(maxEnergyRangeUjPartialPath, dramRaplPath)
dramMaxEnergyUjFile, err := os.Open(dramMaxEnergyUjPath)
if err != nil {
return fmt.Errorf("error opening dram max_energy_range_uj file on path %s, err: %v", dramMaxEnergyUjPath, err)
}
defer dramMaxEnergyUjFile.Close()
return r.calculateData(socketID, socketEnergyUjFile, dramEnergyUjFile, socketMaxEnergyUjFile, dramMaxEnergyUjFile)
}
func (r *raplServiceImpl) getConstraintMaxPowerWatts(socketID string) (float64, error) {
socketRaplPath := fmt.Sprintf(intelRaplSocketPartialPath, intelRaplPath, socketID)
socketMaxPowerPath := fmt.Sprintf(maxPowerUwPartialPath, socketRaplPath)
socketMaxPowerFile, err := os.Open(socketMaxPowerPath)
if err != nil {
return 0, fmt.Errorf("error opening constraint_0_max_power_uw file on path %s, err: %v", socketMaxPowerPath, err)
}
defer socketMaxPowerFile.Close()
socketMaxPower, _, err := r.fs.readFileToFloat64(socketMaxPowerFile)
return convertMicroWattToWatt(socketMaxPower), err
}
func (r *raplServiceImpl) prepareData() {
intelRaplPrefix := "intel-rapl:"
intelRapl := fmt.Sprintf("%s%s", intelRaplPrefix, "[0-9]*")
raplPattern := fmt.Sprintf("%s/%s", intelRaplPath, intelRapl)
raplPaths, err := r.fs.getStringsMatchingPatternOnPath(raplPattern)
if err != nil {
r.log.Errorf("error while preparing RAPL data: %v", err)
r.data = make(map[string]*raplData)
return
}
if len(raplPaths) == 0 {
r.log.Debugf("RAPL data wasn't found using pattern: %s", raplPattern)
r.data = make(map[string]*raplData)
return
}
// If RAPL exists initialize data map (if it wasn't initialized before).
if len(r.data) == 0 {
for _, raplPath := range raplPaths {
socketID := strings.TrimPrefix(filepath.Base(raplPath), intelRaplPrefix)
r.data[socketID] = &raplData{
socketCurrentEnergy: 0,
dramCurrentEnergy: 0,
socketEnergy: 0,
dramEnergy: 0,
readDate: 0,
}
}
}
}
func (r *raplServiceImpl) findDramFolders() {
intelRaplPrefix := "intel-rapl:"
intelRaplDram := fmt.Sprintf("%s%s", intelRaplPrefix, "[0-9]*[0-9]*")
// Clean existing map
r.dramFolders = make(map[string]string)
for socketID := range r.data {
path := fmt.Sprintf(intelRaplSocketPartialPath, intelRaplPath, socketID)
raplFoldersPattern := fmt.Sprintf("%s/%s", path, intelRaplDram)
pathsToRaplFolders, err := r.fs.getStringsMatchingPatternOnPath(raplFoldersPattern)
if err != nil {
r.log.Errorf("error during lookup for rapl dram: %v", err)
continue
}
if len(pathsToRaplFolders) == 0 {
r.log.Debugf("RAPL folders weren't found using pattern: %s", raplFoldersPattern)
continue
}
raplFolders := make([]string, 0)
for _, folderPath := range pathsToRaplFolders {
raplFolders = append(raplFolders, filepath.Base(folderPath))
}
r.findDramFolder(raplFolders, socketID)
}
}
func (r *raplServiceImpl) findDramFolder(raplFolders []string, socketID string) {
for _, raplFolder := range raplFolders {
potentialDramPath := fmt.Sprintf(intelRaplDramPartialPath, intelRaplPath, socketID, raplFolder)
nameFilePath := fmt.Sprintf(intelRaplDramNamePartialPath, potentialDramPath)
read, err := r.fs.readFile(nameFilePath)
if err != nil {
r.log.Errorf("error reading file on path: %s, err: %v", nameFilePath, err)
continue
}
// Remove new line character
trimmedString := strings.TrimRight(string(read), "\n")
if trimmedString == "dram" {
// There should be only one DRAM folder per socket
r.dramFolders[socketID] = raplFolder
return
}
}
}
func (r *raplServiceImpl) calculateData(socketID string, socketEnergyUjFile io.Reader, dramEnergyUjFile io.Reader,
socketMaxEnergyUjFile io.Reader, dramMaxEnergyUjFile io.Reader) error {
newSocketEnergy, _, err := r.readEnergyInJoules(socketEnergyUjFile)
if err != nil {
return err
}
newDramEnergy, readDate, err := r.readEnergyInJoules(dramEnergyUjFile)
if err != nil {
return err
}
interval := convertNanoSecondsToSeconds(readDate - r.data[socketID].readDate)
r.data[socketID].readDate = readDate
if interval == 0 {
return fmt.Errorf("interval between last two Telegraf cycles is 0")
}
if newSocketEnergy > r.data[socketID].socketEnergy {
r.data[socketID].socketCurrentEnergy = (newSocketEnergy - r.data[socketID].socketEnergy) / interval
} else {
socketMaxEnergy, _, err := r.readEnergyInJoules(socketMaxEnergyUjFile)
if err != nil {
return err
}
// When socket energy_uj counter reaches maximum value defined in max_energy_range_uj file it
// starts counting from 0.
r.data[socketID].socketCurrentEnergy = (socketMaxEnergy - r.data[socketID].socketEnergy + newSocketEnergy) / interval
}
if newDramEnergy > r.data[socketID].dramEnergy {
r.data[socketID].dramCurrentEnergy = (newDramEnergy - r.data[socketID].dramEnergy) / interval
} else {
dramMaxEnergy, _, err := r.readEnergyInJoules(dramMaxEnergyUjFile)
if err != nil {
return err
}
// When dram energy_uj counter reaches maximum value defined in max_energy_range_uj file it
// starts counting from 0.
r.data[socketID].dramCurrentEnergy = (dramMaxEnergy - r.data[socketID].dramEnergy + newDramEnergy) / interval
}
r.data[socketID].socketEnergy = newSocketEnergy
r.data[socketID].dramEnergy = newDramEnergy
return nil
}
func (r *raplServiceImpl) readEnergyInJoules(reader io.Reader) (float64, int64, error) {
currentEnergy, readDate, err := r.fs.readFileToFloat64(reader)
return convertMicroJoulesToJoules(currentEnergy), readDate, err
}
func newRaplServiceWithFs(logger telegraf.Logger, fs fileService) *raplServiceImpl {
return &raplServiceImpl{
log: logger,
data: make(map[string]*raplData),
dramFolders: make(map[string]string),
fs: fs,
}
}