-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathharness
executable file
·136 lines (123 loc) · 3.8 KB
/
harness
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/opt/maths/bin/perl
use strict;
use warnings;
use lib ('../lib', 'lib');
use Seq::Db;
use H::Heap;
use Math::GMP;
use POSIX qw{ :sys_wait_h };
use Type;
print "kill -HUP $$\n";
my($opt_recreate, $debug, $opt_strategy, $opt_gain) = (0, 0, 0, 0);
my($maxproc, $maxrun, $maxload, $limit, $drain) = (10, 100, 0, 0);
my $typename = 't';
my %override;
$SIG{'HUP'} = sub { $drain = 1 };
while (@ARGV && $ARGV[0] =~ /^-/) {
my $arg = shift(@ARGV);
last if $arg eq '--';
$opt_recreate = 1, next if $arg eq '-r';
$opt_strategy = 1, next if $arg eq '-s';
($maxload = $1 || shift(@ARGV)), next if $arg =~ /^-a(\d*)$/;
($limit = 1, $maxload = $1 || shift(@ARGV)), next if $arg =~ /^-A(\d*)$/;
$debug = 1, next if $arg eq '-d';
$override{$1} = $2, next if $arg =~ /^-p(-?\d+)=(-?\d+(?:\.\d+)?)$/;
$opt_gain = $1, next if $arg =~ /^-g(-?\d+(?:\.\d+)?)$/;
$typename = $1, next if $arg =~ /^-y(.*)$/;
die "Unknown option '$arg'";
}
$maxproc = shift(@ARGV) if @ARGV;
$maxrun = shift(@ARGV) if @ARGV;
$maxload ||= $maxproc;
my $type = Type->new($typename);
$type->bind_owner('harness');
local @ENV{qw{ DBI_TRACE DBIC_TRACE }} = (1, 1) if $debug;
my $db = Seq::Db->new($type, $opt_recreate);
use Data::Dumper;
local $Data::Dumper::Indent = $Data::Dumper::Sortkeys = 1;
Seq::Run->restrategise($db) if $opt_strategy;
my $prio = H::Heap->new(q{ ::prio($b) <=> ::prio($a) });
$prio->insert(Seq::Starter->new($type, 0, $maxload));
my(%pid, @runnable);
while (1) {
if ($drain) {
printf "Draining %s processes\n", 0 + keys %pid;
waitFor($db, \%pid, 1) while keys %pid;
last;
}
if (@runnable) {
if (keys(%pid) >= $maxproc) {
waitFor($db, \%pid, 1);
} else {
my $r = shift @runnable;
$override{$r->n} -= $opt_gain;
my $pid = $r->run($db);
if ($pid) {
$pid{$pid} = $r;
$drain = 1, next if --$maxrun <= 0;
}
}
} elsif (my $obj = $prio->fetch) {
$prio->insert($_) for $obj->prep($db);
push @runnable, $obj->runnable;
} else {
waitFor($db, \%pid, 0);
}
}
exit 0;
sub prio {
my($obj) = @_;
return $obj->rprio($type) + ($override{$obj->n} // 0);
}
sub waitFor {
my($db, $pids, $block) = @_;
my @t0 = times;
my $pid = waitpid(-1, $block ? 0 : WNOHANG);
return 0 if $pid <= 0;
my $obj = delete $pids->{$pid};
my @t1 = times;
my $time = $t1[2] + $t1[3] - $t0[2] - $t0[3];
$prio->insert($_) for $obj->finalize($db, $time);
return 1;
}
package Seq::Starter {
use List::Util qw{ max };
sub new {
my($class, $type, $start, $count) = @_;
my $self = bless {
type => $type,
start => $start,
count => $count,
}, $class;
warn sprintf "new starter: from %s for %s prio %.2f\n",
$start, $count, $self->rprio($type);
return $self;
}
sub n { -$_[0]->{start} }
sub type { $_[0]->{type} }
sub rprio {
my($self, $type) = @_;
my $start = $self->{start};
my $count = $self->{count};
# We don't want to block fetching of later entries just because
# there are low priority ones in the way.
return $self->{rprio} //= max(
map $type->gprio($_), grep $_ > 1, $start .. $start + $count
);
}
sub prep {
my($self, $db) = @_;
my $start = $self->{start};
my $count = $self->{count};
my $end = $start + $count - 1;
my $max = Seq::TauG->max_known($db);
if ($end > $max) {
Seq::TauG->genTo($db, $end);
}
return (
($limit ? () : Seq::Starter->new($self->type, $end + 1, $count)),
Seq::TauG->range($db, $start, $end),
);
}
sub runnable { () }
};