-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequentlyUsed.cpp
290 lines (263 loc) · 9.99 KB
/
FrequentlyUsed.cpp
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
/* SJSU Fall 2018
* CS149 Operating Systems
* Sijing Xie (013829478), Group 2
* Project 4
*
* Running LFU/MFU page replacement sims.
*/
#include "common.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <forward_list>
#include <pthread.h>
#include <unistd.h>
using namespace std;
ofstream lfu_output;
ofstream mfu_output;
const int num_process = 150;
const int num_free = 100;
const int total_time = 60000; // in ms
const int num_threads = 25; // max # running jobs at any given time
pthread_mutex_t mutex;
pthread_cond_t cond;
int ready = 0;
forward_list<Page> freePages; // linked list of free pages
int size = 0; // to track size of free list
forward_list<Process> jobs; // linked list of jobs to run
forward_list<Process> done; // linked list of completed jobs
void printMap(ofstream &file, int map[]) {
int len = total_time / 1000;
for (int i = 0; i < len; i++) {
if (i > 0 && i % 10 == 0) { file << "\n"; }
if (map[i] == 0) { file << ".\t"; }
else { file << map[i] << "\t"; }
}
file << "\n\n";
}
/* start routine which takes the algorithm type ('L' or 'M') as argument. */
void *start(void *algType) {
int type = (long) algType;
int timestamp = 0;
int mem_map[total_time / 1000] = { 0 };
ready++;
// choosing output file to write
ofstream file;
if (type == 0) {
file.open("lfu.txt", ofstream::app);
}
else if (type == 1) {
file.open("mfu.txt", ofstream::app);
}
while (!jobs.empty() && timestamp < total_time) {
pthread_mutex_lock(&mutex);
// checking all threads ready
if (ready != num_threads) { pthread_cond_wait(&cond, &mutex); }
else { pthread_cond_broadcast(&cond); }
// get next job in queue
Process curr = jobs.front();
jobs.pop_front();
if (timestamp < curr.getArrival()) { timestamp = curr.getArrival(); }
file << setfill('0') << setw(2) << timestamp / 1000 << "s: Process "
<< curr.name << " arrived. Total size " << curr.getSize()
<< "MB. Duration " << curr.getService() / 1000 << "s.\n"
<< "MEMORY MAP:" << endl;
printMap(file, mem_map);
pthread_mutex_unlock(&mutex);
// reference page 0
int i = 0; // tracking page # for locality
Page f;
int pgNum, procNum; // preserve old data for printing
bool flag = false; // check for eviction
if (size >= 4) {
pthread_mutex_lock(&mutex);
f = freePages.front();
freePages.pop_front();
if (f.getProcess() != -1 && f.getPage() != -1) { // if page has old data
procNum = f.getProcess();
f.setProcess(curr.name);
pgNum = f.getPage();
flag = true;
}
f.setPage(i);
curr.memPages.emplace_front(f);
size--;
curr.miss++;
file << setfill('0') << setw(2) << timestamp / 1000
<< "s: Process " << curr.name << " referenced page " << i
<< ". Page not in memory. ";
if (flag) {
file << "Process " << procNum << ", page " << pgNum << " evicted."
<< endl;
} else {
file << "No page evicted." << endl;
}
pthread_mutex_unlock(&mutex);
flag = false; // reset
sleep(0.1);
}
// continue referencing pages, using free list as available
// and alg where applicable
int localTime = timestamp; // preserving start time
while (timestamp < localTime + curr.getService() && timestamp < total_time) {
if (timestamp % 1000 == 0) { mem_map[timestamp / 1000] = curr.name; }
timestamp += 100;
bool ref = false; // if page has been referenced
i = locality(i, curr.getSize()); // get next ref page #
// check if page already loaded
for (auto it = curr.memPages.begin(); it != curr.memPages.end(); ++it) {
if (it->getPage() == i) {
curr.hit++;
it->addCount();
pthread_mutex_lock(&mutex);
file << setfill('0') << setw(2) << timestamp / 1000
<< "s: Process " << curr.name << " referenced page "
<< i << ". Page in memory. No page evicted."
<< endl;
pthread_mutex_unlock(&mutex);
ref = true;
break;
}
}
if (!ref) {
pthread_mutex_lock(&mutex);
if (!freePages.empty()) { // check if free pages available
f = freePages.front();
freePages.pop_front();
size--;
if (f.getProcess() != -1) { // if page has old data
procNum = f.getProcess();
f.setProcess(curr.name);
pgNum = f.getPage();
flag = true;
}
f.setPage(i);
curr.memPages.emplace_front(f);
curr.miss++;
file << setfill('0') << setw(2) << timestamp / 1000
<< "s: Process " << curr.name << " referenced page " << i
<< ". Page not in memory. ";
if (flag) {
file << "Process " << procNum << ", page " << pgNum
<< " evicted." << endl;
} else {
file << "No page evicted." << endl;
}
pthread_mutex_unlock(&mutex);
flag = false; // reset
} else if (!curr.memPages.empty()) { // use alg to replace page
curr.memPages.sort(); // sort in ascending order by counters
if (type == 1) { curr.memPages.reverse(); } // reverse if mfu
Page m = curr.memPages.front();
curr.memPages.pop_front();
pgNum = m.getPage();
procNum = curr.name;
m.setPage(i);
m.resetCount();
curr.memPages.emplace_front(m);
curr.miss++;
pthread_mutex_lock(&mutex);
file << setfill('0') << setw(2) << timestamp / 1000
<< "s: Process " << curr.name << " referenced page " << i
<< ". Page not in memory. Process " << procNum << ", page "
<< pgNum << " evicted." << endl;
pthread_mutex_unlock(&mutex);
} else { // no available free pages or already loaded pages
continue;
}
}
// sleep(100); // stalled threads + returned to main/join, for some reason
}
// print job stats
pthread_mutex_lock(&mutex);
file << setfill('0') << setw(2) << timestamp / 1000 << "s: Process "
<< curr.name << " completed. Total size " << curr.getSize()
<< "MB. Duration " << curr.getService() / 1000 << "s.\n"
<< "MEMORY MAP:" << endl;
printMap(file, mem_map);
// add job to done list
done.emplace_front(curr);
// return pages to free list but don't clear
for (auto it = curr.memPages.begin(); it != curr.memPages.end();) {
f = *it;
++it;
curr.memPages.pop_front();
freePages.emplace_front(f);
size++;
}
pthread_mutex_unlock(&mutex);
}
file.close();
cout << "RIGHT BEFORE CANCEL" << endl;
pthread_cancel(pthread_self());
return NULL;
}
int main() {
srand(time(NULL));
pthread_t threads[num_threads];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
// gen 100 page free list, each 1MB
for (int j = 0; j < num_free; j++) {
Page pg = Page();
freePages.emplace_front(pg);
size++;
}
// gen 150 random processes in job list + names. sort by arrival.
for (int i = 1; i <= num_process; i++) {
Process p = Process();
p.name = i;
jobs.emplace_front(p);
}
jobs.sort();
// comment out lfu section when running mfu
lfu_output.open("lfu.txt", ofstream::app);
lfu_output << "USING LFU.\n\n";
lfu_output.close();
for (int i = 0; i < num_threads; i++) {
int type = 0;
pthread_create(&threads[i], NULL, start, reinterpret_cast<void*>(type));
}
for (int i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
// calc stats for 1 run
int hits = 0, misses = 0, complete = 0;
for (auto it = done.begin(); it != done.end(); ++it) {
Process p = *it;
hits += p.hit;
misses += p.miss;
complete++;
}
lfu_output.open("lfu.txt", ofstream::app);
lfu_output << "STATS:\n\tHITS = " << hits << ", MISSES = " << misses << "\n\n";
lfu_output.close();
// end lfu comment here
// comment out mfu section when running lfu
/*mfu_output.open("mfu.txt", ofstream::app);
mfu_output << "USING MFU.\n\n";
mfu_output.close();
for (int i = 0; i < num_threads; i++) {
int type = 1;
pthread_create(&threads[i], NULL, start, reinterpret_cast<void*>(type));
}
for (int i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
// calc stats for 1 run
int hits = 0, misses = 0, complete = 0;
for (auto it = done.begin(); it != done.end(); ++it) {
Process p = *it;
hits += p.hit;
misses += p.miss;
complete++;
}
mfu_output.open("mfu.txt", ofstream::app);
mfu_output << "STATS:\n\tHITS = " << hits << ", MISSES = " << misses << "\n\n";
mfu_output.close();*/
// end mfu comment here
return 0;
}