bin/sbalance

filter: Statementstmt 75.1% 109 / 145 Branchbran 50.0% 30 / 60 Conditioncond 34.2% 13 / 38 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 60.0% 167 / 278 CC 31 SCAR 43.6
CC
31
Cov
63%
dump_usage_machine
210.0
dump_usage
16.0
get_accounts_from_users
4.1
CRAP 78.2
↑ Summary blib/lib/Slurm/Sshare.pm »
1 #!/usr/bin/env perl
2 #The above "shebang" will run whatever Perl is found in the user's
3 #path. It is advised that sysadmins replace this with the proper
4 #path to the appropriate Perl binary on their systems. Presumably
5 #the one with Slurm::Share module installed.
6 #
7 #Script to report on usage and limits for users/accounts
8 325 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 325 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #If Slurm::Sshare is not installed in the standard location for
12 #site libraries for this version of Perl, you probably need to
13 #uncomment and edit the 'use lib' line below to enable perl to
14 #find the requisite modules.
15 #use lib "/path/to/my/perl/libraries"
16  
17 325 ▶use Slurm::Sshare;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 325 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 325 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 325 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #For convenience, sbalance versions should match associated Slurm::Sshare
24 #version, but do separately (in case someone somehows mixes versions)
25 325 my $SBALANCE_VERSION = qw(1.2.2);
26  
27 325 my $Sshare = 'Slurm::Sshare';
28  
29 325 my $DEFAULT_SSHARE_PATH;
30 #If $DEFAULT_SSHARE_PATH is not set, this script will default to
31 #using whatever sshare command the Slurm::Sshare module defaults to,
32 #which typically is whatever sshare is in the user's path. It is
33 #advised to set DEFAULT_SSHARE_PATH to the appropriate path to the
34 #sshare command on your system, to avoid this script being overly
35 #dependent on the user's environment. To do so, uncomment the
36 #line below and change to the full path to sshare on your system.
37 #$DEFAULT_SSHARE_PATH='/usr/local/bin/sshare';
38  
39 my $sshare_cmd_source;
40  
41  
42 #System administrators probably will want to modify the
43 #following subroutine to properly default the cluster for
44 #their systems. If not modified (i.e. returns undef), the
45 #user will either need to specify the cluster on the command
46 #line, or the sshare command will default the cluster, which
47 #might or might not be what is desired
48 sub default_our_cluster()
49 86 ▶{ return;
Subroutine default_our_cluster: called CC=1 SCAR=0.0
50 }
51  
52 sub show_version_info()
53 1 ▶{ my $sshare_version = $Slurm::Sshare::VERSION;
Subroutine show_version_info: called CC=1 SCAR=0.0
54 1 my $perl_version = $^V;
55 1 my $perl_version_str = sprintf "%vd", $perl_version;
56 1 my $sshare_command = $Sshare->sshare;
57  
58 1 print STDERR <<EOF;
59 sbalance version $SBALANCE_VERSION
60 Perl version $perl_version_str
61 Slurm::Share version $sshare_version
62 Using sshare command: $sshare_command [$sshare_cmd_source]
63  
64 EOF
65 }
66
67 sub get_accounts_from_users($$)
68 #Does same as get_accounts_from_user, but combines over multiple users
69 252 ▶{ my $users = shift;
Subroutine get_accounts_from_users: called CC=4 SCAR=14.2
70 252 my $cluster = shift;
71 252 ▶ die "Invalid user list '$users' to get_accounts_from_users at "
Branch: 1 branch
BranchTrueFalseTotal
unless $users and ref $users eq "ARRAY" 0 252 252
Condition33%1/3
$users and ref $users eq "ARRAY"
!ll&&!rl&&r
00252
MC/DC0%0/2
$users and ref $users eq "ARRAY"
$users ref $users eq "ARRAY"
Truth table33%1/3
A and B
A= $usersB= ref $users eq "ARRAY"
ABresult
0X0
100
111
72 unless $users && ref($users) eq 'ARRAY';
73  
74 252 my $sshare_recs = $Sshare->sshare_list(users=>$users, clusters=>$cluster);
75 #We only want records with both the user and account fields set
76 148 ▶ my @sshare_recs = grep { defined $_->user && defined $_->account } @$sshare_recs;
Condition66%2/3
defined $_->user && defined $_->account
!ll&&!rl&&r
5120468
MC/DC50%1/2
defined $_->user && defined $_->account
defined $_->user defined $_->account
Truth table66%2/3
A && B
A= defined $_->userB= defined $_->account
ABresult
0X0
100
111
77  
78 #Extract the accounts
79 148 my @accounts = map { $_->account } @sshare_recs;
80 #Remove duplicates
81 148 my %temp = map { $_ => undef } @accounts;
82 148 my $accounts = [ keys %temp ];
83 148 return $accounts;
84 }
85  
86 sub dump_usage($$$;$)
87 324 ▶{ my $account = shift;
Subroutine dump_usage: called CC=15 SCAR=27.8
88 324 my $cluster = shift;
89 324 my $users = shift;
90 324 ▶ my $options = shift || {};
Condition50%1/2
shift() || {}
l!l
3240
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
91  
92 324 my $suppress0 = $options->{suppress0};
93  
94 324 my $rec = $Sshare->usage_for_account_in_cluster(
95 cluster=>$cluster, account=>$account, users=>$users);
96 180 ▶ unless ( $rec && ref($rec) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec and ref $rec eq "ARRAY") 0 180 180
Condition33%1/3
$rec and ref $rec eq "ARRAY"
!ll&&!rl&&r
00180
MC/DC0%0/2
$rec and ref $rec eq "ARRAY"
$rec ref $rec eq "ARRAY"
Truth table33%1/3
A and B
A= $recB= ref $rec eq "ARRAY"
ABresult
0X0
100
111
97 0 { die "\nError looking up usage for account $account: $rec\n";
98 }
99 180 my ( $cpusec_used, $cpumin_limit, $cpumin_unused, $userdata) = @$rec;
100  
101 180 my $ksu_used = sprintf "%.2f", $cpusec_used/60/60/1000;
102 180 ▶ my $ksu_lim = sprintf "%.2f", $cpumin_limit/60/1000 if defined $cpumin_limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cpumin_limit 180 0 180
103 180 my $ksu_unused = sprintf "%.2f", $cpumin_unused/60/1000;
104 180 my $pused =0 ;
105 180 ▶ $pused = $cpusec_used/60/$cpumin_limit*100 if $cpumin_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $cpumin_limit 180 0 180
106 180 $pused = sprintf "%.1f", $pused;
107  
108
109 180 ▶ $cluster = 'DEFAULT' unless defined $cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cluster 68 112 180
110 180 print <<EOF;
111 Account: $account ($cluster)
112 EOF
113 180 ▶ if ( defined $cpumin_limit )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cpumin_limit) 180 0 180
114 {
115 180 print <<EOF;
116 Limit: $ksu_lim kSU
117 Unused: $ksu_unused kSU
118 EOF
119 }
120 180 print <<EOF;
121 Used: $ksu_used kSU ($pused % of limit)
122 EOF
123  
124 180 my @users = ();
125 180 ▶ if ( $userdata && ref($userdata) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ($userdata and ref $userdata eq "HASH") 174 6 180
Condition66%2/3
$userdata and ref $userdata eq "HASH"
!ll&&!rl&&r
60174
MC/DC50%1/2
$userdata and ref $userdata eq "HASH"
$userdata ref $userdata eq "HASH"
Truth table66%2/3
A and B
A= $userdataB= ref $userdata eq "HASH"
ABresult
0X0
100
111
126 174 { @users = sort ( keys %$userdata );
127 }
128  
129 180 USER: foreach my $user (@users)
130 326 ▶ { my $usersecs = $userdata->{$user} || 0;
Condition100%2/2
$userdata->{$user} || 0
l!l
30818
MC/DC100%1/1
$userdata->{$user} || 0
$userdata->{$user}
Truth table100%2/2
A
A= $userdata->{$user}
Aresult
11
00
131 326 ▶ next USER if $suppress0 && ! $usersecs;
Branch: 1 branch
BranchTrueFalseTotal
if $suppress0 and not $usersecs 16 310 326
Condition100%3/3
$suppress0 and not $usersecs
!ll&&!rl&&r
11619416
MC/DC100%2/2
$suppress0 and not $usersecs
$suppress0 not $usersecs
Truth table100%3/3
A and not(B)
A= $suppress0B= not $usersecs
ABresult
0X0
100
111
132 310 $ksu_used = sprintf "%.4f", $usersecs/60/60/1000;
133 310 $pused =0;
134 310 ▶ $pused = $usersecs/$cpusec_used*100 if $cpusec_used;
Branch: 1 branch
BranchTrueFalseTotal
if $cpusec_used 310 0 310
135 310 $pused = sprintf "%.1f", $pused;
136 310 print <<EOF;
137 User $user used $ksu_used kSU ($pused % of total usage)
138 EOF
139 }
140 }
141  
142 sub dump_usage_machine($$$;$)
143 0 ▶{ my $account = shift;
Subroutine dump_usage_machine: not called CC=14 SCAR=53.5
144 0 my $cluster = shift;
145 0 my $users = shift;
146 0 ▶ my $options = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
147  
148 0 my $noheaders = $options->{noheaders};
149 0 my $suppress0 = $options->{suppress0};
150  
151 0 my $rec = $Sshare->usage_for_account_in_cluster(
152 cluster=>$cluster, account=>$account, users=>$users);
153 0 ▶ unless ( $rec && ref($rec) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec and ref $rec eq "ARRAY") 0 0 0
Condition0%0/3
$rec and ref $rec eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$rec and ref $rec eq "ARRAY"
$rec ref $rec eq "ARRAY"
Truth table0%0/3
A and B
A= $recB= ref $rec eq "ARRAY"
ABresult
0X0
100
111
154 0 { die "\nError looking up usage for account $account: $rec\n";
155 }
156 0 my ( $cpusec_used, $cpumin_limit, $cpumin_unused, $userdata) = @$rec;
157
158 0 ▶ $cluster = 'DEFAULT' unless defined $cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cluster 0 0 0
159 0 ▶ print "#cluster : account : limit (cpu-min):used (cpu-sec) : " .
Branch: 1 branch
BranchTrueFalseTotal
unless $noheaders 0 0 0
160 "unused (cpu-min)\n" unless $noheaders;
161 0 print "${cluster}:${account}:${cpumin_limit}:${cpusec_used}:${cpumin_unused}\n";
162  
163 0 my @users = ();
164 0 ▶ if ( $userdata && ref($userdata) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ($userdata and ref $userdata eq "HASH") 0 0 0
Condition0%0/3
$userdata and ref $userdata eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$userdata and ref $userdata eq "HASH"
$userdata ref $userdata eq "HASH"
Truth table0%0/3
A and B
A= $userdataB= ref $userdata eq "HASH"
ABresult
0X0
100
111
165 0 { @users = sort ( keys %$userdata );
166 }
167  
168 0 my $header_printed = 0;
169 0 USERS: foreach my $user (@users)
170 0 ▶ { my $usersecs = $userdata->{$user} || 0;
Condition0%0/2
$userdata->{$user} || 0
l!l
00
MC/DC0%0/1
$userdata->{$user} || 0
$userdata->{$user}
Truth table0%0/2
A
A= $userdata->{$user}
Aresult
11
00
171 0 ▶ next USERS if $suppress0 && ! $usersecs;
Branch: 1 branch
BranchTrueFalseTotal
if $suppress0 and not $usersecs 0 0 0
Condition0%0/3
$suppress0 and not $usersecs
!ll&&!rl&&r
000
MC/DC0%0/2
$suppress0 and not $usersecs
$suppress0 not $usersecs
Truth table0%0/3
A and not(B)
A= $suppress0B= not $usersecs
ABresult
0X0
100
111
172 0 ▶ unless ( $noheaders || $header_printed )
Branch: 1 branch
BranchTrueFalseTotal
unless ($noheaders or $header_printed) 0 0 0
Condition0%0/3
$noheaders or $header_printed
l!l&&r!l&&!r
000
MC/DC0%0/2
$noheaders or $header_printed
$noheaders $header_printed
Truth table0%0/3
A or B
A= $noheadersB= $header_printed
ABresult
1X1
011
000
173 0 { print "#cluster : account : username :" .
174 " used (cpu-sec)\n";
175 0 $header_printed = 1;
176 }
177 0 print "${cluster}:${account}:${user}:${usersecs}\n";
178 }
179 }
180
181  
182 #-------------------------------------------------------------------
183 # Main
184 #-------------------------------------------------------------------
185  
186 325 my $help;
187 325 my $man;
188 325 my $cluster;
189 325 my $accounts = [];
190 325 my $users = [];
191 325 my $allusers = 0;
192 325 my $headers = 1;
193 325 my $machine=0;
194 325 my $verbose=0;
195 325 my $suppress0 = 1;
196 325 my $alt_sshare_path;
197 my $show_version;
198  
199 325 my $res = GetOptions(
200 'h|help!' => \$help,
201 'V|version!' => \$show_version,
202 'manual!' => \$man,
203 'machine!' => \$machine,
204 'allusers|all-users|all_users!' => \$allusers,
205 'headers!' => \$headers,
206 'v|verbose!' => \$verbose,
207 'suppress|suppress0|suppress-zero|suppresszero|suppress_zero!' => \$suppress0,
208  
209 'cluster=s' => \$cluster,
210 'a|account=s' => $accounts,
211 'user=s' => $users,
212  
213 #This is for debugging purposes only, do NOT advertise it in usage, etc
214 'sshare-alternate-path=s' => \$alt_sshare_path,
215 );
216  
217 325 ▶unless ( $res )
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 325 325
218 0 { pod2usage( -exitval=>1, -verbose=>0, -msg=>'Error parsing arguments');
219 }
220 325 ▶if ( $man )
Branch: 1 branch
BranchTrueFalseTotal
if ($man) 0 325 325
221 0 { pod2usage( -exitval=>0, -verbose=>2);
222 }
223 325 ▶if ( $help )
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 325 325
224 0 { pod2usage( -exitval=>0, -verbose=>1);
225 }
226  
227 #Set the sshare command here (show will show up in show_version_info)
228 325 ▶if ( defined $alt_sshare_path )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $alt_sshare_path) { } 325 0 325
elsif (defined $DEFAULT_SSHARE_PATH) { } 0 0 0
229 325 { $Sshare->sshare($alt_sshare_path);
230 325 $sshare_cmd_source='cmd-line argument';
231 } elsif ( defined $DEFAULT_SSHARE_PATH )
232 0 { $Sshare->sshare($DEFAULT_SSHARE_PATH);
233 0 $sshare_cmd_source='script default';
234 } else
235 0 { $sshare_cmd_source='Slurm::Sshare default';
236 }
237
238 325 ▶if ( $show_version )
Branch: 1 branch
BranchTrueFalseTotal
if ($show_version) 1 324 325
239 1 { show_version_info;
240 exit
241 1 }
242  
243 324 my $noheaders = ! $headers;
244 324 ▶if ( $noheaders )
Branch: 1 branch
BranchTrueFalseTotal
if ($noheaders) 0 324 324
245 0 ▶{ warn "Ignoring --noheaders; only valid with machine parsable output.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $machine 0 0 0
246 unless $machine;
247 }
248 324 my $options = {
249 noheaders => $noheaders,
250 suppress0 => $suppress0,
251 };
252  
253 324 $Sshare->verbose($verbose);
254  
255 324 ▶unless ( defined $cluster )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cluster) 86 238 324
256 { #Use default_our_cluster to default the cluster
257 86 $cluster = default_our_cluster;
258 }
259  
260 324 my $tmp;
261 324 ▶unless ( scalar(@$accounts) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$accounts) 252 72 324
262 { #No accounts given
263 252 ▶ $users = [ $ENV{USER} ] unless scalar(@$users);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$users 48 204 252
264  
265 252 $accounts = get_accounts_from_users($users, $cluster);
266 148 ▶ die "Error getting accounts from user list: $accounts at "
Branch: 1 branch
BranchTrueFalseTotal
unless $accounts and ref $accounts eq "ARRAY" 0 148 148
Condition33%1/3
$accounts and ref $accounts eq "ARRAY"
!ll&&!rl&&r
00148
MC/DC0%0/2
$accounts and ref $accounts eq "ARRAY"
$accounts ref $accounts eq "ARRAY"
Truth table33%1/3
A and B
A= $accountsB= ref $accounts eq "ARRAY"
ABresult
0X0
100
111
267 unless ( $accounts && ref($accounts) eq 'ARRAY');
268 148 ▶ unless ( scalar(@$accounts) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$accounts) 0 148 148
269 0 { $tmp = join ", ", @$users;
270 0 print "No accounts found for user list ($tmp).\n";
271 0 exit 0;
272 }
273 }
274  
275 220 my $sep = '';
276 220 ▶$users = undef if $allusers;
Branch: 1 branch
BranchTrueFalseTotal
if $allusers 124 96 220
277 220 foreach my $account (sort @$accounts)
278 324 { print $sep;
279 324 ▶ if ( $machine )
Branch: 1 branch
BranchTrueFalseTotal
if ($machine) { } 0 324 324
280 0 { dump_usage_machine($account, $cluster, $users, $options);
281 } else
282 324 { dump_usage($account, $cluster, $users, $options);
283 180 $sep="\n";
284 }
285 }
286  
287 __END__
↑ Summary blib/lib/Slurm/Sshare.pm »