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.
78 $result = run_test ($test);
79 if ($result eq 'ok') {
80 $result = grade_test ($test);
84 } while ($result eq 'rerun');
86 $result{$test} = $result;
95 = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
97 die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
99 # Sort tarballs in reverse order by time.
100 @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
102 print "Multiple tarballs: choosing $tarballs[0]\n"
103 if scalar (@tarballs) > 1;
107 sub extract_tarball {
108 my ($tarball) = choose_tarball ();
110 mkdir "pintos" or die "pintos: mkdir: $!\n";
111 mkdir "pintos/src" or die "pintos: mkdir: $!\n";
113 print "Extracting $tarball...\n";
114 xsystem ("cd pintos/src && tar xzf ../../$tarball",
115 DIE => "extraction failed\n");
117 print "Patching...\n";
118 xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
120 DIE => "patch failed\n");
125 my ($ms, $d, $y, $H, $M, $S) =
126 $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
128 my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
130 return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
135 my ($src) = "$GRADES_DIR/$test.c";
136 $src = "$GRADES_DIR/mlfqs.c" if $test =~ /^mlfqs/;
137 -e $src or die "$src: stat: $!\n";
143 $defines .= "#define MLFQS 1\n" if $test eq 'mlfqs-on';
149 return "ok" if -f "output/$test.run.out";
151 my ($defines) = test_constants ($test);
152 if ($defines ne snarf ("pintos/src/constants.h")) {
153 open (CONSTANTS, ">pintos/src/constants.h");
154 print CONSTANTS $defines;
158 my ($src) = test_source ($test);
159 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
160 unlink ("pintos/src/threads/build/threads/test.o");
161 unlink ("pintos/src/threads/build/kernel.o");
162 unlink ("pintos/src/threads/build/kernel.bin");
163 unlink ("pintos/src/threads/build/os.dsk");
164 xsystem ("cd pintos/src/threads && make", LOG => "$test.make")
165 or return "compile error";
168 $timeout = 600 if $test =~ /^mlfqs/;
169 xsystem ("cd pintos/src/threads/build && pintos -v run -q",
172 or return "Bochs error";
179 my (@output) = snarf ("output/$test.run.out");
181 if (-e "$GRADES_DIR/$test.exp") {
183 verify_common (@output);
184 compare_output ("$GRADES_DIR/$test.exp", @output);
188 ($grade_func = $test) =~ s/-/_/g;
189 eval "grade_$grade_func (\@output)";
192 die $@ if $@ =~ /at \S+ line \d+$/;
198 sub grade_alarm_single {
199 verify_alarm (1, @_);
202 sub grade_alarm_multiple {
203 verify_alarm (7, @_);
207 my ($iterations, @output) = @_;
209 verify_common (@output);
212 for (my ($i) = 0; $i < $iterations; $i++) {
213 for (my ($t) = 0; $t < 5; $t++) {
214 push (@products, ($i + 1) * ($t + 1) * 10);
217 @products = sort {$a <=> $b} @products;
221 die $_ if /Out of order/;
223 my ($p) = /product=(\d+)$/;
226 my ($q) = shift (@products);
227 die "Too many wakeups.\n" if !defined $q;
228 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
230 die scalar (@products) . " fewer wakeups than expected.\n"
234 sub grade_alarm_zero {
236 verify_common (@output);
237 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
240 sub grade_alarm_negative {
242 verify_common (@output);
243 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
246 sub grade_join_invalid {
248 verify_common (@output);
249 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
250 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
255 verify_common (@output);
256 grep (/Testing no join/, @output) or die "Test didn't start\n";
257 grep (/No join test done/, @output) or die "Test didn't complete\n";
260 sub grade_join_multiple {
263 verify_common (@output);
265 $t[4] = $t[5] = $t[6] = -1;
268 my ($idx) = /^Thread (\d+)/ or next;
269 my ($iter) = /iteration (\d+)$/;
270 $iter = 5 if /done!$/;
271 die "Malformed output\n" if !defined $iter;
273 die "Thread 6 started before either other thread finished\n"
274 if $t[4] < 5 && $t[5] < 5;
275 die "Thread 6 started before thread 4 finished\n"
277 die "Thread 6 started before thread 5 finished\n"
280 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
285 for my $idx (4, 5, 6) {
286 if ($t[$idx] == -1) {
287 $err .= "Thread $idx did not run at all\n";
288 } elsif ($t[$idx] != 5) {
289 $err .= "Thread $idx only completed $t[$idx] iterations\n";
292 die $err if $err ne '';
295 sub grade_priority_fifo {
298 verify_common (@output);
299 my ($thread_cnt) = 10;
302 my (@t) = (-1) x $thread_cnt;
305 my ($idx) = /^Thread (\d+)/ or next;
306 my ($iter) = /iteration (\d+)$/;
307 $iter = $iter_cnt if /done!$/;
308 die "Malformed output\n" if !defined $iter;
309 if (@order < $thread_cnt) {
311 die "Thread $idx repeated within first $thread_cnt iterations: "
312 . join (' ', @order) . ".\n"
313 if grep ($_ == $idx, @order) != 1;
315 die "Thread $idx ran when $order[0] should have.\n"
316 if $idx != $order[0];
317 push (@order, shift @order);
319 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
324 for my $idx (0..$#t) {
325 if ($t[$idx] == -1) {
326 $err .= "Thread $idx did not run at all.\n";
327 } elsif ($t[$idx] != $iter_cnt) {
328 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
331 die $err if $err ne '';
336 verify_common (@output);
337 mlfqs_stats (@output);
340 sub grade_mlfqs_off {
342 verify_common (@output);
343 mlfqs_stats (@output);
348 my (%stats) = ("io" => {}, "cpu" => {}, "mix" => {});
349 my (%map) = ("CPU intensive" => "cpu",
350 "IO intensive" => "io",
351 "Alternating IO/CPU" => "mix");
354 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
355 my ($t) = $map{$thread} or next;
357 my ($s) = $stats{$t};
360 $$s{"sum2"} += $pri * $pri;
361 $$s{"min"} = $pri if !defined ($$s{"min"}) || $pri < $$s{"min"};
362 $$s{"max"} = $pri if !defined ($$s{"max"}) || $pri > $$s{"max"};
366 for my $t (keys %stats) {
367 my ($s) = $stats{$t};
368 $details .= "$t: n=$$s{'n'}, min=$$s{'min'}, max=$$s{'max'}, avg=" . int ($$s{'sum'} / $$s{'n'}) . "\n";
370 $details{$test} = $details;
377 my (@assertion) = grep (/PANIC/, @output);
378 if (@assertion != 0) {
379 my ($details) = "Kernel panic:\n $assertion[0]\n";
381 my (@stack_line) = grep (/Call stack:/, @output);
382 if (@stack_line != 0) {
384 $details .= " $stack_line[0]\n\n";
385 $details .= "Translation of backtrace:\n";
386 my ($addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
390 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
393 $A2L = "i386-elf-addr2line";
395 open (A2L, "$A2L -fe pintos/src/threads/build/kernel.o $addrs|");
396 while (my $function = <A2L>) {
400 $details .= " $function ($line)\n";
403 $extra{$test} = $details;
404 die "Kernel panic. Details at end of file.\n"
407 die "No output at all\n" if @output == 0;
408 die "Didn't start up properly: no \"Pintos booting\" startup message\n"
409 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
410 die "Didn't start up properly: no \"Boot complete\" startup message\n"
411 if !grep (/Boot complete/, @output);
412 die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
413 if !grep (/Timer: \d+ ticks/, @output);
414 die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
415 if !grep (/Powering off/, @output);
419 if (!files_equal ("pintos/src/threads/test.c", test_source ($test))
420 || !file_contains ("pintos/src/constants.h",
421 test_constants ($test))) {
422 unlink ("output/$test.run.out")
423 or die "output/$test.run.out: unlink: $!\n";
429 my ($exp_file, @actual) = @_;
430 my (@expected) = snarf ($exp_file);
432 @actual = map ("$_\n", @actual);
433 @expected = map ("$_\n", @expected);
435 # Trim header and trailer from @actual.
436 while (scalar (@actual) && $actual[0] ne $expected[0]) {
439 die "First line of expected output was not present.\n" if !@actual;
440 while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
443 die "Final line of expected output was not present.\n" if !@actual;
445 # Check whether they're the same.
446 if ($#actual == $#expected) {
448 for (my ($i) = 0; $i <= $#expected; $i++) {
449 $eq = 0 if $actual[$i] ne $expected[$i];
454 # They differ. Output a diff.
456 my ($d) = Algorithm::Diff->new (\@expected, \@actual);
458 while ($d->Next ()) {
459 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
462 $diff .= "Actual lines $af...$al match expected lines "
465 $diff .= "Actual line $af matches expected line $ef.\n";
468 my (@i1) = $d->Items (1);
469 my (@i2) = $d->Items (2);
471 $diff .= "Extra or misplaced line(s) $af...$al "
472 . "in actual output:\n";
473 $diff .= number_lines ($af, \@i2);
474 } elsif (!$d->Items (2)) {
475 $diff .= "Expected line(s) $ef...$el missing or misplaced:\n";
476 $diff .= number_lines ($ef, \@i1);
478 $diff .= "The following expected line(s) $ef...$el:\n";
479 $diff .= number_lines ($ef, \@i1);
480 $diff .= "became actual line(s) $af...$al:\n";
481 $diff .= number_lines ($af, \@i2);
487 $details .= "$test actual output (line numbers added):\n";
488 $details .= number_lines (1, \@actual);
489 $details .= "\n$test expected output (line numbers added):\n";
490 $details .= number_lines (1, \@expected);
491 $details .= "\n$diff\n";
492 $details{$test} = $details;
493 die "Output differs from expected. Details at end of file.\n";
497 my (@summary) = snarf ("$GRADES_DIR/tests.txt");
502 for (my ($i) = 0; $i <= $#summary; $i++) {
503 local ($_) = $summary[$i];
504 if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
505 my ($result) = $result{$test} || "Not tested.";
507 if ($result eq 'ok') {
508 splice (@summary, $i, 1);
513 splice (@summary, $i + 1, 0,
514 map (" $_", split ("\n", $result)));
516 } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
518 $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
519 splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
524 my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
525 $summary[0] =~ s/\[\[total\]\]/$ts/;
527 open (SUMMARY, ">tests.out");
528 print SUMMARY map ("$_\n", @summary);
533 open (DETAILS, ">details.out");
535 for my $test (@TESTS) {
536 next if $result{$test} eq 'ok';
538 my ($details) = $details{$test};
539 next if !defined ($details) && ! -e "output/$test.run.out";
541 print DETAILS "\n" if $n++;
542 print DETAILS "--- $test details ", '-' x (50 - length ($test));
543 print DETAILS "\n\n";
545 if (!defined $details) {
546 $details = "Output:\n\n" . snarf ("output/$test.run.out");
548 print DETAILS $details;
550 print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
551 if defined $extra{$test};
558 my ($command, %options) = @_;
559 print "$command\n" if $VERBOSE || $options{VERBOSE};
561 my ($log) = $options{LOG};
562 if (defined ($log)) {
563 $command = "($command) >output/$log.out 2>output/$log.err";
568 local $SIG{ALRM} = sub { die "alarm\n" };
569 alarm $options{TIMEOUT} if defined $options{TIMEOUT};
571 die "fork: $!\n" if !defined $pid;
572 exec ($command), die "$command: exec: $!\n" if !$pid;
578 die unless $@ eq "alarm\n"; # propagate unexpected errors
579 print "Timed out.\n";
584 if (WIFSIGNALED ($status)) {
585 my ($signal) = WTERMSIG ($status);
586 die "Interrupted\n" if $signal == SIGINT;
587 print "Child terminated with signal $signal\n";
590 unlink ("output/$log.err") if defined ($log) && $status == 0;
597 open (OUTPUT, $file) or die "$file: open: $!\n";
598 my (@lines) = <OUTPUT>;
601 return wantarray ? @lines : join ('', map ("$_\n", @lines));
607 open (A, "<$a") or die "$a: open: $!\n";
608 open (B, "<$b") or die "$b: open: $!\n";
614 sysread (A, $sa, 1024);
615 sysread (B, $sb, 1024);
616 $equal = 0, last if $sa ne $sb;
617 $equal = 1, last if $sa eq '';
626 my ($file, $expected) = @_;
627 open (FILE, "<$file") or die "$file: open: $!\n";
629 sysread (FILE, $actual, -s FILE);
630 my ($equal) = $actual eq $expected;
636 my ($ln, $lines) = @_;
638 for my $line (@$lines) {
640 $out .= sprintf "%4d %s\n", $ln++, $line;