-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
233 lines (218 loc) · 5.91 KB
/
main.cpp
File metadata and controls
233 lines (218 loc) · 5.91 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
#include <cstdio>
#include <iostream>
#include <vector>
#define INFINITE 9999
#define a 0.5
struct FHash{
int inode,start,end,lastTime,period;
bool ghostRef= false;
};
struct PHash{
int pc,fresh,reused,period;
};
struct DoubleReturn{
std::string stringValue;
int intValue=0;
};
int threshold=0;
struct Input{
int inode , block , pc , curTime;
};
std::vector<FHash*> F;
std::vector<PHash*> P;
//here Race is going to act as a partition allocator
int findsP( PHash p){
int i=0;
for(auto x : P)
{
if(x==&p)
{
return i;
}
i++;
}
return -1;
}
int *lastCalled= nullptr;
DoubleReturn RACE(int inode,int block,int pc,int curTime, int &countForGhost){
if(F.empty())
{
FHash *f = new FHash;
f->start=block;
f->end=block;
f->lastTime=curTime;
f->inode=inode;
f->period=INFINITE;
F.push_back(f);
}
if(P.empty())
{
PHash *temp=new PHash;
temp->pc=pc;
temp->fresh=0;
temp->reused=0;
temp->period=INFINITE;
P.push_back(temp);
DoubleReturn rr;
rr.intValue=-1;
rr.stringValue="Add more Values";
return rr;
}
int check=0;
DoubleReturn Return;
//line 3
for (auto i : P)
{
if(pc==i->pc){
check =1;
break;
}
}
//now inserting PC
if(check==0)
{
PHash *temp=new PHash;
temp->pc=pc,temp->fresh=0,temp->reused=0,temp->period=INFINITE;
P.push_back(temp);
}
//line 4
int lastTime;
for (auto f : F) {
if (f->inode == inode && f->start <= block <= f->end && !f->ghostRef) {
lastTime = curTime - (block - f->start);
if(countForGhost==0)
{
f->ghostRef = true;
countForGhost++;
break;
}
//{consider "ghost" reference time of the 1st block}
f->period = a * f->period + (1 - a) * (lastTime - f->lastTime);//exponential average
for (auto p : P) {
if (p->pc == pc) {
p->reused++;
p->fresh--;
p->period = a * f->period + (1 - a) * p->period;//exponential average
//updating last reference time of the 1st block;
int index1=findsP(*p);
if (index1<*lastCalled)//access direction is reversed
{
f->lastTime = lastTime;
}
Return.stringValue= "looping";
Return.intValue=f->period;
int index=findsP(*p);
lastCalled=&index;
return Return;
}
}
}
}
for(auto f : F)
{
if(f->inode==inode && f->end==block-1 && !f->ghostRef)
{
f->end=block;
for (auto p : P)
{
if(p->pc==pc)
{
p->fresh++;
break;
}
}
}
else {
FHash *f1=new FHash;
f1->inode=inode; f1->start=block;
f1->end=block; f1->lastTime=curTime;
f1->period=INFINITE;
F.push_back(f1);
for (auto p : P)
{
if(p->pc==pc)
{
p->fresh++;
break;
}
}
break;
}
}
for (auto p : P)
{
if(p->pc==pc && p->reused>=p->fresh)
{
Return.stringValue= "looping";
Return.intValue= p->period;
return Return;
}
}
for (auto p : P)
{//line number 21
if(p->pc==pc && p->fresh>threshold)//threshold is not defined
{
Return.stringValue= "Sequential";
Return.intValue= 0;
return Return;
}
}
Return.stringValue="Other";
Return.intValue=0;
return Return;
}
int main()
{
float bufferCache;
printf("Enter the size of Buffer cache:\t");
scanf("%f",&bufferCache);
int response;
printf("press 1 to enter values press 2 to exit : ");
scanf("%d",&response);
int countLoop=0;
int countSequence=0;
int countOther = 0;
while(response==1) {
std::vector<Input *> inputArray;
printf("Inode | Block | PC | current time |\n");
Input temp{};
scanf("%d %d %d %d", &temp.inode, &temp.block, &temp.pc, &temp.curTime);
inputArray.push_back(&temp);
int blockCount = 0;
for (auto Inp : inputArray) {
DoubleReturn rr;
rr = RACE(Inp->inode, Inp->block, Inp->pc, Inp->curTime, blockCount);
if(rr.stringValue=="Add more Values")
{
printf(" Add more Values...\n");
}
else if(rr.stringValue=="looping")
{
printf(" Reference detected is: Looping");
printf("\n Period is %d\n",rr.intValue);
countLoop++;
}
else if (rr.stringValue=="Sequential")
{
printf(" Reference detected is: Sequential");
printf("\n Period is %d\n",rr.intValue);
countSequence++;
}
else if (rr.stringValue=="Other")
{
printf(" Reference detected is: Other");
countOther++;
}
}
printf("press 1 to enter values press 2 to exit : ");
scanf("%d",&response);
}
int total =countLoop+countOther+countSequence;
printf("Partition of cache for Loop reference: %2f\n "
"for Sequence reference: %2f\n"
"for Others: %2f\n",bufferCache*countLoop/total,
bufferCache*countSequence/total,
bufferCache*countOther/total);
printf("Exiting...\n");
return 0;
}