-
Notifications
You must be signed in to change notification settings - Fork 2
/
mem_sim.c
538 lines (426 loc) · 14.3 KB
/
mem_sim.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include "mem_sim.h"
/**************************/
/* Name: Netanel Draiman */
/* ID: 200752152 */
/* Home Ex5 */
/**************************/
/************************************************************/
/* SYSTEM Simulated: */
/* 12 bit system - 12 bit per pointer */
/* virtual memory = 2^12 = 4096 bit */
/* 7 msb = page number - 2^7 = 128 pages */
/* 5 lsb = offset in page - size of page: 2^5 = 32 bit */
/************************************************************/
//Sizes are in Bytes
#define SWAP_SIZE 4096
#define PAGE_SIZE 32
#define MEMORY_SIZE 512
#define FRAME_NUM MEMORY_SIZE/PAGE_SIZE //16 frames
/**********************************/
/********** Data Structs **********/
/**********************************/
typedef struct page_descriptor {
unsigned int valid; //valid bit - 1=in main memory, 0=in swapfile\executable
unsigned int permission; // 0=READ & WRITE, 1=READ ONLY
unsigned int dirty; // 1=data was written to page, 0=no data was written
unsigned int frame; // page's frame # in main memory (if valid=1)
}page_descriptor_t;
struct sim_database {
page_descriptor_t* page_table; //pointer to page page_table
char* swapfile_name; //name of swap file
int swapfile_fd; //swap file fd
char* executable_name; //name of executable file
int executable_fd; //executable file fd
};
/**************************************/
/********** Global Variables **********/
/**************************************/
const int numOfPages = SWAP_SIZE/PAGE_SIZE; //128 pages
int exec_size; // save file size - used to determine beginning of heap\stack
static char RAM[MEMORY_SIZE]; //each 32 Bytes is a frame. total: 16 frames
int bitmap[FRAME_NUM]; //array to determine if RAM frame is occupied
//1 = occupied, 0 = free.
char temp_page[PAGE_SIZE]; //temp page for swapping
int global_counter=0; //used for LRU
int lru[FRAME_NUM]; //holds global counter for each page loaded in RAM
int lru_page[FRAME_NUM]; //holds page number for each page loaded in RAM
//Statistics Variables
int count_memoryAccess=0; //# of times vmload & vmstore got called
int count_hits = 0; //# of access to pages already in memory
int count_miss = 0; //# of pagefaults - access to pages not in memory
int count_illegalAddr = 0; //# of illegal addresses
int count_read_only_err = 0; //# of writing attempts to a read-only section
/**************************************************/
/********** Private Methods Declarations **********/
/**************************************************/
static void freeDb(sim_database_t*); //free memory
static int freeFrame(sim_database_t*); //find free frame in RAM
static int swap(sim_database_t*, int); //Swap out frame out of RAM.
static void init_tempPage(); //initialize temp page to PAGE_SIZE zeroes (32)
/******************************************/
/********** Main Program Methods **********/
/******************************************/
/*************************/
/* Constructor */
/*************************/
sim_database_t* vm_constructor(char *executable, int text_size, int data_size,
int bss_size){
if(executable == NULL)
return NULL;
//Init database
sim_database_t* db = (sim_database_t*)malloc(sizeof(sim_database_t));
if(db == NULL) {
perror("ERROR: failed to allocate memory for sim_database\n");
return NULL;
}
db->page_table = (page_descriptor_t*)malloc(
sizeof(page_descriptor_t)*(numOfPages));
if(db->page_table == NULL) {
perror("ERROR: failed to allocate memory for page_table\n");
free(db);
return NULL;
}
//open executable and swap files
db->executable_name = executable;
if( (db->executable_fd = open(db->executable_name, O_RDONLY, 0777)) < 0) {
perror("ERROR: failed to open executable\n");
free(db->page_table);
free(db);
return NULL;
}
db->swapfile_name = "swapfile.txt";
if((db->swapfile_fd = open(db->swapfile_name, O_RDWR | O_CREAT | O_TRUNC,
0777)) < 0) {
perror("ERROR: failed to create swap file\n");
free(db->page_table);
free(db);
return NULL;
}
//Setup Page Table
int i;
for(i=0; i < numOfPages; i++) {
if(i < text_size)
db->page_table[i].permission = 1; //READ ONLY - Executable text segment
else
db->page_table[i].permission = 0; //READ & WRITE
db->page_table[i].valid = 0;
db->page_table[i].dirty = 0;
db->page_table[i].frame = -1;
}
//Init LRU and Memory arrays
for(i=0; i < FRAME_NUM; i++) {
bitmap[i] = 0;
lru[i] = 0;
lru_page[i] = 0;
}
for(i=0; i < MEMORY_SIZE; i++) {
RAM[i] = '0';
}
exec_size = text_size + bss_size + data_size; //size in pages
return db;
}
/********************** Constructor END ***************************/
/*************************/
/* vm_load */
/*************************/
int vm_load(sim_database_t *sim_db, unsigned short address,
unsigned char *p_char) {
if(sim_db == NULL || p_char == NULL)
return -1;
int page, offset, frame;
page = (address >> 5); //shift number 5 bits to get page number
offset = (address & (PAGE_SIZE-1)); // mask 5 LSBs to get offset number
//update counters
count_memoryAccess++;
global_counter++;
//Check Address legality
if(page >= numOfPages || page < 0) {
count_illegalAddr++;
perror("ERROR: illegal address - page out of range\n");
return -1;
}
else if(offset >= PAGE_SIZE || offset < 0) {
count_illegalAddr++;
perror("ERROR: illegal address - offset out of range\n");
return -1;
}
int bytes;
/********** Find Page **********/
//if in RAM
if (sim_db->page_table[page].valid) {
count_hits++;
frame = sim_db->page_table[page].frame;
lru[frame] = global_counter;
*p_char = RAM[(frame * PAGE_SIZE) + offset];
return 0;
}
//if in SWAP
else if (sim_db->page_table[page].dirty) {
//Position file offset to Page position
bytes = lseek(sim_db->swapfile_fd, (page * PAGE_SIZE), SEEK_SET);
if(bytes != (page * PAGE_SIZE)) {
perror("ERROR: vmload failed to access SWAP file\n");
return -1;
}
frame = freeFrame(sim_db); //get Free frame in RAM
if(frame < 0) {
perror("ERROR: failed to swap out frame\n");
return -1;
}
//Read Page
bytes = read(sim_db->swapfile_fd, &RAM[frame * PAGE_SIZE], PAGE_SIZE);
if(bytes != PAGE_SIZE) {
perror("ERROR: failed to read from SWAP file\n");
return -1;
}
}
//if in DATA/BSS
else if( page < exec_size ) {
bytes = lseek(sim_db->executable_fd, (page * PAGE_SIZE), SEEK_SET);
if(bytes != (page * PAGE_SIZE)) {
perror("ERROR: vmload failed to access Exec file\n");
return -1;
}
frame = freeFrame(sim_db);
if(frame < 0) {
perror("ERROR: failed to swap out frame\n");
return -1;
}
bytes = read(sim_db->executable_fd, &RAM[frame * PAGE_SIZE], PAGE_SIZE);
if(bytes != PAGE_SIZE) {
perror("ERROR: failed to read from Exec file\n");
return -1;
}
}
else {
count_miss++;
perror("ERROR: Page not Initialized\n");
return -1;
}
count_miss++;
*p_char = RAM[(frame * PAGE_SIZE) + offset];
lru[frame] = global_counter;
lru_page[frame] = page;
bitmap[frame] = 1;
sim_db->page_table[page].valid = 1;
sim_db->page_table[page].frame = frame;
return 0;
}
/********************** vm_load END ***************************/
/*************************/
/* vm_store */
/*************************/
int vm_store(sim_database_t *sim_db, unsigned short address,
unsigned char value) {
if(sim_db == NULL)
return -1;
int page, offset, frame;
page = (address >> 5); //shift number 5 bits to get page number
offset = (address & (PAGE_SIZE-1)); // mask 5 LSBs to get offset number
//update counters
count_memoryAccess++;
global_counter++;
//Check Address legality
if(page >= numOfPages || page < 0) {
count_illegalAddr++;
perror("ERROR: illegal address - page out of range\n");
return -1;
}
else if(offset >= PAGE_SIZE || offset < 0) {
count_illegalAddr++;
perror("ERROR: illegal address - offset out of range\n");
return -1;
}
//check if address is part of exec text segement - read-only
else if(sim_db->page_table[page].permission) {
count_read_only_err++;
perror("ERROR: trying to write onto a read-only address\n");
return -1;
}
int bytes;
/********** Find Page **********/
//if in RAM
if(sim_db->page_table[page].valid) {
count_hits++;
lru[frame] = global_counter;
frame = sim_db->page_table[page].frame;
sim_db->page_table[page].dirty = 1;
RAM[(frame * PAGE_SIZE) + offset] = value;
return 0;
}
//if in SWAP
else if (sim_db->page_table[page].dirty) {
bytes = lseek(sim_db->swapfile_fd, (page * PAGE_SIZE), SEEK_SET);
if(bytes != (page * PAGE_SIZE)) {
perror("ERROR: vmload failed to access SWAP file\n");
return -1;
}
frame = freeFrame(sim_db);
if(frame < 0) {
perror("ERROR: failed to swap out frame\n");
return -1;
}
bytes = read(sim_db->swapfile_fd, &RAM[frame * PAGE_SIZE], PAGE_SIZE);
if(bytes != PAGE_SIZE) {
perror("ERROR: failed to read from SWAP file\n");
return -1;
}
}
//if in DATA/BSS
else if (!sim_db->page_table[page].permission && page < exec_size) {
bytes = lseek(sim_db->executable_fd, (page * PAGE_SIZE), SEEK_SET);
if(bytes != (page * PAGE_SIZE)) {
perror("ERROR: vmload failed to access DATA/BSS file\n");
return -1;
}
frame = freeFrame(sim_db);
if(frame < 0) {
perror("ERROR: failed to swap out frame\n");
return -1;
}
bytes = read(sim_db->executable_fd, &RAM[frame * PAGE_SIZE], PAGE_SIZE);
if(bytes != PAGE_SIZE) {
perror("ERROR: failed to read from DATA/BSS file\n");
return -1;
}
}
//else init new page
else {
init_tempPage(); //reset Temp Page
frame = freeFrame(sim_db);
if(frame < 0) {
perror("ERROR: failed to swap out frame\n");
return -1;
}
strncpy(&RAM[frame * PAGE_SIZE], temp_page, PAGE_SIZE);
}
count_miss++;
lru[frame] = global_counter;
lru_page[frame] = page;
bitmap[frame] = 1;
sim_db->page_table[page].frame = frame;
sim_db->page_table[page].dirty = 1;
sim_db->page_table[page].valid = 1;
RAM[(frame * PAGE_SIZE) + offset] = value;
return 0;
}
/********************** vm_store END ***************************/
/*************************/
/* Destructor */
/*************************/
void vm_destructor(sim_database_t *sim_db) {
close(sim_db->swapfile_fd);
close(sim_db->executable_fd);
freeDb(sim_db);
}
/********************** Destructor END ***************************/
/*************************/
/* vm_print */
/*************************/
void vm_print(sim_database_t* sim_db) {
if(sim_db == NULL)
return;
printf("\nPrinting Database...\n");
int i;
page_descriptor_t page;
printf("*** Printing Page Table ***\n");
for(i=0; i < numOfPages; i++) {
page = sim_db->page_table[i];
printf("Page #%d - valid = %d, permission = %d, dirty = %d, frame = %d \n",
i, page.valid, page.permission, page.dirty, page.frame);
}
printf("Swap File name = %s\n", sim_db->swapfile_name);
printf("Swap File fd = %d \n", sim_db->swapfile_fd);
printf("Executable File name = %s\n", sim_db->executable_name);
printf("\n*** Printing RAM ***\n");
for(i=0; i < MEMORY_SIZE; i++) {
if(!(i % PAGE_SIZE))
printf("\nframe #%d: ", i/PAGE_SIZE);
printf("%c", RAM[i]);
}
printf("\n\n*** Printing Statistics ***\n");
printf("Memory Access = %d, Hits = %d, Miss = %d\n",
count_memoryAccess, count_hits, count_miss);
printf("Illegal Address = %d, Write to READ-ONLY = %d\n",
count_illegalAddr, count_read_only_err);
}
/********************** vm_print END ***************************/
/****************************************************/
/********** Private Methods Implementation **********/
/****************************************************/
//Free Memory Method - (used outside of ctor)
static void freeDb(sim_database_t *db) {
free(db->page_table);
free(db);
}
/****************************************/
/****************************************/
//Finds free Frame in RAM or frees an old one
//return value: on sucess - frame #, on failure returns -1
static int freeFrame(sim_database_t *db) {
int i, page = 0, frame = 0;
int min_counter = INT_MAX, min_lru;
int bytes;
//search for free Frame in RAM
for(i=0; i < FRAME_NUM; i++) {
if(!bitmap[i])
return i;
if(min_counter > lru[i]) {
min_counter = lru[i];
min_lru = i;
}
}
//swap out Frame according to minimum LRU counter
if(swap(db,min_lru)) {
return -1;
}
//return Free Frame
return min_lru;
}
/****************************************/
/****************************************/
//Swap out frame out of RAM according to LRU counter
static int swap(sim_database_t* db, int frame) {
int i, page, bytes;
page = lru_page[frame];
//if not valid - error!
if(!db->page_table[page].valid) {
perror("ERROR: trying to swap out a non-valid page\n");
return -1;
}
//if dirty - in SWAP
else if (db->page_table[page].dirty) {
//Write with offset
bytes = pwrite(db->swapfile_fd, &RAM[frame * PAGE_SIZE],
PAGE_SIZE, (page * PAGE_SIZE));
if(bytes != PAGE_SIZE) {
perror("ERROR: Swapping frame failed - writing to swap failed\n");
return -1;
}
}
//else in Exec - no need to swap out
//Update page table
db->page_table[page].valid = 0;
db->page_table[page].frame = -1;
bitmap[frame] = 0;
return 0;
}
/****************************************/
/****************************************/
//initialize temp page to PAGE_SIZE zeroes (32)
static void init_tempPage() {
int i;
for(i=0; i < PAGE_SIZE; i++) {
temp_page[i] = '0';
}
}
/****************************************************/
/****************************************************/
/****************************************************/