-
Notifications
You must be signed in to change notification settings - Fork 4
/
arc.h
143 lines (124 loc) · 3.86 KB
/
arc.h
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
//Ankit Jain (17CO208)
//ARC Cacje replacement algorithm
//Implementation bas eon https://www.usenix.org/legacy/events/fast03/tech/full_papers/megiddo/megiddo.pdf
#include <bits/stdc++.h>
using namespace std;
class AdaptiveReplacement {
int c; // cache block size
int p;
deque <long long> T1, T2, B1, B2;
set <long long> cache;
public:
// static int hit;
// static int miss;
AdaptiveReplacement() {
}
AdaptiveReplacement(int number_of_ways){
c=number_of_ways;
p = 0;
}
void replace(long long x) {
long long old;
if (T1.size()>=1 && ((find(B2.begin(),B2.end(),x)!=B2.end() && T1.size()==p) || T1.size()>p )) {
old = T1.front();
T1.pop_front();
B1.push_back(old);
}
else {
old = T2.front();
T2.pop_front();
B2.push_back(old);
}
cache.erase(old);
}
int re(long long x) {
if (find(T1.begin(),T1.end(),x)!=T1.end() || find(T2.begin(),T2.end(),x)!=T2.end())
{
// hit++;
// cout<<"hit"<<"\n";
if(find(T1.begin(),T1.end(),x)!=T1.end())
{
auto it = find(T1.begin(),T1.end(),x);
T1.erase(it);
} else{
auto it = find(T2.begin(),T2.end(),x);
T2.erase(it);
}
T2.push_back(x);
return 1;
}
else if (find(B1.begin(),B1.end(),x)!=B1.end()) {
//miss++;
p = min(c, p+1);
replace(x);
auto it = find(B1.begin(),B1.end(),x);
B1.erase(it);
T2.push_back(x);
cache.insert(x);
}
else if (find(B2.begin(),B2.end(),x)!=B2.end()) {
//miss++;
p = max(0, p-1);
replace(x);
auto it = find(B2.begin(),B2.end(),x);
B2.erase(it);
T2.push_back(x);
cache.insert(x);
}
else {
//miss++;
if (T1.size()+B1.size() == c) {
if(T1.size()<c) {
B1.pop_front();
replace(x);
}
else {
long long old = T1.front();
T1.pop_front();
cache.erase(old);
}
}
else if(T1.size()+B1.size()< c && T1.size()+T2.size()+B1.size()+B2.size()>=c){
if (T1.size()+T2.size()+B1.size()+B2.size() == 2*c) {
B2.pop_front();
}
replace(x);
}
T1.push_back(x);
cache.insert(x);
}
//cout<<"miss"<<"\n";
return 0;
// cout<<"Item: "<<x<<"\n";
// cout<<"Cache: ";
// for(auto it = cache.begin(); it!=cache.end(); ++it)
// cout<<*it<<" ";
// cout<<"\np: "<<p;
// cout<<"\nT1: ";
// for(auto it = T1.begin(); it!=T1.end(); ++it)
// cout<<*it<<" ";
// cout<<"\nB1: ";
// for(auto it = B1.begin(); it!=B1.end(); ++it)
// cout<<*it<<" ";
// cout<<"\nT2: ";
// for(auto it = T2.begin(); it!=T2.end(); ++it)
// cout<<*it<<" ";
// cout<<"\nB2: ";
// for(auto it = B2.begin(); it!=B2.end(); ++it)
// cout<<*it<<" ";
// cout<<"\n Hits: "<<hit<<"\nMiss: "<<miss<<"\n";
}
};
// int AdaptiveReplacement::hit = 0;
// int AdaptiveReplacement::miss = 0;
static int flag =0;
AdaptiveReplacement arc[1024*32/(64*16)];
float ARC(long long int tagValue, long long int setValue , int numberOfWays, int blockSize) {
if(flag==0) {
int no_of_sets = 1024*32/(blockSize*numberOfWays); // 32KB is cache size
for(int i=0;i<no_of_sets;i++)
arc[i]= AdaptiveReplacement(numberOfWays);
flag++;
}
return arc[setValue].re(tagValue);
}