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 "j|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;
144 has_colors && COLORS >= 8 && COLOR_PAIRS >= 8 or die "Your terminal is too dumb for me\n";
150 init_pair(1, COLOR_YELLOW, COLOR_BLUE);
151 init_pair(2, COLOR_YELLOW, COLOR_RED);
152 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
153 init_pair(4, COLOR_RED, COLOR_BLACK);
155 $nrows = $C->getmaxy - 2;
156 if ($BEX::Config::max_parallel_jobs > $nrows) {
157 $BEX::Config::max_parallel_jobs = $nrows;
160 %host_state = %host_cnt = ();
161 %job_state = %job_cnt = ();
162 for my $s ('unknown', 'ready', 'running', 'done', 'failed') {
164 $job_cnt{'*'}{$s} = 0;
174 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
175 $C->addstr($C->getmaxy-1, 0, "Press any key to quit...");
183 $C->bkgdset(COLOR_PAIR(2) | A_BOLD);
184 $C->addnstr($C->getmaxy-1, 0, "ERROR: $msg", $C->getmaxx);
189 sub set_host_status($$$) {
190 my ($ui, $mach, $stat) = @_;
191 my $prev_stat = $host_state{$mach};
192 if (defined $prev_stat) {
193 $host_cnt{$prev_stat}--;
195 for my $s ('unknown', 'ready', 'running', 'done', 'failed') { $job_cnt{$mach}{$s} = 0; }
197 $host_state{$mach} = $stat;
201 sub set_job_status($$$$) {
202 my ($ui, $mach, $jid, $stat) = @_;
203 my $prev_stat = $job_state{$mach}{$jid} // 'unknown';
204 $job_cnt{$mach}{$prev_stat}--;
205 $job_cnt{'*'}{$prev_stat}--;
206 $job_state{$mach}{$jid} = $stat;
207 $job_cnt{$mach}{$stat}++;
208 $job_cnt{'*'}{$stat}++;
211 sub refresh_status($) {
212 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
214 sprintf("BEX Hosts: %dR %dD %dE %dW Jobs: %dR %dD %dE %dW",
215 $host_cnt{'running'},
219 $job_cnt{'*'}{'running'},
220 $job_cnt{'*'}{'done'},
221 $job_cnt{'*'}{'failed'},
222 $job_cnt{'*'}{'ready'},
231 if (defined ($s = $by_host{$mach})) {
235 for my $i (0..$nrows-1) {
241 } elsif ($r->{'Gone'} && (!$best || $best->{'Gone'} > $r->{'Gone'})) {
247 delete $by_host{$best->{'Host'}};
249 $s->{'Host'} = $mach;
250 $s->{'Row'} = $besti;
251 $by_host{$mach} = $s;
252 $by_row[$besti] = $s;
257 my $gone_counter = 1;
260 $s->{'Gone'} = $gone_counter++;
265 my $mach = $s->{'Host'};
266 my $stat = $s->{'Status'} // "?";
267 my $jid = $s->{'Job'} // "";
268 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
269 my $jcnt = $job_cnt{$mach};
270 if ($jcnt->{'running'}) {
271 if ($jcnt->{'failed'}) {
272 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
274 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
277 if ($jcnt->{'failed'}) {
278 $C->bkgdset(COLOR_PAIR(4));
283 my $r = $s->{'Row'} + 1;
284 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
285 if ($jcnt->{'failed'}) {
286 $C->bkgdset(COLOR_PAIR(4));
287 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
293 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
294 if ($stat eq 'DONE') {
295 if (defined $host_last_fail_stat{$mach}) {
296 $C->bkgdset(COLOR_PAIR(4));
297 $C->addstr(sprintf(" %-8s %s", $host_last_fail_stat{$mach}, $queue->job_name($host_last_fail_job{$mach})));
300 my $text = sprintf(" %-8s %s", $stat, $jname);
308 my ($ui, $mach, $jid, $stat) = @_;
309 my $s = get_slot($mach);
312 # Pseudo-state generated internally
313 $ui->set_host_status($mach, 'ready');
314 $ui->set_job_status($mach, $jid, 'ready');
317 $ui->set_job_status($mach, $jid, 'done');
319 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL']) {
320 $ui->set_job_status($mach, $jid, 'failed');
321 $host_last_fail_job{$mach} = $jid;
322 $host_last_fail_stat{$mach} = $stat;
325 if ($job_cnt{$mach}{'failed'}) {
326 $ui->set_host_status($mach, 'failed');
328 $ui->set_host_status($mach, 'done');
332 $ui->set_host_status($mach, 'running');
333 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
337 $ui->set_job_status($mach, $jid, 'failed');
339 for my $j (keys %{$job_state{$mach}}) {
340 $ui->set_job_status($mach, $jid, 'failed');
342 $ui->set_host_status($mach, 'failed');
343 $host_last_fail_job{$mach} = $jid;
344 $host_last_fail_stat{$mach} = $stat;
347 when (['START', 'PING', 'SEND', 'RUN']) {
350 $ui->err("Received unknown job status $stat");
354 $s->{'Status'} = $stat;
356 if ($stat eq 'DONE') { delete_slot($s); }