-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprsim.cpp
194 lines (175 loc) · 5.98 KB
/
prsim.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <time.h>
// Data structure to store page numbers as a long array
class Array {
public:
// Set maxSize based on num-pages and initialize local data
Array(int maxSize) {
this->maxSize = maxSize;
curSize = 0;
index = 0;
items = new long[maxSize];
}
// Check to see if the array is full
bool isFull() {
return curSize == maxSize;
}
// Linearly search for a specified value in the array
bool search(long value) {
for (int i = 0; i < curSize; i++) {
if (items[i] == value) {
return true;
}
}
return false;
}
// Add the new item to array and remove the oldest item if full
void addfifo(long newItem) {
items[index] = newItem;
index++;
curSize++;
if (curSize > maxSize) {
curSize--;
}
index %= maxSize;
}
// If full, randomly pick an index to replace
void addrandom(long newItem) {
if (isFull()) {
int idx = rand() % maxSize;
this->replace(newItem, idx);
} else {
items[curSize] = newItem;
curSize++;
}
}
// Put an item into a specified index in the array
void replace(long replaceItem, int idx) {
items[idx] = replaceItem;
}
int maxSize; // Maximum array size
int curSize; // Current size of indeces used
int index; // Used for FIFO insertion
long *items; // Long pointer to hold page numbers
};
// Does page replacement on a random index in the array
unsigned int rrandom(int n) {
Array *list = new Array(n);
unsigned int num_faults = 0;
char a;
long mem_address;
int size;
// Scan for the trace instructions from traces
while (scanf("%c %lx, %d\n", &a, &mem_address, &size) > 0) {
// Check for junk instructions and skip them
if (a == '=') {
scanf("%*s[\n]");
continue;
}
// Extract the page number from a 64-bit address
long page_num = mem_address >> 12;
// If the page isn't in the array, remove the first in and increase page faults
if (list->search(page_num) == false) {
list->addrandom(page_num);
num_faults++;
}
// Check the next instruction to see if it's on the same page as before
// and skip if they're the same page
mem_address = mem_address + size - 1;
if (mem_address >> 12 != page_num) {
page_num = mem_address >> 12;
if (list->search(page_num) == false) {
list->addrandom(page_num);
num_faults++;
}
}
}
delete list;
return num_faults;
}
// Does page replacement with a First In First Out algorithm
unsigned int fifo(int n) {
Array *list = new Array(n);
unsigned int num_faults = 0;
char a;
long mem_address;
int size;
// Scan for the trace instructions from traces
while (scanf("%c %lx, %d\n", &a, &mem_address, &size) > 0) {
// Check for junk instructions and skip them
if (a == '=') {
scanf("%*s[\n]");
continue;
}
// Extract the page number from a 64-bit address
long page_num = mem_address >> 12;
// If the page isn't in the array, remove the first in and increase page faults
if (list->search(page_num) == false) {
list->addfifo(page_num);
num_faults++;
}
// Check the next instruction to see if it's on the same page as before
// and skip if they're the same page
mem_address = mem_address + size - 1;
if (mem_address >> 12 != page_num) {
page_num = mem_address >> 12;
if (list->search(page_num) == false) {
list->addfifo(page_num);
num_faults++;
}
}
}
delete list;
return num_faults;
}
// Runs a page replacement simulator that does memory page replacement
// with a specified number of pages to use and a specified algorithm of replacing pages
int main(int argc, char **argv) {
// Seed randomness
srand(time(NULL));
// Struct for allowing --num-pages and --policy arguments
static struct option long_options[] = {
{"num-pages", required_argument, 0, 'n'},
{"policy", required_argument, 0, 'p'},
{0, 0, 0, 0}
};
int n; // Store num-pages
char *p; // Store policy
int option_index = 0; // Index through long_options
int c; // Store current argument option
// Parse command line for number of pages and policy
while ((c = getopt_long(argc, argv, "n:p:", long_options, &option_index)) != -1) {
switch (c) {
case 'n':
// Read in num-pages
n = atoi(optarg);
break;
case 'p':
// Read in the desired page replacement policy
p = optarg;
break;
default:
printf("Default\n");
break;
};
}
// Unsigned int for holding up to 3 billion as a number
unsigned int num_faults;
// Captitalize 'random' to match output with 'prsim-good'
char *tmp;
if (strcmp(p, "random") == 0) {
num_faults = rrandom(n);
tmp = (char *) "Random";
} else if (strcmp(p, "FIFO") == 0) {
num_faults = fifo(n);
tmp = (char *) "FIFO";
} else {
printf("Error: choose another policy\n");
}
// Print results
printf("%s %d: %u\n", tmp, n, num_faults);
return 0;
}