forked from utkuozdemir/nvidia_gpu_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
221 lines (196 loc) · 11.4 KB
/
fields.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
package exporter
import (
"bytes"
"errors"
"fmt"
"os/exec"
"regexp"
"strings"
)
const (
uuidQField qField = "uuid"
nameQField qField = "name"
driverModelCurrentQField qField = "driver_model.current"
driverModelPendingQField qField = "driver_model.pending"
vBiosVersionQField qField = "vbios_version"
driverVersionQField qField = "driver_version"
qFieldsAuto = "AUTO"
DefaultQField = qFieldsAuto
)
var (
ErrNoQueryFields = errors.New("could not extract any query fields")
fieldRegex = regexp.MustCompile(`(?m)\n\s*\n^"([^"]+)"`)
fallbackQFieldToRFieldMap = map[qField]rField{
"timestamp": "timestamp",
"driver_version": "driver_version",
"count": "count",
"name": "name",
"serial": "serial",
"uuid": "uuid",
"pci.bus_id": "pci.bus_id",
"pci.domain": "pci.domain",
"pci.bus": "pci.bus",
"pci.device": "pci.device",
"pci.device_id": "pci.device_id",
"pci.sub_device_id": "pci.sub_device_id",
"pcie.link.gen.current": "pcie.link.gen.current",
"pcie.link.gen.max": "pcie.link.gen.max",
"pcie.link.width.current": "pcie.link.width.current",
"pcie.link.width.max": "pcie.link.width.max",
"index": "index",
"display_mode": "display_mode",
"display_active": "display_active",
"persistence_mode": "persistence_mode",
"accounting.mode": "accounting.mode",
"accounting.buffer_size": "accounting.buffer_size",
"driver_model.current": "driver_model.current",
"driver_model.pending": "driver_model.pending",
"vbios_version": "vbios_version",
"inforom.img": "inforom.img",
"inforom.oem": "inforom.oem",
"inforom.ecc": "inforom.ecc",
"inforom.pwr": "inforom.pwr",
"gom.current": "gom.current",
"gom.pending": "gom.pending",
"fan.speed": "fan.speed [%]",
"pstate": "pstate",
"clocks_throttle_reasons.supported": "clocks_throttle_reasons.supported",
"clocks_throttle_reasons.active": "clocks_throttle_reasons.active",
"clocks_throttle_reasons.gpu_idle": "clocks_throttle_reasons.gpu_idle",
"clocks_throttle_reasons.applications_clocks_setting": "clocks_throttle_reasons.applications_clocks_setting",
"clocks_throttle_reasons.sw_power_cap": "clocks_throttle_reasons.sw_power_cap",
"clocks_throttle_reasons.hw_slowdown": "clocks_throttle_reasons.hw_slowdown",
"clocks_throttle_reasons.hw_thermal_slowdown": "clocks_throttle_reasons.hw_thermal_slowdown",
"clocks_throttle_reasons.hw_power_brake_slowdown": "clocks_throttle_reasons.hw_power_brake_slowdown",
"clocks_throttle_reasons.sw_thermal_slowdown": "clocks_throttle_reasons.sw_thermal_slowdown",
"clocks_throttle_reasons.sync_boost": "clocks_throttle_reasons.sync_boost",
"memory.total": "memory.total [MiB]",
"memory.used": "memory.used [MiB]",
"memory.free": "memory.free [MiB]",
"compute_mode": "compute_mode",
"utilization.gpu": "utilization.gpu [%]",
"utilization.memory": "utilization.memory [%]",
"encoder.stats.sessionCount": "encoder.stats.sessionCount",
"encoder.stats.averageFps": "encoder.stats.averageFps",
"encoder.stats.averageLatency": "encoder.stats.averageLatency",
"ecc.mode.current": "ecc.mode.current",
"ecc.mode.pending": "ecc.mode.pending",
"ecc.errors.corrected.volatile.device_memory": "ecc.errors.corrected.volatile.device_memory",
"ecc.errors.corrected.volatile.dram": "ecc.errors.corrected.volatile.dram",
"ecc.errors.corrected.volatile.register_file": "ecc.errors.corrected.volatile.register_file",
"ecc.errors.corrected.volatile.l1_cache": "ecc.errors.corrected.volatile.l1_cache",
"ecc.errors.corrected.volatile.l2_cache": "ecc.errors.corrected.volatile.l2_cache",
"ecc.errors.corrected.volatile.texture_memory": "ecc.errors.corrected.volatile.texture_memory",
"ecc.errors.corrected.volatile.cbu": "ecc.errors.corrected.volatile.cbu",
"ecc.errors.corrected.volatile.sram": "ecc.errors.corrected.volatile.sram",
"ecc.errors.corrected.volatile.total": "ecc.errors.corrected.volatile.total",
"ecc.errors.corrected.aggregate.device_memory": "ecc.errors.corrected.aggregate.device_memory",
"ecc.errors.corrected.aggregate.dram": "ecc.errors.corrected.aggregate.dram",
"ecc.errors.corrected.aggregate.register_file": "ecc.errors.corrected.aggregate.register_file",
"ecc.errors.corrected.aggregate.l1_cache": "ecc.errors.corrected.aggregate.l1_cache",
"ecc.errors.corrected.aggregate.l2_cache": "ecc.errors.corrected.aggregate.l2_cache",
"ecc.errors.corrected.aggregate.texture_memory": "ecc.errors.corrected.aggregate.texture_memory",
"ecc.errors.corrected.aggregate.cbu": "ecc.errors.corrected.aggregate.cbu",
"ecc.errors.corrected.aggregate.sram": "ecc.errors.corrected.aggregate.sram",
"ecc.errors.corrected.aggregate.total": "ecc.errors.corrected.aggregate.total",
"ecc.errors.uncorrected.volatile.device_memory": "ecc.errors.uncorrected.volatile.device_memory",
"ecc.errors.uncorrected.volatile.dram": "ecc.errors.uncorrected.volatile.dram",
"ecc.errors.uncorrected.volatile.register_file": "ecc.errors.uncorrected.volatile.register_file",
"ecc.errors.uncorrected.volatile.l1_cache": "ecc.errors.uncorrected.volatile.l1_cache",
"ecc.errors.uncorrected.volatile.l2_cache": "ecc.errors.uncorrected.volatile.l2_cache",
"ecc.errors.uncorrected.volatile.texture_memory": "ecc.errors.uncorrected.volatile.texture_memory",
"ecc.errors.uncorrected.volatile.cbu": "ecc.errors.uncorrected.volatile.cbu",
"ecc.errors.uncorrected.volatile.sram": "ecc.errors.uncorrected.volatile.sram",
"ecc.errors.uncorrected.volatile.total": "ecc.errors.uncorrected.volatile.total",
"ecc.errors.uncorrected.aggregate.device_memory": "ecc.errors.uncorrected.aggregate.device_memory",
"ecc.errors.uncorrected.aggregate.dram": "ecc.errors.uncorrected.aggregate.dram",
"ecc.errors.uncorrected.aggregate.register_file": "ecc.errors.uncorrected.aggregate.register_file",
"ecc.errors.uncorrected.aggregate.l1_cache": "ecc.errors.uncorrected.aggregate.l1_cache",
"ecc.errors.uncorrected.aggregate.l2_cache": "ecc.errors.uncorrected.aggregate.l2_cache",
"ecc.errors.uncorrected.aggregate.texture_memory": "ecc.errors.uncorrected.aggregate.texture_memory",
"ecc.errors.uncorrected.aggregate.cbu": "ecc.errors.uncorrected.aggregate.cbu",
"ecc.errors.uncorrected.aggregate.sram": "ecc.errors.uncorrected.aggregate.sram",
"ecc.errors.uncorrected.aggregate.total": "ecc.errors.uncorrected.aggregate.total",
"retired_pages.single_bit_ecc.count": "retired_pages.single_bit_ecc.count",
"retired_pages.double_bit.count": "retired_pages.double_bit.count",
"retired_pages.pending": "retired_pages.pending",
"temperature.gpu": "temperature.gpu",
"temperature.memory": "temperature.memory",
"power.management": "power.management",
"power.draw": "power.draw [W]",
"power.limit": "power.limit [W]",
"enforced.power.limit": "enforced.power.limit [W]",
"power.default_limit": "power.default_limit [W]",
"power.min_limit": "power.min_limit [W]",
"power.max_limit": "power.max_limit [W]",
"clocks.current.graphics": "clocks.current.graphics [MHz]",
"clocks.current.sm": "clocks.current.sm [MHz]",
"clocks.current.memory": "clocks.current.memory [MHz]",
"clocks.current.video": "clocks.current.video [MHz]",
"clocks.applications.graphics": "clocks.applications.graphics [MHz]",
"clocks.applications.memory": "clocks.applications.memory [MHz]",
"clocks.default_applications.graphics": "clocks.default_applications.graphics [MHz]",
"clocks.default_applications.memory": "clocks.default_applications.memory [MHz]",
"clocks.max.graphics": "clocks.max.graphics [MHz]",
"clocks.max.sm": "clocks.max.sm [MHz]",
"clocks.max.memory": "clocks.max.memory [MHz]",
"mig.mode.current": "mig.mode.current",
"mig.mode.pending": "mig.mode.pending",
}
)
func parseAutoQFields(nvidiaSmiCommand string, command runCmd) ([]qField, error) {
cmdAndArgs := strings.Fields(nvidiaSmiCommand)
cmdAndArgs = append(cmdAndArgs, "--help-query-gpu")
cmd := exec.Command(cmdAndArgs[0], cmdAndArgs[1:]...) //nolint:gosec
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := command(cmd)
outStr := stdout.String()
errStr := stdout.String()
exitCode := -1
var exitError *exec.ExitError
if errors.As(err, &exitError) {
exitCode = exitError.ExitCode()
}
if err != nil {
return nil, fmt.Errorf("%w: command failed. code: %d | command: %s | stdout: %s | stderr: %s", err,
exitCode, strings.Join(cmdAndArgs, " "), outStr, errStr)
}
fields := extractQFields(outStr)
if fields == nil {
return nil, fmt.Errorf("%w: code: %d | command: %s | stdout: %s | stderr: %s", ErrNoQueryFields,
exitCode, strings.Join(cmdAndArgs, " "), outStr, errStr)
}
return fields, nil
}
func extractQFields(text string) []qField {
found := fieldRegex.FindAllStringSubmatch(text, -1)
fields := make([]qField, len(found))
for i, ss := range found {
fields[i] = qField(ss[1])
}
return fields
}
func toQFieldSlice(ss []string) []qField {
r := make([]qField, len(ss))
for i, s := range ss {
r[i] = qField(s)
}
return r
}
func toRFieldSlice(ss []string) []rField {
r := make([]rField, len(ss))
for i, s := range ss {
r[i] = rField(s)
}
return r
}
func QFieldSliceToStringSlice(qs []qField) []string {
r := make([]string, len(qs))
for i, q := range qs {
r[i] = string(q)
}
return r
}