6 use File::Temp 'tempfile';
7 use Getopt::Long qw(:config bundling);
9 # Command-line options.
10 our ($start_time) = time ();
11 our ($sim); # Simulator: bochs, qemu, or gsx.
12 our ($debug) = "none"; # Debugger: none, monitor, or gdb.
13 our ($mem) = 4; # Physical RAM in MB.
14 our ($serial_out) = 1; # Send output to serial port?
15 our ($vga); # VGA output: window, terminal, or none.
16 our ($jitter); # Seed for random timer interrupts, if set.
17 our ($realtime); # Synchronize timer interrupts with real time?
18 our ($timeout); # Maximum runtime in seconds, if set.
19 our ($kill_on_failure); # Abort quickly on test failure?
20 our (@puts); # Files to copy into the VM.
21 our (@gets); # Files to copy out of the VM.
22 our ($as_ref); # Reference to last addition to @gets or @puts.
23 our (@kernel_args); # Arguments to pass to kernel.
24 our (%disks) = (OS => {DEF_FN => 'os.dsk'}, # Disks to give VM.
25 FS => {DEF_FN => 'fs.dsk'},
26 SCRATCH => {DEF_FN => 'scratch.dsk'},
27 SWAP => {DEF_FN => 'swap.dsk'});
28 our (@disks_by_iface) = @disks{qw (OS FS SCRATCH SWAP)};
30 parse_command_line ();
32 prepare_scratch_disk ();
35 finish_scratch_disk ();
39 # Parses the command line.
40 sub parse_command_line {
41 usage (0) if @ARGV == 0 || (@ARGV == 1 && $ARGV[0] eq '--help');
44 if (grep ($_ eq '--', @kernel_args)) {
46 while ((my $arg = shift (@kernel_args)) ne '--') {
49 GetOptions ("sim=s" => sub { set_sim (@_) },
50 "bochs" => sub { set_sim ("bochs") },
51 "qemu" => sub { set_sim ("qemu") },
52 "gsx" => sub { set_sim ("gsx") },
54 "debug=s" => sub { set_debug (@_) },
55 "no-debug" => sub { set_debug ("none") },
56 "monitor" => sub { set_debug ("monitor") },
57 "gdb" => sub { set_debug ("gdb") },
59 "m|memory=i" => \$mem,
60 "j|jitter=i" => sub { set_jitter (@_) },
61 "r|realtime" => sub { set_realtime () },
63 "T|timeout=i" => \$timeout,
64 "k|kill-on-failure" => \$kill_on_failure,
66 "v|no-vga" => sub { set_vga ('none'); },
67 "s|no-serial" => sub { $serial_out = 0; },
68 "t|terminal" => sub { set_vga ('terminal'); },
70 "p|put-file=s" => sub { add_file (\@puts, $_[1]); },
71 "g|get-file=s" => sub { add_file (\@gets, $_[1]); },
72 "a|as=s" => sub { set_as ($_[1]); },
74 "h|help" => sub { usage (0); },
76 "os-disk=s" => \$disks{OS}{FILE_NAME},
77 "fs-disk=s" => \$disks{FS}{FILE_NAME},
78 "scratch-disk=s" => \$disks{SCRATCH}{FILE_NAME},
79 "swap-disk=s" => \$disks{SWAP}{FILE_NAME},
81 "0|disk-0|hda=s" => \$disks_by_iface[0]{FILE_NAME},
82 "1|disk-1|hdb=s" => \$disks_by_iface[1]{FILE_NAME},
83 "2|disk-2|hdc=s" => \$disks_by_iface[2]{FILE_NAME},
84 "3|disk-3|hdd=s" => \$disks_by_iface[3]{FILE_NAME})
88 $sim = "bochs" if !defined $sim;
89 $debug = "none" if !defined $debug;
90 $vga = "window" if !defined $vga;
92 undef $timeout, print "warning: disabling timeout with --$debug\n"
93 if defined ($timeout) && $debug ne 'none';
95 print "warning: enabling serial output for -k or --kill-on-failure\n"
96 if $kill_on_failure && !$serial_out;
100 # Prints a usage message and exits with $exitcode.
103 $exitcode = 1 unless defined $exitcode;
105 pintos, a utility for running Pintos in a simulator
106 Usage: pintos [OPTION...] -- [ARGUMENT...]
107 where each OPTION is one of the following options
108 and each ARGUMENT is passed to Pintos kernel verbatim.
110 --bochs (default) Use Bochs as simulator
111 --qemu Use qemu as simulator
112 --gsx Use VMware GSX Server 3.x as simulator
114 --no-debug (default) No debugger
115 --monitor Debug with simulator's monitor
117 Display options: (default is both VGA and serial)
118 -v, --no-vga No VGA display
119 -s, --no-serial No serial output
120 -t, --terminal Display VGA in terminal (Bochs only)
121 Timing options: (Bochs only)
122 -j SEED Randomize timer interrupts
123 -r, --realtime Use realistic, not reproducible, timings
125 -T, --timeout=N Kill Pintos after N seconds CPU time or N*load_avg
126 seconds wall-clock time (whichever comes first)
127 -k, --kill-on-failure Kill Pintos a few seconds after a kernel or user
128 panic, test failure, or triple fault
129 Configuration options:
130 -m, --mem=N Give Pintos N MB physical RAM (default: 4)
131 File system commands (for `run' command):
132 -p, --put-file=HOSTFN Copy HOSTFN into VM, by default under same name
133 -g, --get-file=GUESTFN Copy GUESTFN out of VM, by default under same name
134 -a, --as=FILENAME Specifies guest (for -p) or host (for -g) file name
135 Disk options: (name an existing FILE or specify SIZE in MB for a temp disk)
136 --os-disk=FILE Set OS disk file (default: os.dsk)
137 --fs-disk=FILE|SIZE Set FS disk file (default: fs.dsk)
138 --scratch-disk=FILE|SIZE Set scratch disk (default: scratch.dsk)
139 --swap-disk=FILE|SIZE Set swap disk file (default: swap.dsk)
141 -h, --help Display this help message.
146 # Sets the simulator.
149 die "--$new_sim conflicts with --$sim\n"
150 if defined ($sim) && $sim ne $new_sim;
156 my ($new_debug) = @_;
157 die "--$new_debug conflicts with --$debug\n"
158 if $debug ne 'none' && $new_debug ne 'none' && $debug ne $new_debug;
162 # Sets VGA output destination.
165 if (defined ($vga) && $vga ne $new_vga) {
166 print "warning: conflicting vga display options\n";
171 # Sets randomized timer interrupts.
173 my ($new_jitter) = @_;
174 die "--realtime conflicts with --jitter\n" if defined $realtime;
175 die "different --jitter already defined\n"
176 if defined $jitter && $jitter != $new_jitter;
177 $jitter = $new_jitter;
180 # Sets real-time timer interrupts.
182 die "--realtime conflicts with --jitter\n" if defined $jitter;
186 # add_file(\@list, $file)
188 # Adds [$file] to @list, which should be @puts or @gets.
189 # Sets $as_ref to point to the added element.
191 my ($list, $file) = @_;
193 push (@$list, $as_ref);
196 # Sets the guest/host name for the previous put/get.
199 die "-a (or --as) is only allowed after -p or -g\n" if !defined $as_ref;
200 die "Only one -a (or --as) is allowed after -p or -g\n"
201 if defined $as_ref->[1];
205 # Locates the files used to back each of the virtual disks,
206 # and creates temporary disks.
208 for my $disk (values %disks) {
209 # If there's no assigned file name but the default file exists,
210 # try to assign a default file name.
211 if (!defined ($disk->{FILE_NAME})) {
212 for my $try_fn ($disk->{DEF_FN}, "build/" . $disk->{DEF_FN}) {
213 $disk->{FILE_NAME} = $try_fn, last
218 # If there's no file name, we're done.
219 next if !defined ($disk->{FILE_NAME});
221 if ($disk->{FILE_NAME} =~ /^\d+(\.\d+)?|\.\d+$/) {
222 # Create a temporary disk of approximately the specified
224 die "OS disk can't be temporary\n" if $disk == $disks{OS};
226 my ($mb) = $disk->{FILE_NAME};
227 undef $disk->{FILE_NAME};
229 my ($cyl_size) = 512 * 16 * 63;
230 extend_disk ($disk, ceil ($mb * 2) * $cyl_size);
232 # The file must exist and have nonzero size.
233 -e $disk->{FILE_NAME} or die "$disk->{FILE_NAME}: stat: $!\n";
234 -s _ or die "$disk->{FILE_NAME}: disk has zero size\n";
238 # Warn about (potentially) missing disks.
239 die "Cannot find OS disk\n" if !defined $disks{OS}{FILE_NAME};
240 if (my ($project) = `pwd` =~ /\b(threads|userprog|vm|filesys)\b/) {
241 if ((grep ($project eq $_, qw (userprog vm filesys)))
242 && !defined ($disks{FS}{FILE_NAME})) {
243 print STDERR "warning: it looks like you're running the $project ";
244 print STDERR "project, but no file system disk is present\n";
246 if ($project eq 'vm' && !defined $disks{SWAP}{FILE_NAME}) {
247 print STDERR "warning: it looks like you're running the $project ";
248 print STDERR "project, but no swap disk is present\n";
253 # Prepare the scratch disk for gets and puts.
254 sub prepare_scratch_disk {
255 # Copy the files to put onto the scratch disk.
256 put_scratch_file ($_->[0]) foreach @puts;
258 # Make sure the scratch disk is big enough to get big files.
259 extend_disk ($disks{SCRATCH}, @gets * 1024 * 1024) if @gets;
262 # Read "get" files from the scratch disk.
263 sub finish_scratch_disk {
264 # We need to start reading the scratch disk from the beginning again.
266 close ($disks{SCRATCH}{HANDLE});
267 undef ($disks{SCRATCH}{HANDLE});
271 get_scratch_file (defined ($_->[1]) ? $_->[1] : $_->[0]) foreach @gets;
274 # put_scratch_file($file).
276 # Copies $file into the scratch disk.
277 sub put_scratch_file {
278 my ($put_file_name) = @_;
279 my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH});
281 print "Copying $put_file_name into $disk_file_name...\n";
283 # Write metadata sector, which consists of a 4-byte signature
284 # followed by the file size.
285 stat $put_file_name or die "$put_file_name: stat: $!\n";
287 my ($metadata) = pack ("a4 V x504", "PUT\0", $size);
288 write_fully ($disk_handle, $disk_file_name, $metadata);
292 sysopen ($put_handle, $put_file_name, O_RDONLY)
293 or die "$put_file_name: open: $!\n";
294 copy_file ($put_handle, $put_file_name, $disk_handle, $disk_file_name,
298 # Round up disk data to beginning of next sector.
299 write_fully ($disk_handle, $disk_file_name, "\0" x (512 - $size % 512))
303 # get_scratch_file($file).
305 # Copies from the scratch disk to $file.
306 sub get_scratch_file {
307 my ($get_file_name) = @_;
308 my ($disk_handle, $disk_file_name) = open_disk ($disks{SCRATCH});
310 print "Copying $get_file_name out of $disk_file_name...\n";
312 # Read metadata sector, which has a 4-byte signature followed by
314 my ($metadata) = read_fully ($disk_handle, $disk_file_name, 512);
315 my ($signature, $size) = unpack ("a4 V", $metadata);
316 die "bad signature reading scratch disk--did Pintos run correctly?\n"
317 if $signature ne "GET\0";
321 sysopen ($get_handle, $get_file_name, O_WRONLY | O_CREAT | O_EXCL, 0666)
322 or die "$get_file_name: create: $!\n";
323 copy_file ($disk_handle, $disk_file_name, $get_handle, $get_file_name,
327 # Skip forward in disk up to beginning of next sector.
328 read_fully ($disk_handle, $disk_file_name, 512 - $size % 512)
332 # Prepares the arguments to pass to the Pintos kernel,
333 # and then write them into Pintos bootloader.
334 sub prepare_arguments {
336 push (@args, shift (@kernel_args))
337 while @kernel_args && $kernel_args[0] =~ /^-/;
338 push (@args, 'put', defined $_->[1] ? $_->[1] : $_->[0]) foreach @puts;
339 push (@args, @kernel_args);
340 push (@args, 'get', $_->[0]) foreach @gets;
341 write_cmd_line ($disks{OS}, @args);
344 # Writes @args into the Pintos bootloader at the beginning of $disk.
346 my ($disk, @args) = @_;
348 # Figure out command line to write.
349 my ($arg_cnt) = pack ("V", scalar (@args));
350 my ($args) = join ('', map ("$_\0", @args));
351 die "command line exceeds 128 bytes" if length ($args) > 128;
352 $args .= "\0" x (128 - length ($args));
354 # Write command line.
355 my ($handle, $file_name) = open_disk_copy ($disk);
356 print "Writing command line to $file_name...\n";
357 sysseek ($handle, 0x17a, 0) == 0x17a or die "$file_name: seek: $!\n";
358 syswrite ($handle, "$arg_cnt$args") or die "$file_name: write: $!\n";
361 # Running simulators.
363 # Runs the selected simulator.
365 if ($sim eq 'bochs') {
367 } elsif ($sim eq 'qemu') {
369 } elsif ($sim eq 'gsx') {
372 die "unknown simulator `$sim'\n";
378 # Select Bochs binary based on the chosen debugger.
379 my ($bin) = $debug eq 'monitor' ? 'bochs-dbg' : 'bochs';
381 # Write bochsrc.txt configuration file.
382 open (BOCHSRC, ">", "bochsrc.txt") or die "bochsrc.txt: create: $!\n";
384 romimage: file=\$BXSHARE/BIOS-bochs-latest, address=0xf0000
385 vgaromimage: file=\$BXSHARE/VGABIOS-lgpl-latest
392 print BOCHSRC "gdbstub: enabled=1\n" if $debug eq 'gdb';
393 print BOCHSRC "clock: sync=", $realtime ? 'realtime' : 'none',
395 print_bochs_disk_line ("ata0-master", 0);
396 print_bochs_disk_line ("ata0-slave", 1);
397 if (defined ($disks_by_iface[2]{FILE_NAME})
398 || defined ($disks_by_iface[3]{FILE_NAME})) {
399 print BOCHSRC "ata1: enabled=1, ioaddr1=0x170, ",
400 "ioaddr2=0x370, irq=15\n";
401 print_bochs_disk_line ("ata1-master", 2);
402 print_bochs_disk_line ("ata1-slave", 3);
404 if ($vga ne 'terminal') {
405 print BOCHSRC "com1: enabled=1, mode=file, dev=/dev/stdout\n"
407 print BOCHSRC "display_library: nogui\n" if $vga eq 'none';
409 print BOCHSRC "display_library: term\n";
413 # Compose Bochs command line.
414 my (@cmd) = ($bin, '-q');
415 push (@cmd, '-j', $jitter) if defined $jitter;
418 print join (' ', @cmd), "\n";
419 my ($exit) = xsystem (@cmd);
420 if (WIFEXITED ($exit)) {
421 # Bochs exited normally.
422 # Ignore the exit code; Bochs normally exits with status 1,
424 } elsif (WIFSIGNALED ($exit)) {
425 die "Bochs died with signal ", WTERMSIG ($exit), "\n";
427 die "Bochs died: code $exit\n";
431 # print_bochs_disk_line($device, $iface)
433 # If IDE interface $iface has a disk attached, prints a bochsrc.txt
434 # line for attaching it to $device.
435 sub print_bochs_disk_line {
436 my ($device, $iface) = @_;
437 my ($disk) = $disks_by_iface[$iface];
438 my ($file) = $disk->{FILE_NAME};
440 my (%geom) = disk_geometry ($disk);
441 print BOCHSRC "$device: type=disk, path=$file, mode=flat, ";
442 print BOCHSRC "cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, ";
443 print BOCHSRC "translation=none\n";
449 print "warning: qemu doesn't support --terminal\n"
450 if $vga eq 'terminal';
451 print "warning: qemu doesn't support jitter\n"
453 my (@cmd) = ('qemu');
454 for my $iface (0...3) {
455 my ($option) = ('-hda', '-hdb', '-hdc', '-hdd')[$iface];
456 push (@cmd, $option, $disks_by_iface[$iface]{FILE_NAME})
457 if defined $disks_by_iface[$iface]{FILE_NAME};
459 push (@cmd, '-m', $mem);
460 push (@cmd, '-net', 'none');
461 push (@cmd, '-nographic') if $vga eq 'none';
462 push (@cmd, '-serial', 'stdio') if $serial_out && $vga ne 'none';
463 push (@cmd, '-S') if $debug eq 'monitor';
464 push (@cmd, '-s', '-S') if $debug eq 'gdb';
465 push (@cmd, '-monitor', 'null') if $vga eq 'none' && $debug eq 'none';
471 # Prints a message that $flag is unsupported by GSX Server.
474 print "warning: no support for $flag with VMware GSX Server\n";
477 # Runs VMware GSX Server.
479 gsx_unsup ("--$debug") if $debug ne 'none';
480 gsx_unsup ("--no-vga") if $vga eq 'none';
481 gsx_unsup ("--terminal") if $vga eq 'terminal';
482 gsx_unsup ("--jitter") if defined $jitter;
483 gsx_unsup ("--kill-on-failure") if defined $kill_on_failure;
485 unlink ("pintos.out");
487 open (VMX, ">", "pintos.vmx") or die "pintos.vmx: create: $!\n";
488 chmod 0777 & ~umask, "pintos.vmx";
490 #! /usr/bin/vmware -G
493 floppy0.present = FALSE
497 print VMX <<EOF if $serial;
498 serial0.present = TRUE
499 serial0.fileType = "file"
500 serial0.fileName = "pintos.out"
503 for (my ($i) = 0; $i < 4; $i++) {
504 my ($disk) = $disks_by_iface[$i];
505 my ($dsk) = $disk->{FILE_NAME};
506 next if !defined $dsk;
512 my ($device) = "ide" . int ($i / 2) . ":" . ($i % 2);
515 $device.present = TRUE
516 $device.deviceType = "plainDisk"
517 $device.fileName = "$pln"
520 my (%geom) = disk_geometry ($disk);
521 open (PLN, ">", $pln) or die "$pln: create: $!\n";
529 #vm|CAPACITY $geom{CAPACITY}
530 ACCESS "$dsk" 0 $geom{CAPACITY}
536 my ($vmx) = getcwd () . "/pintos.vmx";
537 system ("vmware-cmd -s register $vmx >&/dev/null");
538 system ("vmware-cmd $vmx stop hard >&/dev/null");
539 system ("vmware -l -G -x -q $vmx");
540 system ("vmware-cmd $vmx stop hard >&/dev/null");
541 system ("vmware-cmd -s unregister $vmx >&/dev/null");
548 # Opens $disk, if it is not already open, and returns its file handle
552 if (!defined ($disk->{HANDLE})) {
553 if ($disk->{FILE_NAME}) {
554 sysopen ($disk->{HANDLE}, $disk->{FILE_NAME}, O_RDWR)
555 or die "$disk->{FILE_NAME}: open: $!\n";
557 ($disk->{HANDLE}, $disk->{FILE_NAME}) = tempfile (UNLINK => 1,
561 return ($disk->{HANDLE}, $disk->{FILE_NAME});
564 # open_disk_copy($disk)
566 # Makes a temporary copy of $disk and returns its file handle and file name.
569 die if !$disk->{FILE_NAME};
571 my ($orig_handle, $orig_file_name) = open_disk ($disk);
572 my ($cp_handle, $cp_file_name) = tempfile (UNLINK => 1, SUFFIX => '.dsk');
573 copy_file ($orig_handle, $orig_file_name, $cp_handle, $cp_file_name,
575 return ($disk->{HANDLE}, $disk->{FILE_NAME}) = ($cp_handle, $cp_file_name);
578 # extend_disk($disk, $size)
580 # Extends $disk, if necessary, so that it is at least $size bytes
583 my ($disk, $size) = @_;
584 my ($handle, $file_name) = open_disk ($disk);
585 if (-s ($handle) < $size) {
586 sysseek ($handle, $size - 1, 0) == $size - 1
587 or die "$file_name: seek: $!\n";
588 syswrite ($handle, "\0") == 1
589 or die "$file_name: write: $!\n";
593 # disk_geometry($file)
595 # Examines $file and returns a valid IDE disk geometry for it, as a
599 my ($file) = $disk->{FILE_NAME};
600 my ($size) = -s $file;
601 die "$file: stat: $!\n" if !defined $size;
602 die "$file: size not a multiple of 512 bytes\n" if $size % 512;
603 my ($cyl_size) = 512 * 16 * 63;
604 my ($cylinders) = ceil ($size / $cyl_size);
605 extend_disk ($disk, $cylinders * $cyl_size) if $size % $cyl_size;
607 return (CAPACITY => $size / 512,
613 # copy_file($from_handle, $from_file_name, $to_handle, $to_file_name, $size)
615 # Copies $size bytes from $from_handle to $to_handle.
616 # $from_file_name and $to_file_name are used in error messages.
618 my ($from_handle, $from_file_name, $to_handle, $to_file_name, $size) = @_;
621 my ($chunk_size) = 4096;
622 $chunk_size = $size if $chunk_size > $size;
623 $size -= $chunk_size;
625 my ($data) = read_fully ($from_handle, $from_file_name, $chunk_size);
626 write_fully ($to_handle, $to_file_name, $data);
630 # read_fully($handle, $file_name, $bytes)
632 # Reads exactly $bytes bytes from $handle and returns the data read.
633 # $file_name is used in error messages.
635 my ($handle, $file_name, $bytes) = @_;
637 my ($read_bytes) = sysread ($handle, $data, $bytes);
638 die "$file_name: read: $!\n" if !defined $read_bytes;
639 die "$file_name: unexpected end of file\n" if $read_bytes != $bytes;
643 # write_fully($handle, $file_name, $data)
645 # Write $data to $handle.
646 # $file_name is used in error messages.
648 my ($handle, $file_name, $data) = @_;
649 my ($written_bytes) = syswrite ($handle, $data);
650 die "$file_name: write: $!\n" if !defined $written_bytes;
651 die "$file_name: short write\n" if $written_bytes != length $data;
654 # Subprocess utilities.
658 # Runs xsystem(@args).
659 # Also prints the command it's running and checks that it succeeded.
661 print join (' ', @_), "\n";
662 die "command failed\n" if xsystem (@_);
667 # Creates a subprocess via exec(@args) and waits for it to complete.
668 # Relays common signals to the subprocess.
669 # If $timeout is set then the subprocess will be killed after that long.
671 # qemu turns off local echo and does not restore it if killed by a signal.
672 # We compensate by restoring it ourselves.
673 my $cleanup = sub {};
675 my $termios = POSIX::Termios->new;
676 $termios->getattr (0);
677 $cleanup = sub { $termios->setattr (0, &POSIX::TCSANOW); }
680 # Create pipe for filtering output.
681 pipe (my $in, my $out) or die "pipe: $!\n" if $kill_on_failure;
684 if (!defined ($pid)) {
688 # Running in child process.
689 dup2 (fileno ($out), STDOUT_FILENO) or die "dup2: $!\n"
693 # Running in parent process.
694 close $out if $kill_on_failure;
697 local $SIG{ALRM} = sub { timeout ($pid, $cause, $cleanup); };
698 local $SIG{INT} = sub { relay_signal ($pid, "INT", $cleanup); };
699 local $SIG{TERM} = sub { relay_signal ($pid, "TERM", $cleanup); };
700 alarm ($timeout * get_load_average () + 1) if defined ($timeout);
702 if ($kill_on_failure) {
708 if (waitpid ($pid, WNOHANG) != 0) {
709 # Subprocess died. Pass through any remaining data.
710 print $buf while sysread ($in, $buf, 4096) > 0;
714 # Read and print out pipe data.
715 my ($len) = length ($buf);
716 waitpid ($pid, 0), last
717 if sysread ($in, $buf, 4096, $len) <= 0;
718 print substr ($buf, $len);
720 # Remove full lines from $buf and scan them for keywords.
721 while ((my $idx = index ($buf, "\n")) >= 0) {
722 local $_ = substr ($buf, 0, $idx + 1, '');
723 next if defined ($cause);
724 if (/(Kernel PANIC|User process ABORT)/ ) {
727 } elsif (/Pintos booting/ && ++$boots > 1) {
728 $cause = "triple fault";
731 $cause = "test failure";
742 if (WIFSIGNALED ($?) && WTERMSIG ($?) == SIGVTALRM ()) {
744 print "\nTIMEOUT after $timeout seconds of host CPU time\n";
752 # relay_signal($pid, $signal, &$cleanup)
754 # Relays $signal to $pid and then reinvokes it for us with the default
755 # handler. Also cleans up temporary files and invokes $cleanup.
757 my ($pid, $signal, $cleanup) = @_;
759 eval { File::Temp::cleanup() }; # Not defined in old File::Temp.
761 $SIG{$signal} = 'DEFAULT';
762 kill $signal, getpid ();
765 # timeout($pid, $cause, &$cleanup)
767 # Interrupts $pid and dies with a timeout error message,
768 # after invoking $cleanup.
770 my ($pid, $cause, $cleanup) = @_;
775 if (!defined ($cause)) {
776 my ($load_avg) = `uptime` =~ /(load average:.*)$/i;
777 print "\nTIMEOUT after ", time () - $start_time,
778 " seconds of wall-clock time";
779 print " - $load_avg" if defined $load_avg;
782 print "Simulation terminated due to $cause.\n";
787 # Returns the system load average over the last minute.
788 # If the load average is less than 1.0 or cannot be determined, returns 1.0.
789 sub get_load_average {
790 my ($avg) = `uptime` =~ /load average:\s*([^,]+),/;
791 return $avg >= 1.0 ? $avg : 1.0;
794 # Calls setitimer to set a timeout, then execs what was passed to us.
796 if (defined $timeout) {
799 use Time::HiRes qw(setitimer ITIMER_VIRTUAL);
800 setitimer (ITIMER_VIRTUAL, $timeout, 0);
803 { exec ("setitimer-helper", $timeout, @_); };
804 exit 1 if !$!{ENOENT};
805 print STDERR "warning: setitimer-helper is not installed, so ",
806 "CPU time limit will not be enforced\n";
816 foreach my $name (split(' ', $Config{sig_name})) {
817 return $i if $name eq 'VTALRM';