forked from dotcom900825/cse240
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefetcher.C
250 lines (209 loc) · 5.81 KB
/
prefetcher.C
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "prefetcher.h"
#include <stdio.h>
#define DEBUG 0
// Helper function to initilize GHB table
void initGHB(struct item *ghbTable){
if (ghbTable) {
for (int i = 0; i < MAX_CAPACITY; i++) {
ghbTable[i].delta = 0;
ghbTable[i].prev = -1;
}
} else {
if(DEBUG == 1) printf("ERROR: items initialization failed");
}
}
Prefetcher::Prefetcher() {
_nextReq.addr = 0;
_ready = false;
_queue_full = false;
_nextReq.addr = 0;
_end_of_queue = 0;
_prev_addr = 0;
_next_delta = 0;
_depth = 0;
initGHB(_ghb_table);
}
Prefetcher::~Prefetcher() {
}
/*
Helper function
find max item from the candidate array
*/
int findMax(struct candidate candidates[], int end_index){
int max_candidate_value = 0;
int index = 0;
//Sort through the candidate list and find the most likely candidate item.
for (int i = 0; i < end_index; i++) {
if(DEBUG == 1) printf(" candidate %d, 0x%x, %d\n", i, candidates[i].delta, candidates[i].count);
if (candidates[i].count > max_candidate_value) {
max_candidate_value = candidates[i].count;
index = i;
}
}
}
/*
Helper function
Iterate from the end of the array since is a FIFO queue.
Use the given address difference try to find the previous entry in the global
history table with the same addresss distance
*/
int Prefetcher::locatePrevEntry(int delta){
int cur_enq = _end_of_queue;
int i;
if (_queue_full == false)
{
for (i = _end_of_queue; i >= 0; i--)
{
if (_ghb_table[i].delta == delta) {
return i;
}
}
return -1;
}else{
while( i < MAX_CAPACITY - 1){
if (cur_enq == 0)
cur_enq = MAX_CAPACITY - 1;
else
cur_enq--;
if (_ghb_table[cur_enq].delta == delta) {
return cur_enq;
}
i++;
}
}
return -1;
}
/*
Helper function
Search the global history table, with the current address difference,
find the next most likely request's address delta relative to current requrest
We will keep a candidate array to track all the possible request' address delta
and eventually sort the array and add the most possible next distance to current
request address, we will use it to format the next prefetched request address.
*/
int Prefetcher::locateCandidate(int delta){
int cur_enq = _end_of_queue;
int possible_delta_index;
struct candidate candidates[10];
int i;
bool found = false;
int candidate_array_index = 0;
int flip = 0;
int index = 0;
int next_pos = 0;
int temp_delta = 0;
/* Initialize the candidates array with 10 entrys */
for (i = 0; i < 10; i++) {
candidates[i].delta = 0;
candidates[i].count = 0; //set counter to 0
}
while(1) {
next_pos = _ghb_table[cur_enq].prev;
if (next_pos == -1)
break;
if (next_pos >= cur_enq && flip)
break;
if (next_pos >= cur_enq)
flip = 1;
if (_ghb_table[next_pos].delta != delta)
break;
possible_delta_index = cur_enq + 1;
if (possible_delta_index == MAX_CAPACITY){
possible_delta_index = 0;
}
/* Insert the addr of candidate entry to candidates array */
temp_delta = _ghb_table[possible_delta_index].delta;
if (temp_delta) {
for (i = 0; i < candidate_array_index; i++) {
if (candidates[i].delta == temp_delta) {
candidates[i].count++;
break;
}
}
if (i == candidate_array_index && candidate_array_index < 10) { //add new candidates
candidates[candidate_array_index].delta = temp_delta;
candidates[candidate_array_index].count++;
candidate_array_index++;
}
}
if(DEBUG == 1) printf("delta %d, enqueue %d, next_pos %d, cur_enq %d\n", delta, _end_of_queue, next_pos, cur_enq);
cur_enq = next_pos;
}
index = findMax(candidates, candidate_array_index);
if (candidates[index].delta){
return candidates[index].delta;
}else{
return 0;
}
}
/*
When CPU is idle, we will issue up to three prefetch request
Although you can issue more than 3, but it would occupy the L2 queue and has
impact on performence
*/
bool Prefetcher::hasRequest(u_int32_t cycle){
_depth++;
if (_depth > 3)
_ready = false;
else
_ready = true;
if(DEBUG == 1) printf("Has request, _depth %d, ready %d\n", _depth, _ready);
return _ready;
}
/*
We will fetch 3 different request based on the next delta value
After some trial and fine tuning, we find out 32 is a reasonable value to use
for alignment
*/
Request Prefetcher::getRequest(u_int32_t cycle){
int next_delta;
switch(_depth){
case 1:
next_delta = _next_delta;
break;
case 2:
next_delta = _next_delta - 32;
break;
default:
next_delta = _next_delta + 32;
}
_nextReq.addr = _prev_addr + next_delta;
if(DEBUG == 1) printf("Next address %d\n", _nextReq.addr);
return _nextReq;
}
void Prefetcher::completeRequest(u_int32_t cycle){
return;
}
/*
*/
void Prefetcher::cpuRequest(Request req){
int delta;
int next_delta;
if(!_ready && !req.HitL1) {
delta = req.addr - _prev_addr;
//TODO use LRU replacement
_ghb_table[_end_of_queue].delta = delta;
_ghb_table[_end_of_queue].prev = locatePrevEntry(delta);
_end_of_queue++;
if (_end_of_queue == MAX_CAPACITY){
_end_of_queue = 0;
_queue_full = true;
}
next_delta = locateCandidate(delta);
/* align next_delta to 64 */
if (next_delta >= 0 && next_delta < 64) {
if(DEBUG == 1) printf("find candidate delta %d for 0x%x\n", next_delta, req.addr);
next_delta = 64;
} else if (next_delta < 0 && next_delta > -64){
next_delta = -64;
}
_next_delta = next_delta;
_prev_addr = req.addr;
/*
Set flag variable to ready state, then when CPU is in idle state,
we can start prefetching
*/
_ready = true;
_depth = 0;
}
}