-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMT2019030.cpp
86 lines (70 loc) · 2.86 KB
/
IMT2019030.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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
#include "IMT2019030_DirectMappedCache.h"
#include "IMT2019030_SetAssociativeCache.h"
#include "Colors.h"
//Given conditions
#define SIZE 256*1024
#define BLOCKSIZE 4
#define WAYS 4
Colors c;
//function to print the results onto the command prompt
void PrintOutput(string file,int Hits,int Miss,double HitRate,double MissRate,string cachetype){
cout<<endl;
if(cachetype=="DM")
cout<<c.ANSI_YELLOW<<"____DM Cache____"<<c.ANSI_RESET<<endl;
if(cachetype=="SA")
cout<<c.ANSI_YELLOW<<"____SA Cache____"<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Current File : "<<c.ANSI_RESET<<c.ANSI_WHITE<<file<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Number of Hits : "<<c.ANSI_RESET<<c.ANSI_WHITE<<Hits<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Number of Miss : "<<c.ANSI_RESET<<c.ANSI_WHITE<<Miss<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Hit Rate : "<<c.ANSI_RESET<<c.ANSI_WHITE<<HitRate<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Miss Rate : "<<c.ANSI_RESET<<c.ANSI_WHITE<<MissRate<<c.ANSI_RESET<<endl;
cout<<c.ANSI_RED<<"Hit To Miss Ratio : "<<c.ANSI_RESET<<c.ANSI_WHITE<<(double)(HitRate)/(MissRate)<<c.ANSI_RESET<<endl;
cout<<c.ANSI_GREEN<<"____Finished Execution____"<<c.ANSI_RESET<<endl<<endl;
}
int main(){
vector<string>files={"traces/gcc.trace","traces/gzip.trace","traces/mcf.trace","traces/swim.trace","traces/twolf.trace"};
for(int i=0;i<5;i++){
//We can even place them outside the loop and clear the cache for every trace file
DMCache DMcache(SIZE,BLOCKSIZE);
SACache SAcache(SIZE,WAYS,BLOCKSIZE);
fstream file;
file.open(files[i]);
//Taking input from the files inside the traces folder
int DMHits=0,DMMiss=0;
int SAHits=0,SAMiss=0;
string field1,field2,field3;
while(file>>field1>>field2>>field3){
string Address=field2;
//calculating the #hits and #miss for both DM and CA cache for current address
if(DMcache.CheckAndHit(Address)){
DMHits++;
}
else{
DMMiss++;
}
if(SAcache.CheckAndHit(Address)){
SAHits++;
}
else{
SAMiss++;
}
}
//calculate the hit and miss rates
double DMHitRate=(double)(DMHits*1.0)/(DMHits+DMMiss);
double DMMissRate=(double)(DMMiss*1.0)/(DMHits+DMMiss);
double SAHitRate=(double)(SAHits*1.0)/(SAHits+SAMiss);
double SAMissRate=(double)(SAMiss*1.0)/(SAHits+SAMiss);
//print the output
PrintOutput(files[i],DMHits,DMMiss,DMHitRate,DMMissRate,"DM");
PrintOutput(files[i],SAHits,SAMiss,SAHitRate,SAMissRate,"SA");
//close the file
file.close();
}
return 0;
}