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
54 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
55 $args{$key} = "some arguments for you!";
57 $args{'args-single'} = "onearg";
58 $args{'args-dbl-space'} = "two args";
60 # Handle final grade mode.
62 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
64 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
71 my (@tests) = snarf ("tests.out");
72 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
74 my (@review) = snarf ("review.txt");
75 my ($part_lost) = (0, 0);
76 for (my ($i) = $#review; $i >= 0; $i--) {
77 local ($_) = $review[$i];
78 if (my ($loss) = /^\s*([-+]\d+)/) {
80 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
81 my ($got) = $out_of + $part_lost;
83 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
90 die "Lost points outside a section\n" if $part_lost;
92 for (my ($i) = 1; $i <= $#review; $i++) {
93 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
94 $review[$i] = '-' x (length ($review[$i - 1]));
98 print OUT "\nOVERALL SCORE\n";
99 print OUT "-------------\n";
100 print OUT "$p_got points out of $p_pos total\n\n";
102 print OUT map ("$_\n", @tests), "\n";
103 print OUT map ("$_\n", @review), "\n";
105 print OUT "DETAILS\n";
106 print OUT "-------\n\n";
107 print OUT map ("$_\n", snarf ("details.out"));
112 # Find the directory that contains the grading files.
114 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
115 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
118 # Verify that we're roughly in the correct directory
119 # before we go blasting away files.
122 xsystem ("rm -rf output pintos", VERBOSE => 1);
123 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
126 # Create output directory, if it doesn't already exist.
127 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
129 # Extract submission.
130 extract_tarball () if ! -d "pintos";
132 # Compile submission.
135 # Verify that the proper directory was submitted.
136 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
138 # Run and grade the tests.
145 my ($result) = run_test ($test);
146 if ($result eq 'ok') {
147 $result = grade_test ($test);
151 print " - with warnings" if $result eq 'ok' && defined $details{$test};
154 $result{$test} = $result;
163 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
165 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
167 # Sort tarballs in reverse order by time.
168 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
170 print "Multiple tarballs: choosing $tarballs[0]\n"
171 if scalar (@tarballs) > 1;
175 sub extract_tarball {
176 my ($tarball) = choose_tarball ();
178 mkdir "pintos" or die "pintos: mkdir: $!\n";
179 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
181 print "Extracting $tarball...\n";
182 xsystem ("cd pintos/src && tar xzf ../../$tarball",
183 DIE => "extraction failed\n");
185 print "Patching...\n";
186 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
188 DIE => "patch failed\n");
193 my ($ms, $d, $y, $H, $M, $S) =
194 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
196 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
198 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
203 my ($src) = "$GRADES_DIR/$test.c";
204 -e $src or die "$src: stat: $!\n";
216 # Reuse older results if any.
217 if (open (DONE, "<output/$test/done")) {
225 # Really run the test.
226 my ($status) = really_run_test ($test);
228 # Save the results for later.
229 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
230 print DONE "$status\n";
237 print "Compiling...\n";
238 xsystem ("cd pintos/src/userprog && make", LOG => "make")
239 or return "compile error";
242 sub really_run_test {
244 # If there's residue from an earlier test, move it to .old.
245 # If there's already a .old, delete it.
246 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
247 rename "output/$test", "output/$test.old" or die "rename: $!\n"
248 if -d "output/$test";
250 # Make output directory.
251 mkdir "output/$test";
252 xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
253 DIE => "cp failed\n");
257 my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
259 . "--os-disk=pintos/src/userprog/build/os.dsk "
260 . "--fs-disk=output/$test/fs.dsk "
261 . "-v run -q -ex \"$test$testargs\"",
264 or return "Bochs error";
272 my (@output) = snarf ("output/$test/run.out");
274 if (-e "$GRADES_DIR/$test.exp") {
276 verify_common (@output);
277 compare_output ("$GRADES_DIR/$test.exp", @output);
281 ($grade_func = $test) =~ s/-/_/g;
282 eval "grade_$grade_func (\@output)";
285 die $@ if $@ =~ /at \S+ line \d+$/;
291 sub grade_alarm_multiple {
292 verify_alarm (7, @_);
296 my ($iterations, @output) = @_;
298 verify_common (@output);
301 for (my ($i) = 0; $i < $iterations; $i++) {
302 for (my ($t) = 0; $t < 5; $t++) {
303 push (@products, ($i + 1) * ($t + 1) * 10);
306 @products = sort {$a <=> $b} @products;
310 die $_ if /Out of order/;
312 my ($p) = /product=(\d+)$/;
315 my ($q) = shift (@products);
316 die "Too many wakeups.\n" if !defined $q;
317 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
319 die scalar (@products) . " fewer wakeups than expected.\n"
323 sub grade_alarm_zero {
325 verify_common (@output);
326 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
329 sub grade_alarm_negative {
331 verify_common (@output);
332 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
335 sub grade_join_invalid {
337 verify_common (@output);
338 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
339 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
344 verify_common (@output);
345 grep (/Testing no join/, @output) or die "Test didn't start\n";
346 grep (/No join test done/, @output) or die "Test didn't complete\n";
349 sub grade_join_multiple {
352 verify_common (@output);
354 $t[4] = $t[5] = $t[6] = -1;
357 my ($idx) = /^Thread (\d+)/ or next;
358 my ($iter) = /iteration (\d+)$/;
359 $iter = 5 if /done!$/;
360 die "Malformed output\n" if !defined $iter;
362 die "Thread 6 started before either other thread finished\n"
363 if $t[4] < 5 && $t[5] < 5;
364 die "Thread 6 started before thread 4 finished\n"
366 die "Thread 6 started before thread 5 finished\n"
369 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
374 for my $idx (4, 5, 6) {
375 if ($t[$idx] == -1) {
376 $err .= "Thread $idx did not run at all\n";
377 } elsif ($t[$idx] != 5) {
378 $err .= "Thread $idx only completed $t[$idx] iterations\n";
381 die $err if $err ne '';
384 sub grade_priority_fifo {
387 verify_common (@output);
388 my ($thread_cnt) = 10;
391 my (@t) = (-1) x $thread_cnt;
394 my ($idx) = /^Thread (\d+)/ or next;
395 my ($iter) = /iteration (\d+)$/;
396 $iter = $iter_cnt if /done!$/;
397 die "Malformed output\n" if !defined $iter;
398 if (@order < $thread_cnt) {
400 die "Thread $idx repeated within first $thread_cnt iterations: "
401 . join (' ', @order) . ".\n"
402 if grep ($_ == $idx, @order) != 1;
404 die "Thread $idx ran when $order[0] should have.\n"
405 if $idx != $order[0];
406 push (@order, shift @order);
408 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
413 for my $idx (0..$#t) {
414 if ($t[$idx] == -1) {
415 $err .= "Thread $idx did not run at all.\n";
416 } elsif ($t[$idx] != $iter_cnt) {
417 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
420 die $err if $err ne '';
425 verify_common (@output);
426 our (@mlfqs_on_stats) = mlfqs_stats (@output);
429 sub grade_mlfqs_off {
431 verify_common (@output);
432 our (@mlfqs_off_stats) = mlfqs_stats (@output);
435 sub grade_mlfqs_speedup {
436 our (@mlfqs_off_stats);
437 our (@mlfqs_on_stats);
440 my ($off_ticks) = $mlfqs_off_stats[1];
441 my ($on_ticks) = $mlfqs_on_stats[1];
442 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
443 if $on_ticks >= $off_ticks;
447 $result{'mlfqs-speedup'} = $@;
450 sub grade_mlfqs_priority {
451 our (@mlfqs_off_stats);
452 our (@mlfqs_on_stats);
454 check_mlfqs () if !defined (@mlfqs_on_stats);
455 for my $cat qw (CPU IO MIX) {
456 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
457 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
458 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
459 die "Minimum priority never changed from PRI_DEFAULT (29) "
461 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
462 die "Maximum priority never changed from PRI_DEFAULT (29) "
464 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
469 $result{'mlfqs-priority'} = $@;
473 our (@mlfqs_off_stats);
474 our (@mlfqs_on_stats);
475 die "p1-4 didn't finish with MLFQS on or off\n"
476 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
477 die "p1-4 didn't finish with MLFQS on\n"
478 if !defined (@mlfqs_on_stats);
479 die "p1-4 didn't finish with MLFQS off\n"
480 if !defined (@mlfqs_off_stats);
485 my (%stats) = (CPU => {}, IO => {}, MIX => {});
486 my (%map) = ("CPU intensive" => 'CPU',
487 "IO intensive" => 'IO',
488 "Alternating IO/CPU" => 'MIX');
489 my (%rmap) = reverse %map;
493 $ticks = $1 if /Timer: (\d+) ticks/;
494 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
495 my ($t) = $map{$thread} or next;
497 my ($s) = $stats{$t};
500 $$s{SUM2} += $pri * $pri;
501 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
502 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
505 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
506 for my $cat (values (%map)) {
507 my ($s) = $stats{$cat};
508 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
509 if $$s{N} != $expect_n{$cat};
510 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
512 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
514 $$s{MEAN} = $$s{SUM} / $$s{N};
517 return (\%stats, $ticks);
523 my (@assertion) = grep (/PANIC/, @output);
524 if (@assertion != 0) {
525 my ($details) = "Kernel panic:\n $assertion[0]\n";
527 my (@stack_line) = grep (/Call stack:/, @output);
528 if (@stack_line != 0) {
529 $details .= " $stack_line[0]\n\n";
530 $details .= "Translation of backtrace:\n";
531 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
535 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
538 $A2L = "i386-elf-addr2line";
540 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
542 my ($function, $line);
543 last unless defined ($function = <A2L>);
547 $details .= " $function ($line)\n";
550 $extra{$test} = $details;
551 die "Kernel panic. Details at end of file.\n"
554 die "No output at all\n" if @output == 0;
555 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
556 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
557 die "Didn't start up properly: no \"Boot complete\" startup message\n"
558 if !grep (/Boot complete/, @output);
559 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
560 if !grep (/Timer: \d+ ticks/, @output);
561 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
562 if !grep (/Powering off/, @output);
565 # Get @output without header or trailer.
566 sub get_core_output {
571 for ($first = 0; $first <= $#output; $first++) {
572 $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
576 for ($last = $#output; $last >= 0; $last--) {
577 $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
580 if ($last < $first) {
581 my ($no_first) = $first > $#output;
582 my ($no_last) = $last < $#output;
583 die "Couldn't locate output.\n";
586 return @output[$first ... $last];
590 my ($exp, @actual) = @_;
591 @actual = get_core_output (map ("$_\n", @actual));
593 # Fix up lines that look like exit codes.
594 for my $i (0...$#actual) {
595 if (my ($process, $code)
596 = $actual[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
597 $process = substr ($process, 0, 15);
598 $process =~ s/\s.*//;
599 $actual[$i] = "$process: exit($code)\n";
604 $details .= "$test actual output:\n";
605 $details .= join ('', map (" $_", @actual));
607 my (@exp) = map ("$_\n", snarf ($exp));
609 my ($fuzzy_match) = 0;
613 my ($s) = shift (@exp);
614 last if $s eq "--OR--\n";
615 push (@expected, $s);
618 $details .= "\n$test acceptable output:\n";
619 $details .= join ('', map (" $_", @expected));
621 # Check whether they're the same.
622 if ($#actual == $#expected) {
624 for (my ($i) = 0; $i <= $#expected; $i++) {
625 $eq = 0 if $actual[$i] ne $expected[$i];
630 # They differ. Output a diff.
632 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
633 my ($not_fuzzy_match) = 0;
634 while ($d->Next ()) {
635 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
637 push (@diff, map (" $_", $d->Items (1)));
639 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
640 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
642 || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
643 $not_fuzzy_match = 1;
647 $fuzzy_match = 1 if !$not_fuzzy_match;
649 $details .= "Differences in `diff -u' format:\n";
650 $details .= join ('', @diff);
651 $details .= "(This is considered a `fuzzy match'.)\n" if $fuzzy_match;
654 $details{$test} = $details;
655 die "Output differs from expected. Details at end of file.\n"
660 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
665 for (my ($i) = 0; $i <= $#summary; $i++) {
666 local ($_) = $summary[$i];
667 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
668 my ($result) = $result{$test} || "Not tested.";
670 if ($result eq 'ok') {
671 splice (@summary, $i, 1);
676 splice (@summary, $i + 1, 0,
677 map (" $_", split ("\n", $result)));
679 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
681 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
682 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
687 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
688 $summary[0] =~ s/\[\[total\]\]/$ts/;
690 open (SUMMARY, ">tests.out");
691 print SUMMARY map ("$_\n", @summary);
696 open (DETAILS, ">details.out");
698 for my $test (@TESTS) {
699 next if $result{$test} eq 'ok' && !defined $details{$test};
701 my ($details) = $details{$test};
702 next if !defined ($details) && ! -e "output/$test/run.out";
704 print DETAILS "\n" if $n++;
705 print DETAILS "--- $test details ", '-' x (50 - length ($test));
706 print DETAILS "\n\n";
708 if (!defined $details) {
709 $details = "Output:\n\n" . snarf ("output/$test/run.out");
711 print DETAILS $details;
713 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
714 if defined $extra{$test};
721 my ($command, %options) = @_;
722 print "$command\n" if $VERBOSE || $options{VERBOSE};
724 my ($log) = $options{LOG};
725 if (defined ($log)) {
726 $command = "($command) >output/$log.out 2>output/$log.err";
731 local $SIG{ALRM} = sub { die "alarm\n" };
732 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
734 die "fork: $!\n" if !defined $pid;
735 exec ($command), die "$command: exec: $!\n" if !$pid;
741 die unless $@ eq "alarm\n"; # propagate unexpected errors
742 print "Timed out.\n";
747 if (WIFSIGNALED ($status)) {
748 my ($signal) = WTERMSIG ($status);
749 die "Interrupted\n" if $signal == SIGINT;
750 print "Child terminated with signal $signal\n";
753 unlink ("output/$log.err") if defined ($log) && $status == 0;
755 die $options{DIE} if $status != 0 && defined $options{DIE};
762 open (OUTPUT, $file) or die "$file: open: $!\n";
763 my (@lines) = <OUTPUT>;
766 return wantarray ? @lines : join ('', map ("$_\n", @lines));
772 open (A, "<$a") or die "$a: open: $!\n";
773 open (B, "<$b") or die "$b: open: $!\n";
779 sysread (A, $sa, 1024);
780 sysread (B, $sb, 1024);
781 $equal = 0, last if $sa ne $sb;
782 $equal = 1, last if $sa eq '';
791 my ($file, $expected) = @_;
792 open (FILE, "<$file") or die "$file: open: $!\n";
794 sysread (FILE, $actual, -s FILE);
795 my ($equal) = $actual eq $expected;
801 my ($ln, $lines) = @_;
803 for my $line (@$lines) {
805 $out .= sprintf "%4d %s\n", $ln++, $line;