9 our ($VERBOSE) = 0; # Verbosity of output
10 our (@TESTS); # Tests to run.
14 GetOptions ("v|verbose+" => \$VERBOSE,
15 "h|help" => sub { usage (0) },
16 "t|test=s" => \@TESTS,
19 or die "Malformed command line; use --help for help.\n";
20 die "Non-option argument not supported; use --help for help.\n"
25 print "run-tests, for grading Pintos multiprogramming projects.\n\n";
26 print "Invoke from a directory containing a student tarball named by\n";
27 print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
28 print "In normal usage, no options are needed.\n\n";
29 print "Output is produced in tests.out and details.out.\n\n";
31 print " -c, --clean Remove old output files before starting\n";
32 print " -t, --test=TEST Execute TEST only (allowed multiple times)\n";
33 print " -g, --grade Instead of running tests, compose grade.out\n";
34 print " -v, --verbose Print commands before executing them\n";
35 print " -h, --help Print this help message\n";
39 # Default set of tests.
40 @TESTS = qw (args-argc args-argv0 args-argvn args-single args-multiple
42 sc-bad-sp sc-bad-arg sc-boundary
44 create-normal create-empty create-null create-bad-ptr
45 create-long create-exists create-bound
46 open-normal open-missing open-boundary open-empty open-null
47 open-bad-ptr open-twice
48 close-normal close-twice close-stdin close-stdout close-bad-fd
49 read-normal read-bad-ptr read-boundary read-zero read-stdout
51 write-normal write-bad-ptr write-boundary write-zero write-stdin
53 exec-once exec-arg exec-multiple exec-missing exec-bad-ptr
54 join-simple join-twice join-killed join-bad-pid
55 multi-recurse multi-oom multi-child-fd
59 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
60 $args{$key} = "some arguments for you!";
62 $args{'args-single'} = "onearg";
63 $args{'args-dbl-space'} = "two args";
64 $args{'multi-recurse'} = "15";
65 $args{'multi-oom'} = "0";
67 # Handle final grade mode.
69 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
71 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
78 my (@tests) = snarf ("tests.out");
79 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
81 my (@review) = snarf ("review.txt");
82 my ($part_lost) = (0, 0);
83 for (my ($i) = $#review; $i >= 0; $i--) {
84 local ($_) = $review[$i];
85 if (my ($loss) = /^\s*([-+]\d+)/) {
87 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
88 my ($got) = $out_of + $part_lost;
90 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
97 die "Lost points outside a section\n" if $part_lost;
99 for (my ($i) = 1; $i <= $#review; $i++) {
100 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
101 $review[$i] = '-' x (length ($review[$i - 1]));
105 print OUT "\nOVERALL SCORE\n";
106 print OUT "-------------\n";
107 print OUT "$p_got points out of $p_pos total\n\n";
109 print OUT map ("$_\n", @tests), "\n";
110 print OUT map ("$_\n", @review), "\n";
112 print OUT "DETAILS\n";
113 print OUT "-------\n\n";
114 print OUT map ("$_\n", snarf ("details.out"));
119 # Find the directory that contains the grading files.
121 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
122 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
125 # Verify that we're roughly in the correct directory
126 # before we go blasting away files.
129 xsystem ("rm -rf output pintos", VERBOSE => 1);
130 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
133 # Create output directory, if it doesn't already exist.
134 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
136 # Extract submission.
137 extract_tarball () if ! -d "pintos";
139 # Compile submission.
142 # Verify that the proper directory was submitted.
143 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
145 # Run and grade the tests.
152 my ($result) = run_test ($test);
153 if ($result eq 'ok') {
154 $result = grade_test ($test);
158 print " - with warnings" if $result eq 'ok' && defined $details{$test};
161 $result{$test} = $result;
170 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
172 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
174 # Sort tarballs in reverse order by time.
175 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
177 print "Multiple tarballs: choosing $tarballs[0]\n"
178 if scalar (@tarballs) > 1;
182 sub extract_tarball {
183 my ($tarball) = choose_tarball ();
185 mkdir "pintos" or die "pintos: mkdir: $!\n";
186 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
188 print "Extracting $tarball...\n";
189 xsystem ("cd pintos/src && tar xzf ../../$tarball",
190 DIE => "extraction failed\n");
193 print "Running fixme.sh...\n";
194 xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
197 print "Patching...\n";
198 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
200 DIE => "patch failed\n");
201 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
202 . "< $GRADES_DIR/random.diff",
204 DIE => "patch failed\n");
206 open (CONSTANTS, ">pintos/src/constants.h")
207 or die "constants.h: create: $!\n";
208 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
214 my ($ms, $d, $y, $H, $M, $S) =
215 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
217 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
219 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
224 my ($src) = "$GRADES_DIR/$test.c";
225 -e $src or die "$src: stat: $!\n";
237 # Reuse older results if any.
238 if (open (DONE, "<output/$test/done")) {
246 # Really run the test.
247 my ($status) = really_run_test ($test);
249 # Save the results for later.
250 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
251 print DONE "$status\n";
258 print "Compiling...\n";
259 xsystem ("cd pintos/src/userprog && make", LOG => "make")
260 or return "compile error";
263 sub really_run_test {
265 # If there's residue from an earlier test, move it to .old.
266 # If there's already a .old, delete it.
267 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
268 rename "output/$test", "output/$test.old" or die "rename: $!\n"
269 if -d "output/$test";
271 # Make output directory.
272 mkdir "output/$test";
273 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
274 DIE => "cp failed\n");
278 $timeout = 600 if $test =~ /^multi-/;
279 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
281 . "--os-disk=pintos/src/userprog/build/os.dsk "
282 . "--fs-disk=output/$test/fs.dsk "
283 . "-v run -q -ex \"$test$testargs\"",
286 or return "Bochs error";
294 my (@output) = snarf ("output/$test/run.out");
296 my ($grade_func) = "grade_$test";
297 $grade_func =~ s/-/_/g;
298 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
300 verify_common (@output);
301 compare_output ("$GRADES_DIR/$test.exp", @output);
304 eval "$grade_func (\@output)";
307 die $@ if $@ =~ /at \S+ line \d+$/;
313 sub grade_write_normal {
315 verify_common (@output);
316 compare_output ("$GRADES_DIR/write-normal.exp", @output);
317 my ($test_txt) = "output/$test/test.txt";
318 get_file ("test.txt", $test_txt) if ! -e $test_txt;
320 my (@actual) = snarf ($test_txt);
321 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
324 if ($#actual == $#expected) {
326 for my $i (0...$#actual) {
327 $eq = 0 if $actual[$i] ne $expected[$i];
334 $details = "Expected file content:\n";
335 $details .= join ('', map (" $_\n", @expected));
336 $details .= "Actual file content:\n";
337 $details .= join ('', map (" $_\n", @actual));
338 $extra{$test} = $details;
340 die "File written didn't have expected content.\n";
344 sub grade_multi_oom {
346 verify_common (@output);
348 @output = fix_exit_codes (get_core_output (@output));
350 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
351 die "Child process $m started out of order.\n" if $m != $n;
355 die "Only $n child process(es) started.\n" if $n < 15;
357 # There could be a death notice for a process that didn't get
358 # fully loaded, and/or notices from the loader.
360 && ($output[0] =~ /^multi-oom: exit\(-1\)$/
361 || $output[0] =~ /^load: /)) {
366 die "Output ended unexpectedly before process $n finished.\n"
370 chomp ($_ = shift @output);
371 die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
372 die "Child process $n ended out of order.\n"
373 if !/^\(multi-oom\) end $n$/;
375 chomp ($_ = shift @output);
376 die "Kernel didn't print proper exit message for process $n.\n"
377 if !/^multi-oom: exit\($n\)$/;
379 die "Spurious output at end: '$output[0]'.\n" if @output;
383 my ($guest_fn, $host_fn) = @_;
385 . "--os-disk=pintos/src/userprog/build/os.dsk "
386 . "--fs-disk=output/$test/fs.dsk "
387 . "-v get $guest_fn $host_fn",
388 LOG => "$test/get-$guest_fn",
390 or die "get $guest_fn failed\n";
397 my (@assertion) = grep (/PANIC/, @output);
398 if (@assertion != 0) {
399 my ($details) = "Kernel panic:\n $assertion[0]\n";
401 my (@stack_line) = grep (/Call stack:/, @output);
402 if (@stack_line != 0) {
403 $details .= " $stack_line[0]\n\n";
404 $details .= "Translation of backtrace:\n";
405 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
409 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
412 $A2L = "i386-elf-addr2line";
414 open (A2L, "$A2L -fe pintos/src/userprog/build/kernel.o @addrs|");
416 my ($function, $line);
417 last unless defined ($function = <A2L>);
421 $details .= " $function ($line)\n";
424 $extra{$test} = $details;
425 die "Kernel panic. Details at end of file.\n"
428 if (grep (/Pintos booting/, @output) > 1) {
431 $details = "Pintos spontaneously rebooted during this test.\n";
432 $details .= "This is most often due to unhandled page faults.\n";
433 $details .= "Here's the output from the initial boot through the\n";
434 $details .= "first reboot:\n\n";
440 last if /Pintos booting/ && ++$i > 1;
442 $details{$test} = $details;
443 die "Triple-fault caused spontaneous reboot(s). "
444 . "Details at end of file.\n";
447 die "No output at all\n" if @output == 0;
448 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
449 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
450 die "Didn't start up properly: no \"Boot complete\" startup message\n"
451 if !grep (/Boot complete/, @output);
452 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
453 if !grep (/Timer: \d+ ticks/, @output);
454 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
455 if !grep (/Powering off/, @output);
458 # Get @output without header or trailer.
459 sub get_core_output {
464 for ($first = 0; $first <= $#output; $first++) {
465 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
469 for ($last = $#output; $last >= 0; $last--) {
470 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
473 if ($last < $first) {
474 my ($no_first) = $first > $#output;
475 my ($no_last) = $last < $#output;
476 die "Couldn't locate output.\n";
479 return @output[$first ... $last];
485 # Fix up lines that look like exit codes.
486 # Exit codes are supposed to be printed in the form "process: exit(code)"
487 # but people get unfortunately creative with it.
488 for my $i (0...$#output) {
489 local ($_) = $output[$i];
491 my ($process, $code);
492 if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
493 || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
494 || (($process, $code)
495 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
496 || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
497 || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
499 $process = substr ($process, 0, 15);
500 $process =~ s/\s.*//;
501 $output[$i] = "$process: exit($code)\n";
509 my ($exp, @actual) = @_;
510 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
513 $details .= "$test actual output:\n";
514 $details .= join ('', map (" $_", @actual));
516 my (@exp) = map ("$_\n", snarf ($exp));
518 my ($fuzzy_match) = 0;
522 my ($s) = shift (@exp);
523 last if $s eq "--OR--\n";
524 push (@expected, $s);
527 $details .= "\n$test acceptable output:\n";
528 $details .= join ('', map (" $_", @expected));
530 # Check whether they're the same.
531 if ($#actual == $#expected) {
533 for (my ($i) = 0; $i <= $#expected; $i++) {
534 $eq = 0 if $actual[$i] ne $expected[$i];
539 # They differ. Output a diff.
541 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
542 my ($not_fuzzy_match) = 0;
543 while ($d->Next ()) {
544 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
546 push (@diff, map (" $_", $d->Items (1)));
548 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
549 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
551 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
552 $not_fuzzy_match = 1;
556 $fuzzy_match = 1 if !$not_fuzzy_match;
558 $details .= "Differences in `diff -u' format:\n";
559 $details .= join ('', @diff);
560 $details .= "(This is considered a `fuzzy match'.)\n"
561 if !$not_fuzzy_match;
566 "This test passed, but with extra, unexpected output.\n"
567 . "Please inspect your code to make sure that it does not\n"
568 . "produce output other than as specified in the project\n"
573 "This test failed because its output did not match any\n"
574 . "of the acceptable form(s).\n\n"
578 $details{$test} = $details;
579 die "Output differs from expected. Details at end of file.\n"
584 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
590 for (my ($i) = 0; $i <= $#summary; $i++) {
591 local ($_) = $summary[$i];
592 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
593 my ($result) = $result{$test} || "Not tested.";
595 if ($result eq 'ok') {
596 if (!defined $details{$test}) {
597 # Test successful and no warnings.
598 splice (@summary, $i, 1);
601 # Test successful with warnings.
604 splice (@summary, $i + 1, 0,
605 " Test passed with warnings. "
606 . "Details at end of file.");
612 splice (@summary, $i + 1, 0,
613 map (" $_", split ("\n", $result)));
615 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
617 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
618 splice (@summary, $i, 0, " All tests passed.")
619 if $ploss == 0 && !$warnings;
625 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
626 $summary[0] =~ s/\[\[total\]\]/$ts/;
628 open (SUMMARY, ">tests.out");
629 print SUMMARY map ("$_\n", @summary);
634 open (DETAILS, ">details.out");
636 for my $test (@TESTS) {
637 next if $result{$test} eq 'ok' && !defined $details{$test};
639 my ($details) = $details{$test};
640 next if !defined ($details) && ! -e "output/$test/run.out";
643 if ($result{$test} ne 'ok') {
644 $banner = "$test failure details";
646 $banner = "$test warnings";
649 print DETAILS "\n" if $n++;
650 print DETAILS "--- $banner ", '-' x (50 - length ($banner));
651 print DETAILS "\n\n";
653 if (!defined $details) {
654 $details = "Output:\n\n" . snarf ("output/$test/run.out");
656 print DETAILS $details;
658 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
659 if defined $extra{$test};
666 my ($command, %options) = @_;
667 print "$command\n" if $VERBOSE || $options{VERBOSE};
669 my ($log) = $options{LOG};
670 if (defined ($log)) {
671 $command = "($command) >output/$log.out 2>output/$log.err";
676 local $SIG{ALRM} = sub { die "alarm\n" };
677 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
679 die "fork: $!\n" if !defined $pid;
680 exec ($command), die "$command: exec: $!\n" if !$pid;
686 die unless $@ eq "alarm\n"; # propagate unexpected errors
687 print "Timed out.\n";
692 if (WIFSIGNALED ($status)) {
693 my ($signal) = WTERMSIG ($status);
694 die "Interrupted\n" if $signal == SIGINT;
695 print "Child terminated with signal $signal\n";
698 unlink ("output/$log.err") if defined ($log) && $status == 0;
700 die $options{DIE} if $status != 0 && defined $options{DIE};
707 open (OUTPUT, $file) or die "$file: open: $!\n";
708 my (@lines) = <OUTPUT>;
711 return wantarray ? @lines : join ('', map ("$_\n", @lines));
717 open (A, "<$a") or die "$a: open: $!\n";
718 open (B, "<$b") or die "$b: open: $!\n";
724 sysread (A, $sa, 1024);
725 sysread (B, $sb, 1024);
726 $equal = 0, last if $sa ne $sb;
727 $equal = 1, last if $sa eq '';
736 my ($file, $expected) = @_;
737 open (FILE, "<$file") or die "$file: open: $!\n";
739 sysread (FILE, $actual, -s FILE);
740 my ($equal) = $actual eq $expected;
746 my ($ln, $lines) = @_;
748 for my $line (@$lines) {
750 $out .= sprintf "%4d %s\n", $ln++, $line;