bin/session

filter: Statementstmt 54.7% 58 / 106 Branchbran 18.1% 8 / 44 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 66.6% 12 / 18 Podpod n/a totaltotal 45.4% 89 / 196 CC 28 SCAR 35.4
CC
28
Cov
45%
main
86.1
parse_memory
30.0
partition_tag
12.0
CRAP 34.5
« blib/lib/NBI/Opts.pm ↑ Summary lib/NBI/Launcher.pm »
1 #!/usr/bin/env perl
2 #ABSTRACT: Start an interactive session on the cluster
3 #PODNAME: session
4 2 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use FindBin qw($RealBin);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶if (-e "$RealBin/../dist.ini") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$main::RealBin/../dist.ini") 1 0 1
11 1 ▶ say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 1 1
12 2 ▶ use lib "$RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 2 ▶use NBI::Slurm;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 my $BIN = basename($0);
16  
17 1 ▶main() unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 1 0 1
18 1;
19  
20 1 ▶sub main {
Subroutine main: called CC=15 SCAR=44.6
21 1 my $USER = $ENV{USER};
22 1 my $config = NBI::Slurm::load_config("$USER/.nbislurm.config");
23 1 ▶ my $DEFAULT_SESSION = $config->{'session'} // "";
Condition50%1/2
$config->{"session"} // ""
l!l
01
MC/DC0%0/1
$config->{"session"} // ""
$config->{"session"}
Truth table50%1/2
A
A= $config->{"session"}
Aresult
11
00
24 1 ▶ my $SPECIAL_SESSION = $config->{'special_session'} // "";
Condition50%1/2
$config->{"special_session"} // ""
l!l
01
MC/DC0%0/1
$config->{"special_session"} // ""
$config->{"special_session"}
Truth table50%1/2
A
A= $config->{"special_session"}
Aresult
11
00
25 1 ▶ my $DEFAULT_HOURS = $config->{'session_hours'} // 4;
Condition50%1/2
$config->{"session_hours"} // 4
l!l
01
MC/DC0%0/1
$config->{"session_hours"} // 4
$config->{"session_hours"}
Truth table50%1/2
A
A= $config->{"session_hours"}
Aresult
11
00
26 1 my $NAME = "session";
27 1 ▶ my $MEM_MB = $config->{'session_memory'} // 1000;
Condition50%1/2
$config->{"session_memory"} // 1000
l!l
01
MC/DC0%0/1
$config->{"session_memory"} // 1000
$config->{"session_memory"}
Truth table50%1/2
A
A= $config->{"session_memory"}
Aresult
11
00
28 1 ▶ my $CORES = $config->{'session_cpus'} // 1;
Condition50%1/2
$config->{"session_cpus"} // 1
l!l
01
MC/DC0%0/1
$config->{"session_cpus"} // 1
$config->{"session_cpus"}
Truth table50%1/2
A
A= $config->{"session_cpus"}
Aresult
11
00
29 1 my $DAYS = 0;
30 1 my $OPT_HOURS;
31 my $OPT_TIME;
32 1 my $OPT_INTEL;
33 1 my $OPT_PARTITION;
34 1 my $OPT_SPECIAL;
35 1 my $OPT_VERBOSE;
36  
37 GetOptions(
38 'c|threads=i' => \$CORES,
39 'm|mem=s' => \$MEM_MB,
40 'd|days=i' => \$DAYS,
41 'h|hours=i' => \$OPT_HOURS,
42 'T|t|time=s' => \$OPT_TIME,
43 'n|name=s' => \$NAME,
44 'q|queue=s' => \$OPT_PARTITION,
45 's|special' => \$OPT_SPECIAL,
46 'i|intel' => \$OPT_INTEL,
47 'verbose' => \$OPT_VERBOSE,
48 1 ▶ 'version' => sub { say "session v", $NBI::Slurm::VERSION; exit(0); },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 0 ▶ 'help' => sub { usage(); exit(0); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 1 ▶ ) or usage(1);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("c|threads=i", \$CORES, "m|mem=s", \$MEM_MB, "d|days=i", \$DAYS, "h|hours=i", \$OPT_HOURS, "T|t|time=s", \$OPT_TIME, "n|name=s", \$NAME, "q|queue=s", \$OPT_PARTITION, "s|special", \$OPT_SPECIAL, "i|intel", \$OPT_INTEL, "verbose", \$OPT_VERBOSE, "version", sub { say "session v", $NBI::Slurm::VERSION; exit 0; } , "help", sub { usage(); exit 0; } ) or usage(1) 0 0 0
51  
52 # Get memory
53 0 $MEM_MB = parse_memory($MEM_MB);
54  
55 0 ▶ if ($MEM_MB < 350) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MEM_MB < 350) 0 0 0
56 0 say STDERR RED, "[WARNING]", RESET, "Memory set to $MEM_MB Mb, automatically setting to $MEM_MB Gb", RESET;
57 0 $MEM_MB *= 1000;
58 }
59  
60 0 my $time = session_resolve_time(
61 hours => $OPT_HOURS,
62 time_string => $OPT_TIME,
63 default_hours => $DEFAULT_HOURS,
64 days => $DAYS,
65 );
66  
67 # Get queue
68 0 ▶ if (not $OPT_PARTITION) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($OPT_PARTITION) 0 0 0
69 0 ▶ if ($config->{'queue'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'queue'}) { } 0 0 0
70 0 $OPT_PARTITION = $config->{'queue'};
71 } else {
72 0 $OPT_PARTITION = partition_tag($time->{total_hours});
73 }
74 }
75  
76 0 my $PARAMS = $DEFAULT_SESSION;
77 0 ▶ $PARAMS .= $OPT_INTEL ? " --constraint=intel " : "";
Branch: 1 branch
BranchTrueFalseTotal
$OPT_INTEL ? : 0 0 0
78 0 ▶ $PARAMS .= $OPT_SPECIAL ? " $SPECIAL_SESSION " : "";
Branch: 1 branch
BranchTrueFalseTotal
$OPT_SPECIAL ? : 0 0 0
79  
80 0 ▶ my $PARTITION //= $OPT_PARTITION;
Condition0%0/3
my $PARTITION //= $OPT_PARTITION
l!l&&r!l&&!r
000
MC/DC0%0/2
my $PARTITION //= $OPT_PARTITION
my $PARTITION $OPT_PARTITION
Truth table0%0/3
A //= B
A= my $PARTITIONB= $OPT_PARTITION
ABresult
1X1
011
000
81  
82 0 say STDERR "-" x 40;
83 print STDERR YELLOW,
84 "job name ", RESET, $NAME, "\n",
85 YELLOW, "memory ", RESET, $MEM_MB, " Mb\n",
86 YELLOW, "cores ", RESET, $CORES, "\n",
87 0 YELLOW, "time ", RESET, $time->{slurm}, "\n",
88 YELLOW, "partition ", RESET, $PARTITION, "\n";
89  
90 0 say STDERR "-" x 40;
91 0 my $cmd = qq(srun --job-name=$NAME --mem=$MEM_MB -c $CORES --time=$time->{slurm} -n 1 --disable-status -s --pty --partition=$PARTITION $PARAMS -u bash -li);
92  
93 0 ▶ say STDERR $cmd if ($OPT_VERBOSE);
Branch: 1 branch
BranchTrueFalseTotal
if $OPT_VERBOSE 0 0 0
94  
95 0 ▶ if (has_srun() == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_srun() == 0) 0 0 0
96 0 say STDERR RED, "[ERROR] srun not found... Are you on the cluster?", RESET;
97 }
98 0 exec($cmd);
99 }
100  
101 0 ▶sub partition_tag {
Subroutine partition_tag: not called CC=3 SCAR=24.8
102 0 my $hours = shift;
103 0 ▶ if ($hours <= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hours <= 2) { } 0 0 0
elsif ($hours <= 8) { } 0 0 0
104 0 return "qib-short,nbi-short";
105 } elsif ($hours <= 8) {
106 0 return "qib-medium,nbi-medium";
107 } else {
108 0 return "qib-long,nbi-long";
109 }
110 }
111  
112 5 ▶sub session_resolve_time {
Subroutine session_resolve_time: called CC=6 SCAR=18.0
113 5 my %args = @_;
114 confess "ERROR: --hours and --time are mutually exclusive\n"
115 5 ▶ if defined $args{hours} && defined $args{time_string};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"hours"} and defined $args{"time_string"} 1 4 5
Condition100%3/3
defined $args{"hours"} and defined $args{"time_string"}
!ll&&!rl&&r
311
MC/DC100%2/2
defined $args{"hours"} and defined $args{"time_string"}
defined $args{"hours"} defined $args{"time_string"}
Truth table100%3/3
A and B
A= defined $args{"hours"}B= defined $args{"time_string"}
ABresult
0X0
100
111
116  
117 my $hours = defined $args{time_string}
118 ? NBI::Opts::_time_to_hour($args{time_string})
119 : defined $args{hours}
120 ? $args{hours}
121 4 ▶ : $args{default_hours};
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'time_string'} ? : 3 1 4
defined $args{'hours'} ? : 1 0 1
122  
123 4 ▶ $hours += ($args{days} // 0) * 24;
Condition50%1/2
$args{"days"} // 0
l!l
40
MC/DC0%0/1
$args{"days"} // 0
$args{"days"}
Truth table50%1/2
A
A= $args{"days"}
Aresult
11
00
124  
125 return {
126 4 total_hours => $hours,
127 slurm => session_hours_to_slurm($hours),
128 };
129 }
130  
131 0 ▶sub hours_to_days_hours {
Subroutine hours_to_days_hours: not called CC=1 SCAR=6.9
132 0 my $hours = shift;
133 0 my $days = int($hours / 24);
134 0 my $hours = $hours % 24;
135 0 return ($days, $hours);
136 }
137  
138 6 ▶sub session_hours_to_slurm {
Subroutine session_hours_to_slurm: called CC=1 SCAR=0.0
139 6 my $hours = shift;
140 6 my $seconds = int($hours * 3600);
141 6 my $days = int($seconds / 86400);
142 6 $seconds %= 86400;
143 6 my $hh = int($seconds / 3600);
144 6 $seconds %= 3600;
145 6 my $mm = int($seconds / 60);
146 6 my $ss = $seconds % 60;
147 6 return sprintf("%d-%02d:%02d:%02d", $days, $hh, $mm, $ss);
148 }
149  
150  
151 0 ▶sub parse_memory {
Subroutine parse_memory: not called CC=5 SCAR=34.0
152 # Parse string return mb
153 0 my $mem = shift;
154 0 my $mb = 0;
155 0 ▶ if ($mem =~ /^(\d+)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($mem =~ /^(\d+)$/u) { } 0 0 0
elsif ($mem =~ /(\d+)\s?Gb?/iu) { } 0 0 0
elsif ($mem =~ /(\d+)\s?Mb?/iu) { } 0 0 0
elsif ($mem =~ /(\d+)\s?Kb?/iu) { } 0 0 0
156 0 $mb = $1;
157 } elsif ($mem =~ /(\d+)\s?Gb?/i) {
158 0 $mb = $1 * 1000;
159 } elsif ($mem =~ /(\d+)\s?Mb?/i) {
160 0 $mb = $1;
161 } elsif ($mem =~ /(\d+)\s?Kb?/i) {
162 0 $mb = int($1 / 1000);
163 } else {
164 0 $mb = $mem;
165 }
166 0 return $mb;
167 }
168 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
169 0 print <<"EOF";
170 Usage: $BIN [options]
171  
172 Options:
173 -c, --threads Number of CPU cores (default: 1)
174 -m, --mem Memory size in MB (default: 1000)
175 -h, --hours Number of hours (default: 4; mutually exclusive with --time)
176 -t, --time Time string like 12h, 1d, 2h30m
177 -n, --name Job name (default: session)
178 -q, --queue Queue/partition name (default: determined by hours)
179 -s, --special Enable the special parameter string from the config file (default: off)
180 -i, --intel Use Intel constraint (default: off)
181  
182 EOF
183 0 ▶ exit() if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
184 }
185  
186 0 ▶sub has_srun {
Subroutine has_srun: not called CC=2 SCAR=17.9
187 # Check if srun is available
188 0 my $srun = `srun --version 2> /dev/null`;
189 0 chomp $srun;
190 0 ▶ if ($srun eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($srun eq "") 0 0 0
191 0 return 0;
192 }
193 0 return 1;
194 }
195  
196 __END__
« blib/lib/NBI/Opts.pm ↑ Summary lib/NBI/Launcher.pm »