1e718a93b6b151179b33556aa8ab8144165e154e
[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 success () if $action eq 'test';
48
49 assemble_final_grade ();
50 exit success () 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_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);
73         close (TIMER_C);
74     }
75
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);
84         close (TIMER_H);
85     }
86
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");
95
96     # Build.
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";
101     }
102
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");
106
107     # Run.
108     my ($timeout) = $test !~ /^mlfqs/ ? 15 : 600;
109     return run_pintos (["-v", "run", "-q"],
110                        CHDIR => "pintos/src/threads/build",
111                        LOG => "$test/run",
112                        TIMEOUT => $timeout);
113 }
114 \f
115 sub grade_alarm_single {
116     verify_alarm (1, @_);
117 }
118
119 sub grade_alarm_multiple {
120     verify_alarm (7, @_);
121 }
122
123 sub verify_alarm {
124     my ($iterations, @output) = @_;
125
126     verify_common (@output);
127
128     my (@products);
129     for (my ($i) = 0; $i < $iterations; $i++) {
130         for (my ($t) = 0; $t < 5; $t++) {
131             push (@products, ($i + 1) * ($t + 1) * 10);
132         }
133     }
134     @products = sort {$a <=> $b} @products;
135
136     local ($_);
137     foreach (@output) {
138         die $_ if /out of order/i;
139
140         my ($p) = /product=(\d+)$/;
141         next if !defined $p;
142
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
146     }
147     die scalar (@products) . " fewer wakeups than expected.\n"
148         if @products != 0;
149 }
150
151 sub grade_alarm_zero {
152     my (@output) = @_;
153     verify_common (@output);
154     die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
155 }
156
157 sub grade_alarm_negative {
158     my (@output) = @_;
159     verify_common (@output);
160     die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
161 }
162
163 sub grade_join_invalid {
164     my (@output) = @_;
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";
168 }
169
170 sub grade_join_no {
171     my (@output) = @_;
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";
175 }
176
177 sub grade_join_multiple {
178     my (@output) = @_;
179
180     verify_common (@output);
181     my (@t);
182     $t[4] = $t[5] = $t[6] = -1;
183     local ($_);
184     foreach (@output) {
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;
189         if ($idx == 6) {
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"
193                 if $t[4] < 5;
194             die "Thread 6 started before thread 5 finished\n"
195                 if $t[5] < 5;
196         }
197         die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
198         $t[$idx] = $iter;
199     }
200
201     my ($err) = "";
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";
207         }
208     }
209     die $err if $err ne '';
210 }
211
212 sub grade_priority_fifo {
213     my (@output) = @_;
214
215     verify_common (@output);
216     my ($thread_cnt) = 10;
217     my ($iter_cnt) = 5;
218     my (@order);
219     my (@t) = (-1) x $thread_cnt;
220     local ($_);
221     foreach (@output) {
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) {
227             push (@order, $idx);
228             die "Thread $idx repeated within first $thread_cnt iterations: "
229                 . join (' ', @order) . ".\n"
230                 if grep ($_ == $idx, @order) != 1;
231         } else {
232             die "Thread $idx ran when $order[0] should have.\n"
233                 if $idx != $order[0];
234             push (@order, shift @order);
235         }
236         die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
237         $t[$idx] = $iter;
238     }
239
240     my ($err) = "";
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";
246         }
247     }
248     die $err if $err ne '';
249 }
250
251 sub grade_mlfqs_on {
252     my (@output) = @_;
253     verify_common (@output);
254     our (@mlfqs_on_stats) = mlfqs_stats (@output);
255 }
256
257 sub grade_mlfqs_off {
258     my (@output) = @_;
259     verify_common (@output);
260     our (@mlfqs_off_stats) = mlfqs_stats (@output);
261 }
262
263 sub grade_mlfqs_speedup {
264     our (@mlfqs_off_stats);
265     our (@mlfqs_on_stats);
266     eval {
267         check_mlfqs ();
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;
272         die "ok\n";
273     };
274     chomp $@;
275     $result{'mlfqs-speedup'} = $@;
276 }
277
278 sub grade_mlfqs_priority {
279     our (@mlfqs_off_stats);
280     our (@mlfqs_on_stats);
281     eval {
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) "
288                 . "with MLFQS\n"
289                 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
290             die "Maximum priority never changed from PRI_DEFAULT (29) "
291                 . "with MLFQS\n"
292                 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
293         }
294         die "ok\n";
295     };
296     chomp $@;
297     $result{'mlfqs-priority'} = $@;
298 }
299
300 sub check_mlfqs {
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);
309 }
310
311 sub mlfqs_stats {
312     my (@output) = @_;
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;
318     my ($ticks);
319     local ($_);
320     foreach (@output) {
321         $ticks = $1 if /Timer: (\d+) ticks/;
322         my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
323         my ($t) = $map{$thread} or next;
324         
325         my ($s) = $stats{$t};
326         $$s{N}++;
327         $$s{SUM} += $pri;
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};
331     }
332
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"
339             if $$s{MIN} < 0;
340         die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
341             if $$s{MAX} > 59;
342         $$s{MEAN} = $$s{SUM} / $$s{N};
343     }
344
345     return (\%stats, $ticks);
346 }