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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/**
* Copyright (c) 2018 Metempsy Technology Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Ivan Pizarro
*/
#include "mem/cache/prefetch/bop.hh"
#include "debug/HWPrefetch.hh"
#include "params/BOPPrefetcher.hh"
BOPPrefetcher::BOPPrefetcher(const BOPPrefetcherParams *p)
: QueuedPrefetcher(p),
scoreMax(p->score_max), roundMax(p->round_max),
badScore(p->bad_score), rrEntries(p->rr_size),
tagMask((1 << p->tag_bits) - 1),
delayQueueEnabled(p->delay_queue_enable),
delayQueueSize(p->delay_queue_size),
delayTicks(cyclesToTicks(p->delay_queue_cycles)),
delayQueueEvent([this]{ delayQueueEventWrapper(); }, name()),
issuePrefetchRequests(false), bestOffset(1), phaseBestOffset(0),
bestScore(0), round(0)
{
if (!isPowerOf2(rrEntries)) {
fatal("%s: number of RR entries is not power of 2\n", name());
}
if (!isPowerOf2(blkSize)) {
fatal("%s: cache line size is not power of 2\n", name());
}
if (!(p->negative_offsets_enable && (p->offset_list_size % 2 == 0))) {
fatal("%s: negative offsets enabled with odd offset list size\n",
name());
}
rrLeft.resize(rrEntries);
rrRight.resize(rrEntries);
// Following the paper implementation, a list with the specified number
// of offsets which are of the form 2^i * 3^j * 5^k with i,j,k >= 0
const int factors[] = { 2, 3, 5 };
unsigned int i = 0;
int64_t offset_i = 1;
while (i < p->offset_list_size)
{
int64_t offset = offset_i;
for (int n : factors) {
while ((offset % n) == 0) {
offset /= n;
}
}
if (offset == 1) {
offsetsList.push_back(OffsetListEntry(offset_i, 0));
i++;
// If we want to use negative offsets, add also the negative value
// of the offset just calculated
if (p->negative_offsets_enable) {
offsetsList.push_back(OffsetListEntry(-offset_i, 0));
i++;
}
}
offset_i++;
}
offsetsListIterator = offsetsList.begin();
}
void
BOPPrefetcher::delayQueueEventWrapper()
{
while (!delayQueue.empty() &&
delayQueue.front().processTick <= curTick())
{
Addr addr_x = delayQueue.front().baseAddr;
insertIntoRR(addr_x, RRWay::Left);
delayQueue.pop_front();
}
// Schedule an event for the next element if there is one
if (!delayQueue.empty()) {
schedule(delayQueueEvent, delayQueue.front().processTick);
}
}
unsigned int
BOPPrefetcher::hash(Addr addr, unsigned int way) const
{
Addr hash1 = addr >> way;
Addr hash2 = hash1 >> floorLog2(rrEntries);
return (hash1 ^ hash2) & (Addr)(rrEntries - 1);
}
void
BOPPrefetcher::insertIntoRR(Addr addr, unsigned int way)
{
switch (way) {
case RRWay::Left:
rrLeft[hash(addr, RRWay::Left)] = addr;
break;
case RRWay::Right:
rrRight[hash(addr, RRWay::Right)] = addr;
break;
}
}
void
BOPPrefetcher::insertIntoDelayQueue(Addr x)
{
if (delayQueue.size() == delayQueueSize) {
return;
}
// Add the address to the delay queue and schedule an event to process
// it after the specified delay cycles
Tick process_tick = curTick() + delayTicks;
delayQueue.push_back(DelayQueueEntry(x, process_tick));
if (!delayQueueEvent.scheduled()) {
schedule(delayQueueEvent, process_tick);
}
}
void
BOPPrefetcher::resetScores()
{
for (auto& it : offsetsList) {
it.second = 0;
}
}
inline Addr
BOPPrefetcher::tag(Addr addr) const
{
return (addr >> blkSize) & tagMask;
}
bool
BOPPrefetcher::testRR(Addr addr) const
{
for (auto& it : rrLeft) {
if (it == addr) {
return true;
}
}
for (auto& it : rrRight) {
if (it == addr) {
return true;
}
}
return false;
}
void
BOPPrefetcher::bestOffsetLearning(Addr x)
{
Addr offset_addr = (*offsetsListIterator).first;
Addr lookup_addr = x - offset_addr;
// There was a hit in the RR table, increment the score for this offset
if (testRR(lookup_addr)) {
DPRINTF(HWPrefetch, "Address %#lx found in the RR table\n", x);
(*offsetsListIterator).second++;
if ((*offsetsListIterator).second > bestScore) {
bestScore = (*offsetsListIterator).second;
phaseBestOffset = (*offsetsListIterator).first;
DPRINTF(HWPrefetch, "New best score is %lu\n", bestScore);
}
}
offsetsListIterator++;
// All the offsets in the list were visited meaning that a learning
// phase finished. Check if
if (offsetsListIterator == offsetsList.end()) {
offsetsListIterator = offsetsList.begin();
round++;
// Check if the best offset must be updated if:
// (1) One of the scores equals SCORE_MAX
// (2) The number of rounds equals ROUND_MAX
if ((bestScore >= scoreMax) || (round == roundMax)) {
bestOffset = phaseBestOffset;
round = 0;
bestScore = 0;
phaseBestOffset = 0;
resetScores();
issuePrefetchRequests = true;
} else if (phaseBestOffset <= badScore) {
issuePrefetchRequests = false;
}
}
}
void
BOPPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
std::vector<AddrPriority> &addresses)
{
Addr addr = pfi.getAddr();
Addr tag_x = tag(addr);
if (delayQueueEnabled) {
insertIntoDelayQueue(tag_x);
} else {
insertIntoRR(tag_x, RRWay::Left);
}
// Go through the nth offset and update the score, the best score and the
// current best offset if a better one is found
bestOffsetLearning(tag_x);
// This prefetcher is a degree 1 prefetch, so it will only generate one
// prefetch at most per access
if (issuePrefetchRequests) {
Addr prefetch_addr = addr + (bestOffset << lBlkSize);
addresses.push_back(AddrPriority(prefetch_addr, 0));
DPRINTF(HWPrefetch, "Generated prefetch %#lx\n", prefetch_addr);
}
}
void
BOPPrefetcher::notifyFill(const PacketPtr& pkt)
{
// Only insert into the RR right way if it's the pkt is a HWP
if (!pkt->cmd.isHWPrefetch()) return;
Addr tag_y = tag(pkt->getAddr());
if (issuePrefetchRequests) {
insertIntoRR(tag_y - bestOffset, RRWay::Right);
}
}
BOPPrefetcher*
BOPPrefetcherParams::create()
{
return new BOPPrefetcher(this);
}
|