-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.go
215 lines (190 loc) · 5.16 KB
/
main.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
package main
import (
"flag"
"log"
"net/http"
"strconv"
"sync"
"github.com/mindprince/gonvml"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "nvidia_gpu"
)
var (
addr = flag.String("web.listen-address", ":9445", "Address to listen on for web interface and telemetry.")
labels = []string{"minor_number", "uuid", "name"}
)
type Collector struct {
sync.Mutex
numDevices prometheus.Gauge
usedMemory *prometheus.GaugeVec
totalMemory *prometheus.GaugeVec
dutyCycle *prometheus.GaugeVec
powerUsage *prometheus.GaugeVec
temperature *prometheus.GaugeVec
fanSpeed *prometheus.GaugeVec
}
func NewCollector() *Collector {
return &Collector{
numDevices: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "num_devices",
Help: "Number of GPU devices",
},
),
usedMemory: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_used_bytes",
Help: "Memory used by the GPU device in bytes",
},
labels,
),
totalMemory: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_total_bytes",
Help: "Total memory of the GPU device in bytes",
},
labels,
),
dutyCycle: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "duty_cycle",
Help: "Percent of time over the past sample period during which one or more kernels were executing on the GPU device",
},
labels,
),
powerUsage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "power_usage_milliwatts",
Help: "Power usage of the GPU device in milliwatts",
},
labels,
),
temperature: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "temperature_celsius",
Help: "Temperature of the GPU device in celsius",
},
labels,
),
fanSpeed: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "fanspeed_percent",
Help: "Fanspeed of the GPU device as a percent of its maximum",
},
labels,
),
}
}
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.numDevices.Desc()
c.usedMemory.Describe(ch)
c.totalMemory.Describe(ch)
c.dutyCycle.Describe(ch)
c.powerUsage.Describe(ch)
c.temperature.Describe(ch)
c.fanSpeed.Describe(ch)
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
// Only one Collect call in progress at a time.
c.Lock()
defer c.Unlock()
c.usedMemory.Reset()
c.totalMemory.Reset()
c.dutyCycle.Reset()
c.powerUsage.Reset()
c.temperature.Reset()
c.fanSpeed.Reset()
numDevices, err := gonvml.DeviceCount()
if err != nil {
log.Printf("DeviceCount() error: %v", err)
return
} else {
c.numDevices.Set(float64(numDevices))
ch <- c.numDevices
}
for i := 0; i < int(numDevices); i++ {
dev, err := gonvml.DeviceHandleByIndex(uint(i))
if err != nil {
log.Printf("DeviceHandleByIndex(%d) error: %v", i, err)
continue
}
minorNumber, err := dev.MinorNumber()
if err != nil {
log.Printf("MinorNumber() error: %v", err)
continue
}
minor := strconv.Itoa(int(minorNumber))
uuid, err := dev.UUID()
if err != nil {
log.Printf("UUID() error: %v", err)
continue
}
name, err := dev.Name()
if err != nil {
log.Printf("Name() error: %v", err)
continue
}
totalMemory, usedMemory, err := dev.MemoryInfo()
if err != nil {
log.Printf("MemoryInfo() error: %v", err)
} else {
c.usedMemory.WithLabelValues(minor, uuid, name).Set(float64(usedMemory))
c.totalMemory.WithLabelValues(minor, uuid, name).Set(float64(totalMemory))
}
dutyCycle, _, err := dev.UtilizationRates()
if err != nil {
log.Printf("UtilizationRates() error: %v", err)
} else {
c.dutyCycle.WithLabelValues(minor, uuid, name).Set(float64(dutyCycle))
}
powerUsage, err := dev.PowerUsage()
if err != nil {
log.Printf("PowerUsage() error: %v", err)
} else {
c.powerUsage.WithLabelValues(minor, uuid, name).Set(float64(powerUsage))
}
temperature, err := dev.Temperature()
if err != nil {
log.Printf("Temperature() error: %v", err)
} else {
c.temperature.WithLabelValues(minor, uuid, name).Set(float64(temperature))
}
fanSpeed, err := dev.FanSpeed()
if err != nil {
log.Printf("FanSpeed() error: %v", err)
} else {
c.fanSpeed.WithLabelValues(minor, uuid, name).Set(float64(fanSpeed))
}
}
c.usedMemory.Collect(ch)
c.totalMemory.Collect(ch)
c.dutyCycle.Collect(ch)
c.powerUsage.Collect(ch)
c.temperature.Collect(ch)
c.fanSpeed.Collect(ch)
}
func main() {
flag.Parse()
if err := gonvml.Initialize(); err != nil {
log.Fatalf("Couldn't initialize gonvml: %v. Make sure NVML is in the shared library search path.", err)
}
defer gonvml.Shutdown()
if driverVersion, err := gonvml.SystemDriverVersion(); err != nil {
log.Printf("SystemDriverVersion() error: %v", err)
} else {
log.Printf("SystemDriverVersion(): %v", driverVersion)
}
prometheus.MustRegister(NewCollector())
// Serve on all paths under addr
log.Fatalf("ListenAndServe error: %v", http.ListenAndServe(*addr, promhttp.Handler()))
}