File Coverage

bin/session
Criterion Covered Total %
statement 38 89 42.7
branch 2 36 5.5
condition 5 13 38.4
subroutine 8 14 57.1
pod n/a
total 53 152 34.8


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #ABSTRACT: Start an interactive session on the cluster
3             #PODNAME: session
4 1     1   3619 use v5.16;
  1         3  
5 1     1   524 use Getopt::Long;
  1         12340  
  1         5  
6 1     1   132 use File::Basename;
  1         1  
  1         80  
7 1     1   452 use Term::ANSIColor qw(:constants);
  1         8435  
  1         1172  
8 1     1   374 use FindBin qw($RealBin);
  1         916  
  1         144  
9 1 50       134880 if (-e "$RealBin/../dist.ini") {
  0         0  
10 1 50       8 say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
11 1     1   332 use lib "$RealBin/../lib";
  1         517  
  1         5  
12             }
13 1     1   514 use NBI::Slurm;
  1         4  
  1         114802  
14 1         85 my $BIN = basename($0);
15 1         6 my $USER = $ENV{USER};
16 1         9 my $config = NBI::Slurm::load_config("$USER/.nbislurm.config");
17 1   50     10 my $DEFAULT_SESSION = $config->{'session'} // "";
18 1   50     6 my $SPECIAL_SESSION = $config->{'special_session'} // "";
19 1         4 my $NAME = "session";
20 1   50     5 my $MEM_MB = $config->{'session_memory'} // 1000;
21 1   50     8 my $CORES = $config->{'session_cpus'} // 1;
22 1         3 my $DAYS = 0;
23 1   50     30 my $HOURS = $config->{'session_hours'} // 4;
24 1         5 my $OPT_INTEL;
25             my $OPT_PARTITION;
26 1         0 my $OPT_SPECIAL;
27 1         0 my $OPT_VERBOSE;
28             GetOptions(
29             'c|t|threads=i' => \$CORES,
30             'm|mem=s' => \$MEM_MB,
31             'd|days=i' => \$DAYS,
32             'h|hours=i' => \$HOURS,
33             'n|name=s' => \$NAME,
34             'q|queue=s' => \$OPT_PARTITION,
35             's|special' => \$OPT_SPECIAL,
36             'i|intel' => \$OPT_INTEL,
37             'verbose' => \$OPT_VERBOSE,
38 1     1   1913 'version' => sub { say "session v", $NBI::Slurm::VERSION; exit(0); },
  1         123  
39 0     0   0 'help' => sub { usage(); exit(0); },
  0         0  
40 1 0       81 ) or usage(1);
41              
42             # Get memory
43 0         0 $MEM_MB = parse_memory($MEM_MB);
44              
45 0 0       0 if ($MEM_MB < 350) {
46 0         0 say STDERR RED, "[WARNING]", RESET, "Memory set to $MEM_MB Mb, automatically setting to $MEM_MB Gb", RESET;
47 0         0 $MEM_MB *= 1000;
48             }
49             # Get hours / days
50 0         0 my $ADD_DAYS = 0;
51 0         0 ($ADD_DAYS, $HOURS) = hours_to_days_hours($HOURS);
52 0         0 $DAYS += $ADD_DAYS;
53              
54             # Get queue
55 0 0       0 if (not $OPT_PARTITION) {
56 0 0       0 if ($config->{'queue'}) {
57 0         0 $OPT_PARTITION = $config->{'queue'};
58             } else {
59 0         0 $OPT_PARTITION = partition_tag($HOURS);
60             }
61             }
62              
63 0         0 my $PARAMS = $DEFAULT_SESSION;
64 0 0       0 $PARAMS .= $OPT_INTEL ? " --constraint=intel " : "";
65 0 0       0 $PARAMS .= $OPT_SPECIAL ? " $SPECIAL_SESSION " : "";
66              
67 0   0     0 my $PARTITION //= $OPT_PARTITION;
68              
69 0         0 say STDERR "-" x 40;
70 0         0 print STDERR YELLOW,
71             "job name ", RESET, $NAME, "\n",
72             YELLOW, "memory ", RESET, $MEM_MB, " Mb\n",
73             YELLOW, "cores ", RESET, $CORES, "\n",
74             YELLOW, "time ", RESET, "$DAYS days $HOURS hours", "\n",
75             YELLOW, "partition ", RESET, $PARTITION, "\n";
76              
77 0         0 say STDERR "-" x 40;
78 0         0 my $cmd = qq(srun --job-name=$NAME --mem=$MEM_MB -c $CORES --time=$DAYS-$HOURS -n 1 --disable-status -s --pty --partition=$PARTITION $PARAMS -u bash -li);
79              
80 0 0       0 say STDERR $cmd if ($OPT_VERBOSE);
81              
82 0 0       0 if (has_srun() == 0) {
83 0         0 say STDERR RED, "[ERROR] srun not found... Are you on the cluster?", RESET;
84             }
85 0         0 exec($cmd);
86              
87             sub partition_tag {
88 0     0     my $hours = shift;
89 0 0         if ($hours <= 2) {
    0          
90 0           return "qib-short,nbi-short";
91             } elsif ($hours <= 8) {
92 0           return "qib-medium,nbi-medium";
93             } else {
94 0           return "qib-long,nbi-long";
95             }
96             }
97             sub hours_to_days_hours {
98 0     0     my $hours = shift;
99 0           my $days = int($hours / 24);
100 0           my $hours = $hours % 24;
101 0           return ($days, $hours);
102             }
103              
104              
105             sub parse_memory {
106             # Parse string return mb
107 0     0     my $mem = shift;
108 0           my $mb = 0;
109 0 0         if ($mem =~ /^(\d+)$/) {
    0          
    0          
    0          
110 0           $mb = $1;
111             } elsif ($mem =~ /(\d+)\s?Gb?/i) {
112 0           $mb = $1 * 1000;
113             } elsif ($mem =~ /(\d+)\s?Mb?/i) {
114 0           $mb = $1;
115             } elsif ($mem =~ /(\d+)\s?Kb?/i) {
116 0           $mb = int($1 / 1000);
117             } else {
118 0           $mb = $mem;
119             }
120 0           return $mb;
121             }
122             sub usage {
123 0     0     print <<"EOF";
124             Usage: $BIN [options]
125              
126             Options:
127             -c, --threads Number of CPU cores (default: 1)
128             -m, --mem Memory size in MB (default: 1000)
129             -h, --hours Number of hours (default: 4)
130             -n, --name Job name (default: session)
131             -q, --queue Queue/partition name (default: determined by hours)
132             -s, --special Enable the special parameter string from the config file (default: off)
133             -i, --intel Use Intel constraint (default: off)
134              
135             EOF
136 0 0         exit() if ($_[0]);
137             }
138              
139             sub has_srun {
140             # Check if srun is available
141 0     0     my $srun = `srun --version 2> /dev/null`;
142 0           chomp $srun;
143 0 0         if ($srun eq "") {
144 0           return 0;
145             }
146 0           return 1;
147             }
148              
149             __END__