3 #include "tests/threads/tests.h"
4 #include "threads/init.h"
5 #include "threads/malloc.h"
6 #include "threads/palloc.h"
7 #include "threads/synch.h"
8 #include "threads/thread.h"
9 #include "devices/timer.h"
11 static void test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step);
14 test_mlfqs_fair_2 (void)
16 test_mlfqs_fair (2, 0, 0);
20 test_mlfqs_fair_20 (void)
22 test_mlfqs_fair (20, 0, 0);
26 test_mlfqs_nice_2 (void)
28 test_mlfqs_fair (2, 0, 5);
32 test_mlfqs_nice_10 (void)
34 test_mlfqs_fair (10, 0, 1);
37 #define MAX_THREAD_CNT 20
46 static void load_thread (void *aux);
49 test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step)
51 struct thread_info info[MAX_THREAD_CNT];
56 ASSERT (enable_mlfqs);
57 ASSERT (thread_cnt <= MAX_THREAD_CNT);
58 ASSERT (nice_min >= -10);
59 ASSERT (nice_step >= 0);
60 ASSERT (nice_min + nice_step * (thread_cnt - 1) <= 20);
62 thread_set_nice (-20);
64 start_time = timer_ticks ();
65 msg ("Starting %d threads...", thread_cnt);
67 for (i = 0; i < thread_cnt; i++)
69 struct thread_info *ti = &info[i];
72 ti->start_time = start_time;
76 snprintf(name, sizeof name, "load %d", i);
77 thread_create (name, PRI_DEFAULT, load_thread, ti);
81 msg ("Starting threads took %"PRId64" ticks.", timer_elapsed (start_time));
83 msg ("Sleeping 40 seconds to let threads run, please wait...");
84 timer_sleep (40 * TIMER_FREQ);
86 for (i = 0; i < thread_cnt; i++)
87 msg ("Thread %d received %d ticks.", i, info[i].tick_count);
91 load_thread (void *ti_)
93 struct thread_info *ti = ti_;
94 int64_t sleep_time = 5 * TIMER_FREQ;
95 int64_t spin_time = sleep_time + 30 * TIMER_FREQ;
96 int64_t last_time = 0;
98 thread_set_nice (ti->nice);
99 timer_sleep (sleep_time - timer_elapsed (ti->start_time));
100 while (timer_elapsed (ti->start_time) < spin_time)
102 int64_t cur_time = timer_ticks ();
103 if (cur_time != last_time)
105 last_time = cur_time;