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";
176 # Make output directory.
177 mkdir "output/$test";
179 # Change constants.h if necessary.
180 my ($defines) = test_constants ($test);
181 if ($defines ne snarf ("pintos/src/constants.h")) {
182 open (CONSTANTS, ">pintos/src/constants.h");
183 print CONSTANTS $defines;
187 # Copy in the new test.c and delete enough files to ensure a full rebuild.
188 my ($src) = test_source ($test);
189 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
190 unlink ("pintos/src/threads/build/threads/test.o");
191 unlink ("pintos/src/threads/build/kernel.o");
192 unlink ("pintos/src/threads/build/kernel.bin");
193 unlink ("pintos/src/threads/build/os.dsk");
196 xsystem ("cd pintos/src/threads && make", LOG => "$test/make")
197 or return "compile error";
199 # Copy out files for backtraces later.
200 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
201 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
205 $timeout = 600 if $test =~ /^mlfqs/;
206 xsystem ("cd pintos/src/threads/build && pintos -v run -q",
209 or return "Bochs error";
217 my (@output) = snarf ("output/$test/run.out");
219 if (-e "$GRADES_DIR/$test.exp") {
221 verify_common (@output);
222 compare_output ("$GRADES_DIR/$test.exp", @output);
226 ($grade_func = $test) =~ s/-/_/g;
227 eval "grade_$grade_func (\@output)";
230 die $@ if $@ =~ /at \S+ line \d+$/;
236 sub grade_alarm_single {
237 verify_alarm (1, @_);
240 sub grade_alarm_multiple {
241 verify_alarm (7, @_);
245 my ($iterations, @output) = @_;
247 verify_common (@output);
250 for (my ($i) = 0; $i < $iterations; $i++) {
251 for (my ($t) = 0; $t < 5; $t++) {
252 push (@products, ($i + 1) * ($t + 1) * 10);
255 @products = sort {$a <=> $b} @products;
259 die $_ if /Out of order/;
261 my ($p) = /product=(\d+)$/;
264 my ($q) = shift (@products);
265 die "Too many wakeups.\n" if !defined $q;
266 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
268 die scalar (@products) . " fewer wakeups than expected.\n"
272 sub grade_alarm_zero {
274 verify_common (@output);
275 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
278 sub grade_alarm_negative {
280 verify_common (@output);
281 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
284 sub grade_join_invalid {
286 verify_common (@output);
287 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
288 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
293 verify_common (@output);
294 grep (/Testing no join/, @output) or die "Test didn't start\n";
295 grep (/No join test done/, @output) or die "Test didn't complete\n";
298 sub grade_join_multiple {
301 verify_common (@output);
303 $t[4] = $t[5] = $t[6] = -1;
306 my ($idx) = /^Thread (\d+)/ or next;
307 my ($iter) = /iteration (\d+)$/;
308 $iter = 5 if /done!$/;
309 die "Malformed output\n" if !defined $iter;
311 die "Thread 6 started before either other thread finished\n"
312 if $t[4] < 5 && $t[5] < 5;
313 die "Thread 6 started before thread 4 finished\n"
315 die "Thread 6 started before thread 5 finished\n"
318 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
323 for my $idx (4, 5, 6) {
324 if ($t[$idx] == -1) {
325 $err .= "Thread $idx did not run at all\n";
326 } elsif ($t[$idx] != 5) {
327 $err .= "Thread $idx only completed $t[$idx] iterations\n";
330 die $err if $err ne '';
333 sub grade_priority_fifo {
336 verify_common (@output);
337 my ($thread_cnt) = 10;
340 my (@t) = (-1) x $thread_cnt;
343 my ($idx) = /^Thread (\d+)/ or next;
344 my ($iter) = /iteration (\d+)$/;
345 $iter = $iter_cnt if /done!$/;
346 die "Malformed output\n" if !defined $iter;
347 if (@order < $thread_cnt) {
349 die "Thread $idx repeated within first $thread_cnt iterations: "
350 . join (' ', @order) . ".\n"
351 if grep ($_ == $idx, @order) != 1;
353 die "Thread $idx ran when $order[0] should have.\n"
354 if $idx != $order[0];
355 push (@order, shift @order);
357 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
362 for my $idx (0..$#t) {
363 if ($t[$idx] == -1) {
364 $err .= "Thread $idx did not run at all.\n";
365 } elsif ($t[$idx] != $iter_cnt) {
366 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
369 die $err if $err ne '';
374 verify_common (@output);
375 mlfqs_stats (@output);
378 sub grade_mlfqs_off {
380 verify_common (@output);
381 mlfqs_stats (@output);
386 my (%stats) = ("io" => {}, "cpu" => {}, "mix" => {});
387 my (%map) = ("CPU intensive" => "cpu",
388 "IO intensive" => "io",
389 "Alternating IO/CPU" => "mix");
392 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
393 my ($t) = $map{$thread} or next;
395 my ($s) = $stats{$t};
398 $$s{"sum2"} += $pri * $pri;
399 $$s{"min"} = $pri if !defined ($$s{"min"}) || $pri < $$s{"min"};
400 $$s{"max"} = $pri if !defined ($$s{"max"}) || $pri > $$s{"max"};
404 for my $t (keys %stats) {
405 my ($s) = $stats{$t};
406 $details .= "$t: n=$$s{'n'}, min=$$s{'min'}, max=$$s{'max'}, avg=" . int ($$s{'sum'} / $$s{'n'}) . "\n";
408 $details{$test} = $details;
415 my (@assertion) = grep (/PANIC/, @output);
416 if (@assertion != 0) {
417 my ($details) = "Kernel panic:\n $assertion[0]\n";
419 my (@stack_line) = grep (/Call stack:/, @output);
420 if (@stack_line != 0) {
421 $details .= " $stack_line[0]\n\n";
422 $details .= "Translation of backtrace:\n";
423 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
427 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
430 $A2L = "i386-elf-addr2line";
432 open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
433 while (my $function = <A2L>) {
437 $details .= " $function ($line)\n";
440 $extra{$test} = $details;
441 die "Kernel panic. Details at end of file.\n"
444 die "No output at all\n" if @output == 0;
445 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
446 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
447 die "Didn't start up properly: no \"Boot complete\" startup message\n"
448 if !grep (/Boot complete/, @output);
449 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
450 if !grep (/Timer: \d+ ticks/, @output);
451 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
452 if !grep (/Powering off/, @output);
456 my ($exp_file, @actual) = @_;
457 my (@expected) = snarf ($exp_file);
459 @actual = map ("$_\n", @actual);
460 @expected = map ("$_\n", @expected);
462 # Trim header and trailer from @actual.
463 while (scalar (@actual) && $actual[0] ne $expected[0]) {
466 die "First line of expected output was not present.\n" if !@actual;
467 while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
470 die "Final line of expected output was not present.\n" if !@actual;
472 # Check whether they're the same.
473 if ($#actual == $#expected) {
475 for (my ($i) = 0; $i <= $#expected; $i++) {
476 $eq = 0 if $actual[$i] ne $expected[$i];
481 # They differ. Output a diff.
483 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
485 while ($d->Next ()) {
486 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
489 $diff .= "Actual lines $af...$al match expected lines "
492 $diff .= "Actual line $af matches expected line $ef.\n";
495 my (@i1) = $d->Items (1);
496 my (@i2) = $d->Items (2);
498 $diff .= "Extra or misplaced line(s) $af...$al "
499 . "in actual output:\n";
500 $diff .= number_lines ($af, \@i2);
501 } elsif (!$d->Items (2)) {
502 $diff .= "Expected line(s) $ef...$el missing or misplaced:\n";
503 $diff .= number_lines ($ef, \@i1);
505 $diff .= "The following expected line(s) $ef...$el:\n";
506 $diff .= number_lines ($ef, \@i1);
507 $diff .= "became actual line(s) $af...$al:\n";
508 $diff .= number_lines ($af, \@i2);
514 $details .= "$test actual output (line numbers added):\n";
515 $details .= number_lines (1, \@actual);
516 $details .= "\n$test expected output (line numbers added):\n";
517 $details .= number_lines (1, \@expected);
518 $details .= "\n$diff\n";
519 $details{$test} = $details;
520 die "Output differs from expected. Details at end of file.\n";
524 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
529 for (my ($i) = 0; $i <= $#summary; $i++) {
530 local ($_) = $summary[$i];
531 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
532 my ($result) = $result{$test} || "Not tested.";
534 if ($result eq 'ok') {
535 splice (@summary, $i, 1);
540 splice (@summary, $i + 1, 0,
541 map (" $_", split ("\n", $result)));
543 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
545 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
546 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
551 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
552 $summary[0] =~ s/\[\[total\]\]/$ts/;
554 open (SUMMARY, ">tests.out");
555 print SUMMARY map ("$_\n", @summary);
560 open (DETAILS, ">details.out");
562 for my $test (@TESTS) {
563 next if $result{$test} eq 'ok';
565 my ($details) = $details{$test};
566 next if !defined ($details) && ! -e "output/$test/run.out";
568 print DETAILS "\n" if $n++;
569 print DETAILS "--- $test details ", '-' x (50 - length ($test));
570 print DETAILS "\n\n";
572 if (!defined $details) {
573 $details = "Output:\n\n" . snarf ("output/$test/run.out");
575 print DETAILS $details;
577 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
578 if defined $extra{$test};
585 my ($command, %options) = @_;
586 print "$command\n" if $VERBOSE || $options{VERBOSE};
588 my ($log) = $options{LOG};
589 if (defined ($log)) {
590 $command = "($command) >output/$log.out 2>output/$log.err";
595 local $SIG{ALRM} = sub { die "alarm\n" };
596 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
598 die "fork: $!\n" if !defined $pid;
599 exec ($command), die "$command: exec: $!\n" if !$pid;
605 die unless $@ eq "alarm\n"; # propagate unexpected errors
606 print "Timed out.\n";
611 if (WIFSIGNALED ($status)) {
612 my ($signal) = WTERMSIG ($status);
613 die "Interrupted\n" if $signal == SIGINT;
614 print "Child terminated with signal $signal\n";
617 unlink ("output/$log.err") if defined ($log) && $status == 0;
624 open (OUTPUT, $file) or die "$file: open: $!\n";
625 my (@lines) = <OUTPUT>;
628 return wantarray ? @lines : join ('', map ("$_\n", @lines));
634 open (A, "<$a") or die "$a: open: $!\n";
635 open (B, "<$b") or die "$b: open: $!\n";
641 sysread (A, $sa, 1024);
642 sysread (B, $sb, 1024);
643 $equal = 0, last if $sa ne $sb;
644 $equal = 1, last if $sa eq '';
653 my ($file, $expected) = @_;
654 open (FILE, "<$file") or die "$file: open: $!\n";
656 sysread (FILE, $actual, -s FILE);
657 my ($equal) = $actual eq $expected;
663 my ($ln, $lines) = @_;
665 for my $line (@$lines) {
667 $out .= sprintf "%4d %s\n", $ln++, $line;