2 # Batch EXecutor 3.0 -- Parallel Execution Using Screen
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
18 Usage: bex prun [<options>] [[!]<machine-or-class> ...]
21 --debug-children Log stdout and stderr to ./debug.log
22 -p, --parallel=<n> Set limit on the number of jobs run in parallel
23 -q, --queue=<name> Run jobs in the given queue
24 --text Use plain-text user interface instead of curses
30 "q|queue=s" => \$queue_name,
31 "text!" => \$text_mode,
32 "debug-children!" => \$debug_children,
33 "p|parallel=i" => \$BEX::Config::max_parallel_jobs,
35 ) or die "Try `bex prun --help' for more information.\n";
37 system 'tmux', 'has-session';
38 !$? or die "You need to start tmux first.\n";
40 my $queue = BEX::Queue->new($queue_name);
41 my $fifo_name = $queue->{'Path'} . '/status-fifo';
43 mkfifo $fifo_name, 0700 or die "Cannot create $fifo_name: $!";
44 open FIFO, '+<', $fifo_name or die "Cannot open $fifo_name: $!";
46 my $ui = ($text_mode ? BEX::bprun::text->new : BEX::bprun::curses->new);
49 for my $mach (BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*')) {
50 my @jobs = $queue->scan($mach);
52 push @machines, $mach;
53 for (@jobs) { $ui->update($mach, $_, 'READY'); }
57 my $max = $BEX::Config::max_parallel_jobs;
59 while (keys %running || @machines) {
60 if (@machines && keys %running < $max) {
61 my $mach = shift @machines;
62 $ui->update($mach, undef, 'START');
63 my @tm = ('tmux', 'new-window', '-n', $mach, '-d');
64 my $P5LIB = $ENV{"PERL5LIB"} // "";
66 "BEX_HOME='$BEX::Config::home'",
67 "BEX_LIB='$BEX::Config::lib'",
69 "$BEX::Config::lib/bin/bex-run",
70 "--status-fifo=$fifo_name",
71 "--queue=" . $queue->{'Name'},
74 push @cmd, ">debug.log", "2>&1" if $debug_children;
75 push @tm, join(" ", @cmd);
77 !$? or $ui->update($mach, undef, 'INTERR');
78 $running{$mach} = 'START';
83 my ($mach, $jid, $stat) = /^! (\S+) (\S+) (\S+)$/;
85 $ui->err("Received invalid status message <$_>");
88 if (!defined $running{$mach}) {
89 $ui->err("Received status message <$_> for a machine which does not run");
92 $running{$mach} = $stat;
93 $ui->update($mach, ($jid eq '-' ? undef : $jid), $stat);
94 if ($stat eq 'DONE') {
95 delete $running{$mach};
103 package BEX::bprun::text;
113 my ($ui, $mach, $jid, $stat) = @_;
114 print +($mach // '-'), (defined($jid) ? ":$jid" : ""), " $stat\n";
119 print STDERR "ERROR: $msg\n";
122 package BEX::bprun::curses;
138 my %host_last_fail_job;
139 my %host_last_fail_stat;
147 @states = qw(unknown ready running done failed);
160 has_colors && COLORS >= 8 && COLOR_PAIRS >= 8 or die "Your terminal is too dumb for me\n";
166 init_pair(1, COLOR_YELLOW, COLOR_BLUE);
167 init_pair(2, COLOR_YELLOW, COLOR_RED);
168 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
169 init_pair(4, COLOR_RED, COLOR_BLACK);
170 init_pair(5, COLOR_BLUE, COLOR_BLACK);
172 $nrows = $C->getmaxy - 2;
173 if ($BEX::Config::max_parallel_jobs > $nrows) {
174 $BEX::Config::max_parallel_jobs = $nrows;
177 %host_state = %host_cnt = ();
178 %job_state = %job_cnt = ();
179 for my $s (@states) {
181 $job_cnt{'*'}{$s} = 0;
191 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
192 $C->addstr($C->getmaxy-1, 0, "Press any key to quit...");
200 $C->bkgdset(COLOR_PAIR(2) | A_BOLD);
201 $C->addnstr($C->getmaxy-1, 0, "ERROR: $msg", $C->getmaxx);
206 sub set_host_status($$$) {
207 my ($ui, $mach, $stat) = @_;
208 my $prev_stat = $host_state{$mach};
209 if (defined $prev_stat) {
210 $host_cnt{$prev_stat}--;
212 for my $s (@states) { $job_cnt{$mach}{$s} = 0; }
214 $host_state{$mach} = $stat;
218 sub set_job_status($$$$) {
219 my ($ui, $mach, $jid, $stat) = @_;
220 my $prev_stat = $job_state{$mach}{$jid} // 'unknown';
221 $job_cnt{$mach}{$prev_stat}--;
222 $job_cnt{'*'}{$prev_stat}--;
223 $job_state{$mach}{$jid} = $stat;
224 $job_cnt{$mach}{$stat}++;
225 $job_cnt{'*'}{$stat}++;
228 sub refresh_status($) {
229 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
231 sprintf("BEX Hosts: %dR %dD %dE %dW Jobs: %dR %dD %dE %dW",
232 $host_cnt{'running'},
236 $job_cnt{'*'}{'running'},
237 $job_cnt{'*'}{'done'},
238 $job_cnt{'*'}{'failed'},
239 $job_cnt{'*'}{'ready'},
247 my $s = $by_host{$mach};
249 $s = $by_host{$mach} = { 'Host' => $mach };
258 $s->{'LastUpdate'} = $place_counter++;
259 return $s if defined $s->{'Row'};
261 my $pri = $state_to_pri{$host_state{$s->{'Host'}}};
264 for my $i (0..$nrows-1) {
271 my $rpri = $state_to_pri{$host_state{$r->{'Host'}}};
272 next if $rpri > $pri;
274 if ($rpri < $bestpri ||
275 $rpri == $bestpri && $r->{'LastUpdate'} < $best->{'LastUpdate'}) {
276 # Trick: $best must be defined, as otherwise $bestpri == -1
283 if (defined $besti) {
285 delete $best->{'Row'};
287 $s->{'Row'} = $besti;
288 $by_row[$besti] = $s;
294 my $r = $s->{'Row'} // return;
296 my $mach = $s->{'Host'};
297 my $stat = $s->{'Status'} // "?";
298 my $jid = $s->{'Job'} // "";
299 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
300 my $jcnt = $job_cnt{$mach};
301 if ($jcnt->{'running'}) {
302 if ($jcnt->{'failed'}) {
303 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
305 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
308 if ($jcnt->{'failed'}) {
309 $C->bkgdset(COLOR_PAIR(4));
314 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
315 if ($jcnt->{'failed'}) {
316 $C->bkgdset(COLOR_PAIR(4));
317 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
323 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
324 if ($stat eq 'DONE') {
325 if (defined $host_last_fail_stat{$mach}) {
326 $C->bkgdset(($host_last_fail_stat{$mach} eq 'NOPING') ? COLOR_PAIR(5) : COLOR_PAIR(4));
327 $C->addstr(sprintf(" %-8s %s", $host_last_fail_stat{$mach}, $queue->job_name($host_last_fail_job{$mach})));
330 my $text = sprintf(" %-8s %s", $stat, $jname);
338 my ($ui, $mach, $jid, $stat) = @_;
341 # Pseudo-state generated internally
342 $ui->set_host_status($mach, 'ready');
343 $ui->set_job_status($mach, $jid, 'ready');
346 $ui->set_job_status($mach, $jid, 'done');
348 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL', 'NOXFER']) {
349 $ui->set_job_status($mach, $jid, 'failed');
350 $host_last_fail_job{$mach} = $jid;
351 $host_last_fail_stat{$mach} = $stat;
354 if ($job_cnt{$mach}{'failed'}) {
355 $ui->set_host_status($mach, 'failed');
357 $ui->set_host_status($mach, 'done');
361 $ui->set_host_status($mach, 'running');
362 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
366 $ui->set_job_status($mach, $jid, 'failed');
368 for my $j (keys %{$job_state{$mach}}) {
369 $ui->set_job_status($mach, $jid, 'failed');
371 $ui->set_host_status($mach, 'failed');
372 $host_last_fail_job{$mach} = $jid;
373 $host_last_fail_stat{$mach} = $stat;
376 when (['START', 'PING', 'SEND', 'RUN']) {
379 $ui->err("Received unknown job status $stat");
382 my $s = get_slot($mach);
384 $s->{'Status'} = $stat;