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
61 syn-remove syn-read syn-write syn-rw
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/filesys && 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/filesys/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 push (@extra_files, "child-syn-wrt") if $test eq 'syn-write';
284 push (@extra_files, "child-syn-rw") if $test eq 'syn-rw';
285 for my $fn (@extra_files) {
286 return "format/put error"
287 if !xsystem ("$pintos_base_cmd put $GRADES_DIR/$fn $fn",
288 LOG => "$test/put-$fn", TIMEOUT => 60, EXPECT => 1);
293 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
295 xsystem ("$pintos_base_cmd run -q -ex \"$test$testargs\"",
296 LOG => "$test/run", TIMEOUT => $timeout, EXPECT => 1)
297 ? "ok" : "Bochs error";
298 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
305 my (@output) = snarf ("output/$test/run.out");
307 my ($grade_func) = "grade_$test";
308 $grade_func =~ s/-/_/g;
309 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
311 verify_common (@output);
312 compare_output ("$GRADES_DIR/$test.exp", @output);
315 eval "$grade_func (\@output)";
318 die $@ if $@ =~ /at \S+ line \d+$/;
324 sub grade_process_death {
325 my ($proc_name, @output) = @_;
327 verify_common (@output);
328 @output = get_core_output (@output);
329 die "First line of output is not `($proc_name) begin' message.\n"
330 if $output[0] ne "($proc_name) begin";
331 die "Output contains `FAIL' message.\n"
332 if grep (/FAIL/, @output);
333 die "Output contains spurious ($proc_name) message.\n"
334 if grep (/\($proc_name\)/, @output) > 1;
337 sub grade_pt_bad_addr {
338 grade_process_death ('pt-bad-addr', @_);
341 sub grade_pt_write_code {
342 grade_process_death ('pt-write-code', @_);
345 sub grade_mmap_unmap {
346 grade_process_death ('mmap-unmap', @_);
352 my (@assertion) = grep (/PANIC/, @output);
353 if (@assertion != 0) {
354 my ($details) = "Kernel panic:\n $assertion[0]\n";
356 my (@stack_line) = grep (/Call stack:/, @output);
357 if (@stack_line != 0) {
358 $details .= " $stack_line[0]\n\n";
359 $details .= "Translation of backtrace:\n";
360 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
364 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
367 $A2L = "i386-elf-addr2line";
369 open (A2L, "$A2L -fe pintos/src/filesys/build/kernel.o @addrs|");
371 my ($function, $line);
372 last unless defined ($function = <A2L>);
376 $details .= " $function ($line)\n";
380 if ($assertion[0] =~ /sec_no < d->capacity/) {
382 \nThis assertion commonly fails when accessing a file via
383 an inode that has been closed and freed. Freeing an inode
384 clears all its sector indexes to 0xcccccccc, which is not
385 a valid sector number for disks smaller than about 1.6 TB.
389 $extra{$test} = $details;
390 die "Kernel panic. Details at end of file.\n"
393 my (@failure) = grep (/FAIL/, @output);
395 die "Test failed: \"$failure[0]\"\n";
398 if (grep (/Pintos booting/, @output) > 1) {
401 $details = "Pintos spontaneously rebooted during this test.\n";
402 $details .= "This is most often due to unhandled page faults.\n";
403 $details .= "Here's the output from the initial boot through the\n";
404 $details .= "first reboot:\n\n";
410 last if /Pintos booting/ && ++$i > 1;
412 $details{$test} = $details;
413 die "Triple-fault caused spontaneous reboot(s). "
414 . "Details at end of file.\n";
417 die "No output at all\n" if @output == 0;
418 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
419 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
420 die "Didn't start up properly: no \"Boot complete\" startup message\n"
421 if !grep (/Boot complete/, @output);
422 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
423 if !grep (/Timer: \d+ ticks/, @output);
424 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
425 if !grep (/Powering off/, @output);
428 # Get @output without header or trailer.
429 sub get_core_output {
434 for ($first = 0; $first <= $#output; $first++) {
435 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
439 for ($last = $#output; $last >= 0; $last--) {
440 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
443 if ($last < $first) {
444 my ($no_first) = $first > $#output;
445 my ($no_last) = $last < $#output;
446 die "Couldn't locate output.\n";
449 return @output[$first ... $last];
455 # Remove lines that look like exit codes.
456 # Exit codes are supposed to be printed in the form "process: exit(code)"
457 # but people get unfortunately creative with it.
458 for (my ($i) = 0; $i <= $#output; $i++) {
459 local ($_) = $output[$i];
461 my ($process, $code);
462 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
463 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
464 || (($process, $code)
465 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
466 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
467 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
469 splice (@output, $i, 1);
478 my ($exp, @actual) = @_;
479 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
480 die "Program produced no output.\n" if !@actual;
483 $details .= "$test actual output:\n";
484 $details .= join ('', map (" $_", @actual));
486 my (@exp) = map ("$_\n", snarf ($exp));
488 my ($fuzzy_match) = 0;
492 my ($s) = shift (@exp);
493 last if $s eq "--OR--\n";
494 push (@expected, $s);
497 $details .= "\n$test acceptable output:\n";
498 $details .= join ('', map (" $_", @expected));
500 # Check whether they're the same.
501 if ($#actual == $#expected) {
503 for (my ($i) = 0; $i <= $#expected; $i++) {
504 $eq = 0 if $actual[$i] ne $expected[$i];
509 # They differ. Output a diff.
511 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
512 my ($not_fuzzy_match) = 0;
513 while ($d->Next ()) {
514 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
516 push (@diff, map (" $_", $d->Items (1)));
518 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
519 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
521 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
523 $not_fuzzy_match = 1;
527 $fuzzy_match = 1 if !$not_fuzzy_match;
529 $details .= "Differences in `diff -u' format:\n";
530 $details .= join ('', @diff);
531 $details .= "(This is considered a `fuzzy match'.)\n"
532 if !$not_fuzzy_match;
537 "This test passed, but with extra, unexpected output.\n"
538 . "Please inspect your code to make sure that it does not\n"
539 . "produce output other than as specified in the project\n"
544 "This test failed because its output did not match any\n"
545 . "of the acceptable form(s).\n\n"
549 $details{$test} = $details;
550 die "Output differs from expected. Details at end of file.\n"
555 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
561 for (my ($i) = 0; $i <= $#summary; $i++) {
562 local ($_) = $summary[$i];
563 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
564 my ($result) = $result{$test} || "Not tested.";
566 if ($result eq 'ok') {
567 if (!defined $details{$test}) {
568 # Test successful and no warnings.
569 splice (@summary, $i, 1);
572 # Test successful with warnings.
575 splice (@summary, $i + 1, 0,
576 " Test passed with warnings. "
577 . "Details at end of file.");
583 splice (@summary, $i + 1, 0,
584 map (" $_", split ("\n", $result)));
586 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
588 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
589 splice (@summary, $i, 0, " All tests passed.")
590 if $ploss == 0 && !$warnings;
596 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
597 $summary[0] =~ s/\[\[total\]\]/$ts/;
599 open (SUMMARY, ">tests.out");
600 print SUMMARY map ("$_\n", @summary);
605 open (DETAILS, ">details.out");
607 for my $test (@TESTS) {
608 next if $result{$test} eq 'ok' && !defined $details{$test};
610 my ($details) = $details{$test};
611 next if !defined ($details) && ! -e "output/$test/run.out";
614 if ($result{$test} ne 'ok') {
615 $banner = "$test failure details";
617 $banner = "$test warnings";
620 print DETAILS "\n" if $n++;
621 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
622 print DETAILS "\n\n";
624 if (!defined $details) {
625 my (@output) = snarf ("output/$test/run.out");
627 # Print only the first in a series of recursing panics.
629 for my $i (0...$#output) {
630 local ($_) = $output[$i];
631 if (/PANIC/ && $panic++ > 0) {
632 @output = @output[0...$i];
634 "[...details of recursive panic omitted...]");
638 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
640 print DETAILS $details;
642 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
643 if defined $extra{$test};
650 my ($command, %options) = @_;
651 print "$command\n" if $verbose || $options{VERBOSE};
653 my ($log) = $options{LOG};
657 local $SIG{ALRM} = sub { die "alarm\n" };
658 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
660 die "fork: $!\n" if !defined $pid;
663 open (STDOUT, ">output/$log.out");
664 open (STDERR, ">output/$log.err");
676 die unless $@ eq "alarm\n"; # propagate unexpected errors
678 for (my ($i) = 0; $i < 10; $i++) {
679 kill ('SIGTERM', $pid);
681 my ($retval) = waitpid ($pid, WNOHANG);
682 last if $retval == $pid || $retval == -1;
683 print "Waiting for $pid to die" if $i == 0;
688 if (WIFSIGNALED ($status)) {
689 my ($signal) = WTERMSIG ($status);
690 die "Interrupted\n" if $signal == SIGINT;
691 print "Child terminated with signal $signal\n";
694 my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
695 $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
699 if (!$ok && defined $options{DIE}) {
700 my ($msg) = $options{DIE};
701 if (defined ($log)) {
703 $msg .= "; see output/$log.err and output/$log.out for details\n";
706 } elsif (defined ($log) && $ok) {
707 unlink ("output/$log.err");
715 open (OUTPUT, $file) or die "$file: open: $!\n";
716 my (@lines) = <OUTPUT>;
719 return wantarray ? @lines : join ('', map ("$_\n", @lines));
725 open (A, "<$a") or die "$a: open: $!\n";
726 open (B, "<$b") or die "$b: open: $!\n";
732 sysread (A, $sa, 1024);
733 sysread (B, $sb, 1024);
734 $equal = 0, last if $sa ne $sb;
735 $equal = 1, last if $sa eq '';
744 my ($file, $expected) = @_;
745 open (FILE, "<$file") or die "$file: open: $!\n";
747 sysread (FILE, $actual, -s FILE);
748 my ($equal) = $actual eq $expected;
754 my ($ln, $lines) = @_;
756 for my $line (@$lines) {
758 $out .= sprintf "%4d %s\n", $ln++, $line;