3 # Find the directory that contains the grading files.
7 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
8 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
9 unshift @INC, "$GRADES_DIR/../lib";
18 our ($VERBOSE) = 0; # Verbosity of output
19 our (@TESTS); # Tests to run.
23 GetOptions ("v|verbose+" => \$VERBOSE,
24 "h|help" => sub { usage (0) },
25 "t|test=s" => \@TESTS,
28 or die "Malformed command line; use --help for help.\n";
29 die "Non-option argument not supported; use --help for help.\n"
34 print "run-tests, for grading Pintos multiprogramming projects.\n\n";
35 print "Invoke from a directory containing a student tarball named by\n";
36 print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
37 print "In normal usage, no options are needed.\n\n";
38 print "Output is produced in tests.out and details.out.\n\n";
40 print " -c, --clean Remove old output files before starting\n";
41 print " -t, --test=TEST Execute TEST only (allowed multiple times)\n";
42 print " -g, --grade Instead of running tests, compose grade.out\n";
43 print " -v, --verbose Print commands before executing them\n";
44 print " -h, --help Print this help message\n";
48 # Default set of tests.
49 @TESTS = qw (pt-grow-stack pt-big-stk-obj pt-bad-addr pt-write-code
50 page-linear page-parallel page-merge-seq page-merge-par
51 page-shuffle mmap-read mmap-close mmap-unmap mmap-overlap
52 mmap-twice mmap-write mmap-exit mmap-shuffle
57 # Handle final grade mode.
59 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
61 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
68 my (@tests) = snarf ("tests.out");
69 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
71 my (@review) = snarf ("review.txt");
72 my ($part_lost) = (0, 0);
73 for (my ($i) = $#review; $i >= 0; $i--) {
74 local ($_) = $review[$i];
75 if (my ($loss) = /^\s*([-+]\d+)/) {
77 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
78 my ($got) = $out_of + $part_lost;
80 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
87 die "Lost points outside a section\n" if $part_lost;
89 for (my ($i) = 1; $i <= $#review; $i++) {
90 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
91 $review[$i] = '-' x (length ($review[$i - 1]));
95 print OUT "\nOVERALL SCORE\n";
96 print OUT "-------------\n";
97 print OUT "$p_got points out of $p_pos total\n\n";
99 print OUT map ("$_\n", @tests), "\n";
100 print OUT map ("$_\n", @review), "\n";
102 print OUT "DETAILS\n";
103 print OUT "-------\n\n";
104 print OUT map ("$_\n", snarf ("details.out"));
110 # Verify that we're roughly in the correct directory
111 # before we go blasting away files.
114 xsystem ("rm -rf output pintos", VERBOSE => 1);
115 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
118 # Create output directory, if it doesn't already exist.
119 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
121 # Extract submission.
122 extract_tarball () if ! -d "pintos";
124 # Compile submission.
127 # Verify that the proper directory was submitted.
128 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
130 # Run and grade the tests.
137 my ($result) = run_test ($test);
138 if ($result eq 'ok') {
139 $result = grade_test ($test);
143 print " - with warnings" if $result eq 'ok' && defined $details{$test};
146 $result{$test} = $result;
155 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
157 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
159 # Sort tarballs in reverse order by time.
160 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
162 print "Multiple tarballs: choosing $tarballs[0]\n"
163 if scalar (@tarballs) > 1;
167 sub extract_tarball {
168 my ($tarball) = choose_tarball ();
170 mkdir "pintos" or die "pintos: mkdir: $!\n";
171 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
173 print "Extracting $tarball...\n";
174 xsystem ("cd pintos/src && tar xzf ../../$tarball",
175 DIE => "extraction failed\n");
178 print "Running fixme.sh...\n";
179 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
182 print "Patching...\n";
183 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
185 DIE => "patch failed\n");
186 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
187 . "< $GRADES_DIR/random.diff",
189 DIE => "patch failed\n");
191 open (CONSTANTS, ">pintos/src/constants.h")
192 or die "constants.h: create: $!\n";
193 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
199 my ($ms, $d, $y, $H, $M, $S) =
200 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
202 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
204 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
209 my ($src) = "$GRADES_DIR/$test.c";
210 -e $src or die "$src: stat: $!\n";
222 # Reuse older results if any
223 if (open (DONE, "<output/$test/done")) {
231 # Really run the test.
232 my ($status) = really_run_test ($test);
234 # Save the results for later.
235 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
236 print DONE "$status\n";
243 print "Compiling...\n";
244 xsystem ("cd pintos/src/vm && make", LOG => "make")
245 or return "compile error";
248 sub really_run_test {
250 # If there's residue from an earlier test, move it to .old.
251 # If there's already a .old, delete it.
252 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
253 rename "output/$test", "output/$test.old" or die "rename: $!\n"
254 if -d "output/$test";
256 # Make output directory.
257 mkdir "output/$test";
258 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
259 DIE => "cp failed\n");
260 xsystem ("pintos make-disk output/$test/swap.dsk 2 >/dev/null 2>&1",
261 DIE => "failed to create swap disk");
265 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
266 my ($result) = xsystem ("pintos "
267 . "--os-disk=pintos/src/vm/build/os.dsk "
268 . "--fs-disk=output/$test/fs.dsk "
269 . "--swap-disk=output/$test/swap.dsk "
270 . "-v run -q -ex \"$test$testargs\"",
274 ? "ok" : "Bochs error";
275 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
282 my (@output) = snarf ("output/$test/run.out");
284 my ($grade_func) = "grade_$test";
285 $grade_func =~ s/-/_/g;
286 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
288 verify_common (@output);
289 compare_output ("$GRADES_DIR/$test.exp", @output);
292 eval "$grade_func (\@output)";
295 die $@ if $@ =~ /at \S+ line \d+$/;
301 sub grade_process_death {
302 my ($proc_name, @output) = @_;
304 verify_common (@output);
305 @output = get_core_output (@output);
306 die "First line of output is not `($proc_name) begin' message.\n"
307 if $output[0] ne "($proc_name) begin";
308 die "Output contains `FAIL' message.\n"
309 if grep (/FAIL/, @output);
310 die "Output contains spurious ($proc_name) message.\n"
311 if grep (/\($proc_name\)/, @output) > 1;
314 sub grade_pt_bad_addr {
315 grade_process_death ('pt-bad-addr', @_);
318 sub grade_pt_write_code {
319 grade_process_death ('pt-write-code', @_);
322 sub grade_mmap_unmap {
323 grade_process_death ('mmap-unmap', @_);
329 my (@assertion) = grep (/PANIC/, @output);
330 if (@assertion != 0) {
331 my ($details) = "Kernel panic:\n $assertion[0]\n";
333 my (@stack_line) = grep (/Call stack:/, @output);
334 if (@stack_line != 0) {
335 $details .= " $stack_line[0]\n\n";
336 $details .= "Translation of backtrace:\n";
337 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
341 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
344 $A2L = "i386-elf-addr2line";
346 open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
348 my ($function, $line);
349 last unless defined ($function = <A2L>);
353 $details .= " $function ($line)\n";
357 if ($assertion[0] =~ /sec_no < d->capacity/) {
359 \nThis assertion commonly fails when accessing a file via
360 an inode that has been closed and freed. Freeing an inode
361 clears all its sector indexes to 0xcccccccc, which is not
362 a valid sector number for disks smaller than about 1.6 TB.
366 $extra{$test} = $details;
367 die "Kernel panic. Details at end of file.\n"
370 if (grep (/Pintos booting/, @output) > 1) {
373 $details = "Pintos spontaneously rebooted during this test.\n";
374 $details .= "This is most often due to unhandled page faults.\n";
375 $details .= "Here's the output from the initial boot through the\n";
376 $details .= "first reboot:\n\n";
382 last if /Pintos booting/ && ++$i > 1;
384 $details{$test} = $details;
385 die "Triple-fault caused spontaneous reboot(s). "
386 . "Details at end of file.\n";
389 die "No output at all\n" if @output == 0;
390 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
391 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
392 die "Didn't start up properly: no \"Boot complete\" startup message\n"
393 if !grep (/Boot complete/, @output);
394 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
395 if !grep (/Timer: \d+ ticks/, @output);
396 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
397 if !grep (/Powering off/, @output);
400 # Get @output without header or trailer.
401 sub get_core_output {
406 for ($first = 0; $first <= $#output; $first++) {
407 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
411 for ($last = $#output; $last >= 0; $last--) {
412 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
415 if ($last < $first) {
416 my ($no_first) = $first > $#output;
417 my ($no_last) = $last < $#output;
418 die "Couldn't locate output.\n";
421 return @output[$first ... $last];
427 # Remove lines that look like exit codes.
428 # Exit codes are supposed to be printed in the form "process: exit(code)"
429 # but people get unfortunately creative with it.
430 for (my ($i) = 0; $i <= $#output; $i++) {
431 local ($_) = $output[$i];
433 my ($process, $code);
434 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
435 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
436 || (($process, $code)
437 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
438 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
439 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
441 splice (@output, $i, 1);
450 my ($exp, @actual) = @_;
451 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
452 die "Program produced no output.\n" if !@actual;
455 $details .= "$test actual output:\n";
456 $details .= join ('', map (" $_", @actual));
458 my (@exp) = map ("$_\n", snarf ($exp));
460 my ($fuzzy_match) = 0;
464 my ($s) = shift (@exp);
465 last if $s eq "--OR--\n";
466 push (@expected, $s);
469 $details .= "\n$test acceptable output:\n";
470 $details .= join ('', map (" $_", @expected));
472 # Check whether they're the same.
473 if ($#actual == $#expected) {
475 for (my ($i) = 0; $i <= $#expected; $i++) {
476 $eq = 0 if $actual[$i] ne $expected[$i];
481 # They differ. Output a diff.
483 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
484 my ($not_fuzzy_match) = 0;
485 while ($d->Next ()) {
486 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
488 push (@diff, map (" $_", $d->Items (1)));
490 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
491 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
493 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
495 $not_fuzzy_match = 1;
499 $fuzzy_match = 1 if !$not_fuzzy_match;
501 $details .= "Differences in `diff -u' format:\n";
502 $details .= join ('', @diff);
503 $details .= "(This is considered a `fuzzy match'.)\n"
504 if !$not_fuzzy_match;
509 "This test passed, but with extra, unexpected output.\n"
510 . "Please inspect your code to make sure that it does not\n"
511 . "produce output other than as specified in the project\n"
516 "This test failed because its output did not match any\n"
517 . "of the acceptable form(s).\n\n"
521 $details{$test} = $details;
522 die "Output differs from expected. Details at end of file.\n"
527 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
533 for (my ($i) = 0; $i <= $#summary; $i++) {
534 local ($_) = $summary[$i];
535 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
536 my ($result) = $result{$test} || "Not tested.";
538 if ($result eq 'ok') {
539 if (!defined $details{$test}) {
540 # Test successful and no warnings.
541 splice (@summary, $i, 1);
544 # Test successful with warnings.
547 splice (@summary, $i + 1, 0,
548 " Test passed with warnings. "
549 . "Details at end of file.");
555 splice (@summary, $i + 1, 0,
556 map (" $_", split ("\n", $result)));
558 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
560 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
561 splice (@summary, $i, 0, " All tests passed.")
562 if $ploss == 0 && !$warnings;
568 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
569 $summary[0] =~ s/\[\[total\]\]/$ts/;
571 open (SUMMARY, ">tests.out");
572 print SUMMARY map ("$_\n", @summary);
577 open (DETAILS, ">details.out");
579 for my $test (@TESTS) {
580 next if $result{$test} eq 'ok' && !defined $details{$test};
582 my ($details) = $details{$test};
583 next if !defined ($details) && ! -e "output/$test/run.out";
586 if ($result{$test} ne 'ok') {
587 $banner = "$test failure details";
589 $banner = "$test warnings";
592 print DETAILS "\n" if $n++;
593 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
594 print DETAILS "\n\n";
596 if (!defined $details) {
597 my (@output) = snarf ("output/$test/run.out");
599 # Print only the first in a series of recursing panics.
601 for my $i (0...$#output) {
602 local ($_) = $output[$i];
603 if (/PANIC/ && $panic++ > 0) {
604 @output = @output[0...$i];
606 "[...details of recursive panic omitted...]");
610 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
612 print DETAILS $details;
614 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
615 if defined $extra{$test};
622 my ($command, %options) = @_;
623 print "$command\n" if $VERBOSE || $options{VERBOSE};
625 my ($log) = $options{LOG};
629 local $SIG{ALRM} = sub { die "alarm\n" };
630 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
632 die "fork: $!\n" if !defined $pid;
635 open (STDOUT, ">output/$log.out");
636 open (STDERR, ">output/$log.err");
648 die unless $@ eq "alarm\n"; # propagate unexpected errors
650 for (my ($i) = 0; $i < 10; $i++) {
651 kill ('SIGTERM', $pid);
653 my ($retval) = waitpid ($pid, WNOHANG);
654 last if $retval == $pid || $retval == -1;
655 print "Waiting for $pid to die" if $i == 0;
660 if (WIFSIGNALED ($status)) {
661 my ($signal) = WTERMSIG ($status);
662 die "Interrupted\n" if $signal == SIGINT;
663 print "Child terminated with signal $signal\n";
666 my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
667 $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
670 unlink ("output/$log.err") if defined ($log) && $ok;
672 die $options{DIE} if !$ok && defined $options{DIE};
679 open (OUTPUT, $file) or die "$file: open: $!\n";
680 my (@lines) = <OUTPUT>;
683 return wantarray ? @lines : join ('', map ("$_\n", @lines));
689 open (A, "<$a") or die "$a: open: $!\n";
690 open (B, "<$b") or die "$b: open: $!\n";
696 sysread (A, $sa, 1024);
697 sysread (B, $sb, 1024);
698 $equal = 0, last if $sa ne $sb;
699 $equal = 1, last if $sa eq '';
708 my ($file, $expected) = @_;
709 open (FILE, "<$file") or die "$file: open: $!\n";
711 sysread (FILE, $actual, -s FILE);
712 my ($equal) = $actual eq $expected;
718 my ($ln, $lines) = @_;
720 for my $line (@$lines) {
722 $out .= sprintf "%4d %s\n", $ln++, $line;