2 # Batch EXecutor -- Parallel Execution Using Screen
3 # (c) 2011-2013 Martin Mares <mj@ucw.cz>
19 Usage: bex prun [<options>] [[!]<machine-or-class> ...]
22 --debug Log status changes to stderr
23 --debug-children Log stdout and stderr of child processes to ./debug.log
24 -p, --parallel=<n> Set limit on the number of jobs run in parallel
25 -q, --queue=<name> Run jobs in the given queue
26 --text Use plain-text user interface instead of curses
32 "q|queue=s" => \$queue_name,
33 "text!" => \$text_mode,
35 "debug-children!" => \$debug_children,
36 "p|parallel=i" => \$BEX::Config::max_parallel_jobs,
38 ) or die "Try `bex prun --help' for more information.\n";
40 system 'tmux', 'has-session';
41 !$? or die "You need to start tmux first.\n";
43 my $queue = BEX::Queue->new($queue_name);
44 my $fifo_name = $queue->{'Path'} . '/status-fifo';
46 mkfifo $fifo_name, 0700 or die "Cannot create $fifo_name: $!";
47 open FIFO, '+<', $fifo_name or die "Cannot open $fifo_name: $!";
49 my $ui = ($text_mode ? BEX::bprun::text->new : BEX::bprun::curses->new);
52 for my $mach (BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*')) {
53 my @jobs = $queue->scan($mach);
55 push @machines, $mach;
56 for (@jobs) { $ui->update($mach, $_, 'READY'); }
60 my $max = $BEX::Config::max_parallel_jobs;
62 while (keys %running || @machines) {
63 if (@machines && keys %running < $max) {
64 my $mach = shift @machines;
65 $ui->update($mach, undef, 'START');
66 my @tm = ('tmux', 'new-window', '-n', $mach, '-d');
67 my $P5LIB = $ENV{"PERL5LIB"} // "";
69 "BEX_HOME='$BEX::Config::home'",
70 "BEX_LIB='$BEX::Config::lib'",
72 "$BEX::Config::lib/bin/bex-run",
73 "--status-fifo=$fifo_name",
74 "--queue=" . $queue->{'Name'},
77 push @cmd, ">>debug.log", "2>&1" if $debug_children;
78 push @tm, join(" ", @cmd);
80 !$? or $ui->update($mach, undef, 'INTERR');
81 $running{$mach} = 'START';
86 print STDERR "<< $_\n" if $debug;
87 my ($mach, $jid, $stat) = /^! (\S+) (\S+) (\S+)$/;
89 $ui->err("Received invalid status message <$_>");
92 if (!defined $running{$mach}) {
93 $ui->err("Received status message <$_> for a machine which does not run")
94 unless $stat eq 'DONE';
97 $running{$mach} = $stat;
98 $ui->update($mach, ($jid eq '-' ? undef : $jid), $stat);
99 if ($stat eq 'DONE') {
100 delete $running{$mach};
108 package BEX::bprun::text;
118 my ($ui, $mach, $jid, $stat) = @_;
119 print +($mach // '-'), (defined($jid) ? ":$jid" : ""), " $stat\n";
124 print STDERR "ERROR: $msg\n";
127 package BEX::bprun::curses;
143 my %host_last_fail_job;
144 my %host_last_fail_stat;
152 @states = qw(unknown ready running done failed);
165 has_colors && COLORS >= 8 && COLOR_PAIRS >= 8 or die "Your terminal is too dumb for me\n";
171 init_pair(1, COLOR_YELLOW, COLOR_BLUE);
172 init_pair(2, COLOR_YELLOW, COLOR_RED);
173 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
174 init_pair(4, COLOR_RED, COLOR_BLACK);
175 init_pair(5, COLOR_BLUE, COLOR_BLACK);
177 $nrows = $C->getmaxy - 2;
178 if ($BEX::Config::max_parallel_jobs > $nrows) {
179 $BEX::Config::max_parallel_jobs = $nrows;
182 %host_state = %host_cnt = ();
183 %job_state = %job_cnt = ();
184 for my $s (@states) {
186 $job_cnt{'*'}{$s} = 0;
196 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
197 $C->addstr($C->getmaxy-1, 0, "Press any key to quit...");
205 $C->bkgdset(COLOR_PAIR(2) | A_BOLD);
206 $C->addnstr($C->getmaxy-1, 0, "ERROR: $msg", $C->getmaxx);
211 sub set_host_status($$$) {
212 my ($ui, $mach, $stat) = @_;
213 print STDERR "H: $mach $stat\n" if $debug;
214 my $prev_stat = $host_state{$mach};
215 if (defined $prev_stat) {
216 $host_cnt{$prev_stat}--;
218 for my $s (@states) { $job_cnt{$mach}{$s} = 0; }
220 $host_state{$mach} = $stat;
224 sub set_job_status($$$$) {
225 my ($ui, $mach, $jid, $stat) = @_;
226 print STDERR "J: $mach $jid $stat\n" if $debug;
227 my $prev_stat = $job_state{$mach}{$jid} // 'unknown';
228 $job_cnt{$mach}{$prev_stat}--;
229 $job_cnt{'*'}{$prev_stat}--;
230 $job_state{$mach}{$jid} = $stat;
231 $job_cnt{$mach}{$stat}++;
232 $job_cnt{'*'}{$stat}++;
235 sub refresh_status($) {
236 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
238 sprintf("BEX Hosts: %dR %dD %dE %dW Jobs: %dR %dD %dE %dW",
239 $host_cnt{'running'},
243 $job_cnt{'*'}{'running'},
244 $job_cnt{'*'}{'done'},
245 $job_cnt{'*'}{'failed'},
246 $job_cnt{'*'}{'ready'},
254 my $s = $by_host{$mach};
256 $s = $by_host{$mach} = { 'Host' => $mach };
265 $s->{'LastUpdate'} = $place_counter++;
266 return $s if defined $s->{'Row'};
268 my $pri = $state_to_pri{$host_state{$s->{'Host'}}};
271 for my $i (0..$nrows-1) {
278 my $rpri = $state_to_pri{$host_state{$r->{'Host'}}};
279 print STDERR "I: ... considering ", $r->{'Host'}, " (pri $rpri, lu ", $r->{'LastUpdate'}, ")\n" if $debug > 1;
280 next if $rpri > $pri;
282 if ($rpri < $bestpri ||
283 $rpri == $bestpri && $r->{'LastUpdate'} < $best->{'LastUpdate'}) {
284 # Trick: $best must be defined, as otherwise $bestpri == 99
291 if (defined $besti) {
293 print STDERR "I: Replacing ", $best->{'Host'}, " (pri $bestpri)\n" if $debug;
294 delete $best->{'Row'};
296 print STDERR "I: Allocated ", $s->{'Host'}, " \@$besti (pri $pri)\n" if $debug;
297 $s->{'Row'} = $besti;
298 $by_row[$besti] = $s;
300 print STDERR "I: No place for ", $s->{'Host'}, " (pri $pri)\n" if $debug;
306 my $r = $s->{'Row'} // return;
308 my $mach = $s->{'Host'};
309 my $stat = $s->{'Status'} // "?";
310 my $jid = $s->{'Job'} // "";
311 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
312 my $jcnt = $job_cnt{$mach};
313 if ($jcnt->{'running'}) {
314 if ($jcnt->{'failed'}) {
315 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
317 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
320 if ($jcnt->{'failed'}) {
321 $C->bkgdset(COLOR_PAIR(4));
326 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
327 if ($jcnt->{'failed'}) {
328 $C->bkgdset(COLOR_PAIR(4));
329 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
335 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
336 if ($stat eq 'DONE') {
337 my $lfs = $host_last_fail_stat{$mach};
338 my $lfj = $host_last_fail_job{$mach};
340 $C->bkgdset(($lfs eq 'NOPING') ? COLOR_PAIR(5) : COLOR_PAIR(4));
341 $C->addstr(sprintf(" %-8s %s", $lfs, $lfj ? $queue->job_name($lfj) : ""));
344 my $text = sprintf(" %-8s %s", $stat, $jname);
352 my ($ui, $mach, $jid, $stat) = @_;
355 # Pseudo-state generated internally
356 $ui->set_host_status($mach, 'ready');
357 $ui->set_job_status($mach, $jid, 'ready');
360 $ui->set_job_status($mach, $jid, 'done');
362 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL', 'NOXFER']) {
363 $ui->set_job_status($mach, $jid, 'failed');
364 $host_last_fail_job{$mach} = $jid;
365 $host_last_fail_stat{$mach} = $stat;
368 if ($job_cnt{$mach}{'failed'}) {
369 $ui->set_host_status($mach, 'failed');
371 $ui->set_host_status($mach, 'done');
375 $ui->set_host_status($mach, 'running');
376 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
380 $ui->set_job_status($mach, $jid, 'failed');
382 for my $j (keys %{$job_state{$mach}}) {
383 $ui->set_job_status($mach, $j, 'failed');
385 $ui->set_host_status($mach, 'failed');
386 $host_last_fail_job{$mach} = $jid;
387 $host_last_fail_stat{$mach} = $stat;
390 when (['START', 'PING', 'PREP', 'SEND', 'RUN']) {
393 $ui->err("Received unknown job status $stat");
396 my $s = get_slot($mach);
398 $s->{'Status'} = $stat;