PKUOS - Pintos
Pintos source browser for PKU Operating System course
mlfqs-recent-1.c
Go to the documentation of this file.
1/** Checks that recent_cpu is calculated properly for the case of
2 a single ready process.
3
4 The expected output is this (some margin of error is allowed):
5
6 After 2 seconds, recent_cpu is 6.40, load_avg is 0.03.
7 After 4 seconds, recent_cpu is 12.60, load_avg is 0.07.
8 After 6 seconds, recent_cpu is 18.61, load_avg is 0.10.
9 After 8 seconds, recent_cpu is 24.44, load_avg is 0.13.
10 After 10 seconds, recent_cpu is 30.08, load_avg is 0.15.
11 After 12 seconds, recent_cpu is 35.54, load_avg is 0.18.
12 After 14 seconds, recent_cpu is 40.83, load_avg is 0.21.
13 After 16 seconds, recent_cpu is 45.96, load_avg is 0.24.
14 After 18 seconds, recent_cpu is 50.92, load_avg is 0.26.
15 After 20 seconds, recent_cpu is 55.73, load_avg is 0.29.
16 After 22 seconds, recent_cpu is 60.39, load_avg is 0.31.
17 After 24 seconds, recent_cpu is 64.90, load_avg is 0.33.
18 After 26 seconds, recent_cpu is 69.27, load_avg is 0.35.
19 After 28 seconds, recent_cpu is 73.50, load_avg is 0.38.
20 After 30 seconds, recent_cpu is 77.60, load_avg is 0.40.
21 After 32 seconds, recent_cpu is 81.56, load_avg is 0.42.
22 After 34 seconds, recent_cpu is 85.40, load_avg is 0.44.
23 After 36 seconds, recent_cpu is 89.12, load_avg is 0.45.
24 After 38 seconds, recent_cpu is 92.72, load_avg is 0.47.
25 After 40 seconds, recent_cpu is 96.20, load_avg is 0.49.
26 After 42 seconds, recent_cpu is 99.57, load_avg is 0.51.
27 After 44 seconds, recent_cpu is 102.84, load_avg is 0.52.
28 After 46 seconds, recent_cpu is 106.00, load_avg is 0.54.
29 After 48 seconds, recent_cpu is 109.06, load_avg is 0.55.
30 After 50 seconds, recent_cpu is 112.02, load_avg is 0.57.
31 After 52 seconds, recent_cpu is 114.89, load_avg is 0.58.
32 After 54 seconds, recent_cpu is 117.66, load_avg is 0.60.
33 After 56 seconds, recent_cpu is 120.34, load_avg is 0.61.
34 After 58 seconds, recent_cpu is 122.94, load_avg is 0.62.
35 After 60 seconds, recent_cpu is 125.46, load_avg is 0.64.
36 After 62 seconds, recent_cpu is 127.89, load_avg is 0.65.
37 After 64 seconds, recent_cpu is 130.25, load_avg is 0.66.
38 After 66 seconds, recent_cpu is 132.53, load_avg is 0.67.
39 After 68 seconds, recent_cpu is 134.73, load_avg is 0.68.
40 After 70 seconds, recent_cpu is 136.86, load_avg is 0.69.
41 After 72 seconds, recent_cpu is 138.93, load_avg is 0.70.
42 After 74 seconds, recent_cpu is 140.93, load_avg is 0.71.
43 After 76 seconds, recent_cpu is 142.86, load_avg is 0.72.
44 After 78 seconds, recent_cpu is 144.73, load_avg is 0.73.
45 After 80 seconds, recent_cpu is 146.54, load_avg is 0.74.
46 After 82 seconds, recent_cpu is 148.29, load_avg is 0.75.
47 After 84 seconds, recent_cpu is 149.99, load_avg is 0.76.
48 After 86 seconds, recent_cpu is 151.63, load_avg is 0.76.
49 After 88 seconds, recent_cpu is 153.21, load_avg is 0.77.
50 After 90 seconds, recent_cpu is 154.75, load_avg is 0.78.
51 After 92 seconds, recent_cpu is 156.23, load_avg is 0.79.
52 After 94 seconds, recent_cpu is 157.67, load_avg is 0.79.
53 After 96 seconds, recent_cpu is 159.06, load_avg is 0.80.
54 After 98 seconds, recent_cpu is 160.40, load_avg is 0.81.
55 After 100 seconds, recent_cpu is 161.70, load_avg is 0.81.
56 After 102 seconds, recent_cpu is 162.96, load_avg is 0.82.
57 After 104 seconds, recent_cpu is 164.18, load_avg is 0.83.
58 After 106 seconds, recent_cpu is 165.35, load_avg is 0.83.
59 After 108 seconds, recent_cpu is 166.49, load_avg is 0.84.
60 After 110 seconds, recent_cpu is 167.59, load_avg is 0.84.
61 After 112 seconds, recent_cpu is 168.66, load_avg is 0.85.
62 After 114 seconds, recent_cpu is 169.69, load_avg is 0.85.
63 After 116 seconds, recent_cpu is 170.69, load_avg is 0.86.
64 After 118 seconds, recent_cpu is 171.65, load_avg is 0.86.
65 After 120 seconds, recent_cpu is 172.58, load_avg is 0.87.
66 After 122 seconds, recent_cpu is 173.49, load_avg is 0.87.
67 After 124 seconds, recent_cpu is 174.36, load_avg is 0.88.
68 After 126 seconds, recent_cpu is 175.20, load_avg is 0.88.
69 After 128 seconds, recent_cpu is 176.02, load_avg is 0.88.
70 After 130 seconds, recent_cpu is 176.81, load_avg is 0.89.
71 After 132 seconds, recent_cpu is 177.57, load_avg is 0.89.
72 After 134 seconds, recent_cpu is 178.31, load_avg is 0.89.
73 After 136 seconds, recent_cpu is 179.02, load_avg is 0.90.
74 After 138 seconds, recent_cpu is 179.72, load_avg is 0.90.
75 After 140 seconds, recent_cpu is 180.38, load_avg is 0.90.
76 After 142 seconds, recent_cpu is 181.03, load_avg is 0.91.
77 After 144 seconds, recent_cpu is 181.65, load_avg is 0.91.
78 After 146 seconds, recent_cpu is 182.26, load_avg is 0.91.
79 After 148 seconds, recent_cpu is 182.84, load_avg is 0.92.
80 After 150 seconds, recent_cpu is 183.41, load_avg is 0.92.
81 After 152 seconds, recent_cpu is 183.96, load_avg is 0.92.
82 After 154 seconds, recent_cpu is 184.49, load_avg is 0.92.
83 After 156 seconds, recent_cpu is 185.00, load_avg is 0.93.
84 After 158 seconds, recent_cpu is 185.49, load_avg is 0.93.
85 After 160 seconds, recent_cpu is 185.97, load_avg is 0.93.
86 After 162 seconds, recent_cpu is 186.43, load_avg is 0.93.
87 After 164 seconds, recent_cpu is 186.88, load_avg is 0.94.
88 After 166 seconds, recent_cpu is 187.31, load_avg is 0.94.
89 After 168 seconds, recent_cpu is 187.73, load_avg is 0.94.
90 After 170 seconds, recent_cpu is 188.14, load_avg is 0.94.
91 After 172 seconds, recent_cpu is 188.53, load_avg is 0.94.
92 After 174 seconds, recent_cpu is 188.91, load_avg is 0.95.
93 After 176 seconds, recent_cpu is 189.27, load_avg is 0.95.
94 After 178 seconds, recent_cpu is 189.63, load_avg is 0.95.
95 After 180 seconds, recent_cpu is 189.97, load_avg is 0.95.
96*/
97
98#include <stdio.h>
99#include "tests/threads/tests.h"
100#include "threads/init.h"
101#include "threads/malloc.h"
102#include "threads/synch.h"
103#include "threads/thread.h"
104#include "devices/timer.h"
105
106/** Sensitive to assumption that recent_cpu updates happen exactly
107 when timer_ticks() % TIMER_FREQ == 0. */
108
109void
111{
113 int last_elapsed = 0;
114
116
117 do
118 {
119 msg ("Sleeping 10 seconds to allow recent_cpu to decay, please wait...");
122 + 10 * TIMER_FREQ);
123 }
124 while (thread_get_recent_cpu () > 700);
125
127 for (;;)
128 {
129 int elapsed = timer_elapsed (start_time);
130 if (elapsed % (TIMER_FREQ * 2) == 0 && elapsed > last_elapsed)
131 {
132 int recent_cpu = thread_get_recent_cpu ();
133 int load_avg = thread_get_load_avg ();
134 int elapsed_seconds = elapsed / TIMER_FREQ;
135 msg ("After %d seconds, recent_cpu is %d.%02d, load_avg is %d.%02d.",
136 elapsed_seconds,
137 recent_cpu / 100, recent_cpu % 100,
138 load_avg / 100, load_avg % 100);
139 if (elapsed_seconds >= 180)
140 break;
141 }
142 last_elapsed = elapsed;
143 }
144}
#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
void msg(const char *format,...)
Definition: lib.c:28
static int64_t start_time
Starts 60 threads that each sleep for 10 seconds, then spin in a tight loop for 60 seconds,...
void test_mlfqs_recent_1(void)
Checks that recent_cpu is calculated properly for the case of a single ready process.
#define DIV_ROUND_UP(X, STEP)
Yields X divided by STEP, rounded up.
Definition: round.h:10
signed long long int int64_t
Definition: stdint.h:16
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
int thread_get_recent_cpu(void)
Returns 100 times the current thread's recent_cpu value.
Definition: thread.c:373
int thread_get_load_avg(void)
Returns 100 times the system load average.
Definition: thread.c:365
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