Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / threads / mlfqs-fair.c
1 #include <stdio.h>
2 #include <inttypes.h>
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"
10
11 static void test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step);
12
13 void
14 test_mlfqs_fair_2 (void) 
15 {
16   test_mlfqs_fair (2, 0, 0);
17 }
18
19 void
20 test_mlfqs_fair_20 (void) 
21 {
22   test_mlfqs_fair (20, 0, 0);
23 }
24
25 void
26 test_mlfqs_nice_2 (void) 
27 {
28   test_mlfqs_fair (2, 0, 5);
29 }
30
31 void
32 test_mlfqs_nice_10 (void) 
33 {
34   test_mlfqs_fair (10, 0, 1);
35 }
36 \f
37 #define MAX_THREAD_CNT 20
38
39 struct thread_info 
40   {
41     int64_t start_time;
42     int tick_count;
43     int nice;
44   };
45
46 static void load_thread (void *aux);
47
48 static void
49 test_mlfqs_fair (int thread_cnt, int nice_min, int nice_step)
50 {
51   struct thread_info info[MAX_THREAD_CNT];
52   int64_t start_time;
53   int nice;
54   int i;
55
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);
61
62   thread_set_nice (-20);
63
64   start_time = timer_ticks ();
65   msg ("Starting %d threads...", thread_cnt);
66   nice = nice_min;
67   for (i = 0; i < thread_cnt; i++) 
68     {
69       struct thread_info *ti = &info[i];
70       char name[16];
71
72       ti->start_time = start_time;
73       ti->tick_count = 0;
74       ti->nice = nice;
75
76       snprintf(name, sizeof name, "load %d", i);
77       thread_create (name, PRI_DEFAULT, load_thread, ti);
78
79       nice += nice_step;
80     }
81   msg ("Starting threads took %"PRId64" ticks.", timer_elapsed (start_time));
82
83   msg ("Sleeping 40 seconds to let threads run, please wait...");
84   timer_sleep (40 * TIMER_FREQ);
85   
86   for (i = 0; i < thread_cnt; i++)
87     msg ("Thread %d received %d ticks.", i, info[i].tick_count);
88 }
89
90 static void
91 load_thread (void *ti_) 
92 {
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;
97
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) 
101     {
102       int64_t cur_time = timer_ticks ();
103       if (cur_time != last_time)
104         ti->tick_count++;
105       last_time = cur_time;
106     }
107 }