Use runtime options instead of conditional compilation for MLFQS,
[pintos-anon] / grading / lib / Pintos / Grading.pm
index 3ebeb223b6a7ee53f5b7c6f11e943c2010075156..ad2a8259ea85a88683357e883895ea5a6ed02c04 100644 (file)
@@ -13,13 +13,20 @@ our $action;
 our $hw;
 
 use POSIX;
-use Getopt::Long;
+use Getopt::Long qw(:config no_ignore_case);
 use Algorithm::Diff;
 \f
+# We execute lots of subprocesses.
+# Without this, our stdout output can get flushed multiple times,
+# which is harmless but looks bizarre.
+$| = 1;
+
 sub parse_cmd_line {
+    my ($do_regex, $no_regex);
     GetOptions ("v|verbose+" => \$verbose,
                "h|help" => sub { usage (0) },
-               "tests=s" => \@TESTS,
+               "d|do-tests=s" => \$do_regex,
+               "n|no-tests=s" => \$no_regex,
                "c|clean" => sub { set_action ('clean'); },
                "x|extract" => sub { set_action ('extract'); },
                "b|build" => sub { set_action ('build'); },
@@ -33,6 +40,11 @@ sub parse_cmd_line {
     if (!defined $action) {
        $action = -e 'review.txt' ? 'assemble' : 'test';
     }
+
+    my (@default_tests) = @_;
+    @TESTS = @default_tests;
+    @TESTS = grep (/$do_regex/, @TESTS) if defined $do_regex;
+    @TESTS = grep (!/$no_regex/, @TESTS) if defined $no_regex;
 }
 
 sub set_action {
@@ -70,7 +82,8 @@ Workflow:
 
 Options:
   -c, --clean        Delete test results and temporary files, then exit.
-  --tests=TESTS      Run only the specified comma-separated tests.
+  -d, --do-tests=RE  Run only tests that match the given regular expression.
+  -n, --no-tests=RE  Do not run tests that match the given regular expression.
   -x, --extract      Stop after step 1.
   -b, --build        Stop after step 2.
   -t, --test         Stop after step 3 (default if "review.txt" not present).
@@ -83,9 +96,8 @@ EOF
 \f
 # Source tarballs.
 
-# Extracts the group's source files into pintos/src,
-# applies any patches providing in the grading directory,
-# and installs a default pintos/src/constants.h
+# Extracts the group's source files into pintos/src
+# and applies any patches providing in the grading directory.
 sub extract_sources {
     # Make sure the output dir exists.
     -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
@@ -124,12 +136,6 @@ sub extract_sources {
        xsystem ("patch -fs -p0 < $patch",
                 LOG => $stem, DIE => "applying patch $stem failed\n");
     }
-
-    # Install default pintos/src/constants.h.
-    open (CONSTANTS, ">pintos/src/constants.h")
-       or die "constants.h: create: $!\n";
-    print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
-    close CONSTANTS;
 }
 
 # Returns the name of the tarball to extract.
@@ -303,7 +309,13 @@ sub xsystem {
                open (STDOUT, ">output/$log.out");
                open (STDERR, ">output/$log.err");
            }
-           exec ($command);
+           chdir $options{CHDIR} or die "$options{CHDIR}: chdir: $!\n"
+             if defined ($options{CHDIR});
+           if (!defined ($options{EXEC})) {
+               exec ($command);
+           } else {
+               exec (@{$options{EXEC}});
+           }
            exit (-1);
        }
        waitpid ($pid, 0);
@@ -332,12 +344,9 @@ sub xsystem {
            print "Child terminated with signal $signal\n";
        }
 
-       my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
-       $result = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status
-           ? "ok" : "error";
+       $result = $status == 0 ? "ok" : "error";
     }
 
-
     if ($result eq 'error' && defined $options{DIE}) {
        my ($msg) = $options{DIE};
        if (defined ($log)) {
@@ -389,10 +398,15 @@ sub get_test_result {
 
 sub run_pintos {
     my ($cmd_line, %args) = @_;
-    $args{EXPECT} = 1 unless defined $args{EXPECT};
-    my ($retval) = xsystem ($cmd_line, %args);
+    unshift (@$cmd_line, 'pintos');
+    my ($retval) = xsystem (join (' ', @$cmd_line), %args, EXEC => $cmd_line);
     return 'ok' if $retval eq 'ok';
-    return "Timed out after $args{TIMEOUT} seconds" if $retval eq 'timeout';
+    if ($retval eq 'timeout') {
+       my ($msg) = "Timed out after $args{TIMEOUT} seconds";
+       my ($load_avg) = `uptime` =~ /(load average:.*)$/i;
+       $msg .= " - $load_avg" if defined $load_avg;
+       return $msg;
+    }
     return 'Error running Bochs' if $retval eq 'error';
     die;
 }
@@ -400,7 +414,16 @@ sub run_pintos {
 # Grade the test.
 sub grade_test {
     # Read test output.
-    my (@output) = snarf ("output/$test/run.out");
+    my ($outfile) = "output/$test/run.out";
+    if (! -e $outfile) {
+       if (-s "output/$test/make.err") {
+           # make failed.
+           $details{$test} = snarf ("output/$test/make.err");
+           return "make failed.  Error messages at end of file.";
+       }
+       return "preparation for test failed";
+    }
+    my (@output) = snarf ($outfile);
 
     # If there's a function "grade_$test", use it to evaluate the output.
     # If there's a file "$GRADES_DIR/$test.exp", compare its contents
@@ -817,5 +840,12 @@ sub file_contains {
     close (FILE);
     return $equal;
 }
+\f
+sub success {
+    for my $test (@TESTS) {
+       return 1 if !defined ($result{$test}) || $result{$test} ne 'ok';
+    }
+    return 0;
+}
 
 1;