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 next if $rpri > $pri;
280 if ($rpri < $bestpri ||
281 $rpri == $bestpri && $r->{'LastUpdate'} < $best->{'LastUpdate'}) {
282 # Trick: $best must be defined, as otherwise $bestpri == -1
289 if (defined $besti) {
291 print STDERR "I: Replacing ", $best->{'Host'}, " (pri $bestpri)\n";
292 delete $best->{'Row'};
294 print STDERR "I: Allocated ", $s->{'Host'}, " \@$besti (pri $pri)\n";
295 $s->{'Row'} = $besti;
296 $by_row[$besti] = $s;
298 print STDERR "I: No place for ", $s->{'Host'}, " (pri $pri)\n" if $debug;
304 my $r = $s->{'Row'} // return;
306 my $mach = $s->{'Host'};
307 my $stat = $s->{'Status'} // "?";
308 my $jid = $s->{'Job'} // "";
309 my $jname = ($jid eq "" ? "" : $queue->job_name($jid));
310 my $jcnt = $job_cnt{$mach};
311 if ($jcnt->{'running'}) {
312 if ($jcnt->{'failed'}) {
313 $C->bkgdset(COLOR_PAIR(4) | A_BOLD);
315 $C->bkgdset(COLOR_PAIR(3) | A_BOLD);
318 if ($jcnt->{'failed'}) {
319 $C->bkgdset(COLOR_PAIR(4));
324 $C->addstr($r, 0, sprintf("%-20.20s", $mach));
325 if ($jcnt->{'failed'}) {
326 $C->bkgdset(COLOR_PAIR(4));
327 $C->addstr(sprintf("%3dE ", $jcnt->{'failed'}));
333 $C->addstr(sprintf("%3dD %3dW", $jcnt->{'done'}, $jcnt->{'ready'}));
334 if ($stat eq 'DONE') {
335 if (defined $host_last_fail_stat{$mach}) {
336 $C->bkgdset(($host_last_fail_stat{$mach} eq 'NOPING') ? COLOR_PAIR(5) : COLOR_PAIR(4));
337 $C->addstr(sprintf(" %-8s %s", $host_last_fail_stat{$mach}, $queue->job_name($host_last_fail_job{$mach})));
340 my $text = sprintf(" %-8s %s", $stat, $jname);
348 my ($ui, $mach, $jid, $stat) = @_;
351 # Pseudo-state generated internally
352 $ui->set_host_status($mach, 'ready');
353 $ui->set_job_status($mach, $jid, 'ready');
356 $ui->set_job_status($mach, $jid, 'done');
358 when (['FAILED', 'INTERR', 'NOPING', 'PREPFAIL', 'NOXFER']) {
359 $ui->set_job_status($mach, $jid, 'failed');
360 $host_last_fail_job{$mach} = $jid;
361 $host_last_fail_stat{$mach} = $stat;
364 if ($job_cnt{$mach}{'failed'}) {
365 $ui->set_host_status($mach, 'failed');
367 $ui->set_host_status($mach, 'done');
371 $ui->set_host_status($mach, 'running');
372 $ui->set_job_status($mach, $jid, 'running') if defined $jid;
376 $ui->set_job_status($mach, $jid, 'failed');
378 for my $j (keys %{$job_state{$mach}}) {
379 $ui->set_job_status($mach, $jid, 'failed');
381 $ui->set_host_status($mach, 'failed');
382 $host_last_fail_job{$mach} = $jid;
383 $host_last_fail_stat{$mach} = $stat;
386 when (['START', 'PING', 'SEND', 'RUN']) {
389 $ui->err("Received unknown job status $stat");
392 my $s = get_slot($mach);
394 $s->{'Status'} = $stat;