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
51 grow-create grow-seq-sm grow-seq-lg grow-file-size grow-tell
52 grow-sparse grow-root-sm grow-root-lg grow-dir-lg grow-two-files
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");
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 ("pintos make-disk output/$test/fs.dsk 2 >/dev/null 2>&1",
255 DIE => "failed to create file system disk");
256 xsystem ("pintos make-disk output/$test/swap.dsk 2 >/dev/null 2>&1",
257 DIE => "failed to create swap disk");
259 # Format disk, install test.
260 my ($pintos_base_cmd) =
262 . "--os-disk=pintos/src/vm/build/os.dsk "
263 . "--fs-disk=output/$test/fs.dsk "
264 . "--swap-disk=output/$test/swap.dsk "
266 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk"),
267 return "format/put error"
268 if !xsystem ("$pintos_base_cmd put -f $GRADES_DIR/$test $test",
269 LOG => "$test/put", TIMEOUT => 60, EXPECT => 1);
273 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
275 xsystem ("$pintos_base_cmd run -q -ex \"$test$testargs\"",
276 LOG => "$test/run", TIMEOUT => $timeout, EXPECT => 1)
277 ? "ok" : "Bochs error";
278 unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
285 my (@output) = snarf ("output/$test/run.out");
287 my ($grade_func) = "grade_$test";
288 $grade_func =~ s/-/_/g;
289 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
291 verify_common (@output);
292 compare_output ("$GRADES_DIR/$test.exp", @output);
295 eval "$grade_func (\@output)";
298 die $@ if $@ =~ /at \S+ line \d+$/;
304 sub grade_process_death {
305 my ($proc_name, @output) = @_;
307 verify_common (@output);
308 @output = get_core_output (@output);
309 die "First line of output is not `($proc_name) begin' message.\n"
310 if $output[0] ne "($proc_name) begin";
311 die "Output contains `FAIL' message.\n"
312 if grep (/FAIL/, @output);
313 die "Output contains spurious ($proc_name) message.\n"
314 if grep (/\($proc_name\)/, @output) > 1;
317 sub grade_pt_bad_addr {
318 grade_process_death ('pt-bad-addr', @_);
321 sub grade_pt_write_code {
322 grade_process_death ('pt-write-code', @_);
325 sub grade_mmap_unmap {
326 grade_process_death ('mmap-unmap', @_);
332 my (@assertion) = grep (/PANIC/, @output);
333 if (@assertion != 0) {
334 my ($details) = "Kernel panic:\n $assertion[0]\n";
336 my (@stack_line) = grep (/Call stack:/, @output);
337 if (@stack_line != 0) {
338 $details .= " $stack_line[0]\n\n";
339 $details .= "Translation of backtrace:\n";
340 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
344 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
347 $A2L = "i386-elf-addr2line";
349 open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
351 my ($function, $line);
352 last unless defined ($function = <A2L>);
356 $details .= " $function ($line)\n";
360 if ($assertion[0] =~ /sec_no < d->capacity/) {
362 \nThis assertion commonly fails when accessing a file via
363 an inode that has been closed and freed. Freeing an inode
364 clears all its sector indexes to 0xcccccccc, which is not
365 a valid sector number for disks smaller than about 1.6 TB.
369 $extra{$test} = $details;
370 die "Kernel panic. Details at end of file.\n"
373 if (grep (/Pintos booting/, @output) > 1) {
376 $details = "Pintos spontaneously rebooted during this test.\n";
377 $details .= "This is most often due to unhandled page faults.\n";
378 $details .= "Here's the output from the initial boot through the\n";
379 $details .= "first reboot:\n\n";
385 last if /Pintos booting/ && ++$i > 1;
387 $details{$test} = $details;
388 die "Triple-fault caused spontaneous reboot(s). "
389 . "Details at end of file.\n";
392 die "No output at all\n" if @output == 0;
393 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
394 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
395 die "Didn't start up properly: no \"Boot complete\" startup message\n"
396 if !grep (/Boot complete/, @output);
397 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
398 if !grep (/Timer: \d+ ticks/, @output);
399 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
400 if !grep (/Powering off/, @output);
403 # Get @output without header or trailer.
404 sub get_core_output {
409 for ($first = 0; $first <= $#output; $first++) {
410 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
414 for ($last = $#output; $last >= 0; $last--) {
415 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
418 if ($last < $first) {
419 my ($no_first) = $first > $#output;
420 my ($no_last) = $last < $#output;
421 die "Couldn't locate output.\n";
424 return @output[$first ... $last];
430 # Remove lines that look like exit codes.
431 # Exit codes are supposed to be printed in the form "process: exit(code)"
432 # but people get unfortunately creative with it.
433 for (my ($i) = 0; $i <= $#output; $i++) {
434 local ($_) = $output[$i];
436 my ($process, $code);
437 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
438 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
439 || (($process, $code)
440 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
441 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
442 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
444 splice (@output, $i, 1);
453 my ($exp, @actual) = @_;
454 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
455 die "Program produced no output.\n" if !@actual;
458 $details .= "$test actual output:\n";
459 $details .= join ('', map (" $_", @actual));
461 my (@exp) = map ("$_\n", snarf ($exp));
463 my ($fuzzy_match) = 0;
467 my ($s) = shift (@exp);
468 last if $s eq "--OR--\n";
469 push (@expected, $s);
472 $details .= "\n$test acceptable output:\n";
473 $details .= join ('', map (" $_", @expected));
475 # Check whether they're the same.
476 if ($#actual == $#expected) {
478 for (my ($i) = 0; $i <= $#expected; $i++) {
479 $eq = 0 if $actual[$i] ne $expected[$i];
484 # They differ. Output a diff.
486 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
487 my ($not_fuzzy_match) = 0;
488 while ($d->Next ()) {
489 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
491 push (@diff, map (" $_", $d->Items (1)));
493 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
494 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
496 || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
498 $not_fuzzy_match = 1;
502 $fuzzy_match = 1 if !$not_fuzzy_match;
504 $details .= "Differences in `diff -u' format:\n";
505 $details .= join ('', @diff);
506 $details .= "(This is considered a `fuzzy match'.)\n"
507 if !$not_fuzzy_match;
512 "This test passed, but with extra, unexpected output.\n"
513 . "Please inspect your code to make sure that it does not\n"
514 . "produce output other than as specified in the project\n"
519 "This test failed because its output did not match any\n"
520 . "of the acceptable form(s).\n\n"
524 $details{$test} = $details;
525 die "Output differs from expected. Details at end of file.\n"
530 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
536 for (my ($i) = 0; $i <= $#summary; $i++) {
537 local ($_) = $summary[$i];
538 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
539 my ($result) = $result{$test} || "Not tested.";
541 if ($result eq 'ok') {
542 if (!defined $details{$test}) {
543 # Test successful and no warnings.
544 splice (@summary, $i, 1);
547 # Test successful with warnings.
550 splice (@summary, $i + 1, 0,
551 " Test passed with warnings. "
552 . "Details at end of file.");
558 splice (@summary, $i + 1, 0,
559 map (" $_", split ("\n", $result)));
561 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
563 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
564 splice (@summary, $i, 0, " All tests passed.")
565 if $ploss == 0 && !$warnings;
571 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
572 $summary[0] =~ s/\[\[total\]\]/$ts/;
574 open (SUMMARY, ">tests.out");
575 print SUMMARY map ("$_\n", @summary);
580 open (DETAILS, ">details.out");
582 for my $test (@TESTS) {
583 next if $result{$test} eq 'ok' && !defined $details{$test};
585 my ($details) = $details{$test};
586 next if !defined ($details) && ! -e "output/$test/run.out";
589 if ($result{$test} ne 'ok') {
590 $banner = "$test failure details";
592 $banner = "$test warnings";
595 print DETAILS "\n" if $n++;
596 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
597 print DETAILS "\n\n";
599 if (!defined $details) {
600 my (@output) = snarf ("output/$test/run.out");
602 # Print only the first in a series of recursing panics.
604 for my $i (0...$#output) {
605 local ($_) = $output[$i];
606 if (/PANIC/ && $panic++ > 0) {
607 @output = @output[0...$i];
609 "[...details of recursive panic omitted...]");
613 $details = "Output:\n\n" . join ('', map ("$_\n", @output));
615 print DETAILS $details;
617 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
618 if defined $extra{$test};
625 my ($command, %options) = @_;
626 print "$command\n" if $verbose || $options{VERBOSE};
628 my ($log) = $options{LOG};
632 local $SIG{ALRM} = sub { die "alarm\n" };
633 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
635 die "fork: $!\n" if !defined $pid;
638 open (STDOUT, ">output/$log.out");
639 open (STDERR, ">output/$log.err");
651 die unless $@ eq "alarm\n"; # propagate unexpected errors
653 for (my ($i) = 0; $i < 10; $i++) {
654 kill ('SIGTERM', $pid);
656 my ($retval) = waitpid ($pid, WNOHANG);
657 last if $retval == $pid || $retval == -1;
658 print "Waiting for $pid to die" if $i == 0;
663 if (WIFSIGNALED ($status)) {
664 my ($signal) = WTERMSIG ($status);
665 die "Interrupted\n" if $signal == SIGINT;
666 print "Child terminated with signal $signal\n";
669 my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
670 $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
674 if (!$ok && defined $options{DIE}) {
675 my ($msg) = $options{DIE};
676 if (defined ($log)) {
678 $msg .= "; see output/$log.err and output/$log.out for details\n";
681 } elsif (defined ($log) && $ok) {
682 unlink ("output/$log.err");
690 open (OUTPUT, $file) or die "$file: open: $!\n";
691 my (@lines) = <OUTPUT>;
694 return wantarray ? @lines : join ('', map ("$_\n", @lines));
700 open (A, "<$a") or die "$a: open: $!\n";
701 open (B, "<$b") or die "$b: open: $!\n";
707 sysread (A, $sa, 1024);
708 sysread (B, $sb, 1024);
709 $equal = 0, last if $sa ne $sb;
710 $equal = 1, last if $sa eq '';
719 my ($file, $expected) = @_;
720 open (FILE, "<$file") or die "$file: open: $!\n";
722 sysread (FILE, $actual, -s FILE);
723 my ($equal) = $actual eq $expected;
729 my ($ln, $lines) = @_;
731 for my $line (@$lines) {
733 $out .= sprintf "%4d %s\n", $ln++, $line;