f958f816460df7999873523e955ccd6a2d928354
[pintos-anon] / src / tests / threads / mlfqs-fair.c
1 /* Measures the correctness of the "nice" implementation.
2
3    The "fair" tests run either 2 or 20 threads all niced to 0.
4    The threads should all receive approximately the same number
5    of ticks.
6
7    The mlfqs-nice-2 test runs 2 threads, one with nice 0, the
8    other with nice 5, which should receive 1,904 and 1,096 ticks,
9    respectively, over 30 seconds.
10
11    The mlfqs-nice-10 test runs 10 threads with nice 0 through 9.
12    They should receive 672, 588, 492, 408, 316, 232, 152, 92, 40,
13    and 8 ticks, respectively, over 30 seconds.
14
15    (The above are computed via simulation in mlfqs.pm.) */
16
17 #include <stdio.h>
18 #include <inttypes.h>
19 #include "tests/threads/tests.h"
20 #include "threads/init.h"
21 #include "threads/malloc.h"
22 #include "threads/palloc.h"
23 #include "threads/synch.h"
24 #include "threads/thread.h"
25 #include "devices/timer.h"
26
27 static void test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step);
28
29 void
30 test_mlfqs_fair_2 (void) 
31 {
32   test_mlfqs_fair (2, 0, 0);
33 }
34
35 void
36 test_mlfqs_fair_20 (void) 
37 {
38   test_mlfqs_fair (20, 0, 0);
39 }
40
41 void
42 test_mlfqs_nice_2 (void) 
43 {
44   test_mlfqs_fair (2, 0, 5);
45 }
46
47 void
48 test_mlfqs_nice_10 (void) 
49 {
50   test_mlfqs_fair (10, 0, 1);
51 }
52 \f
53 #define MAX_THREAD_CNT 20
54
55 struct thread_info 
56   {
57     int64_t start_time;
58     int tick_count;
59     int nice;
60   };
61
62 static void load_thread (void *aux);
63
64 static void
65 test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step)
66 {
67   struct thread_info info[MAX_THREAD_CNT];
68   int64_t start_time;
69   int nice;
70   int i;
71
72   ASSERT (thread_mlfqs);
73   ASSERT (thread_cnt <= MAX_THREAD_CNT);
74   ASSERT (nice_min >= -10);
75   ASSERT (nice_step >= 0);
76   ASSERT (nice_min + nice_step * (thread_cnt - 1) <= 20);
77
78   thread_set_nice (-20);
79
80   start_time = timer_ticks ();
81   msg ("Starting %d threads...", thread_cnt);
82   nice = nice_min;
83   for (i = 0; i < thread_cnt; i++) 
84     {
85       struct thread_info *ti = &info[i];
86       char name[16];
87
88       ti->start_time = start_time;
89       ti->tick_count = 0;
90       ti->nice = nice;
91
92       snprintf(name, sizeof name, "load %d", i);
93       thread_create (name, PRI_DEFAULT, load_thread, ti);
94
95       nice += nice_step;
96     }
97   msg ("Starting threads took %"PRId64" ticks.", timer_elapsed (start_time));
98
99   msg ("Sleeping 40 seconds to let threads run, please wait...");
100   timer_sleep (40 * TIMER_FREQ);
101   
102   for (i = 0; i < thread_cnt; i++)
103     msg ("Thread %d received %d ticks.", i, info[i].tick_count);
104 }
105
106 static void
107 load_thread (void *ti_) 
108 {
109   struct thread_info *ti = ti_;
110   int64_t sleep_time = 5 * TIMER_FREQ;
111   int64_t spin_time = sleep_time + 30 * TIMER_FREQ;
112   int64_t last_time = 0;
113
114   thread_set_nice (ti->nice);
115   timer_sleep (sleep_time - timer_elapsed (ti->start_time));
116   while (timer_elapsed (ti->start_time) < spin_time) 
117     {
118       int64_t cur_time = timer_ticks ();
119       if (cur_time != last_time)
120         ti->tick_count++;
121       last_time = cur_time;
122     }
123 }