-
Notifications
You must be signed in to change notification settings - Fork 149
/
scheduler.go
251 lines (236 loc) · 10 KB
/
scheduler.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* 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 (
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
/*
* Execute the Slurm sdiag command to read the current statistics
* from the Slurm scheduler. It will be repreatedly called by the
* collector.
*/
// Basic metrics for the scheduler
type SchedulerMetrics struct {
threads float64
queue_size float64
dbd_queue_size float64
last_cycle float64
mean_cycle float64
cycle_per_minute float64
backfill_last_cycle float64
backfill_mean_cycle float64
backfill_depth_mean float64
total_backfilled_jobs_since_start float64
total_backfilled_jobs_since_cycle float64
total_backfilled_heterogeneous float64
}
// Execute the sdiag command and return its output
func SchedulerData() []byte {
cmd := exec.Command("sdiag")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
out, _ := ioutil.ReadAll(stdout)
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
return out
}
// Extract the relevant metrics from the sdiag output
func ParseSchedulerMetrics(input []byte) *SchedulerMetrics {
var sm SchedulerMetrics
lines := strings.Split(string(input), "\n")
// Guard variables to check for string repetitions in the output of sdiag
// (two occurencies of the following strings: 'Last cycle', 'Mean cycle')
lc_count := 0
mc_count := 0
for _, line := range lines {
if strings.Contains(line, ":") {
state := strings.Split(line, ":")[0]
st := regexp.MustCompile(`^Server thread`)
qs := regexp.MustCompile(`^Agent queue`)
dbd := regexp.MustCompile(`^DBD Agent`)
lc := regexp.MustCompile(`^[\s]+Last cycle$`)
mc := regexp.MustCompile(`^[\s]+Mean cycle$`)
cpm := regexp.MustCompile(`^[\s]+Cycles per`)
dpm := regexp.MustCompile(`^[\s]+Depth Mean$`)
tbs := regexp.MustCompile(`^[\s]+Total backfilled jobs \(since last slurm start\)`)
tbc := regexp.MustCompile(`^[\s]+Total backfilled jobs \(since last stats cycle start\)`)
tbh := regexp.MustCompile(`^[\s]+Total backfilled heterogeneous job components`)
switch {
case st.MatchString(state) == true:
sm.threads, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case qs.MatchString(state) == true:
sm.queue_size, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case dbd.MatchString(state) == true:
sm.dbd_queue_size, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case lc.MatchString(state) == true:
if lc_count == 0 {
sm.last_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
lc_count = 1
}
if lc_count == 1 {
sm.backfill_last_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
}
case mc.MatchString(state) == true:
if mc_count == 0 {
sm.mean_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
mc_count = 1
}
if mc_count == 1 {
sm.backfill_mean_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
}
case cpm.MatchString(state) == true:
sm.cycle_per_minute, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case dpm.MatchString(state) == true:
sm.backfill_depth_mean, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case tbs.MatchString(state) == true:
sm.total_backfilled_jobs_since_start, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case tbc.MatchString(state) == true:
sm.total_backfilled_jobs_since_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
case tbh.MatchString(state) == true:
sm.total_backfilled_heterogeneous, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
}
}
}
return &sm
}
// Returns the scheduler metrics
func SchedulerGetMetrics() *SchedulerMetrics {
return ParseSchedulerMetrics(SchedulerData())
}
/*
* Implement the Prometheus Collector interface and feed the
* Slurm scheduler metrics into it.
* https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
*/
// Collector strcture
type SchedulerCollector struct {
threads *prometheus.Desc
queue_size *prometheus.Desc
dbd_queue_size *prometheus.Desc
last_cycle *prometheus.Desc
mean_cycle *prometheus.Desc
cycle_per_minute *prometheus.Desc
backfill_last_cycle *prometheus.Desc
backfill_mean_cycle *prometheus.Desc
backfill_depth_mean *prometheus.Desc
total_backfilled_jobs_since_start *prometheus.Desc
total_backfilled_jobs_since_cycle *prometheus.Desc
total_backfilled_heterogeneous *prometheus.Desc
}
// Send all metric descriptions
func (c *SchedulerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.threads
ch <- c.queue_size
ch <- c.dbd_queue_size
ch <- c.last_cycle
ch <- c.mean_cycle
ch <- c.cycle_per_minute
ch <- c.backfill_last_cycle
ch <- c.backfill_mean_cycle
ch <- c.backfill_depth_mean
ch <- c.total_backfilled_jobs_since_start
ch <- c.total_backfilled_jobs_since_cycle
ch <- c.total_backfilled_heterogeneous
}
// Send the values of all metrics
func (sc *SchedulerCollector) Collect(ch chan<- prometheus.Metric) {
sm := SchedulerGetMetrics()
ch <- prometheus.MustNewConstMetric(sc.threads, prometheus.GaugeValue, sm.threads)
ch <- prometheus.MustNewConstMetric(sc.queue_size, prometheus.GaugeValue, sm.queue_size)
ch <- prometheus.MustNewConstMetric(sc.dbd_queue_size, prometheus.GaugeValue, sm.dbd_queue_size)
ch <- prometheus.MustNewConstMetric(sc.last_cycle, prometheus.GaugeValue, sm.last_cycle)
ch <- prometheus.MustNewConstMetric(sc.mean_cycle, prometheus.GaugeValue, sm.mean_cycle)
ch <- prometheus.MustNewConstMetric(sc.cycle_per_minute, prometheus.GaugeValue, sm.cycle_per_minute)
ch <- prometheus.MustNewConstMetric(sc.backfill_last_cycle, prometheus.GaugeValue, sm.backfill_last_cycle)
ch <- prometheus.MustNewConstMetric(sc.backfill_mean_cycle, prometheus.GaugeValue, sm.backfill_mean_cycle)
ch <- prometheus.MustNewConstMetric(sc.backfill_depth_mean, prometheus.GaugeValue, sm.backfill_depth_mean)
ch <- prometheus.MustNewConstMetric(sc.total_backfilled_jobs_since_start, prometheus.GaugeValue, sm.total_backfilled_jobs_since_start)
ch <- prometheus.MustNewConstMetric(sc.total_backfilled_jobs_since_cycle, prometheus.GaugeValue, sm.total_backfilled_jobs_since_cycle)
ch <- prometheus.MustNewConstMetric(sc.total_backfilled_heterogeneous, prometheus.GaugeValue, sm.total_backfilled_heterogeneous)
}
// Returns the Slurm scheduler collector, used to register with the prometheus client
func NewSchedulerCollector() *SchedulerCollector {
return &SchedulerCollector{
threads: prometheus.NewDesc(
"slurm_scheduler_threads",
"Information provided by the Slurm sdiag command, number of scheduler threads ",
nil,
nil),
queue_size: prometheus.NewDesc(
"slurm_scheduler_queue_size",
"Information provided by the Slurm sdiag command, length of the scheduler queue",
nil,
nil),
dbd_queue_size: prometheus.NewDesc(
"slurm_scheduler_dbd_queue_size",
"Information provided by the Slurm sdiag command, length of the DBD agent queue",
nil,
nil),
last_cycle: prometheus.NewDesc(
"slurm_scheduler_last_cycle",
"Information provided by the Slurm sdiag command, scheduler last cycle time in (microseconds)",
nil,
nil),
mean_cycle: prometheus.NewDesc(
"slurm_scheduler_mean_cycle",
"Information provided by the Slurm sdiag command, scheduler mean cycle time in (microseconds)",
nil,
nil),
cycle_per_minute: prometheus.NewDesc(
"slurm_scheduler_cycle_per_minute",
"Information provided by the Slurm sdiag command, number scheduler cycles per minute",
nil,
nil),
backfill_last_cycle: prometheus.NewDesc(
"slurm_scheduler_backfill_last_cycle",
"Information provided by the Slurm sdiag command, scheduler backfill last cycle time in (microseconds)",
nil,
nil),
backfill_mean_cycle: prometheus.NewDesc(
"slurm_scheduler_backfill_mean_cycle",
"Information provided by the Slurm sdiag command, scheduler backfill mean cycle time in (microseconds)",
nil,
nil),
backfill_depth_mean: prometheus.NewDesc(
"slurm_scheduler_backfill_depth_mean",
"Information provided by the Slurm sdiag command, scheduler backfill mean depth",
nil,
nil),
total_backfilled_jobs_since_start: prometheus.NewDesc(
"slurm_scheduler_backfilled_jobs_since_start_total",
"Information provided by the Slurm sdiag command, number of jobs started thanks to backfilling since last slurm start",
nil,
nil),
total_backfilled_jobs_since_cycle: prometheus.NewDesc(
"slurm_scheduler_backfilled_jobs_since_cycle_total",
"Information provided by the Slurm sdiag command, number of jobs started thanks to backfilling since last time stats where reset",
nil,
nil),
total_backfilled_heterogeneous: prometheus.NewDesc(
"slurm_scheduler_backfilled_heterogeneous_total",
"Information provided by the Slurm sdiag command, number of heterogeneous job components started thanks to backfilling since last Slurm start",
nil,
nil),
}
}