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
57 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
58 $args{$key} = "some arguments for you!";
60 $args{'args-single'} = "onearg";
61 $args{'args-dbl-space'} = "two args";
63 # Handle final grade mode.
65 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
67 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
74 my (@tests) = snarf ("tests.out");
75 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
77 my (@review) = snarf ("review.txt");
78 my ($part_lost) = (0, 0);
79 for (my ($i) = $#review; $i >= 0; $i--) {
80 local ($_) = $review[$i];
81 if (my ($loss) = /^\s*([-+]\d+)/) {
83 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
84 my ($got) = $out_of + $part_lost;
86 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
93 die "Lost points outside a section\n" if $part_lost;
95 for (my ($i) = 1; $i <= $#review; $i++) {
96 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
97 $review[$i] = '-' x (length ($review[$i - 1]));
101 print OUT "\nOVERALL SCORE\n";
102 print OUT "-------------\n";
103 print OUT "$p_got points out of $p_pos total\n\n";
105 print OUT map ("$_\n", @tests), "\n";
106 print OUT map ("$_\n", @review), "\n";
108 print OUT "DETAILS\n";
109 print OUT "-------\n\n";
110 print OUT map ("$_\n", snarf ("details.out"));
115 # Find the directory that contains the grading files.
117 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
118 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
121 # Verify that we're roughly in the correct directory
122 # before we go blasting away files.
125 xsystem ("rm -rf output pintos", VERBOSE => 1);
126 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
129 # Create output directory, if it doesn't already exist.
130 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
132 # Extract submission.
133 extract_tarball () if ! -d "pintos";
135 # Compile submission.
138 # Verify that the proper directory was submitted.
139 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
141 # Run and grade the tests.
148 my ($result) = run_test ($test);
149 if ($result eq 'ok') {
150 $result = grade_test ($test);
154 print " - with warnings" if $result eq 'ok' && defined $details{$test};
157 $result{$test} = $result;
166 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
168 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
170 # Sort tarballs in reverse order by time.
171 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
173 print "Multiple tarballs: choosing $tarballs[0]\n"
174 if scalar (@tarballs) > 1;
178 sub extract_tarball {
179 my ($tarball) = choose_tarball ();
181 mkdir "pintos" or die "pintos: mkdir: $!\n";
182 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
184 print "Extracting $tarball...\n";
185 xsystem ("cd pintos/src && tar xzf ../../$tarball",
186 DIE => "extraction failed\n");
188 print "Patching...\n";
189 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
191 DIE => "patch failed\n");
196 my ($ms, $d, $y, $H, $M, $S) =
197 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
199 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
201 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
206 my ($src) = "$GRADES_DIR/$test.c";
207 -e $src or die "$src: stat: $!\n";
219 # Reuse older results if any.
220 if (open (DONE, "<output/$test/done")) {
228 # Really run the test.
229 my ($status) = really_run_test ($test);
231 # Save the results for later.
232 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
233 print DONE "$status\n";
240 print "Compiling...\n";
241 xsystem ("cd pintos/src/userprog && make", LOG => "make")
242 or return "compile error";
245 sub really_run_test {
247 # If there's residue from an earlier test, move it to .old.
248 # If there's already a .old, delete it.
249 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
250 rename "output/$test", "output/$test.old" or die "rename: $!\n"
251 if -d "output/$test";
253 # Make output directory.
254 mkdir "output/$test";
255 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
256 DIE => "cp failed\n");
260 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
262 . "--os-disk=pintos/src/userprog/build/os.dsk "
263 . "--fs-disk=output/$test/fs.dsk "
264 . "-v run -q -ex \"$test$testargs\"",
267 or return "Bochs error";
275 my (@output) = snarf ("output/$test/run.out");
277 my ($grade_func) = "grade_$test";
278 $grade_func =~ s/-/_/g;
279 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
281 verify_common (@output);
282 compare_output ("$GRADES_DIR/$test.exp", @output);
285 eval "$grade_func (\@output)";
288 die $@ if $@ =~ /at \S+ line \d+$/;
294 sub grade_write_normal {
296 verify_common (@output);
297 compare_output ("$GRADES_DIR/write-normal.exp", @output);
298 my ($test_txt) = "output/$test/test.txt";
299 get_file ("test.txt", $test_txt) if ! -e $test_txt;
301 my (@actual) = snarf ($test_txt);
302 my (@expected) = snarf ("$GRADES_DIR/sample.txt");
305 if ($#actual == $#expected) {
307 for my $i (0...$#actual) {
308 $eq = 0 if $actual[$i] ne $expected[$i];
315 $details = "Expected file content:\n";
316 $details .= join ('', map (" $_\n", @expected));
317 $details .= "Actual file content:\n";
318 $details .= join ('', map (" $_\n", @actual));
319 $extra{$test} = $details;
321 die "File written didn't have expected content.\n";
326 my ($guest_fn, $host_fn) = @_;
328 . "--os-disk=pintos/src/userprog/build/os.dsk "
329 . "--fs-disk=output/$test/fs.dsk "
330 . "-v get $guest_fn $host_fn",
331 LOG => "$test/get-$guest_fn",
333 or die "get $guest_fn failed\n";
336 sub grade_alarm_negative {
338 verify_common (@output);
339 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
342 sub grade_join_invalid {
344 verify_common (@output);
345 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
346 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
351 verify_common (@output);
352 grep (/Testing no join/, @output) or die "Test didn't start\n";
353 grep (/No join test done/, @output) or die "Test didn't complete\n";
356 sub grade_join_multiple {
359 verify_common (@output);
361 $t[4] = $t[5] = $t[6] = -1;
364 my ($idx) = /^Thread (\d+)/ or next;
365 my ($iter) = /iteration (\d+)$/;
366 $iter = 5 if /done!$/;
367 die "Malformed output\n" if !defined $iter;
369 die "Thread 6 started before either other thread finished\n"
370 if $t[4] < 5 && $t[5] < 5;
371 die "Thread 6 started before thread 4 finished\n"
373 die "Thread 6 started before thread 5 finished\n"
376 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
381 for my $idx (4, 5, 6) {
382 if ($t[$idx] == -1) {
383 $err .= "Thread $idx did not run at all\n";
384 } elsif ($t[$idx] != 5) {
385 $err .= "Thread $idx only completed $t[$idx] iterations\n";
388 die $err if $err ne '';
391 sub grade_priority_fifo {
394 verify_common (@output);
395 my ($thread_cnt) = 10;
398 my (@t) = (-1) x $thread_cnt;
401 my ($idx) = /^Thread (\d+)/ or next;
402 my ($iter) = /iteration (\d+)$/;
403 $iter = $iter_cnt if /done!$/;
404 die "Malformed output\n" if !defined $iter;
405 if (@order < $thread_cnt) {
407 die "Thread $idx repeated within first $thread_cnt iterations: "
408 . join (' ', @order) . ".\n"
409 if grep ($_ == $idx, @order) != 1;
411 die "Thread $idx ran when $order[0] should have.\n"
412 if $idx != $order[0];
413 push (@order, shift @order);
415 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
420 for my $idx (0..$#t) {
421 if ($t[$idx] == -1) {
422 $err .= "Thread $idx did not run at all.\n";
423 } elsif ($t[$idx] != $iter_cnt) {
424 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
427 die $err if $err ne '';
432 verify_common (@output);
433 our (@mlfqs_on_stats) = mlfqs_stats (@output);
436 sub grade_mlfqs_off {
438 verify_common (@output);
439 our (@mlfqs_off_stats) = mlfqs_stats (@output);
442 sub grade_mlfqs_speedup {
443 our (@mlfqs_off_stats);
444 our (@mlfqs_on_stats);
447 my ($off_ticks) = $mlfqs_off_stats[1];
448 my ($on_ticks) = $mlfqs_on_stats[1];
449 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
450 if $on_ticks >= $off_ticks;
454 $result{'mlfqs-speedup'} = $@;
457 sub grade_mlfqs_priority {
458 our (@mlfqs_off_stats);
459 our (@mlfqs_on_stats);
461 check_mlfqs () if !defined (@mlfqs_on_stats);
462 for my $cat qw (CPU IO MIX) {
463 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
464 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
465 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
466 die "Minimum priority never changed from PRI_DEFAULT (29) "
468 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
469 die "Maximum priority never changed from PRI_DEFAULT (29) "
471 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
476 $result{'mlfqs-priority'} = $@;
480 our (@mlfqs_off_stats);
481 our (@mlfqs_on_stats);
482 die "p1-4 didn't finish with MLFQS on or off\n"
483 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
484 die "p1-4 didn't finish with MLFQS on\n"
485 if !defined (@mlfqs_on_stats);
486 die "p1-4 didn't finish with MLFQS off\n"
487 if !defined (@mlfqs_off_stats);
492 my (%stats) = (CPU => {}, IO => {}, MIX => {});
493 my (%map) = ("CPU intensive" => 'CPU',
494 "IO intensive" => 'IO',
495 "Alternating IO/CPU" => 'MIX');
496 my (%rmap) = reverse %map;
500 $ticks = $1 if /Timer: (\d+) ticks/;
501 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
502 my ($t) = $map{$thread} or next;
504 my ($s) = $stats{$t};
507 $$s{SUM2} += $pri * $pri;
508 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
509 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
512 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
513 for my $cat (values (%map)) {
514 my ($s) = $stats{$cat};
515 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
516 if $$s{N} != $expect_n{$cat};
517 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
519 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
521 $$s{MEAN} = $$s{SUM} / $$s{N};
524 return (\%stats, $ticks);
530 my (@assertion) = grep (/PANIC/, @output);
531 if (@assertion != 0) {
532 my ($details) = "Kernel panic:\n $assertion[0]\n";
534 my (@stack_line) = grep (/Call stack:/, @output);
535 if (@stack_line != 0) {
536 $details .= " $stack_line[0]\n\n";
537 $details .= "Translation of backtrace:\n";
538 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
542 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
545 $A2L = "i386-elf-addr2line";
547 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
549 my ($function, $line);
550 last unless defined ($function = <A2L>);
554 $details .= " $function ($line)\n";
557 $extra{$test} = $details;
558 die "Kernel panic. Details at end of file.\n"
561 die "No output at all\n" if @output == 0;
562 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
563 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
564 die "Didn't start up properly: no \"Boot complete\" startup message\n"
565 if !grep (/Boot complete/, @output);
566 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
567 if !grep (/Timer: \d+ ticks/, @output);
568 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
569 if !grep (/Powering off/, @output);
572 # Get @output without header or trailer.
573 sub get_core_output {
578 for ($first = 0; $first <= $#output; $first++) {
579 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
583 for ($last = $#output; $last >= 0; $last--) {
584 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
587 if ($last < $first) {
588 my ($no_first) = $first > $#output;
589 my ($no_last) = $last < $#output;
590 die "Couldn't locate output.\n";
593 return @output[$first ... $last];
597 my ($exp, @actual) = @_;
598 @actual = get_core_output (map ("$_\n", @actual));
600 # Fix up lines that look like exit codes.
601 for my $i (0...$#actual) {
602 if (my ($process, $code)
603 = $actual[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
604 $process = substr ($process, 0, 15);
605 $process =~ s/\s.*//;
606 $actual[$i] = "$process: exit($code)\n";
611 $details .= "$test actual output:\n";
612 $details .= join ('', map (" $_", @actual));
614 my (@exp) = map ("$_\n", snarf ($exp));
616 my ($fuzzy_match) = 0;
620 my ($s) = shift (@exp);
621 last if $s eq "--OR--\n";
622 push (@expected, $s);
625 $details .= "\n$test acceptable output:\n";
626 $details .= join ('', map (" $_", @expected));
628 # Check whether they're the same.
629 if ($#actual == $#expected) {
631 for (my ($i) = 0; $i <= $#expected; $i++) {
632 $eq = 0 if $actual[$i] ne $expected[$i];
637 # They differ. Output a diff.
639 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
640 my ($not_fuzzy_match) = 0;
641 while ($d->Next ()) {
642 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
644 push (@diff, map (" $_", $d->Items (1)));
646 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
647 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
649 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
650 $not_fuzzy_match = 1;
654 $fuzzy_match = 1 if !$not_fuzzy_match;
656 $details .= "Differences in `diff -u' format:\n";
657 $details .= join ('', @diff);
658 $details .= "(This is considered a `fuzzy match'.)\n" if $fuzzy_match;
661 $details{$test} = $details;
662 die "Output differs from expected. Details at end of file.\n"
667 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
672 for (my ($i) = 0; $i <= $#summary; $i++) {
673 local ($_) = $summary[$i];
674 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
675 my ($result) = $result{$test} || "Not tested.";
677 if ($result eq 'ok') {
678 splice (@summary, $i, 1);
683 splice (@summary, $i + 1, 0,
684 map (" $_", split ("\n", $result)));
686 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
688 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
689 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
694 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
695 $summary[0] =~ s/\[\[total\]\]/$ts/;
697 open (SUMMARY, ">tests.out");
698 print SUMMARY map ("$_\n", @summary);
703 open (DETAILS, ">details.out");
705 for my $test (@TESTS) {
706 next if $result{$test} eq 'ok' && !defined $details{$test};
708 my ($details) = $details{$test};
709 next if !defined ($details) && ! -e "output/$test/run.out";
711 print DETAILS "\n" if $n++;
712 print DETAILS "--- $test details ", '-' x (50 - length ($test));
713 print DETAILS "\n\n";
715 if (!defined $details) {
716 $details = "Output:\n\n" . snarf ("output/$test/run.out");
718 print DETAILS $details;
720 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
721 if defined $extra{$test};
728 my ($command, %options) = @_;
729 print "$command\n" if $VERBOSE || $options{VERBOSE};
731 my ($log) = $options{LOG};
732 if (defined ($log)) {
733 $command = "($command) >output/$log.out 2>output/$log.err";
738 local $SIG{ALRM} = sub { die "alarm\n" };
739 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
741 die "fork: $!\n" if !defined $pid;
742 exec ($command), die "$command: exec: $!\n" if !$pid;
748 die unless $@ eq "alarm\n"; # propagate unexpected errors
749 print "Timed out.\n";
754 if (WIFSIGNALED ($status)) {
755 my ($signal) = WTERMSIG ($status);
756 die "Interrupted\n" if $signal == SIGINT;
757 print "Child terminated with signal $signal\n";
760 unlink ("output/$log.err") if defined ($log) && $status == 0;
762 die $options{DIE} if $status != 0 && defined $options{DIE};
769 open (OUTPUT, $file) or die "$file: open: $!\n";
770 my (@lines) = <OUTPUT>;
773 return wantarray ? @lines : join ('', map ("$_\n", @lines));
779 open (A, "<$a") or die "$a: open: $!\n";
780 open (B, "<$b") or die "$b: open: $!\n";
786 sysread (A, $sa, 1024);
787 sysread (B, $sb, 1024);
788 $equal = 0, last if $sa ne $sb;
789 $equal = 1, last if $sa eq '';
798 my ($file, $expected) = @_;
799 open (FILE, "<$file") or die "$file: open: $!\n";
801 sysread (FILE, $actual, -s FILE);
802 my ($equal) = $actual eq $expected;
808 my ($ln, $lines) = @_;
810 for my $line (@$lines) {
812 $out .= sprintf "%4d %s\n", $ln++, $line;