From 38166bd14d614c7c0678a1b62c424d36931a1ecd Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 25 Feb 2012 08:30:49 +0000 Subject: [PATCH] compiler_vm: reset vm only on time-outs to improve response speed --- PBot/VERSION.pm | 4 ++-- modules/compiler_vm/compiler_server.pl | 27 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 2a0c4b30..d18b5556 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,8 +13,8 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 362, - BUILD_DATE => "2012-02-15", + BUILD_REVISION => 363, + BUILD_DATE => "2012-02-25", }; 1; diff --git a/modules/compiler_vm/compiler_server.pl b/modules/compiler_vm/compiler_server.pl index ca1bae7b..5551540b 100755 --- a/modules/compiler_vm/compiler_server.pl +++ b/modules/compiler_vm/compiler_server.pl @@ -39,8 +39,7 @@ sub vm_start { } if($pid == 0) { - #system('cp /home/compiler/compiler-saved-vm-backup /home/compiler/compiler-saved-vm'); - my $command = 'nice -n -20 qemu-system-x86_64 -M pc -net none -hda /home/compiler/compiler-saved-vm -m 76 -monitor tcp:127.0.0.1:3335,server,nowait -serial tcp:127.0.0.1:3333,server,nowait -enable-kvm -boot c -loadvm 1 -nographic'; + my $command = 'nice -n -20 qemu-system-x86_64 -M pc -net none -hda /home/compiler/compiler-saved-vm -m 128 -monitor tcp:127.0.0.1:3335,server,nowait -serial tcp:127.0.0.1:3333,server,nowait -boot c -loadvm 1 -enable-kvm -nographic'; my @command_list = split / /, $command; exec(@command_list); } else { @@ -78,7 +77,7 @@ sub execute { my $pid = open(my $fh, '-|', "$cmdline 2>&1"); - local $SIG{ALRM} = sub { print "Time out\n"; kill 'TERM', $pid; die "Timed-out\n"; }; + local $SIG{ALRM} = sub { print "Time out\n"; kill 'TERM', $pid; die "Timed-out: $result\n"; }; alarm(7); while(my $line = <$fh>) { @@ -89,20 +88,21 @@ sub execute { my $ret = $? >> 8; alarm 0; - print "[$ret, $result]\n"; + #print "[$ret, $result]\n"; return ($ret, $result); }; alarm 0; - if($@ =~ /Timed-out/) { - return (-13, '[Timed-out]'); + if($@ =~ /Timed-out: (.*)/) { + return (-13, "[Timed-out] $1"); } return ($ret, $result); } else { waitpid($child, 0); + my $result = $? >> 8; print "child exited, parent continuing\n"; - return undef; + return (undef, $result); } } @@ -116,6 +116,8 @@ sub compiler_server { $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $client->peerhost; + my $timed_out; + eval { my $lang; my $nick; @@ -140,10 +142,12 @@ sub compiler_server { my ($ret, $result) = execute("./compiler_vm_client.pl $tnick -lang=$tlang $code"); if(not defined $ret) { - print "parent continued\n"; + #print "parent continued\n"; + $timed_out = 1 if $result == 243; # -13 == 243 last; } + $result =~ s/\s+$//; print "Ret: $ret; result: [$result]\n"; if($ret == -13) { @@ -153,8 +157,8 @@ sub compiler_server { print $client $result . "\n"; close $client; # child exit - print "child exit\n"; - exit; + # print "child exit\n"; + exit $ret; } if($line =~ /compile:([^:]+):(.*)$/) { @@ -174,6 +178,9 @@ sub compiler_server { alarm 0; close $client; + + next unless $timed_out; + print "stopping vm $vm_pid\n"; vm_stop $vm_pid; $vm_pid = vm_start;