-
Notifications
You must be signed in to change notification settings - Fork 150
/
nvidia_smi.go
145 lines (130 loc) · 3.24 KB
/
nvidia_smi.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
package mpnvidiasmi
import (
"flag"
"fmt"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
"os/exec"
"strconv"
"strings"
)
var queryOptions = []string{
"utilization.gpu",
"utilization.memory",
"temperature.gpu",
"fan.speed",
"memory.total",
"memory.used",
"memory.free",
}
var formatOptions = []string{
"noheader",
"nounits",
"csv",
}
var nvidiaSmiOptions = []string{
fmt.Sprintf("--format=%s", strings.Join(formatOptions, ",")),
fmt.Sprintf("--query-gpu=%s", strings.Join(queryOptions, ",")),
}
var metricsKeyFormats = []string{
"gpu.util.gpu%d",
"memory.util.gpu%d",
"temperature.gpu%d",
"fanspeed.gpu%d",
"memory.usage.gpu%d.total",
"memory.usage.gpu%d.used",
"memory.usage.gpu%d.free",
}
func (n NVidiaSMIPlugin) getMetricKey(index int, gpuIndex int) string {
return fmt.Sprintf(metricsKeyFormats[index], gpuIndex)
}
// NVidiaSMIPlugin mackerel plugin for nvidia-smi
type NVidiaSMIPlugin struct {
Prefix string
}
// GraphDefinition interface for mackerelplugin
func (n NVidiaSMIPlugin) GraphDefinition() map[string]mp.Graphs {
var graphdef = map[string]mp.Graphs{
"gpu.util": {
Label: "GPU Utilization",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "#", Label: "util"},
},
},
"memory.util": {
Label: "GPU Memory Utilization",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "#", Label: "util"},
},
},
"temperature": {
Label: "GPU Temperature",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "#", Label: "temp"},
},
},
"fanspeed": {
Label: "GPU Fan Speed",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "#", Label: "fan speed"},
},
},
"memory.usage.#": {
Label: "GPU Memory Usage",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "total", Label: "total", Scale: 1024 * 1024},
{Name: "used", Label: "used", Scale: 1024 * 1024, Stacked: true},
{Name: "free", Label: "free", Scale: 1024 * 1024, Stacked: true},
},
},
}
return graphdef
}
// FetchMetrics interface for mackerelplugin
func (n NVidiaSMIPlugin) FetchMetrics() (map[string]interface{}, error) {
ret, err := exec.Command("nvidia-smi", nvidiaSmiOptions...).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("%s: %s", err, ret)
}
return n.parseStats(string(ret))
}
// MetricKeyPrefix interface for mackerelplugin
func (n NVidiaSMIPlugin) MetricKeyPrefix() string {
return n.Prefix
}
func (n NVidiaSMIPlugin) parseStats(ret string) (map[string]interface{}, error) {
stats := make(map[string]interface{})
for id, line := range strings.Split(ret, "\n") {
err := n.parseLine(id, line, &stats)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, ret)
}
}
return stats, nil
}
func (n NVidiaSMIPlugin) parseLine(id int, line string, stats *map[string]interface{}) error {
if strings.TrimSpace(line) == "" {
return nil
}
for i, value := range strings.Split(line, ",") {
value, err := strconv.ParseUint(strings.TrimSpace(value), 10, 64)
if err != nil {
continue
}
(*stats)[n.getMetricKey(i, id)] = value
}
return nil
}
// Do the plugin
func Do() {
optPrefix := flag.String("metric-key-prefix", "nvidia.gpu", "Metric key prefix")
flag.Parse()
var plugin NVidiaSMIPlugin
plugin.Prefix = *optPrefix
helper := mp.NewMackerelPlugin(plugin)
helper.Run()
}