[9] | 1 | #!/usr/bin/perl |
---|
| 2 | # |
---|
| 3 | # 2011/11/03 gabriel |
---|
| 4 | |
---|
| 5 | use strict; |
---|
| 6 | |
---|
| 7 | use Getopt::Long(); |
---|
| 8 | use Pod::Usage; |
---|
| 9 | use Coro; |
---|
[12] | 10 | use Coro::Signal; |
---|
[9] | 11 | use Coro::Semaphore; |
---|
[96] | 12 | use Coro::AnyEvent; |
---|
[94] | 13 | use Coro::Handle; |
---|
| 14 | use IO::File; |
---|
[9] | 15 | |
---|
| 16 | my $task = 0; |
---|
[95] | 17 | my $overload = 1; |
---|
[9] | 18 | my $file = ''; |
---|
[95] | 19 | my $dir; |
---|
| 20 | my $cmd; |
---|
[94] | 21 | my $logtrace; |
---|
[9] | 22 | my $verbose; |
---|
| 23 | my $help; |
---|
[94] | 24 | my $sig_transmit; |
---|
| 25 | my $sig_checkpoint = 'USR2'; |
---|
[97] | 26 | my $brake = 5; # five second time brake |
---|
[9] | 27 | |
---|
| 28 | Getopt::Long::GetOptions( |
---|
[92] | 29 | 'task=i' => \$task, |
---|
| 30 | 'overload=f' => \$overload, |
---|
| 31 | 'file=s' => \$file, |
---|
[95] | 32 | 'dir=s' => \$dir, |
---|
| 33 | 'cmd=s' => \$cmd, |
---|
[94] | 34 | 'logtrace=s' => \$logtrace, |
---|
[92] | 35 | 'verbose' => \$verbose, |
---|
| 36 | 'help' => \$help, |
---|
[94] | 37 | 'transmit' => \$sig_transmit, |
---|
| 38 | 'kill=s' => \$sig_checkpoint, |
---|
[97] | 39 | 'brake=i' => \$brake, |
---|
[92] | 40 | ) || pod2usage(-verbose => 0); |
---|
[9] | 41 | pod2usage(-verbose => 2) if $help; |
---|
| 42 | |
---|
| 43 | if ($task == 0) { |
---|
[92] | 44 | open(NODE_FILE, '<', "$ENV{OAR_NODE_FILE}") or die "can't open ENV{OAR_NODE_FILE}: $!"; |
---|
| 45 | $task++ while <NODE_FILE>; |
---|
| 46 | close NODE_FILE; |
---|
| 47 | } |
---|
[9] | 48 | |
---|
[94] | 49 | # re-run, keep trace of job already done |
---|
| 50 | my %state; |
---|
| 51 | my $log_h = IO::File->new(); |
---|
| 52 | if (-e "$logtrace") { |
---|
| 53 | $log_h->open("< $logtrace") |
---|
| 54 | or die "error: can't read log file: $!"; |
---|
| 55 | while (<$log_h>) { |
---|
| 56 | $state{$1} = 'start' if m/^start\s+job\s+([^\s]+)\s/; |
---|
| 57 | $state{$1} = 'end' if m/^end\s+job\s+([^\s]+)\s/; |
---|
| 58 | } |
---|
| 59 | $log_h->close(); |
---|
| 60 | } |
---|
| 61 | if ($logtrace) { |
---|
| 62 | $log_h->open(">> $logtrace") |
---|
| 63 | or die "error: can't append log file $logtrace: $!"; |
---|
| 64 | $log_h->autoflush; |
---|
| 65 | $log_h = unblock $log_h; |
---|
| 66 | } |
---|
| 67 | |
---|
[92] | 68 | # job to run |
---|
[9] | 69 | my @job = (); |
---|
[95] | 70 | #open (JOB_LIST, '<', "$file") or die "can't open $file: $!"; |
---|
| 71 | #while (<JOB_LIST>) { |
---|
| 72 | # chomp; |
---|
| 73 | # next if m/^#/; |
---|
| 74 | # push @job, $_ if m/^\s*oarsub/; |
---|
| 75 | # } |
---|
| 76 | #close JOB_LIST; |
---|
| 77 | if (-e "$file") { |
---|
| 78 | my $job_num = 0; |
---|
| 79 | open(JOB_LIST, '<', "$file") or die "error: can't open job file $file: $!"; |
---|
| 80 | while (my $job_cmd = <JOB_LIST>) { |
---|
| 81 | chomp $job_cmd; |
---|
| 82 | next if $job_cmd =~ m/^#/; |
---|
| 83 | next if $job_cmd =~ m/^\s*$/; |
---|
| 84 | # Add oarsub -S if not |
---|
| 85 | $job_cmd = "oarsub -S $job_cmd" if $job_cmd !~ m/^\s*oarsub/; |
---|
| 86 | $job_num++; |
---|
| 87 | my ($job_name) = $job_cmd =~ m/#.*?\bname=(\S+?)\b/i; |
---|
| 88 | $job_name ||= $job_num; |
---|
| 89 | push @job, { |
---|
| 90 | name => $job_name, |
---|
| 91 | cmd => "$job_cmd", |
---|
| 92 | num => $job_num, |
---|
| 93 | }; |
---|
| 94 | } |
---|
| 95 | close JOB_LIST; |
---|
[92] | 96 | } |
---|
[95] | 97 | else { |
---|
| 98 | # Add oarsub -S if not |
---|
| 99 | $cmd = "oarsub -S $cmd" if $cmd !~ m/^\s*oarsub/; |
---|
| 100 | my $job_num = 0; |
---|
| 101 | opendir(DIR, $dir) or die "error: can't open folder $dir: $!"; |
---|
| 102 | while (my $item = readdir(DIR)) { |
---|
| 103 | next if $item =~ m/^\./; |
---|
| 104 | next if $item =~ m/:/; |
---|
| 105 | next if $item =~ m/\.old$/; |
---|
| 106 | next if $item =~ m/\.sav$/; |
---|
| 107 | next if $item =~ m/\.bak$/; |
---|
| 108 | next if $item =~ m/\.no$/; |
---|
| 109 | next unless (-d "$dir/$item"); |
---|
| 110 | $job_num++; |
---|
| 111 | push @job, { |
---|
| 112 | name => $item, |
---|
| 113 | cmd => "cd $dir/$item/; $cmd", |
---|
| 114 | num => $job_num, |
---|
| 115 | }; |
---|
| 116 | } |
---|
| 117 | closedir DIR; |
---|
| 118 | } |
---|
[9] | 119 | |
---|
| 120 | my $container_id=$ENV{OAR_JOB_ID}; |
---|
| 121 | my $insert_oar_option = "-t inner=$container_id"; |
---|
| 122 | |
---|
| 123 | # interactive job |
---|
| 124 | if (not $container_id > 1) { |
---|
[92] | 125 | $insert_oar_option = ''; |
---|
| 126 | $overload = 1; |
---|
| 127 | } |
---|
[9] | 128 | |
---|
| 129 | my $finished = new Coro::Signal; |
---|
| 130 | my $job_todo = new Coro::Semaphore 0; |
---|
[95] | 131 | my $job_name_maxlen; |
---|
| 132 | for (@job) { |
---|
| 133 | $job_todo->up; |
---|
| 134 | $job_name_maxlen = length($_->{name}) if length($_->{name}) > $job_name_maxlen; |
---|
| 135 | } |
---|
[9] | 136 | |
---|
[95] | 137 | my $job_active = new Coro::Semaphore 0; |
---|
| 138 | |
---|
[9] | 139 | my %scheduled = (); |
---|
| 140 | |
---|
[94] | 141 | # OAR checkpoint and default signal SIGUSR2 |
---|
| 142 | my $oar_checkpoint = new Coro::Semaphore 0; |
---|
| 143 | my $notify = new Coro::Signal; |
---|
| 144 | $SIG{$sig_checkpoint} = sub { |
---|
| 145 | print "warning: receive checkpoint at " |
---|
| 146 | . time |
---|
| 147 | . ", no new job, just finishing running job\n" |
---|
| 148 | if $verbose; |
---|
| 149 | $oar_checkpoint->up(); |
---|
| 150 | $notify->send if $sig_transmit; |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | # asynchrone notify job |
---|
| 154 | async { |
---|
| 155 | while () { |
---|
| 156 | $notify->wait; |
---|
| 157 | |
---|
| 158 | for my $job_pid (keys %scheduled) { |
---|
| 159 | system "oardel --checkpoint --signal $sig_checkpoint $job_pid"; |
---|
| 160 | cede; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
[92] | 165 | # asynchrone start job block |
---|
[9] | 166 | async { |
---|
[92] | 167 | JOB: |
---|
| 168 | for my $job (@job) { |
---|
[95] | 169 | my $job_name = $job->{name}; |
---|
| 170 | my $job_cmd = $job->{cmd}; |
---|
| 171 | |
---|
| 172 | # job has been already run ? |
---|
| 173 | if (exists $state{$job_name}) { |
---|
| 174 | if ($state{$job_name} eq 'start') { |
---|
| 175 | print "warning: job $job_name was not clearly finished, relaunching...\n" |
---|
| 176 | if $verbose; |
---|
| 177 | } |
---|
| 178 | elsif ($state{$job_name} eq 'end') { |
---|
| 179 | delete $state{$job_name}; # free memory |
---|
| 180 | $job_todo->down; |
---|
| 181 | print "warning: job $job_name already run\n" if $verbose; |
---|
| 182 | cede; |
---|
| 183 | next JOB; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
[92] | 187 | while ($job_active->count >= $task*$overload) { |
---|
| 188 | cede; |
---|
| 189 | } |
---|
[94] | 190 | |
---|
| 191 | # no more launch job when OAR checkpointing |
---|
| 192 | last JOB if $oar_checkpoint->count() > 0; |
---|
| 193 | |
---|
[95] | 194 | $job_cmd =~ s/^\b(oarsub)\b/$1 $insert_oar_option/; |
---|
| 195 | print "$job_cmd" if $verbose; |
---|
| 196 | my $job_id = `$job_cmd|grep ^OAR_JOB_ID|cut -f 2 -d '='`; |
---|
[92] | 197 | chomp $job_id; |
---|
| 198 | if ($job_id > 1) { |
---|
[95] | 199 | $scheduled{$job_id} = { |
---|
| 200 | name => $job_name, |
---|
| 201 | }; |
---|
[92] | 202 | $job_active->up; |
---|
[95] | 203 | |
---|
| 204 | my $msg = sprintf "start job %${job_name_maxlen}s / %i at %s\n", |
---|
| 205 | $job_name, $job_id, time; |
---|
| 206 | $log_h->print($msg) if $logtrace; |
---|
| 207 | print($msg) if $verbose; |
---|
[92] | 208 | } |
---|
| 209 | cede; |
---|
[96] | 210 | |
---|
[93] | 211 | # asynchrone guard for job end |
---|
| 212 | async { |
---|
[96] | 213 | my $timer; |
---|
[93] | 214 | GUARD: |
---|
| 215 | while () { |
---|
[96] | 216 | cede; |
---|
[97] | 217 | |
---|
| 218 | # wait to not re-launch oarstat to fast |
---|
| 219 | # equivalent to sleep $brake |
---|
| 220 | $timer = AE::now + $brake; |
---|
| 221 | while ( AE::now < $timer ) { |
---|
| 222 | # force update of AE time |
---|
| 223 | AE::now_update; |
---|
| 224 | cede; |
---|
| 225 | } |
---|
| 226 | |
---|
[93] | 227 | my $is_finish = `oarstat -s -j $job_id`; |
---|
| 228 | chomp $is_finish; |
---|
| 229 | last GUARD if $is_finish =~ m/Terminated/; |
---|
| 230 | } |
---|
[95] | 231 | |
---|
| 232 | my $msg = sprintf "end job %${job_name_maxlen}s / %i at %s\n", |
---|
| 233 | $scheduled{$job_id}->{name}, $job_id, time; |
---|
| 234 | |
---|
[97] | 235 | # Job non finish, just suspend if received checkpoint signal |
---|
| 236 | $msg =~ s/^end\s+job/suspend job/ |
---|
| 237 | if $sig_transmit and $oar_checkpoint->count() > 0; |
---|
[95] | 238 | |
---|
[97] | 239 | $log_h->print($msg) if $logtrace; |
---|
| 240 | print($msg) if $verbose; |
---|
[95] | 241 | |
---|
[93] | 242 | $job_active->down; |
---|
| 243 | $job_todo->down; |
---|
[95] | 244 | delete $scheduled{$job_id}; |
---|
[96] | 245 | } |
---|
[92] | 246 | } |
---|
[97] | 247 | } |
---|
[9] | 248 | |
---|
| 249 | async { |
---|
[92] | 250 | while () { |
---|
[94] | 251 | # checkpointing ! just finishing running job and quit |
---|
| 252 | $finished->send if $oar_checkpoint->count() > 0 and scalar(keys(%scheduled)) == 0; |
---|
| 253 | |
---|
[92] | 254 | $finished->send if $job_todo->count == 0; |
---|
| 255 | cede; |
---|
| 256 | } |
---|
[97] | 257 | } |
---|
[9] | 258 | |
---|
| 259 | cede; |
---|
| 260 | |
---|
[92] | 261 | # all job have been done |
---|
[9] | 262 | $finished->wait; |
---|
| 263 | |
---|
[94] | 264 | # close log trace file |
---|
| 265 | $log_h->close() if $logtrace; |
---|
[9] | 266 | |
---|
[94] | 267 | |
---|
[9] | 268 | __END__ |
---|
| 269 | |
---|
| 270 | =head1 NAME |
---|
| 271 | |
---|
| 272 | oar-dispatch - dispatch lot of small oar job |
---|
| 273 | |
---|
| 274 | =head1 SYNOPSIS |
---|
| 275 | |
---|
[33] | 276 | oar-dispatch [--task integer] [--overload real] --file filecommand [--verbose] |
---|
[9] | 277 | oar-dispatch --help |
---|
| 278 | |
---|
| 279 | =head1 OPTIONS |
---|
| 280 | |
---|
[30] | 281 | =over 12 |
---|
| 282 | |
---|
| 283 | =item B<[-t|--task integer]> |
---|
| 284 | |
---|
| 285 | Number of task to do in parallel. |
---|
| 286 | Default to the line number of the file OAR_NODE_FILE. |
---|
[9] | 287 | |
---|
[30] | 288 | =item B<[-o|--overload real]> |
---|
[9] | 289 | |
---|
[30] | 290 | Number of OAR job to create / number of task. |
---|
| 291 | Some job are create in advance to start whenever it's possible. |
---|
| 292 | 1.1 by default. |
---|
[9] | 293 | |
---|
[33] | 294 | =item B<[-f|--file filecommand]> |
---|
[30] | 295 | |
---|
| 296 | File name which content OAR job list |
---|
| 297 | |
---|
| 298 | =item B<[-v|--verbose]> |
---|
[9] | 299 | |
---|
[30] | 300 | =item B<[-h|--help]> |
---|
[9] | 301 | |
---|
[30] | 302 | =back |
---|
| 303 | |
---|
[11] | 304 | Input job file name content can have |
---|
[9] | 305 | |
---|
| 306 | - empty line |
---|
| 307 | - comment line begin with # |
---|
| 308 | - oarsub command without -t option |
---|
| 309 | |
---|
| 310 | C<oar-dispatch> will add C<-t inner=container_id> in this command line, |
---|
| 311 | just after C<oarsub>. |
---|
| 312 | |
---|
[30] | 313 | =head1 EXAMPLE |
---|
[9] | 314 | |
---|
[30] | 315 | Example where the file F<$HOME/test/subjob.txt> is a list of OAR script job (and can be executable but not need here). |
---|
| 316 | |
---|
[9] | 317 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob1.oar |
---|
| 318 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob2.oar |
---|
| 319 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob3.oar |
---|
| 320 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob4.oar |
---|
| 321 | ... |
---|
| 322 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob38.oar |
---|
| 323 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob39.oar |
---|
| 324 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob40.oar |
---|
| 325 | |
---|
[11] | 326 | These jobs could be launch with |
---|
[9] | 327 | |
---|
| 328 | oarsub -t container -n test-container -l /core=6,walltime=00:35:00 "oar-dispatch -f ./subjob.list.txt" |
---|
| 329 | |
---|
[11] | 330 | Total C<walltime> is defined by the formula: |
---|
[9] | 331 | |
---|
| 332 | total_walltime = subjob_walltime * total_subjob / core + global_delay |
---|
| 333 | |
---|
[11] | 334 | In practise, C<oar-dispatch> take few second and each subjob run in less than it's walltime so |
---|
[9] | 335 | |
---|
| 336 | total_walltime < subjob_walltime * total_subjob / core |
---|
| 337 | |
---|
[10] | 338 | If launch in interactif, C<overload> parameter is equal to 1, |
---|
[9] | 339 | C<task> must be define |
---|
| 340 | and no inner container is add to the C<oarsub> command line. |
---|
| 341 | |
---|
| 342 | |
---|
[28] | 343 | =head1 SEE ALSO |
---|
| 344 | |
---|
| 345 | oar-parexec, mpilauncher |
---|
| 346 | |
---|
| 347 | |
---|
[9] | 348 | =head1 AUTHORS |
---|
| 349 | |
---|
[28] | 350 | Written by Gabriel Moreau, Grenoble - France |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | =head1 LICENSE AND COPYRIGHT |
---|
| 354 | |
---|
| 355 | GPL version 2 or later and Perl equivalent |
---|
| 356 | |
---|
| 357 | Copyright (C) 2011 Gabriel Moreau / LEGI - CNRS UMR 5519 - France |
---|
| 358 | |
---|