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 (["-v", "run", "-q"],
99 CHDIR => "pintos/src/threads/build",
101 TIMEOUT => $timeout);
104 sub grade_alarm_single {
105 verify_alarm (1, @_);
108 sub grade_alarm_multiple {
109 verify_alarm (7, @_);
113 my ($iterations, @output) = @_;
115 verify_common (@output);
118 for (my ($i) = 0; $i < $iterations; $i++) {
119 for (my ($t) = 0; $t < 5; $t++) {
120 push (@products, ($i + 1) * ($t + 1) * 10);
123 @products = sort {$a <=> $b} @products;
127 die $_ if /out of order/i;
129 my ($p) = /product=(\d+)$/;
132 my ($q) = shift (@products);
133 die "Too many wakeups.\n" if !defined $q;
134 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
136 die scalar (@products) . " fewer wakeups than expected.\n"
140 sub grade_alarm_zero {
142 verify_common (@output);
143 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
146 sub grade_alarm_negative {
148 verify_common (@output);
149 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
152 sub grade_join_invalid {
154 verify_common (@output);
155 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
156 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
161 verify_common (@output);
162 grep (/Testing no join/, @output) or die "Test didn't start\n";
163 grep (/No join test done/, @output) or die "Test didn't complete\n";
166 sub grade_join_multiple {
169 verify_common (@output);
171 $t[4] = $t[5] = $t[6] = -1;
174 my ($idx) = /^Thread (\d+)/ or next;
175 my ($iter) = /iteration (\d+)$/;
176 $iter = 5 if /done!$/;
177 die "Malformed output\n" if !defined $iter;
179 die "Thread 6 started before either other thread finished\n"
180 if $t[4] < 5 && $t[5] < 5;
181 die "Thread 6 started before thread 4 finished\n"
183 die "Thread 6 started before thread 5 finished\n"
186 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
191 for my $idx (4, 5, 6) {
192 if ($t[$idx] == -1) {
193 $err .= "Thread $idx did not run at all\n";
194 } elsif ($t[$idx] != 5) {
195 $err .= "Thread $idx only completed $t[$idx] iterations\n";
198 die $err if $err ne '';
201 sub grade_priority_fifo {
204 verify_common (@output);
205 my ($thread_cnt) = 10;
208 my (@t) = (-1) x $thread_cnt;
211 my ($idx) = /^Thread (\d+)/ or next;
212 my ($iter) = /iteration (\d+)$/;
213 $iter = $iter_cnt if /done!$/;
214 die "Malformed output\n" if !defined $iter;
215 if (@order < $thread_cnt) {
217 die "Thread $idx repeated within first $thread_cnt iterations: "
218 . join (' ', @order) . ".\n"
219 if grep ($_ == $idx, @order) != 1;
221 die "Thread $idx ran when $order[0] should have.\n"
222 if $idx != $order[0];
223 push (@order, shift @order);
225 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
230 for my $idx (0..$#t) {
231 if ($t[$idx] == -1) {
232 $err .= "Thread $idx did not run at all.\n";
233 } elsif ($t[$idx] != $iter_cnt) {
234 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
237 die $err if $err ne '';
242 verify_common (@output);
243 our (@mlfqs_on_stats) = mlfqs_stats (@output);
246 sub grade_mlfqs_off {
248 verify_common (@output);
249 our (@mlfqs_off_stats) = mlfqs_stats (@output);
252 sub grade_mlfqs_speedup {
253 our (@mlfqs_off_stats);
254 our (@mlfqs_on_stats);
257 my ($off_ticks) = $mlfqs_off_stats[1];
258 my ($on_ticks) = $mlfqs_on_stats[1];
259 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
260 if $on_ticks >= $off_ticks;
264 $result{'mlfqs-speedup'} = $@;
267 sub grade_mlfqs_priority {
268 our (@mlfqs_off_stats);
269 our (@mlfqs_on_stats);
271 check_mlfqs () if !defined (@mlfqs_on_stats);
272 for my $cat qw (CPU IO MIX) {
273 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
274 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
275 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
276 die "Minimum priority never changed from PRI_DEFAULT (29) "
278 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
279 die "Maximum priority never changed from PRI_DEFAULT (29) "
281 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
286 $result{'mlfqs-priority'} = $@;
290 our (@mlfqs_off_stats);
291 our (@mlfqs_on_stats);
292 die "p1-4 didn't finish with MLFQS on or off\n"
293 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
294 die "p1-4 didn't finish with MLFQS on\n"
295 if !defined (@mlfqs_on_stats);
296 die "p1-4 didn't finish with MLFQS off\n"
297 if !defined (@mlfqs_off_stats);
302 my (%stats) = (CPU => {}, IO => {}, MIX => {});
303 my (%map) = ("CPU intensive" => 'CPU',
304 "IO intensive" => 'IO',
305 "Alternating IO/CPU" => 'MIX');
306 my (%rmap) = reverse %map;
310 $ticks = $1 if /Timer: (\d+) ticks/;
311 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
312 my ($t) = $map{$thread} or next;
314 my ($s) = $stats{$t};
317 $$s{SUM2} += $pri * $pri;
318 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
319 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
322 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
323 for my $cat (values (%map)) {
324 my ($s) = $stats{$cat};
325 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
326 if $$s{N} != $expect_n{$cat};
327 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
329 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
331 $$s{MEAN} = $$s{SUM} / $$s{N};
334 return (\%stats, $ticks);