-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMemorySimulator.c
More file actions
422 lines (331 loc) · 11.8 KB
/
Copy pathVirtualMemorySimulator.c
File metadata and controls
422 lines (331 loc) · 11.8 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Virtual Memory Simulator
// This program simulates a virtual memory by creating a virtual TLB,
// virtual page table, and virtual hard disk. This program will be
// able to simulate hits and misses and perform the actions accordingly.
// ---------------------------------------------------------------------
// Due date : 7/26/2019
// Written by Jan Iglesias & Moses Quiliche
// Included libraries
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "project_headers.h"
// Definitions
#define VPAGES 1024 // Number of virtual pages
#define TLB_SIZE 8 // TLB Size
#define FRAMES 256 // Number of frames
// Global variables
unsigned int PageTable[VPAGES][4];
unsigned int TLB [TLB_SIZE][5];
unsigned int FrameTable[FRAMES];
unsigned int simTime;
unsigned int refCounter;
// int statistics [8] will contain the relevant stats data
// statistics [0] = TLB hit
// statistics [1] = TLB miss
// statistics [2] = Page Table Hit
// statistics [3] = Page Table Fault
// statistics [4] = TLB shootdown
// statistics [5] = Page Eviction
// statistics [6] = HD read
// statistics [7] = HD write
// statistics [8] = TLB Write
// statistics [9] = Page Table Write
// statistics [10] = Total accesses
int statistics[11];
// Prints out TLB
// Written by Moses Quiliche
void printTLB(void)
{
printf("=======================TLB=====================\n");
printf("|Entry| V | D | R | VPN | Physical |\n");
printf("===============================================\n");
for(int i = 0; i < TLB_SIZE; i++)
printf("| %d | %d | %d | %d | %d | %d |\n", i, TLB[i][0], TLB[i][1], TLB[i][2], TLB[i][3], TLB[i][4]);
printf("===============================================\n");
}
// Prints out valid entries of the PageTable
// Written by Moses Quiliche
void printValidPageTable(void)
{
printf("=======================PageTable======\n");
printf("| vpn | V | D | R | Physical |\n");
printf("======================================\n");
for(int i = 0; i < VPAGES; i++)
{
if(PageTable[i][0] == 1)
printf("| %d | %d | %d | %d | %d |\n", i, PageTable[i][0], PageTable[i][1], PageTable[i][2], PageTable[i][3]);
}
printf("======================================\n");
}
// Prints out allocated entires in FrameTable
// Written by Jan Iglesias
void printAllocatedFrames(void)
{
printf("ALLOCATED FRAMES\n");
for(int i = 0; i < FRAMES; i++)
if(FrameTable[i] == 1)
printf("%d\n", i);
return;
}
// Prints out unallocated entires in FrameTable
// Written by Moses Quiliche
void printUnAllocatedFrames(void)
{
printf("UNALLOCATED FRAMES\n");
for(int i = 0; i < FRAMES; i++)
if(FrameTable[i] == 0)
printf("%d\n", i);
return;
}
// Prints Stats
// statistics [0] = TLB hit
// statistics [1] = TLB miss
// statistics [2] = Page Table Hit
// statistics [3] = Page Table Fault
// statistics [4] = TLB shootdown
// statistics [5] = Page Eviction
// statistics [6] = HD read
// statistics [7] = HD write
// statistics [8] = TLB Write
// statistics [9] = Page Table Write
// statistics [10] = Total accesses
// Written by Jan Iglesias
void printStats(void)
{
printf("\n-----------------------------------------------------------------------------\n");
printf(" SIMULATION RESULTS \n");
printf("-----------------------------------------------------------------------------\n");
printf("TLB hits: %d\t\tTLB misses: %d\t\tTLB hit rate: %.2f%%\n", statistics[0], statistics[1], ((double) statistics[0]) / (statistics[0] + statistics[1]) * 100);
printf("TLB Shootdowns: %d\tTLB Writes: %d\n\n", statistics[4], statistics[8]);
printf("Pg Table accesses: %d\n", statistics[2] + statistics[3]);
printf("Pg Table hits: %d\tPg faults: %d\t\tPg Table hit rate: %.2f%%\n", statistics[2], statistics[3], ((double) statistics[2]) / (statistics[2] + statistics[3]) * 100);
printf("Pg evictions: %d\t\tPg Table writes: %d\n\n", statistics[5], statistics[9]);
printf("Hard disk reads: %d\tHard disk writes: %d\n", statistics[6], statistics[7]);
printf("-----------------------------------------------------------------------------\n");
}
// The following function simulates one access
// Written by : Jan Iglesias
void doSim (unsigned int TLB[][5], int tlb_size, unsigned int PageTable[][4],
int page_table_size, unsigned int FrameTable[], int frame_table_size,
unsigned int address, int read_write, int statistics [10])
{
char readOrWrite;
if(read_write == 1)
readOrWrite = 'w';
else
readOrWrite = 'r';
printf("T:\t%d\tAddress: %c\t%d\t\tVPN: %d\n", simTime++, readOrWrite, address, address / 1024);
memory_access ( TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, address, read_write, statistics );
}
// The following function will take the input from the user and simulate those memory accesses
// and display the results.
// Written by : Jan Iglesias
void getInput(void)
{
int address;
char readOrWrite;
int inputs = 0;
int addressArray[1024];
char readOrWriteArray[1024];
printf("Please enter r for read or w for write followed by the memory address you would like to access\n");
printf("To finish inputting values please enter z -1.\n");
printf("For example: To read 142, write 82, read 45, read 84. Please enter the following\n");
printf("\tr 142 \n\tw 82 \n\tr 45 \n\tr 84 \n\tz -1 \n");
printf("INPUT\n");
while(address != -1)
{
scanf(" %c %d", &readOrWrite, &address);
// printf("\n");
// printf("I just got %d\n", address);
if(address == -1)
break;
addressArray[inputs] = address;
if(tolower(readOrWrite) == 'r')
readOrWriteArray[inputs] = 0;
else
readOrWriteArray[inputs] = 1;
inputs++;
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < inputs; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// The following function will generate n random addresses with a 50/50 chance of being a read or write and run those memory accesses in the sim
// Written by : Jan Iglesias
void runSimWithNRandomRWFair (int n)
{
int generatedAddress;
int fairReadOrWrite;
int addressArray[1024];
char readOrWriteArray[1024];
for(int i = 0; i < n; i++)
{
generatedAddress = (rand() % (1048575 - 0 + 1) + 0);
fairReadOrWrite = (rand() % (1 - 0 + 1) + 0);
addressArray[i] = generatedAddress;
readOrWriteArray[i] = fairReadOrWrite;
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < n; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// Written by : Moses Quiliche
// The following function has the same functionality as runSimWithNRandomRWFair, but will assign a write access to all addresses.
void runSimWithNRandomAllW (int n)
{
int generatedAddress;
int fairReadOrWrite;
int addressArray[1024];
char readOrWriteArray[1024];
for(int i = 0; i < n; i++)
{
generatedAddress = (rand() % (1048575 - 0 + 1) + 0);
addressArray[i] = generatedAddress;
readOrWriteArray[i] = 1;
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < n; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// Written by : Moses Quiliche
// The following function will generate a 1000 total memory accesses. Starting at addresses assigned to page 0
// and incrementing the page every 40 memory_accesses
void consecutiveRandomAccessThousand (void)
{
int count = 0, upper, lower;
int generatedAddress;
int fairReadOrWrite;
int addressArray[1024];
char readOrWriteArray[1024];
for(int i = 1; i <= 25; i++)
{
for(int j = 0; j < 40; j++)
{
upper = i * 1024;
lower = (i - 1) * 1024;
generatedAddress = (rand() % (upper - lower + 1)) + lower;
fairReadOrWrite = (rand() % (1 - 0 + 1) + 0);
addressArray[count] = generatedAddress;
readOrWriteArray[count] = fairReadOrWrite;
count++;
}
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < 1000; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// Written by : Moses Quiliche
// The following function will generate a 3000 total memory accesses. Starting at addresses assigned to page 0
// and incrementing the page every 10 memory_accesses
void consecutiveRandomAccessThreeThousand (void)
{
int count = 0, upper, lower;
int generatedAddress;
int fairReadOrWrite;
int addressArray[4096];
char readOrWriteArray[4096];
for(int i = 1; i <= 300; i++)
{
for(int j = 0; j < 10; j++)
{
upper = i * 1024;
lower = (i - 1) * 1024;
generatedAddress = (rand() % (upper - lower + 1)) + lower;
fairReadOrWrite = (rand() % (1 - 0 + 1) + 0);
addressArray[count] = generatedAddress;
readOrWriteArray[count] = fairReadOrWrite;
count++;
}
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < 3000; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// Written by Jan Iglesias
// The following function will generate 20 random addresses in page 0, then 20 random addresses in page 19
// then 20 random addresses in page 1, then 20 random addresses in page 18, until 400 addresses are generated.
void randomAccessInvertShuffle (void)
{
int count = 0, upper, lower;
int generatedAddress;
int fairReadOrWrite;
int addressArray[4096];
char readOrWriteArray[4096];
for(int i = 1; i <= 10; i++)
{
// Getting lower 20
for(int j = 0; j < 20; j++)
{
upper = i * 1024;
lower = (i - 1) * 1024;
generatedAddress = (rand() % (upper - lower + 1)) + lower;
fairReadOrWrite = (rand() % (1 - 0 + 1) + 0);
addressArray[count] = generatedAddress;
readOrWriteArray[count] = fairReadOrWrite;
count++;
}
// Getting upper 20
for(int j = 0; j < 20; j++)
{
upper = (20 - i + 1) * 1024;
lower = (20 - i ) * 1024;
generatedAddress = (rand() % (upper - lower + 1)) + lower;
fairReadOrWrite = (rand() % (1 - 0 + 1) + 0);
addressArray[count] = generatedAddress;
readOrWriteArray[count] = fairReadOrWrite;
count++;
}
}
printf("-----------------------------------------\n");
printf(" VIRTUAL MEMORY SIMULATION\n");
printf("-----------------------------------------\n");
for(int i = 0; i < 400; i++)
{
// printf("About to simulate: address = %d, readOrWrite = %d\n", addressArray[i], readOrWriteArray[i]);
doSim (TLB, TLB_SIZE, PageTable, VPAGES, FrameTable, FRAMES, addressArray[i], readOrWriteArray[i], statistics );
}
printStats();
}
// Main function
int main(void)
{
refCounter=0;
int address, read_write;
// Seeding RNG needed for functions and starting timer
simTime = 1;
srand(time(NULL));
getInput();
return 0;
}