-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemorymanager.cpp
More file actions
243 lines (207 loc) · 7.1 KB
/
memorymanager.cpp
File metadata and controls
243 lines (207 loc) · 7.1 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
#include "memorymanager.h"
#include <sstream>
#include <iterator>
Logger logger;
// Make outputting to stdout or file easier
std::ostream& operator<<(std::ostream& os, const Page& p) {
os << p.variable.varid << " " <<
p.variable.val << " " <<
p.variable.lastAccess << " " <<
p.free;
return os;
}
Page::Page(std::string vid, unsigned int v, unsigned int t, bool f) {
this->variable.varid = vid;
this->variable.val = v;
this->variable.lastAccess = t;
this->free = f;
}
MemoryManager::MemoryManager(unsigned int MEMSIZE, std::string fname) : disk(fname), MEMSIZE(MEMSIZE) {}
// return the index in memory of the variable with id `varid`
// Index is continuous so if the index returned is greater than MEMSIZE
// then it resides on the disk and its adress on the disk is address_returned - MEMSIZE
int MemoryManager::getAddress(std::string varid) {
std::shared_lock lock(pt_lk); // Multiple threads may read at the same time
auto it = this->pageTable.find(varid);
if (it == this->pageTable.end()) {
return -1;
}
return it->second;
}
// UpdateTable will take a varid and its address and add it to the table.
// In the case that addr is less than 0, the varid will be deleted from the table
void MemoryManager::updateTable(std::string varid, int addr) {
std::unique_lock lock(pt_lk); // Only one writer can write to table at a time
if (addr < 0) {
auto it = pageTable.find(varid);
if (it != pageTable.end()) {
pageTable.erase(it);
return;
}
}
pageTable[varid] = addr;
}
/*** API Function - Allow Threads to Store/Overwrite Variables Safely ***/
void MemoryManager::Store(std::string varid, unsigned int v, unsigned int currtime) {
auto addr = getAddress(varid);
// Check that Variable doesn't exist in memory, so we create it in the first empty page we find
if (addr < 0) {
if (writeToMemory(varid, v, currtime)) {
return;
}
// Memory is full, store it on disk
addToDisk(varid, v, currtime);
return;
}
// Determine if the var is located in Main memory or on disk
if (addr < MEMSIZE) {
modifyMemory(addr, v, currtime);
return;
}
modifyDisk(addr - MEMSIZE, v, currtime);
}
/*** API Function - Allow Threads to Delete Variables Safely ***/
// Rather than overwriting the values with empty values, this function
// will simply set the `free` variable of the page to True and update the page Table
void MemoryManager::Release(std::string varid, unsigned int currtime) {
auto addr = getAddress(varid);
if (addr < 0) {
return;
}
// Determine if the var is located in Memory or on the Disk
if (addr < MEMSIZE) {
std::scoped_lock lock(mm_lk);
mainMemory[addr].free = true;
updateTable(varid, -1);
return;
}
// Free Disk Space
std::scoped_lock lock(d_lk);
auto data = readFromDisk();
data[addr - MEMSIZE].free = true;
writeToDisk(data);
updateTable(varid, -1);
}
/*** API Function - Allow Threads to Retrieve the value of a variable stored in memory safely ***/
int MemoryManager::Lookup(std::string varid, unsigned int currtime) {
auto addr = getAddress(varid);
if (addr < 0) {
return -1;
}
if (addr < MEMSIZE) {
std::scoped_lock lock(mm_lk);
auto val = mainMemory[addr].variable.val;
return val;
}
// Find smallest access time in main memory
/* Scoped_lock will acquire both locks and ensure deadlock doesn't occur */
std::scoped_lock lock(mm_lk, d_lk);
unsigned int smallest = -1; // uints are modulo so -1 will set it to UINT_MAX
auto index = 0;
auto it = 0;
for (const auto &p : mainMemory) {
if (p.variable.lastAccess < smallest) {
smallest = p.variable.lastAccess;
index = it;
}
it++;
}
auto data = readFromDisk();
Page swap_reg = mainMemory[index];
mainMemory[index] = data[addr - MEMSIZE];
data[addr - MEMSIZE] = swap_reg;
writeToDisk(data);
auto val = mainMemory[index].variable.val;
// Update page table to reflect change
updateTable(swap_reg.variable.varid, addr);
updateTable(mainMemory[index].variable.varid, addr - MEMSIZE);
Logger::memManagerSwapLog(currtime, "SWAP", swap_reg.variable.varid, mainMemory[index].variable.varid);
return val;
}
// modifyMemory will overwrite the value of the variable at position `addr` in Main Memory
// with param `v` as well as its last access time
void MemoryManager::modifyMemory(unsigned int addr, unsigned int v, unsigned int currtime) {
std::scoped_lock lock(mm_lk);
mainMemory[addr].variable.val = v;
mainMemory[addr].variable.lastAccess = currtime;
}
// Takes a variable and will look for a free page in memory to store it.
//
// If it finds a free slot, it will store the variable, set the `free` bool of the page to false
// and will update the page table
//
// If no free slot exists, it will return false
bool MemoryManager::writeToMemory(std::string varid, unsigned int v, unsigned int currtime) {
std::scoped_lock lock(mm_lk);
auto i = 0;
while(i < mainMemory.size() && i < MEMSIZE) {
if (mainMemory[i].free) {
mainMemory[i] = Page(varid, v, currtime, false);
updateTable(varid, i);
return true;
}
++i;
}
if (i < MEMSIZE) {
mainMemory.push_back(Page(varid, v, currtime, false));
updateTable(varid, i);
return true;
}
return false;
}
// Takes a variable and will store it on the disk. Although we have unlimited disk space,
// we set the variable in the first free space starting from the beginning of disk space.
//
// If we did not do this, the disk file would continue to grow with holes in memory, this
// is essentially external fragmentation which defeats the purpose of using a paging scheme
void MemoryManager::addToDisk(std::string varid, unsigned int v, unsigned int currtime) {
std::scoped_lock lock(d_lk);
auto data = readFromDisk();
auto i = 0;
while(i < data.size() && !data[i].free) {
i++;
}
if (i < data.size()) {
data[i] = Page(varid, v, currtime, false);
} else {
data.push_back(Page(varid, v, currtime, false));
}
// Update Page Table
updateTable(varid, i + MEMSIZE);
// Save modifications to disk
writeToDisk(data);
}
// given a page's address on the disk, modify only its contents
void MemoryManager::modifyDisk(unsigned int addr, unsigned int v, unsigned int currtime) {
std::scoped_lock lock(d_lk);
auto data = readFromDisk();
data[addr].variable.val = v;
data[addr].variable.lastAccess = currtime;
writeToDisk(data);
}
/* Write the data vector onto the disk (vm.txt) */
void MemoryManager::writeToDisk(const std::vector<Page>& data) {
std::scoped_lock lock(f_lk);
std::ofstream f(this->disk, std::ofstream::out | std::ofstream::trunc);
for (const auto &var : data) {
f << var << std::endl;
}
f.close();
}
/* Read the data from the disk (vm.txt) and return the contents as Page vector*/
std::vector<Page> MemoryManager::readFromDisk() {
std::scoped_lock lock(f_lk);
std::ifstream f(this->disk, std::ifstream::in);
std::vector<std::string> tok((std::istream_iterator<std::string>(f)),
std::istream_iterator<std::string>());
std::vector<Page> data;
for (auto i = 0; i < tok.size(); i += 4) {
if (tok[i + 3] == "1") {
data.push_back(Page("", 0, 0, 1)); // Free page in memory
} else {
data.push_back(Page(tok[i], std::stoi(tok[i + 1]), std::stoi(tok[i + 2]), 0));
}
}
f.close();
return data;
}