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_c) = snarf ("pintos/src/devices/timer.c");
68 if (!grep (/^\#define TIME_SLICE $new_time_slice$/, @timer_c)) {
69 @timer_c = grep (!/^\#define TIME_SLICE/, @timer_c);
70 unshift (@timer_c, "#define TIME_SLICE $new_time_slice");
71 open (TIMER_C, ">pintos/src/devices/timer.c");
72 print TIMER_C map ("$_\n", @timer_c);
76 # Changes devices/timer.h if necessary.
77 my ($new_timer_freq) = $test eq 'priority-fifo' ? 19 : 100;
78 my (@timer_h) = snarf ("pintos/src/devices/timer.h");
79 if (!grep (/^\#define TIMER_FREQ $new_time_slice$/, @timer_h)) {
80 @timer_h = grep (!/^\#define TIMER_FREQ/, @timer_h);
81 unshift (@timer_h, "#define TIMER_FREQ $new_timer_freq");
82 open (TIMER_H, ">pintos/src/devices/timer.h");
83 print TIMER_H map ("$_\n", @timer_h);
87 # Copy in the new test.c and delete enough files to ensure a full rebuild.
88 my ($src) = "$GRADES_DIR/" . ($test !~ /^mlfqs/ ? "$test.c" : "mlfqs.c");
89 -e $src or die "$src: stat: $!\n";
90 xsystem ("cp $src pintos/src/threads/test.c", DIE => "cp failed\n");
91 unlink ("pintos/src/threads/build/threads/test.o");
92 unlink ("pintos/src/threads/build/kernel.o");
93 unlink ("pintos/src/threads/build/kernel.bin");
94 unlink ("pintos/src/threads/build/os.dsk");
97 if (xsystem ("cd pintos/src/threads && make",
98 LOG => "$test/make") ne 'ok') {
99 $details{$test} = snarf ("output/$test/make.err");
100 return "Compile error";
103 # Copy out files for backtraces later.
104 xsystem ("cp pintos/src/threads/build/kernel.o output/$test");
105 xsystem ("cp pintos/src/threads/build/os.dsk output/$test");
108 my ($timeout) = $test !~ /^mlfqs/ ? 15 : 600;
109 return run_pintos (["-v", "run", "-q"],
110 CHDIR => "pintos/src/threads/build",
112 TIMEOUT => $timeout);
115 sub grade_alarm_single {
116 verify_alarm (1, @_);
119 sub grade_alarm_multiple {
120 verify_alarm (7, @_);
124 my ($iterations, @output) = @_;
126 verify_common (@output);
129 for (my ($i) = 0; $i < $iterations; $i++) {
130 for (my ($t) = 0; $t < 5; $t++) {
131 push (@products, ($i + 1) * ($t + 1) * 10);
134 @products = sort {$a <=> $b} @products;
138 die $_ if /out of order/i;
140 my ($p) = /product=(\d+)$/;
143 my ($q) = shift (@products);
144 die "Too many wakeups.\n" if !defined $q;
145 die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME
147 die scalar (@products) . " fewer wakeups than expected.\n"
151 sub grade_alarm_zero {
153 verify_common (@output);
154 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
157 sub grade_alarm_negative {
159 verify_common (@output);
160 die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
163 sub grade_join_invalid {
165 verify_common (@output);
166 grep (/Testing invalid join/, @output) or die "Test didn't start\n";
167 grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
172 verify_common (@output);
173 grep (/Testing no join/, @output) or die "Test didn't start\n";
174 grep (/No join test done/, @output) or die "Test didn't complete\n";
177 sub grade_join_multiple {
180 verify_common (@output);
182 $t[4] = $t[5] = $t[6] = -1;
185 my ($idx) = /^Thread (\d+)/ or next;
186 my ($iter) = /iteration (\d+)$/;
187 $iter = 5 if /done!$/;
188 die "Malformed output\n" if !defined $iter;
190 die "Thread 6 started before either other thread finished\n"
191 if $t[4] < 5 && $t[5] < 5;
192 die "Thread 6 started before thread 4 finished\n"
194 die "Thread 6 started before thread 5 finished\n"
197 die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
202 for my $idx (4, 5, 6) {
203 if ($t[$idx] == -1) {
204 $err .= "Thread $idx did not run at all\n";
205 } elsif ($t[$idx] != 5) {
206 $err .= "Thread $idx only completed $t[$idx] iterations\n";
209 die $err if $err ne '';
212 sub grade_priority_fifo {
215 verify_common (@output);
216 my ($thread_cnt) = 10;
219 my (@t) = (-1) x $thread_cnt;
222 my ($idx) = /^Thread (\d+)/ or next;
223 my ($iter) = /iteration (\d+)$/;
224 $iter = $iter_cnt if /done!$/;
225 die "Malformed output\n" if !defined $iter;
226 if (@order < $thread_cnt) {
228 die "Thread $idx repeated within first $thread_cnt iterations: "
229 . join (' ', @order) . ".\n"
230 if grep ($_ == $idx, @order) != 1;
232 die "Thread $idx ran when $order[0] should have.\n"
233 if $idx != $order[0];
234 push (@order, shift @order);
236 die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
241 for my $idx (0..$#t) {
242 if ($t[$idx] == -1) {
243 $err .= "Thread $idx did not run at all.\n";
244 } elsif ($t[$idx] != $iter_cnt) {
245 $err .= "Thread $idx only completed $t[$idx] iterations.\n";
248 die $err if $err ne '';
253 verify_common (@output);
254 our (@mlfqs_on_stats) = mlfqs_stats (@output);
257 sub grade_mlfqs_off {
259 verify_common (@output);
260 our (@mlfqs_off_stats) = mlfqs_stats (@output);
263 sub grade_mlfqs_speedup {
264 our (@mlfqs_off_stats);
265 our (@mlfqs_on_stats);
268 my ($off_ticks) = $mlfqs_off_stats[1];
269 my ($on_ticks) = $mlfqs_on_stats[1];
270 die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
271 if $on_ticks >= $off_ticks;
275 $result{'mlfqs-speedup'} = $@;
278 sub grade_mlfqs_priority {
279 our (@mlfqs_off_stats);
280 our (@mlfqs_on_stats);
282 check_mlfqs () if !defined (@mlfqs_on_stats);
283 for my $cat qw (CPU IO MIX) {
284 die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
285 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
286 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
287 die "Minimum priority never changed from PRI_DEFAULT (29) "
289 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
290 die "Maximum priority never changed from PRI_DEFAULT (29) "
292 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
297 $result{'mlfqs-priority'} = $@;
301 our (@mlfqs_off_stats);
302 our (@mlfqs_on_stats);
303 die "p1-4 didn't finish with MLFQS on or off\n"
304 if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
305 die "p1-4 didn't finish with MLFQS on\n"
306 if !defined (@mlfqs_on_stats);
307 die "p1-4 didn't finish with MLFQS off\n"
308 if !defined (@mlfqs_off_stats);
313 my (%stats) = (CPU => {}, IO => {}, MIX => {});
314 my (%map) = ("CPU intensive" => 'CPU',
315 "IO intensive" => 'IO',
316 "Alternating IO/CPU" => 'MIX');
317 my (%rmap) = reverse %map;
321 $ticks = $1 if /Timer: (\d+) ticks/;
322 my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
323 my ($t) = $map{$thread} or next;
325 my ($s) = $stats{$t};
328 $$s{SUM2} += $pri * $pri;
329 $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
330 $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
333 my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
334 for my $cat (values (%map)) {
335 my ($s) = $stats{$cat};
336 die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
337 if $$s{N} != $expect_n{$cat};
338 die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
340 die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
342 $$s{MEAN} = $$s{SUM} / $$s{N};
345 return (\%stats, $ticks);