-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryManager_Exame.c
463 lines (390 loc) · 12.2 KB
/
MemoryManager_Exame.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
/**
* CES-33 Final Project
*
* Memory Manager Simulator
*
* Felipe Tuyama de F. Barbosa
* Luiz Angel Rocha Rafael
*/
/**
* Memory Manager Includes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Memory Manager Defines
*/
// Max Number Definitions
#define MaxStringLength 7
// Virtual Memory Pages
#define PagesAmount 256
#define TLBEntriesAmount 16
// Physical Memory RAM
#define FramesAmount 128
#define FrameBytesSize 256
// Address Bits
#define SegmentBits 4
#define PageBits 256
#define OffsetBits 256
//Segmentation
#define SegmentsAmount 4
//Files
#define inputfile_default "addresses.txt"
#define backingStore_default "BACKING_STORE.bin"
#define result_default "result.txt"
/**
* Memory Manager Structs
*/
// Page Table (256 pages)
typedef struct pageTable {
int frameNumber[PagesAmount];
unsigned int FIFO[FramesAmount];
} PageTable;
// Segmentation (4 segmentations)
typedef struct segmentation {
PageTable pageTable[SegmentsAmount];
} Segmentation;
// Page (256 bytes)
typedef struct page {
char PageContent[FrameBytesSize];
} Page;
// TLB - Maps Pages on Physical Memory (16 entries)
typedef struct tlb {
int segmentNumber[TLBEntriesAmount];
int pageNumber[TLBEntriesAmount];
int frameNumber[TLBEntriesAmount];
unsigned int FIFO[TLBEntriesAmount];
} TLB;
// Physical Memory (65.536 bytes)
typedef struct memory {
Page frame[SegmentsAmount*FramesAmount];
int available[SegmentsAmount*FramesAmount];
int availableSegmentation[SegmentsAmount];
} Memory;
// Statistics
typedef struct statistics {
int TranslatedAddressesCounter;
int SegmentationFaultsCounter;
int PageFaultsCounter;
int TLBHitsCounter;
} Statistics;
/**
* Program Global Variables
*/
FILE *addresses, *result, *backingStore;
char line[MaxStringLength] = "";
Segmentation *_descriptorTable;
Statistics *_statistics;
Memory *_memory;
TLB *_TLB;
/**
* Output results methods
*/
// Write Output results
void writeOut(int segmentNumber, int virtualAddress, int realAddress, int value)
{
fprintf(result, "Virtual address: %d-%d ", segmentNumber, virtualAddress);
fprintf(result, "Physical address: %d-%d ", segmentNumber, realAddress);
fprintf(result, "Value: %d\n", value);
_statistics->TranslatedAddressesCounter++;
}
// Statistics Output Log
void statisticsLog()
{
float segmentationFaultRate = _statistics->SegmentationFaultsCounter;
segmentationFaultRate = segmentationFaultRate / _statistics->TranslatedAddressesCounter;
float pageFaultRate = _statistics->PageFaultsCounter;
pageFaultRate = pageFaultRate / _statistics->TranslatedAddressesCounter;
float tlbHitsRate = _statistics->TLBHitsCounter;
tlbHitsRate = tlbHitsRate / _statistics->TranslatedAddressesCounter;
fprintf(result, "Number of Translated Addresses = %d\n", _statistics->TranslatedAddressesCounter);
fprintf(result, "Segmentation Faults = %d\n", _statistics->SegmentationFaultsCounter);
fprintf(result, "Segmentation Fault Rate = %.3f\n", segmentationFaultRate);
fprintf(result, "Page Faults = %d\n", _statistics->PageFaultsCounter);
fprintf(result, "Page Fault Rate = %.3f\n", pageFaultRate);
fprintf(result, "TLB Hits = %d\n", _statistics->TLBHitsCounter);
fprintf(result, "TLB Hit Rate = %.3f\n", tlbHitsRate);
}
/**
* Initialization/Finalization methods
*/
// Initializing the Memory Manager
void initialize(char * inputfile)
{
backingStore = fopen(backingStore_default, "r");
addresses = fopen(inputfile, "r");
result = fopen(result_default, "w");
_statistics = (Statistics*)malloc(sizeof(Statistics));
_memory = (Memory*)malloc(sizeof(Memory));
_TLB = (TLB*)malloc(sizeof(TLB));
_descriptorTable = (Segmentation*)malloc(SegmentsAmount * sizeof(Segmentation));
for (int j = 0; j < SegmentsAmount; j ++) {
for (int i = 0; i < PagesAmount; i++) {
_descriptorTable[j].pageTable->frameNumber[i] = -1;
_descriptorTable[j].pageTable->FIFO[i] = -1;
}
}
for (int i = 0; i < TLBEntriesAmount; i++)
_TLB->segmentNumber[i] = _TLB->frameNumber[i] = _TLB->pageNumber[i] = _TLB->FIFO[i] = -1;
for (int i = 0; i < SegmentsAmount; i++)
_memory->availableSegmentation[i] = -1;
for (int i = 0; i < SegmentsAmount*FramesAmount; i++)
_memory->available[i] = 1;
_statistics->TranslatedAddressesCounter = 0;
_statistics->SegmentationFaultsCounter = 0;
_statistics->PageFaultsCounter = 0;
_statistics->TLBHitsCounter = 0;
}
// Finalizing the Memory Manager
void finalize()
{
statisticsLog();
fclose(backingStore);
fclose(addresses);
fclose(result);
free(_descriptorTable);
free(_memory);
free(_TLB);
}
/**
* Managing Page Table methods
*/
// Finding Requested Page on Page Table
int findPageOnPageTable(int segmentNumber, int pageNumber)
{
//Return -1 if page is not present (Page Fault)
return _descriptorTable[segmentNumber].pageTable->frameNumber[pageNumber];
}
// Setting Used Page on Page Table
void setPageOnPageTable(int segmentNumber, int pageNumber, int frameNumber)
{
_descriptorTable[segmentNumber].pageTable->frameNumber[pageNumber] = frameNumber;
}
/**
* Managing TLB methods
*/
// Update TLB FIFO
int updateTLBFIFO()
{
int slot = 0;
//There is a free slot on TLB
if (_TLB->FIFO[0] == -1) {
//Move the FIFO
for (int i = 0; i < TLBEntriesAmount - 1; i++)
_TLB->FIFO[i] = _TLB->FIFO[i + 1];
//Seeks for free slot on the TLB
for (int i = 0; i < TLBEntriesAmount; i++) {
if (_TLB->pageNumber[i] == -1) {
slot = i;
break;
}
}
}
//There is not a free slot on TLB
else {
//Picks the first slot on TLB
slot = _TLB->FIFO[0];
for (int i = 0; i < TLBEntriesAmount - 1; i++)
_TLB->FIFO[i] = _TLB->FIFO[i + 1];
}
_TLB->FIFO[TLBEntriesAmount - 1] = slot;
return slot;
}
// Finding Requested Page on TLB
int findPageOnTLB(int segmentNumber, int pageNumber)
{
for (int i = 0; i < TLBEntriesAmount; i++) {
// Find segment number on TLB
if (_TLB->segmentNumber[i] == segmentNumber)
// Find page number on TLB
if (_TLB->pageNumber[i] == pageNumber) {
_statistics->TLBHitsCounter++;
//Requested Page is found on TLB
return _TLB->frameNumber[i];
}
}
// Requested Page not found on TLB.
return -1;
}
// Setting Used Page on TLB
void setPageOnTLB(int segmentNumber, int pageNumber, int frameNumber)
{
int newTLBindex = updateTLBFIFO();
_TLB->segmentNumber[newTLBindex] = segmentNumber;
_TLB->frameNumber[newTLBindex] = frameNumber;
_TLB->pageNumber[newTLBindex] = pageNumber;
}
/**
* Managing Memory methods
*/
// Find Oldest Frame on memory (first element on FIFO)
int findOldestFrameOnMemory(int segmentNumber, int pageNumber)
{
//The oldest page on the queue
int switchedpage = _descriptorTable[segmentNumber].pageTable->FIFO[0];
//Sets the switched page as unavailable
int slot = _descriptorTable[segmentNumber].pageTable->frameNumber[switchedpage];
_descriptorTable[segmentNumber].pageTable->frameNumber[switchedpage] = -1;
//Updates the
for (int i = 0; i < FramesAmount - 1; i++) {
_descriptorTable[segmentNumber].pageTable->FIFO[i] = _descriptorTable[segmentNumber].pageTable->FIFO[i + 1];
}
_descriptorTable[segmentNumber].pageTable->FIFO[FramesAmount - 1] = pageNumber;
return slot;
}
//Updates values of Page Table FIFO
void updatePageTableFIFO(int segmentNumber, int pageNumber) {
for (int i = 0; i < FramesAmount - 1; i++) {
_descriptorTable[segmentNumber].pageTable->FIFO[i] = _descriptorTable[segmentNumber].pageTable->FIFO[i + 1];
}
//Puts the most recent page as the last of queue
_descriptorTable[segmentNumber].pageTable->FIFO[FramesAmount - 1] = pageNumber;
}
// Find Available Frame on memory
int findAvailableFrameOnMemory(int segmentNumber, int pageNumber)
{
int memIndex = segmentNumber*FramesAmount;
for (int i = memIndex; i < memIndex + FramesAmount; i++) {
if (_memory->available[i] == 1) {
_memory->available[i] = 0;
updatePageTableFIFO(segmentNumber, pageNumber);
//There is available memory
return i;
}
}
//There is not available memory
return -1;
}
// Find Segmentation Slot on Memory
int findSegmentationSlotOnMemory(int segmentNumber)
{
// Memory Segmentation Slot
int segmentationSlot = segmentNumber%SegmentsAmount;
// Segmentation slot already allocated by requested segmentNumber
if (_memory->availableSegmentation[segmentationSlot] == segmentNumber) {
// Does nothing
}
// Segmentation slot available on memory for requested segmentNumber
else if (_memory->availableSegmentation[segmentationSlot] == -1) {
// Segmentation Fault
_statistics->SegmentationFaultsCounter++;
_memory->availableSegmentation[segmentationSlot] = segmentNumber;
}
// Segmentation slot already used by ANOTHER segmentNumber
else {
// Overwrite previous Segmentation allocated on memory
// NOT IMPLEMENTED IN THIS PROJECT
printf("DEATH");
// Segmentation Fault
_statistics->SegmentationFaultsCounter++;
_memory->availableSegmentation[segmentationSlot] = segmentNumber;
}
// Return Available Segmentation Slot on memory
return segmentationSlot;
}
// Find Frame on memory
int findFrameOnMemory(int segmentNumber, int pageNumber)
{
int segmentationSlot = findSegmentationSlotOnMemory(segmentNumber);
int chosenFrame = findAvailableFrameOnMemory(segmentationSlot, pageNumber);
if (chosenFrame == -1) {
chosenFrame = findOldestFrameOnMemory(segmentationSlot, pageNumber);
}
return chosenFrame;
}
/**
* Managing Backing Store methods
*/
// Manage Backing_Store
void getBackingStorePage(int segmentNumber, int pageNumber, int frameNumber)
{
_statistics->PageFaultsCounter++;
fseek(backingStore, pageNumber * FrameBytesSize, SEEK_SET);
fread(_memory->frame[frameNumber].PageContent, FrameBytesSize, 1, backingStore);
}
/**
* Debug application methods
*/
// Debug TLB
void debugTLB()
{
printf("nTLBf[");
for (int i = 0; i < TLBEntriesAmount; i++)
printf("%d-%3d ", _TLB->segmentNumber[i], _TLB->frameNumber[i]);
printf("]n");
}
// Debug Page Address
void debugPageAddress(int address, int segmentNumber, int pageNumber, int offset)
{
printf ("Virtual Address: %5d ", address);
printf ("SegmentNumber : %d ", segmentNumber);
printf ("PageNumber : %3d ", pageNumber);
printf ("PageOffset : %3d", offset);
getchar();
}
// Debug Real Address
void debugFrameAddress(int address, int segmentNumber, int frameNumber, int offset)
{
printf (" Real Address: %5d ", address);
printf ("SegmentNumber : %d ", segmentNumber);
printf ("FrameNumber: %3d ", frameNumber);
printf ("FrameOffset: %3d", offset);
getchar();
}
/**
* Find Frame Number
*/
int findFrameNumber(int segmentNumber, int pageNumber)
{
// Find frameNumber on TLB
int frameNumber = findPageOnTLB(segmentNumber, pageNumber);
// If TLB find fails
if (frameNumber == -1)
{
// Find frameNumber on Page Table
frameNumber = findPageOnPageTable(segmentNumber, pageNumber);
// If Page Fault
if (frameNumber == -1)
{
// Load on memory entire page of BACKING_STORE
frameNumber = findFrameOnMemory(segmentNumber, pageNumber);
getBackingStorePage(segmentNumber, pageNumber, frameNumber);
// Set up accessed page on Page Table
setPageOnPageTable(segmentNumber, pageNumber, frameNumber);
}
// Set up accessed page on TLB
setPageOnTLB(segmentNumber, pageNumber, frameNumber);
}
return frameNumber;
}
/**
* Main Memory Manager
*/
int main(int arc, char** argv)
{
if (arc == 1) initialize(inputfile_default);
else initialize(argv[1]);
while (fgets(line , MaxStringLength, addresses))
{
// Read new virtual Address
int virtualAddress = atoi(line);
int segmentNumber = (virtualAddress / PageBits / OffsetBits) & (SegmentBits - 1);
int pageNumber = (virtualAddress / OffsetBits) & (PageBits - 1);
int offset = virtualAddress & (OffsetBits - 1);
// Segmentation Number consideration (only 4 segmentations)
segmentNumber = _statistics->TranslatedAddressesCounter % SegmentsAmount;
// Find frameNumber
int frameNumber = findFrameNumber(segmentNumber, pageNumber);
// Parse real Address
int frameIndex = frameNumber - segmentNumber*FramesAmount;
int value = _memory->frame[frameNumber].PageContent[offset];
int realAddress = frameIndex*PagesAmount + offset;
writeOut(segmentNumber, virtualAddress, realAddress, value);
// Debugging PageAddress and FrameAddress
//debugPageAddress(virtualAddress, segmentNumber, pageNumber, offset);
//debugFrameAddress(realAddress, segmentNumber, frameNumber, offset);
}
finalize();
return 0;
}