4199d94214f09b9b61348ff487f620937ea39dcd
[pintos-anon] / grading / threads / run-tests
1 #! /usr/bin/perl
2
3 # Find the directory that contains the grading files.
4 our ($GRADES_DIR);
5
6 # Add our Perl library directory to the include path. 
7 BEGIN {
8     ($GRADES_DIR = $0) =~ s#/[^/]+$##;
9     -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
10     unshift @INC, "$GRADES_DIR/../lib";
11 }
12
13 use warnings;
14 use strict;
15 use Pintos::Grading;
16
17 our ($hw) = "threads";
18 our (@TESTS);           # Tests to run.
19 our ($test);
20 our (%details);
21 our (%result);
22 our ($action);
23
24 parse_cmd_line qw (alarm-single alarm-multiple alarm-zero alarm-negative
25                    join-simple
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
30                    mlfqs-on mlfqs-off);
31
32 clean_dir (), exit if $action eq 'clean';
33
34 extract_sources (); 
35 exit if $action eq 'extract';
36
37 build (); 
38 exit if $action eq 'build';
39
40 run_and_grade_tests ();
41 if (defined ($result{'mlfqs-on'}) && defined ($result{'mlfqs-off'})) {
42     grade_mlfqs_speedup ();
43     grade_mlfqs_priority ();
44 }
45 write_grades (); 
46 write_details ();
47 exit if $action eq 'test';
48
49 assemble_final_grade ();
50 exit if $action eq 'assemble';
51
52 die "Don't know how to '$action'";
53
54 # Runs $test in directory output/$test.
55 # Returns 'ok' if it went ok, otherwise an explanation.
56 sub run_test {
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;
62         close (CONSTANTS);
63     }
64
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);
73         close (TIMER);
74     }
75
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");
84
85     # Build.
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";
90     }
91
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");
95
96     # Run.
97     my ($timeout) = $test !~ /^mlfqs/ ? 10 : 600;
98     return run_pintos ("cd pintos/src/threads/build && pintos -v run -q",
99                        LOG => "$test/run",
100                        TIMEOUT => $timeout);
101 }
102 \f
103 sub grade_alarm_single {
104     verify_alarm (1, @_);
105 }
106
107 sub grade_alarm_multiple {
108     verify_alarm (7, @_);
109 }
110
111 sub verify_alarm {
112     my ($iterations, @output) = @_;
113
114     verify_common (@output);
115
116     my (@products);
117     for (my ($i) = 0; $i < $iterations; $i++) {
118         for (my ($t) = 0; $t < 5; $t++) {
119             push (@products, ($i + 1) * ($t + 1) * 10);
120         }
121     }
122     @products = sort {$a <=> $b} @products;
123
124     local ($_);
125     foreach (@output) {
126         die $_ if /out of order/i;
127
128         my ($p) = /product=(\d+)$/;
129         next if !defined $p;
130
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
134     }
135     die scalar (@products) . " fewer wakeups than expected.\n"
136         if @products != 0;
137 }
138
139 sub grade_alarm_zero {
140     my (@output) = @_;
141     verify_common (@output);
142     die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
143 }
144
145 sub grade_alarm_negative {
146     my (@output) = @_;
147     verify_common (@output);
148     die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
149 }
150
151 sub grade_join_invalid {
152     my (@output) = @_;
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";
156 }
157
158 sub grade_join_no {
159     my (@output) = @_;
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";
163 }
164
165 sub grade_join_multiple {
166     my (@output) = @_;
167
168     verify_common (@output);
169     my (@t);
170     $t[4] = $t[5] = $t[6] = -1;
171     local ($_);
172     foreach (@output) {
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;
177         if ($idx == 6) {
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"
181                 if $t[4] < 5;
182             die "Thread 6 started before thread 5 finished\n"
183                 if $t[5] < 5;
184         }
185         die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
186         $t[$idx] = $iter;
187     }
188
189     my ($err) = "";
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";
195         }
196     }
197     die $err if $err ne '';
198 }
199
200 sub grade_priority_fifo {
201     my (@output) = @_;
202
203     verify_common (@output);
204     my ($thread_cnt) = 10;
205     my ($iter_cnt) = 5;
206     my (@order);
207     my (@t) = (-1) x $thread_cnt;
208     local ($_);
209     foreach (@output) {
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) {
215             push (@order, $idx);
216             die "Thread $idx repeated within first $thread_cnt iterations: "
217                 . join (' ', @order) . ".\n"
218                 if grep ($_ == $idx, @order) != 1;
219         } else {
220             die "Thread $idx ran when $order[0] should have.\n"
221                 if $idx != $order[0];
222             push (@order, shift @order);
223         }
224         die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
225         $t[$idx] = $iter;
226     }
227
228     my ($err) = "";
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";
234         }
235     }
236     die $err if $err ne '';
237 }
238
239 sub grade_mlfqs_on {
240     my (@output) = @_;
241     verify_common (@output);
242     our (@mlfqs_on_stats) = mlfqs_stats (@output);
243 }
244
245 sub grade_mlfqs_off {
246     my (@output) = @_;
247     verify_common (@output);
248     our (@mlfqs_off_stats) = mlfqs_stats (@output);
249 }
250
251 sub grade_mlfqs_speedup {
252     our (@mlfqs_off_stats);
253     our (@mlfqs_on_stats);
254     eval {
255         check_mlfqs ();
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;
260         die "ok\n";
261     };
262     chomp $@;
263     $result{'mlfqs-speedup'} = $@;
264 }
265
266 sub grade_mlfqs_priority {
267     our (@mlfqs_off_stats);
268     our (@mlfqs_on_stats);
269     eval {
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) "
276                 . "with MLFQS\n"
277                 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
278             die "Maximum priority never changed from PRI_DEFAULT (29) "
279                 . "with MLFQS\n"
280                 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
281         }
282         die "ok\n";
283     };
284     chomp $@;
285     $result{'mlfqs-priority'} = $@;
286 }
287
288 sub check_mlfqs {
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);
297 }
298
299 sub mlfqs_stats {
300     my (@output) = @_;
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;
306     my ($ticks);
307     local ($_);
308     foreach (@output) {
309         $ticks = $1 if /Timer: (\d+) ticks/;
310         my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
311         my ($t) = $map{$thread} or next;
312         
313         my ($s) = $stats{$t};
314         $$s{N}++;
315         $$s{SUM} += $pri;
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};
319     }
320
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"
327             if $$s{MIN} < 0;
328         die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
329             if $$s{MAX} > 59;
330         $$s{MEAN} = $$s{SUM} / $$s{N};
331     }
332
333     return (\%stats, $ticks);
334 }