-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
353 lines (276 loc) · 8.42 KB
/
main.js
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
var logicalAddressSpace = 64, //KB
physicalMemory = 16, //KB
frameSize = 1, //KB
pageReferences = [],
pageFrameTable = new PageFrameTable(physicalMemory, frameSize),
stats = {
total: 0,
faults: 0,
},
lastReferencedPage,
lastVictimPage,
pageTables = {},
total,
hits,
faults,
memDisplay,
pageTableDisplay,
intRef = 0,
playbtn,
stepbtn,
nextfaultbtn;
//Runs on page load to set up variables
function loader() {
total = document.getElementById("total");
hits = document.getElementById("hits");
faults = document.getElementById("faults");
memDisplay = document.getElementById("memDisplay");
pageTableDisplay = document.getElementById("pageTableDisplay");
playbtn = document.getElementById("playbtn");
stepbtn = document.getElementById("stepbtn");
nextfaultbtn = document.getElementById("nextfaultbtn");
//Load input and parse into structure
var input = document.getElementById("data").textContent.split("\n");
for (var i = 0; i < input.length; i++) {
var parts = input[i].split(":");
if (parts.length !== 2) {
continue;
}
pageReferences.push({
pid: parts[0].trim(),
page: parseInt(parts[1].trim(), 2)
});
}
updatePage();
}
//Updates global stats and runs all print functions
function updatePage() {
//Update global stats
total.innerHTML = stats.total;
hits.innerHTML = (stats.total - stats.faults) || 0;
faults.innerHTML = stats.faults;
//Update page frame table
var frag = pageFrameTable.PrintFrames();
memDisplay.innerHTML = "";
memDisplay.appendChild(frag);
//Update page tables of every process
pageTableDisplay.innerHTML = "";
for (var s in pageTables) {
if (pageTables.hasOwnProperty(s)) {
frag = pageTables[s].PrintPageTable();
pageTableDisplay.appendChild(frag);
}
}
}
//Processes next page reference from the input
function doNextPageRef() {
currentRef = pageReferences.shift();
stats.total++;
var pageTable = pageTables[currentRef.pid];
//If page table does not exist, create one
if (typeof pageTable === "undefined") {
pageTable = pageTables[currentRef.pid] = new PageTable(currentRef.pid);
}
//Add page to page table if its not there
pageTable.Add(currentRef.page);
//Tell frame table to access the referenced page
var isFault = pageFrameTable.AccessPage(pageTable, currentRef.page);
if (isFault) {
stats.faults++;
}
}
//Runs through the next reference and updates the display
//noUpdate is optional - if true it will not update the display
function simStep(noUpdate) {
if (pageReferences.length === 0) {
simStop();
playbtn.disabled = true;
stepbtn.disabled = true;
nextfaultbtn.disabled = true;
return;
}
doNextPageRef();
if (typeof noUpdate === "undefined" || !noUpdate) {
updatePage();
}
}
//Runs through entire simulatation (unless stopped)
function simPlay() {
playbtn.onclick = simStop;
playbtn.innerHTML = "Stop";
stepbtn.disabled = true;
nextfaultbtn.disabled = true;
clearInterval(intRef);
intRef = setInterval(function() {
simStep();
}, 50);
}
//Stops a running simulation
function simStop() {
clearInterval(intRef);
playbtn.onclick = simPlay;
playbtn.innerHTML = "Play";
stepbtn.disabled = false;
nextfaultbtn.disabled = false;
}
//Runs through simulation until a fault is reached or simulation is over
function simRunToNextFault() {
playbtn.disabled = true;
stepbtn.disabled = true;
nextfaultbtn.disabled = true;
var currentFaultCount = stats.faults;
while (stats.faults <= currentFaultCount && pageReferences.length > 0) {
simStep(true);
}
updatePage();
if (pageReferences.length > 0) {
playbtn.disabled = false;
stepbtn.disabled = false;
nextfaultbtn.disabled = false;
}
}
//Object to handle the page frame table
function PageFrameTable(memorySize, frameSize) {
this.memorySize = memorySize;
this.frameSize = frameSize;
this.numFrames = this.memorySize / this.frameSize;
this.padding = "0";
this.padlength = (this.numFrames+"").length;
for (var i = 1; i < this.padlength; i++) {
this.padding += this.padding[0];
}
this.frames = [];
this.freeFrames = [];
for (i = 0; i < this.numFrames; i++) {
this.freeFrames.push(i);
}
this.clock = 0;
}
//Simulates a page access - returns true if there is a page fault
PageFrameTable.prototype.AccessPage = function(pageTable, pageNum) {
//Update page table access stat
pageTable.references++;
//Set last referenced page
lastReferencedPage = pageTable.pages[pageNum];
//If the page is in the page frame table just update the time
if (typeof lastReferencedPage.frame !== "undefined" && lastReferencedPage.frame >= 0) {
this.frames[lastReferencedPage.frame].time = this.clock++;
return;
}
//Not in page table so find victim or empty spot
var victim = this.freeFrames.shift() || this.getNextVictim();
//If there is a victim, tell the victim page it is not in a frame
// and set it as last victim
if (typeof this.frames[victim] !== "undefined") {
var victimPage = this.frames[victim].page;
lastVictimPage = pageTables[victimPage.pid].pages[victimPage.number];
lastVictimPage.frame = -1;
}
pageTable.faults++;
//Tell the referenced page which frame it is in now
lastReferencedPage.frame = victim;
//Put the reference in the frame table with the current time
this.frames[victim] = {page: lastReferencedPage, time: this.clock++};
return true;
};
//Returns the index to the next vicitim or empty space in the frame table
PageFrameTable.prototype.getNextVictim = function() {
if (this.frames.length === 0 || this.numFrames < 2) {
return 0;
}
var last = 0;
for (var i = 1; i < this.numFrames; i++) {
//If there is an empty frame return it
if (typeof this.frames[i] === "undefined") {
return i;
}
if (this.frames[i].time < this.frames[last].time) {
last = i;
}
}
return last;
};
//Returns a document fragment with the current frame table info
PageFrameTable.prototype.PrintFrames = function() {
var frag = document.createDocumentFragment();
for (var i = 0; i < this.numFrames; i++) {
var frame = document.createElement("div");
frame.className = "frame";
var frameNum = document.createElement("div");
frameNum.className = "frameNum";
frameNum.innerHTML = "" + (this.padding + i).slice(this.padlength * -1);
frame.appendChild(frameNum);
if (typeof this.frames[i] !== "undefined") {
var framePid = document.createElement("span"),
container = document.createElement("div");
framePid.className = "framePid";
framePid.innerHTML = this.frames[i].page.pid;
container.appendChild(framePid);
frame.appendChild(container);
var framePage = document.createElement("span"),
container = document.createElement("div");
framePage.className = "framePage";
framePage.innerHTML = this.frames[i].page.number;
container.appendChild(framePage);
frame.appendChild(container);
if (i === lastReferencedPage.frame) {
frame.className += " lastReferenced";
}
}
frag.appendChild(frame);
}
return frag;
};
//Object to handle page tables
function PageTable(pid) {
this.pages = [];
this.faults = 0;
this.references = 0;
this.pid = pid;
}
//Adds page to the table if it is not there already
PageTable.prototype.Add = function(pageNum) {
if (typeof this.pages[pageNum] !== "undefined") {
return;
}
this.pages[pageNum] = {number: pageNum, frame: -1, pid: this.pid};
};
//Returns a document fragment with the current page table info
PageTable.prototype.PrintPageTable = function() {
var frag = document.createDocumentFragment(),
table = document.createElement("div"),
title = document.createElement("div"),
div, frame;
table.className = "pageTable";
title.className = "pageTitle";
title.innerHTML = this.pid;
table.appendChild(title);
for (var i = 0; i < this.pages.length; i++) {
if (typeof this.pages[i] === "undefined") {
continue;
}
div = document.createElement("div");
div.className = "page";
frame = this.pages[i].frame;
if (this.pages[i] === lastReferencedPage) {
div.className += " lastReferenced";
}
else if (this.pages[i] === lastVictimPage) {
div.className += " lastVictim";
}
div.innerHTML = this.pages[i].number;
if (frame !== -1) {
var frameLink = document.createElement("span");
frameLink.className = "frameLink";
frameLink.innerHTML = this.pages[i].frame;
div.appendChild(frameLink);
}
table.appendChild(div);
}
div = document.createElement("div");
div.className = "pageTableStats";
div.innerHTML = "Size: " + this.pages.length + "<br />Faults: " + this.faults + "<br /> References: " + this.references;
table.appendChild(div);
frag.appendChild(table);
return frag;
};