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 (sm-create sm-full sm-seq-block sm-seq-random sm-random
52 grow-create grow-seq-sm grow-seq-lg grow-file-size grow-tell
53 grow-sparse grow-too-big grow-root-sm grow-root-lg grow-dir-lg
56 dir-mkdir dir-rmdir dir-mk-vine dir-rm-vine dir-mk-tree
57 dir-rm-tree dir-lsdir dir-rm-cwd dir-rm-cwd-cd
58 dir-rm-parent dir-rm-root dir-over-file dir-under-file
59 dir-empty-name dir-open
66 # Handle final grade mode.
68 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
70 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
77 my (@tests) = snarf ("tests.out");
78 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
80 my (@review) = snarf ("review.txt");
81 my ($part_lost) = (0, 0);
82 for (my ($i) = $#review; $i >= 0; $i--) {
83 local ($_) = $review[$i];
84 if (my ($loss) = /^\s*([-+]\d+)/) {
86 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
87 my ($got) = $out_of + $part_lost;
89 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
96 die "Lost points outside a section\n" if $part_lost;
98 for (my ($i) = 1; $i <= $#review; $i++) {
99 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
100 $review[$i] = '-' x (length ($review[$i - 1]));
104 print OUT "\nOVERALL SCORE\n";
105 print OUT "-------------\n";
106 print OUT "$p_got points out of $p_pos total\n\n";
108 print OUT map ("$_\n", @tests), "\n";
109 print OUT map ("$_\n", @review), "\n";
111 print OUT "DETAILS\n";
112 print OUT "-------\n\n";
113 print OUT map ("$_\n", snarf ("details.out"));
119 # Verify that we're roughly in the correct directory
120 # before we go blasting away files.
123 xsystem ("rm -rf output pintos", VERBOSE => 1);
124 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
127 # Create output directory, if it doesn't already exist.
128 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
130 # Extract submission.
131 extract_tarball () if ! -d "pintos";
133 # Compile submission.
136 # Verify that the proper directory was submitted.
137 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
139 # Run and grade the tests.
146 my ($result) = run_test ($test);
147 if ($result eq 'ok') {
148 $result = grade_test ($test);
152 print " - with warnings" if $result eq 'ok' && defined $details{$test};
155 $result{$test} = $result;
164 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
166 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
168 # Sort tarballs in reverse order by time.
169 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
171 print "Multiple tarballs: choosing $tarballs[0]\n"
172 if scalar (@tarballs) > 1;
176 sub extract_tarball {
177 my ($tarball) = choose_tarball ();
179 mkdir "pintos" or die "pintos: mkdir: $!\n";
180 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
182 print "Extracting $tarball...\n";
183 xsystem ("cd pintos/src && tar xzf ../../$tarball",
184 DIE => "extraction failed\n");
187 print "Running fixme.sh...\n";
188 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
191 print "Patching...\n";
192 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
194 DIE => "patch failed\n");
196 open (CONSTANTS, ">pintos/src/constants.h")
197 or die "constants.h: create: $!\n";
198 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
204 my ($ms, $d, $y, $H, $M, $S) =
205 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
207 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
209 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
214 my ($src) = "$GRADES_DIR/$test.c";
215 -e $src or die "$src: stat: $!\n";
227 # Reuse older results if any
228 if (open (DONE, "<output/$test/done")) {
236 # Really run the test.
237 my ($status) = really_run_test ($test);
239 # Save the results for later.
240 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
241 print DONE "$status\n";
248 print "Compiling...\n";
249 xsystem ("cd pintos/src/vm && make", LOG => "make")
250 or return "compile error";
253 sub really_run_test {
255 # If there's residue from an earlier test, move it to .old.
256 # If there's already a .old, delete it.
257 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
258 rename "output/$test", "output/$test.old" or die "rename: $!\n"
259 if -d "output/$test";
261 # Make output directory.
262 mkdir "output/$test";
263 my ($fs_size) = $test ne 'grow-too-big' ? 2 : .25;
264 xsystem ("pintos make-disk output/$test/fs.dsk $fs_size >/dev/null 2>&1",
265 DIE => "failed to create file system disk");
266 xsystem ("pintos make-disk output/$test/swap.dsk 2 >/dev/null 2>&1",
267 DIE => "failed to create swap disk");
269 # Format disk, install test.
270 my ($pintos_base_cmd) =
272 . "--os-disk=pintos/src/vm/build/os.dsk "
273 . "--fs-disk=output/$test/fs.dsk "
274 . "--swap-disk=output/$test/swap.dsk "
276 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk"),
277 return "format/put error"
278 if !xsystem ("$pintos_base_cmd put -f $GRADES_DIR/$test $test",
279 LOG => "$test/put", TIMEOUT => 60, EXPECT => 1);
282 push (@extra_files, "child-syn-read") if $test eq 'syn-read';
283 for my $fn (@extra_files) {
284 return "format/put error"
285 if !xsystem ("$pintos_base_cmd put $GRADES_DIR/$fn $fn",
286 LOG => "$test/put-$fn", TIMEOUT => 60, EXPECT => 1);
291 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
293 xsystem ("$pintos_base_cmd run -q -ex \"$test$testargs\"",
294 LOG => "$test/run", TIMEOUT => $timeout, EXPECT => 1)
295 ? "ok" : "Bochs error";
296 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
303 my (@output) = snarf ("output/$test/run.out");
305 my ($grade_func) = "grade_$test";
306 $grade_func =~ s/-/_/g;
307 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
309 verify_common (@output);
310 compare_output ("$GRADES_DIR/$test.exp", @output);
313 eval "$grade_func (\@output)";
316 die $@ if $@ =~ /at \S+ line \d+$/;
322 sub grade_process_death {
323 my ($proc_name, @output) = @_;
325 verify_common (@output);
326 @output = get_core_output (@output);
327 die "First line of output is not `($proc_name) begin' message.\n"
328 if $output[0] ne "($proc_name) begin";
329 die "Output contains `FAIL' message.\n"
330 if grep (/FAIL/, @output);
331 die "Output contains spurious ($proc_name) message.\n"
332 if grep (/\($proc_name\)/, @output) > 1;
335 sub grade_pt_bad_addr {
336 grade_process_death ('pt-bad-addr', @_);
339 sub grade_pt_write_code {
340 grade_process_death ('pt-write-code', @_);
343 sub grade_mmap_unmap {
344 grade_process_death ('mmap-unmap', @_);
350 my (@assertion) = grep (/PANIC/, @output);
351 if (@assertion != 0) {
352 my ($details) = "Kernel panic:\n $assertion[0]\n";
354 my (@stack_line) = grep (/Call stack:/, @output);
355 if (@stack_line != 0) {
356 $details .= " $stack_line[0]\n\n";
357 $details .= "Translation of backtrace:\n";
358 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
362 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
365 $A2L = "i386-elf-addr2line";
367 open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
369 my ($function, $line);
370 last unless defined ($function = <A2L>);
374 $details .= " $function ($line)\n";
378 if ($assertion[0] =~ /sec_no < d->capacity/) {
380 \nThis assertion commonly fails when accessing a file via
381 an inode that has been closed and freed. Freeing an inode
382 clears all its sector indexes to 0xcccccccc, which is not
383 a valid sector number for disks smaller than about 1.6 TB.
387 $extra{$test} = $details;
388 die "Kernel panic. Details at end of file.\n"
391 if (grep (/Pintos booting/, @output) > 1) {
394 $details = "Pintos spontaneously rebooted during this test.\n";
395 $details .= "This is most often due to unhandled page faults.\n";
396 $details .= "Here's the output from the initial boot through the\n";
397 $details .= "first reboot:\n\n";
403 last if /Pintos booting/ && ++$i > 1;
405 $details{$test} = $details;
406 die "Triple-fault caused spontaneous reboot(s). "
407 . "Details at end of file.\n";
410 die "No output at all\n" if @output == 0;
411 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
412 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
413 die "Didn't start up properly: no \"Boot complete\" startup message\n"
414 if !grep (/Boot complete/, @output);
415 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
416 if !grep (/Timer: \d+ ticks/, @output);
417 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
418 if !grep (/Powering off/, @output);
421 # Get @output without header or trailer.
422 sub get_core_output {
427 for ($first = 0; $first <= $#output; $first++) {
428 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
432 for ($last = $#output; $last >= 0; $last--) {
433 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
436 if ($last < $first) {
437 my ($no_first) = $first > $#output;
438 my ($no_last) = $last < $#output;
439 die "Couldn't locate output.\n";
442 return @output[$first ... $last];
448 # Remove lines that look like exit codes.
449 # Exit codes are supposed to be printed in the form "process: exit(code)"
450 # but people get unfortunately creative with it.
451 for (my ($i) = 0; $i <= $#output; $i++) {
452 local ($_) = $output[$i];
454 my ($process, $code);
455 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
456 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
457 || (($process, $code)
458 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
459 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
460 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
462 splice (@output, $i, 1);
471 my ($exp, @actual) = @_;
472 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
473 die "Program produced no output.\n" if !@actual;
476 $details .= "$test actual output:\n";
477 $details .= join ('', map (" $_", @actual));
479 my (@exp) = map ("$_\n", snarf ($exp));
481 my ($fuzzy_match) = 0;
485 my ($s) = shift (@exp);
486 last if $s eq "--OR--\n";
487 push (@expected, $s);
490 $details .= "\n$test acceptable output:\n";
491 $details .= join ('', map (" $_", @expected));
493 # Check whether they're the same.
494 if ($#actual == $#expected) {
496 for (my ($i) = 0; $i <= $#expected; $i++) {
497 $eq = 0 if $actual[$i] ne $expected[$i];
502 # They differ. Output a diff.
504 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
505 my ($not_fuzzy_match) = 0;
506 while ($d->Next ()) {
507 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
509 push (@diff, map (" $_", $d->Items (1)));
511 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
512 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
514 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
516 $not_fuzzy_match = 1;
520 $fuzzy_match = 1 if !$not_fuzzy_match;
522 $details .= "Differences in `diff -u' format:\n";
523 $details .= join ('', @diff);
524 $details .= "(This is considered a `fuzzy match'.)\n"
525 if !$not_fuzzy_match;
530 "This test passed, but with extra, unexpected output.\n"
531 . "Please inspect your code to make sure that it does not\n"
532 . "produce output other than as specified in the project\n"
537 "This test failed because its output did not match any\n"
538 . "of the acceptable form(s).\n\n"
542 $details{$test} = $details;
543 die "Output differs from expected. Details at end of file.\n"
548 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
554 for (my ($i) = 0; $i <= $#summary; $i++) {
555 local ($_) = $summary[$i];
556 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
557 my ($result) = $result{$test} || "Not tested.";
559 if ($result eq 'ok') {
560 if (!defined $details{$test}) {
561 # Test successful and no warnings.
562 splice (@summary, $i, 1);
565 # Test successful with warnings.
568 splice (@summary, $i + 1, 0,
569 " Test passed with warnings. "
570 . "Details at end of file.");
576 splice (@summary, $i + 1, 0,
577 map (" $_", split ("\n", $result)));
579 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
581 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
582 splice (@summary, $i, 0, " All tests passed.")
583 if $ploss == 0 && !$warnings;
589 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
590 $summary[0] =~ s/\[\[total\]\]/$ts/;
592 open (SUMMARY, ">tests.out");
593 print SUMMARY map ("$_\n", @summary);
598 open (DETAILS, ">details.out");
600 for my $test (@TESTS) {
601 next if $result{$test} eq 'ok' && !defined $details{$test};
603 my ($details) = $details{$test};
604 next if !defined ($details) && ! -e "output/$test/run.out";
607 if ($result{$test} ne 'ok') {
608 $banner = "$test failure details";
610 $banner = "$test warnings";
613 print DETAILS "\n" if $n++;
614 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
615 print DETAILS "\n\n";
617 if (!defined $details) {
618 my (@output) = snarf ("output/$test/run.out");
620 # Print only the first in a series of recursing panics.
622 for my $i (0...$#output) {
623 local ($_) = $output[$i];
624 if (/PANIC/ && $panic++ > 0) {
625 @output = @output[0...$i];
627 "[...details of recursive panic omitted...]");
631 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
633 print DETAILS $details;
635 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
636 if defined $extra{$test};
643 my ($command, %options) = @_;
644 print "$command\n" if $verbose || $options{VERBOSE};
646 my ($log) = $options{LOG};
650 local $SIG{ALRM} = sub { die "alarm\n" };
651 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
653 die "fork: $!\n" if !defined $pid;
656 open (STDOUT, ">output/$log.out");
657 open (STDERR, ">output/$log.err");
669 die unless $@ eq "alarm\n"; # propagate unexpected errors
671 for (my ($i) = 0; $i < 10; $i++) {
672 kill ('SIGTERM', $pid);
674 my ($retval) = waitpid ($pid, WNOHANG);
675 last if $retval == $pid || $retval == -1;
676 print "Waiting for $pid to die" if $i == 0;
681 if (WIFSIGNALED ($status)) {
682 my ($signal) = WTERMSIG ($status);
683 die "Interrupted\n" if $signal == SIGINT;
684 print "Child terminated with signal $signal\n";
687 my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
688 $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
692 if (!$ok && defined $options{DIE}) {
693 my ($msg) = $options{DIE};
694 if (defined ($log)) {
696 $msg .= "; see output/$log.err and output/$log.out for details\n";
699 } elsif (defined ($log) && $ok) {
700 unlink ("output/$log.err");
708 open (OUTPUT, $file) or die "$file: open: $!\n";
709 my (@lines) = <OUTPUT>;
712 return wantarray ? @lines : join ('', map ("$_\n", @lines));
718 open (A, "<$a") or die "$a: open: $!\n";
719 open (B, "<$b") or die "$b: open: $!\n";
725 sysread (A, $sa, 1024);
726 sysread (B, $sb, 1024);
727 $equal = 0, last if $sa ne $sb;
728 $equal = 1, last if $sa eq '';
737 my ($file, $expected) = @_;
738 open (FILE, "<$file") or die "$file: open: $!\n";
740 sysread (FILE, $actual, -s FILE);
741 my ($equal) = $actual eq $expected;
747 my ($ln, $lines) = @_;
749 for my $line (@$lines) {
751 $out .= sprintf "%4d %s\n", $ln++, $line;