3 # Find the directory that contains the grading files.
6 # Add our Perl library directory to the include path.
8 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
9 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
10 unshift @INC, "$GRADES_DIR/../lib";
19 our ($VERBOSE) = 0; # Verbosity of output
20 our (@TESTS); # Tests to run.
24 GetOptions ("v|verbose+" => \$VERBOSE,
25 "h|help" => sub { usage (0) },
26 "t|test=s" => \@TESTS,
29 or die "Malformed command line; use --help for help.\n";
30 die "Non-option argument not supported; use --help for help.\n"
35 print "run-tests, for grading Pintos multiprogramming projects.\n\n";
36 print "Invoke from a directory containing a student tarball named by\n";
37 print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
38 print "In normal usage, no options are needed.\n\n";
39 print "Output is produced in tests.out and details.out.\n\n";
41 print " -c, --clean Remove old output files before starting\n";
42 print " -t, --test=TEST Execute TEST only (allowed multiple times)\n";
43 print " -g, --grade Instead of running tests, compose grade.out\n";
44 print " -v, --verbose Print commands before executing them\n";
45 print " -h, --help Print this help message\n";
49 # Default set of tests.
50 @TESTS = qw (args-argc args-argv0 args-argvn args-single args-multiple
52 sc-bad-sp sc-bad-arg sc-boundary
54 create-normal create-empty create-null create-bad-ptr
55 create-long create-exists create-bound
56 open-normal open-missing open-boundary open-empty open-null
57 open-bad-ptr open-twice
58 close-normal close-twice close-stdin close-stdout close-bad-fd
59 read-normal read-bad-ptr read-boundary read-zero read-stdout
61 write-normal write-bad-ptr write-boundary write-zero write-stdin
63 exec-once exec-arg exec-multiple exec-missing exec-bad-ptr
64 join-simple join-twice join-killed join-bad-pid
65 multi-recurse multi-oom multi-child-fd
69 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
70 $args{$key} = "some arguments for you!";
72 $args{'args-single'} = "onearg";
73 $args{'args-dbl-space'} = "two args";
74 $args{'multi-recurse'} = "15";
75 $args{'multi-oom'} = "0";
77 # Handle final grade mode.
79 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
81 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
88 my (@tests) = snarf ("tests.out");
89 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
91 my (@review) = snarf ("review.txt");
92 my ($part_lost) = (0, 0);
93 for (my ($i) = $#review; $i >= 0; $i--) {
94 local ($_) = $review[$i];
95 if (my ($loss) = /^\s*([-+]\d+)/) {
97 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
98 my ($got) = $out_of + $part_lost;
100 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
107 die "Lost points outside a section\n" if $part_lost;
109 for (my ($i) = 1; $i <= $#review; $i++) {
110 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
111 $review[$i] = '-' x (length ($review[$i - 1]));
115 print OUT "\nOVERALL SCORE\n";
116 print OUT "-------------\n";
117 print OUT "$p_got points out of $p_pos total\n\n";
119 print OUT map ("$_\n", @tests), "\n";
120 print OUT map ("$_\n", @review), "\n";
122 print OUT "DETAILS\n";
123 print OUT "-------\n\n";
124 print OUT map ("$_\n", snarf ("details.out"));
130 # Verify that we're roughly in the correct directory
131 # before we go blasting away files.
134 xsystem ("rm -rf output pintos", VERBOSE => 1);
135 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
138 # Create output directory, if it doesn't already exist.
139 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
141 # Extract submission.
142 extract_tarball () if ! -d "pintos";
144 # Compile submission.
147 # Verify that the proper directory was submitted.
148 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
150 # Run and grade the tests.
157 my ($result) = run_test ($test);
158 if ($result eq 'ok') {
159 $result = grade_test ($test);
163 print " - with warnings" if $result eq 'ok' && defined $details{$test};
166 $result{$test} = $result;
175 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
177 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
179 # Sort tarballs in reverse order by time.
180 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
182 print "Multiple tarballs: choosing $tarballs[0]\n"
183 if scalar (@tarballs) > 1;
187 sub extract_tarball {
188 my ($tarball) = choose_tarball ();
190 mkdir "pintos" or die "pintos: mkdir: $!\n";
191 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
193 print "Extracting $tarball...\n";
194 xsystem ("cd pintos/src && tar xzf ../../$tarball",
195 DIE => "extraction failed\n");
198 print "Running fixme.sh...\n";
199 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
202 print "Patching...\n";
203 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
205 DIE => "patch failed\n");
206 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
207 . "< $GRADES_DIR/random.diff",
209 DIE => "patch failed\n");
211 open (CONSTANTS, ">pintos/src/constants.h")
212 or die "constants.h: create: $!\n";
213 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
219 my ($ms, $d, $y, $H, $M, $S) =
220 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
222 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
224 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
229 my ($src) = "$GRADES_DIR/$test.c";
230 -e $src or die "$src: stat: $!\n";
242 # Reuse older results if any.
243 if (open (DONE, "<output/$test/done")) {
251 # Really run the test.
252 my ($status) = really_run_test ($test);
254 # Save the results for later.
255 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
256 print DONE "$status\n";
263 print "Compiling...\n";
264 xsystem ("cd pintos/src/userprog && make", LOG => "make")
265 or return "compile error";
268 sub really_run_test {
270 # If there's residue from an earlier test, move it to .old.
271 # If there's already a .old, delete it.
272 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
273 rename "output/$test", "output/$test.old" or die "rename: $!\n"
274 if -d "output/$test";
276 # Make output directory.
277 mkdir "output/$test";
278 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
279 DIE => "cp failed\n");
283 $timeout = 600 if $test =~ /^multi-/;
284 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
286 . "--os-disk=pintos/src/userprog/build/os.dsk "
287 . "--fs-disk=output/$test/fs.dsk "
288 . "-v run -q -ex \"$test$testargs\"",
291 or return "Bochs error";
299 my (@output) = snarf ("output/$test/run.out");
301 my ($grade_func) = "grade_$test";
302 $grade_func =~ s/-/_/g;
303 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
305 verify_common (@output);
306 compare_output ("$GRADES_DIR/$test.exp", @output);
309 eval "$grade_func (\@output)";
312 die $@ if $@ =~ /at \S+ line \d+$/;
318 sub grade_write_normal {
320 verify_common (@output);
321 compare_output ("$GRADES_DIR/write-normal.exp", @output);
322 my ($test_txt) = "output/$test/test.txt";
323 get_file ("test.txt", $test_txt) if ! -e $test_txt;
325 my (@actual) = snarf ($test_txt);
326 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
329 if ($#actual == $#expected) {
331 for my $i (0...$#actual) {
332 $eq = 0 if $actual[$i] ne $expected[$i];
339 $details = "Expected file content:\n";
340 $details .= join ('', map (" $_\n", @expected));
341 $details .= "Actual file content:\n";
342 $details .= join ('', map (" $_\n", @actual));
343 $extra{$test} = $details;
345 die "File written didn't have expected content.\n";
349 sub grade_multi_oom {
351 verify_common (@output);
353 @output = fix_exit_codes (get_core_output (@output));
355 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
356 die "Child process $m started out of order.\n" if $m != $n;
360 die "Only $n child process(es) started.\n" if $n < 15;
362 # There could be a death notice for a process that didn't get
363 # fully loaded, and/or notices from the loader.
365 && ($output[0] =~ /^multi-oom: exit\(-1\)$/
366 || $output[0] =~ /^load: /)) {
371 die "Output ended unexpectedly before process $n finished.\n"
375 chomp ($_ = shift @output);
376 die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
377 die "Child process $n ended out of order.\n"
378 if !/^\(multi-oom\) end $n$/;
380 chomp ($_ = shift @output);
381 die "Kernel didn't print proper exit message for process $n.\n"
382 if !/^multi-oom: exit\($n\)$/;
384 die "Spurious output at end: '$output[0]'.\n" if @output;
388 my ($guest_fn, $host_fn) = @_;
390 . "--os-disk=pintos/src/userprog/build/os.dsk "
391 . "--fs-disk=output/$test/fs.dsk "
392 . "-v get $guest_fn $host_fn",
393 LOG => "$test/get-$guest_fn",
395 or die "get $guest_fn failed\n";
402 my (@assertion) = grep (/PANIC/, @output);
403 if (@assertion != 0) {
404 my ($details) = "Kernel panic:\n $assertion[0]\n";
406 my (@stack_line) = grep (/Call stack:/, @output);
407 if (@stack_line != 0) {
408 $details .= " $stack_line[0]\n\n";
409 $details .= "Translation of backtrace:\n";
410 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
414 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
417 $A2L = "i386-elf-addr2line";
419 open (A2L, "$A2L -fe pintos/src/userprog/build/kernel.o @addrs|");
421 my ($function, $line);
422 last unless defined ($function = <A2L>);
426 $details .= " $function ($line)\n";
429 $extra{$test} = $details;
430 die "Kernel panic. Details at end of file.\n"
433 if (grep (/Pintos booting/, @output) > 1) {
436 $details = "Pintos spontaneously rebooted during this test.\n";
437 $details .= "This is most often due to unhandled page faults.\n";
438 $details .= "Here's the output from the initial boot through the\n";
439 $details .= "first reboot:\n\n";
445 last if /Pintos booting/ && ++$i > 1;
447 $details{$test} = $details;
448 die "Triple-fault caused spontaneous reboot(s). "
449 . "Details at end of file.\n";
452 die "No output at all\n" if @output == 0;
453 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
454 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
455 die "Didn't start up properly: no \"Boot complete\" startup message\n"
456 if !grep (/Boot complete/, @output);
457 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
458 if !grep (/Timer: \d+ ticks/, @output);
459 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
460 if !grep (/Powering off/, @output);
463 # Get @output without header or trailer.
464 sub get_core_output {
469 for ($first = 0; $first <= $#output; $first++) {
470 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
474 for ($last = $#output; $last >= 0; $last--) {
475 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
478 if ($last < $first) {
479 my ($no_first) = $first > $#output;
480 my ($no_last) = $last < $#output;
481 die "Couldn't locate output.\n";
484 return @output[$first ... $last];
490 # Fix up lines that look like exit codes.
491 # Exit codes are supposed to be printed in the form "process: exit(code)"
492 # but people get unfortunately creative with it.
493 for my $i (0...$#output) {
494 local ($_) = $output[$i];
496 my ($process, $code);
497 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
498 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
499 || (($process, $code)
500 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
501 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
502 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
504 $process = substr ($process, 0, 15);
505 $process =~ s/\s.*//;
506 $output[$i] = "$process: exit($code)\n";
514 my ($exp, @actual) = @_;
515 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
518 $details .= "$test actual output:\n";
519 $details .= join ('', map (" $_", @actual));
521 my (@exp) = map ("$_\n", snarf ($exp));
523 my ($fuzzy_match) = 0;
527 my ($s) = shift (@exp);
528 last if $s eq "--OR--\n";
529 push (@expected, $s);
532 $details .= "\n$test acceptable output:\n";
533 $details .= join ('', map (" $_", @expected));
535 # Check whether they're the same.
536 if ($#actual == $#expected) {
538 for (my ($i) = 0; $i <= $#expected; $i++) {
539 $eq = 0 if $actual[$i] ne $expected[$i];
544 # They differ. Output a diff.
546 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
547 my ($not_fuzzy_match) = 0;
548 while ($d->Next ()) {
549 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
551 push (@diff, map (" $_", $d->Items (1)));
553 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
554 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
556 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
557 $not_fuzzy_match = 1;
561 $fuzzy_match = 1 if !$not_fuzzy_match;
563 $details .= "Differences in `diff -u' format:\n";
564 $details .= join ('', @diff);
565 $details .= "(This is considered a `fuzzy match'.)\n"
566 if !$not_fuzzy_match;
571 "This test passed, but with extra, unexpected output.\n"
572 . "Please inspect your code to make sure that it does not\n"
573 . "produce output other than as specified in the project\n"
578 "This test failed because its output did not match any\n"
579 . "of the acceptable form(s).\n\n"
583 $details{$test} = $details;
584 die "Output differs from expected. Details at end of file.\n"
589 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
595 for (my ($i) = 0; $i <= $#summary; $i++) {
596 local ($_) = $summary[$i];
597 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
598 my ($result) = $result{$test} || "Not tested.";
600 if ($result eq 'ok') {
601 if (!defined $details{$test}) {
602 # Test successful and no warnings.
603 splice (@summary, $i, 1);
606 # Test successful with warnings.
609 splice (@summary, $i + 1, 0,
610 " Test passed with warnings. "
611 . "Details at end of file.");
617 splice (@summary, $i + 1, 0,
618 map (" $_", split ("\n", $result)));
620 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
622 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
623 splice (@summary, $i, 0, " All tests passed.")
624 if $ploss == 0 && !$warnings;
630 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
631 $summary[0] =~ s/\[\[total\]\]/$ts/;
633 open (SUMMARY, ">tests.out");
634 print SUMMARY map ("$_\n", @summary);
639 open (DETAILS, ">details.out");
641 for my $test (@TESTS) {
642 next if $result{$test} eq 'ok' && !defined $details{$test};
644 my ($details) = $details{$test};
645 next if !defined ($details) && ! -e "output/$test/run.out";
648 if ($result{$test} ne 'ok') {
649 $banner = "$test failure details";
651 $banner = "$test warnings";
654 print DETAILS "\n" if $n++;
655 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
656 print DETAILS "\n\n";
658 if (!defined $details) {
659 $details = "Output:\n\n" . snarf ("output/$test/run.out");
661 print DETAILS $details;
663 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
664 if defined $extra{$test};
671 my ($command, %options) = @_;
672 print "$command\n" if $VERBOSE || $options{VERBOSE};
674 my ($log) = $options{LOG};
675 if (defined ($log)) {
676 $command = "($command) >output/$log.out 2>output/$log.err";
681 local $SIG{ALRM} = sub { die "alarm\n" };
682 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
684 die "fork: $!\n" if !defined $pid;
685 exec ($command), die "$command: exec: $!\n" if !$pid;
691 die unless $@ eq "alarm\n"; # propagate unexpected errors
692 print "Timed out.\n";
697 if (WIFSIGNALED ($status)) {
698 my ($signal) = WTERMSIG ($status);
699 die "Interrupted\n" if $signal == SIGINT;
700 print "Child terminated with signal $signal\n";
703 unlink ("output/$log.err") if defined ($log) && $status == 0;
705 die $options{DIE} if $status != 0 && defined $options{DIE};
712 open (OUTPUT, $file) or die "$file: open: $!\n";
713 my (@lines) = <OUTPUT>;
716 return wantarray ? @lines : join ('', map ("$_\n", @lines));
722 open (A, "<$a") or die "$a: open: $!\n";
723 open (B, "<$b") or die "$b: open: $!\n";
729 sysread (A, $sa, 1024);
730 sysread (B, $sb, 1024);
731 $equal = 0, last if $sa ne $sb;
732 $equal = 1, last if $sa eq '';
741 my ($file, $expected) = @_;
742 open (FILE, "<$file") or die "$file: open: $!\n";
744 sysread (FILE, $actual, -s FILE);
745 my ($equal) = $actual eq $expected;
751 my ($ln, $lines) = @_;
753 for my $line (@$lines) {
755 $out .= sprintf "%4d %s\n", $ln++, $line;