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
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");
199 my ($ms, $d, $y, $H, $M, $S) =
200 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
202 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
204 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
209 my ($src) = "$GRADES_DIR/$test.c";
210 -e $src or die "$src: stat: $!\n";
222 # Reuse older results if any.
223 if (open (DONE, "<output/$test/done")) {
231 # Really run the test.
232 my ($status) = really_run_test ($test);
234 # Save the results for later.
235 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
236 print DONE "$status\n";
243 print "Compiling...\n";
244 xsystem ("cd pintos/src/userprog && make", LOG => "make")
245 or return "compile error";
248 sub really_run_test {
250 # If there's residue from an earlier test, move it to .old.
251 # If there's already a .old, delete it.
252 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
253 rename "output/$test", "output/$test.old" or die "rename: $!\n"
254 if -d "output/$test";
256 # Make output directory.
257 mkdir "output/$test";
258 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
259 DIE => "cp failed\n");
263 $timeout = 60 if $test eq 'multi-oom';
264 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
266 . "--os-disk=pintos/src/userprog/build/os.dsk "
267 . "--fs-disk=output/$test/fs.dsk "
268 . "-v run -q -ex \"$test$testargs\"",
271 or return "Bochs error";
279 my (@output) = snarf ("output/$test/run.out");
281 my ($grade_func) = "grade_$test";
282 $grade_func =~ s/-/_/g;
283 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
285 verify_common (@output);
286 compare_output ("$GRADES_DIR/$test.exp", @output);
289 eval "$grade_func (\@output)";
292 die $@ if $@ =~ /at \S+ line \d+$/;
298 sub grade_write_normal {
300 verify_common (@output);
301 compare_output ("$GRADES_DIR/write-normal.exp", @output);
302 my ($test_txt) = "output/$test/test.txt";
303 get_file ("test.txt", $test_txt) if ! -e $test_txt;
305 my (@actual) = snarf ($test_txt);
306 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
309 if ($#actual == $#expected) {
311 for my $i (0...$#actual) {
312 $eq = 0 if $actual[$i] ne $expected[$i];
319 $details = "Expected file content:\n";
320 $details .= join ('', map (" $_\n", @expected));
321 $details .= "Actual file content:\n";
322 $details .= join ('', map (" $_\n", @actual));
323 $extra{$test} = $details;
325 die "File written didn't have expected content.\n";
329 sub grade_multi_oom {
331 verify_common (@output);
333 @output = fix_exit_codes (get_core_output (@output));
335 while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
336 die "Child process $m started out of order.\n" if $m != $n;
340 die "Only $n child processes started.\n" if $n < 15;
342 die "Output ended unexpectedly before process $n finished.\n"
344 die "Child process $n ended out of order.\n"
345 if $output[0] !~ /^\(multi-oom\) end $n$/;
348 die "Child process $n didn't print proper exit message.\n"
349 if $output[0] !~ /^multi-oom: exit\($n\)$/;
352 die "Spurious output at end: '$output[0]'.\n" if @output;
356 my ($guest_fn, $host_fn) = @_;
358 . "--os-disk=pintos/src/userprog/build/os.dsk "
359 . "--fs-disk=output/$test/fs.dsk "
360 . "-v get $guest_fn $host_fn",
361 LOG => "$test/get-$guest_fn",
363 or die "get $guest_fn failed\n";
370 my (@assertion) = grep (/PANIC/, @output);
371 if (@assertion != 0) {
372 my ($details) = "Kernel panic:\n $assertion[0]\n";
374 my (@stack_line) = grep (/Call stack:/, @output);
375 if (@stack_line != 0) {
376 $details .= " $stack_line[0]\n\n";
377 $details .= "Translation of backtrace:\n";
378 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
382 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
385 $A2L = "i386-elf-addr2line";
387 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
389 my ($function, $line);
390 last unless defined ($function = <A2L>);
394 $details .= " $function ($line)\n";
397 $extra{$test} = $details;
398 die "Kernel panic. Details at end of file.\n"
401 die "No output at all\n" if @output == 0;
402 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
403 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
404 die "Didn't start up properly: no \"Boot complete\" startup message\n"
405 if !grep (/Boot complete/, @output);
406 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
407 if !grep (/Timer: \d+ ticks/, @output);
408 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
409 if !grep (/Powering off/, @output);
412 # Get @output without header or trailer.
413 sub get_core_output {
418 for ($first = 0; $first <= $#output; $first++) {
419 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
423 for ($last = $#output; $last >= 0; $last--) {
424 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
427 if ($last < $first) {
428 my ($no_first) = $first > $#output;
429 my ($no_last) = $last < $#output;
430 die "Couldn't locate output.\n";
433 return @output[$first ... $last];
439 # Fix up lines that look like exit codes.
440 for my $i (0...$#output) {
441 if (my ($process, $code)
442 = $output[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
443 $process = substr ($process, 0, 15);
444 $process =~ s/\s.*//;
445 $output[$i] = "$process: exit($code)\n";
453 my ($exp, @actual) = @_;
454 @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
457 $details .= "$test actual output:\n";
458 $details .= join ('', map (" $_", @actual));
460 my (@exp) = map ("$_\n", snarf ($exp));
462 my ($fuzzy_match) = 0;
466 my ($s) = shift (@exp);
467 last if $s eq "--OR--\n";
468 push (@expected, $s);
471 $details .= "\n$test acceptable output:\n";
472 $details .= join ('', map (" $_", @expected));
474 # Check whether they're the same.
475 if ($#actual == $#expected) {
477 for (my ($i) = 0; $i <= $#expected; $i++) {
478 $eq = 0 if $actual[$i] ne $expected[$i];
483 # They differ. Output a diff.
485 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
486 my ($not_fuzzy_match) = 0;
487 while ($d->Next ()) {
488 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
490 push (@diff, map (" $_", $d->Items (1)));
492 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
493 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
495 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
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" if $fuzzy_match;
507 $details{$test} = $details;
508 die "Output differs from expected. Details at end of file.\n"
513 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
518 for (my ($i) = 0; $i <= $#summary; $i++) {
519 local ($_) = $summary[$i];
520 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
521 my ($result) = $result{$test} || "Not tested.";
523 if ($result eq 'ok') {
524 splice (@summary, $i, 1);
529 splice (@summary, $i + 1, 0,
530 map (" $_", split ("\n", $result)));
532 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
534 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
535 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
540 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
541 $summary[0] =~ s/\[\[total\]\]/$ts/;
543 open (SUMMARY, ">tests.out");
544 print SUMMARY map ("$_\n", @summary);
549 open (DETAILS, ">details.out");
551 for my $test (@TESTS) {
552 next if $result{$test} eq 'ok' && !defined $details{$test};
554 my ($details) = $details{$test};
555 next if !defined ($details) && ! -e "output/$test/run.out";
557 print DETAILS "\n" if $n++;
558 print DETAILS "--- $test details ", '-' x (50 - length ($test));
559 print DETAILS "\n\n";
561 if (!defined $details) {
562 $details = "Output:\n\n" . snarf ("output/$test/run.out");
564 print DETAILS $details;
566 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
567 if defined $extra{$test};
574 my ($command, %options) = @_;
575 print "$command\n" if $VERBOSE || $options{VERBOSE};
577 my ($log) = $options{LOG};
578 if (defined ($log)) {
579 $command = "($command) >output/$log.out 2>output/$log.err";
584 local $SIG{ALRM} = sub { die "alarm\n" };
585 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
587 die "fork: $!\n" if !defined $pid;
588 exec ($command), die "$command: exec: $!\n" if !$pid;
594 die unless $@ eq "alarm\n"; # propagate unexpected errors
595 print "Timed out.\n";
600 if (WIFSIGNALED ($status)) {
601 my ($signal) = WTERMSIG ($status);
602 die "Interrupted\n" if $signal == SIGINT;
603 print "Child terminated with signal $signal\n";
606 unlink ("output/$log.err") if defined ($log) && $status == 0;
608 die $options{DIE} if $status != 0 && defined $options{DIE};
615 open (OUTPUT, $file) or die "$file: open: $!\n";
616 my (@lines) = <OUTPUT>;
619 return wantarray ? @lines : join ('', map ("$_\n", @lines));
625 open (A, "<$a") or die "$a: open: $!\n";
626 open (B, "<$b") or die "$b: open: $!\n";
632 sysread (A, $sa, 1024);
633 sysread (B, $sb, 1024);
634 $equal = 0, last if $sa ne $sb;
635 $equal = 1, last if $sa eq '';
644 my ($file, $expected) = @_;
645 open (FILE, "<$file") or die "$file: open: $!\n";
647 sysread (FILE, $actual, -s FILE);
648 my ($equal) = $actual eq $expected;
654 my ($ln, $lines) = @_;
656 for my $line (@$lines) {
658 $out .= sprintf "%4d %s\n", $ln++, $line;