PKUOS - Pintos
Pintos source browser for PKU Operating System course
mlfqs-load-avg.c
Go to the documentation of this file.
1/** Starts 60 threads numbered 0 through 59. Thread #i sleeps for
2 (10+i) seconds, then spins in a loop for 60 seconds, then
3 sleeps until a total of 120 seconds have passed. Every 2
4 seconds, starting 10 seconds in, the main thread prints the
5 load average.
6
7 The expected output is listed below. Some margin of error is
8 allowed.
9
10 If your implementation fails this test but passes most other
11 tests, then consider whether you are doing too much work in
12 the timer interrupt. If the timer interrupt handler takes too
13 long, then the test's main thread will not have enough time to
14 do its own work (printing a message) and go back to sleep
15 before the next tick arrives. Then the main thread will be
16 ready, instead of sleeping, when the tick arrives,
17 artificially driving up the load average.
18
19 After 0 seconds, load average=0.00.
20 After 2 seconds, load average=0.05.
21 After 4 seconds, load average=0.16.
22 After 6 seconds, load average=0.34.
23 After 8 seconds, load average=0.58.
24 After 10 seconds, load average=0.87.
25 After 12 seconds, load average=1.22.
26 After 14 seconds, load average=1.63.
27 After 16 seconds, load average=2.09.
28 After 18 seconds, load average=2.60.
29 After 20 seconds, load average=3.16.
30 After 22 seconds, load average=3.76.
31 After 24 seconds, load average=4.42.
32 After 26 seconds, load average=5.11.
33 After 28 seconds, load average=5.85.
34 After 30 seconds, load average=6.63.
35 After 32 seconds, load average=7.46.
36 After 34 seconds, load average=8.32.
37 After 36 seconds, load average=9.22.
38 After 38 seconds, load average=10.15.
39 After 40 seconds, load average=11.12.
40 After 42 seconds, load average=12.13.
41 After 44 seconds, load average=13.16.
42 After 46 seconds, load average=14.23.
43 After 48 seconds, load average=15.33.
44 After 50 seconds, load average=16.46.
45 After 52 seconds, load average=17.62.
46 After 54 seconds, load average=18.81.
47 After 56 seconds, load average=20.02.
48 After 58 seconds, load average=21.26.
49 After 60 seconds, load average=22.52.
50 After 62 seconds, load average=23.71.
51 After 64 seconds, load average=24.80.
52 After 66 seconds, load average=25.78.
53 After 68 seconds, load average=26.66.
54 After 70 seconds, load average=27.45.
55 After 72 seconds, load average=28.14.
56 After 74 seconds, load average=28.75.
57 After 76 seconds, load average=29.27.
58 After 78 seconds, load average=29.71.
59 After 80 seconds, load average=30.06.
60 After 82 seconds, load average=30.34.
61 After 84 seconds, load average=30.55.
62 After 86 seconds, load average=30.68.
63 After 88 seconds, load average=30.74.
64 After 90 seconds, load average=30.73.
65 After 92 seconds, load average=30.66.
66 After 94 seconds, load average=30.52.
67 After 96 seconds, load average=30.32.
68 After 98 seconds, load average=30.06.
69 After 100 seconds, load average=29.74.
70 After 102 seconds, load average=29.37.
71 After 104 seconds, load average=28.95.
72 After 106 seconds, load average=28.47.
73 After 108 seconds, load average=27.94.
74 After 110 seconds, load average=27.36.
75 After 112 seconds, load average=26.74.
76 After 114 seconds, load average=26.07.
77 After 116 seconds, load average=25.36.
78 After 118 seconds, load average=24.60.
79 After 120 seconds, load average=23.81.
80 After 122 seconds, load average=23.02.
81 After 124 seconds, load average=22.26.
82 After 126 seconds, load average=21.52.
83 After 128 seconds, load average=20.81.
84 After 130 seconds, load average=20.12.
85 After 132 seconds, load average=19.46.
86 After 134 seconds, load average=18.81.
87 After 136 seconds, load average=18.19.
88 After 138 seconds, load average=17.59.
89 After 140 seconds, load average=17.01.
90 After 142 seconds, load average=16.45.
91 After 144 seconds, load average=15.90.
92 After 146 seconds, load average=15.38.
93 After 148 seconds, load average=14.87.
94 After 150 seconds, load average=14.38.
95 After 152 seconds, load average=13.90.
96 After 154 seconds, load average=13.44.
97 After 156 seconds, load average=13.00.
98 After 158 seconds, load average=12.57.
99 After 160 seconds, load average=12.15.
100 After 162 seconds, load average=11.75.
101 After 164 seconds, load average=11.36.
102 After 166 seconds, load average=10.99.
103 After 168 seconds, load average=10.62.
104 After 170 seconds, load average=10.27.
105 After 172 seconds, load average=9.93.
106 After 174 seconds, load average=9.61.
107 After 176 seconds, load average=9.29.
108 After 178 seconds, load average=8.98.
109*/
110
111#include <stdio.h>
112#include "tests/threads/tests.h"
113#include "threads/init.h"
114#include "threads/malloc.h"
115#include "threads/synch.h"
116#include "threads/thread.h"
117#include "devices/timer.h"
118
120
121static void load_thread (void *seq_no);
122
123#define THREAD_CNT 60
124
125void
127{
128 int i;
129
131
133 msg ("Starting %d load threads...", THREAD_CNT);
134 for (i = 0; i < THREAD_CNT; i++)
135 {
136 char name[16];
137 snprintf(name, sizeof name, "load %d", i);
139 }
140 msg ("Starting threads took %d seconds.",
142 thread_set_nice (-20);
143
144 for (i = 0; i < 90; i++)
145 {
146 int64_t sleep_until = start_time + TIMER_FREQ * (2 * i + 10);
147 int load_avg;
148 timer_sleep (sleep_until - timer_ticks ());
149 load_avg = thread_get_load_avg ();
150 msg ("After %d seconds, load average=%d.%02d.",
151 i * 2, load_avg / 100, load_avg % 100);
152 }
153}
154
155static void
156load_thread (void *seq_no_)
157{
158 int seq_no = (int) seq_no_;
159 int sleep_time = TIMER_FREQ * (10 + seq_no);
160 int spin_time = sleep_time + TIMER_FREQ * THREAD_CNT;
161 int exit_time = TIMER_FREQ * (THREAD_CNT * 2);
162
163 timer_sleep (sleep_time - timer_elapsed (start_time));
164 while (timer_elapsed (start_time) < spin_time)
165 continue;
166 timer_sleep (exit_time - timer_elapsed (start_time));
167}
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
char * name[]
Definition: insult.c:47
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
void msg(const char *format,...)
Definition: lib.c:28
static void load_thread(void *seq_no)
#define THREAD_CNT
void test_mlfqs_load_avg(void)
static int64_t start_time
Starts 60 threads numbered 0 through 59.
signed long long int int64_t
Definition: stdint.h:16
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
tid_t thread_create(const char *name, int priority, thread_func *function, void *aux)
Creates a new kernel thread named NAME with the given initial PRIORITY, which executes FUNCTION passi...
Definition: thread.c:166
void thread_set_nice(int nice UNUSED)
Sets the current thread's nice value to NICE.
Definition: thread.c:350
int thread_get_load_avg(void)
Returns 100 times the system load average.
Definition: thread.c:365
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
int64_t timer_ticks(void)
Returns the number of timer ticks since the OS booted.
Definition: timer.c:71
int64_t timer_elapsed(int64_t then)
Returns the number of timer ticks elapsed since THEN, which should be a value once returned by timer_...
Definition: timer.c:82
void timer_sleep(int64_t ticks)
Sleeps for approximately TICKS timer ticks.
Definition: timer.c:90
#define TIMER_FREQ
Number of timer interrupts per second.
Definition: timer.h:8