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 (pt-grow-stack pt-big-stk-obj pt-bad-addr pt-write-code
51 page-linear page-parallel page-merge-seq page-merge-par
52 page-shuffle mmap-read mmap-close mmap-unmap mmap-overlap
53 mmap-twice mmap-write mmap-exit mmap-shuffle
58 # Handle final grade mode.
60 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
62 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
69 my (@tests) = snarf ("tests.out");
70 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
72 my (@review) = snarf ("review.txt");
73 my ($part_lost) = (0, 0);
74 for (my ($i) = $#review; $i >= 0; $i--) {
75 local ($_) = $review[$i];
76 if (my ($loss) = /^\s*([-+]\d+)/) {
78 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
79 my ($got) = $out_of + $part_lost;
81 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
88 die "Lost points outside a section\n" if $part_lost;
90 for (my ($i) = 1; $i <= $#review; $i++) {
91 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
92 $review[$i] = '-' x (length ($review[$i - 1]));
96 print OUT "\nOVERALL SCORE\n";
97 print OUT "-------------\n";
98 print OUT "$p_got points out of $p_pos total\n\n";
100 print OUT map ("$_\n", @tests), "\n";
101 print OUT map ("$_\n", @review), "\n";
103 print OUT "DETAILS\n";
104 print OUT "-------\n\n";
105 print OUT map ("$_\n", snarf ("details.out"));
111 # Verify that we're roughly in the correct directory
112 # before we go blasting away files.
115 xsystem ("rm -rf output pintos", VERBOSE => 1);
116 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
119 # Create output directory, if it doesn't already exist.
120 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
122 # Extract submission.
123 extract_tarball () if ! -d "pintos";
125 # Compile submission.
128 # Verify that the proper directory was submitted.
129 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
131 # Run and grade the tests.
138 my ($result) = run_test ($test);
139 if ($result eq 'ok') {
140 $result = grade_test ($test);
144 print " - with warnings" if $result eq 'ok' && defined $details{$test};
147 $result{$test} = $result;
156 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
158 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
160 # Sort tarballs in reverse order by time.
161 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
163 print "Multiple tarballs: choosing $tarballs[0]\n"
164 if scalar (@tarballs) > 1;
168 sub extract_tarball {
169 my ($tarball) = choose_tarball ();
171 mkdir "pintos" or die "pintos: mkdir: $!\n";
172 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
174 print "Extracting $tarball...\n";
175 xsystem ("cd pintos/src && tar xzf ../../$tarball",
176 DIE => "extraction failed\n");
179 print "Running fixme.sh...\n";
180 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
183 print "Patching...\n";
184 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
186 DIE => "patch failed\n");
187 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
188 . "< $GRADES_DIR/random.diff",
190 DIE => "patch failed\n");
192 open (CONSTANTS, ">pintos/src/constants.h")
193 or die "constants.h: create: $!\n";
194 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
200 my ($ms, $d, $y, $H, $M, $S) =
201 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
203 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
205 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
210 my ($src) = "$GRADES_DIR/$test.c";
211 -e $src or die "$src: stat: $!\n";
223 # Reuse older results if any
224 if (open (DONE, "<output/$test/done")) {
232 # Really run the test.
233 my ($status) = really_run_test ($test);
235 # Save the results for later.
236 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
237 print DONE "$status\n";
244 print "Compiling...\n";
245 xsystem ("cd pintos/src/vm && make", LOG => "make")
246 or return "compile error";
249 sub really_run_test {
251 # If there's residue from an earlier test, move it to .old.
252 # If there's already a .old, delete it.
253 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
254 rename "output/$test", "output/$test.old" or die "rename: $!\n"
255 if -d "output/$test";
257 # Make output directory.
258 mkdir "output/$test";
259 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
260 DIE => "cp failed\n");
261 xsystem ("pintos make-disk output/$test/swap.dsk 2 >/dev/null 2>&1",
262 DIE => "failed to create swap disk");
266 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
267 my ($result) = xsystem ("pintos "
268 . "--os-disk=pintos/src/vm/build/os.dsk "
269 . "--fs-disk=output/$test/fs.dsk "
270 . "--swap-disk=output/$test/swap.dsk "
271 . "-v run -q -ex \"$test$testargs\"",
275 ? "ok" : "Bochs error";
276 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
283 my (@output) = snarf ("output/$test/run.out");
285 my ($grade_func) = "grade_$test";
286 $grade_func =~ s/-/_/g;
287 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
289 verify_common (@output);
290 compare_output ("$GRADES_DIR/$test.exp", @output);
293 eval "$grade_func (\@output)";
296 die $@ if $@ =~ /at \S+ line \d+$/;
302 sub grade_process_death {
303 my ($proc_name, @output) = @_;
305 verify_common (@output);
306 @output = get_core_output (@output);
307 die "First line of output is not `($proc_name) begin' message.\n"
308 if $output[0] ne "($proc_name) begin";
309 die "Output contains `FAIL' message.\n"
310 if grep (/FAIL/, @output);
311 die "Output contains spurious ($proc_name) message.\n"
312 if grep (/\($proc_name\)/, @output) > 1;
315 sub grade_pt_bad_addr {
316 grade_process_death ('pt-bad-addr', @_);
319 sub grade_pt_write_code {
320 grade_process_death ('pt-write-code', @_);
323 sub grade_mmap_unmap {
324 grade_process_death ('mmap-unmap', @_);
330 my (@assertion) = grep (/PANIC/, @output);
331 if (@assertion != 0) {
332 my ($details) = "Kernel panic:\n $assertion[0]\n";
334 my (@stack_line) = grep (/Call stack:/, @output);
335 if (@stack_line != 0) {
336 $details .= " $stack_line[0]\n\n";
337 $details .= "Translation of backtrace:\n";
338 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
342 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
345 $A2L = "i386-elf-addr2line";
347 open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
349 my ($function, $line);
350 last unless defined ($function = <A2L>);
354 $details .= " $function ($line)\n";
358 if ($assertion[0] =~ /sec_no < d->capacity/) {
360 \nThis assertion commonly fails when accessing a file via
361 an inode that has been closed and freed. Freeing an inode
362 clears all its sector indexes to 0xcccccccc, which is not
363 a valid sector number for disks smaller than about 1.6 TB.
367 $extra{$test} = $details;
368 die "Kernel panic. Details at end of file.\n"
371 if (grep (/Pintos booting/, @output) > 1) {
374 $details = "Pintos spontaneously rebooted during this test.\n";
375 $details .= "This is most often due to unhandled page faults.\n";
376 $details .= "Here's the output from the initial boot through the\n";
377 $details .= "first reboot:\n\n";
383 last if /Pintos booting/ && ++$i > 1;
385 $details{$test} = $details;
386 die "Triple-fault caused spontaneous reboot(s). "
387 . "Details at end of file.\n";
390 die "No output at all\n" if @output == 0;
391 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
392 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
393 die "Didn't start up properly: no \"Boot complete\" startup message\n"
394 if !grep (/Boot complete/, @output);
395 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
396 if !grep (/Timer: \d+ ticks/, @output);
397 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
398 if !grep (/Powering off/, @output);
401 # Get @output without header or trailer.
402 sub get_core_output {
407 for ($first = 0; $first <= $#output; $first++) {
408 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
412 for ($last = $#output; $last >= 0; $last--) {
413 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
416 if ($last < $first) {
417 my ($no_first) = $first > $#output;
418 my ($no_last) = $last < $#output;
419 die "Couldn't locate output.\n";
422 return @output[$first ... $last];
428 # Remove lines that look like exit codes.
429 # Exit codes are supposed to be printed in the form "process: exit(code)"
430 # but people get unfortunately creative with it.
431 for (my ($i) = 0; $i <= $#output; $i++) {
432 local ($_) = $output[$i];
434 my ($process, $code);
435 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
436 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
437 || (($process, $code)
438 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
439 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
440 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
442 splice (@output, $i, 1);
451 my ($exp, @actual) = @_;
452 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
453 die "Program produced no output.\n" if !@actual;
456 $details .= "$test actual output:\n";
457 $details .= join ('', map (" $_", @actual));
459 my (@exp) = map ("$_\n", snarf ($exp));
461 my ($fuzzy_match) = 0;
465 my ($s) = shift (@exp);
466 last if $s eq "--OR--\n";
467 push (@expected, $s);
470 $details .= "\n$test acceptable output:\n";
471 $details .= join ('', map (" $_", @expected));
473 # Check whether they're the same.
474 if ($#actual == $#expected) {
476 for (my ($i) = 0; $i <= $#expected; $i++) {
477 $eq = 0 if $actual[$i] ne $expected[$i];
482 # They differ. Output a diff.
484 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
485 my ($not_fuzzy_match) = 0;
486 while ($d->Next ()) {
487 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
489 push (@diff, map (" $_", $d->Items (1)));
491 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
492 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
494 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
496 $not_fuzzy_match = 1;
500 $fuzzy_match = 1 if !$not_fuzzy_match;
502 $details .= "Differences in `diff -u' format:\n";
503 $details .= join ('', @diff);
504 $details .= "(This is considered a `fuzzy match'.)\n"
505 if !$not_fuzzy_match;
510 "This test passed, but with extra, unexpected output.\n"
511 . "Please inspect your code to make sure that it does not\n"
512 . "produce output other than as specified in the project\n"
517 "This test failed because its output did not match any\n"
518 . "of the acceptable form(s).\n\n"
522 $details{$test} = $details;
523 die "Output differs from expected. Details at end of file.\n"
528 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
534 for (my ($i) = 0; $i <= $#summary; $i++) {
535 local ($_) = $summary[$i];
536 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
537 my ($result) = $result{$test} || "Not tested.";
539 if ($result eq 'ok') {
540 if (!defined $details{$test}) {
541 # Test successful and no warnings.
542 splice (@summary, $i, 1);
545 # Test successful with warnings.
548 splice (@summary, $i + 1, 0,
549 " Test passed with warnings. "
550 . "Details at end of file.");
556 splice (@summary, $i + 1, 0,
557 map (" $_", split ("\n", $result)));
559 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
561 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
562 splice (@summary, $i, 0, " All tests passed.")
563 if $ploss == 0 && !$warnings;
569 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
570 $summary[0] =~ s/\[\[total\]\]/$ts/;
572 open (SUMMARY, ">tests.out");
573 print SUMMARY map ("$_\n", @summary);
578 open (DETAILS, ">details.out");
580 for my $test (@TESTS) {
581 next if $result{$test} eq 'ok' && !defined $details{$test};
583 my ($details) = $details{$test};
584 next if !defined ($details) && ! -e "output/$test/run.out";
587 if ($result{$test} ne 'ok') {
588 $banner = "$test failure details";
590 $banner = "$test warnings";
593 print DETAILS "\n" if $n++;
594 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
595 print DETAILS "\n\n";
597 if (!defined $details) {
598 my (@output) = snarf ("output/$test/run.out");
600 # Print only the first in a series of recursing panics.
602 for my $i (0...$#output) {
603 local ($_) = $output[$i];
604 if (/PANIC/ && $panic++ > 0) {
605 @output = @output[0...$i];
607 "[...details of recursive panic omitted...]");
611 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
613 print DETAILS $details;
615 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
616 if defined $extra{$test};
623 my ($command, %options) = @_;
624 print "$command\n" if $verbose || $options{VERBOSE};
626 my ($log) = $options{LOG};
630 local $SIG{ALRM} = sub { die "alarm\n" };
631 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
633 die "fork: $!\n" if !defined $pid;
636 open (STDOUT, ">output/$log.out");
637 open (STDERR, ">output/$log.err");
649 die unless $@ eq "alarm\n"; # propagate unexpected errors
651 for (my ($i) = 0; $i < 10; $i++) {
652 kill ('SIGTERM', $pid);
654 my ($retval) = waitpid ($pid, WNOHANG);
655 last if $retval == $pid || $retval == -1;
656 print "Waiting for $pid to die" if $i == 0;
661 if (WIFSIGNALED ($status)) {
662 my ($signal) = WTERMSIG ($status);
663 die "Interrupted\n" if $signal == SIGINT;
664 print "Child terminated with signal $signal\n";
667 my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
668 $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
671 unlink ("output/$log.err") if defined ($log) && $ok;
673 die $options{DIE} if !$ok && defined $options{DIE};
680 open (OUTPUT, $file) or die "$file: open: $!\n";
681 my (@lines) = <OUTPUT>;
684 return wantarray ? @lines : join ('', map ("$_\n", @lines));
690 open (A, "<$a") or die "$a: open: $!\n";
691 open (B, "<$b") or die "$b: open: $!\n";
697 sysread (A, $sa, 1024);
698 sysread (B, $sb, 1024);
699 $equal = 0, last if $sa ne $sb;
700 $equal = 1, last if $sa eq '';
709 my ($file, $expected) = @_;
710 open (FILE, "<$file") or die "$file: open: $!\n";
712 sysread (FILE, $actual, -s FILE);
713 my ($equal) = $actual eq $expected;
719 my ($ln, $lines) = @_;
721 for my $line (@$lines) {
723 $out .= sprintf "%4d %s\n", $ln++, $line;