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")
46 # Find the directory that contains the grading files.
48 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
49 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
52 # Verify that we're roughly in the correct directory
53 # before we go blasting away files.
56 xsystem ("rm -rf output pintos", VERBOSE => 1);
57 xsystem ("rm -f details.out tests.out", VERBOSE => 1);
60 # Create output directory, if it doesn't already exist.
61 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
64 extract_tarball () if ! -d "pintos";
66 # Verify that the proper directory was submitted.
67 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
69 # Run and grade the tests.
76 my ($result) = run_test ($test);
77 if ($result eq 'ok') {
78 $result = grade_test ($test);
83 $result{$test} = $result;
92 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
94 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
96 # Sort tarballs in reverse order by time.
97 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
99 print "Multiple tarballs: choosing $tarballs[0]\n"
100 if scalar (@tarballs) > 1;
104 sub extract_tarball {
105 my ($tarball) = choose_tarball ();
107 mkdir "pintos" or die "pintos: mkdir: $!\n";
108 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
110 print "Extracting $tarball...\n";
111 xsystem ("cd pintos/src && tar xzf ../../$tarball",
112 DIE => "extraction failed\n");
114 print "Patching...\n";
115 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
117 DIE => "patch failed\n");
122 my ($ms, $d, $y, $H, $M, $S) =
123 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
125 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
127 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
132 my ($src) = "$GRADES_DIR/$test.c";
133 $src = "$GRADES_DIR/mlfqs.c" if $test =~ /^mlfqs/;
134 -e $src or die "$src: stat: $!\n";
140 $defines .= "#define MLFQS 1\n" if $test eq 'mlfqs-on';
146 return "ok" if -f "output/$test/done";
148 # Reuse older results if any.
149 if (open (DONE, "<output/$test/done")) {
157 # Really run the test.
158 my ($status) = really_run_test ($test);
160 # Save the results for later.
161 open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
162 print DONE "$status\n";
168 sub really_run_test {
170 # If there's residue from an earlier test, move it to .old.
171 # If there's already a .old, delete it.
172 xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
173 rename "output/$test", "output/$test.old" or die "rename: $!\n"
174 if -d "output/$test";
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|");
434 my ($function, $line);
435 last unless defined ($function = <A2L>);
439 $details .= " $function ($line)\n";
442 $extra{$test} = $details;
443 die "Kernel panic. Details at end of file.\n"
446 die "No output at all\n" if @output == 0;
447 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
448 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
449 die "Didn't start up properly: no \"Boot complete\" startup message\n"
450 if !grep (/Boot complete/, @output);
451 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
452 if !grep (/Timer: \d+ ticks/, @output);
453 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
454 if !grep (/Powering off/, @output);
458 my ($exp_file, @actual) = @_;
459 my (@expected) = snarf ($exp_file);
461 @actual = map ("$_\n", @actual);
462 @expected = map ("$_\n", @expected);
464 # Trim header and trailer from @actual.
465 while (scalar (@actual) && $actual[0] ne $expected[0]) {
468 die "First line of expected output was not present.\n" if !@actual;
469 while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
472 die "Final line of expected output was not present.\n" if !@actual;
474 # Check whether they're the same.
475 if ($#actual == $#expected) {
477 for (my ($i) = 0; $i <= $#expected; $i++) {
478 $eq = 0 if $actual[$i] ne $expected[$i];
483 # They differ. Output a diff.
485 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
487 while ($d->Next ()) {
488 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
491 $diff .= "Actual lines $af...$al match expected lines "
494 $diff .= "Actual line $af matches expected line $ef.\n";
497 my (@i1) = $d->Items (1);
498 my (@i2) = $d->Items (2);
500 $diff .= "Extra or misplaced line(s) $af...$al "
501 . "in actual output:\n";
502 $diff .= number_lines ($af, \@i2);
503 } elsif (!$d->Items (2)) {
504 $diff .= "Expected line(s) $ef...$el missing or misplaced:\n";
505 $diff .= number_lines ($ef, \@i1);
507 $diff .= "The following expected line(s) $ef...$el:\n";
508 $diff .= number_lines ($ef, \@i1);
509 $diff .= "became actual line(s) $af...$al:\n";
510 $diff .= number_lines ($af, \@i2);
516 $details .= "$test actual output (line numbers added):\n";
517 $details .= number_lines (1, \@actual);
518 $details .= "\n$test expected output (line numbers added):\n";
519 $details .= number_lines (1, \@expected);
520 $details .= "\n$diff\n";
521 $details{$test} = $details;
522 die "Output differs from expected. Details at end of file.\n";
526 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
531 for (my ($i) = 0; $i <= $#summary; $i++) {
532 local ($_) = $summary[$i];
533 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
534 my ($result) = $result{$test} || "Not tested.";
536 if ($result eq 'ok') {
537 splice (@summary, $i, 1);
542 splice (@summary, $i + 1, 0,
543 map (" $_", split ("\n", $result)));
545 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
547 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
548 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
553 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
554 $summary[0] =~ s/\[\[total\]\]/$ts/;
556 open (SUMMARY, ">tests.out");
557 print SUMMARY map ("$_\n", @summary);
562 open (DETAILS, ">details.out");
564 for my $test (@TESTS) {
565 next if $result{$test} eq 'ok';
567 my ($details) = $details{$test};
568 next if !defined ($details) && ! -e "output/$test/run.out";
570 print DETAILS "\n" if $n++;
571 print DETAILS "--- $test details ", '-' x (50 - length ($test));
572 print DETAILS "\n\n";
574 if (!defined $details) {
575 $details = "Output:\n\n" . snarf ("output/$test/run.out");
577 print DETAILS $details;
579 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
580 if defined $extra{$test};
587 my ($command, %options) = @_;
588 print "$command\n" if $VERBOSE || $options{VERBOSE};
590 my ($log) = $options{LOG};
591 if (defined ($log)) {
592 $command = "($command) >output/$log.out 2>output/$log.err";
597 local $SIG{ALRM} = sub { die "alarm\n" };
598 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
600 die "fork: $!\n" if !defined $pid;
601 exec ($command), die "$command: exec: $!\n" if !$pid;
607 die unless $@ eq "alarm\n"; # propagate unexpected errors
608 print "Timed out.\n";
613 if (WIFSIGNALED ($status)) {
614 my ($signal) = WTERMSIG ($status);
615 die "Interrupted\n" if $signal == SIGINT;
616 print "Child terminated with signal $signal\n";
619 unlink ("output/$log.err") if defined ($log) && $status == 0;
626 open (OUTPUT, $file) or die "$file: open: $!\n";
627 my (@lines) = <OUTPUT>;
630 return wantarray ? @lines : join ('', map ("$_\n", @lines));
636 open (A, "<$a") or die "$a: open: $!\n";
637 open (B, "<$b") or die "$b: open: $!\n";
643 sysread (A, $sa, 1024);
644 sysread (B, $sb, 1024);
645 $equal = 0, last if $sa ne $sb;
646 $equal = 1, last if $sa eq '';
655 my ($file, $expected) = @_;
656 open (FILE, "<$file") or die "$file: open: $!\n";
658 sysread (FILE, $actual, -s FILE);
659 my ($equal) = $actual eq $expected;
665 my ($ln, $lines) = @_;
667 for my $line (@$lines) {
669 $out .= sprintf "%4d %s\n", $ln++, $line;