2 # Batch EXecutor 3.0 -- Parallel Execution Using Screen
3 # (c) 2011-2012 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");
96 $running{$mach} = $stat;
97 $ui->update($mach, ($jid eq '-' ? undef : $jid), $stat);
98 if ($stat eq 'DONE') {
99 delete $running{$mach};
107 package BEX::bprun::text;
117 my ($ui, $mach, $jid, $stat) = @_;
118 print +($mach // '-'), (defined($jid) ? ":$jid" : ""), " $stat\n";
123 print STDERR "ERROR: $msg\n";
126 package BEX::bprun::curses;
142 my %host_last_fail_job;
143 my %host_last_fail_stat;
151 @states = qw(unknown ready running done failed);
164 has_colors && COLORS >= 8 && COLOR_PAIRS >= 8 or die "Your terminal is too dumb for me\n";
170 init_pair(1, COLOR_YELLOW, COLOR_BLUE);
171 init_pair(2, COLOR_YELLOW, COLOR_RED);
172 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
173 init_pair(4, COLOR_RED, COLOR_BLACK);
174 init_pair(5, COLOR_BLUE, COLOR_BLACK);
176 $nrows = $C->getmaxy - 2;
177 if ($BEX::Config::max_parallel_jobs > $nrows) {
178 $BEX::Config::max_parallel_jobs = $nrows;
181 %host_state = %host_cnt = ();
182 %job_state = %job_cnt = ();
183 for my $s (@states) {
185 $job_cnt{'*'}{$s} = 0;
195 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
196 $C->addstr($C->getmaxy-1, 0, "Press any key to quit...");
204 $C->bkgdset(COLOR_PAIR(2) | A_BOLD);
205 $C->addnstr($C->getmaxy-1, 0, "ERROR: $msg", $C->getmaxx);
210 sub set_host_status($$$) {
211 my ($ui, $mach, $stat) = @_;
212 print STDERR "H: $mach $stat\n" if $debug;
213 my $prev_stat = $host_state{$mach};
214 if (defined $prev_stat) {
215 $host_cnt{$prev_stat}--;
217 for my $s (@states) { $job_cnt{$mach}{$s} = 0; }
219 $host_state{$mach} = $stat;
223 sub set_job_status($$$$) {
224 my ($ui, $mach, $jid, $stat) = @_;
225 print STDERR "J: $mach $jid $stat\n" if $debug;
226 my $prev_stat = $job_state{$mach}{$jid} // 'unknown';
227 $job_cnt{$mach}{$prev_stat}--;
228 $job_cnt{'*'}{$prev_stat}--;
229 $job_state{$mach}{$jid} = $stat;
230 $job_cnt{$mach}{$stat}++;
231 $job_cnt{'*'}{$stat}++;
234 sub refresh_status($) {
235 $C->bkgdset(COLOR_PAIR(1) | A_BOLD);
237 sprintf("BEX Hosts: %dR %dD %dE %dW Jobs: %dR %dD %dE %dW",
238 $host_cnt{'running'},
242 $job_cnt{'*'}{'running'},
243 $job_cnt{'*'}{'done'},
244 $job_cnt{'*'}{'failed'},
245 $job_cnt{'*'}{'ready'},
253 my $s = $by_host{$mach};
255 $s = $by_host{$mach} = { 'Host' => $mach };
264 $s->{'LastUpdate'} = $place_counter++;
265 return $s if defined $s->{'Row'};
267 my $pri = $state_to_pri{$host_state{$s->{'Host'}}};
270 for my $i (0..$nrows-1) {
277 my $rpri = $state_to_pri{$host_state{$r->{'Host'}}};
278 print STDERR "I: ... considering ", $r->{'Host'}, " (pri $rpri, lu ", $r->{'LastUpdate'}, ")\n" if $debug > 1;
279 next if $rpri > $pri;
281 if ($rpri < $bestpri ||
282 $rpri == $bestpri && $r->{'LastUpdate'} < $best->{'LastUpdate'}) {
283 # Trick: $best must be defined, as otherwise $bestpri == -1
290 if (defined $besti) {
292 print STDERR "I: Replacing ", $best->{'Host'}, " (pri $bestpri)\n";
293 delete $best->{'Row'};
295 print STDERR "I: Allocated ", $s->{'Host'}, " \@$besti (pri $pri)\n";
296 $s->{'Row'} = $besti;
297 $by_row[$besti] = $s;
299 print STDERR "I: No place for ", $s->{'Host'}, " (pri $pri)\n" if $debug;
305 my $r = $s->{'Row'} // return;
307 my $mach = $s->{'Host'};
308 my $stat = $s->{'Status'} // "?";
309 my $jid = $s->{'Job'} // "";
310 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
311 my $jcnt = $job_cnt{$mach};
312 if ($jcnt->{'running'}) {
313 if ($jcnt->{'failed'}) {
314 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
316 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
319 if ($jcnt->{'failed'}) {
320 $C->bkgdset(COLOR_PAIR(4));
325 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
326 if ($jcnt->{'failed'}) {
327 $C->bkgdset(COLOR_PAIR(4));
328 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
334 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
335 if ($stat eq 'DONE') {
336 if (defined $host_last_fail_stat{$mach}) {
337 $C->bkgdset(($host_last_fail_stat{$mach} eq 'NOPING') ? COLOR_PAIR(5) : COLOR_PAIR(4));
338 $C->addstr(sprintf(" %-8s %s", $host_last_fail_stat{$mach}, $queue->job_name($host_last_fail_job{$mach})));
341 my $text = sprintf(" %-8s %s", $stat, $jname);
349 my ($ui, $mach, $jid, $stat) = @_;
352 # Pseudo-state generated internally
353 $ui->set_host_status($mach, 'ready');
354 $ui->set_job_status($mach, $jid, 'ready');
357 $ui->set_job_status($mach, $jid, 'done');
359 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL', 'NOXFER']) {
360 $ui->set_job_status($mach, $jid, 'failed');
361 $host_last_fail_job{$mach} = $jid;
362 $host_last_fail_stat{$mach} = $stat;
365 if ($job_cnt{$mach}{'failed'}) {
366 $ui->set_host_status($mach, 'failed');
368 $ui->set_host_status($mach, 'done');
372 $ui->set_host_status($mach, 'running');
373 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
377 $ui->set_job_status($mach, $jid, 'failed');
379 for my $j (keys %{$job_state{$mach}}) {
380 $ui->set_job_status($mach, $jid, 'failed');
382 $ui->set_host_status($mach, 'failed');
383 $host_last_fail_job{$mach} = $jid;
384 $host_last_fail_stat{$mach} = $stat;
387 when (['START', 'PING', 'SEND', 'RUN']) {
390 $ui->err("Received unknown job status $stat");
393 my $s = get_slot($mach);
395 $s->{'Status'} = $stat;