Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / threads / mlfqs-load-avg.c
1 #include <stdio.h>
2 #include "tests/threads/tests.h"
3 #include "threads/init.h"
4 #include "threads/malloc.h"
5 #include "threads/synch.h"
6 #include "threads/thread.h"
7 #include "devices/timer.h"
8
9 static int64_t start_time;
10
11 static void load_thread (void *seq_no);
12
13 #define THREAD_CNT 60
14
15 void
16 test_mlfqs_load_avg (void) 
17 {
18   int i;
19   
20   ASSERT (enable_mlfqs);
21
22   start_time = timer_ticks ();
23   msg ("Starting %d load threads...", THREAD_CNT);
24   for (i = 0; i < THREAD_CNT; i++) 
25     {
26       char name[16];
27       snprintf(name, sizeof name, "load %d", i);
28       thread_create (name, PRI_DEFAULT, load_thread, (void *) i);
29     }
30   msg ("Starting threads took %d seconds.",
31        timer_elapsed (start_time) / TIMER_FREQ);
32   thread_set_nice (-20);
33
34   for (i = 0; i < 90; i++) 
35     {
36       int64_t sleep_until = start_time + TIMER_FREQ * (2 * i + 10);
37       int load_avg;
38       timer_sleep (sleep_until - timer_ticks ());
39       load_avg = thread_get_load_avg ();
40       msg ("After %d seconds, load average=%d.%02d.",
41            i * 2, load_avg / 100, load_avg % 100);
42     }
43 }
44
45 static void
46 load_thread (void *seq_no_) 
47 {
48   int seq_no = (int) seq_no_;
49   int sleep_time = TIMER_FREQ * (10 + seq_no);
50   int spin_time = sleep_time + TIMER_FREQ * THREAD_CNT;
51   int exit_time = TIMER_FREQ * (THREAD_CNT * 2);
52
53   timer_sleep (sleep_time - timer_elapsed (start_time));
54   while (timer_elapsed (start_time) < spin_time)
55     continue;
56   timer_sleep (exit_time - timer_elapsed (start_time));
57 }