When running a "test" or "assemble" target, run-tests now returns zero
[pintos-anon] / grading / threads / run-tests
index 451b6cac0d57c2393623a1c93e663e2178d3aee5..73ab9a0697087724decac48e67dda3cb733ff70f 100755 (executable)
-#! /usr/bin/perl -w
-
-use POSIX;
-use Text::Diff;
-
-$verbose = 0;
-($GRADES_DIR = $0) =~ s#/[^/]+$##;
--d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
-
--d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
-
-if (! -d "pintos") {
-    my (@tarballs)
-       = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
-               glob ("*.tar.gz"));
-    die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
-    die "no pintos dir and multiple tarballs\n" if scalar (@tarballs) > 1;
-    mkdir "pintos" or die "pintos: mkdir: $!\n";
-    mkdir "pintos/src" or die "pintos: mkdir: $!\n";
-    print "Extracting $tarballs[0]...\n";
-    xsystem ("", "cd pintos/src && tar xzf ../../$tarballs[0]")
-       or die "extraction failed\n";
-}
--d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
-
-print "Compiling initial tree...\n";
-xsystem ("make", "cd pintos/src/threads && make") or die;
-
-for my $test ("alarm-single", "alarm-multiple", "alarm-zero", "alarm-negative",
-             "join-simple"
-             #"join-quick", "join-multiple", "join-nested",
-             #"join-dummy", "join-invalid", "join-no",
-             #"priority-preempt", "priority-fifo", "priority-donate-one",
-             #"priority-donate-multiple", "priority-donate-nest",
-             #"mlfqs"
-             ) {
-    print "Testing $test: ";
-    my ($result) = run_test ($test);
-    print "$result\n";
-
-    if ($result eq 'ok') {
-       print "Grading $test: ";
-       $result = grade_test ($test);
-       print "$result\n";
-    }
-}
+#! /usr/bin/perl
 
