forked from JamieGoodson/memory-management-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
364 lines (298 loc) · 9.25 KB
/
Copy pathscript.js
File metadata and controls
364 lines (298 loc) · 9.25 KB
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
354
355
356
357
358
359
360
361
362
363
364
function Process(size, time) {
this.size = size;
this.timeLeft = time;
this.allocatedBlock = null;
this.id = processID;
processID += 1;
this.isAllocated = function() {
return this.allocatedBlock != null;
};
this.tick = function() {
this.timeLeft -=1;
};
};
function MemControlBlock(size) {
this.size = size;
this.process = null;
this.available = true;
this.next = null;
this.prev = null;
this.fromPartition = false; // Used to determine whether height of a MemControlBlock needs to be added
this.setProcess = function(process) {
if (process == null) {
this.process = null;
this.available = true;
} else {
this.process = process;
this.available = false;
};
};
};
// Simulates memory
function Heap() {
this.head = null;
this.size = 0;
// Allocate process to memory.
// Use best-fit method: from the list of holes, choose the smallest hole
this.requestAllocation = function(process, firstFit, fragmentation) {
blockBestFit = this.head;
// Make sure our initial best block is valid
while ((blockBestFit.size < process.size) || (!blockBestFit.available)) {
blockBestFit = blockBestFit.next;
if (blockBestFit == null) {return false}; // Means we couldn't even find an initial valid block
};
//log("Initial best block: " + blockBestFit.size);
// If firstFit requested, then no need to find a better block
if (!firstFit){
// See if there's an even better block
block = blockBestFit.next;
while (block != null) {
//log("Testing block: " + block.size);
if ((block.size >= process.size) && (block.available) && (block.size < blockBestFit.size)) {
blockBestFit = block;
//log("New best block: " + blockBestFit.size);
};
block = block.next;
};
};
spaceLeftover = blockBestFit.size - (process.size + memControlBlockSize); // Space leftover if block was divided
if (!fragmentation){
// Partition block if needed
if (spaceLeftover > 0) {
newBlock = new MemControlBlock(spaceLeftover);
nextBlock = blockBestFit.next;
if (nextBlock != null) {
nextBlock.prev = newBlock;
newBlock.next = nextBlock;
};
blockBestFit.next = newBlock;
newBlock.prev = blockBestFit;
blockBestFit.size = process.size;
newBlock.fromPartition = true;
};
}
else{
document.getElementById("internalFragmentation").innerHTML = spaceLeftover + parseInt(document.getElementById("internalFragmentation").innerHTML);
};
blockBestFit.setProcess(process);
process.allocatedBlock = blockBestFit;
document.getElementById("externalFragmentation").innerHTML = heap.getExternalFragmentation();
return true;
};
this.deallocateProcess = function(process) {
document.getElementById("internalFragmentation").innerHTML = parseInt(document.getElementById("internalFragmentation").innerHTML) - process.allocatedBlock.size + process.size;
process.allocatedBlock.setProcess(null);
process.allocatedBlock = null;
document.getElementById("externalFragmentation").innerHTML = heap.getExternalFragmentation();
};
this.add = function(block) {
if (this.head == null) {
this.head = block;
} else {
block.next = this.head;
this.head.prev = block;
this.head = block;
};
this.size += block.size;
}
this.getExternalFragmentation = function() {
block = this.head;
space = 0;
while (block != null) {
if (block.available){
space += block.size
};
block = block.next;
};
return space;
};
//TODO: Make this properly use add/remove
this.compact = function() {
block = this.head;
space = 0;
while (block != null) {
if (block.available) {
// Save the total space and remove the block
space += block.size;
this.size -= block.size;
if (block == this.head){
this.head = block.next
}
else {
block.prev.next = block.next;
if (block.next){
block.next.prev = block.prev;
};
}
};
block = block.next;
};
this.add(new MemControlBlock(space));
};
this.toString = function() {
string = "[|";
block = this.head;
prefix = "";
suffix = "</span> |";
while (block != null) {
if (block.available) {prefix = "<span style='color: #01DF01;'> "} else {prefix = "<span style='color: #FF0000;'> "};
string += (prefix + block.size + suffix);
block = block.next;
};
string += "]"
return string;
};
this.repaint = function() {
block = this.head;
memoryDiv.innerHTML = "";
while (block != null) {
height = ((block.size/heap.size)*100);
if (block.fromPartition) {
height += (memControlBlockSize/heap.size)*100;
};
// Create div block element
divBlock = document.createElement("div");
divBlock.style.height = (height + "%");
divBlock.setAttribute("id", "block");
if (block.available) {divBlock.className = "available"} else {divBlock.className = "unavailable"};
memoryDiv.appendChild(divBlock);
// Add size label
// TODO: Show process details on mouse over
blockLabel = document.createElement("div");
blockLabel.setAttribute("id", "blockLabel");
blockLabel.style.height = (height + "%");
blockLabel.innerHTML = block.size + "K";
if (block.process != null && height > 10){
blockLabel.innerHTML += "<br />Process ID: "+ block.process.id;
};
if (height <= 2) {
blockLabel.style.display = "none";
};
divBlock.appendChild(blockLabel);
block = block.next;
};
};
};
// Handle front-end process submission
document.getElementById("processForm").onsubmit = function () {
elements = this.elements; // Form elements
inProcessSize = elements.namedItem("processSize");
inProcessTime = elements.namedItem("processTime");
process = new Process(parseInt(inProcessSize.value), parseInt(inProcessTime.value));
/* heap.requestAllocation(process);
heap.repaint();*/
processes.push(process);
addProcessToTable(process);
// Debug log
log("Requesting: " + process.size);
log(heap.toString() + "<br>");
// Clear form
inProcessSize.value = "";
inProcessTime.value = "";
return false;
};
function log(string) {
logBox.innerHTML += (string + "<br />");
}
function addProcessToTable(process) {
row = document.createElement("tr");
row.setAttribute("id", "process" + process.id);
colName = document.createElement("td");
colName.innerHTML = process.id;
colSize = document.createElement("td");
colSize.innerHTML = process.size;
colTime = document.createElement("td");
colTime.setAttribute("id", "process" + process.id + "timeLeft");
colTime.innerHTML = process.timeLeft;
row.appendChild(colName);
row.appendChild(colSize);
row.appendChild(colTime);
processTable.appendChild(row);
};
function removeProcessFromTable(process) {
processTable.removeChild(document.getElementById("process" + process.id));
};
// TODO: Update 'time left' for each row in table
function refreshTable() {
for (i=0; i<processes.length; i++) {
process = processes[i];
document.getElementById("process" + process.id + "timeLeft").innerHTML = process.timeLeft;
};
};
var logBox = document.getElementById("logBox");
var memoryDiv = document.getElementById("memory");
var processTable = document.getElementById("processTable");
var memControlBlockSize = 0;
var processID = 1;
var processes = [];
heap = new Heap();
blockSizes = [450,150,70,50,300,200];
compaction = false;
firstFit = false;
fragmentation = false;
for (i=0; i<blockSizes.length; i++) {
heap.add(new MemControlBlock(blockSizes[i]));
};
// Draw initial heap
heap.repaint();
// Start clock
// Loop through all processes and allocate those that require allocation. Deallocate those that have <0 time remaining
var clock = setInterval(function() {
for (i=0; i<processes.length; i++) {
process = processes[i];
if (!process.isAllocated()) {
if (!heap.requestAllocation(process, firstFit, fragmentation)){
if (compaction){
heap.compact();
};
};
document.getElementById("ProcessSize").focus()
} else {
process.tick();
if (process.timeLeft < 1) {
// Deallocate process from heap
heap.deallocateProcess(process);
// Remove process from processes array
index = processes.indexOf(process);
if (index > -1) {
processes.splice(index, 1);
};
// Remove process from table
removeProcessFromTable(process);
};
};
};
firstFit = document.getElementById("firstFit").checked;
fragmentation = document.getElementById("fragmentation").checked;
compaction = document.getElementById("compaction").checked;
refreshTable();
heap.repaint();
}, 1000);
/*processSizes = [150, 383, 400, 800, 2000, 10, 50, 20, 300, 830];
var processIndex = 0;
var allocateProcesss = setInterval(function() {
process = new Process(processSizes[processIndex]);
//log("Requesting allocation for process: " + process.size);
if (heap.requestAllocation(process)) {
//log("Process successfully allocated.");
//log("New heap: " + heap.toString() + "<br />");
heap.repaint();
} else {
//log("Process failed to be allocated." + "<br />");
};
processIndex += 1;
if (processIndex == (processSizes.length-1)) {
clearInterval(allocateProcesss);
}
}, 1000);*/
/*for (i=0; i<processSizes.length; i++) {
process = new Process(processSizes[i]);
log("Requesting allocation for process: " + process.size);
if (heap.requestAllocation(process)) {
log("Process successfully allocated.");
log("New list: " + heap.toString() + "<br />");
heap.repaint();
} else {
log("Process failed to be allocated." + "<br />");
};
};*/