3 # Find the directory that contains the grading files.
6 # Add our Perl library directory to the include path.
8 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
9 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
10 unshift @INC, "$GRADES_DIR/../lib";
17 our ($hw) = "threads";
18 our (@TESTS); # Tests to run.
24 parse_cmd_line qw (alarm-single alarm-multiple alarm-zero alarm-negative
26 join-quick join-multiple join-nested
27 join-dummy join-invalid join-no
28 priority-preempt priority-fifo priority-donate-one
29 priority-donate-multiple priority-donate-nest
32 clean_dir (), exit if $action eq 'clean';
35 exit if $action eq 'extract';
38 exit if $action eq 'build';
40 run_and_grade_tests ();
41 if (defined ($result{'mlfqs-on'}) && defined ($result{'mlfqs-off'})) {
42 grade_mlfqs_speedup ();
43 grade_mlfqs_priority ();
47 exit success () if $action eq 'test';
49 assemble_final_grade ();
50 exit success () if $action eq 'assemble';
52 die "Don't know how to '$action'";
54 # Runs $test in directory output/$test.
55 # Returns 'ok' if it went ok, otherwise an explanation.
57 # Change constants.h if necessary.
58 my ($defines) = $test ne 'mlfqs-on' ? "" : "#define MLFQS 1\n";
59 if ($defines ne snarf ("pintos/src/constants.h")) {
60 open (CONSTANTS, ">pintos/src/constants.h");
61 print CONSTANTS $defines;
65 # Changes devices/timer.c if necessary.
66 my ($new_time_slice) = $test eq 'priority-fifo' ? 100 : 1;
67 my (@timer) = snarf ("pintos/src/devices/timer.c");
68 if (!grep (/^\#define TIME_SLICE $new_time_slice$/, @timer)) {
69 @timer = grep (!/^\#define TIME_SLICE/, @timer);
70 unshift (@timer, "#define TIME_SLICE $new_time_slice");
71 open (TIMER, ">pintos/src/devices/timer.c");
72 print TIMER map ("$_\n", @timer);
76 # Copy in the new test.c and delete enough files to ensure a full rebuild.
77 my ($src) = "$GRADES_DIR/" . ($test !~ /^mlfqs/ ? "$test.c" : "mlfqs.c");
78 -e $src or die "$src: stat: $!\n";
79 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
80 unlink ("pintos/src/threads/build/threads/test.o");
81 unlink ("pintos/src/threads/build/kernel.o");
82 unlink ("pintos/src/threads/build/kernel.bin");
83 unlink ("pintos/src/threads/build/os.dsk");
86 if (xsystem ("cd pintos/src/threads && make",
87 LOG => "$test/make") ne 'ok') {
88 $details{$test} = snarf ("output/$test/make.err");
89 return "Compile error";
92 # Copy out files for backtraces later.
93 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
94 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
97 my ($timeout) = $test !~ /^mlfqs/ ? 10 : 600;
98 return run_pintos ("cd pintos/src/threads/build && pintos -v run -q",
100 TIMEOUT => $timeout);
103 sub grade_alarm_single {
104 verify_alarm (1, @_);
107 sub grade_alarm_multiple {
108 verify_alarm (7, @_);
112 my ($iterations, @output) = @_;
114 verify_common (@output);
117 for (my ($i) = 0; $i < $iterations; $i++) {
118 for (my ($t) = 0; $t < 5; $t++) {
119 push (@products, ($i + 1) * ($t + 1) * 10);
122 @products = sort {$a <=> $b} @products;
126 die $_ if /out of order/i;
128 my ($p) = /product=(\d+)$/;
131 my ($q) = shift (@products);
132 die "Too many wakeups.\n" if !defined $q;
133 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
135 die scalar (@products) . " fewer wakeups than expected.\n"
139 sub grade_alarm_zero {
141 verify_common (@output);
142 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
145 sub grade_alarm_negative {
147 verify_common (@output);
148 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
151 sub grade_join_invalid {
153 verify_common (@output);
154 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
155 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
160 verify_common (@output);
161 grep (/Testing no join/, @output) or die "Test didn't start\n";
162 grep (/No join test done/, @output) or die "Test didn't complete\n";
165 sub grade_join_multiple {
168 verify_common (@output);
170 $t[4] = $t[5] = $t[6] = -1;
173 my ($idx) = /^Thread (\d+)/ or next;
174 my ($iter) = /iteration (\d+)$/;
175 $iter = 5 if /done!$/;
176 die "Malformed output\n" if !defined $iter;
178 die "Thread 6 started before either other thread finished\n"
179 if $t[4] < 5 && $t[5] < 5;
180 die "Thread 6 started before thread 4 finished\n"
182 die "Thread 6 started before thread 5 finished\n"
185 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
190 for my $idx (4, 5, 6) {
191 if ($t[$idx] == -1) {
192 $err .= "Thread $idx did not run at all\n";
193 } elsif ($t[$idx] != 5) {
194 $err .= "Thread $idx only completed $t[$idx] iterations\n";
197 die $err if $err ne '';
200 sub grade_priority_fifo {
203 verify_common (@output);
204 my ($thread_cnt) = 10;
207 my (@t) = (-1) x $thread_cnt;
210 my ($idx) = /^Thread (\d+)/ or next;
211 my ($iter) = /iteration (\d+)$/;
212 $iter = $iter_cnt if /done!$/;
213 die "Malformed output\n" if !defined $iter;
214 if (@order < $thread_cnt) {
216 die "Thread $idx repeated within first $thread_cnt iterations: "
217 . join (' ', @order) . ".\n"
218 if grep ($_ == $idx, @order) != 1;
220 die "Thread $idx ran when $order[0] should have.\n"
221 if $idx != $order[0];
222 push (@order, shift @order);
224 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
229 for my $idx (0..$#t) {
230 if ($t[$idx] == -1) {
231 $err .= "Thread $idx did not run at all.\n";
232 } elsif ($t[$idx] != $iter_cnt) {
233 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
236 die $err if $err ne '';
241 verify_common (@output);
242 our (@mlfqs_on_stats) = mlfqs_stats (@output);
245 sub grade_mlfqs_off {
247 verify_common (@output);
248 our (@mlfqs_off_stats) = mlfqs_stats (@output);
251 sub grade_mlfqs_speedup {
252 our (@mlfqs_off_stats);
253 our (@mlfqs_on_stats);
256 my ($off_ticks) = $mlfqs_off_stats[1];
257 my ($on_ticks) = $mlfqs_on_stats[1];
258 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
259 if $on_ticks >= $off_ticks;
263 $result{'mlfqs-speedup'} = $@;
266 sub grade_mlfqs_priority {
267 our (@mlfqs_off_stats);
268 our (@mlfqs_on_stats);
270 check_mlfqs () if !defined (@mlfqs_on_stats);
271 for my $cat qw (CPU IO MIX) {
272 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
273 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
274 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
275 die "Minimum priority never changed from PRI_DEFAULT (29) "
277 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
278 die "Maximum priority never changed from PRI_DEFAULT (29) "
280 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
285 $result{'mlfqs-priority'} = $@;
289 our (@mlfqs_off_stats);
290 our (@mlfqs_on_stats);
291 die "p1-4 didn't finish with MLFQS on or off\n"
292 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
293 die "p1-4 didn't finish with MLFQS on\n"
294 if !defined (@mlfqs_on_stats);
295 die "p1-4 didn't finish with MLFQS off\n"
296 if !defined (@mlfqs_off_stats);
301 my (%stats) = (CPU => {}, IO => {}, MIX => {});
302 my (%map) = ("CPU intensive" => 'CPU',
303 "IO intensive" => 'IO',
304 "Alternating IO/CPU" => 'MIX');
305 my (%rmap) = reverse %map;
309 $ticks = $1 if /Timer: (\d+) ticks/;
310 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
311 my ($t) = $map{$thread} or next;
313 my ($s) = $stats{$t};
316 $$s{SUM2} += $pri * $pri;
317 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
318 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
321 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
322 for my $cat (values (%map)) {
323 my ($s) = $stats{$cat};
324 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
325 if $$s{N} != $expect_n{$cat};
326 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
328 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
330 $$s{MEAN} = $$s{SUM} / $$s{N};
333 return (\%stats, $ticks);