-sub grade_test {
-    my ($test) = @_;
+# Find the directory that contains the grading files.
+our ($GRADES_DIR);
 
-    my (@output) = snarf ("output/$test.run.out");
-    
-    ($grade_func = $test) =~ s/-/_/g;
-    eval "grade_$grade_func(\@output)";
-    if ($@) {
-       die $@ if $@ =~ /at \S+ line \d+$/;
-       return $@;
-    }
-    return "ok";
+# Add our Perl library directory to the include path. 
+BEGIN {
+    ($GRADES_DIR = $0) =~ s#/[^/]+$##;
+    -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
+    unshift @INC, "$GRADES_DIR/../lib";
 }
 
-sub grade_alarm_single {
-    verify_alarm (1, @_);
-}
+use warnings;
+use strict;
+use Pintos::Grading;
 
-sub grade_alarm_multiple {
-    verify_alarm (7, @_);
-}
+our ($hw) = "threads";
+our (@TESTS);          # Tests to run.
+our ($test);
+our (%details);
+our (%result);
+our ($action);
 
-sub grade_alarm_zero {
-    my (@output) = @_;
-    #verify_common (@output);
-    die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
-}
+parse_cmd_line qw (alarm-single alarm-multiple alarm-zero alarm-negative
+                  join-simple
+                  join-quick join-multiple join-nested
+                  join-dummy join-invalid join-no
+                  priority-preempt priority-fifo priority-donate-one
+                  priority-donate-multiple priority-donate-nest
+                  mlfqs-on mlfqs-off);
 
-sub grade_alarm_negative {
-    my (@output) = @_;
-    #verify_common (@output);
-    die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
-}
+clean_dir (), exit if $action eq 'clean';
 
-sub grade_join_simple {
-    my (@output) = @_;
-    #verify_common (@output);
-    compare_output ("$GRADES_DIR/join-simple.exp", @output);
+extract_sources (); 
+exit if $action eq 'extract';
+
+build (); 
+exit if $action eq 'build';
+
+run_and_grade_tests ();
+if (defined ($result{'mlfqs-on'}) && defined ($result{'mlfqs-off'})) {
+    grade_mlfqs_speedup ();
+    grade_mlfqs_priority ();
 }
+write_grades (); 
+write_details ();
+exit success () if $action eq 'test';
 
-sub compare_output {
-    my ($exp_file, @actual) = @_;
-    my (@expected) = snarf ($exp_file);
+assemble_final_grade ();
+exit success () if $action eq 'assemble';
 
-    # Trim header and trailer from @actual.
-    while (scalar (@actual) && $actual[0] ne $expected[0]) {
-       shift (@actual);
-    }
-    die "First line of expected output was not present.\n" if !@actual;
-    while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
-       pop (@actual);
+die "Don't know how to '$action'";
+
+# Runs $test in directory output/$test.
+# Returns 'ok' if it went ok, otherwise an explanation.
+sub run_test {
+    # Change constants.h if necessary.
+    my ($defines) = $test ne 'mlfqs-on' ? "" : "#define MLFQS 1\n";
+    if ($defines ne snarf ("pintos/src/constants.h")) {
+       open (CONSTANTS, ">pintos/src/constants.h");
+       print CONSTANTS $defines;
+       close (CONSTANTS);
     }
-    die "Final line of expected output was not present.\n" if !@actual;
-    
-    # Check whether they're the same.
-    if ($#actual == $#expected) {
-       my ($eq) = 1;
-       for (my ($i) = 0; $i <= $#expected; $i++) {
-           $eq = 0 if $actual[$i] ne $expected[$i];
-       }
-       return if $eq;
+
+    # Changes devices/timer.c if necessary.
+    my ($new_time_slice) = $test eq 'priority-fifo' ? 100 : 1;
+    my (@timer) = snarf ("pintos/src/devices/timer.c");
+    if (!grep (/^\#define TIME_SLICE $new_time_slice$/, @timer)) {
+       @timer = grep (!/^\#define TIME_SLICE/, @timer);
+       unshift (@timer, "#define TIME_SLICE $new_time_slice");
+       open (TIMER, ">pintos/src/devices/timer.c");
+       print TIMER map ("$_\n", @timer);
+       close (TIMER);
     }
 
-    # They differ.  Output a diff.
-    my (@diff) = split ("\n", diff (\@expected, \@actual, {CONTEXT => 0}));
-    for (my ($i) = 0; $i < $#diff; ) {
-       if ($diff[$i] =~ /^@@/) {
-           if ($i == 0) {
-               shift (@diff);
-           } else {
-               $diff[$i++] = "";
-           }
-       } else {
-           $i++;
-       }
+    # Copy in the new test.c and delete enough files to ensure a full rebuild.
+    my ($src) = "$GRADES_DIR/" . ($test !~ /^mlfqs/ ? "$test.c" : "mlfqs.c");
+    -e $src or die "$src: stat: $!\n";
+    xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
+    unlink ("pintos/src/threads/build/threads/test.o");
+    unlink ("pintos/src/threads/build/kernel.o");
+    unlink ("pintos/src/threads/build/kernel.bin");
+    unlink ("pintos/src/threads/build/os.dsk");
+
+    # Build.
+    if (xsystem ("cd pintos/src/threads && make",
+                LOG => "$test/make") ne 'ok') {
+       $details{$test} = snarf ("output/$test/make.err");
+       return "Compile error";
     }
-    my ($diff) = join ("\n", @diff);
-    die "Output differs from expected:\n$diff\n";
+
+    # Copy out files for backtraces later.
+    xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
+    xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
+
+    # Run.
+    my ($timeout) = $test !~ /^mlfqs/ ? 10 : 600;
+    return run_pintos ("cd pintos/src/threads/build && pintos -v run -q",
+                      LOG => "$test/run",
+                      TIMEOUT => $timeout);
+}
+\f
+sub grade_alarm_single {
+    verify_alarm (1, @_);
+}
+
+sub grade_alarm_multiple {
+    verify_alarm (7, @_);
 }
 
 sub verify_alarm {
     my ($iterations, @output) = @_;
 
-    #verify_common (@output);
+    verify_common (@output);
 
     my (@products);
     for (my ($i) = 0; $i < $iterations; $i++) {
@@ -138,8 +121,9 @@ sub verify_alarm {
     }
     @products = sort {$a <=> $b} @products;
 
-    for $_ (@output) {
-       die $_ if /Out of order/;
+    local ($_);
+    foreach (@output) {
+       die $_ if /out of order/i;
 
        my ($p) = /product=(\d+)$/;
        next if !defined $p;
@@ -152,63 +136,199 @@ sub verify_alarm {
        if @products != 0;
 }
 
-sub run_test {
-    my ($test) = @_;
-    return "ok" if -f "output/$test.run.out";
-    
-    my ($src) = "$GRADES_DIR/$test.c";
-    -e $src or die "$src: stat: $!\n";
-    xsystem ("", "cp $src pintos/src/threads/test.c") or die;
-    unlink ("pintos/src/threads/build/threads/test.o");
-    unlink ("pintos/src/threads/build/kernel.o");
-    unlink ("pintos/src/threads/build/kernel.bin");
-    unlink ("pintos/src/threads/build/os.dsk");
-    xsystem ("$test.make", "cd pintos/src/threads && make")
-       or return "compile error";
-    xsystem ("$test.run", "cd pintos/src/threads/build && pintos -v run -q")
-       or return "Bochs error";
-    return "ok";
-}
-
-sub xsystem {
-    my ($log, $command) = @_;
-    print "$command\n" if $verbose;
-
-    my ($status);
-    if ($log ne '') {
-       $status = systimeout ("($command) >output/$log.out 2>output/$log.err");
-       unlink ("output/$log.err") unless $status != 0;
-    } else {
-       $status = systimeout ($command);
+sub grade_alarm_zero {
+    my (@output) = @_;
+    verify_common (@output);
+    die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
+}
+
+sub grade_alarm_negative {
+    my (@output) = @_;
+    verify_common (@output);
+    die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
+}
+
+sub grade_join_invalid {
+    my (@output) = @_;
+    verify_common (@output);
+    grep (/Testing invalid join/, @output) or die "Test didn't start\n";
+    grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
+}
+
+sub grade_join_no {
+    my (@output) = @_;
+    verify_common (@output);
+    grep (/Testing no join/, @output) or die "Test didn't start\n";
+    grep (/No join test done/, @output) or die "Test didn't complete\n";
+}
+
+sub grade_join_multiple {
+    my (@output) = @_;
+
+    verify_common (@output);
+    my (@t);
+    $t[4] = $t[5] = $t[6] = -1;
+    local ($_);
+    foreach (@output) {
+       my ($idx) = /^Thread (\d+)/ or next;
+       my ($iter) = /iteration (\d+)$/;
+       $iter = 5 if /done!$/;
+       die "Malformed output\n" if !defined $iter;
+       if ($idx == 6) {
+           die "Thread 6 started before either other thread finished\n"
+               if $t[4] < 5 && $t[5] < 5;
+           die "Thread 6 started before thread 4 finished\n"
+               if $t[4] < 5;
+           die "Thread 6 started before thread 5 finished\n"
+               if $t[5] < 5;
+       }
+       die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
+       $t[$idx] = $iter;
+    }
+
+    my ($err) = "";
+    for my $idx (4, 5, 6) {
+       if ($t[$idx] == -1) {
+           $err .= "Thread $idx did not run at all\n";
+       } elsif ($t[$idx] != 5) {
+           $err .= "Thread $idx only completed $t[$idx] iterations\n";
+       }
+    }
+    die $err if $err ne '';
+}
+
+sub grade_priority_fifo {
+    my (@output) = @_;
+
+    verify_common (@output);
+    my ($thread_cnt) = 10;
+    my ($iter_cnt) = 5;
+    my (@order);
+    my (@t) = (-1) x $thread_cnt;
+    local ($_);
+    foreach (@output) {
+       my ($idx) = /^Thread (\d+)/ or next;
+       my ($iter) = /iteration (\d+)$/;
+       $iter = $iter_cnt if /done!$/;
+       die "Malformed output\n" if !defined $iter;
+       if (@order < $thread_cnt) {
+           push (@order, $idx);
+           die "Thread $idx repeated within first $thread_cnt iterations: "
+               . join (' ', @order) . ".\n"
+               if grep ($_ == $idx, @order) != 1;
+       } else {
+           die "Thread $idx ran when $order[0] should have.\n"
+               if $idx != $order[0];
+           push (@order, shift @order);
+       }
+       die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
+       $t[$idx] = $iter;
+    }
+
+    my ($err) = "";
+    for my $idx (0..$#t) {
+       if ($t[$idx] == -1) {
+           $err .= "Thread $idx did not run at all.\n";
+       } elsif ($t[$idx] != $iter_cnt) {
+           $err .= "Thread $idx only completed $t[$idx] iterations.\n";
+       }
     }
+    die $err if $err ne '';
+}
 
-    die "Interrupted\n"
-       if WIFSIGNALED ($status) && WTERMSIG ($status) == SIGINT;
+sub grade_mlfqs_on {
+    my (@output) = @_;
+    verify_common (@output);
+    our (@mlfqs_on_stats) = mlfqs_stats (@output);
+}
 
-    return $status == 0;
+sub grade_mlfqs_off {
+    my (@output) = @_;
+    verify_common (@output);
+    our (@mlfqs_off_stats) = mlfqs_stats (@output);
 }
 
-sub systimeout {
-    my ($command) = @_;
-    my ($status);
+sub grade_mlfqs_speedup {
+    our (@mlfqs_off_stats);
+    our (@mlfqs_on_stats);
     eval {
-       local $SIG{ALRM} = sub { die "alarm\n" };
-       alarm 10;
-       $status = system ($command);
-       alarm 0;
+       check_mlfqs ();
+       my ($off_ticks) = $mlfqs_off_stats[1];
+       my ($on_ticks) = $mlfqs_on_stats[1];
+       die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
+           if $on_ticks >= $off_ticks;
+       die "ok\n";
     };
-    if ($@) {
-       die unless $@ eq "alarm\n";   # propagate unexpected errors
-       print "Timed out.\n";
-       $status = -1;
-    }
-    return $status;
+    chomp $@;
+    $result{'mlfqs-speedup'} = $@;
 }
 
-sub snarf {
-    my ($file) = @_;
-    open (OUTPUT, $file) or die "$file: open: $!\n";
-    my (@lines) = <OUTPUT>;
-    close (OUTPUT);
-    return @lines;
+sub grade_mlfqs_priority {
+    our (@mlfqs_off_stats);
+    our (@mlfqs_on_stats);
+    eval {
+       check_mlfqs () if !defined (@mlfqs_on_stats);
+       for my $cat qw (CPU IO MIX) {
+           die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
+               if $mlfqs_off_stats[0]{$cat}{MIN} != 29
+               || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
+           die "Minimum priority never changed from PRI_DEFAULT (29) "
+               . "with MLFQS\n"
+               if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
+           die "Maximum priority never changed from PRI_DEFAULT (29) "
+               . "with MLFQS\n"
+               if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
+       }
+       die "ok\n";
+    };
+    chomp $@;
+    $result{'mlfqs-priority'} = $@;
+}
+
+sub check_mlfqs {
+    our (@mlfqs_off_stats);
+    our (@mlfqs_on_stats);
+    die "p1-4 didn't finish with MLFQS on or off\n"
+       if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
+    die "p1-4 didn't finish with MLFQS on\n"
+       if !defined (@mlfqs_on_stats);
+    die "p1-4 didn't finish with MLFQS off\n"
+       if !defined (@mlfqs_off_stats);
+}
+
+sub mlfqs_stats {
+    my (@output) = @_;
+    my (%stats) = (CPU => {}, IO => {}, MIX => {});
+    my (%map) = ("CPU intensive" => 'CPU',
+                "IO intensive" => 'IO',
+                "Alternating IO/CPU" => 'MIX');
+    my (%rmap) = reverse %map;
+    my ($ticks);
+    local ($_);
+    foreach (@output) {
+       $ticks = $1 if /Timer: (\d+) ticks/;
+       my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
+       my ($t) = $map{$thread} or next;
+       
+       my ($s) = $stats{$t};
+       $$s{N}++;
+       $$s{SUM} += $pri;
+       $$s{SUM2} += $pri * $pri;
+       $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
+       $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
+    }
+
+    my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
+    for my $cat (values (%map)) {
+       my ($s) = $stats{$cat};
+       die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
+           if $$s{N} != $expect_n{$cat};
+       die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
+           if $$s{MIN} < 0;
+       die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
+           if $$s{MAX} > 59;
+       $$s{MEAN} = $$s{SUM} / $$s{N};
+    }
+
+    return (\%stats, $ticks);
 }