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 thread 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 = ("alarm-single", "alarm-multiple", "alarm-zero", "alarm-negative",
42 "join-quick", "join-multiple", "join-nested",
43 "join-dummy", "join-invalid", "join-no",
44 "priority-preempt", "priority-fifo", "priority-donate-one",
45 "priority-donate-multiple", "priority-donate-nest",
46 "mlfqs-on", "mlfqs-off")
49 # Handle final grade mode.
51 open (OUT, ">grade.out") or die "grade.out: create: $!\n";
53 open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
60 my (@tests) = snarf ("tests.out");
61 my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
63 my (@review) = snarf ("review.txt");
64 my ($part_lost) = (0, 0);
65 for (my ($i) = $#review; $i >= 0; $i--) {
66 local ($_) = $review[$i];
67 if (my ($loss) = /^\s*([-+]\d+)/) {
69 } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
70 my ($got) = $out_of + $part_lost;
72 $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
79 die "Lost points outside a section\n" if $part_lost;
81 for (my ($i) = 1; $i <= $#review; $i++) {
82 if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
83 $review[$i] = '-' x (length ($review[$i - 1]));
87 print OUT "\nOVERALL SCORE\n";
88 print OUT "-------------\n";
89 print OUT "$p_got points out of $p_pos total\n\n";
91 print OUT map ("$_\n", @tests), "\n";
92 print OUT map ("$_\n", @review);
97 # Find the directory that contains the grading files.
99 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
100 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
103 # Verify that we're roughly in the correct directory
104 # before we go blasting away files.
107 xsystem ("rm -rf output pintos", VERBOSE => 1);
108 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
111 # Create output directory, if it doesn't already exist.
112 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
114 # Extract submission.
115 extract_tarball () if ! -d "pintos";
117 # Verify that the proper directory was submitted.
118 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
120 # Run and grade the tests.
127 my ($result) = run_test ($test);
128 if ($result eq 'ok') {
129 $result = grade_test ($test);
134 $result{$test} = $result;
137 # MLFQS takes results from mlfqs-on and mlfqs-off.
138 grade_mlfqs_speedup ();
139 grade_mlfqs_priority ();
147 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
149 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
151 # Sort tarballs in reverse order by time.
152 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
154 print "Multiple tarballs: choosing $tarballs[0]\n"
155 if scalar (@tarballs) > 1;
159 sub extract_tarball {
160 my ($tarball) = choose_tarball ();
162 mkdir "pintos" or die "pintos: mkdir: $!\n";
163 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
165 print "Extracting $tarball...\n";
166 xsystem ("cd pintos/src && tar xzf ../../$tarball",
167 DIE => "extraction failed\n");
169 print "Patching...\n";
170 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
172 DIE => "patch failed\n");
177 my ($ms, $d, $y, $H, $M, $S) =
178 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
180 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
182 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
187 my ($src) = "$GRADES_DIR/$test.c";
188 $src = "$GRADES_DIR/mlfqs.c" if $test =~ /^mlfqs/;
189 -e $src or die "$src: stat: $!\n";
195 $defines .= "#define MLFQS 1\n" if $test eq 'mlfqs-on';
202 # Reuse older results if any.
203 if (open (DONE, "<output/$test/done")) {
211 # Really run the test.
212 my ($status) = really_run_test ($test);
214 # Save the results for later.
215 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
216 print DONE "$status\n";
222 sub really_run_test {
224 # If there's residue from an earlier test, move it to .old.
225 # If there's already a .old, delete it.
226 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
227 rename "output/$test", "output/$test.old" or die "rename: $!\n"
228 if -d "output/$test";
230 # Make output directory.
231 mkdir "output/$test";
233 # Change constants.h if necessary.
234 my ($defines) = test_constants ($test);
235 if ($defines ne snarf ("pintos/src/constants.h")) {
236 open (CONSTANTS, ">pintos/src/constants.h");
237 print CONSTANTS $defines;
241 # Copy in the new test.c and delete enough files to ensure a full rebuild.
242 my ($src) = test_source ($test);
243 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
244 unlink ("pintos/src/threads/build/threads/test.o");
245 unlink ("pintos/src/threads/build/kernel.o");
246 unlink ("pintos/src/threads/build/kernel.bin");
247 unlink ("pintos/src/threads/build/os.dsk");
250 xsystem ("cd pintos/src/threads && make", LOG => "$test/make")
251 or return "compile error";
253 # Copy out files for backtraces later.
254 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
255 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
259 $timeout = 600 if $test =~ /^mlfqs/;
260 xsystem ("cd pintos/src/threads/build && pintos -v run -q",
263 or return "Bochs error";
271 my (@output) = snarf ("output/$test/run.out");
273 if (-e "$GRADES_DIR/$test.exp") {
275 verify_common (@output);
276 compare_output ("$GRADES_DIR/$test.exp", @output);
280 ($grade_func = $test) =~ s/-/_/g;
281 eval "grade_$grade_func (\@output)";
284 die $@ if $@ =~ /at \S+ line \d+$/;
290 sub grade_alarm_single {
291 verify_alarm (1, @_);
294 sub grade_alarm_multiple {
295 verify_alarm (7, @_);
299 my ($iterations, @output) = @_;
301 verify_common (@output);
304 for (my ($i) = 0; $i < $iterations; $i++) {
305 for (my ($t) = 0; $t < 5; $t++) {
306 push (@products, ($i + 1) * ($t + 1) * 10);
309 @products = sort {$a <=> $b} @products;
313 die $_ if /Out of order/;
315 my ($p) = /product=(\d+)$/;
318 my ($q) = shift (@products);
319 die "Too many wakeups.\n" if !defined $q;
320 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
322 die scalar (@products) . " fewer wakeups than expected.\n"
326 sub grade_alarm_zero {
328 verify_common (@output);
329 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
332 sub grade_alarm_negative {
334 verify_common (@output);
335 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
338 sub grade_join_invalid {
340 verify_common (@output);
341 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
342 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
347 verify_common (@output);
348 grep (/Testing no join/, @output) or die "Test didn't start\n";
349 grep (/No join test done/, @output) or die "Test didn't complete\n";
352 sub grade_join_multiple {
355 verify_common (@output);
357 $t[4] = $t[5] = $t[6] = -1;
360 my ($idx) = /^Thread (\d+)/ or next;
361 my ($iter) = /iteration (\d+)$/;
362 $iter = 5 if /done!$/;
363 die "Malformed output\n" if !defined $iter;
365 die "Thread 6 started before either other thread finished\n"
366 if $t[4] < 5 && $t[5] < 5;
367 die "Thread 6 started before thread 4 finished\n"
369 die "Thread 6 started before thread 5 finished\n"
372 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
377 for my $idx (4, 5, 6) {
378 if ($t[$idx] == -1) {
379 $err .= "Thread $idx did not run at all\n";
380 } elsif ($t[$idx] != 5) {
381 $err .= "Thread $idx only completed $t[$idx] iterations\n";
384 die $err if $err ne '';
387 sub grade_priority_fifo {
390 verify_common (@output);
391 my ($thread_cnt) = 10;
394 my (@t) = (-1) x $thread_cnt;
397 my ($idx) = /^Thread (\d+)/ or next;
398 my ($iter) = /iteration (\d+)$/;
399 $iter = $iter_cnt if /done!$/;
400 die "Malformed output\n" if !defined $iter;
401 if (@order < $thread_cnt) {
403 die "Thread $idx repeated within first $thread_cnt iterations: "
404 . join (' ', @order) . ".\n"
405 if grep ($_ == $idx, @order) != 1;
407 die "Thread $idx ran when $order[0] should have.\n"
408 if $idx != $order[0];
409 push (@order, shift @order);
411 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
416 for my $idx (0..$#t) {
417 if ($t[$idx] == -1) {
418 $err .= "Thread $idx did not run at all.\n";
419 } elsif ($t[$idx] != $iter_cnt) {
420 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
423 die $err if $err ne '';
428 verify_common (@output);
429 our (@mlfqs_on_stats) = mlfqs_stats (@output);
432 sub grade_mlfqs_off {
434 verify_common (@output);
435 our (@mlfqs_off_stats) = mlfqs_stats (@output);
438 sub grade_mlfqs_speedup {
439 our (@mlfqs_off_stats);
440 our (@mlfqs_on_stats);
443 my ($off_ticks) = $mlfqs_off_stats[1];
444 my ($on_ticks) = $mlfqs_on_stats[1];
445 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
446 if $on_ticks >= $off_ticks;
450 $result{'mlfqs-speedup'} = $@;
453 sub grade_mlfqs_priority {
454 our (@mlfqs_off_stats);
455 our (@mlfqs_on_stats);
457 check_mlfqs () if !defined (@mlfqs_on_stats);
458 for my $cat qw (CPU IO MIX) {
459 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
460 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
461 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
462 die "Minimum priority never changed from PRI_DEFAULT (29) "
464 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
465 die "Maximum priority never changed from PRI_DEFAULT (29) "
467 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
472 $result{'mlfqs-priority'} = $@;
476 our (@mlfqs_off_stats);
477 our (@mlfqs_on_stats);
478 die "p1-4 didn't finish with MLFQS on or off\n"
479 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
480 die "p1-4 didn't finish with MLFQS on\n"
481 if !defined (@mlfqs_on_stats);
482 die "p1-4 didn't finish with MLFQS off\n"
483 if !defined (@mlfqs_off_stats);
488 my (%stats) = (CPU => {}, IO => {}, MIX => {});
489 my (%map) = ("CPU intensive" => 'CPU',
490 "IO intensive" => 'IO',
491 "Alternating IO/CPU" => 'MIX');
492 my (%rmap) = reverse %map;
496 $ticks = $1 if /Timer: (\d+) ticks/;
497 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
498 my ($t) = $map{$thread} or next;
500 my ($s) = $stats{$t};
503 $$s{SUM2} += $pri * $pri;
504 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
505 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
508 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
509 for my $cat (values (%map)) {
510 my ($s) = $stats{$cat};
511 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
512 if $$s{N} != $expect_n{$cat};
513 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
515 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
517 $$s{MEAN} = $$s{SUM} / $$s{N};
520 return (\%stats, $ticks);
526 my (@assertion) = grep (/PANIC/, @output);
527 if (@assertion != 0) {
528 my ($details) = "Kernel panic:\n $assertion[0]\n";
530 my (@stack_line) = grep (/Call stack:/, @output);
531 if (@stack_line != 0) {
532 $details .= " $stack_line[0]\n\n";
533 $details .= "Translation of backtrace:\n";
534 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
538 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
541 $A2L = "i386-elf-addr2line";
543 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
545 my ($function, $line);
546 last unless defined ($function = <A2L>);
550 $details .= " $function ($line)\n";
553 $extra{$test} = $details;
554 die "Kernel panic. Details at end of file.\n"
557 die "No output at all\n" if @output == 0;
558 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
559 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
560 die "Didn't start up properly: no \"Boot complete\" startup message\n"
561 if !grep (/Boot complete/, @output);
562 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
563 if !grep (/Timer: \d+ ticks/, @output);
564 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
565 if !grep (/Powering off/, @output);
569 my ($exp_file, @actual) = @_;
570 my (@expected) = snarf ($exp_file);
572 @actual = map ("$_\n", @actual);
573 @expected = map ("$_\n", @expected);
575 # Trim header and trailer from @actual.
576 while (scalar (@actual) && $actual[0] ne $expected[0]) {
579 die "First line of expected output was not present.\n" if !@actual;
580 while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
583 die "Final line of expected output was not present.\n" if !@actual;
585 # Check whether they're the same.
586 if ($#actual == $#expected) {
588 for (my ($i) = 0; $i <= $#expected; $i++) {
589 $eq = 0 if $actual[$i] ne $expected[$i];
594 # They differ. Output a diff.
596 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
598 while ($d->Next ()) {
599 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
602 $diff .= "Actual lines $af...$al match expected lines "
605 $diff .= "Actual line $af matches expected line $ef.\n";
608 my (@i1) = $d->Items (1);
609 my (@i2) = $d->Items (2);
611 $diff .= "Extra or misplaced line(s) $af...$al "
612 . "in actual output:\n";
613 $diff .= number_lines ($af, \@i2);
614 } elsif (!$d->Items (2)) {
615 $diff .= "Expected line(s) $ef...$el missing or misplaced:\n";
616 $diff .= number_lines ($ef, \@i1);
618 $diff .= "The following expected line(s) $ef...$el:\n";
619 $diff .= number_lines ($ef, \@i1);
620 $diff .= "became actual line(s) $af...$al:\n";
621 $diff .= number_lines ($af, \@i2);
627 $details .= "$test actual output (line numbers added):\n";
628 $details .= number_lines (1, \@actual);
629 $details .= "\n$test expected output (line numbers added):\n";
630 $details .= number_lines (1, \@expected);
631 $details .= "\n$diff\n";
632 $details{$test} = $details;
633 die "Output differs from expected. Details at end of file.\n";
637 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
642 for (my ($i) = 0; $i <= $#summary; $i++) {
643 local ($_) = $summary[$i];
644 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
645 my ($result) = $result{$test} || "Not tested.";
647 if ($result eq 'ok') {
648 splice (@summary, $i, 1);
653 splice (@summary, $i + 1, 0,
654 map (" $_", split ("\n", $result)));
656 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
658 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
659 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
664 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
665 $summary[0] =~ s/\[\[total\]\]/$ts/;
667 open (SUMMARY, ">tests.out");
668 print SUMMARY map ("$_\n", @summary);
673 open (DETAILS, ">details.out");
675 for my $test (@TESTS) {
676 next if $result{$test} eq 'ok';
678 my ($details) = $details{$test};
679 next if !defined ($details) && ! -e "output/$test/run.out";
681 print DETAILS "\n" if $n++;
682 print DETAILS "--- $test details ", '-' x (50 - length ($test));
683 print DETAILS "\n\n";
685 if (!defined $details) {
686 $details = "Output:\n\n" . snarf ("output/$test/run.out");
688 print DETAILS $details;
690 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
691 if defined $extra{$test};
698 my ($command, %options) = @_;
699 print "$command\n" if $VERBOSE || $options{VERBOSE};
701 my ($log) = $options{LOG};
702 if (defined ($log)) {
703 $command = "($command) >output/$log.out 2>output/$log.err";
708 local $SIG{ALRM} = sub { die "alarm\n" };
709 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
711 die "fork: $!\n" if !defined $pid;
712 exec ($command), die "$command: exec: $!\n" if !$pid;
718 die unless $@ eq "alarm\n"; # propagate unexpected errors
719 print "Timed out.\n";
724 if (WIFSIGNALED ($status)) {
725 my ($signal) = WTERMSIG ($status);
726 die "Interrupted\n" if $signal == SIGINT;
727 print "Child terminated with signal $signal\n";
730 unlink ("output/$log.err") if defined ($log) && $status == 0;
737 open (OUTPUT, $file) or die "$file: open: $!\n";
738 my (@lines) = <OUTPUT>;
741 return wantarray ? @lines : join ('', map ("$_\n", @lines));
747 open (A, "<$a") or die "$a: open: $!\n";
748 open (B, "<$b") or die "$b: open: $!\n";
754 sysread (A, $sa, 1024);
755 sysread (B, $sb, 1024);
756 $equal = 0, last if $sa ne $sb;
757 $equal = 1, last if $sa eq '';
766 my ($file, $expected) = @_;
767 open (FILE, "<$file") or die "$file: open: $!\n";
769 sysread (FILE, $actual, -s FILE);
770 my ($equal) = $actual eq $expected;
776 my ($ln, $lines) = @_;
778 for my $line (@$lines) {
780 $out .= sprintf "%4d %s\n", $ln++, $line;