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");
192 print "Patching...\n";
193 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
195 DIE => "patch failed\n");
196 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
197 . "< $GRADES_DIR/random.diff",
199 DIE => "patch failed\n");
201 open (CONSTANTS, ">pintos/src/constants.h")
202 or die "constants.h: create: $!\n";
203 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
209 my ($ms, $d, $y, $H, $M, $S) =
210 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
212 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
214 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
219 my ($src) = "$GRADES_DIR/$test.c";
220 -e $src or die "$src: stat: $!\n";
232 # Reuse older results if any.
233 if (open (DONE, "<output/$test/done")) {
241 # Really run the test.
242 my ($status) = really_run_test ($test);
244 # Save the results for later.
245 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
246 print DONE "$status\n";
253 print "Compiling...\n";
254 xsystem ("cd pintos/src/userprog && make", LOG => "make")
255 or return "compile error";
258 sub really_run_test {
260 # If there's residue from an earlier test, move it to .old.
261 # If there's already a .old, delete it.
262 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
263 rename "output/$test", "output/$test.old" or die "rename: $!\n"
264 if -d "output/$test";
266 # Make output directory.
267 mkdir "output/$test";
268 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
269 DIE => "cp failed\n");
273 $timeout = 60 if $test eq 'multi-oom';
274 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
276 . "--os-disk=pintos/src/userprog/build/os.dsk "
277 . "--fs-disk=output/$test/fs.dsk "
278 . "-v run -q -ex \"$test$testargs\"",
281 or return "Bochs error";
289 my (@output) = snarf ("output/$test/run.out");
291 my ($grade_func) = "grade_$test";
292 $grade_func =~ s/-/_/g;
293 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
295 verify_common (@output);
296 compare_output ("$GRADES_DIR/$test.exp", @output);
299 eval "$grade_func (\@output)";
302 die $@ if $@ =~ /at \S+ line \d+$/;
308 sub grade_write_normal {
310 verify_common (@output);
311 compare_output ("$GRADES_DIR/write-normal.exp", @output);
312 my ($test_txt) = "output/$test/test.txt";
313 get_file ("test.txt", $test_txt) if ! -e $test_txt;
315 my (@actual) = snarf ($test_txt);
316 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
319 if ($#actual == $#expected) {
321 for my $i (0...$#actual) {
322 $eq = 0 if $actual[$i] ne $expected[$i];
329 $details = "Expected file content:\n";
330 $details .= join ('', map (" $_\n", @expected));
331 $details .= "Actual file content:\n";
332 $details .= join ('', map (" $_\n", @actual));
333 $extra{$test} = $details;
335 die "File written didn't have expected content.\n";
339 sub grade_multi_oom {
341 verify_common (@output);
343 @output = fix_exit_codes (get_core_output (@output));
345 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
346 die "Child process $m started out of order.\n" if $m != $n;
350 die "Only $n child processes started.\n" if $n < 15;
352 # There could be a death notice for a process that didn't get
353 # fully loaded, and/or notices from the loader.
355 && ($output[0] =~ /^\(multi-oom\) end $n$/
356 || $output[0] =~ /^load: /)) {
361 die "Output ended unexpectedly before process $n finished.\n"
365 chomp ($_ = shift @output);
366 die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
367 die "Child process $n ended out of order.\n"
368 if !/^\(multi-oom\) end $n$/;
370 chomp ($_ = shift @output);
371 die "Kernel didn't print proper exit message for process $n.\n"
372 if !/^multi-oom: exit\($n\)$/;
374 die "Spurious output at end: '$output[0]'.\n" if @output;
378 my ($guest_fn, $host_fn) = @_;
380 . "--os-disk=pintos/src/userprog/build/os.dsk "
381 . "--fs-disk=output/$test/fs.dsk "
382 . "-v get $guest_fn $host_fn",
383 LOG => "$test/get-$guest_fn",
385 or die "get $guest_fn failed\n";
392 my (@assertion) = grep (/PANIC/, @output);
393 if (@assertion != 0) {
394 my ($details) = "Kernel panic:\n $assertion[0]\n";
396 my (@stack_line) = grep (/Call stack:/, @output);
397 if (@stack_line != 0) {
398 $details .= " $stack_line[0]\n\n";
399 $details .= "Translation of backtrace:\n";
400 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
404 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
407 $A2L = "i386-elf-addr2line";
409 open (A2L, "$A2L -fe pintos/src/userprog/build/kernel.o @addrs|");
411 my ($function, $line);
412 last unless defined ($function = <A2L>);
416 $details .= " $function ($line)\n";
419 $extra{$test} = $details;
420 die "Kernel panic. Details at end of file.\n"
423 die "No output at all\n" if @output == 0;
424 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
425 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
426 die "Didn't start up properly: no \"Boot complete\" startup message\n"
427 if !grep (/Boot complete/, @output);
428 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
429 if !grep (/Timer: \d+ ticks/, @output);
430 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
431 if !grep (/Powering off/, @output);
434 # Get @output without header or trailer.
435 sub get_core_output {
440 for ($first = 0; $first <= $#output; $first++) {
441 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
445 for ($last = $#output; $last >= 0; $last--) {
446 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
449 if ($last < $first) {
450 my ($no_first) = $first > $#output;
451 my ($no_last) = $last < $#output;
452 die "Couldn't locate output.\n";
455 return @output[$first ... $last];
461 # Fix up lines that look like exit codes.
462 # Exit codes are supposed to be printed in the form "process: exit(code)"
463 # but people get unfortunately creative with it.
464 for my $i (0...$#output) {
465 local ($_) = $output[$i];
467 my ($process, $code);
468 if ((($process, $code) = /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
469 || (($process, $code) = /^([-a-zA-Z0-9 ]+) exit\((-?\d+)\)$/)
470 || (($process, $code)
471 = /^([-a-zA-Z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
472 || (($process, $code) = /^([-a-zA-Z0-9 ]+):\( (-?\d+) \) $/)
474 $process = substr ($process, 0, 15);
475 $process =~ s/\s.*//;
476 $output[$i] = "$process: exit($code)\n";
484 my ($exp, @actual) = @_;
485 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
488 $details .= "$test actual output:\n";
489 $details .= join ('', map (" $_", @actual));
491 my (@exp) = map ("$_\n", snarf ($exp));
493 my ($fuzzy_match) = 0;
497 my ($s) = shift (@exp);
498 last if $s eq "--OR--\n";
499 push (@expected, $s);
502 $details .= "\n$test acceptable output:\n";
503 $details .= join ('', map (" $_", @expected));
505 # Check whether they're the same.
506 if ($#actual == $#expected) {
508 for (my ($i) = 0; $i <= $#expected; $i++) {
509 $eq = 0 if $actual[$i] ne $expected[$i];
514 # They differ. Output a diff.
516 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
517 my ($not_fuzzy_match) = 0;
518 while ($d->Next ()) {
519 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
521 push (@diff, map (" $_", $d->Items (1)));
523 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
524 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
526 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
527 $not_fuzzy_match = 1;
531 $fuzzy_match = 1 if !$not_fuzzy_match;
533 $details .= "Differences in `diff -u' format:\n";
534 $details .= join ('', @diff);
535 $details .= "(This is considered a `fuzzy match'.)\n"
536 if !$not_fuzzy_match;
539 $details{$test} = $details;
540 die "Output differs from expected. Details at end of file.\n"
545 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
550 for (my ($i) = 0; $i <= $#summary; $i++) {
551 local ($_) = $summary[$i];
552 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
553 my ($result) = $result{$test} || "Not tested.";
555 if ($result eq 'ok') {
556 splice (@summary, $i, 1);
561 splice (@summary, $i + 1, 0,
562 map (" $_", split ("\n", $result)));
564 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
566 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
567 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
572 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
573 $summary[0] =~ s/\[\[total\]\]/$ts/;
575 open (SUMMARY, ">tests.out");
576 print SUMMARY map ("$_\n", @summary);
581 open (DETAILS, ">details.out");
583 for my $test (@TESTS) {
584 next if $result{$test} eq 'ok' && !defined $details{$test};
586 my ($details) = $details{$test};
587 next if !defined ($details) && ! -e "output/$test/run.out";
589 print DETAILS "\n" if $n++;
590 print DETAILS "--- $test details ", '-' x (50 - length ($test));
591 print DETAILS "\n\n";
593 if (!defined $details) {
594 $details = "Output:\n\n" . snarf ("output/$test/run.out");
596 print DETAILS $details;
598 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
599 if defined $extra{$test};
606 my ($command, %options) = @_;
607 print "$command\n" if $VERBOSE || $options{VERBOSE};
609 my ($log) = $options{LOG};
610 if (defined ($log)) {
611 $command = "($command) >output/$log.out 2>output/$log.err";
616 local $SIG{ALRM} = sub { die "alarm\n" };
617 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
619 die "fork: $!\n" if !defined $pid;
620 exec ($command), die "$command: exec: $!\n" if !$pid;
626 die unless $@ eq "alarm\n"; # propagate unexpected errors
627 print "Timed out.\n";
632 if (WIFSIGNALED ($status)) {
633 my ($signal) = WTERMSIG ($status);
634 die "Interrupted\n" if $signal == SIGINT;
635 print "Child terminated with signal $signal\n";
638 unlink ("output/$log.err") if defined ($log) && $status == 0;
640 die $options{DIE} if $status != 0 && defined $options{DIE};
647 open (OUTPUT, $file) or die "$file: open: $!\n";
648 my (@lines) = <OUTPUT>;
651 return wantarray ? @lines : join ('', map ("$_\n", @lines));
657 open (A, "<$a") or die "$a: open: $!\n";
658 open (B, "<$b") or die "$b: open: $!\n";
664 sysread (A, $sa, 1024);
665 sysread (B, $sb, 1024);
666 $equal = 0, last if $sa ne $sb;
667 $equal = 1, last if $sa eq '';
676 my ($file, $expected) = @_;
677 open (FILE, "<$file") or die "$file: open: $!\n";
679 sysread (FILE, $actual, -s FILE);
680 my ($equal) = $actual eq $expected;
686 my ($ln, $lines) = @_;
688 for my $line (@$lines) {
690 $out .= sprintf "%4d %s\n", $ln++, $line;