-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagingAllocator.cpp
More file actions
276 lines (230 loc) · 9.66 KB
/
PagingAllocator.cpp
File metadata and controls
276 lines (230 loc) · 9.66 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
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
#include "PagingAllocator.h"
#include "backingStore.h"
#include "VMstat.h"
#include <fstream>
#include <iostream>
#include <sstream>
// initialize allocator with max memory size and frame size
PagingAllocator::PagingAllocator(size_t maxMemorySize, size_t frameSize)
: maxMemorySize(maxMemorySize), frameSize(frameSize) {
numFrames = maxMemorySize / frameSize; // calculate number of frames
for (size_t i = 0; i < numFrames; ++i) {
freeFrameList.push_back(i); // initialize free frame list
}
physicalMemory.resize(maxMemorySize, 0);
// std::cout << "Free Frame List: ";
// for (const auto& frame : freeFrameList) {
// std::cout << frame << " ";
// }
std::cout << std::endl;
std::cout << "numFrames: " << numFrames << std::endl;
}
void* PagingAllocator::allocate(Process *process) {
size_t pagesNeeded = process->getNumPages(); //pages needed ni process to run
//Process already instatiated with num pages it needs
int pid = process->getPID(); //get process id
for (size_t pageNum = 0; pageNum < pagesNeeded; ++pageNum) {
handlePageFault(pid, pageNum); // demand paging
}
pidToPages[pid].resize(pagesNeeded); //track numpages allocated to pid
return reinterpret_cast<void*>(process->getPID()+1); // simulate pointer
}
void PagingAllocator::handlePageFault(int pid, size_t pageNumber) {
// Check if page is already in memory
for (const auto& [frame, pair] : pageMap) {
if (pair.first == pid && pair.second == pageNumber) {
return; //page loaded already, return
}
}
// If not, evict a frame
if (freeFrameList.empty()) {
evictPage();
}
//allocate new frame and simulate loading from backing store
size_t frame = allocateFrame(pid, pageNumber);
loadFromBackingStore(pid, pageNumber, -2); //from backing store //TO DO: Sample lang yung -2
pageMap[frame] = {pid, pageNumber}; //track mapping
fifoQueue.push(frame); //add to FIFO queue for replacement
}
//allocate frame from free llist
size_t PagingAllocator::allocateFrame(int pid, size_t pageNumber) {
size_t frame = freeFrameList.back(); //get last frame from free list
freeFrameList.pop_back(); //remove from free list
return frame;
}
//remove oldest page and free frame (FIFO replacement)
void PagingAllocator::evictPage() {
if (fifoQueue.empty()) return;
size_t frameToEvict = fifoQueue.front(); //get oldest
fifoQueue.pop(); //remove from queue
auto [victimPid, victimPage] = pageMap[frameToEvict]; //get page info
writeToBackingStore(victimPid, victimPage); //write to backing store
pageMap.erase(frameToEvict); //remove from page map
freeFrameList.push_back(frameToEvict); //mark as free again
}
//TODO: proper format?
void PagingAllocator::writeToBackingStore(int pid, size_t pageNumber) {
//format: PID | Page # | command counter | cpu core assigned (-1 = not in ram/cpu)
std::lock_guard <std::mutex> lock(backingStoreMutex); //lock for thread safety
std::ifstream backingFile("csopesy-backing-store.txt");
std::vector<std::string> lines; //store backingstore information
std::string pageLine;
int linePid;
size_t linePageNum;
int lineCore;
try{
if (backingFile.is_open()) {
while (std::getline(backingFile, pageLine)){
lines.push_back(pageLine); //store each line to mem
}
backingFile.close(); //close file after reading
}
//locate line of interested page
bool found = false;
for (int i = 0; i < lines.size(); i++){
std::istringstream iss(lines[i]);
if (iss >> linePid >> linePageNum >> lineCore) { //read PID, page number, and core
if (linePid == pid && linePageNum == pageNumber) {
found = true; //found the page to update
lines[i] = std::to_string(linePid) + " " + std::to_string(linePageNum) + " -1"; //update core status
break;
}
}
}
//overwrite backing store with updated lines
if (found) {
std::ofstream out("csopesy-backing-store.txt", std::ios::trunc); //overwrite
for (const auto& line : lines) {
out << line << "\n"; //write each line back
}
out.close(); //close file after writing
//update vmstat
std::lock_guard<std::mutex> vmLock(vmstatMutex);
vmstats.numPagedOut++; //increment page out count
} else {
std::cerr << "Page not found in backing store: PID " << pid << ", Page " << pageNumber << std::endl;
}
}catch(const std::exception& error){
std::cerr << "Error reading backing file: " << error.what() << std::endl;
return;
}
}
void PagingAllocator::loadFromBackingStore(int pid, size_t pageNumber, int coreNum) {
//format: PID | Page # | command counter | cpu core assigned (-1 = not in ram/cpu)
//can refactor to just have one function for updating backing store?
//just include cpu core to be assigned with in bs + option if write / load
std::lock_guard <std::mutex> lock(backingStoreMutex); //lock for thread safety
std::ifstream backingFile("csopesy-backing-store.txt");
std::vector<std::string> lines; //store backingstore information
std::string pageLine;
int linePid;
size_t linePageNum;
int lineCore;
try{
if (backingFile.is_open()) {
while (std::getline(backingFile, pageLine)){
lines.push_back(pageLine); //store each line to mem
}
backingFile.close(); //close file after reading
}
//locate line of interested page
bool found = false;
for (int i = 0; i < lines.size(); i++){
std::istringstream iss(lines[i]);
if (iss >> linePid >> linePageNum >> lineCore) { //read PID, page number, and core
if (linePid == pid && linePageNum == pageNumber) {
found = true; //found the page to update
lines[i] = std::to_string(linePid) + " " + std::to_string(linePageNum) + " " + std::to_string(coreNum); //update core status
break;
}
}
}
//overwrite backing store with updated lines
if (found) {
std::ofstream out("csopesy-backing-store.txt", std::ios::trunc); //overwrite
for (const auto& line : lines) {
out << line << "\n"; //write each line back
}
out.close(); //close file after writing
std::lock_guard<std::mutex> vmLock(vmstatMutex);
vmstats.numPagedIn++; //increment page out count
} else {
std::cerr << "Page not found in backing store: PID " << pid << ", Page " << pageNumber << std::endl;
}
}catch(const std::exception& error){
std::cerr << "Error reading backing file: " << error.what() << std::endl;
return;
}
}
void PagingAllocator::deallocate(int pid) {
std::vector<size_t> toFree;
//find frames used by process
for (const auto& [frame, info] : pageMap) {
if (info.first == pid) {
toFree.push_back(frame); //mark frame for freeing
writeToBackingStore(pid, info.second); //write to backing store
}
}
//remove frames from pageMap and add to free list
for (size_t frame : toFree) {
pageMap.erase(frame);
freeFrameList.push_back(frame);
}
pidToPages.erase(pid); //remove page tracking from this process
}
std::string PagingAllocator::visualizeMemory() const {
std::string output = "Paging Memory Visualization:\n";
for (size_t i = 0; i < numFrames; ++i) {
auto it = pageMap.find(i);
if (it != pageMap.end()) {
output += "Frame " + std::to_string(i) + " -> PID " +
std::to_string(it->second.first) + " PAGE " +
std::to_string(it->second.second) + "\n";
} else {
output += "Frame " + std::to_string(i) + " -> Free\n";
}
}
return output;
}
//TODO:
void PagingAllocator::snapshot(int quantum) const {
}
std::unordered_map<int, std::vector<size_t>> PagingAllocator::getPidOffsets() {
std::lock_guard<std::mutex> lock(allocatorMutex);
return pidToPages;
}
std::vector<size_t> PagingAllocator::getFreeFrameList() {
return freeFrameList; // Return a copy of the free frame list
}
std::unordered_map<size_t, std::pair<int, size_t>> PagingAllocator::getPageMap() {
//add mutex
std::lock_guard<std::mutex> lock(allocatorMutex); // Ensure thread safety
return pageMap; // Return a copy of the page map
}
bool PagingAllocator::checkProcessFrames(Process *process){
std::lock_guard<std::mutex> lock(allocatorMutex);
size_t pagesNeeded = process->getNumPages();
size_t pagesInFrame = 0;
for (const auto& [frameNum, pair] : pageMap) {
if (pair.first == process->getPID()){
pagesInFrame++;
}
}
return pagesInFrame >= pagesNeeded;
}
uint16_t PagingAllocator::readUint16(uint32_t address) const {
if (address + 1 >= physicalMemory.size()) return 0;
return (static_cast<uint16_t>(physicalMemory[address]) |
(static_cast<uint16_t>(physicalMemory[address + 1]) << 8));
}
void PagingAllocator::writeUint16(uint32_t address, uint16_t value) {
if (address + 1 >= physicalMemory.size()) return;
physicalMemory[address] = value & 0xFF;
physicalMemory[address + 1] = (value >> 8) & 0xFF;
}
size_t PagingAllocator::getMaxMemorySize() const {
return maxMemorySize;
}
const std::vector<uint8_t>& PagingAllocator::getPhysicalMemory() const {
return physicalMemory;
}