-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvidiasmi.go
182 lines (155 loc) · 3.76 KB
/
nvidiasmi.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
package main
import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
metric "google.golang.org/genproto/googleapis/api/metric"
)
type nvidiasmiQuery struct {
Name string
DisplayName string
Kind metric.MetricDescriptor_MetricKind
Type metric.MetricDescriptor_ValueType
Unit string
}
var (
// Units format from https://ucum.org/ucum.html
nvidiasmiQueries = []nvidiasmiQuery{
{
Name: "temperature.gpu",
DisplayName: "Temperature GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "1/{degres C}",
},
{
Name: "utilization.gpu",
DisplayName: "Utilization GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "%",
},
{
Name: "utilization.memory",
DisplayName: "Utilization Memory GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "%",
},
{
Name: "memory.total",
DisplayName: "Memory Total GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "MiBy",
},
{
Name: "memory.free",
DisplayName: "Memory Free GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "MiBy",
},
{
Name: "memory.used",
DisplayName: "Memory Used GPU",
Kind: metric.MetricDescriptor_GAUGE,
Type: metric.MetricDescriptor_INT64,
Unit: "MiBy",
},
}
)
func (q *nvidiasmiQuery) gcpFormat() string {
return strings.ReplaceAll(q.Name, ".", "_")
}
const (
queryFormat string = "-u --format=csv,noheader"
)
func getGPUAmount() (int, error) {
o, err := exec.Command("/bin/sh",
"-c",
"nvidia-smi --query-gpu=index "+queryFormat,
).Output()
if err != nil {
return 0, fmt.Errorf("%s - %s", err.Error(), string(o))
}
amount := len(strings.Split(string(o), "\n")) - 1
if amount == 0 {
return 0, errors.New("Can't fetch metrics on 0 GPUs")
}
return amount, nil
}
func getGPUbusID(id int) (string, error) {
o, err := exec.Command("/bin/sh",
"-c",
fmt.Sprintf("nvidia-smi --query-gpu=pci.bus_id --id=%d "+queryFormat,
id),
).Output()
if err != nil {
return "", fmt.Errorf("%s - %s", err.Error(), string(o))
}
return strings.Split(string(o), "\n")[0], nil
}
func getGPUMetric(query string, id int) (int64, string, error) {
var cmd string
if id >= 0 {
cmd = fmt.Sprintf("nvidia-smi --id=%d --query-gpu=%s "+queryFormat,
id, query)
} else {
cmd = fmt.Sprintf("nvidia-smi --query-gpu=%s "+queryFormat,
query)
}
o, err := exec.Command("/bin/sh",
"-c",
cmd,
).Output()
if err != nil {
return 0, "", err
}
lines := strings.Split(string(o), "\n")
amount := int64(len(lines) - 1)
if amount == 0 {
return 0, "", errors.New("Can't fetch metrics on 0 GPUs")
}
//delete last blank line
lines = lines[:amount]
sumValues := int64(0)
unit := ""
for _, line := range lines {
elem := strings.Split(line, " ")
if len(elem) >= 2 {
unit = elem[1]
}
v, err := strconv.ParseInt(elem[0], 10, 64)
if err != nil {
v = 0
}
sumValues += v
}
value := sumValues / amount
return value, unit, nil
}
func isNvidiasmiExist() error {
o, err := exec.Command("/bin/sh",
"-c",
"nvidia-smi --list-gpus",
).CombinedOutput()
if err != nil {
return fmt.Errorf("%s - %s", err.Error(), string(o))
}
return nil
}
// enablePMNvidiasmi aims to enable persistence mod on nvidia smi
// to prevent 100% gpu usage on one GPU at each query
func enablePMNvidiasmi() error {
o, err := exec.Command("/bin/sh",
"-c",
"sudo nvidia-smi -pm 1",
).Output()
if err != nil {
return fmt.Errorf("%s - %s", err.Error(), string(o))
}
return nil
}