Fix zero-length file case on put.
[pintos-anon] / src / utils / pintos
index f30e03adfe67ed56f582041d457f81f2d6f6a5e6..a3c9c4c2a54679985323438d8c456718f6177b62 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;
@@ -96,12 +110,18 @@ if ($cmd eq 'run') {
     # Create scratch disk from file.
     die "$hostfn: $!\n" if ! -e $hostfn;
     my ($size) = -s _;
-    copy_pad ($hostfn, "scratch.dsk", 512);
+    if ($size) { 
+       copy_pad ($hostfn, "scratch.dsk", 512);
+    } else {
+       open (SCRATCH, ">scratch.dsk") or die "scratch.dsk: create: $!\n";
+       syswrite (SCRATCH, "\0" x 512);
+       close (SCRATCH);
+    }
 
     # 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 +138,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";
@@ -165,6 +185,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 +206,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 +243,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 +262,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 +358,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})) {