-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
145 lines (131 loc) · 4.42 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "log.h"
#define MAX_ADDRESSES 10000
#define TLB_SIZE 16
#define MAX_FRAME 256
#define ADDRESS_MASK 65535
#define SPRAIN_MASK 255
#define CHUNK 256
void getPage(int address);
void readBinary(char *inputFile, char const *argv);
void insertTLB(int pageNumber, int frameNumber);
void readStore(int pageNumber);
FILE *backingStore;
FILE *addressFile;
int pageFaults = 0, virtualAdress;
int physicalMemory[MAX_FRAME][MAX_FRAME];
int pageNumberTLB[TLB_SIZE];
int frameNumberTLB[TLB_SIZE];
int pageNumbersTable[MAX_FRAME];
int framesTable[MAX_FRAME];
int numOfTLB = 0, hits = 0, firstFrame = 0, firstPage = 0;
char logicalAddress[10];
signed char memoryByteValue;
signed char buffer[CHUNK];
int main(int argc, char const *argv[]) {
if(argc != 2) {
log_error("Error: o arquivo addresses.txt deve ser passado como parametro\n", errno);
log_error("Error open file: %s\n", strerror(errno));
exit(1);
}
readBinary("BACKING_STORE.bin", argv[1]);
printf("Page Faults: %d\n", pageFaults);
printf("Page Faults Percentual: %.2f%%\n", (double) pageFaults / (double)MAX_ADDRESSES * 100.0);
printf("TLB Hits: %d\n", hits);
printf("TLB Hit Percentual = %.2f%%\n", (double) hits / (double)MAX_ADDRESSES * 100.0);
fclose(addressFile);
fclose(backingStore);
return 0;
}
void readBinary(char *inputFile, char const *argv) {
backingStore = fopen(inputFile, "rb");
addressFile = fopen(argv, "r");
if(addressFile == NULL) {
log_error("Error no %d in opening file...\n", errno);
log_error("Error open file: %s\n", strerror(errno));
exit(1);
}
for(int i = 0; i < MAX_ADDRESSES; ++i) {
fscanf(addressFile, "%d", &virtualAdress);
getPage(virtualAdress);
}
}
void getPage(int virtualAdress) {
int sprain = virtualAdress & SPRAIN_MASK, pageNumber = (virtualAdress & ADDRESS_MASK) >> 8, frame = -1;
for(int i = 0; i < TLB_SIZE; ++i){
if(pageNumberTLB[i] == pageNumber){
frame = frameNumberTLB[i];
hits++;
}
}
if(frame == -1){
for(int i = 0; i < firstPage; ++i){
if(pageNumbersTable[i] == pageNumber){
frame = framesTable[i];
}
}
if(frame == -1){
readStore(pageNumber);
pageFaults++;
frame = firstFrame - 1;
}
}
insertTLB(pageNumber, frame);
memoryByteValue = physicalMemory[frame][sprain];
log_debug("Logical Address: %d\nPage Number: %d\nSprain: %d\n", virtualAdress, pageNumber, sprain);
log_debug("Frame Number: %d\nPhysical Address: %d\nByte Value: %d\n", frame, (frame << 8) | sprain, memoryByteValue);
}
void insertTLB(int pageNumber, int frameNumber) {
int alreadyInsert = 0, i;
for(i = 0; i < numOfTLB; ++i) {
if(pageNumberTLB[i] == pageNumber) {
alreadyInsert = 1;
break;
}
}
if(alreadyInsert == 1) return;
if(i == numOfTLB) {
if(numOfTLB < TLB_SIZE) {
pageNumberTLB[numOfTLB] = pageNumber;
frameNumberTLB[numOfTLB] = frameNumber;
} else {
for(i = 0; i < TLB_SIZE - 1; ++i) {
pageNumberTLB[i] = pageNumberTLB[i + 1];
frameNumberTLB[i] = frameNumberTLB[i + 1];
}
pageNumberTLB[numOfTLB - 1] = pageNumber;
frameNumberTLB[numOfTLB - 1] = frameNumber;
}
} else {
for(i = i; i < numOfTLB - 1; ++i) {
pageNumberTLB[i] = pageNumberTLB[i + 1];
frameNumberTLB[i] = frameNumberTLB[i + 1];
}
if(numOfTLB < TLB_SIZE) {
pageNumberTLB[numOfTLB] = pageNumber;
frameNumberTLB[numOfTLB] = frameNumber;
} else {
pageNumberTLB[numOfTLB - 1] = pageNumber;
frameNumberTLB[numOfTLB - 1] = frameNumber;
}
}
if(numOfTLB < TLB_SIZE) numOfTLB++;
}
void readStore(int pageNumber){
if(fseek(backingStore, pageNumber * CHUNK, SEEK_SET) != 0) {
log_error("Error seeking in backing store\n");
}
if(fread(buffer, sizeof(signed char), CHUNK, backingStore) == 0) {
log_error("Error reading from backing store\n");
}
for(int i = 0; i < CHUNK; ++i){
physicalMemory[firstFrame][i] = buffer[i];
}
pageNumbersTable[firstPage] = pageNumber;
framesTable[firstPage] = firstFrame;
firstFrame++;
firstPage++;
}