Add "real-time" option to pintos utility and document its usage.
[pintos-anon] / src / utils / pintos
index a27384ddf3f01cf1a7d74fdb6187756e92ec1a07..124269848c0926063935cd0e4a7d897b162a310d 100755 (executable)
@@ -8,7 +8,7 @@ our (@disks) = ("os.dsk", "fs.dsk", "scratch.dsk", "swap.dsk");
 our ($sim);
 our ($debug);
 our ($vga);
-our ($jitter);
+our ($jitter, $realtime);
 
 use Getopt::Long qw(:config require_order bundling);
 GetOptions ("sim=s" => sub { set_sim (@_) },
@@ -24,7 +24,8 @@ GetOptions ("sim=s" => sub { set_sim (@_) },
            "run|get|put|make-disk" => \&cmd_option,
            
            "m|memory=i" => \$mem,
-           "j|jitter=i" => \$jitter,
+           "j|jitter=i" => sub { set_jitter (@_) },
+           "r|realtime" => sub { set_realtime () },
            
            "v|no-vga" => sub { set_vga ('none'); },
            "s|no-serial" => sub { $serial_out = 0; },
@@ -64,6 +65,19 @@ sub set_vga {
     $vga = $new_vga;
 }
 
+sub set_jitter {
+    my ($new_jitter) = @_;
+    die "--realtime conflicts with --jitter\n" if defined $realtime;
+    die "different --jitter already defined\n"
+       if defined $jitter && $jitter != $new_jitter;
+    $jitter = $new_jitter;
+}
+
+sub set_realtime {
+    die "--realtime conflicts with --jitter\n" if defined $jitter;
+    $realtime = 1;
+}
+
 sub cmd_option {
     # Force an end to option processing, as with --.
     die ("!FINISH");
@@ -73,7 +87,7 @@ die "no command specified; use --help for help\n"
     if @ARGV < 1;
 my ($cmd) = shift @ARGV;
 if ($cmd eq 'run') {
-    run_vm (@ARGV);
+    run_vm ('EXEC', @ARGV);
 } elsif ($cmd eq 'make-disk') {
     usage () if @ARGV != 2;
     my ($file, $mb) = @ARGV;
@@ -101,7 +115,7 @@ if ($cmd eq 'run') {
     # Do copy.
     my (@cmd) = ("-ci", $guestfn, $size, "-q");
     unshift (@cmd, "-f") if $format;
-    run_vm (@cmd);
+    run_vm ('EXEC', @cmd);
 } elsif ($cmd eq 'get') {
     usage () if @ARGV != 1 && @ARGV != 2;
     my ($guestfn, $hostfn) = @ARGV;
@@ -118,7 +132,7 @@ if ($cmd eq 'run') {
        if $scratch_size < $fs_size + 16384;
 
     # Do copy.
-    run_vm ("-co", $guestfn, "-q");
+    run_vm ('FORK', "-co", $guestfn, "-q");
 
     # Read out scratch disk.
     print "copying $guestfn from $disks[2] to $hostfn...\n";
@@ -126,7 +140,7 @@ if ($cmd eq 'run') {
     open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
     my ($input);
     read (SRC, $input, 512) == 512 or die "$disks[2]: read error\n";
-    my ($size) = unpack ("%V", $input);
+    my ($size) = unpack ("V", $input);
     $size != 0xffffffff or die "$guestfn: too big for $disks[2]?";
     my ($src);
     read (SRC, $src, $size) == $size or die "$disks[2]: read error\n";
@@ -165,6 +179,7 @@ sub usage {
     print "  -t, --terminal   Display VGA in terminal (Bochs only)\n";
     print "VM options:\n";
     print "  -j SEED          Randomize timer interrupts (Bochs only)\n";
+    print "  -r, --realtime   Use realistic, but not reproducible, timings\n";
     print "  -m, --mem=MB     Run VM with MB megabytes of physical memory\n";
     print "Disk options:\n";
     print "  --os-disk=DISK    Set OS disk file (default: os.dsk)\n";
@@ -185,6 +200,9 @@ sub create_disk {
 }
 
 sub run_vm {
+    my ($fork) = shift;
+    $fork eq 'FORK' || $fork eq 'EXEC' or die;
+
     our (@disks);
 
     die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
@@ -219,7 +237,11 @@ sub run_vm {
        print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
        print BOCHSRC "boot: c\n";
        print BOCHSRC "ips: 1000000\n";
-       print BOCHSRC "clock: sync=none, time0=0\n";
+       if (!$realtime) {
+           print BOCHSRC "clock: sync=none, time0=0\n";
+       } else {
+           print BOCHSRC "clock: sync=realtime, time0=0\n";
+       }
        print BOCHSRC "megs: $mem\n";
        print BOCHSRC "log: bochsout.txt\n";
        if ($vga ne 'terminal') {
@@ -234,7 +256,8 @@ sub run_vm {
 
        my (@cmd) = ($bin, '-q');
        push (@cmd, '-j', $jitter) if defined $jitter;
-       run_command_no_die (@cmd);
+       print join (' ', @cmd), "\n";
+       $fork eq 'EXEC' ? exec (@cmd) : system (@cmd);
     } elsif ($sim eq 'qemu') {
        print "warning: qemu doesn't support --terminal\n"
            if $vga eq 'terminal';
@@ -329,11 +352,6 @@ sub run_command {
     die "command failed\n" if system (@_);
 }
 
-sub run_command_no_die {
-    print join (' ', @_), "\n";
-    system (@_);
-}
-
 sub search_path {
     my ($target) = @_;
     for my $dir (split (':', $ENV{PATH})) {