-
Notifications
You must be signed in to change notification settings - Fork 3
/
bsd_allocator.cpp
292 lines (239 loc) · 8.59 KB
/
bsd_allocator.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
291
#include<iostream>
#include<vector>
#include<cstdlib>
#include "random"
#include "math.h"
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
// Sets page size in KB
static const int page_size = 4;
// Ram size in GB
static const int ram_size = 16;
// Number of frames
static const uint64_t nPages = ((ram_size * pow(2, 30))/(4 * pow(2,10)));
// Size after which to move nodes from inactive queue to free queue
static const uint64_t moveSize = (nPages* 0.05);
// List which stores pages in UVM
struct node {
uint64_t page_address;
struct node* next;
node() {
next = NULL;
}
};
// Class which implements Queue
// TODO: Refactor to separate active, inactive, free queue
class Queue {
private:
struct node* head;
struct node* tail;
public:
uint64_t size;
Queue() {
this->head = NULL;
this->tail = NULL;
this->size = 0;
}
// Is empty
bool isempty(){
if(this->head == this->tail && (this->head == NULL))
return true;
return false;
}
// Add elements to queue, given a node
void enqueue(uint64_t tmp) {
struct node * tmpNode = new struct node();
tmpNode->page_address = tmp;
// Check for empty queue
if(this->tail != NULL){
(this->tail)->next = tmpNode;
}
this->tail = tmpNode;
// Check for empty queue condition
if(this->head == NULL) {
this->head = this->tail;
}
// Increment size
this->size++;
}
// Remove element from queue
uint64_t dequeue() {
// Check for empty queue condition
if(this->head == NULL){
std::cout<<"\nAttempt to dequeue an empty queue";
exit(EXIT_FAILURE);
}
struct node * tmp = NULL;
if(this->head == this->tail){
tmp = this->head;
this->head = NULL;
this->tail= NULL;
}
else{
tmp = this->head;
this->head = (this->head)->next;
}
// Decrement size
this->size--;
// Return deleted element from queue
if(tmp != NULL){
uint64_t tp = tmp->page_address;
free(tmp);
return tp;
}
else{
std::cout<<"Page element dequeued is NULL";
return 0;
}
}
};
// Class for BSD memory allocation queues
class BSDStructure {
private:
Queue activeQueue, inactiveQueue, freeQueue;
public:
uint64_t freeSize(){
return freeQueue.size;
}
uint64_t activeSize(){
return activeQueue.size;
}
uint64_t inactiveSize(){
return inactiveQueue.size;
}
// Active queue operation
void addActive(const int n);
void removeActive(const int n);
// Free queue operations
void addFree(std::vector<uint64_t> pageLocation);
std::vector<uint64_t> removeFree(const int n);
// Inactive queue operations
void addInactive(std::vector<uint64_t> pageLocation);
std::vector<uint64_t> removeInactive(const int n);
};
// Takes page from free list and adds to active list
void BSDStructure::addActive(const int n) {
// Check for free memory available
if(n > this->freeQueue.size){
std::cout<<"Not enough memory in freeMemory"<<"\n n :"<<n<<"\n freeQ size :"<<this->freeQueue.size;
exit(EXIT_FAILURE);
}
for(int i=0;i<n;i++){
//std::cout<<"\ni: "<<i;
this->activeQueue.enqueue(this->freeQueue.dequeue());
}
/* if(this->freeQueue.size < moveSize){*/
//// Check if inactive list has nodes
//if(inactiveQueue.size > 2){
////std::cout<<"\nReallocating from Inactive list to Free list";
//// Leave one page in inactive list just in case
//addFree(removeInactive(this->inactiveQueue.size - 1));
//}
//else{
//std::cout<<"\nNo free memory in free queue";
//}
/*}*/
}
// Removes page from active list and adds to inactive list
void BSDStructure::removeActive(const int n){
for(int i=0;i<n;i++){
this->freeQueue.enqueue(this->activeQueue.dequeue());
}
}
// Takes page and adds to inactive queue
void BSDStructure::addInactive(std::vector<uint64_t> pageLocation){
for(int i=0;i<pageLocation.size();i++){
this->inactiveQueue.enqueue(pageLocation[i]);
}
}
// Removes page from inactive list
std::vector<uint64_t> BSDStructure::removeInactive(const int n){
std::vector<uint64_t> tp(n);
for(int i=0;i<n;i++){
tp[i] = this->inactiveQueue.dequeue();
}
return tp;
}
// Takes page and adds to free list
void BSDStructure::addFree(std::vector<uint64_t> pageLocation){
for(int i=0;i<pageLocation.size();i++){
this->freeQueue.enqueue(pageLocation[i]);
}
}
// Removes page from free list
std::vector<uint64_t> BSDStructure::removeFree(const int n){
std::vector<uint64_t> tp(n);
for(int i=0;i<n;i++){
tp[i] = this->freeQueue.dequeue();
}
return tp;
}
// Avgs a double vector
double avg(std::vector<double> d){
double sum = 0;
for(int i=0; i<d.size();i++){
sum += d[i];
}
if(d.size() > 0)
return sum/(float)d.size();
return 0;
}
int main() {
std::vector<double> initTime, free1Time, free2Time, allocate1Time, allocate2Time;
//std::vector<uint64_t> memory(nPages);
for(int outerI=0;outerI<1;outerI++){
// Declare mockup of ram memory
BSDStructure B;
ptime initStart, initStop, allocate1Start, allocate1Stop, allocate2Start, allocate2Stop, free1Start, free1Stop, free2Start, free2Stop;
// init free list
std::vector<uint64_t> tp;
for(uint64_t i =0; i<nPages; i++){
tp.push_back(i);
}
initStart = microsec_clock::universal_time();
B.addFree(tp);
initStop = microsec_clock::universal_time();
//std::cout<<"Free Queue size: "<<B.freeSize()<<"\n nPages: "<<nPages;
allocate1Start = microsec_clock::universal_time();
// Allocate 3/4th memory in random size amounts
while(B.activeSize() < 0.75*nPages){
const int step = 1;
//std::cout<<"\nStep :"<<step;
B.addActive(step);
}
allocate1Stop = microsec_clock::universal_time();
free1Start = microsec_clock::universal_time();
// Free down to 1/2 of memory
B.removeActive(nPages/4);
free1Stop = microsec_clock::universal_time();
allocate2Start = microsec_clock::universal_time();
// Randomly allocate again upto 3/4 from 1/2
while(B.activeSize() < 0.75*nPages){
const int step = 1;
//std::cout<<"\nStep :"<<step;
B.addActive(step);
}
allocate2Stop = microsec_clock::universal_time();
free2Start = microsec_clock::universal_time();
// Free all memory
B.removeActive(B.activeSize());
free2Stop = microsec_clock::universal_time();
boost::posix_time::time_duration initTimeD = -(initStart-initStop);
boost::posix_time::time_duration allocate1TimeD = -(allocate1Start - allocate1Stop);
boost::posix_time::time_duration allocate2TimeD = -(allocate2Start - allocate2Stop);
boost::posix_time::time_duration free1TimeD = -(free1Start - free1Stop);
boost::posix_time::time_duration free2TimeD = -(free2Start - free2Stop);
initTime.push_back(initTimeD.total_nanoseconds());
allocate1Time.push_back(allocate1TimeD.total_nanoseconds());
allocate2Time.push_back(allocate2TimeD.total_nanoseconds());
free1Time.push_back(free1TimeD.total_nanoseconds());
free2Time.push_back(free2TimeD.total_nanoseconds());
}
std::cout<<"\nTime Taken - BSD (ns)";
std::cout<<"\n---------------------";
std::cout<<"\nInit time: "<<avg(initTime);
std::cout<<"\nAvg time for allocating a single frame while allocating 3/4th memory: "<<avg(allocate1Time)/(0.75*nPages);
std::cout<<"\nAvg time for allocating a single frame while alllocating from 1/2 memory to 3/4th memory: "<<avg(allocate2Time)/(0.25*nPages);
std::cout<<"\nFreeing memory from 3/4 memory to 1/2: "<<avg(free1Time)/(nPages/4);
std::cout<<"\nFreeing memory from 3/4 memory to empty: "<<avg(free2Time)/(nPages*0.75)<<std::endl;
}