2011-01-26 02:59:19 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use IO::Socket;
|
|
|
|
use Net::hostent;
|
2014-02-23 01:56:51 +01:00
|
|
|
use IPC::Shareable;
|
2017-09-08 02:51:13 +02:00
|
|
|
use Time::HiRes qw/gettimeofday/;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
my $SERVER_PORT = 9000;
|
|
|
|
my $SERIAL_PORT = 3333;
|
|
|
|
my $HEARTBEAT_PORT = 3336;
|
2019-07-11 03:52:11 +02:00
|
|
|
my $DOMAIN_NAME = 'compiler';
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2015-05-19 05:48:39 +02:00
|
|
|
my $COMPILE_TIMEOUT = 10;
|
2014-02-25 12:47:57 +01:00
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
sub server_listen {
|
|
|
|
my $port = shift @_;
|
|
|
|
|
2019-06-26 18:34:19 +02:00
|
|
|
my $server = IO::Socket::INET->new(
|
2011-01-26 02:59:19 +01:00
|
|
|
Proto => 'tcp',
|
|
|
|
LocalPort => $port,
|
|
|
|
Listen => SOMAXCONN,
|
|
|
|
Reuse => 1);
|
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
die "can't setup server: $!" unless $server;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
print "[Server $0 accepting clients]\n";
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub vm_stop {
|
2019-07-11 03:52:11 +02:00
|
|
|
system("virsh shutdown $DOMAIN_NAME");
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub vm_start {
|
2019-07-11 03:52:11 +02:00
|
|
|
system("virsh start $DOMAIN_NAME");
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2011-12-31 00:20:29 +01:00
|
|
|
sub vm_reset {
|
2019-07-11 03:52:11 +02:00
|
|
|
return if $ENV{NORESET};
|
|
|
|
#system("virsh detach-disk $DOMAIN_NAME vdb");
|
|
|
|
system("virsh snapshot-revert $DOMAIN_NAME 1");
|
|
|
|
#system("virsh attach-disk $DOMAIN_NAME --source /var/lib/libvirt/images/factdata.qcow2 --target vdb");
|
2014-02-23 01:56:51 +01:00
|
|
|
print "Reset vm\n";
|
2011-12-31 00:20:29 +01:00
|
|
|
}
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
sub execute {
|
|
|
|
my ($cmdline) = @_;
|
|
|
|
|
2011-12-31 00:20:29 +01:00
|
|
|
print "execute($cmdline)\n";
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
my @list = split / /, $cmdline;
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
my ($ret, $result);
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
#$SIG{CHLD} = 'IGNORE';
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
my $child = fork;
|
|
|
|
|
|
|
|
if($child == 0) {
|
|
|
|
($ret, $result) = eval {
|
|
|
|
my $result = '';
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
my $pid = open(my $fh, '-|', @list);
|
|
|
|
|
|
|
|
if (not defined $pid) {
|
|
|
|
print "Couldn't fork: $!\n";
|
|
|
|
return (-13, "[Fatal error]");
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2015-05-19 05:48:39 +02:00
|
|
|
local $SIG{ALRM} = sub { print "Time out\n"; kill 9, $pid; print "sent KILL to $pid\n"; die "Timed-out: $result\n"; };
|
2014-02-25 12:47:57 +01:00
|
|
|
alarm($COMPILE_TIMEOUT);
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
print "Reading...\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
while(my $line = <$fh>) {
|
2019-07-11 03:52:11 +02:00
|
|
|
print "read [$line]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
$result .= $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
close $fh;
|
2019-07-11 03:52:11 +02:00
|
|
|
print "Done reading.\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
my $ret = $? >> 8;
|
|
|
|
alarm 0;
|
2019-07-11 03:52:11 +02:00
|
|
|
|
|
|
|
print "[$ret, $result]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
return ($ret, $result);
|
|
|
|
};
|
|
|
|
|
|
|
|
alarm 0;
|
2012-02-25 09:30:49 +01:00
|
|
|
if($@ =~ /Timed-out: (.*)/) {
|
|
|
|
return (-13, "[Timed-out] $1");
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ($ret, $result);
|
|
|
|
} else {
|
|
|
|
waitpid($child, 0);
|
2019-07-11 03:52:11 +02:00
|
|
|
print "?: $?\n";
|
2012-02-25 09:30:49 +01:00
|
|
|
my $result = $? >> 8;
|
2012-10-05 03:59:04 +02:00
|
|
|
print "child exited, parent continuing [result = $result]\n";
|
2012-02-25 09:30:49 +01:00
|
|
|
return (undef, $result);
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub compiler_server {
|
2014-02-23 01:56:51 +01:00
|
|
|
my ($server, $heartbeat_pid, $heartbeat_monitor);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
my $heartbeat;
|
|
|
|
my $running;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
tie $heartbeat, 'IPC::Shareable', 'dat1', { create => 1 };
|
|
|
|
tie $running, 'IPC::Shareable', 'dat2', { create => 1 };
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-08 02:51:13 +02:00
|
|
|
my $last_wait = 0;
|
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
while(1) {
|
|
|
|
$running = 1;
|
|
|
|
$heartbeat = 0;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
vm_reset;
|
|
|
|
print "vm started\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
$heartbeat_pid = fork;
|
|
|
|
die "Fork failed: $!" if not defined $heartbeat_pid;
|
2012-10-05 03:59:04 +02:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
if($heartbeat_pid == 0) {
|
|
|
|
tie $heartbeat, 'IPC::Shareable', 'dat1', { create => 1 };
|
|
|
|
tie $running, 'IPC::Shareable', 'dat2', { create => 1 };
|
2012-10-05 03:59:04 +02:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
$heartbeat_monitor = undef;
|
2017-09-08 02:51:13 +02:00
|
|
|
my $attempts = 0;
|
|
|
|
while((not $heartbeat_monitor) and $attempts < 5) {
|
2014-02-23 01:56:51 +01:00
|
|
|
print "Connecting to heartbeat ...";
|
|
|
|
$heartbeat_monitor = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $HEARTBEAT_PORT, Proto => 'tcp', Type => SOCK_STREAM);
|
|
|
|
if(not $heartbeat_monitor) {
|
|
|
|
print " failed.\n";
|
2017-09-08 02:51:13 +02:00
|
|
|
++$attempts;
|
2014-02-23 01:56:51 +01:00
|
|
|
sleep 2;
|
|
|
|
} else {
|
|
|
|
print " success!\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
2014-02-23 01:56:51 +01:00
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-08 02:51:13 +02:00
|
|
|
if ($attempts >= 5) {
|
|
|
|
print "heart not beating... restarting\n";
|
|
|
|
$heartbeat = -1;
|
|
|
|
sleep 5;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
print "child: running: $running\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
while($running and <$heartbeat_monitor>) {
|
|
|
|
$heartbeat = 1;
|
|
|
|
#print "child: got heartbeat\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:51:13 +02:00
|
|
|
print "child no longer running\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
exit;
|
|
|
|
} else {
|
2017-09-08 02:51:13 +02:00
|
|
|
|
|
|
|
while ($heartbeat <= 0) {
|
|
|
|
if ($heartbeat == -1) {
|
|
|
|
print "heartbeat died\n";
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
print "sleeping for heartbeat...\n";
|
|
|
|
sleep 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($heartbeat == -1) {
|
|
|
|
print "fucking dead, restarting\n";
|
|
|
|
waitpid $heartbeat_pid, 0;
|
2019-07-11 03:52:11 +02:00
|
|
|
#vm_stop;
|
2017-09-08 02:51:13 +02:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
print "K, got heartbeat, here we go...\n";
|
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
if(not defined $server) {
|
|
|
|
print "Starting compiler server on port $SERVER_PORT\n";
|
|
|
|
$server = server_listen($SERVER_PORT);
|
|
|
|
} else {
|
|
|
|
print "Compiler server already listening on port $SERVER_PORT\n";
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-08 02:51:13 +02:00
|
|
|
print "parent: running: $running\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
|
|
|
|
while ($running and my $client = $server->accept()) {
|
|
|
|
$client->autoflush(1);
|
|
|
|
my $hostinfo = gethostbyaddr($client->peeraddr);
|
|
|
|
print '-' x 20, "\n";
|
|
|
|
printf "[Connect from %s at %s]\n", $client->peerhost, scalar localtime;
|
|
|
|
my $timed_out = 0;
|
|
|
|
my $killed = 0;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
local $SIG{ALRM} = sub { die 'Timed-out'; };
|
|
|
|
alarm 5;
|
|
|
|
|
|
|
|
while (my $line = <$client>) {
|
|
|
|
$line =~ s/[\r\n]+$//;
|
|
|
|
next if $line =~ m/^\s*$/;
|
|
|
|
alarm 5;
|
|
|
|
print "got: [$line]\n";
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
if($heartbeat <= 0) {
|
|
|
|
print "No heartbeat yet, ignoring compile attempt.\n";
|
|
|
|
print $client "Recovering from previous snippet, please wait.\n" if gettimeofday - $last_wait > 60;
|
|
|
|
$last_wait = gettimeofday;
|
|
|
|
last;
|
|
|
|
}
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
print "Attempting compile...\n";
|
|
|
|
alarm 0;
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
my ($ret, $result) = execute("perl compiler_vm_client.pl $line");
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
if(not defined $ret) {
|
|
|
|
#print "parent continued\n";
|
|
|
|
print "parent continued [$result]\n";
|
|
|
|
$timed_out = 1 if $result == 243 or $result == -13; # -13 == 243
|
|
|
|
$killed = 1 if $result == 242 or $result == -14; # -14 = 242
|
|
|
|
last;
|
|
|
|
}
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
$result =~ s/\s+$//;
|
|
|
|
print "Ret: $ret; result: [$result]\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
if($result =~ m/\[Killed\]$/) {
|
|
|
|
print "Process was killed\n";
|
|
|
|
$killed = 1;
|
2014-02-23 01:56:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
print $client $result . "\n";
|
|
|
|
close $client;
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
$ret = -14 if $killed;
|
|
|
|
|
|
|
|
# child exit
|
|
|
|
print "child exit\n";
|
|
|
|
exit $ret;
|
2014-02-23 01:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alarm 0;
|
|
|
|
};
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
alarm 0;
|
2012-02-25 09:30:49 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
close $client;
|
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
print "timed out: $timed_out; killed: $killed\n";
|
|
|
|
next unless ($timed_out or $killed);
|
|
|
|
|
|
|
|
vm_reset;
|
|
|
|
next;
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2019-07-11 03:52:11 +02:00
|
|
|
print "stopping vm\n";
|
|
|
|
#vm_stop;
|
2014-02-23 01:56:51 +01:00
|
|
|
$running = 0;
|
|
|
|
last;
|
2019-06-26 18:34:19 +02:00
|
|
|
}
|
2017-09-08 02:51:13 +02:00
|
|
|
print "Compiler server no longer running, restarting...\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
}
|
2019-07-11 03:52:11 +02:00
|
|
|
print "waiting on heartbeat pid?\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
waitpid($heartbeat_pid, 0);
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
compiler_server;
|