-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart3.cpp
124 lines (113 loc) · 2.12 KB
/
part3.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
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
map<ull,ull>innermap;
map<ull,map<ull,ull>>outermap;
ull CacheHits=0,CacheMisses=0;
class Cache
{
public:
ull tag,set,way;
ull flag=0;
void setGetter(ull addr);
void tagGetter(ull addr);
int isMissOrHit(ull set, ull tag);
void LRU(ull set, ull tag);
};
void Cache::setGetter(ull addr)
{
set = (addr & 0x1FFC0) >> 6;
}
void Cache::tagGetter(ull addr)
{
tag = (addr & 0xFFFFFFFFFFFE0000) >> 17;
}
int Cache::isMissOrHit(ull set, ull tag)
{
int hit=-1;
pair<ull,ull>temp;
auto outer_itr = outermap.find(set);
if(outer_itr != outermap.end())
{
innermap=outermap[set];
for(auto e: innermap)
{
if(e.second == tag)
{
temp=e;
hit=1;
break;
}
}
if(hit==1)
{
innermap.erase(temp.first);
innermap.insert({flag,tag});
outermap[set]=innermap;
return 1;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
void Cache::LRU(ull set, ull tag)
{
auto outer_itr = outermap.find(set);
if(outer_itr == outermap.end())
{
outermap[set].insert({flag,tag});
}
else
{
innermap=outermap[set];
if(innermap.size()<16)
{
innermap.insert({flag,tag});
}
else
{
innermap.erase(innermap.begin());
innermap.insert({flag,tag});
}
outermap[set]=innermap;
}
}
int main(int agrc, char *argv[])
{
char input_name[100];
ull addr=0;
int tid;
sprintf(input_name, "%s", argv[1]);
string output_name=input_name;
for(int k=0;k<4;k++) output_name.pop_back();
output_name=output_name+"_cachemiss.out";
fstream fout;
fout.open(output_name, ios::out | ios::app);
ifstream inputfile(input_name);
Cache cacheObj;
while (inputfile >> tid >> addr)
{
cacheObj.setGetter(addr);
cacheObj.tagGetter(addr);
cacheObj.flag++;
int hit = cacheObj.isMissOrHit(cacheObj.set,cacheObj.tag);
if(hit==-1)
{
CacheMisses++;
fout<<tid<<" "<<addr<<"\n";
cacheObj.LRU(cacheObj.set,cacheObj.tag);
}
if(hit==1)
{
CacheHits++;
}
}
cout<<"Cache Hits:"<<CacheHits<<" Cache Misses:"<<CacheMisses<<endl;
return 0;
}