1 /* Measures the correctness of the "nice" implementation.
3 The "fair" tests run either 2 or 20 threads all niced to 0.
4 The threads should all receive approximately the same number
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.
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.
15 (The above are computed via simulation in mlfqs.pm.) */
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"
27 static void test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step);
30 test_mlfqs_fair_2 (void)
32 test_mlfqs_fair (2, 0, 0);
36 test_mlfqs_fair_20 (void)
38 test_mlfqs_fair (20, 0, 0);
42 test_mlfqs_nice_2 (void)
44 test_mlfqs_fair (2, 0, 5);
48 test_mlfqs_nice_10 (void)
50 test_mlfqs_fair (10, 0, 1);
53 #define MAX_THREAD_CNT 20
62 static void load_thread (void *aux);
65 test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step)
67 struct thread_info info[MAX_THREAD_CNT];
72 ASSERT (enable_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);
78 thread_set_nice (-20);
80 start_time = timer_ticks ();
81 msg ("Starting %d threads...", thread_cnt);
83 for (i = 0; i < thread_cnt; i++)
85 struct thread_info *ti = &info[i];
88 ti->start_time = start_time;
92 snprintf(name, sizeof name, "load %d", i);
93 thread_create (name, PRI_DEFAULT, load_thread, ti);
97 msg ("Starting threads took %"PRId64" ticks.", timer_elapsed (start_time));
99 msg ("Sleeping 40 seconds to let threads run, please wait...");
100 timer_sleep (40 * TIMER_FREQ);
102 for (i = 0; i < thread_cnt; i++)
103 msg ("Thread %d received %d ticks.", i, info[i].tick_count);
107 load_thread (void *ti_)
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;
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)
118 int64_t cur_time = timer_ticks ();
119 if (cur_time != last_time)
121 last_time = cur_time;