00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <inttypes.h>
00020 #include "tests/threads/tests.h"
00021 #include "threads/init.h"
00022 #include "threads/malloc.h"
00023 #include "threads/palloc.h"
00024 #include "threads/synch.h"
00025 #include "threads/thread.h"
00026 #include "devices/timer.h"
00027
00028 static void test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step);
00029
00030 void
00031 test_mlfqs_fair_2 (void)
00032 {
00033 test_mlfqs_fair (2, 0, 0);
00034 }
00035
00036 void
00037 test_mlfqs_fair_20 (void)
00038 {
00039 test_mlfqs_fair (20, 0, 0);
00040 }
00041
00042 void
00043 test_mlfqs_nice_2 (void)
00044 {
00045 test_mlfqs_fair (2, 0, 5);
00046 }
00047
00048 void
00049 test_mlfqs_nice_10 (void)
00050 {
00051 test_mlfqs_fair (10, 0, 1);
00052 }
00053
00054 #define MAX_THREAD_CNT 20
00055
00056 struct thread_info
00057 {
00058 int64_t start_time;
00059 int tick_count;
00060 int nice;
00061 };
00062
00063 static void load_thread (void *aux);
00064
00065 static void
00066 test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step)
00067 {
00068 struct thread_info info[MAX_THREAD_CNT];
00069 int64_t start_time;
00070 int nice;
00071 int i;
00072
00073 ASSERT (thread_mlfqs);
00074 ASSERT (thread_cnt <= MAX_THREAD_CNT);
00075 ASSERT (nice_min >= -10);
00076 ASSERT (nice_step >= 0);
00077 ASSERT (nice_min + nice_step * (thread_cnt - 1) <= 20);
00078
00079 thread_set_nice (-20);
00080
00081 start_time = timer_ticks ();
00082 msg ("Starting %d threads...", thread_cnt);
00083 nice = nice_min;
00084 for (i = 0; i < thread_cnt; i++)
00085 {
00086 struct thread_info *ti = &info[i];
00087 char name[16];
00088
00089 ti->start_time = start_time;
00090 ti->tick_count = 0;
00091 ti->nice = nice;
00092
00093 snprintf(name, sizeof name, "load %d", i);
00094 thread_create (name, PRI_DEFAULT, load_thread, ti);
00095
00096 nice += nice_step;
00097 }
00098 msg ("Starting threads took %"PRId64" ticks.", timer_elapsed (start_time));
00099
00100 msg ("Sleeping 40 seconds to let threads run, please wait...");
00101 timer_sleep (40 * TIMER_FREQ);
00102
00103 for (i = 0; i < thread_cnt; i++)
00104 msg ("Thread %d received %d ticks.", i, info[i].tick_count);
00105 }
00106
00107 static void
00108 load_thread (void *ti_)
00109 {
00110 struct thread_info *ti = ti_;
00111 int64_t sleep_time = 5 * TIMER_FREQ;
00112 int64_t spin_time = sleep_time + 30 * TIMER_FREQ;
00113 int64_t last_time = 0;
00114
00115 thread_set_nice (ti->nice);
00116 timer_sleep (sleep_time - timer_elapsed (ti->start_time));
00117 while (timer_elapsed (ti->start_time) < spin_time)
00118 {
00119 int64_t cur_time = timer_ticks ();
00120 if (cur_time != last_time)
00121 ti->tick_count++;
00122 last_time = cur_time;
00123 }
00124 }