-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
348 lines (288 loc) · 8.76 KB
/
Copy pathmain.cpp
File metadata and controls
348 lines (288 loc) · 8.76 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <set>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <fstream>
#include <queue>
#include <cassert>
extern "C" {
#include "genzip.h"
}
#define CACHE_POLICY 0
#define EVICT_POLICY 0
using namespace std;
int find_max_index(int* array,int N,int w,set<int>);
int find_min_index(int* array,int N,int w,set<int>);
int find_min(map<int,int> ma,bool evict_page);
int memory_write=0;
int host_write=0;
// Page numbering and block numbering starts at zero.
//Function that prints the physical disk
void print_disk(int* a,int size,int s)
{
//int k;
for(int i=0;i<size;i++)
{
cout<<"| "<<a[i]<< " |";
}
cout<<endl;
cout<<"Sim Count"<<s;
//cin>>k;
}
double print_writeampl(int host_write,int memory_write)
{
return (double)host_write/memory_write;
}
int find_last_accessed(int *values,set<int> cache_set,int start_index);
int main()
{
ofstream myfile;
myfile.open("Min.txt");
queue<int> free_block_queue;
set<int> free_set;
// static int physical_disk[1024];
// Number of blocks in the total memory
const int no_blocks=32;
// Number of pages in the total memory
const int no_pages=1024;
// Number of pages per block
const int block_size=32;
// Write pointer points the page number that is written tofree_block
// Multiply block_size times block number to get write pointer to start of block. starts are zero
int write_pointer=0;
// free count for all the blocks
int free_block_count=31;
// the number of pages at which the garbage collection is triggered.
int garbage_treshold=5;
//evict flag.. specifies if a page is evicted from cache
bool evict_flag=false;
// currently evicted page
int evict_page;
bool gc_flag=false;
// page that is updated in cache
int page_no;
//cache set holds the cache elements
set<int> cache_set;
//cache count,
map<int,int> cache_count_map;
// Garbage collection flag
bool garbage_collect=false;
static int dirty_count[no_blocks];
map<int,int> ad_map;
map<int,int> block_map;
int simulation_count=0;
int reclaim_block=0;
int num_total_write=0;
double alpha=1;
int* zipf_values=NULL;
zipf_values=NULL;
zipf_values=generate_zipf(10000,alpha,32.0*32*27,434);
for(int i=1;i<32;i++)
{
free_block_queue.push(i);
free_set.insert(i);
}
while(num_total_write<10000)
{
//simulation runs until the user breaks it.
int log_address=zipf_values[simulation_count];
int page_no=log_address/32;
evict_page=page_no;
// IF the page is found in the cache.
if(cache_set.find(page_no)!=cache_set.end())
{
if(cache_count_map.find(page_no)!=cache_count_map.end()){
map<int,int>::iterator it=cache_count_map.find(page_no);
it->second=it->second+1;
evict_flag=false;
}
else{
cache_count_map.insert(pair<int,int>(page_no,1));
evict_flag=false;
}
}
else
{
if(cache_set.size()<32)
{
cache_set.insert(page_no);
cache_count_map.insert(pair<int,int>(page_no,1));
evict_flag=false;
}
else
{
evict_flag=true;
//find the index of the minimum element in the map
evict_page=find_last_accessed(zipf_values,cache_set,simulation_count+1);
//assert(evict_page!=0);
cout<<"Evicted Page "<<evict_page<<endl;
cache_set.erase(evict_page);
cache_set.insert(page_no);
cache_count_map.erase(evict_page);
cache_count_map.insert(pair<int,int>(page_no,1));
// remove the element at that index from count map and also the set.
//do something else
}
}
if(evict_flag==true)
{
evict_flag=false;
if(write_pointer%block_size==0 && write_pointer!=0)
{
free_block_count--;
write_pointer=free_block_queue.front()*block_size;
free_set.erase(free_block_queue.front());
free_block_queue.pop();
//print_disk(physical_disk,no_pages);
//print_disk(dirty_count,block_size,simulation_count);
//cout<<"Write Pointer"<<write_pointer<<endl;
}
if(ad_map.find(evict_page)!=ad_map.end())
{
map<int,int>::iterator it=ad_map.find(evict_page);
int block_address=it->second;
assert(block_map.find(block_address)!=block_map.end());
block_map.erase(block_address);
dirty_count[(block_address)/no_blocks]++;
ad_map.erase(evict_page);
}
memory_write++;
host_write++;
assert(ad_map.size()==block_map.size());
ad_map.insert(pair<int,int>(evict_page,write_pointer));
if(block_map.find(write_pointer)!=block_map.end())
cout<<"Failed here!"<<write_pointer<<endl;
block_map.insert(pair<int,int>(write_pointer,evict_page));
write_pointer++;
if(free_block_queue.size()<5)
{
if(CACHE_POLICY==0)
reclaim_block=find_max_index(dirty_count,no_blocks,write_pointer/no_blocks,free_set);
if(CACHE_POLICY==1)
reclaim_block=find_min_index(dirty_count,no_blocks,write_pointer/no_blocks,free_set);
if(CACHE_POLICY==2)
{
int k=rand()%32;
while(k==(write_pointer/no_blocks) || free_set.find(k)!=free_set.end())
k=rand()%32;
reclaim_block=k;
}
cout<<"reclaim Block"<<reclaim_block<<"Free Block queue"<<free_block_queue.size();
free_block_count++;
dirty_count[reclaim_block]=0;
for(int j=0;j<block_size;j++)
{
if(write_pointer%block_size==0)
{
free_block_count--;
write_pointer=free_block_queue.front()*block_size;
free_set.erase(free_block_queue.front());
free_block_queue.pop();
}
map<int,int>::iterator it=block_map.find((reclaim_block*block_size)+j);
if(it!=block_map.end())
{
int mem_address=it->second;
assert(ad_map.find(it->second)!=ad_map.end());
ad_map.erase(it->second);
block_map.erase(reclaim_block*block_size+j);
ad_map.insert(pair<int,int>(mem_address,write_pointer));
block_map.insert(pair<int,int>(write_pointer,mem_address));
write_pointer++;
host_write++;
}
block_map.erase(reclaim_block*block_size+j);
}
free_block_queue.push(reclaim_block);
free_set.insert(reclaim_block);
}
}
num_total_write++;
simulation_count++;
}
simulation_count=0;
num_total_write=0;
num_total_write=0;
myfile<<alpha<<"\t"<<print_writeampl(host_write,memory_write)<<endl;
host_write=memory_write=0;
alpha=alpha+0.1;
while(!free_block_queue.empty())
free_block_queue.pop();
free_set.clear();
ad_map.clear();
block_map.clear();
myfile.close();
}
int find_max_index(int* array,int N,int w,set<int> free_set)
{
int i,k = N-1;
int max = 0;
for (i = N-1; i >= 0 ; --i)
{
if (array[i] >= max && i!=w && free_set.find(i)==free_set.end())
{
max = (int)array[i];
k = i;
}
}
if(max==0)
cout<<"Garbage Collected a Block with zero dirty Pages";
return k;
}
int find_min_index(int* array,int N,int w,set<int> free_set)
{
int i,k = 0;
int min = 33;
for (i = 0; i < N; ++i)
{
if (array[i] < min && array[i]>1 && i!=w && free_set.find(i)==free_set.end())
{
min = (int)array[i];
k = i;
}
}
return k;
}
int find_last_accessed(int *values,set<int> cache_set,int start_index)
{
set<int> access_list;
int last_index=-1;
while(access_list.size()<32 && start_index<10000)
{
if(cache_set.count(values[start_index]/32)!=0 && access_list.count(values[start_index]/32)==0)
{
access_list.insert(values[start_index]/32);
last_index=start_index;
}
if(access_list.size()==32)
return values[last_index]/32;
start_index++;
}
for(set<int>::iterator it=cache_set.begin();it!=cache_set.end();it++)
if(access_list.count((*it))==0)
{
return *it;
}
}
int find_min(map<int,int> frequencyCount,bool rand_flag)
{
int currentMax = 9999;
int rand_no=rand()%frequencyCount.size();
int arg_max = 0;
int c=0;
for(map<int,int>::iterator it = frequencyCount.begin(); it != frequencyCount.end(); ++it,c++ )
{
if(rand_flag==1 && c==rand_no)
{
return it->first;
}
if(it ->second < currentMax) {
arg_max = it->first;
currentMax = it->second;
}
}
return arg_max;
}