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 multi-recurse multi-oom multi-child-fd
58 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
59 $args{$key} = "some arguments for you!";
61 $args{'args-single'} = "onearg";
62 $args{'args-dbl-space'} = "two args";
63 $args{'multi-recurse'} = "15";
64 $args{'multi-oom'} = "0";
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"));
118 # Find the directory that contains the grading files.
120 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
121 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
124 # Verify that we're roughly in the correct directory
125 # before we go blasting away files.
128 xsystem ("rm -rf output pintos", VERBOSE => 1);
129 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
132 # Create output directory, if it doesn't already exist.
133 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
135 # Extract submission.
136 extract_tarball () if ! -d "pintos";
138 # Compile submission.
141 # Verify that the proper directory was submitted.
142 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
144 # Run and grade the tests.
151 my ($result) = run_test ($test);
152 if ($result eq 'ok') {
153 $result = grade_test ($test);
157 print " - with warnings" if $result eq 'ok' && defined $details{$test};
160 $result{$test} = $result;
169 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
171 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
173 # Sort tarballs in reverse order by time.
174 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
176 print "Multiple tarballs: choosing $tarballs[0]\n"
177 if scalar (@tarballs) > 1;
181 sub extract_tarball {
182 my ($tarball) = choose_tarball ();
184 mkdir "pintos" or die "pintos: mkdir: $!\n";
185 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
187 print "Extracting $tarball...\n";
188 xsystem ("cd pintos/src && tar xzf ../../$tarball",
189 DIE => "extraction failed\n");
191 print "Patching...\n";
192 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
194 DIE => "patch failed\n");
195 xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
196 . "< $GRADES_DIR/random.diff",
198 DIE => "patch failed\n");
200 open (CONSTANTS, ">pintos/src/constants.h")
201 or die "constants.h: create: $!\n";
202 print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
208 my ($ms, $d, $y, $H, $M, $S) =
209 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
211 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
213 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
218 my ($src) = "$GRADES_DIR/$test.c";
219 -e $src or die "$src: stat: $!\n";
231 # Reuse older results if any.
232 if (open (DONE, "<output/$test/done")) {
240 # Really run the test.
241 my ($status) = really_run_test ($test);
243 # Save the results for later.
244 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
245 print DONE "$status\n";
252 print "Compiling...\n";
253 xsystem ("cd pintos/src/userprog && make", LOG => "make")
254 or return "compile error";
257 sub really_run_test {
259 # If there's residue from an earlier test, move it to .old.
260 # If there's already a .old, delete it.
261 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
262 rename "output/$test", "output/$test.old" or die "rename: $!\n"
263 if -d "output/$test";
265 # Make output directory.
266 mkdir "output/$test";
267 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
268 DIE => "cp failed\n");
272 $timeout = 60 if $test eq 'multi-oom';
273 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
275 . "--os-disk=pintos/src/userprog/build/os.dsk "
276 . "--fs-disk=output/$test/fs.dsk "
277 . "-v run -q -ex \"$test$testargs\"",
280 or return "Bochs error";
288 my (@output) = snarf ("output/$test/run.out");
290 my ($grade_func) = "grade_$test";
291 $grade_func =~ s/-/_/g;
292 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
294 verify_common (@output);
295 compare_output ("$GRADES_DIR/$test.exp", @output);
298 eval "$grade_func (\@output)";
301 die $@ if $@ =~ /at \S+ line \d+$/;
307 sub grade_write_normal {
309 verify_common (@output);
310 compare_output ("$GRADES_DIR/write-normal.exp", @output);
311 my ($test_txt) = "output/$test/test.txt";
312 get_file ("test.txt", $test_txt) if ! -e $test_txt;
314 my (@actual) = snarf ($test_txt);
315 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
318 if ($#actual == $#expected) {
320 for my $i (0...$#actual) {
321 $eq = 0 if $actual[$i] ne $expected[$i];
328 $details = "Expected file content:\n";
329 $details .= join ('', map (" $_\n", @expected));
330 $details .= "Actual file content:\n";
331 $details .= join ('', map (" $_\n", @actual));
332 $extra{$test} = $details;
334 die "File written didn't have expected content.\n";
338 sub grade_multi_oom {
340 verify_common (@output);
342 @output = fix_exit_codes (get_core_output (@output));
344 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
345 die "Child process $m started out of order.\n" if $m != $n;
349 die "Only $n child processes started.\n" if $n < 15;
351 die "Output ended unexpectedly before process $n finished.\n"
353 die "Child process $n ended out of order.\n"
354 if $output[0] !~ /^\(multi-oom\) end $n$/;
357 die "Child process $n didn't print proper exit message.\n"
358 if $output[0] !~ /^multi-oom: exit\($n\)$/;
361 die "Spurious output at end: '$output[0]'.\n" if @output;
365 my ($guest_fn, $host_fn) = @_;
367 . "--os-disk=pintos/src/userprog/build/os.dsk "
368 . "--fs-disk=output/$test/fs.dsk "
369 . "-v get $guest_fn $host_fn",
370 LOG => "$test/get-$guest_fn",
372 or die "get $guest_fn failed\n";
379 my (@assertion) = grep (/PANIC/, @output);
380 if (@assertion != 0) {
381 my ($details) = "Kernel panic:\n $assertion[0]\n";
383 my (@stack_line) = grep (/Call stack:/, @output);
384 if (@stack_line != 0) {
385 $details .= " $stack_line[0]\n\n";
386 $details .= "Translation of backtrace:\n";
387 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
391 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
394 $A2L = "i386-elf-addr2line";
396 open (A2L, "$A2L -fe pintos/src/userprog/build/kernel.o @addrs|");
398 my ($function, $line);
399 last unless defined ($function = <A2L>);
403 $details .= " $function ($line)\n";
406 $extra{$test} = $details;
407 die "Kernel panic. Details at end of file.\n"
410 die "No output at all\n" if @output == 0;
411 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
412 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
413 die "Didn't start up properly: no \"Boot complete\" startup message\n"
414 if !grep (/Boot complete/, @output);
415 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
416 if !grep (/Timer: \d+ ticks/, @output);
417 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
418 if !grep (/Powering off/, @output);
421 # Get @output without header or trailer.
422 sub get_core_output {
427 for ($first = 0; $first <= $#output; $first++) {
428 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
432 for ($last = $#output; $last >= 0; $last--) {
433 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
436 if ($last < $first) {
437 my ($no_first) = $first > $#output;
438 my ($no_last) = $last < $#output;
439 die "Couldn't locate output.\n";
442 return @output[$first ... $last];
448 # Fix up lines that look like exit codes.
449 for my $i (0...$#output) {
450 if (my ($process, $code)
451 = $output[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
452 $process = substr ($process, 0, 15);
453 $process =~ s/\s.*//;
454 $output[$i] = "$process: exit($code)\n";
462 my ($exp, @actual) = @_;
463 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
466 $details .= "$test actual output:\n";
467 $details .= join ('', map (" $_", @actual));
469 my (@exp) = map ("$_\n", snarf ($exp));
471 my ($fuzzy_match) = 0;
475 my ($s) = shift (@exp);
476 last if $s eq "--OR--\n";
477 push (@expected, $s);
480 $details .= "\n$test acceptable output:\n";
481 $details .= join ('', map (" $_", @expected));
483 # Check whether they're the same.
484 if ($#actual == $#expected) {
486 for (my ($i) = 0; $i <= $#expected; $i++) {
487 $eq = 0 if $actual[$i] ne $expected[$i];
492 # They differ. Output a diff.
494 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
495 my ($not_fuzzy_match) = 0;
496 while ($d->Next ()) {
497 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
499 push (@diff, map (" $_", $d->Items (1)));
501 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
502 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
504 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
505 $not_fuzzy_match = 1;
509 $fuzzy_match = 1 if !$not_fuzzy_match;
511 $details .= "Differences in `diff -u' format:\n";
512 $details .= join ('', @diff);
513 $details .= "(This is considered a `fuzzy match'.)\n" if $fuzzy_match;
516 $details{$test} = $details;
517 die "Output differs from expected. Details at end of file.\n"
522 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
527 for (my ($i) = 0; $i <= $#summary; $i++) {
528 local ($_) = $summary[$i];
529 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
530 my ($result) = $result{$test} || "Not tested.";
532 if ($result eq 'ok') {
533 splice (@summary, $i, 1);
538 splice (@summary, $i + 1, 0,
539 map (" $_", split ("\n", $result)));
541 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
543 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
544 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
549 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
550 $summary[0] =~ s/\[\[total\]\]/$ts/;
552 open (SUMMARY, ">tests.out");
553 print SUMMARY map ("$_\n", @summary);
558 open (DETAILS, ">details.out");
560 for my $test (@TESTS) {
561 next if $result{$test} eq 'ok' && !defined $details{$test};
563 my ($details) = $details{$test};
564 next if !defined ($details) && ! -e "output/$test/run.out";
566 print DETAILS "\n" if $n++;
567 print DETAILS "--- $test details ", '-' x (50 - length ($test));
568 print DETAILS "\n\n";
570 if (!defined $details) {
571 $details = "Output:\n\n" . snarf ("output/$test/run.out");
573 print DETAILS $details;
575 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
576 if defined $extra{$test};
583 my ($command, %options) = @_;
584 print "$command\n" if $VERBOSE || $options{VERBOSE};
586 my ($log) = $options{LOG};
587 if (defined ($log)) {
588 $command = "($command) >output/$log.out 2>output/$log.err";
593 local $SIG{ALRM} = sub { die "alarm\n" };
594 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
596 die "fork: $!\n" if !defined $pid;
597 exec ($command), die "$command: exec: $!\n" if !$pid;
603 die unless $@ eq "alarm\n"; # propagate unexpected errors
604 print "Timed out.\n";
609 if (WIFSIGNALED ($status)) {
610 my ($signal) = WTERMSIG ($status);
611 die "Interrupted\n" if $signal == SIGINT;
612 print "Child terminated with signal $signal\n";
615 unlink ("output/$log.err") if defined ($log) && $status == 0;
617 die $options{DIE} if $status != 0 && defined $options{DIE};
624 open (OUTPUT, $file) or die "$file: open: $!\n";
625 my (@lines) = <OUTPUT>;
628 return wantarray ? @lines : join ('', map ("$_\n", @lines));
634 open (A, "<$a") or die "$a: open: $!\n";
635 open (B, "<$b") or die "$b: open: $!\n";
641 sysread (A, $sa, 1024);
642 sysread (B, $sb, 1024);
643 $equal = 0, last if $sa ne $sb;
644 $equal = 1, last if $sa eq '';
653 my ($file, $expected) = @_;
654 open (FILE, "<$file") or die "$file: open: $!\n";
656 sysread (FILE, $actual, -s FILE);
657 my ($equal) = $actual eq $expected;
663 my ($ln, $lines) = @_;
665 for my $line (@$lines) {
667 $out .= sprintf "%4d %s\n", $ln++, $line;