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 -q, --queue=<name> Run jobs in the given queue
23 --text Use plain-text user interface instead of curses
29 "q|queue=s" => \$queue_name,
30 "text!" => \$text_mode,
31 "debug-children!" => \$debug_children,
33 ) or die "Try `bex prun --help' for more information.\n";
35 system 'tmux', 'has-session';
36 !$? or die "You need to start tmux first.\n";
38 my $queue = BEX::Queue->new($queue_name);
39 my $fifo_name = $queue->{'Path'} . '/status-fifo';
41 mkfifo $fifo_name, 0700 or die "Cannot create $fifo_name: $!";
42 open FIFO, '+<', $fifo_name or die "Cannot open $fifo_name: $!";
44 my $ui = ($text_mode ? BEX::bprun::text->new : BEX::bprun::curses->new);
47 for my $mach (BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*')) {
48 my @jobs = $queue->scan($mach);
50 push @machines, $mach;
51 for (@jobs) { $ui->update($mach, $_, 'READY'); }
55 my $max = $BEX::Config::max_parallel_jobs;
57 while (keys %running || @machines) {
58 if (@machines && keys %running < $max) {
59 my $mach = shift @machines;
60 $ui->update($mach, undef, 'START');
61 my @tm = ('tmux', 'new-window', '-n', $mach, '-d');
62 my $P5LIB = $ENV{"PERL5LIB"} // "";
64 "BEX_HOME='$BEX::Config::home'",
65 "BEX_LIB='$BEX::Config::lib'",
67 "$BEX::Config::lib/bin/bex-run",
68 "--status-fifo=$fifo_name",
69 "--queue=" . $queue->{'Name'},
72 push @cmd, ">debug.log", "2>&1" if $debug_children;
73 push @tm, join(" ", @cmd);
75 !$? or $ui->update($mach, undef, 'INTERR');
76 $running{$mach} = 'START';
81 my ($mach, $jid, $stat) = /^! (\S+) (\S+) (\S+)$/;
83 $ui->err("Received invalid status message <$_>");
86 if (!defined $running{$mach}) {
87 $ui->err("Received status message <$_> for a machine which does not run");
90 $running{$mach} = $stat;
91 $ui->update($mach, ($jid eq '-' ? undef : $jid), $stat);
92 if ($stat eq 'DONE') {
93 delete $running{$mach};
101 package BEX::bprun::text;
111 my ($ui, $mach, $jid, $stat) = @_;
112 print +($mach // '-'), (defined($jid) ? ":$jid" : ""), " $stat\n";
117 print STDERR "ERROR: $msg\n";
120 package BEX::bprun::curses;
136 my %host_last_fail_job;
137 my %host_last_fail_stat;
142 has_colors && COLORS >= 8 && COLOR_PAIRS >= 8 or die "Your terminal is too dumb for me\n";
148 init_pair(1, COLOR_YELLOW, COLOR_BLUE);
149 init_pair(2, COLOR_YELLOW, COLOR_RED);
150 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
151 init_pair(4, COLOR_RED, COLOR_BLACK);
153 $nrows = $C->getmaxy - 2;
154 if ($BEX::Config::max_parallel_jobs > $nrows) {
155 $BEX::Config::max_parallel_jobs = $nrows;
158 %host_state = %host_cnt = ();
159 %job_state = %job_cnt = ();
160 for my $s ('unknown', 'ready', 'running', 'done', 'failed') {
162 $job_cnt{'*'}{$s} = 0;
172 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
173 $C->addstr($C->getmaxy-1, 0, "Press any key to quit...");
181 $C->bkgdset(COLOR_PAIR(2) | A_BOLD);
182 $C->addnstr($C->getmaxy-1, 0, "ERROR: $msg", $C->getmaxx);
187 sub set_host_status($$$) {
188 my ($ui, $mach, $stat) = @_;
189 my $prev_stat = $host_state{$mach};
190 if (defined $prev_stat) {
191 $host_cnt{$prev_stat}--;
193 for my $s ('unknown', 'ready', 'running', 'done', 'failed') { $job_cnt{$mach}{$s} = 0; }
195 $host_state{$mach} = $stat;
199 sub set_job_status($$$$) {
200 my ($ui, $mach, $jid, $stat) = @_;
201 my $prev_stat = $job_state{$mach}{$jid} // 'unknown';
202 $job_cnt{$mach}{$prev_stat}--;
203 $job_cnt{'*'}{$prev_stat}--;
204 $job_state{$mach}{$jid} = $stat;
205 $job_cnt{$mach}{$stat}++;
206 $job_cnt{'*'}{$stat}++;
209 sub refresh_status($) {
210 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
212 sprintf("BEX Hosts: %dR %dD %dE %dW Jobs: %dR %dD %dE %dW",
213 $host_cnt{'running'},
217 $job_cnt{'*'}{'running'},
218 $job_cnt{'*'}{'done'},
219 $job_cnt{'*'}{'failed'},
220 $job_cnt{'*'}{'ready'},
229 if (defined ($s = $by_host{$mach})) {
233 for my $i (0..$nrows-1) {
239 } elsif ($r->{'Gone'} && (!$best || $best->{'Gone'} > $r->{'Gone'})) {
245 delete $by_host{$best->{'Host'}};
247 $s->{'Host'} = $mach;
248 $s->{'Row'} = $besti;
249 $by_host{$mach} = $s;
250 $by_row[$besti] = $s;
255 my $gone_counter = 1;
258 $s->{'Gone'} = $gone_counter++;
263 my $mach = $s->{'Host'};
264 my $stat = $s->{'Status'} // "?";
265 my $jid = $s->{'Job'} // "";
266 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
267 my $jcnt = $job_cnt{$mach};
268 if ($jcnt->{'running'}) {
269 if ($jcnt->{'failed'}) {
270 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
272 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
275 if ($jcnt->{'failed'}) {
276 $C->bkgdset(COLOR_PAIR(4));
281 my $r = $s->{'Row'} + 1;
282 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
283 if ($jcnt->{'failed'}) {
284 $C->bkgdset(COLOR_PAIR(4));
285 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
291 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
292 if ($stat eq 'DONE') {
293 if (defined $host_last_fail_stat{$mach}) {
294 $C->bkgdset(COLOR_PAIR(4));
295 $C->addstr(sprintf(" %-8s %s", $host_last_fail_stat{$mach}, $queue->job_name($host_last_fail_job{$mach})));
298 my $text = sprintf(" %-8s %s", $stat, $jname);
306 my ($ui, $mach, $jid, $stat) = @_;
307 my $s = get_slot($mach);
310 # Pseudo-state generated internally
311 $ui->set_host_status($mach, 'ready');
312 $ui->set_job_status($mach, $jid, 'ready');
315 $ui->set_job_status($mach, $jid, 'done');
317 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL']) {
318 $ui->set_job_status($mach, $jid, 'failed');
319 $host_last_fail_job{$mach} = $jid;
320 $host_last_fail_stat{$mach} = $stat;
323 if ($job_cnt{$mach}{'failed'}) {
324 $ui->set_host_status($mach, 'failed');
326 $ui->set_host_status($mach, 'done');
330 $ui->set_host_status($mach, 'running');
331 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
335 $ui->set_job_status($mach, $jid, 'failed');
337 for my $j (keys %{$job_state{$mach}}) {
338 $ui->set_job_status($mach, $jid, 'failed');
340 $ui->set_host_status($mach, 'failed');
341 $host_last_fail_job{$mach} = $jid;
342 $host_last_fail_stat{$mach} = $stat;
345 when (['START', 'PING', 'SEND', 'RUN']) {
348 $ui->err("Received unknown job status $stat");
352 $s->{'Status'} = $stat;
354 if ($stat eq 'DONE') { delete_slot($s); }