stride.cc (13425:00abf35b2f7e) stride.cc (13426:d2b0e9ec67f1)
1/*
2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ron Dreslinski
41 * Steve Reinhardt
42 */
43
44/**
45 * @file
46 * Stride Prefetcher template instantiations.
47 */
48
49#include "mem/cache/prefetch/stride.hh"
50
51#include <cassert>
52
53#include "base/intmath.hh"
54#include "base/logging.hh"
55#include "base/random.hh"
56#include "base/trace.hh"
57#include "debug/HWPrefetch.hh"
58#include "params/StridePrefetcher.hh"
59
1/*
2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ron Dreslinski
41 * Steve Reinhardt
42 */
43
44/**
45 * @file
46 * Stride Prefetcher template instantiations.
47 */
48
49#include "mem/cache/prefetch/stride.hh"
50
51#include <cassert>
52
53#include "base/intmath.hh"
54#include "base/logging.hh"
55#include "base/random.hh"
56#include "base/trace.hh"
57#include "debug/HWPrefetch.hh"
58#include "params/StridePrefetcher.hh"
59
60StridePrefetcher::StrideEntry::StrideEntry()
61{
62 invalidate();
63}
64
65void
66StridePrefetcher::StrideEntry::invalidate()
67{
68 instAddr = 0;
69 lastAddr = 0;
70 isSecure = false;
71 stride = 0;
72 confidence = 0;
73}
74
60StridePrefetcher::StridePrefetcher(const StridePrefetcherParams *p)
61 : QueuedPrefetcher(p),
62 maxConf(p->max_conf),
63 threshConf(p->thresh_conf),
64 minConf(p->min_conf),
65 startConf(p->start_conf),
66 pcTableAssoc(p->table_assoc),
67 pcTableSets(p->table_sets),
68 useMasterId(p->use_master_id),
69 degree(p->degree)
70{
71 assert(isPowerOf2(pcTableSets));
72}
73
74StridePrefetcher::PCTable*
75StridePrefetcher::findTable(int context)
76{
77 // Check if table for given context exists
78 auto it = pcTables.find(context);
79 if (it != pcTables.end())
80 return &it->second;
81
82 // If table does not exist yet, create one
83 return allocateNewContext(context);
84}
85
86StridePrefetcher::PCTable*
87StridePrefetcher::allocateNewContext(int context)
88{
89 // Create new table
90 auto insertion_result = pcTables.insert(std::make_pair(context,
91 PCTable(pcTableAssoc, pcTableSets, name())));
92
93 DPRINTF(HWPrefetch, "Adding context %i with stride entries\n", context);
94
95 // Get iterator to new pc table, and then return a pointer to the new table
96 return &(insertion_result.first->second);
97}
98
99StridePrefetcher::PCTable::PCTable(int assoc, int sets, const std::string name)
100 : pcTableAssoc(assoc), pcTableSets(sets), _name(name), entries(pcTableSets)
101{
102 for (auto& set : entries) {
103 set.resize(pcTableAssoc);
104 }
105}
106
107StridePrefetcher::PCTable::~PCTable()
108{
109}
110
111void
112StridePrefetcher::calculatePrefetch(const PacketPtr &pkt,
113 std::vector<AddrPriority> &addresses)
114{
115 if (!pkt->req->hasPC()) {
116 DPRINTF(HWPrefetch, "Ignoring request with no PC.\n");
117 return;
118 }
119
120 // Get required packet info
121 Addr pkt_addr = pkt->getAddr();
122 Addr pc = pkt->req->getPC();
123 bool is_secure = pkt->isSecure();
124 MasterID master_id = useMasterId ? pkt->req->masterId() : 0;
125
126 // Get corresponding pc table
127 PCTable* pcTable = findTable(master_id);
128
129 // Search for entry in the pc table
130 StrideEntry *entry = pcTable->findEntry(pc, is_secure);
131
132 if (entry != nullptr) {
133 // Hit in table
134 int new_stride = pkt_addr - entry->lastAddr;
135 bool stride_match = (new_stride == entry->stride);
136
137 // Adjust confidence for stride entry
138 if (stride_match && new_stride != 0) {
139 if (entry->confidence < maxConf)
140 entry->confidence++;
141 } else {
142 if (entry->confidence > minConf)
143 entry->confidence--;
144 // If confidence has dropped below the threshold, train new stride
145 if (entry->confidence < threshConf)
146 entry->stride = new_stride;
147 }
148
149 DPRINTF(HWPrefetch, "Hit: PC %x pkt_addr %x (%s) stride %d (%s), "
150 "conf %d\n", pc, pkt_addr, is_secure ? "s" : "ns", new_stride,
151 stride_match ? "match" : "change",
152 entry->confidence);
153
154 entry->lastAddr = pkt_addr;
155
156 // Abort prefetch generation if below confidence threshold
157 if (entry->confidence < threshConf)
158 return;
159
160 // Generate up to degree prefetches
161 for (int d = 1; d <= degree; d++) {
162 // Round strides up to atleast 1 cacheline
163 int prefetch_stride = new_stride;
164 if (abs(new_stride) < blkSize) {
165 prefetch_stride = (new_stride < 0) ? -blkSize : blkSize;
166 }
167
168 Addr new_addr = pkt_addr + d * prefetch_stride;
169 if (samePage(pkt_addr, new_addr)) {
170 DPRINTF(HWPrefetch, "Queuing prefetch to %#x.\n", new_addr);
171 addresses.push_back(AddrPriority(new_addr, 0));
172 } else {
173 // Record the number of page crossing prefetches generated
174 pfSpanPage += degree - d + 1;
175 DPRINTF(HWPrefetch, "Ignoring page crossing prefetch.\n");
176 return;
177 }
178 }
179 } else {
180 // Miss in table
181 DPRINTF(HWPrefetch, "Miss: PC %x pkt_addr %x (%s)\n", pc, pkt_addr,
182 is_secure ? "s" : "ns");
183
184 StrideEntry* entry = pcTable->findVictim(pc);
75StridePrefetcher::StridePrefetcher(const StridePrefetcherParams *p)
76 : QueuedPrefetcher(p),
77 maxConf(p->max_conf),
78 threshConf(p->thresh_conf),
79 minConf(p->min_conf),
80 startConf(p->start_conf),
81 pcTableAssoc(p->table_assoc),
82 pcTableSets(p->table_sets),
83 useMasterId(p->use_master_id),
84 degree(p->degree)
85{
86 assert(isPowerOf2(pcTableSets));
87}
88
89StridePrefetcher::PCTable*
90StridePrefetcher::findTable(int context)
91{
92 // Check if table for given context exists
93 auto it = pcTables.find(context);
94 if (it != pcTables.end())
95 return &it->second;
96
97 // If table does not exist yet, create one
98 return allocateNewContext(context);
99}
100
101StridePrefetcher::PCTable*
102StridePrefetcher::allocateNewContext(int context)
103{
104 // Create new table
105 auto insertion_result = pcTables.insert(std::make_pair(context,
106 PCTable(pcTableAssoc, pcTableSets, name())));
107
108 DPRINTF(HWPrefetch, "Adding context %i with stride entries\n", context);
109
110 // Get iterator to new pc table, and then return a pointer to the new table
111 return &(insertion_result.first->second);
112}
113
114StridePrefetcher::PCTable::PCTable(int assoc, int sets, const std::string name)
115 : pcTableAssoc(assoc), pcTableSets(sets), _name(name), entries(pcTableSets)
116{
117 for (auto& set : entries) {
118 set.resize(pcTableAssoc);
119 }
120}
121
122StridePrefetcher::PCTable::~PCTable()
123{
124}
125
126void
127StridePrefetcher::calculatePrefetch(const PacketPtr &pkt,
128 std::vector<AddrPriority> &addresses)
129{
130 if (!pkt->req->hasPC()) {
131 DPRINTF(HWPrefetch, "Ignoring request with no PC.\n");
132 return;
133 }
134
135 // Get required packet info
136 Addr pkt_addr = pkt->getAddr();
137 Addr pc = pkt->req->getPC();
138 bool is_secure = pkt->isSecure();
139 MasterID master_id = useMasterId ? pkt->req->masterId() : 0;
140
141 // Get corresponding pc table
142 PCTable* pcTable = findTable(master_id);
143
144 // Search for entry in the pc table
145 StrideEntry *entry = pcTable->findEntry(pc, is_secure);
146
147 if (entry != nullptr) {
148 // Hit in table
149 int new_stride = pkt_addr - entry->lastAddr;
150 bool stride_match = (new_stride == entry->stride);
151
152 // Adjust confidence for stride entry
153 if (stride_match && new_stride != 0) {
154 if (entry->confidence < maxConf)
155 entry->confidence++;
156 } else {
157 if (entry->confidence > minConf)
158 entry->confidence--;
159 // If confidence has dropped below the threshold, train new stride
160 if (entry->confidence < threshConf)
161 entry->stride = new_stride;
162 }
163
164 DPRINTF(HWPrefetch, "Hit: PC %x pkt_addr %x (%s) stride %d (%s), "
165 "conf %d\n", pc, pkt_addr, is_secure ? "s" : "ns", new_stride,
166 stride_match ? "match" : "change",
167 entry->confidence);
168
169 entry->lastAddr = pkt_addr;
170
171 // Abort prefetch generation if below confidence threshold
172 if (entry->confidence < threshConf)
173 return;
174
175 // Generate up to degree prefetches
176 for (int d = 1; d <= degree; d++) {
177 // Round strides up to atleast 1 cacheline
178 int prefetch_stride = new_stride;
179 if (abs(new_stride) < blkSize) {
180 prefetch_stride = (new_stride < 0) ? -blkSize : blkSize;
181 }
182
183 Addr new_addr = pkt_addr + d * prefetch_stride;
184 if (samePage(pkt_addr, new_addr)) {
185 DPRINTF(HWPrefetch, "Queuing prefetch to %#x.\n", new_addr);
186 addresses.push_back(AddrPriority(new_addr, 0));
187 } else {
188 // Record the number of page crossing prefetches generated
189 pfSpanPage += degree - d + 1;
190 DPRINTF(HWPrefetch, "Ignoring page crossing prefetch.\n");
191 return;
192 }
193 }
194 } else {
195 // Miss in table
196 DPRINTF(HWPrefetch, "Miss: PC %x pkt_addr %x (%s)\n", pc, pkt_addr,
197 is_secure ? "s" : "ns");
198
199 StrideEntry* entry = pcTable->findVictim(pc);
200
201 // Invalidate victim
202 entry->invalidate();
203
204 // Insert new entry's data
185 entry->instAddr = pc;
186 entry->lastAddr = pkt_addr;
187 entry->isSecure= is_secure;
205 entry->instAddr = pc;
206 entry->lastAddr = pkt_addr;
207 entry->isSecure= is_secure;
188 entry->stride = 0;
189 entry->confidence = startConf;
190 }
191}
192
193inline Addr
194StridePrefetcher::PCTable::pcHash(Addr pc) const
195{
196 Addr hash1 = pc >> 1;
197 Addr hash2 = hash1 >> floorLog2(pcTableSets);
198 return (hash1 ^ hash2) & (Addr)(pcTableSets - 1);
199}
200
201inline StridePrefetcher::StrideEntry*
202StridePrefetcher::PCTable::findVictim(Addr pc)
203{
204 // Rand replacement for now
205 int set = pcHash(pc);
206 int way = random_mt.random<int>(0, pcTableAssoc - 1);
207
208 DPRINTF(HWPrefetch, "Victimizing lookup table[%d][%d].\n", set, way);
209 return &entries[set][way];
210}
211
212inline StridePrefetcher::StrideEntry*
213StridePrefetcher::PCTable::findEntry(Addr pc, bool is_secure)
214{
215 int set = pcHash(pc);
216 std::vector<StrideEntry>& set_entries = entries[set];
217 for (int way = 0; way < pcTableAssoc; way++) {
218 StrideEntry* entry = &set_entries[way];
219 // Search ways for match
220 if ((entry->instAddr == pc) && (entry->isSecure == is_secure)) {
221 DPRINTF(HWPrefetch, "Lookup hit table[%d][%d].\n", set, way);
222 return entry;
223 }
224 }
225 return nullptr;
226}
227
228StridePrefetcher*
229StridePrefetcherParams::create()
230{
231 return new StridePrefetcher(this);
232}
208 entry->confidence = startConf;
209 }
210}
211
212inline Addr
213StridePrefetcher::PCTable::pcHash(Addr pc) const
214{
215 Addr hash1 = pc >> 1;
216 Addr hash2 = hash1 >> floorLog2(pcTableSets);
217 return (hash1 ^ hash2) & (Addr)(pcTableSets - 1);
218}
219
220inline StridePrefetcher::StrideEntry*
221StridePrefetcher::PCTable::findVictim(Addr pc)
222{
223 // Rand replacement for now
224 int set = pcHash(pc);
225 int way = random_mt.random<int>(0, pcTableAssoc - 1);
226
227 DPRINTF(HWPrefetch, "Victimizing lookup table[%d][%d].\n", set, way);
228 return &entries[set][way];
229}
230
231inline StridePrefetcher::StrideEntry*
232StridePrefetcher::PCTable::findEntry(Addr pc, bool is_secure)
233{
234 int set = pcHash(pc);
235 std::vector<StrideEntry>& set_entries = entries[set];
236 for (int way = 0; way < pcTableAssoc; way++) {
237 StrideEntry* entry = &set_entries[way];
238 // Search ways for match
239 if ((entry->instAddr == pc) && (entry->isSecure == is_secure)) {
240 DPRINTF(HWPrefetch, "Lookup hit table[%d][%d].\n", set, way);
241 return entry;
242 }
243 }
244 return nullptr;
245}
246
247StridePrefetcher*
248StridePrefetcherParams::create()
249{
250 return new StridePrefetcher(this);
251}