forked from vpenso/prometheus-slurm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition_gpus.go
157 lines (139 loc) · 4.92 KB
/
partition_gpus.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
/* Copyright 2017 Victor Penso, Matteo Dessalvi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package main
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
type PartitionGPUMetrics struct {
gpuAlloc map[string]float64
gpuTotal map[string]float64
gpuIdle map[string]float64
}
func PartitionGetGPUMetrics(longestGRES int, ignore []string, logger log.Logger) (*PartitionGPUMetrics, error) {
gpuData, err := PartitionGPUsData(longestGRES, logger)
if err != nil {
return &PartitionGPUMetrics{}, err
}
return ParsePartitionGPUs(gpuData, ignore), nil
}
func ParsePartitionGPUs(input string, ignore []string) *PartitionGPUMetrics {
var pm PartitionGPUMetrics
gpuAlloc := make(map[string]float64)
gpuTotal := make(map[string]float64)
gpuIdle := make(map[string]float64)
lines := strings.Split(input, "\n")
for _, line := range lines {
data := strings.Fields(line)
if len(data) != 3 {
continue
}
part := data[0]
if sliceContains(ignore, part) {
continue
}
// This is how many GPUs are available on the node
avail := parseGRES(data[1])
// This is how many GPUs are allocated on the node
alloc := parseGRES(data[2])
gpuAlloc[part] += alloc
gpuTotal[part] += avail
gpuIdle[part] += (avail - alloc)
}
pm.gpuAlloc = gpuAlloc
pm.gpuTotal = gpuTotal
pm.gpuIdle = gpuIdle
return &pm
}
func PartitionGPUsData(longestGRES int, logger log.Logger) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*collectorTimeout)*time.Second)
defer cancel()
format := fmt.Sprintf("--Format=partitionname:50,gres:%d,gresused:%d", longestGRES, longestGRES)
cmd := exec.CommandContext(ctx, "sinfo", "-a", "-h", "--Node", format)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
level.Error(logger).Log("msg", "Timeout executing sinfo for GPUs")
return "", ctx.Err()
} else {
level.Error(logger).Log("msg", "Error executing sinfo for GPUs", "err", stderr.String(), "out", stdout.String())
return "", err
}
}
return stdout.String(), nil
}
/*
* Implement the Prometheus Collector interface and feed the
* Slurm scheduler metrics into it.
* https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
*/
func NewPartitionGPUsCollector(longestGRES int, ignore []string, logger log.Logger) *PartitionGPUsCollector {
labels := []string{"partition"}
return &PartitionGPUsCollector{
gpuAlloc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "partition", "gpus_alloc"),
"Number of GPUs allocated in the partition", labels, nil),
gpuTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "partition", "gpus_total"),
"Number of GPUs in the partition", labels, nil),
gpuIdle: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "partition", "gpus_idle"),
"Number of GPUs idle in the partition", labels, nil),
longestGRES: longestGRES,
ignore: ignore,
logger: log.With(logger, "collector", "partition_gpu"),
}
}
type PartitionGPUsCollector struct {
gpuAlloc *prometheus.Desc
gpuTotal *prometheus.Desc
gpuIdle *prometheus.Desc
longestGRES int
ignore []string
logger log.Logger
}
// Send all metric descriptions
func (pc *PartitionGPUsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- pc.gpuAlloc
ch <- pc.gpuTotal
ch <- pc.gpuIdle
}
func (pc *PartitionGPUsCollector) Collect(ch chan<- prometheus.Metric) {
var timeout, errorMetric float64
pm, err := PartitionGetGPUMetrics(pc.longestGRES, pc.ignore, pc.logger)
if err == context.DeadlineExceeded {
timeout = 1
} else if err != nil {
errorMetric = 1
}
for partition, count := range pm.gpuIdle {
ch <- prometheus.MustNewConstMetric(pc.gpuIdle, prometheus.GaugeValue, count, partition)
}
for partition, count := range pm.gpuAlloc {
ch <- prometheus.MustNewConstMetric(pc.gpuAlloc, prometheus.GaugeValue, count, partition)
}
for partition, count := range pm.gpuTotal {
ch <- prometheus.MustNewConstMetric(pc.gpuTotal, prometheus.GaugeValue, count, partition)
}
ch <- prometheus.MustNewConstMetric(collectError, prometheus.GaugeValue, errorMetric, "partition_gpu")
ch <- prometheus.MustNewConstMetric(collecTimeout, prometheus.GaugeValue, timeout, "partition_gpu")
}