| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #ABSTRACT: NBI Slurm module | 
| 2 | 5 |  |  | 5 |  | 160927 | use strict; | 
|  | 5 |  |  |  |  | 39 |  | 
|  | 5 |  |  |  |  | 146 |  | 
| 3 | 5 |  |  | 5 |  | 26 | use warnings; | 
|  | 5 |  |  |  |  | 7 |  | 
|  | 5 |  |  |  |  | 167 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package NBI::Slurm; | 
| 6 | 5 |  |  | 5 |  | 2262 | use NBI::Job; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 251 |  | 
| 7 | 5 |  |  | 5 |  | 2435 | use NBI::Opts; | 
|  | 5 |  |  |  |  | 14 |  | 
|  | 5 |  |  |  |  | 253 |  | 
| 8 | 5 |  |  | 5 |  | 33 | use base qw(Exporter); | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 4717 |  | 
| 9 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 10 |  |  |  |  |  |  | our @EXPORT = qw(Job Opts load_config %FORMAT_STRINGS); | 
| 11 |  |  |  |  |  |  | $NBI::Slurm::VERSION = '0.6.3'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our %FORMAT_STRINGS = ( | 
| 17 |  |  |  |  |  |  | 'account'    => '%a', | 
| 18 |  |  |  |  |  |  | 'jobid'      => '%A', | 
| 19 |  |  |  |  |  |  | 'jobname'    => '%j', | 
| 20 |  |  |  |  |  |  | 'cpus'       => '%C', | 
| 21 |  |  |  |  |  |  | 'end_time'   => '%E', | 
| 22 |  |  |  |  |  |  | 'start_time' => '%S', | 
| 23 |  |  |  |  |  |  | 'total_time' => '%l', | 
| 24 |  |  |  |  |  |  | 'time_left'  => '%L', | 
| 25 |  |  |  |  |  |  | 'memory'     => '%m', | 
| 26 |  |  |  |  |  |  | 'command'    => '%o', | 
| 27 |  |  |  |  |  |  | 'queue'      => '%P', | 
| 28 |  |  |  |  |  |  | 'reason'     => '%r', | 
| 29 |  |  |  |  |  |  | 'status'     => '%T', # short: %t | 
| 30 |  |  |  |  |  |  | 'workdir'    => '%Z', | 
| 31 |  |  |  |  |  |  | 'user'       => '%u', | 
| 32 |  |  |  |  |  |  | ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub load_config { | 
| 36 | 0 |  |  | 0 | 1 | 0 | my $filename = shift; | 
| 37 | 0 | 0 |  |  |  | 0 | if (! $filename) { | 
| 38 | 0 |  |  |  |  | 0 | $filename = "$ENV{HOME}/.nbislurm.config"; | 
| 39 |  |  |  |  |  |  | } | 
| 40 | 0 |  |  |  |  | 0 | my $config = {}; | 
| 41 | 0 | 0 |  |  |  | 0 | if (! -e "$filename") { | 
| 42 | 0 | 0 |  |  |  | 0 | say STDERR "# Config file not found: $filename" if ($ENV{"DEBUG"}); | 
| 43 | 0 |  |  |  |  | 0 | return $config; | 
| 44 |  |  |  |  |  |  | } | 
| 45 | 0 | 0 |  |  |  | 0 | open(my $fh, "<", $filename) or die "Cannot open $filename: $!"; | 
| 46 | 0 |  |  |  |  | 0 | while (<$fh>) { | 
| 47 | 0 |  |  |  |  | 0 | chomp; | 
| 48 | 0 | 0 |  |  |  | 0 | next if (/^\s*$/); | 
| 49 | 0 | 0 |  |  |  | 0 | next if (/^#/); | 
| 50 | 0 | 0 |  |  |  | 0 | next if (/^;/); | 
| 51 | 0 |  |  |  |  | 0 | my ($key, $value) = split(/=/, $_); | 
| 52 |  |  |  |  |  |  | # discard keys with spaces | 
| 53 | 0 | 0 |  |  |  | 0 | next if ($key =~ /\s/); | 
| 54 | 0 |  |  |  |  | 0 | $config->{$key} = $value; | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 0 |  |  |  |  | 0 | close  $fh; | 
| 57 | 0 |  |  |  |  | 0 | return $config; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub has_squeue { | 
| 62 | 1 |  |  | 1 | 1 | 78 | my $cmd = "squeue --version"; | 
| 63 | 1 |  |  |  |  | 4604 | my $output = `$cmd 2>&1`; | 
| 64 | 1 | 50 |  |  |  | 64 | if ($? == 0) { | 
| 65 | 0 |  |  |  |  | 0 | return 1; | 
| 66 |  |  |  |  |  |  | } else { | 
| 67 | 1 |  |  |  |  | 37 | return 0; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub queues { | 
| 72 | 0 |  |  | 0 | 1 |  | my $can_fail = shift; | 
| 73 |  |  |  |  |  |  | # Retrieve queues from SLURM | 
| 74 | 0 |  |  |  |  |  | my $cmd = "sinfo --format '%P' --noheader"; | 
| 75 | 0 |  |  |  |  |  | my @output = `$cmd 2>/dev/null`; | 
| 76 | 0 | 0 | 0 |  |  |  | if ($? != 0 and ! $can_fail) { | 
| 77 | 0 |  |  |  |  |  | Carp::croak "ERROR NBI::Slurm: sinfo failed. Are you in a SLURM cluster?\n"; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 0 |  |  |  |  |  | chomp @output; | 
| 80 | 0 |  |  |  |  |  | return @output; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub valid_queue { | 
| 84 | 0 |  |  | 0 | 1 |  | my $queue = shift; | 
| 85 | 0 |  |  |  |  |  | my @queues = queues(1); | 
| 86 | 0 |  |  |  |  |  | my @input_queues = split(/,/, $queue); | 
| 87 | 0 |  |  |  |  |  | foreach my $input_queue (@input_queues) { | 
| 88 | 0 | 0 |  |  |  |  | if (! grep { $_ eq $input_queue } @queues) { | 
|  | 0 |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | return 0; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } | 
| 92 | 0 |  |  |  |  |  | return 1; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub days_since_update { | 
| 97 | 0 |  |  | 0 | 1 |  | my $file_path = shift; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # Check if the required modules can be loaded | 
| 100 | 0 |  |  |  |  |  | eval { | 
| 101 | 0 |  |  |  |  |  | require File::Spec; | 
| 102 | 0 |  |  |  |  |  | require Time::Piece; | 
| 103 | 0 |  |  |  |  |  | require Time::Seconds; | 
| 104 |  |  |  |  |  |  | }; | 
| 105 | 0 | 0 |  |  |  |  | if ($@) { | 
| 106 | 0 |  |  |  |  |  | return -1;  # Failed to load required module(s) | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | # Check if the file exists | 
| 110 | 0 | 0 |  |  |  |  | unless (-e $file_path) { | 
| 111 | 0 |  |  |  |  |  | return -1;  # File not found | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | # Get the file's last modification time | 
| 115 | 0 |  |  |  |  |  | my $last_modified = (stat($file_path))[9]; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # Calculate the number of days since the last modification | 
| 118 | 0 |  |  |  |  |  | my $current_time = time(); | 
| 119 | 0 |  |  |  |  |  | my $days_since_update = int(($current_time - $last_modified) / (24 * 60 * 60)); | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 |  |  |  |  |  | return $days_since_update; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | 1; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | __END__ |