-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect_mapped_cache.cpp
99 lines (81 loc) · 2.42 KB
/
direct_mapped_cache.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
#include <bits/stdc++.h>
using namespace std;
struct cache_content
{
bool v;
unsigned int tag;
// unsigned int data[16];
};
const int K = 1024;
double log2(double n)
{
// log(n) / log(2) is log2.
return log(n) / log(double(2));
}
void simulate(int cache_size, int block_size,const char* filename)
{
unsigned int tag, index, x;
int offset_bit = (int)log2(block_size);
int index_bit = (int)log2(cache_size / block_size);
int line = cache_size >> (offset_bit);
cache_content *cache = new cache_content[line];
//cout << "cache line: " << line << endl;
cout<<"cache_size:"<<cache_size/K<<"k "<<"block_size:"<<block_size<<"(Bytes): ";
for(int j = 0; j < line; j++)
cache[j].v = false;
FILE *fp = fopen(filename, "r"); // read file
double miss_num=0,cnt=0;
while(fscanf(fp, "%x", &x) != EOF)
{
++cnt;
//cout << x << endl;;
index = (x >> offset_bit) & (line - 1);
tag = x >> (index_bit + offset_bit);
if(cache[index].v && cache[index].tag == tag){
//cout<<index<<" ";
cache[index].v = true; // hit
}
else
{
cache[index].v = true; // miss
cache[index].tag = tag;
++miss_num;
}
}
//cout<<" miss: "<<miss_num<<" ";
cout<<setprecision(10)<<miss_num/cnt*100<<"%"<<endl;
// cout << "offset_bit: "<<offset_bit<<" index_bit: "<<index_bit<<endl;
fclose(fp);
delete [] cache;
}
int main()
{
cout<<"ICACHE.txt"<<endl;
// Let us simulate 4KB cache with 16B blocks
FILE *fp = fopen("ICACHE.txt","r");
const char *f="ICACHE.txt";
for(int i=4;i<=256;i*=4){
cout<<"*******************************************************************"<<endl;
for(int j=4;j<=9;j++){
simulate(i*K,pow(2,j),f);
}
cout<<"*******************************************************************"<<endl;
cout<<endl;
}
fclose(fp);
cout<<"================================================================="<<endl;
cout<<"================================================================="<<endl;
cout<<endl;
cout<<"DCACHE.txt.txt"<<endl;
FILE *fp2 = fopen("DCACHE.txt","r");
const char *f2="DCACHE.txt";
for(int i=4;i<=256;i*=4){
cout<<"*******************************************************************"<<endl;
for(int j=4;j<=9;j++){
simulate(i*K,pow(2,j),f2);
}
cout<<"*******************************************************************"<<endl;
cout<<endl;
}
fclose(fp2);
}