PKUOS - Pintos
Pintos source browser for PKU Operating System course
mlfqs-load-60.c
Go to the documentation of this file.
1/** Starts 60 threads that each sleep for 10 seconds, then spin in
2 a tight loop for 60 seconds, and sleep for another 60 seconds.
3 Every 2 seconds after the initial sleep, the main thread
4 prints the load average.
5
6 The expected output is this (some margin of error is allowed):
7
8 After 0 seconds, load average=1.00.
9 After 2 seconds, load average=2.95.
10 After 4 seconds, load average=4.84.
11 After 6 seconds, load average=6.66.
12 After 8 seconds, load average=8.42.
13 After 10 seconds, load average=10.13.
14 After 12 seconds, load average=11.78.
15 After 14 seconds, load average=13.37.
16 After 16 seconds, load average=14.91.
17 After 18 seconds, load average=16.40.
18 After 20 seconds, load average=17.84.
19 After 22 seconds, load average=19.24.
20 After 24 seconds, load average=20.58.
21 After 26 seconds, load average=21.89.
22 After 28 seconds, load average=23.15.
23 After 30 seconds, load average=24.37.
24 After 32 seconds, load average=25.54.
25 After 34 seconds, load average=26.68.
26 After 36 seconds, load average=27.78.
27 After 38 seconds, load average=28.85.
28 After 40 seconds, load average=29.88.
29 After 42 seconds, load average=30.87.
30 After 44 seconds, load average=31.84.
31 After 46 seconds, load average=32.77.
32 After 48 seconds, load average=33.67.
33 After 50 seconds, load average=34.54.
34 After 52 seconds, load average=35.38.
35 After 54 seconds, load average=36.19.
36 After 56 seconds, load average=36.98.
37 After 58 seconds, load average=37.74.
38 After 60 seconds, load average=37.48.
39 After 62 seconds, load average=36.24.
40 After 64 seconds, load average=35.04.
41 After 66 seconds, load average=33.88.
42 After 68 seconds, load average=32.76.
43 After 70 seconds, load average=31.68.
44 After 72 seconds, load average=30.63.
45 After 74 seconds, load average=29.62.
46 After 76 seconds, load average=28.64.
47 After 78 seconds, load average=27.69.
48 After 80 seconds, load average=26.78.
49 After 82 seconds, load average=25.89.
50 After 84 seconds, load average=25.04.
51 After 86 seconds, load average=24.21.
52 After 88 seconds, load average=23.41.
53 After 90 seconds, load average=22.64.
54 After 92 seconds, load average=21.89.
55 After 94 seconds, load average=21.16.
56 After 96 seconds, load average=20.46.
57 After 98 seconds, load average=19.79.
58 After 100 seconds, load average=19.13.
59 After 102 seconds, load average=18.50.
60 After 104 seconds, load average=17.89.
61 After 106 seconds, load average=17.30.
62 After 108 seconds, load average=16.73.
63 After 110 seconds, load average=16.17.
64 After 112 seconds, load average=15.64.
65 After 114 seconds, load average=15.12.
66 After 116 seconds, load average=14.62.
67 After 118 seconds, load average=14.14.
68 After 120 seconds, load average=13.67.
69 After 122 seconds, load average=13.22.
70 After 124 seconds, load average=12.78.
71 After 126 seconds, load average=12.36.
72 After 128 seconds, load average=11.95.
73 After 130 seconds, load average=11.56.
74 After 132 seconds, load average=11.17.
75 After 134 seconds, load average=10.80.
76 After 136 seconds, load average=10.45.
77 After 138 seconds, load average=10.10.
78 After 140 seconds, load average=9.77.
79 After 142 seconds, load average=9.45.
80 After 144 seconds, load average=9.13.
81 After 146 seconds, load average=8.83.
82 After 148 seconds, load average=8.54.
83 After 150 seconds, load average=8.26.
84 After 152 seconds, load average=7.98.
85 After 154 seconds, load average=7.72.
86 After 156 seconds, load average=7.47.
87 After 158 seconds, load average=7.22.
88 After 160 seconds, load average=6.98.
89 After 162 seconds, load average=6.75.
90 After 164 seconds, load average=6.53.
91 After 166 seconds, load average=6.31.
92 After 168 seconds, load average=6.10.
93 After 170 seconds, load average=5.90.
94 After 172 seconds, load average=5.70.
95 After 174 seconds, load average=5.52.
96 After 176 seconds, load average=5.33.
97 After 178 seconds, load average=5.16.
98*/
99
100#include <stdio.h>
101#include "tests/threads/tests.h"
102#include "threads/init.h"
103#include "threads/malloc.h"
104#include "threads/synch.h"
105#include "threads/thread.h"
106#include "devices/timer.h"
107
109
110static void load_thread (void *aux);
111
112#define THREAD_CNT 60
113
114void
116{
117 int i;
118
120
122 msg ("Starting %d niced load threads...", THREAD_CNT);
123 for (i = 0; i < THREAD_CNT; i++)
124 {
125 char name[16];
126 snprintf(name, sizeof name, "load %d", i);
128 }
129 msg ("Starting threads took %d seconds.",
131
132 for (i = 0; i < 90; i++)
133 {
134 int64_t sleep_until = start_time + TIMER_FREQ * (2 * i + 10);
135 int load_avg;
136 timer_sleep (sleep_until - timer_ticks ());
137 load_avg = thread_get_load_avg ();
138 msg ("After %d seconds, load average=%d.%02d.",
139 i * 2, load_avg / 100, load_avg % 100);
140 }
141}
142
143static void
144load_thread (void *aux UNUSED)
145{
146 int64_t sleep_time = 10 * TIMER_FREQ;
147 int64_t spin_time = sleep_time + 60 * TIMER_FREQ;
148 int64_t exit_time = spin_time + 60 * TIMER_FREQ;
149
150 thread_set_nice (20);
151 timer_sleep (sleep_time - timer_elapsed (start_time));
152 while (timer_elapsed (start_time) < spin_time)
153 continue;
154 timer_sleep (exit_time - timer_elapsed (start_time));
155}
#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
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
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
#define THREAD_CNT
void test_mlfqs_load_60(void)
static void load_thread(void *aux)
static int64_t start_time
Starts 60 threads that each sleep for 10 seconds, then spin in a tight loop for 60 seconds,...
#define NULL
Definition: stddef.h:4
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