-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.c
240 lines (205 loc) · 7.87 KB
/
simulator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* CS 537 Programming Assignment 4 (Fall 2020)
* @file simulator.c
* @brief Core module that coordinates the simulation
* @author Michael Noguera and Julien de Castelnau
*/
#include "simulator.h"
#include "intervaltree.h"
#include "replace.h"
#include "stat.h"
#include <assert.h>
// === SIMULATION PARAMETERS ===
enum {
CLOCK_TICK = 1 /*ns*/,
DISK_PENALTY = 2000000 /*ns = 2 ms*/,
};
static_assert(CLOCK_TICK > 0, "Clock tick must be greater than zero.");
static_assert(DISK_PENALTY > 0, "Disk penalty must be greater than zero.");
static_assert(DISK_PENALTY % CLOCK_TICK == 0,
"Disk penalty must be a multiple of clock tick, else ticks won't "
"ever add up to a completed I/O.");
// === HELPER FUNCTIONS ===
/** @return true when all processes are finished */
static inline bool Simulator_notDone() {
return Process_existsWithStatus(RUNNABLE)
|| Process_existsWithStatus(BLOCKED);
}
/** ftell wrapper, returns current line on success or errors with message */
static inline long safe_ftell(FILE* tracefile) {
long fpos = ftell(tracefile);
if (fpos == -1) {
perror("Error finding current line in trace file.");
exit(EXIT_FAILURE);
}
return fpos;
}
/** Return to the file position saved in a given process */
static inline void Simulator_seekSavedLine(FILE* tracefile, Process* p) {
assert(p != NULL && p->status != FINISHED);
if (fseek(tracefile, p->currentPos, SEEK_SET) != 0
|| safe_ftell(tracefile) != p->currentPos) {
perror("Seek to position within tracefile failed.");
exit(EXIT_FAILURE);
}
}
/** Load the next blocked page, evicting if neccesary
* @details notifies replacement module of page load
*/
static inline void Simulator_finishCurrentDiskIO() {
Process* p = Process_peek(BLOCKED);
assert(p != NULL && p->status == BLOCKED);
assert(p->waitTime == 0 && "Page can't load until after process waits");
assert(p->waitingOnPage != NULL && "no page to fetch");
unsigned long ppn;
if (Memory_hasFreePage()) {
ppn = Memory_getFreePage();
Memory_loadPage(p->waitingOnPage, ppn);
Replace_notifyPageLoad(p->waitingOnPage->overhead);
} else {
ppn = Replace_getPageToEvict();
Memory_evictPage(ppn);
Memory_loadPage(p->waitingOnPage, ppn);
Replace_notifyPageLoad(p->waitingOnPage->overhead);
}
p->waitingOnPage = NULL;
}
/**
* Wrapper that handles special cases of status/context switches. Use only this
* function to perform context switches in the simulator.
* @param tracefile currently open file
* @param p process to switch
* @param new new status for process
* @param fpos
*/
static inline void Simulator_safelySwitchStatus(FILE* tracefile, Process* p,
ProcessStatus new, long fpos) {
assert(tracefile != NULL && ferror(tracefile) == 0);
assert(p != NULL);
ProcessStatus old = p->status;
assert(Process_peek(old) == p && "not at head of queue");
if (old == RUNNABLE) {
assert(tracefile != NULL && Process_peek(RUNNABLE) != NULL);
Process_peek(RUNNABLE)->currentPos = fpos;
} else if (old == BLOCKED) {
assert(p->waitTime == 0
&& "Cannot resume blocked process prematurely.");
} else if (old == FINISHED) {
fprintf(stderr,
"ERROR: Attempted to resume already-finished process.\n");
exit(EXIT_FAILURE);
}
if (Process_switchStatus(old, new) != p) {
fprintf(stderr,
"ERROR: Corruption in process queue. Do not modify data "
"structures concurrently with execution.");
exit(EXIT_FAILURE);
}
if (new == RUNNABLE) {
Simulator_seekSavedLine(tracefile, p);
} else if (new == BLOCKED) {
assert(p->waitTime == DISK_PENALTY
&& "Set block timer in order to block process.");
} else if (new == FINISHED) {
assert(!Process_hasLinesRemainingInFile(p)
&& "Cannot mark process as finished with lines left to run.");
Process_quit(p); // clean up and free memory
}
}
// === SIMULATION ===
/**
* Runs the simulation.
* @param pointer to tracefile, opened in read mode
*/
unsigned long Simulator_runSimulation(FILE* tracefile) {
unsigned long time = 0; // time in nanoseconds
assert(tracefile != NULL && "tracefile can't be null");
rewind(tracefile); // reset ptr
Process* p = Process_peek(RUNNABLE);
while (Simulator_notDone()) {
// 0. Account for clock tick
time += CLOCK_TICK;
Stat_default(CLOCK_TICK);
// 1. Advance disk wait counter if needed
if (Process_existsWithStatus(BLOCKED)) {
Process_peek(BLOCKED)->waitTime -= CLOCK_TICK;
if (Process_peek(BLOCKED)->waitTime == 0) {
Simulator_finishCurrentDiskIO(); // evicts if needed
Simulator_safelySwitchStatus(tracefile, Process_peek(BLOCKED),
RUNNABLE, 0);
continue;
}
}
// 2. If all processes are blocked, jump to the time where one finishes
// waiting
if (!Process_existsWithStatus(RUNNABLE)) {
assert((Process_peek(BLOCKED))->waitTime != 0);
unsigned long skip =
CLOCK_TICK * ((Process_peek(BLOCKED))->waitTime - 1);
time += skip;
Stat_default(skip);
Process_peek(BLOCKED)->waitTime -= skip;
assert((Process_peek(BLOCKED))->waitTime == 1);
continue;
}
// upon context switch, jump to the new file position
if (p != Process_peek(RUNNABLE)) {
p = Process_peek(RUNNABLE);
if (p->currentPos != ftell(tracefile)) {
Simulator_seekSavedLine(tracefile, p);
}
}
// 3. Find line to run next
unsigned long pid;
unsigned long vpn;
assert(p != NULL);
assert(p->currInterval != NULL);
long fpos = ftell(tracefile);
if (fpos == -1) {
perror("Error finding current line in trace file.");
exit(EXIT_FAILURE);
}
if (fscanf(tracefile, "%lu %lu\n", &pid, &vpn) != 2) {
perror("Error reading from trace file.");
exit(EXIT_FAILURE);
}
if (pid != p->pid) {
fprintf(stderr, "ERROR: Wrong line in tracefile recieved.\n");
}
// 4. Simulate memory reference
// get the virtual page-> look up in page table for this proc.
VPage* v = Process_getVirtualPage(p, vpn);
// Create new v. page if none exists
if (v == NULL) {
v = Process_allocVirtualPage(p, vpn);
assert(v != NULL);
assert(Process_getVirtualPage(p, vpn) == v);
}
// Is it a hit or a miss?
if (Process_virtualPageInMemory(p, vpn)) {
Stat_hit();
Replace_notifyPageAccess(v->overhead);
if (Process_onLastLineInInterval(p)
&& Process_hasIntervalsRemaining(p)) {
// advance file pointer
Process_jumpToNextInterval(p);
Simulator_seekSavedLine(tracefile, p);
// context switch
Process_switchStatus(RUNNABLE, RUNNABLE); // does not check for
// safety, but fast
} else if (Process_hasLinesRemainingInInterval(p)) {
p->currentline++;
} else {
// no remaining intervals, no remaining lines -> finished
Simulator_safelySwitchStatus(tracefile, p, FINISHED, 0);
p = NULL;
}
} else {
Stat_miss();
p->waitTime = DISK_PENALTY;
p->waitingOnPage = v;
Simulator_safelySwitchStatus(tracefile, p, BLOCKED, fpos);
}
}
return time;
}