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
56 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
57 $args{$key} = "some arguments for you!";
59 $args{'args-single'} = "onearg";
60 $args{'args-dbl-space'} = "two args";
62 # Handle final grade mode.
64 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
66 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
73 my (@tests) = snarf ("tests.out");
74 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
76 my (@review) = snarf ("review.txt");
77 my ($part_lost) = (0, 0);
78 for (my ($i) = $#review; $i >= 0; $i--) {
79 local ($_) = $review[$i];
80 if (my ($loss) = /^\s*([-+]\d+)/) {
82 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
83 my ($got) = $out_of + $part_lost;
85 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
92 die "Lost points outside a section\n" if $part_lost;
94 for (my ($i) = 1; $i <= $#review; $i++) {
95 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
96 $review[$i] = '-' x (length ($review[$i - 1]));
100 print OUT "\nOVERALL SCORE\n";
101 print OUT "-------------\n";
102 print OUT "$p_got points out of $p_pos total\n\n";
104 print OUT map ("$_\n", @tests), "\n";
105 print OUT map ("$_\n", @review), "\n";
107 print OUT "DETAILS\n";
108 print OUT "-------\n\n";
109 print OUT map ("$_\n", snarf ("details.out"));
114 # Find the directory that contains the grading files.
116 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
117 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
120 # Verify that we're roughly in the correct directory
121 # before we go blasting away files.
124 xsystem ("rm -rf output pintos", VERBOSE => 1);
125 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
128 # Create output directory, if it doesn't already exist.
129 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
131 # Extract submission.
132 extract_tarball () if ! -d "pintos";
134 # Compile submission.
137 # Verify that the proper directory was submitted.
138 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
140 # Run and grade the tests.
147 my ($result) = run_test ($test);
148 if ($result eq 'ok') {
149 $result = grade_test ($test);
153 print " - with warnings" if $result eq 'ok' && defined $details{$test};
156 $result{$test} = $result;
165 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
167 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
169 # Sort tarballs in reverse order by time.
170 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
172 print "Multiple tarballs: choosing $tarballs[0]\n"
173 if scalar (@tarballs) > 1;
177 sub extract_tarball {
178 my ($tarball) = choose_tarball ();
180 mkdir "pintos" or die "pintos: mkdir: $!\n";
181 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
183 print "Extracting $tarball...\n";
184 xsystem ("cd pintos/src && tar xzf ../../$tarball",
185 DIE => "extraction failed\n");
187 print "Patching...\n";
188 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
190 DIE => "patch failed\n");
195 my ($ms, $d, $y, $H, $M, $S) =
196 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
198 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
200 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
205 my ($src) = "$GRADES_DIR/$test.c";
206 -e $src or die "$src: stat: $!\n";
218 # Reuse older results if any.
219 if (open (DONE, "<output/$test/done")) {
227 # Really run the test.
228 my ($status) = really_run_test ($test);
230 # Save the results for later.
231 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
232 print DONE "$status\n";
239 print "Compiling...\n";
240 xsystem ("cd pintos/src/userprog && make", LOG => "make")
241 or return "compile error";
244 sub really_run_test {
246 # If there's residue from an earlier test, move it to .old.
247 # If there's already a .old, delete it.
248 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
249 rename "output/$test", "output/$test.old" or die "rename: $!\n"
250 if -d "output/$test";
252 # Make output directory.
253 mkdir "output/$test";
254 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
255 DIE => "cp failed\n");
259 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
261 . "--os-disk=pintos/src/userprog/build/os.dsk "
262 . "--fs-disk=output/$test/fs.dsk "
263 . "-v run -q -ex \"$test$testargs\"",
266 or return "Bochs error";
274 my (@output) = snarf ("output/$test/run.out");
276 my ($grade_func) = "grade_$test";
277 $grade_func =~ s/-/_/g;
278 if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
280 verify_common (@output);
281 compare_output ("$GRADES_DIR/$test.exp", @output);
284 eval "$grade_func (\@output)";
287 die $@ if $@ =~ /at \S+ line \d+$/;
293 sub grade_write_normal {
295 verify_common (@output);
296 compare_output ("$GRADES_DIR/write-normal.exp", @output);
297 my ($test_txt) = "output/$test/test.txt";
298 get_file ("test.txt", $test_txt) if ! -e $test_txt;
299 compare_output ("$GRADES_DIR/sample.txt", snarf ($test_txt));
303 my ($guest_fn, $host_fn) = @_;
305 . "--os-disk=pintos/src/userprog/build/os.dsk "
306 . "--fs-disk=output/$test/fs.dsk "
307 . "-v get $guest_fn $host_fn",
308 LOG => "$test/get-$guest_fn",
310 or die "get $guest_fn failed\n";
313 sub grade_alarm_negative {
315 verify_common (@output);
316 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
319 sub grade_join_invalid {
321 verify_common (@output);
322 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
323 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
328 verify_common (@output);
329 grep (/Testing no join/, @output) or die "Test didn't start\n";
330 grep (/No join test done/, @output) or die "Test didn't complete\n";
333 sub grade_join_multiple {
336 verify_common (@output);
338 $t[4] = $t[5] = $t[6] = -1;
341 my ($idx) = /^Thread (\d+)/ or next;
342 my ($iter) = /iteration (\d+)$/;
343 $iter = 5 if /done!$/;
344 die "Malformed output\n" if !defined $iter;
346 die "Thread 6 started before either other thread finished\n"
347 if $t[4] < 5 && $t[5] < 5;
348 die "Thread 6 started before thread 4 finished\n"
350 die "Thread 6 started before thread 5 finished\n"
353 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
358 for my $idx (4, 5, 6) {
359 if ($t[$idx] == -1) {
360 $err .= "Thread $idx did not run at all\n";
361 } elsif ($t[$idx] != 5) {
362 $err .= "Thread $idx only completed $t[$idx] iterations\n";
365 die $err if $err ne '';
368 sub grade_priority_fifo {
371 verify_common (@output);
372 my ($thread_cnt) = 10;
375 my (@t) = (-1) x $thread_cnt;
378 my ($idx) = /^Thread (\d+)/ or next;
379 my ($iter) = /iteration (\d+)$/;
380 $iter = $iter_cnt if /done!$/;
381 die "Malformed output\n" if !defined $iter;
382 if (@order < $thread_cnt) {
384 die "Thread $idx repeated within first $thread_cnt iterations: "
385 . join (' ', @order) . ".\n"
386 if grep ($_ == $idx, @order) != 1;
388 die "Thread $idx ran when $order[0] should have.\n"
389 if $idx != $order[0];
390 push (@order, shift @order);
392 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
397 for my $idx (0..$#t) {
398 if ($t[$idx] == -1) {
399 $err .= "Thread $idx did not run at all.\n";
400 } elsif ($t[$idx] != $iter_cnt) {
401 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
404 die $err if $err ne '';
409 verify_common (@output);
410 our (@mlfqs_on_stats) = mlfqs_stats (@output);
413 sub grade_mlfqs_off {
415 verify_common (@output);
416 our (@mlfqs_off_stats) = mlfqs_stats (@output);
419 sub grade_mlfqs_speedup {
420 our (@mlfqs_off_stats);
421 our (@mlfqs_on_stats);
424 my ($off_ticks) = $mlfqs_off_stats[1];
425 my ($on_ticks) = $mlfqs_on_stats[1];
426 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
427 if $on_ticks >= $off_ticks;
431 $result{'mlfqs-speedup'} = $@;
434 sub grade_mlfqs_priority {
435 our (@mlfqs_off_stats);
436 our (@mlfqs_on_stats);
438 check_mlfqs () if !defined (@mlfqs_on_stats);
439 for my $cat qw (CPU IO MIX) {
440 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
441 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
442 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
443 die "Minimum priority never changed from PRI_DEFAULT (29) "
445 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
446 die "Maximum priority never changed from PRI_DEFAULT (29) "
448 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
453 $result{'mlfqs-priority'} = $@;
457 our (@mlfqs_off_stats);
458 our (@mlfqs_on_stats);
459 die "p1-4 didn't finish with MLFQS on or off\n"
460 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
461 die "p1-4 didn't finish with MLFQS on\n"
462 if !defined (@mlfqs_on_stats);
463 die "p1-4 didn't finish with MLFQS off\n"
464 if !defined (@mlfqs_off_stats);
469 my (%stats) = (CPU => {}, IO => {}, MIX => {});
470 my (%map) = ("CPU intensive" => 'CPU',
471 "IO intensive" => 'IO',
472 "Alternating IO/CPU" => 'MIX');
473 my (%rmap) = reverse %map;
477 $ticks = $1 if /Timer: (\d+) ticks/;
478 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
479 my ($t) = $map{$thread} or next;
481 my ($s) = $stats{$t};
484 $$s{SUM2} += $pri * $pri;
485 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
486 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
489 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
490 for my $cat (values (%map)) {
491 my ($s) = $stats{$cat};
492 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
493 if $$s{N} != $expect_n{$cat};
494 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
496 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
498 $$s{MEAN} = $$s{SUM} / $$s{N};
501 return (\%stats, $ticks);
507 my (@assertion) = grep (/PANIC/, @output);
508 if (@assertion != 0) {
509 my ($details) = "Kernel panic:\n $assertion[0]\n";
511 my (@stack_line) = grep (/Call stack:/, @output);
512 if (@stack_line != 0) {
513 $details .= " $stack_line[0]\n\n";
514 $details .= "Translation of backtrace:\n";
515 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
519 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
522 $A2L = "i386-elf-addr2line";
524 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
526 my ($function, $line);
527 last unless defined ($function = <A2L>);
531 $details .= " $function ($line)\n";
534 $extra{$test} = $details;
535 die "Kernel panic. Details at end of file.\n"
538 die "No output at all\n" if @output == 0;
539 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
540 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
541 die "Didn't start up properly: no \"Boot complete\" startup message\n"
542 if !grep (/Boot complete/, @output);
543 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
544 if !grep (/Timer: \d+ ticks/, @output);
545 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
546 if !grep (/Powering off/, @output);
549 # Get @output without header or trailer.
550 sub get_core_output {
555 for ($first = 0; $first <= $#output; $first++) {
556 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
560 for ($last = $#output; $last >= 0; $last--) {
561 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
564 if ($last < $first) {
565 my ($no_first) = $first > $#output;
566 my ($no_last) = $last < $#output;
567 die "Couldn't locate output.\n";
570 return @output[$first ... $last];
574 my ($exp, @actual) = @_;
575 @actual = get_core_output (map ("$_\n", @actual));
577 # Fix up lines that look like exit codes.
578 for my $i (0...$#actual) {
579 if (my ($process, $code)
580 = $actual[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
581 $process = substr ($process, 0, 15);
582 $process =~ s/\s.*//;
583 $actual[$i] = "$process: exit($code)\n";
588 $details .= "$test actual output:\n";
589 $details .= join ('', map (" $_", @actual));
591 my (@exp) = map ("$_\n", snarf ($exp));
593 my ($fuzzy_match) = 0;
597 my ($s) = shift (@exp);
598 last if $s eq "--OR--\n";
599 push (@expected, $s);
602 $details .= "\n$test acceptable output:\n";
603 $details .= join ('', map (" $_", @expected));
605 # Check whether they're the same.
606 if ($#actual == $#expected) {
608 for (my ($i) = 0; $i <= $#expected; $i++) {
609 $eq = 0 if $actual[$i] ne $expected[$i];
614 # They differ. Output a diff.
616 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
617 my ($not_fuzzy_match) = 0;
618 while ($d->Next ()) {
619 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
621 push (@diff, map (" $_", $d->Items (1)));
623 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
624 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
626 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
627 $not_fuzzy_match = 1;
631 $fuzzy_match = 1 if !$not_fuzzy_match;
633 $details .= "Differences in `diff -u' format:\n";
634 $details .= join ('', @diff);
635 $details .= "(This is considered a `fuzzy match'.)\n" if $fuzzy_match;
638 $details{$test} = $details;
639 die "Output differs from expected. Details at end of file.\n"
644 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
649 for (my ($i) = 0; $i <= $#summary; $i++) {
650 local ($_) = $summary[$i];
651 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
652 my ($result) = $result{$test} || "Not tested.";
654 if ($result eq 'ok') {
655 splice (@summary, $i, 1);
660 splice (@summary, $i + 1, 0,
661 map (" $_", split ("\n", $result)));
663 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
665 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
666 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
671 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
672 $summary[0] =~ s/\[\[total\]\]/$ts/;
674 open (SUMMARY, ">tests.out");
675 print SUMMARY map ("$_\n", @summary);
680 open (DETAILS, ">details.out");
682 for my $test (@TESTS) {
683 next if $result{$test} eq 'ok' && !defined $details{$test};
685 my ($details) = $details{$test};
686 next if !defined ($details) && ! -e "output/$test/run.out";
688 print DETAILS "\n" if $n++;
689 print DETAILS "--- $test details ", '-' x (50 - length ($test));
690 print DETAILS "\n\n";
692 if (!defined $details) {
693 $details = "Output:\n\n" . snarf ("output/$test/run.out");
695 print DETAILS $details;
697 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
698 if defined $extra{$test};
705 my ($command, %options) = @_;
706 print "$command\n" if $VERBOSE || $options{VERBOSE};
708 my ($log) = $options{LOG};
709 if (defined ($log)) {
710 $command = "($command) >output/$log.out 2>output/$log.err";
715 local $SIG{ALRM} = sub { die "alarm\n" };
716 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
718 die "fork: $!\n" if !defined $pid;
719 exec ($command), die "$command: exec: $!\n" if !$pid;
725 die unless $@ eq "alarm\n"; # propagate unexpected errors
726 print "Timed out.\n";
731 if (WIFSIGNALED ($status)) {
732 my ($signal) = WTERMSIG ($status);
733 die "Interrupted\n" if $signal == SIGINT;
734 print "Child terminated with signal $signal\n";
737 unlink ("output/$log.err") if defined ($log) && $status == 0;
739 die $options{DIE} if $status != 0 && defined $options{DIE};
746 open (OUTPUT, $file) or die "$file: open: $!\n";
747 my (@lines) = <OUTPUT>;
750 return wantarray ? @lines : join ('', map ("$_\n", @lines));
756 open (A, "<$a") or die "$a: open: $!\n";
757 open (B, "<$b") or die "$b: open: $!\n";
763 sysread (A, $sa, 1024);
764 sysread (B, $sb, 1024);
765 $equal = 0, last if $sa ne $sb;
766 $equal = 1, last if $sa eq '';
775 my ($file, $expected) = @_;
776 open (FILE, "<$file") or die "$file: open: $!\n";
778 sysread (FILE, $actual, -s FILE);
779 my ($equal) = $actual eq $expected;
785 my ($ln, $lines) = @_;
787 for my $line (@$lines) {
789 $out .= sprintf "%4d %s\n", $ln++, $line;