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 $defines .= "#define THREAD_JOIN_IMPLEMENTED 1\n";
60 if ($defines ne snarf ("pintos/src/constants.h")) {
61 open (CONSTANTS, ">pintos/src/constants.h");
62 print CONSTANTS $defines;
66 # Changes devices/timer.c if necessary.
67 my ($new_time_slice) = $test eq 'priority-fifo' ? 100 : 1;
68 my (@timer_c) = snarf ("pintos/src/devices/timer.c");
69 if (!grep (/^\#define TIME_SLICE $new_time_slice$/, @timer_c)) {
70 @timer_c = grep (!/^\#define TIME_SLICE/, @timer_c);
71 unshift (@timer_c, "#define TIME_SLICE $new_time_slice");
72 open (TIMER_C, ">pintos/src/devices/timer.c");
73 print TIMER_C map ("$_\n", @timer_c);
77 # Changes devices/timer.h if necessary.
78 my ($new_timer_freq) = $test eq 'priority-fifo' ? 19 : 100;
79 my (@timer_h) = snarf ("pintos/src/devices/timer.h");
80 if (!grep (/^\#define TIMER_FREQ $new_time_slice$/, @timer_h)) {
81 @timer_h = grep (!/^\#define TIMER_FREQ/, @timer_h);
82 unshift (@timer_h, "#define TIMER_FREQ $new_timer_freq");
83 open (TIMER_H, ">pintos/src/devices/timer.h");
84 print TIMER_H map ("$_\n", @timer_h);
88 # Copy in the new test.c and delete enough files to ensure a full rebuild.
89 my ($src) = "$GRADES_DIR/" . ($test !~ /^mlfqs/ ? "$test.c" : "mlfqs.c");
90 -e $src or die "$src: stat: $!\n";
91 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
92 unlink ("pintos/src/threads/build/threads/test.o");
93 unlink ("pintos/src/threads/build/kernel.o");
94 unlink ("pintos/src/threads/build/kernel.bin");
95 unlink ("pintos/src/threads/build/os.dsk");
98 if (xsystem ("cd pintos/src/threads && make",
99 LOG => "$test/make") ne 'ok') {
100 $details{$test} = snarf ("output/$test/make.err");
101 return "Compile error";
104 # Copy out files for backtraces later.
105 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
106 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
109 my ($timeout) = $test !~ /^mlfqs/ ? 15 : 600;
110 return run_pintos (["-v", "run", "-q"],
111 CHDIR => "pintos/src/threads/build",
113 TIMEOUT => $timeout);
116 sub grade_alarm_single {
117 verify_alarm (1, @_);
120 sub grade_alarm_multiple {
121 verify_alarm (7, @_);
125 my ($iterations, @output) = @_;
127 verify_common (@output);
130 for (my ($i) = 0; $i < $iterations; $i++) {
131 for (my ($t) = 0; $t < 5; $t++) {
132 push (@products, ($i + 1) * ($t + 1) * 10);
135 @products = sort {$a <=> $b} @products;
139 die $_ if /out of order/i;
141 my ($p) = /product=(\d+)$/;
144 my ($q) = shift (@products);
145 die "Too many wakeups.\n" if !defined $q;
146 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
148 die scalar (@products) . " fewer wakeups than expected.\n"
152 sub grade_alarm_zero {
154 verify_common (@output);
155 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
158 sub grade_alarm_negative {
160 verify_common (@output);
161 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
164 sub grade_join_invalid {
166 verify_common (@output);
167 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
168 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
173 verify_common (@output);
174 grep (/Testing no join/, @output) or die "Test didn't start\n";
175 grep (/No join test done/, @output) or die "Test didn't complete\n";
178 sub grade_join_multiple {
181 verify_common (@output);
183 $t[4] = $t[5] = $t[6] = -1;
186 my ($idx) = /^Thread (\d+)/ or next;
187 my ($iter) = /iteration (\d+)$/;
188 $iter = 5 if /done!$/;
189 die "Malformed output\n" if !defined $iter;
191 die "Thread 6 started before either other thread finished\n"
192 if $t[4] < 5 && $t[5] < 5;
193 die "Thread 6 started before thread 4 finished\n"
195 die "Thread 6 started before thread 5 finished\n"
198 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
203 for my $idx (4, 5, 6) {
204 if ($t[$idx] == -1) {
205 $err .= "Thread $idx did not run at all\n";
206 } elsif ($t[$idx] != 5) {
207 $err .= "Thread $idx only completed $t[$idx] iterations\n";
210 die $err if $err ne '';
213 sub grade_priority_fifo {
216 verify_common (@output);
217 my ($thread_cnt) = 10;
220 my (@t) = (-1) x $thread_cnt;
223 my ($idx) = /^Thread (\d+)/ or next;
224 my ($iter) = /iteration (\d+)$/;
225 $iter = $iter_cnt if /done!$/;
226 die "Malformed output\n" if !defined $iter;
227 if (@order < $thread_cnt) {
229 die "Thread $idx repeated within first $thread_cnt iterations: "
230 . join (' ', @order) . ".\n"
231 if grep ($_ == $idx, @order) != 1;
233 die "Thread $idx ran when $order[0] should have.\n"
234 if $idx != $order[0];
235 push (@order, shift @order);
237 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
242 for my $idx (0..$#t) {
243 if ($t[$idx] == -1) {
244 $err .= "Thread $idx did not run at all.\n";
245 } elsif ($t[$idx] != $iter_cnt) {
246 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
249 die $err if $err ne '';
254 verify_common (@output);
255 our (@mlfqs_on_stats) = mlfqs_stats (@output);
258 sub grade_mlfqs_off {
260 verify_common (@output);
261 our (@mlfqs_off_stats) = mlfqs_stats (@output);
264 sub grade_mlfqs_speedup {
265 our (@mlfqs_off_stats);
266 our (@mlfqs_on_stats);
269 my ($off_ticks) = $mlfqs_off_stats[1];
270 my ($on_ticks) = $mlfqs_on_stats[1];
271 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
272 if $on_ticks >= $off_ticks;
276 $result{'mlfqs-speedup'} = $@;
279 sub grade_mlfqs_priority {
280 our (@mlfqs_off_stats);
281 our (@mlfqs_on_stats);
283 check_mlfqs () if !defined (@mlfqs_on_stats);
284 for my $cat qw (CPU IO MIX) {
285 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
286 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
287 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
288 die "Minimum priority never changed from PRI_DEFAULT (29) "
290 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
291 die "Maximum priority never changed from PRI_DEFAULT (29) "
293 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
298 $result{'mlfqs-priority'} = $@;
302 our (@mlfqs_off_stats);
303 our (@mlfqs_on_stats);
304 die "p1-4 didn't finish with MLFQS on or off\n"
305 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
306 die "p1-4 didn't finish with MLFQS on\n"
307 if !defined (@mlfqs_on_stats);
308 die "p1-4 didn't finish with MLFQS off\n"
309 if !defined (@mlfqs_off_stats);
314 my (%stats) = (CPU => {}, IO => {}, MIX => {});
315 my (%map) = ("CPU intensive" => 'CPU',
316 "IO intensive" => 'IO',
317 "Alternating IO/CPU" => 'MIX');
318 my (%rmap) = reverse %map;
322 $ticks = $1 if /Timer: (\d+) ticks/;
323 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
324 my ($t) = $map{$thread} or next;
326 my ($s) = $stats{$t};
329 $$s{SUM2} += $pri * $pri;
330 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
331 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
334 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
335 for my $cat (values (%map)) {
336 my ($s) = $stats{$cat};
337 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
338 if $$s{N} != $expect_n{$cat};
339 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
341 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
343 $$s{MEAN} = $$s{SUM} / $$s{N};
346 return (\%stats, $ticks);