bin/runjob

filter: Statementstmt 68.4% 215 / 314 Branchbran 47.8% 91 / 190 Conditioncond 34.1% 28 / 82 MC/DCmcdc 6.5% 3 / 46 Subroutinesub 80.0% 20 / 25 Podpod n/a totaltotal 54.3% 357 / 657 CC 49 SCAR 32.7
CC
49
Cov
57%
get_array_task_limit
89.6
xdump
20.0
get_params_array_info
15.1
CRAP 26.4
« bin/slurm-load ↑ Summary blib/lib/NBI/QueuedJob.pm »
1 #!/usr/bin/env perl
2 #ABSTRACT: Run a job in the cluster using NBI::Slurm
3 #PODNAME: runjob
4  
5 4 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use FindBin qw($RealBin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 my $BIN = basename($0);
16 4 $Data::Dumper::Sortkeys = 1;
17  
18 4 ▶if (-e "$RealBin/../dist.ini") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$main::RealBin/../dist.ini") 4 0 4
19 4 ▶ say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 4 4
20 4 ▶ use lib "$RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 }
22  
23 4 ▶use NBI::Slurm;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use NBI::EcoScheduler;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 my $user_home_dir = $ENV{HOME};
28 4 my @afterok;
29 4 my $user_current_dir = getcwd();
30 4 my $username = $ENV{USER};
31 4 my $config = NBI::Slurm::load_config("$user_home_dir/.nbislurm.config");
32 4 my $version = $NBI::Slurm::VERSION;
33 4 ▶my $queue = $config->{'queue'} // 'qib-short';
Condition50%1/2
$config->{"queue"} // "qib-short"
l!l
04
MC/DC0%0/1
$config->{"queue"} // "qib-short"
$config->{"queue"}
Truth table50%1/2
A
A= $config->{"queue"}
Aresult
11
00
34 4 ▶my $gpuqueue = $config->{'gpuqueue'} // 'qib-gpu';
Condition50%1/2
$config->{"gpuqueue"} // "qib-gpu"
l!l
04
MC/DC0%0/1
$config->{"gpuqueue"} // "qib-gpu"
$config->{"gpuqueue"}
Truth table50%1/2
A
A= $config->{"gpuqueue"}
Aresult
11
00
35 4 ▶my $threads = $config->{'threads'} // 1;
Condition50%1/2
$config->{"threads"} // 1
l!l
04
MC/DC0%0/1
$config->{"threads"} // 1
$config->{"threads"}
Truth table50%1/2
A
A= $config->{"threads"}
Aresult
11
00
36 4 ▶my $memory = $config->{'memory'} // 8000;
Condition50%1/2
$config->{"memory"} // 8000
l!l
04
MC/DC0%0/1
$config->{"memory"} // 8000
$config->{"memory"}
Truth table50%1/2
A
A= $config->{"memory"}
Aresult
11
00
37 4 ▶my $time = $config->{'time'} // "2h";
Condition50%1/2
$config->{"time"} // "2h"
l!l
04
MC/DC0%0/1
$config->{"time"} // "2h"
$config->{"time"}
Truth table50%1/2
A
A= $config->{"time"}
Aresult
11
00
38 4 ▶my $tmpdir = $config->{'tmpdir'} // "/tmp";
Condition50%1/2
$config->{"tmpdir"} // "/tmp"
l!l
04
MC/DC0%0/1
$config->{"tmpdir"} // "/tmp"
$config->{"tmpdir"}
Truth table50%1/2
A
A= $config->{"tmpdir"}
Aresult
11
00
39 4 my $name;
40 4 ▶my $email_address = $config->{'email'} // undef;
Condition50%1/2
$config->{"email"} // undef
l!l
04
MC/DC0%0/1
$config->{"email"} // undef
$config->{"email"}
Truth table50%1/2
A
A= $config->{"email"}
Aresult
11
00
41 4 ▶my $mail_type = $config->{'email_type'} // "none";
Condition50%1/2
$config->{"email_type"} // "none"
l!l
04
MC/DC0%0/1
$config->{"email_type"} // "none"
$config->{"email_type"}
Truth table50%1/2
A
A= $config->{"email_type"}
Aresult
11
00
42 4 ▶my $opt_placeholder = $config->{'placeholder'} // "#FILE#";
Condition50%1/2
$config->{"placeholder"} // "#FILE#"
l!l
04
MC/DC0%0/1
$config->{"placeholder"} // "#FILE#"
$config->{"placeholder"}
Truth table50%1/2
A
A= $config->{"placeholder"}
Aresult
11
00
43 4 my $params_array_file;
44 my $command;
45 4 my $verbose;
46 4 my $debug;
47 4 my $run;
48 4 my $gpu;
49 4 my $opt_eco;
50 4 my $opt_no_eco;
51 4 my @slurm_options;
52 GetOptions(
53 'm|memory=s' => \$memory,
54 'c|cores|threads=i' => \$threads,
55 'q|queue=s' => \$queue,
56 'T|time=s' => \$time,
57 'w|tmpdir=s' => \$tmpdir,
58 'n|name=s' => \$name,
59 'r|run' => \$run,
60 'a|email-address=s' => \$email_address,
61 'e|mail-type=s' => \$mail_type, # 'BEGIN,END,FAIL,REQUEUE,ALL'
62 'after=s' => \@afterok,
63 'f|files=s' => \my @files,
64 'p|params-array=s' => \$params_array_file,
65 'placeholder=s' => \$opt_placeholder,
66 'gpu' => \$gpu,
67 'option=s' => \@slurm_options,
68 'eco' => \$opt_eco,
69 'no-eco' => \$opt_no_eco,
70 'verbose' => \$verbose,
71 0 ▶ 'version' => sub { say "runjob v", $NBI::Slurm::VERSION; exit },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 'debug' => \$debug,
73 0 ▶ 'help' => sub { usage(1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 4 ▶) or usage(1);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("m|memory=s", \$memory, "c|cores|threads=i", \$threads, "q|queue=s", \$queue, "T|time=s", \$time, "w|tmpdir=s", \$tmpdir, "n|name=s", \$name, "r|run", \$run, "a|email-address=s", \$email_address, "e|mail-type=s", \$mail_type, "after=s", \@afterok, "f|files=s", \my @files, "p|params-array=s", \$params_array_file, "placeholder=s", \$opt_placeholder, "gpu", \$gpu, "option=s", \@slurm_options, "eco", \$opt_eco, "no-eco", \$opt_no_eco, "verbose", \$verbose, "version", sub { say "runjob v", $NBI::Slurm::VERSION; exit; } , "debug", \$debug, "help", sub { usage(1); } ) or usage(1) 4 0 4
75  
76 4 ▶if ($opt_eco && $opt_no_eco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_eco and $opt_no_eco) 0 4 4
Condition33%1/3
$opt_eco and $opt_no_eco
!ll&&!rl&&r
400
MC/DC0%0/2
$opt_eco and $opt_no_eco
$opt_eco $opt_no_eco
Truth table33%1/3
A and B
A= $opt_ecoB= $opt_no_eco
ABresult
0X0
100
111
77 0 say STDERR RED, BOLD, "ERROR:", RESET, " --eco and --no-eco are mutually exclusive";
78 0 exit 1;
79 }
80  
81 4 ▶$debug = 1 if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 4 4
82 4 ▶$verbose = 1 if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
83  
84 # Update queue based on time
85 4 my $time_hours = NBI::Opts::_time_to_hour($time);
86 4 $queue = update_queue($queue, $time_hours);
87 4 $command = join(" ", @ARGV);
88 4 ▶usage(1) unless ($command);
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 4 4
89  
90 4 ▶if (@files and defined $params_array_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files and defined $params_array_file) 1 3 4
Condition66%2/3
@files and defined $params_array_file
!ll&&!rl&&r
301
MC/DC50%1/2
@files and defined $params_array_file
@files defined $params_array_file
Truth table66%2/3
A and B
A= @filesB= defined $params_array_file
ABresult
0X0
100
111
91 1 say STDERR RED, BOLD, "ERROR:", RESET, " --files and --params-array are mutually exclusive";
92 1 exit 1;
93 }
94  
95 3 my @abs_files = get_abs_files(@files);
96 3 my ($params_array_abs, $params_rows) = get_params_array_info($params_array_file, $command);
97 2 ▶my $total_array_tasks = @abs_files ? scalar(@abs_files) : $params_rows;
Branch: 1 branch
BranchTrueFalseTotal
@abs_files ? : 0 2 2
98 2 my $array_task_limit = get_array_task_limit($config);
99 2 my @array_chunks = build_array_chunks($total_array_tasks, $array_task_limit);
100  
101  
102 2 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
103 0 say STDERR YELLOW, BOLD, "PARAMETERS:",RESET;
104 0 ▶ say STDERR YELLOW, "Queue: ", RESET, $queue // "(not set)";
Condition0%0/2
$queue // "(not set)"
l!l
00
MC/DC0%0/1
$queue // "(not set)"
$queue
Truth table0%0/2
A
A= $queue
Aresult
11
00
105 0 ▶ say STDERR YELLOW, "Memory: ", RESET, $memory // "(not set)";
Condition0%0/2
$memory // "(not set)"
l!l
00
MC/DC0%0/1
$memory // "(not set)"
$memory
Truth table0%0/2
A
A= $memory
Aresult
11
00
106 0 ▶ say STDERR YELLOW, "Threads: ", RESET, $threads // "(not set)";
Condition0%0/2
$threads // "(not set)"
l!l
00
MC/DC0%0/1
$threads // "(not set)"
$threads
Truth table0%0/2
A
A= $threads
Aresult
11
00
107 0 ▶ say STDERR YELLOW, "Time: ", RESET, $time // "(not set)";
Condition0%0/2
$time // "(not set)"
l!l
00
MC/DC0%0/1
$time // "(not set)"
$time
Truth table0%0/2
A
A= $time
Aresult
11
00
108 0 ▶ say STDERR YELLOW, "Tmpdir: ", RESET, $tmpdir // "(not set)";
Condition0%0/2
$tmpdir // "(not set)"
l!l
00
MC/DC0%0/1
$tmpdir // "(not set)"
$tmpdir
Truth table0%0/2
A
A= $tmpdir
Aresult
11
00
109 0 ▶ say STDERR YELLOW, "Name: ", RESET, $name // "(not set)";
Condition0%0/2
$name // "(not set)"
l!l
00
MC/DC0%0/1
$name // "(not set)"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
110 0 ▶ say STDERR YELLOW, "Command: ", RESET, $command // "(not set)";
Condition0%0/2
$command // "(not set)"
l!l
00
MC/DC0%0/1
$command // "(not set)"
$command
Truth table0%0/2
A
A= $command
Aresult
11
00
111 0 ▶ say STDERR YELLOW, "Params TSV: ", RESET, $params_array_abs if defined $params_array_abs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $params_array_abs 0 0 0
112 0 ▶ say STDERR YELLOW, "Array size: ", RESET, $total_array_tasks if $total_array_tasks;
Branch: 1 branch
BranchTrueFalseTotal
if $total_array_tasks 0 0 0
113 0 ▶ say STDERR YELLOW, "Array cap: ", RESET, $array_task_limit if defined $array_task_limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $array_task_limit 0 0 0
114 }
115 # Check threadshours
116 2 ▶if (-d $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $time) 0 2 2
117 0 say STDERR RED, BOLD, "ERROR:", RESET, " did you mean \"--tmpdir $time\" instead of $threads threads?";
118 0 usage();
119 }
120  
121 # Sanitize tempdir
122 2 ▶if (-d $tmpdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $tmpdir) { } 2 0 2
123 2 $tmpdir = File::Spec->rel2abs($tmpdir);
124 2 ▶ say STDERR "[DEBUG] Using $tmpdir as temporary directory" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
125 } else {
126 # Check if it was meant to be a TIME instead
127 0 ▶ if ($tmpdir =~/ / or $tmpdir =~/(\d+)[dh]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpdir =~ / /u or $tmpdir =~ /(\d+)[dh]/u) 0 0 0
Condition0%0/3
$tmpdir =~ / /u or $tmpdir =~ /(\d+)[dh]/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmpdir =~ / /u or $tmpdir =~ /(\d+)[dh]/u
$tmpdir =~ / /u $tmpdir =~ /(\d+)[dh]/u
Truth table0%0/3
A or B
A= $tmpdir =~ / /uB= $tmpdir =~ /(\d+)[dh]/u
ABresult
1X1
011
000
128 0 say STDERR RED, BOLD, "WARNING:", RESET, " $tmpdir looks like a time string, not a directory!";
129 }
130 0 eval {
131 0 ▶ mkdir($tmpdir) or die "Cannot create $tmpdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $tmpdir or die "Cannot create ${tmpdir}: $!" 0 0 0
132 };
133 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($verbose or $debug) { } 0 0 0
134 0 say STDERR RED, BOLD, "ERROR:", RESET, " Cannot create $tmpdir: $@";
135 0 exit 1;
136 ▶ } elsif ($verbose or $debug) {
Condition0%0/3
$verbose or $debug
l!l&&r!l&&!r
000
MC/DC0%0/2
$verbose or $debug
$verbose $debug
Truth table0%0/3
A or B
A= $verboseB= $debug
ABresult
1X1
011
000
137 0 say STDERR "[INFO] Created $tmpdir";
138 }
139 }
140  
141 # Sanitize memory
142 2 ▶if ($memory =~/^(\d+)$/ and $1 < 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($memory =~ /^(\d+)$/u and $1 < 200) 0 2 2
Condition33%1/3
$memory =~ /^(\d+)$/u and $1 < 200
!ll&&!rl&&r
020
MC/DC0%0/2
$memory =~ /^(\d+)$/u and $1 < 200
$memory =~ /^(\d+)$/u $1 < 200
Truth table33%1/3
A and B
A= $memory =~ /^(\d+)$/uB= $1 < 200
ABresult
0X0
100
111
143 0 say STDERR RED, BOLD, "WARNING:", RESET, " $memory is in Mb but <200, autoscaling to $memory GB";
144 0 $memory = $1 * 1000;
145 }
146 2 ▶$name = autoname($command) unless (defined $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 1 2
147 2 ▶say STDERR "[DEBUG] Using $name as job name" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
148  
149 2 ▶my $afterok_string = defined $afterok[0] ? "-d afterok:" . join(":", @afterok) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $afterok[0] ? : 0 2 2
150  
151 # Prepare additional SLURM options
152 2 my @additional_opts = ();
153 2 ▶push @additional_opts, $afterok_string if defined $afterok_string;
Branch: 1 branch
BranchTrueFalseTotal
if defined $afterok_string 0 2 2
154 2 ▶push @additional_opts, "--gres=gpu:1" if $gpu;
Branch: 1 branch
BranchTrueFalseTotal
if $gpu 0 2 2
155  
156 2 ▶push @additional_opts, @slurm_options if @slurm_options;
Branch: 1 branch
BranchTrueFalseTotal
if @slurm_options 0 2 2
157  
158 2 ▶$queue = $gpuqueue if $gpu;
Branch: 1 branch
BranchTrueFalseTotal
if $gpu 0 2 2
159  
160 #
161 2 ▶if (not NBI::Slurm::valid_queue($queue)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (NBI::Slurm::valid_queue($queue)) 1 1 2
162 1 my @valid_queues = NBI::Slurm::queues('CAN FAIL');
163 1 ▶ if (scalar @valid_queues) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @valid_queues) { } 0 1 1
164 0 say STDERR RED, BOLD, "WARNING:", RESET, " $queue is not a valid queue: ", join(", ", @valid_queues);
165 } else {
166 1 ▶ if ($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) { } 0 1 1
167 0 say STDERR RED, BOLD, "ERROR:", RESET, " You might be outside a slurm cluster. Run without --run to see the script", join(", ", @valid_queues);
168 0 exit 1;
169 } else {
170 1 say STDERR RED, BOLD, "WARNING:", RESET, " You might be outside a slurm cluster", join(", ", @valid_queues);
171 }
172
173
174 }
175
176 }
177  
178 # Eco scheduling
179 # eco_default from config defaults to 1 (on) if not explicitly set
180 2 ▶my $eco_default = defined $config->{eco_default} ? $config->{eco_default} : $NBI::EcoScheduler::DEFAULTS{eco_default};
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'eco_default'} ? : 0 2 2
181 2 ▶my $use_eco = $opt_no_eco ? 0
Branch: 2 branches
BranchTrueFalseTotal
$opt_no_eco ? : 1 1 2
$opt_eco ? : 0 1 1
182 : $opt_eco ? 1
183 : $eco_default;
184  
185 2 ▶if ($use_eco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_eco) 1 1 2
186 1 ▶ if (defined $config->{start_time} || defined $config->{start_date}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"start_time"} or defined $config->{"start_date"}) 0 1 1
Condition33%1/3
defined $config->{"start_time"} or defined $config->{"start_date"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $config->{"start_time"} or defined $config->{"start_date"}
defined $config->{"start_time"} defined $config->{"start_date"}
Truth table33%1/3
A or B
A= defined $config->{"start_time"}B= defined $config->{"start_date"}
ABresult
1X1
011
000
187 0 say STDERR YELLOW, "[eco] --begin already set explicitly, ignoring eco scheduling", RESET;
188 }
189 }
190  
191 0 ▶sub xdump {
Subroutine xdump: not called CC=4 SCAR=30.0
192 0 my ($hash_of_hashes, $indent) = @_;
193 0 ▶ $indent //= ' ';
Condition0%0/2
$indent //= " "
l!l
00
MC/DC0%0/1
$indent //= " "
$indent
Truth table0%0/2
A
A= $indent
Aresult
11
00
194 0 ▶ while (my ($key, $value) = each %$hash_of_hashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash_of_hashes) 0 0 0
195 0 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 0 0 0
196 0 print STDERR BOLD, "$indent$key :\n", RESET;
197 0 xdump($value, "$indent ");
198 0 print "$indent\n";
199 } else {
200 0 print STDERR "$indent$key = $value\n";
201 }
202 }
203 }
204  
205  
206 2 ▶if (@array_chunks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@array_chunks > 1) 1 1 2
207 1 say STDERR YELLOW, "[array] Splitting $total_array_tasks tasks into ", scalar(@array_chunks), " jobs", RESET;
208 }
209  
210 2 my @jobs;
211 2 ▶my $name_width = scalar(@array_chunks) > 1 ? length(scalar(@array_chunks)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @array_chunks > 1 ? : 1 1 2
212 2 for my $chunk_idx (0 .. $#array_chunks) {
213 4 my ($array_offset, $chunk_tasks) = @{$array_chunks[$chunk_idx]};
214 4 my @job_files = @abs_files;
215 4 my $job_params_array = $params_array_abs;
216 4 my $job_params_rows = $params_rows;
217 4 my $job_array_offset = 0;
218  
219 4 ▶ if (@abs_files && @array_chunks > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@abs_files and @array_chunks > 1) { } 0 4 4
elsif (defined $params_array_abs) { } 4 0 4
Condition33%1/3
@abs_files and @array_chunks > 1
!ll&&!rl&&r
400
MC/DC0%0/2
@abs_files and @array_chunks > 1
@abs_files @array_chunks > 1
Truth table33%1/3
A and B
A= @abs_filesB= @array_chunks > 1
ABresult
0X0
100
111
220 0 @job_files = @abs_files[$array_offset .. ($array_offset + $chunk_tasks - 1)];
221 } elsif (defined $params_array_abs) {
222 4 $job_array_offset = $array_offset;
223 }
224  
225 4 my $job_name = $name;
226 4 ▶ if (@array_chunks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@array_chunks > 1) 3 1 4
227 3 $job_name = sprintf("%s.part%0*d", $name, $name_width, $chunk_idx + 1);
228 }
229  
230 4 my $opts = NBI::Opts->new(
231 -queue => $queue,
232 -threads => $threads,
233 -memory => $memory,
234 -time => $time,
235 -tmpdir => $tmpdir,
236 -email_address => $email_address,
237 -email_type => $mail_type,
238 -opts => \@additional_opts,
239 -files => \@job_files,
240 -params_array => $job_params_array,
241 -params_rows => $job_params_rows,
242 -placeholder => $opt_placeholder,
243 -array_offset => $job_array_offset,
244 -array_tasks => $chunk_tasks,
245 );
246  
247 4 ▶ if ($use_eco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_eco) 1 3 4
248 1 my ($begin_epoch, $tier) = NBI::EcoScheduler::find_eco_begin($opts->hours, $config);
249 1 ▶ if (defined $begin_epoch) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $begin_epoch) { } 1 0 1
250 1 my $begin_str = NBI::EcoScheduler::epoch_to_slurm($begin_epoch);
251 1 my $delay = NBI::EcoScheduler::format_delay($begin_epoch);
252 1 ($opts->{start_date}, $opts->{start_time}) = split /T/, $begin_str;
253 1 ▶ my $tier_warn = $tier == 2 ? " (job may overrun eco window)"
Branch: 2 branches
BranchTrueFalseTotal
$tier == 2 ? : 0 1 1
$tier == 3 ? : 0 1 1
254 : $tier == 3 ? " (job may overlap peak hours — best available)"
255 : "";
256 1 ▶ say STDERR GREEN, "[eco] Job scheduled for $begin_str (in $delay)$tier_warn", RESET if $chunk_idx == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_idx == 0 1 0 1
257 } else {
258 0 ▶ say STDERR YELLOW, "[eco] No eco slot found in lookahead period, submitting immediately", RESET if $chunk_idx == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_idx == 0 0 0 0
259 }
260 }
261  
262 4 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 4 4
263 0 ▶ say STDERR GREEN, "CONFIG:\n", RESET if $chunk_idx == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_idx == 0 0 0 0
264 0 ▶ xdump($config) if $chunk_idx == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_idx == 0 0 0 0
265 0 ▶ print STDERR "\n" if $chunk_idx == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_idx == 0 0 0 0
266 0 say STDERR $opts->view();
267 }
268  
269 4 my $job = NBI::Job->new(
270 -name => $job_name,
271 -command => "cd \"$user_current_dir\"",
272 -opts => $opts,
273 );
274 4 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 4 4
275 0 say STDERR YELLOW, "JOB:\n", RESET, Dumper($job);
276 }
277 4 $job->append_command($command);
278 4 push @jobs, $job;
279 }
280  
281  
282 2 my $err = 0;
283 2 ▶if ($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) { } 1 1 2
284 1 my @job_ids;
285 1 for my $job (@jobs) {
286 3 my $j = $job->run();
287 3 ▶ if ($j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j) { } 3 0 3
288 3 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 3 3
289 0 say STDERR $job->view();
290 }
291 3 push @job_ids, $j;
292 } else {
293 0 say STDERR RED, "Job not submitted: $j", RESET;
294 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
295 0 say STDERR $job->view();
296 }
297 0 $err = 1;
298 0 last;
299 }
300 }
301 1 ▶ if (!$err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 1 0 1
302 1 say $_ for @job_ids;
303 }
304 } else {
305 1 for my $idx (0 .. $#jobs) {
306 1 ▶ say STDERR GREEN, "JOB SCRIPT\n", RESET if @jobs == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @jobs == 1 1 0 1
307 1 ▶ say STDERR GREEN, "JOB SCRIPT ", ($idx + 1), "/", scalar(@jobs), "\n", RESET if @jobs > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @jobs > 1 0 1 1
308 1 say $jobs[$idx]->script();
309 }
310 }
311  
312  
313 2 ▶if ($err) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err) { } 0 2 2
elsif ($run) { } 1 1 2
314 0 exit 1;
315 } elsif ($run) {
316 1 my @submitted = map { $_->jobid } @jobs;
317 1 say STDERR GREEN, "Jobs submitted: ", join(",", @submitted), RESET;
318 }
319 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
320 0 say STDERR <<END;
321  
322 Options:
323 -n, --name Job name [optional]
324 -q, --queue Queue name [default: nbi-short]
325 -m, --memory Memory to use [default: 8Gb]
326 -c, --cores Number of threads [default: 1]
327 -T, --time Time string [default: "0d 8h"]
328 --after INT Job ID to wait for [can be used multiple times]
329 -w, --tmpdir Temporary directory [default: /tmp]
330 -r, --run Run the job (otherwise, just print the script)
331 -f, --files Input files for array (either specify -f as many times as needed or
332 use a _quoted_ pattern like "*.fasta" to include multiple files)
333 -p, --params-array
334 TSV file for array jobs; refer to columns as ##1##, ##2##, ...
335 Mutually exclusive with --files
336 --placeholder Placeholder for array input files [default: #FILE#]
337 Ignored when --params-array is used
338 --gpu Request GPU resources (adds --gres=gpu:1)
339 --option VALUE Additional SLURM options (can be used multiple times)
340 --eco Schedule job in next low-energy window (overrides eco_default=0)
341 --no-eco Submit immediately, ignoring eco scheduling (overrides eco_default=1)
342 --verbose Verbose output
343 --help This help message
344 ----------------------------------------------------------
345 END
346 0 ▶ exit() if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
347 }
348  
349 1 ▶sub autoname {
Subroutine autoname: called CC=6 SCAR=20.2
350 1 my $string = shift;
351 1 my @parts = split(/\s+/, $string);
352 1 my @ints = ("bash", "perl", "python", "python3", "R", "Rscript", "sh", "zsh");
353 1 my @subs = ("bwa", "samtools", "seqfu", "seqkit", "bedtools", "taxonkit", "kmcp", "seqtk", "usearch", "vsearch");
354
355 # From each @parts, replace it with the first [A-Za-z0-9]+ part
356 1 for my $i (0..$#parts) {
357 4 $parts[$i] =~ /^([A-Za-z0-9]+)/;
358 4 $parts[$i] = $1;
359 }
360 # check if $fisrt is part of the @int array of strings
361 1 ▶ if (grep {$_ eq $parts[0]} @ints and defined $parts[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq $parts[0];} @ints and defined $parts[1]) { } 0 1 1
elsif (grep {$_ eq $parts[0];} @subs and defined $parts[1]) { } 0 1 1
Condition33%1/3
grep {$_ eq $parts[0];} @ints and defined $parts[1]
!ll&&!rl&&r
100
MC/DC0%0/2
grep {$_ eq $parts[0];} @ints and defined $parts[1]
grep {$_ eq $parts[0];} @ints defined $parts[1]
Truth table33%1/3
A and B
A= grep {$_ eq $parts[0];} @intsB= defined $parts[1]
ABresult
0X0
100
111
362 0 return $parts[1];
363 10 ▶ } elsif (grep {$_ eq $parts[0]} @subs and defined $parts[1]) {
Condition33%1/3
grep {$_ eq $parts[0];} @subs and defined $parts[1]
!ll&&!rl&&r
100
MC/DC0%0/2
grep {$_ eq $parts[0];} @subs and defined $parts[1]
grep {$_ eq $parts[0];} @subs defined $parts[1]
Truth table33%1/3
A and B
A= grep {$_ eq $parts[0];} @subsB= defined $parts[1]
ABresult
0X0
100
111
364 0 return $parts[0] . "-" . $parts[1];
365 } else {
366 1 return $parts[0];
367 }
368  
369 }
370  
371  
372 4 ▶sub update_queue {
Subroutine update_queue: called CC=4 SCAR=23.7
373 4 my ($queue, $time) = @_;
374 # If the queue has a star, it can be updated
375 4 ▶ if ($queue !~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($queue =~ /\*/u) 4 0 4
376 4 return $queue;
377 }
378 # Get time in hours
379 0 my $mock_opt = NBI::Opts->new(-time => $time);
380 0 my $time_h = $mock_opt->hours;
381
382 0 ▶ if ($time <= 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($time <= 2) { } 0 0 0
elsif ($time_h <= 48) { } 0 0 0
383 # Replace * with short
384 0 $queue =~ s/\*/short/g;
385 } elsif ($time_h <= 48) {
386 # Replace * with medium
387 0 $queue =~ s/\*/medium/g;
388 } else {
389 # Replace * with long
390 0 $queue =~ s/\*/long/g;
391 }
392 0 return $queue;
393 }
394  
395  
396 0 ▶sub expand_pattern_to_abs_paths {
Subroutine expand_pattern_to_abs_paths: not called CC=1 SCAR=2.6
397 0 my ($pattern) = @_;
398
399 # Get the current working directory
400 0 my $current_dir = getcwd();
401  
402 # Expand the pattern
403 0 ▶ my @files = glob($pattern);
Subroutine BEGIN: called CC=1 SCAR=0.2
404  
405 # Convert to absolute paths
406 0 my @abs_paths = map { File::Spec->rel2abs($_, $current_dir) } @files;
407  
408 0 return @abs_paths;
409 }
410  
411 3 ▶sub get_abs_files {
Subroutine get_abs_files: called CC=4 SCAR=25.3
412 3 my @files = @_;
413 3 for my $file (@files) {
414
415 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
416 0 push @abs_files, abs_path($file);
417 0 next;
418 } else {
419 0 my @tmpfiles = ();
420 0 push @tmpfiles, expand_pattern_to_abs_paths($file);
421 0 ▶ if (scalar @tmpfiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @tmpfiles) { } 0 0 0
422 0 push @abs_files, @tmpfiles;
423 } else {
424 0 say STDERR RED, BOLD, "ERROR:", RESET, " $file does not exist!";
425 0 exit 1;
426 }
427 }
428
429 }
430 3 return @abs_files;
431 }
432  
433 3 ▶sub get_params_array_info {
Subroutine get_params_array_info: called CC=13 SCAR=27.2
434 3 my ($file, $command) = @_;
435 3 ▶ return (undef, 0) unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 3 3
436  
437 3 ▶ unless (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 3 3
438 0 say STDERR RED, BOLD, "ERROR:", RESET, " $file does not exist!";
439 0 exit 1;
440 }
441  
442 3 my $abs_file = abs_path($file);
443 3 ▶ open(my $fh, "<", $abs_file) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $abs_file or do { say STDERR RED(), BOLD(), "ERROR:", RESET(), " Cannot read ${abs_file}: $!"; exit 1 } 3 0 3
444 0 say STDERR RED, BOLD, "ERROR:", RESET, " Cannot read $abs_file: $!";
445 0 exit 1;
446 };
447  
448 3 my %placeholders;
449 3 ▶ while ($command =~ /##(\d+)##/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /##(\d+)##/gu) 5 3 8
450 5 $placeholders{$1} = 1;
451 }
452 3 my $max_placeholder = 0;
453 3 for my $n (keys %placeholders) {
454 5 ▶ $max_placeholder = $n if $n > $max_placeholder;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max_placeholder 4 1 5
455 }
456 3 ▶ if ($max_placeholder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_placeholder == 0) 0 3 3
457 0 say STDERR RED, BOLD, "ERROR:", RESET, " --params-array requires placeholders like ##1## in the command";
458 0 exit 1;
459 }
460  
461 3 my $rows = 0;
462 3 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 12 2 14
463 12 chomp $line;
464 12 $line =~ s/\r$//;
465 12 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/u 1 11 12
466 11 ▶ next if $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/u 1 10 11
467 10 my @fields = split /\t/, $line, -1;
468 10 ▶ if (scalar(@fields) < $max_placeholder) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @fields < $max_placeholder) 1 9 10
469 1 say STDERR RED, BOLD, "ERROR:", RESET, " $abs_file row ", ($rows + 1), " has ", scalar(@fields), " columns but the command requires ##$max_placeholder##";
470 1 exit 1;
471 }
472 9 $rows++;
473 }
474 2 close $fh;
475  
476 2 ▶ if ($rows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows == 0) 0 2 2
477 0 say STDERR RED, BOLD, "ERROR:", RESET, " $abs_file does not contain any usable TSV rows";
478 0 exit 1;
479 }
480  
481 2 return ($abs_file, $rows);
482 }
483  
484 2 ▶sub build_array_chunks {
Subroutine build_array_chunks: called CC=7 SCAR=19.6
485 2 my ($total_tasks, $task_limit) = @_;
486 2 ▶ return ([0, 0]) unless $total_tasks;
Branch: 1 branch
BranchTrueFalseTotal
unless $total_tasks 0 2 2
487 2 ▶ return ([0, $total_tasks]) unless defined $task_limit && $task_limit > 0 && $total_tasks > $task_limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task_limit and $task_limit > 0 and $total_tasks > $task_limit 1 1 2
Condition66%4/6
defined $task_limit and $task_limit > 0 and $total_tasks > $task_limit
!ll&&!rl&&r
101
defined $task_limit and $task_limit > 0
!ll&&!rl&&r
101
MC/DC33%1/3
defined $task_limit and $task_limit > 0 and $total_tasks > $task_limit
defined $task_limit $task_limit > 0 $total_tasks > $task_limit
Truth table50%2/4
A and B and C
A= defined $task_limitB= $task_limit > 0C= $total_tasks > $task_limit
ABCresult
0XX0
10X0
1100
1111
488  
489 1 my @chunks;
490 1 ▶ for (my $offset = 0; $offset < $total_tasks; $offset += $task_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $total_tasks) 3 1 4
491 3 my $chunk_tasks = $task_limit;
492 3 my $remaining = $total_tasks - $offset;
493 3 ▶ $chunk_tasks = $remaining if $remaining < $chunk_tasks;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining < $chunk_tasks 1 2 3
494 3 push @chunks, [$offset, $chunk_tasks];
495 }
496 1 return @chunks;
497 }
498  
499 2 ▶sub get_array_task_limit {
Subroutine get_array_task_limit: called CC=16 SCAR=45.0
500 2 my ($config) = @_;
501  
502 2 ▶ if (defined $ENV{NBI_MAX_ARRAY_SIZE} && $ENV{NBI_MAX_ARRAY_SIZE} =~ /^\d+$/ && $ENV{NBI_MAX_ARRAY_SIZE} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"NBI_MAX_ARRAY_SIZE"} and $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/u and $ENV{"NBI_MAX_ARRAY_SIZE"} > 0) 1 1 2
Condition66%4/6
defined $ENV{"NBI_MAX_ARRAY_SIZE"} and $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/u and $ENV{"NBI_MAX_ARRAY_SIZE"} > 0
!ll&&!rl&&r
101
defined $ENV{"NBI_MAX_ARRAY_SIZE"} and $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/u
!ll&&!rl&&r
101
MC/DC33%1/3
defined $ENV{"NBI_MAX_ARRAY_SIZE"} and $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/u and $ENV{"NBI_MAX_ARRAY_SIZE"} > 0
defined $ENV{"NBI_MAX_ARRAY_SIZE"} $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/u $ENV{"NBI_MAX_ARRAY_SIZE"} > 0
Truth table50%2/4
A and B and C
A= defined $ENV{"NBI_MAX_ARRAY_SIZE"}B= $ENV{"NBI_MAX_ARRAY_SIZE"} =~ /^\d+$/uC= $ENV{"NBI_MAX_ARRAY_SIZE"} > 0
ABCresult
0XX0
10X0
1100
1111
503 1 return $ENV{NBI_MAX_ARRAY_SIZE};
504 }
505 1 ▶ if (defined $config->{max_array_size} && $config->{max_array_size} =~ /^\d+$/ && $config->{max_array_size} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"max_array_size"} and $config->{"max_array_size"} =~ /^\d+$/u and $config->{"max_array_size"} > 0) 0 1 1
Condition33%2/6
defined $config->{"max_array_size"} and $config->{"max_array_size"} =~ /^\d+$/u and $config->{"max_array_size"} > 0
!ll&&!rl&&r
100
defined $config->{"max_array_size"} and $config->{"max_array_size"} =~ /^\d+$/u
!ll&&!rl&&r
100
MC/DC0%0/3
defined $config->{"max_array_size"} and $config->{"max_array_size"} =~ /^\d+$/u and $config->{"max_array_size"} > 0
defined $config->{"max_array_size"} $config->{"max_array_size"} =~ /^\d+$/u $config->{"max_array_size"} > 0
Truth table25%1/4
A and B and C
A= defined $config->{"max_array_size"}B= $config->{"max_array_size"} =~ /^\d+$/uC= $config->{"max_array_size"} > 0
ABCresult
0XX0
10X0
1100
1111
506 0 return $config->{max_array_size};
507 }
508  
509 1 my $output = `scontrol show config 2>/dev/null`;
510 1 ▶ return undef if $? != 0 || !$output;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 or not $output 1 0 1
Condition33%1/3
$? != 0 or not $output
l!l&&r!l&&!r
100
MC/DC0%0/2
$? != 0 or not $output
$? != 0 not $output
Truth table33%1/3
A or not(B)
A= $? != 0B= not $output
ABresult
1X1
011
000
511  
512 0 my @limits;
513 0 ▶ if ($output =~ /^MaxArraySize\s*=\s*(\d+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /^MaxArraySize\s*=\s*(\d+)/mu) 0 0 0
514 0 ▶ push @limits, $1 if $1 > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $1 > 0 0 0 0
515 }
516 0 ▶ if ($output =~ /max_array_tasks=(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /max_array_tasks=(\d+)/u) 0 0 0
517 0 ▶ push @limits, $1 if $1 > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $1 > 0 0 0 0
518 }
519 0 ▶ return undef unless @limits;
Branch: 1 branch
BranchTrueFalseTotal
unless @limits 0 0 0
520  
521 0 my $limit = shift @limits;
522 0 for my $candidate (@limits) {
523 0 ▶ $limit = $candidate if $candidate < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate < $limit 0 0 0
524 }
525 0 return $limit;
526 }
527  
528 __END__
« bin/slurm-load ↑ Summary blib/lib/NBI/QueuedJob.pm »