9 our ($VERBOSE) = 0; # Verbosity of output
10 our (@TESTS); # Tests to run.
14 GetOptions ("v|verbose+" => \$VERBOSE,
15 "h|help" => sub { usage (0) },
16 "t|test=s" => \@TESTS,
19 or die "Malformed command line; use --help for help.\n";
20 die "Non-option argument not supported; use --help for help.\n"
25 print "run-tests, for grading Pintos multiprogramming projects.\n\n";
26 print "Invoke from a directory containing a student tarball named by\n";
27 print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
28 print "In normal usage, no options are needed.\n\n";
29 print "Output is produced in tests.out and details.out.\n\n";
31 print " -c, --clean Remove old output files before starting\n";
32 print " -t, --test=TEST Execute TEST only (allowed multiple times)\n";
33 print " -g, --grade Instead of running tests, compose grade.out\n";
34 print " -v, --verbose Print commands before executing them\n";
35 print " -h, --help Print this help message\n";
39 # Default set of tests.
40 @TESTS = qw (pt-grow-stack pt-big-stk-obj pt-bad-addr pt-write-code
41 page-linear page-parallel page-merge-seq page-merge-par
42 page-shuffle mmap-read mmap-close mmap-unmap mmap-overlap
43 mmap-twice mmap-write mmap-exit mmap-shuffle
48 # Handle final grade mode.
50 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
52 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
59 my (@tests) = snarf ("tests.out");
60 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
62 my (@review) = snarf ("review.txt");
63 my ($part_lost) = (0, 0);
64 for (my ($i) = $#review; $i >= 0; $i--) {
65 local ($_) = $review[$i];
66 if (my ($loss) = /^\s*([-+]\d+)/) {
68 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
69 my ($got) = $out_of + $part_lost;
71 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
78 die "Lost points outside a section\n" if $part_lost;
80 for (my ($i) = 1; $i <= $#review; $i++) {
81 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
82 $review[$i] = '-' x (length ($review[$i - 1]));
86 print OUT "\nOVERALL SCORE\n";
87 print OUT "-------------\n";
88 print OUT "$p_got points out of $p_pos total\n\n";
90 print OUT map ("$_\n", @tests), "\n";
91 print OUT map ("$_\n", @review), "\n";
93 print OUT "DETAILS\n";
94 print OUT "-------\n\n";
95 print OUT map ("$_\n", snarf ("details.out"));
100 # Find the directory that contains the grading files.
102 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
103 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
106 # Verify that we're roughly in the correct directory
107 # before we go blasting away files.
110 xsystem ("rm -rf output pintos", VERBOSE => 1);
111 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
114 # Create output directory, if it doesn't already exist.
115 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
117 # Extract submission.
118 extract_tarball () if ! -d "pintos";
120 # Compile submission.
123 # Verify that the proper directory was submitted.
124 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
126 # Run and grade the tests.
133 my ($result) = run_test ($test);
134 if ($result eq 'ok') {
135 $result = grade_test ($test);
139 print " - with warnings" if $result eq 'ok' && defined $details{$test};
142 $result{$test} = $result;
151 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
153 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
155 # Sort tarballs in reverse order by time.
156 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
158 print "Multiple tarballs: choosing $tarballs[0]\n"
159 if scalar (@tarballs) > 1;
163 sub extract_tarball {
164 my ($tarball) = choose_tarball ();
166 mkdir "pintos" or die "pintos: mkdir: $!\n";
167 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
169 print "Extracting $tarball...\n";
170 xsystem ("cd pintos/src && tar xzf ../../$tarball",
171 DIE => "extraction failed\n");
174 print "Running fixme.sh...\n";
175 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
178 print "Patching...\n";
179 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
181 DIE => "patch failed\n");
182 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
183 . "< $GRADES_DIR/random.diff",
185 DIE => "patch failed\n");
187 open (CONSTANTS, ">pintos/src/constants.h")
188 or die "constants.h: create: $!\n";
189 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
195 my ($ms, $d, $y, $H, $M, $S) =
196 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
198 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
200 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
205 my ($src) = "$GRADES_DIR/$test.c";
206 -e $src or die "$src: stat: $!\n";
218 # Reuse older results if any
219 if (open (DONE, "<output/$test/done")) {
227 # Really run the test.
228 my ($status) = really_run_test ($test);
230 # Save the results for later.
231 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
232 print DONE "$status\n";
239 print "Compiling...\n";
240 xsystem ("cd pintos/src/vm && make", LOG => "make")
241 or return "compile error";
244 sub really_run_test {
246 # If there's residue from an earlier test, move it to .old.
247 # If there's already a .old, delete it.
248 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
249 rename "output/$test", "output/$test.old" or die "rename: $!\n"
250 if -d "output/$test";
252 # Make output directory.
253 mkdir "output/$test";
254 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
255 DIE => "cp failed\n");
256 xsystem ("pintos make-disk output/$test/swap.dsk 2 >/dev/null 2>&1",
257 DIE => "failed to create swap disk");
261 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
263 . "--os-disk=pintos/src/vm/build/os.dsk "
264 . "--fs-disk=output/$test/fs.dsk "
265 . "--swap-disk=output/$test/swap.dsk "
266 . "-v run -q -ex \"$test$testargs\"",
269 or return "Bochs error";
277 my (@output) = snarf ("output/$test/run.out");
279 my ($grade_func) = "grade_$test";
280 $grade_func =~ s/-/_/g;
281 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
283 verify_common (@output);
284 compare_output ("$GRADES_DIR/$test.exp", @output);
287 eval "$grade_func (\@output)";
290 die $@ if $@ =~ /at \S+ line \d+$/;
296 sub grade_write_normal {
298 verify_common (@output);
299 compare_output ("$GRADES_DIR/write-normal.exp", @output);
300 my ($test_txt) = "output/$test/test.txt";
301 get_file ("test.txt", $test_txt) if ! -e $test_txt;
303 my (@actual) = snarf ($test_txt);
304 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
307 if ($#actual == $#expected) {
309 for my $i (0...$#actual) {
310 $eq = 0 if $actual[$i] ne $expected[$i];
317 $details = "Expected file content:\n";
318 $details .= join ('', map (" $_\n", @expected));
319 $details .= "Actual file content:\n";
320 $details .= join ('', map (" $_\n", @actual));
321 $extra{$test} = $details;
323 die "File written didn't have expected content.\n";
327 sub grade_multi_oom {
329 verify_common (@output);
331 @output = fix_exit_codes (get_core_output (@output));
333 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
334 die "Child process $m started out of order.\n" if $m != $n;
338 die "Only $n child process(es) started.\n" if $n < 15;
340 # There could be a death notice for a process that didn't get
341 # fully loaded, and/or notices from the loader.
343 && ($output[0] =~ /^multi-oom: exit\(-1\)$/
344 || $output[0] =~ /^load: /)) {
349 die "Output ended unexpectedly before process $n finished.\n"
353 chomp ($_ = shift @output);
354 die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
355 die "Child process $n ended out of order.\n"
356 if !/^\(multi-oom\) end $n$/;
358 chomp ($_ = shift @output);
359 die "Kernel didn't print proper exit message for process $n.\n"
360 if !/^multi-oom: exit\($n\)$/;
362 die "Spurious output at end: '$output[0]'.\n" if @output;
366 my ($guest_fn, $host_fn) = @_;
368 . "--os-disk=pintos/src/vm/build/os.dsk "
369 . "--fs-disk=output/$test/fs.dsk "
370 . "-v get $guest_fn $host_fn",
371 LOG => "$test/get-$guest_fn",
373 or die "get $guest_fn failed\n";
380 my (@assertion) = grep (/PANIC/, @output);
381 if (@assertion != 0) {
382 my ($details) = "Kernel panic:\n $assertion[0]\n";
384 my (@stack_line) = grep (/Call stack:/, @output);
385 if (@stack_line != 0) {
386 $details .= " $stack_line[0]\n\n";
387 $details .= "Translation of backtrace:\n";
388 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
392 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
395 $A2L = "i386-elf-addr2line";
397 open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
399 my ($function, $line);
400 last unless defined ($function = <A2L>);
404 $details .= " $function ($line)\n";
408 if ($assertion[0] =~ /sec_no < d->capacity/) {
410 \nThis assertion commonly fails when accessing a file via
411 an inode that has been closed and freed. Freeing an inode
412 clears all its sector indexes to 0xcccccccc, which is not
413 a valid sector number for disks smaller than about 1.6 TB.
417 $extra{$test} = $details;
418 die "Kernel panic. Details at end of file.\n"
421 if (grep (/Pintos booting/, @output) > 1) {
424 $details = "Pintos spontaneously rebooted during this test.\n";
425 $details .= "This is most often due to unhandled page faults.\n";
426 $details .= "Here's the output from the initial boot through the\n";
427 $details .= "first reboot:\n\n";
433 last if /Pintos booting/ && ++$i > 1;
435 $details{$test} = $details;
436 die "Triple-fault caused spontaneous reboot(s). "
437 . "Details at end of file.\n";
440 die "No output at all\n" if @output == 0;
441 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
442 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
443 die "Didn't start up properly: no \"Boot complete\" startup message\n"
444 if !grep (/Boot complete/, @output);
445 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
446 if !grep (/Timer: \d+ ticks/, @output);
447 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
448 if !grep (/Powering off/, @output);
451 # Get @output without header or trailer.
452 sub get_core_output {
457 for ($first = 0; $first <= $#output; $first++) {
458 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
462 for ($last = $#output; $last >= 0; $last--) {
463 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
466 if ($last < $first) {
467 my ($no_first) = $first > $#output;
468 my ($no_last) = $last < $#output;
469 die "Couldn't locate output.\n";
472 return @output[$first ... $last];
478 # Remove lines that look like exit codes.
479 # Exit codes are supposed to be printed in the form "process: exit(code)"
480 # but people get unfortunately creative with it.
481 for (my ($i) = 0; $i <= $#output; $i++) {
482 local ($_) = $output[$i];
484 my ($process, $code);
485 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
486 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
487 || (($process, $code)
488 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
489 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
490 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
492 splice (@output, $i, 1);
501 my ($exp, @actual) = @_;
502 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
503 die "Program produced no output.\n" if !@actual;
506 $details .= "$test actual output:\n";
507 $details .= join ('', map (" $_", @actual));
509 my (@exp) = map ("$_\n", snarf ($exp));
511 my ($fuzzy_match) = 0;
515 my ($s) = shift (@exp);
516 last if $s eq "--OR--\n";
517 push (@expected, $s);
520 $details .= "\n$test acceptable output:\n";
521 $details .= join ('', map (" $_", @expected));
523 # Check whether they're the same.
524 if ($#actual == $#expected) {
526 for (my ($i) = 0; $i <= $#expected; $i++) {
527 $eq = 0 if $actual[$i] ne $expected[$i];
532 # They differ. Output a diff.
534 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
535 my ($not_fuzzy_match) = 0;
536 while ($d->Next ()) {
537 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
539 push (@diff, map (" $_", $d->Items (1)));
541 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
542 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
544 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
546 $not_fuzzy_match = 1;
550 $fuzzy_match = 1 if !$not_fuzzy_match;
552 $details .= "Differences in `diff -u' format:\n";
553 $details .= join ('', @diff);
554 $details .= "(This is considered a `fuzzy match'.)\n"
555 if !$not_fuzzy_match;
560 "This test passed, but with extra, unexpected output.\n"
561 . "Please inspect your code to make sure that it does not\n"
562 . "produce output other than as specified in the project\n"
567 "This test failed because its output did not match any\n"
568 . "of the acceptable form(s).\n\n"
572 $details{$test} = $details;
573 die "Output differs from expected. Details at end of file.\n"
578 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
584 for (my ($i) = 0; $i <= $#summary; $i++) {
585 local ($_) = $summary[$i];
586 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
587 my ($result) = $result{$test} || "Not tested.";
589 if ($result eq 'ok') {
590 if (!defined $details{$test}) {
591 # Test successful and no warnings.
592 splice (@summary, $i, 1);
595 # Test successful with warnings.
598 splice (@summary, $i + 1, 0,
599 " Test passed with warnings. "
600 . "Details at end of file.");
606 splice (@summary, $i + 1, 0,
607 map (" $_", split ("\n", $result)));
609 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
611 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
612 splice (@summary, $i, 0, " All tests passed.")
613 if $ploss == 0 && !$warnings;
619 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
620 $summary[0] =~ s/\[\[total\]\]/$ts/;
622 open (SUMMARY, ">tests.out");
623 print SUMMARY map ("$_\n", @summary);
628 open (DETAILS, ">details.out");
630 for my $test (@TESTS) {
631 next if $result{$test} eq 'ok' && !defined $details{$test};
633 my ($details) = $details{$test};
634 next if !defined ($details) && ! -e "output/$test/run.out";
637 if ($result{$test} ne 'ok') {
638 $banner = "$test failure details";
640 $banner = "$test warnings";
643 print DETAILS "\n" if $n++;
644 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
645 print DETAILS "\n\n";
647 if (!defined $details) {
648 my (@output) = snarf ("output/$test/run.out");
650 # Print only the first in a series of recursing panics.
652 for my $i (0...$#output) {
653 local ($_) = $output[$i];
654 if (/PANIC/ && $panic++ > 0) {
655 @output = @output[0...$i];
657 "[...details of recursive panic omitted...]");
661 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
663 print DETAILS $details;
665 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
666 if defined $extra{$test};
673 my ($command, %options) = @_;
674 print "$command\n" if $VERBOSE || $options{VERBOSE};
676 my ($log) = $options{LOG};
680 local $SIG{ALRM} = sub { die "alarm\n" };
681 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
683 die "fork: $!\n" if !defined $pid;
687 open (STDOUT, ">output/$log.out");
689 open (STDERR, ">output/$log.err");
699 die unless $@ eq "alarm\n"; # propagate unexpected errors
700 print "Timed out $pid.\n";
701 print "not killed\n" if !kill ('SIGTERM', $pid);
705 if (WIFSIGNALED ($status)) {
706 my ($signal) = WTERMSIG ($status);
707 die "Interrupted\n" if $signal == SIGINT;
708 print "Child terminated with signal $signal\n";
711 unlink ("output/$log.err") if defined ($log) && $status == 0;
713 die $options{DIE} if $status != 0 && defined $options{DIE};
720 open (OUTPUT, $file) or die "$file: open: $!\n";
721 my (@lines) = <OUTPUT>;
724 return wantarray ? @lines : join ('', map ("$_\n", @lines));
730 open (A, "<$a") or die "$a: open: $!\n";
731 open (B, "<$b") or die "$b: open: $!\n";
737 sysread (A, $sa, 1024);
738 sysread (B, $sb, 1024);
739 $equal = 0, last if $sa ne $sb;
740 $equal = 1, last if $sa eq '';
749 my ($file, $expected) = @_;
750 open (FILE, "<$file") or die "$file: open: $!\n";
752 sysread (FILE, $actual, -s FILE);
753 my ($equal) = $actual eq $expected;
759 my ($ln, $lines) = @_;
761 for my $line (@$lines) {
763 $out .= sprintf "%4d %s\n", $ln++, $line;