Fix order of checks on load average.
[pintos-anon] / src / tests / threads / mlfqs-load-1.c
1 /* Verifies that a single busy thread raises the load average to
2    0.5 in 38 to 45 seconds.  The expected time is 42 seconds, as
3    you can verify:
4    perl -e '$i++,$a=(59*$a+1)/60while$a<=.5;print "$i\n"'
5
6    Then, verifies that 10 seconds of inactivity drop the load
7    average back below 0.5 again. */
8
9 #include <stdio.h>
10 #include "tests/threads/tests.h"
11 #include "threads/init.h"
12 #include "threads/malloc.h"
13 #include "threads/synch.h"
14 #include "threads/thread.h"
15 #include "devices/timer.h"
16
17 void
18 test_mlfqs_load_1 (void) 
19 {
20   int64_t start_time;
21   int elapsed;
22   int load_avg;
23   
24   ASSERT (thread_mlfqs);
25
26   msg ("spinning for up to 45 seconds, please wait...");
27
28   start_time = timer_ticks ();
29   for (;;) 
30     {
31       load_avg = thread_get_load_avg ();
32       ASSERT (load_avg >= 0);
33       elapsed = timer_elapsed (start_time) / TIMER_FREQ;
34       if (load_avg > 100)
35         fail ("load average is %d.%02d "
36               "but should be between 0 and 1 (after %d seconds)",
37               load_avg / 100, load_avg % 100, elapsed);
38       else if (load_avg > 50)
39         break;
40       else if (elapsed > 45)
41         fail ("load average stayed below 0.5 for more than 45 seconds");
42     }
43
44   if (elapsed < 38)
45     fail ("load average took only %d seconds to rise above 0.5", elapsed);
46   msg ("load average rose to 0.5 after %d seconds", elapsed);
47
48   msg ("sleeping for another 10 seconds, please wait...");
49   timer_sleep (TIMER_FREQ * 10);
50
51   load_avg = thread_get_load_avg ();
52   if (load_avg < 0)
53     fail ("load average fell below 0");
54   if (load_avg > 50)
55     fail ("load average stayed above 0.5 for more than 10 seconds");
56   msg ("load average fell back below 0.5 (to %d.%02d)",
57        load_avg / 100, load_avg % 100);
58
59   pass ();
60 }