Repository navigation
Expand file tree
/
Copy pathsimulator.cpp
More file actions
186 lines (161 loc) · 6.11 KB
/
Copy pathsimulator.cpp
File metadata and controls
186 lines (161 loc) · 6.11 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
#include <iostream>
#include <cstring>
#include <cmath>
#include <fstream>
#include "hashtable.h"
#include "lru.h"
#include "secondchance.h"
#define PAGE_SIZE 4096
#define MIN_ARGUMENTS 4
#define ALL_REFERENCES 2000000
#define LGC_ADDRESS_SIZE 8 // 8 hex characters
#define SUCCESS 0
#define FAIL -1
using namespace std;
int main(int argc, char** argv)
{
int algorithmType = 0, memoryFrames = 0, q = 0, maxReferences = 0;
int totalWrites = 0, totalReads = 0, pf = 0, totalReferences = 0;
if (argc < MIN_ARGUMENTS) {
cout << "Not enough arguments" << endl;
return FAIL;
}
//
// get command line arguments
//
algorithmType = atoi(argv[1]);
memoryFrames = atoi(argv[2]);
q = atoi(argv[3]);
// if max references not given in command line
// read all references
if (argc == MIN_ARGUMENTS) {
maxReferences = ALL_REFERENCES;
} else {
maxReferences = atoi(argv[4]);
}
//
// initialize main memory's frames to 0
//
unordered_map<int, int> mainMemory; // contains frame # + 1/0 for occupied/empty
for (int i = memoryFrames; i > 0; i--) {
mainMemory.insert(pair<int, int>(i, 0));
}
//
// open files
//
ifstream trace1("bzip.trace", ios::in), trace2("gcc.trace", ios::in);
if (!trace1 || !trace2) {
cout << "No such file";
return FAIL;
}
ifstream* t = &trace1;
//
// variables for cropping offset's bits from logic address
//
const double offsetSize = log2(PAGE_SIZE); // in bits (here 12)
const int croppedBits = offsetSize / 4; // how many hex characters we're going to crop
// each hex character represents 4 bits thus we divide by 4
string lgc(LGC_ADDRESS_SIZE, '\0'); // for logic address
string rw(1, '\0'); // for R/W operation
int pageNum;
std::pair <int,int> frameAndDirty;
frameAndDirty = std::make_pair(-1, 0);
Hashtable ht1(1, memoryFrames);
Hashtable ht2(2, memoryFrames);
Hashtable* currentPT = &ht1;
Hashtable* otherPT = &ht2;
LRU lru(memoryFrames);
SecondChance sc(memoryFrames);
// Steps:
// 1) get logic address and R/W operation from trace
// 2) crop last [croppedBits] bits from logic address
// 3) convert hex characters of logic address to integer -> this is page number
// 4) if page doesn't exist in current page table -> page fault occurs + reads counter increases
// 5) iterate through main memory and check either for an empty frame or frame to replace
// 6) if page doesn't exist in current page table -> insert
// else update page's dirty bit in current page table
// 7) change input file and page table when you have read [q] traces from current file
//
while (totalReferences < maxReferences) {
for (int i = 0; i < q; i++) {
// (1)
getline(*t, lgc, ' ');
getline(*t, rw, '\n');
// (2)
lgc.erase(lgc.end() - croppedBits, lgc.end());
// (3)
pageNum = stoi(lgc, nullptr, 16); // here is page reference
// (4)
bool exist = currentPT->search(pageNum);
if (!exist) {
pf++;
totalReads++; // reads increase whether we have Read or Write operation
}
// (5)
for (auto it = mainMemory.begin(); it != mainMemory.end(); it++) {
int full = std::distance(mainMemory.begin(), it) + 1; // variable for checking number of frames that are full
// found empty frame
if (it->second == 0) {
if (!exist) {
it->second = 1;
frameAndDirty.first = it->first; // get frame #
}
// insert page in replacement container
if (algorithmType == 1) {
lru.insert(pageNum, exist);
} else {
sc.insert(pageNum, exist);
}
break;
}
// memory is full -> find frame to replace
else if (it->second != 0 && full == memoryFrames) {
// call replacement algorithm
if (algorithmType == 1) {
frameAndDirty = lru.replace(pageNum, currentPT, otherPT, exist);
} else {
frameAndDirty = sc.replace(pageNum, currentPT, otherPT, exist);
}
// if dirty bit=1 -> the page that we replaced is modified
if (frameAndDirty.second) {
totalWrites++;
}
break;
}
}
// (6)
if (!exist) {
currentPT->insert(pageNum, frameAndDirty.first, &rw[0]);
} else {
currentPT->updateDirtyBit(pageNum, &rw[0]);
}
totalReferences++;
// when maxReferences isn't multiple of q
// stop loop for traces
if (totalReferences == maxReferences) {
break;
}
}
// (7)
if (memcmp((void*)t, (void*)&trace1, sizeof(trace1)) == 0) {
t = &trace2;
currentPT = &ht2;
otherPT = &ht1;
} else {
t = &trace1;
currentPT = &ht1;
otherPT = &ht2;
}
}
//
// print statistics
//
if (algorithmType == 1) {
cout << "Using LRU";
} else {
cout << "Using Second Chance";
}
cout << "\nTotal disk writes: " << totalWrites << "\nTotal disk reads: " << totalReads << "\nPage Faults: " << pf
<< "\nNumber of references (combined for both proccesses): " << totalReferences << "\nNumber of frames: " << memoryFrames << endl;
return SUCCESS;
}