bin/slurm-load

filter: Statementstmt 79.2% 134 / 169 Branchbran 51.8% 28 / 54 Conditioncond 55.5% 30 / 54 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 66.9% 217 / 324 CC 54 SCAR 32.9
CC
54
Cov
69%
print_table
132.0
build_rows
15.7
load_squeue
9.4
CRAP 26.9
« lib/NBI/Launcher.pm ↑ Summary bin/runjob »
1 #!/usr/bin/env perl
2 #ABSTRACT: Summarise Slurm workload by combining sinfo node states with squeue job pressure
3 #PODNAME: slurm-load
4  
5 2 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use FindBin qw($RealBin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶if (-e "$RealBin/../dist.ini") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$main::RealBin/../dist.ini") 2 0 2
12 2 ▶ say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 2 2
13 2 ▶ use lib "$RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 }
15  
16 2 ▶use NBI::Slurm;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 my $opt_tab = 0;
19 2 my $opt_user;
20 2 my $opt_help = 0;
21  
22 GetOptions(
23 't|tab' => \$opt_tab,
24 'u|user=s' => \$opt_user,
25 0 ▶ 'version' => sub { say "slurm-load v", $NBI::Slurm::VERSION; exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 2 ▶ 'h|help' => \$opt_help,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("t|tab", \$opt_tab, "u|user=s", \$opt_user, "version", sub { say "slurm-load v", $NBI::Slurm::VERSION; exit 0; } , "h|help", \$opt_help) or usage(1) 2 0 2
27 ) or usage(1);
28  
29 2 ▶usage(0) if $opt_help;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_help 1 1 2
30  
31 1 for my $cmd (qw(sinfo squeue)) {
32 2 ▶ unless (_has_command($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_has_command($cmd)) 0 2 2
33 0 say STDERR RED, "Error: ", RESET, "$cmd not found in PATH. Are you in a Slurm cluster?";
34 0 exit 1;
35 }
36 }
37  
38 1 my $partitions = load_sinfo();
39 1 my $jobs = load_squeue($opt_user);
40 1 my $rows = build_rows($partitions, $jobs);
41  
42 1 ▶if ($opt_tab) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_tab) { } 1 0 1
43 1 print_tsv($rows);
44 } else {
45 0 print_table($rows);
46 }
47  
48 1 exit 0;
49  
50 1 ▶sub load_sinfo {
Subroutine load_sinfo: called CC=7 SCAR=21.1
51 1 my %partitions;
52 1 my ($ok, @lines) = _command_output('sinfo', '--noheader', '--format=%P|%D|%T');
53 1 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
54 0 die "ERROR slurm-load: sinfo failed\n";
55 }
56  
57 1 for my $line (@lines) {
58 5 chomp $line;
59 5 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 5 5
60 5 my ($partition, $nodes, $state) = split /\|/, $line, 3;
61 5 ▶ next unless defined $partition && defined $nodes && defined $state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and defined $nodes and defined $state 0 5 5
Condition33%2/6
defined $partition and defined $nodes and defined $state
!ll&&!rl&&r
005
defined $partition and defined $nodes
!ll&&!rl&&r
005
MC/DC0%0/3
defined $partition and defined $nodes and defined $state
defined $partition defined $nodes defined $state
Truth table25%1/4
A and B and C
A= defined $partitionB= defined $nodesC= defined $state
ABCresult
0XX0
10X0
1100
1111
62  
63 5 $partition =~ s/\*$//;
64 5 my $bucket = normalise_node_state($state);
65  
66 5 $partitions{$partition}{partition} = $partition;
67 5 $partitions{$partition}{total} += $nodes;
68 5 $partitions{$partition}{$bucket} += $nodes;
69 }
70 1 return \%partitions;
71 }
72  
73 1 ▶sub load_squeue {
Subroutine load_squeue: called CC=8 SCAR=22.4
74 1 my ($user) = @_;
75 1 my %jobs;
76  
77 1 my @cmd = ('squeue', '--noheader', '--format=%P|%t|%D');
78 1 ▶ push @cmd, "--user=$user" if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 1 1
79 1 my ($ok, @lines) = _command_output(@cmd);
80 1 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
81 0 die "ERROR slurm-load: squeue failed\n";
82 }
83  
84 1 for my $line (@lines) {
85 3 chomp $line;
86 3 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 3 3
87 3 my ($partition, $state, $nodes) = split /\|/, $line, 3;
88 3 ▶ next unless defined $partition && defined $state && defined $nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and defined $state and defined $nodes 0 3 3
Condition33%2/6
defined $partition and defined $state and defined $nodes
!ll&&!rl&&r
003
defined $partition and defined $state
!ll&&!rl&&r
003
MC/DC0%0/3
defined $partition and defined $state and defined $nodes
defined $partition defined $state defined $nodes
Truth table25%1/4
A and B and C
A= defined $partitionB= defined $stateC= defined $nodes
ABCresult
0XX0
10X0
1100
1111
89  
90 3 $partition =~ s/\*$//;
91 3 my $bucket = normalise_job_state($state);
92  
93 3 $jobs{$partition}{partition} = $partition;
94 3 $jobs{$partition}{"jobs_$bucket"}++;
95 3 $jobs{$partition}{"nodes_$bucket"} += $nodes;
96 }
97  
98 1 return \%jobs;
99 }
100  
101 1 ▶sub build_rows {
Subroutine build_rows: called CC=15 SCAR=27.6
102 1 my ($partitions, $jobs) = @_;
103 1 my @header = qw(Partition Total Up Idle Mix Alloc Down JobsR JobsPD RunNodes PendNodes Load);
104 1 my @rows = (\@header);
105 1 my %totals = (
106 partition => 'TOTAL',
107 total => 0,
108 up => 0,
109 idle => 0,
110 mix => 0,
111 alloc => 0,
112 down => 0,
113 jobs_running => 0,
114 jobs_pending => 0,
115 nodes_running => 0,
116 nodes_pending => 0,
117 );
118  
119 1 for my $partition (sort {
120 1 ▶ load_pct($partitions->{$b}) <=> load_pct($partitions->{$a}) || $a cmp $b
Condition33%1/3
load_pct($partitions->{$b}) <=> load_pct($partitions->{$a}) || $a cmp $b
l!l&&r!l&&!r
010
MC/DC0%0/2
load_pct($partitions->{$b}) <=> load_pct($partitions->{$a}) || $a cmp $b
load_pct($partitions->{$b}) <=> load_pct($partitions->{$a}) $a cmp $b
Truth table33%1/3
A || B
A= load_pct($partitions->{$b}) <=> load_pct($partitions->{$a})B= $a cmp $b
ABresult
1X1
011
000
121 1 } keys %{$partitions}) {
122 2 ▶ my $node = $partitions->{$partition} || {};
Condition50%1/2
$partitions->{$partition} || {}
l!l
20
MC/DC0%0/1
$partitions->{$partition} || {}
$partitions->{$partition}
Truth table50%1/2
A
A= $partitions->{$partition}
Aresult
11
00
123 2 ▶ my $job = $jobs->{$partition} || {};
Condition50%1/2
$jobs->{$partition} || {}
l!l
20
MC/DC0%0/1
$jobs->{$partition} || {}
$jobs->{$partition}
Truth table50%1/2
A
A= $jobs->{$partition}
Aresult
11
00
124  
125 2 ▶ my $total = $node->{total} || 0;
Condition50%1/2
$node->{'total'} || 0
l!l
20
MC/DC0%0/1
$node->{'total'} || 0
$node->{'total'}
Truth table50%1/2
A
A= $node->{'total'}
Aresult
11
00
126 2 ▶ my $down = $node->{down} || 0;
Condition100%2/2
$node->{'down'} || 0
l!l
11
MC/DC100%1/1
$node->{'down'} || 0
$node->{'down'}
Truth table100%2/2
A
A= $node->{'down'}
Aresult
11
00
127 2 ▶ my $idle = $node->{idle} || 0;
Condition50%1/2
$node->{'idle'} || 0
l!l
20
MC/DC0%0/1
$node->{'idle'} || 0
$node->{'idle'}
Truth table50%1/2
A
A= $node->{'idle'}
Aresult
11
00
128 2 ▶ my $mix = $node->{mix} || 0;
Condition100%2/2
$node->{'mix'} || 0
l!l
11
MC/DC100%1/1
$node->{'mix'} || 0
$node->{'mix'}
Truth table100%2/2
A
A= $node->{'mix'}
Aresult
11
00
129 2 ▶ my $alloc = $node->{alloc} || 0;
Condition100%2/2
$node->{'alloc'} || 0
l!l
11
MC/DC100%1/1
$node->{'alloc'} || 0
$node->{'alloc'}
Truth table100%2/2
A
A= $node->{'alloc'}
Aresult
11
00
130 2 my $up = $total - $down;
131 2 ▶ my $jobs_running = $job->{jobs_running} || 0;
Condition50%1/2
$job->{'jobs_running'} || 0
l!l
20
MC/DC0%0/1
$job->{'jobs_running'} || 0
$job->{'jobs_running'}
Truth table50%1/2
A
A= $job->{'jobs_running'}
Aresult
11
00
132 2 ▶ my $jobs_pending = $job->{jobs_pending} || 0;
Condition100%2/2
$job->{'jobs_pending'} || 0
l!l
11
MC/DC100%1/1
$job->{'jobs_pending'} || 0
$job->{'jobs_pending'}
Truth table100%2/2
A
A= $job->{'jobs_pending'}
Aresult
11
00
133 2 ▶ my $nodes_running = $job->{nodes_running} || 0;
Condition50%1/2
$job->{'nodes_running'} || 0
l!l
20
MC/DC0%0/1
$job->{'nodes_running'} || 0
$job->{'nodes_running'}
Truth table50%1/2
A
A= $job->{'nodes_running'}
Aresult
11
00
134 2 ▶ my $nodes_pending = $job->{nodes_pending} || 0;
Condition100%2/2
$job->{'nodes_pending'} || 0
l!l
11
MC/DC100%1/1
$job->{'nodes_pending'} || 0
$job->{'nodes_pending'}
Truth table100%2/2
A
A= $job->{'nodes_pending'}
Aresult
11
00
135 2 my $load = sprintf('%d%%', load_pct($node));
136  
137 2 push @rows, [
138 $partition,
139 $total,
140 $up,
141 $idle,
142 $mix,
143 $alloc,
144 $down,
145 $jobs_running,
146 $jobs_pending,
147 $nodes_running,
148 $nodes_pending,
149 $load,
150 ];
151  
152 2 $totals{total} += $total;
153 2 $totals{up} += $up;
154 2 $totals{idle} += $idle;
155 2 $totals{mix} += $mix;
156 2 $totals{alloc} += $alloc;
157 2 $totals{down} += $down;
158 2 $totals{jobs_running} += $jobs_running;
159 2 $totals{jobs_pending} += $jobs_pending;
160 2 $totals{nodes_running} += $nodes_running;
161 2 $totals{nodes_pending} += $nodes_pending;
162 }
163  
164 1 ▶ if (@rows > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows > 1) 1 0 1
165 push @rows, [
166 $totals{partition},
167 $totals{total},
168 $totals{up},
169 $totals{idle},
170 $totals{mix},
171 $totals{alloc},
172 $totals{down},
173 $totals{jobs_running},
174 $totals{jobs_pending},
175 $totals{nodes_running},
176 $totals{nodes_pending},
177 1 sprintf('%d%%', load_pct(\%totals)),
178 ];
179 }
180  
181 1 return \@rows;
182 }
183  
184 5 ▶sub load_pct {
Subroutine load_pct: called CC=6 SCAR=18.0
185 5 my ($row) = @_;
186 5 ▶ my $total = $row->{total} || 0;
Condition50%1/2
$row->{'total'} || 0
l!l
50
MC/DC0%0/1
$row->{'total'} || 0
$row->{'total'}
Truth table50%1/2
A
A= $row->{'total'}
Aresult
11
00
187 5 ▶ my $down = $row->{down} || 0;
Condition100%2/2
$row->{'down'} || 0
l!l
32
MC/DC100%1/1
$row->{'down'} || 0
$row->{'down'}
Truth table100%2/2
A
A= $row->{'down'}
Aresult
11
00
188 5 my $up = $total - $down;
189 5 ▶ return 0 if $up <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $up <= 0 0 5 5
190 5 ▶ my $busy = ($row->{mix} || 0) + ($row->{alloc} || 0);
Condition100%4/4
$row->{'mix'} || 0
l!l
32
$row->{'alloc'} || 0
l!l
32
MC/DC100%1/1
$row->{'mix'} || 0
$row->{'mix'}
MC/DC100%1/1
$row->{'alloc'} || 0
$row->{'alloc'}
Truth table100%2/2
A
A= $row->{'mix'}
Aresult
11
00
Truth table100%2/2
A
A= $row->{'alloc'}
Aresult
11
00
191 5 return ($busy / $up) * 100;
192 }
193  
194 5 ▶sub normalise_node_state {
Subroutine normalise_node_state: called CC=6 SCAR=18.2
195 5 my ($state) = @_;
196 5 ▶ $state = lc($state // '');
Condition50%1/2
$state // ""
l!l
50
MC/DC0%0/1
$state // ""
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
197 5 $state =~ s/[^a-z].*$//;
198  
199 5 ▶ return 'idle' if $state =~ /^idle/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^idle/u 2 3 5
200 3 ▶ return 'mix' if $state =~ /^mix/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^mix/u 1 2 3
201 2 ▶ return 'alloc' if $state =~ /^alloc/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^alloc/u 1 1 2
202 1 ▶ return 'down' if $state =~ /^(down|drain|drng|drained|fail)/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^(down|drain|drng|drained|fail)/u 1 0 1
203 0 return 'alloc';
204 }
205  
206 3 ▶sub normalise_job_state {
Subroutine normalise_job_state: called CC=4 SCAR=14.6
207 3 my ($state) = @_;
208 3 ▶ $state = uc($state // '');
Condition50%1/2
$state // ""
l!l
30
MC/DC0%0/1
$state // ""
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
209  
210 3 ▶ return 'pending' if $state eq 'PD';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "PD" 1 2 3
211 2 ▶ return 'running' if $state =~ /^(R|CG|CF|SI|SO|ST)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^(R|CG|CF|SI|SO|ST)$/u 2 0 2
212 0 return 'running';
213 }
214  
215 1 ▶sub print_tsv {
Subroutine print_tsv: called CC=2 SCAR=6.9
216 1 my ($rows) = @_;
217 1 for my $row (@{$rows}) {
218 4 say join("\t", @{$row});
219 }
220 }
221  
222 0 ▶sub print_table {
Subroutine print_table: not called CC=11 SCAR=48.8
223 0 my ($rows) = @_;
224 0 my @widths;
225 0 for my $row (@{$rows}) {
226 0 for my $i (0 .. $#{$row}) {
227 0 ▶ my $len = length($row->[$i] // '');
Condition0%0/2
$row->[$i] // ""
l!l
00
MC/DC0%0/1
$row->[$i] // ""
$row->[$i]
Truth table0%0/2
A
A= $row->[$i]
Aresult
11
00
228 0 ▶ $widths[$i] = $len if !defined $widths[$i] || $len > $widths[$i];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $widths[$i] or $len > $widths[$i] 0 0 0
Condition0%0/3
not defined $widths[$i] or $len > $widths[$i]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $widths[$i] or $len > $widths[$i]
not defined $widths[$i] $len > $widths[$i]
Truth table0%0/3
not(A) or B
A= not defined $widths[$i]B= $len > $widths[$i]
ABresult
1X1
011
000
229 }
230 }
231  
232 0 for my $idx (0 .. $#{$rows}) {
233 0 my $row = $rows->[$idx];
234 0 my @parts;
235 0 for my $i (0 .. $#{$row}) {
236 0 ▶ my $fmt = $i == 0 ? "%-*s" : "%*s";
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 0 0 0
237 0 push @parts, sprintf($fmt, $widths[$i], $row->[$i]);
238 }
239  
240 0 ▶ if ($idx == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx == 0) 0 0 0
241 0 say BOLD, join(" ", @parts), RESET;
242 0 my @rule = map { '-' x $_ } @widths;
243 0 say join(" ", @rule);
244 0 next;
245 }
246  
247 0 ▶ if ($row->[0] eq 'TOTAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[0] eq 'TOTAL') { } 0 0 0
248 0 say BOLD, join(" ", @parts), RESET;
249 } else {
250 0 say join(" ", @parts);
251 }
252 }
253 }
254  
255 2 ▶sub _has_command {
Subroutine _has_command: called CC=2 SCAR=7.6
256 2 my ($cmd) = @_;
257 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2 2
258 0 return system("where $cmd >nul 2>nul") == 0;
259 }
260 2 return system("command -v $cmd >/dev/null 2>&1") == 0;
261 }
262  
263 2 ▶sub _command_output {
Subroutine _command_output: called CC=2 SCAR=7.0
264 2 my (@cmd) = @_;
265 2 ▶ open(my $fh, '-|', @cmd) or die "ERROR slurm-load: cannot execute $cmd[0]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "ERROR slurm-load: cannot execute $cmd[0]: $!\n" 2 0 2
266 2 my @lines = <$fh>;
267 2 close $fh;
268 2 return ($? == 0, @lines);
269 }
270  
271 1 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
272 1 my ($exit_code) = @_;
273 1 print STDERR <<'END';
274 slurm-load - Summarise Slurm workload by partition
275  
276 Usage:
277 slurm-load [options]
278  
279 Options:
280 -t, --tab Print TSV instead of aligned text
281 -u, --user USER Restrict squeue job counts to one user
282 --version Show version and exit
283 -h, --help Show this help and exit
284 END
285 1 exit $exit_code;
286 }
287  
288 __END__
« lib/NBI/Launcher.pm ↑ Summary bin/runjob »