9 our ($VERBOSE) = 0; # Verbosity of output
10 our (@TESTS); # Tests to run.
13 GetOptions ("v|verbose+" => \$VERBOSE,
14 "h|help" => sub { usage (0) },
15 "t|test=s" => \@TESTS,
17 or die "Malformed command line; use --help for help.\n";
18 die "Non-option argument not supported; use --help for help.\n"
23 print "run-tests, for grading Pintos thread projects.\n\n";
24 print "Invoke from a directory containing a student tarball named by\n";
25 print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
26 print "In normal usage, no options are needed.\n\n";
27 print "Output is produced in tests.out and details.out.\n\n";
29 print " -c, --clean Remove old output files before starting\n";
30 print " -t, --test=TEST Execute TEST only (allowed multiple times)\n";
31 print " -v, --verbose Print commands before executing them\n";
32 print " -h, --help Print this help message\n";
36 # Default set of tests.
37 @TESTS = ("alarm-single", "alarm-multiple", "alarm-zero", "alarm-negative",
39 "join-quick", "join-multiple", "join-nested",
40 "join-dummy", "join-invalid", "join-no",
41 "priority-preempt", "priority-fifo", "priority-donate-one",
42 "priority-donate-multiple", "priority-donate-nest",
43 #"mlfqs-on", "mlfqs-off"
47 # Find the directory that contains the grading files.
49 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
50 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
53 # Verify that we're roughly in the correct directory
54 # before we go blasting away files.
57 xsystem ("rm -rf output pintos", VERBOSE => 1);
58 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
61 # Create output directory, if it doesn't already exist.
62 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
65 extract_tarball () if ! -d "pintos";
67 # Verify that the proper directory was submitted.
68 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
70 # Run and grade the tests.
77 my ($result) = run_test ($test);
78 if ($result eq 'ok') {
79 $result = grade_test ($test);
84 $result{$test} = $result;
93 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
95 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
97 # Sort tarballs in reverse order by time.
98 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
100 print "Multiple tarballs: choosing $tarballs[0]\n"
101 if scalar (@tarballs) > 1;
105 sub extract_tarball {
106 my ($tarball) = choose_tarball ();
108 mkdir "pintos" or die "pintos: mkdir: $!\n";
109 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
111 print "Extracting $tarball...\n";
112 xsystem ("cd pintos/src && tar xzf ../../$tarball",
113 DIE => "extraction failed\n");
115 print "Patching...\n";
116 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
118 DIE => "patch failed\n");
123 my ($ms, $d, $y, $H, $M, $S) =
124 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
126 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
128 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
133 my ($src) = "$GRADES_DIR/$test.c";
134 $src = "$GRADES_DIR/mlfqs.c" if $test =~ /^mlfqs/;
135 -e $src or die "$src: stat: $!\n";
141 $defines .= "#define MLFQS 1\n" if $test eq 'mlfqs-on';
147 return "ok" if -f "output/$test/done";
149 # Reuse older results if any.
150 if (open (DONE, "<output/$test/done")) {
158 # Really run the test.
159 my ($status) = really_run_test ($test);
161 # Save the results for later.
162 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
163 print DONE "$status\n";
169 sub really_run_test {
171 # If there's residue from an earlier test, move it to .old.
172 # If there's already a .old, delete it.
173 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
174 rename "output/$test", "output/$test.old" or die "rename: $!\n"
175 if -d "output/$test";
177 # Make output directory.
178 mkdir "output/$test";
180 # Change constants.h if necessary.
181 my ($defines) = test_constants ($test);
182 if ($defines ne snarf ("pintos/src/constants.h")) {
183 open (CONSTANTS, ">pintos/src/constants.h");
184 print CONSTANTS $defines;
188 # Copy in the new test.c and delete enough files to ensure a full rebuild.
189 my ($src) = test_source ($test);
190 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
191 unlink ("pintos/src/threads/build/threads/test.o");
192 unlink ("pintos/src/threads/build/kernel.o");
193 unlink ("pintos/src/threads/build/kernel.bin");
194 unlink ("pintos/src/threads/build/os.dsk");
197 xsystem ("cd pintos/src/threads && make", LOG => "$test/make")
198 or return "compile error";
200 # Copy out files for backtraces later.
201 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
202 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
206 $timeout = 600 if $test =~ /^mlfqs/;
207 xsystem ("cd pintos/src/threads/build && pintos -v run -q",
210 or return "Bochs error";
218 my (@output) = snarf ("output/$test/run.out");
220 if (-e "$GRADES_DIR/$test.exp") {
222 verify_common (@output);
223 compare_output ("$GRADES_DIR/$test.exp", @output);
227 ($grade_func = $test) =~ s/-/_/g;
228 eval "grade_$grade_func (\@output)";
231 die $@ if $@ =~ /at \S+ line \d+$/;
237 sub grade_alarm_single {
238 verify_alarm (1, @_);
241 sub grade_alarm_multiple {
242 verify_alarm (7, @_);
246 my ($iterations, @output) = @_;
248 verify_common (@output);
251 for (my ($i) = 0; $i < $iterations; $i++) {
252 for (my ($t) = 0; $t < 5; $t++) {
253 push (@products, ($i + 1) * ($t + 1) * 10);
256 @products = sort {$a <=> $b} @products;
260 die $_ if /Out of order/;
262 my ($p) = /product=(\d+)$/;
265 my ($q) = shift (@products);
266 die "Too many wakeups.\n" if !defined $q;
267 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
269 die scalar (@products) . " fewer wakeups than expected.\n"
273 sub grade_alarm_zero {
275 verify_common (@output);
276 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
279 sub grade_alarm_negative {
281 verify_common (@output);
282 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
285 sub grade_join_invalid {
287 verify_common (@output);
288 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
289 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
294 verify_common (@output);
295 grep (/Testing no join/, @output) or die "Test didn't start\n";
296 grep (/No join test done/, @output) or die "Test didn't complete\n";
299 sub grade_join_multiple {
302 verify_common (@output);
304 $t[4] = $t[5] = $t[6] = -1;
307 my ($idx) = /^Thread (\d+)/ or next;
308 my ($iter) = /iteration (\d+)$/;
309 $iter = 5 if /done!$/;
310 die "Malformed output\n" if !defined $iter;
312 die "Thread 6 started before either other thread finished\n"
313 if $t[4] < 5 && $t[5] < 5;
314 die "Thread 6 started before thread 4 finished\n"
316 die "Thread 6 started before thread 5 finished\n"
319 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
324 for my $idx (4, 5, 6) {
325 if ($t[$idx] == -1) {
326 $err .= "Thread $idx did not run at all\n";
327 } elsif ($t[$idx] != 5) {
328 $err .= "Thread $idx only completed $t[$idx] iterations\n";
331 die $err if $err ne '';
334 sub grade_priority_fifo {
337 verify_common (@output);
338 my ($thread_cnt) = 10;
341 my (@t) = (-1) x $thread_cnt;
344 my ($idx) = /^Thread (\d+)/ or next;
345 my ($iter) = /iteration (\d+)$/;
346 $iter = $iter_cnt if /done!$/;
347 die "Malformed output\n" if !defined $iter;
348 if (@order < $thread_cnt) {
350 die "Thread $idx repeated within first $thread_cnt iterations: "
351 . join (' ', @order) . ".\n"
352 if grep ($_ == $idx, @order) != 1;
354 die "Thread $idx ran when $order[0] should have.\n"
355 if $idx != $order[0];
356 push (@order, shift @order);
358 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
363 for my $idx (0..$#t) {
364 if ($t[$idx] == -1) {
365 $err .= "Thread $idx did not run at all.\n";
366 } elsif ($t[$idx] != $iter_cnt) {
367 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
370 die $err if $err ne '';
375 verify_common (@output);
376 mlfqs_stats (@output);
379 sub grade_mlfqs_off {
381 verify_common (@output);
382 mlfqs_stats (@output);
387 my (%stats) = ("io" => {}, "cpu" => {}, "mix" => {});
388 my (%map) = ("CPU intensive" => "cpu",
389 "IO intensive" => "io",
390 "Alternating IO/CPU" => "mix");
393 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
394 my ($t) = $map{$thread} or next;
396 my ($s) = $stats{$t};
399 $$s{"sum2"} += $pri * $pri;
400 $$s{"min"} = $pri if !defined ($$s{"min"}) || $pri < $$s{"min"};
401 $$s{"max"} = $pri if !defined ($$s{"max"}) || $pri > $$s{"max"};
405 for my $t (keys %stats) {
406 my ($s) = $stats{$t};
407 $details .= "$t: n=$$s{'n'}, min=$$s{'min'}, max=$$s{'max'}, avg=" . int ($$s{'sum'} / $$s{'n'}) . "\n";
409 $details{$test} = $details;
416 my (@assertion) = grep (/PANIC/, @output);
417 if (@assertion != 0) {
418 my ($details) = "Kernel panic:\n $assertion[0]\n";
420 my (@stack_line) = grep (/Call stack:/, @output);
421 if (@stack_line != 0) {
422 $details .= " $stack_line[0]\n\n";
423 $details .= "Translation of backtrace:\n";
424 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
428 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
431 $A2L = "i386-elf-addr2line";
433 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
435 my ($function, $line);
436 last unless $function = <A2L>;
440 $details .= " $function ($line)\n";
443 $extra{$test} = $details;
444 die "Kernel panic. Details at end of file.\n"
447 die "No output at all\n" if @output == 0;
448 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
449 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
450 die "Didn't start up properly: no \"Boot complete\" startup message\n"
451 if !grep (/Boot complete/, @output);
452 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
453 if !grep (/Timer: \d+ ticks/, @output);
454 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
455 if !grep (/Powering off/, @output);
459 my ($exp_file, @actual) = @_;
460 my (@expected) = snarf ($exp_file);
462 @actual = map ("$_\n", @actual);
463 @expected = map ("$_\n", @expected);
465 # Trim header and trailer from @actual.
466 while (scalar (@actual) && $actual[0] ne $expected[0]) {
469 die "First line of expected output was not present.\n" if !@actual;
470 while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
473 die "Final line of expected output was not present.\n" if !@actual;
475 # Check whether they're the same.
476 if ($#actual == $#expected) {
478 for (my ($i) = 0; $i <= $#expected; $i++) {
479 $eq = 0 if $actual[$i] ne $expected[$i];
484 # They differ. Output a diff.
486 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
488 while ($d->Next ()) {
489 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
492 $diff .= "Actual lines $af...$al match expected lines "
495 $diff .= "Actual line $af matches expected line $ef.\n";
498 my (@i1) = $d->Items (1);
499 my (@i2) = $d->Items (2);
501 $diff .= "Extra or misplaced line(s) $af...$al "
502 . "in actual output:\n";
503 $diff .= number_lines ($af, \@i2);
504 } elsif (!$d->Items (2)) {
505 $diff .= "Expected line(s) $ef...$el missing or misplaced:\n";
506 $diff .= number_lines ($ef, \@i1);
508 $diff .= "The following expected line(s) $ef...$el:\n";
509 $diff .= number_lines ($ef, \@i1);
510 $diff .= "became actual line(s) $af...$al:\n";
511 $diff .= number_lines ($af, \@i2);
517 $details .= "$test actual output (line numbers added):\n";
518 $details .= number_lines (1, \@actual);
519 $details .= "\n$test expected output (line numbers added):\n";
520 $details .= number_lines (1, \@expected);
521 $details .= "\n$diff\n";
522 $details{$test} = $details;
523 die "Output differs from expected. Details at end of file.\n";
527 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
532 for (my ($i) = 0; $i <= $#summary; $i++) {
533 local ($_) = $summary[$i];
534 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
535 my ($result) = $result{$test} || "Not tested.";
537 if ($result eq 'ok') {
538 splice (@summary, $i, 1);
543 splice (@summary, $i + 1, 0,
544 map (" $_", split ("\n", $result)));
546 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
548 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
549 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
554 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
555 $summary[0] =~ s/\[\[total\]\]/$ts/;
557 open (SUMMARY, ">tests.out");
558 print SUMMARY map ("$_\n", @summary);
563 open (DETAILS, ">details.out");
565 for my $test (@TESTS) {
566 next if $result{$test} eq 'ok';
568 my ($details) = $details{$test};
569 next if !defined ($details) && ! -e "output/$test/run.out";
571 print DETAILS "\n" if $n++;
572 print DETAILS "--- $test details ", '-' x (50 - length ($test));
573 print DETAILS "\n\n";
575 if (!defined $details) {
576 $details = "Output:\n\n" . snarf ("output/$test/run.out");
578 print DETAILS $details;
580 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
581 if defined $extra{$test};
588 my ($command, %options) = @_;
589 print "$command\n" if $VERBOSE || $options{VERBOSE};
591 my ($log) = $options{LOG};
592 if (defined ($log)) {
593 $command = "($command) >output/$log.out 2>output/$log.err";
598 local $SIG{ALRM} = sub { die "alarm\n" };
599 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
601 die "fork: $!\n" if !defined $pid;
602 exec ($command), die "$command: exec: $!\n" if !$pid;
608 die unless $@ eq "alarm\n"; # propagate unexpected errors
609 print "Timed out.\n";
614 if (WIFSIGNALED ($status)) {
615 my ($signal) = WTERMSIG ($status);
616 die "Interrupted\n" if $signal == SIGINT;
617 print "Child terminated with signal $signal\n";
620 unlink ("output/$log.err") if defined ($log) && $status == 0;
627 open (OUTPUT, $file) or die "$file: open: $!\n";
628 my (@lines) = <OUTPUT>;
631 return wantarray ? @lines : join ('', map ("$_\n", @lines));
637 open (A, "<$a") or die "$a: open: $!\n";
638 open (B, "<$b") or die "$b: open: $!\n";
644 sysread (A, $sa, 1024);
645 sysread (B, $sb, 1024);
646 $equal = 0, last if $sa ne $sb;
647 $equal = 1, last if $sa eq '';
656 my ($file, $expected) = @_;
657 open (FILE, "<$file") or die "$file: open: $!\n";
659 sysread (FILE, $actual, -s FILE);
660 my ($equal) = $actual eq $expected;
666 my ($ln, $lines) = @_;
668 for my $line (@$lines) {
670 $out .= sprintf "%4d %s\n", $ln++, $line;