gem5 v24.1.0.1
Loading...
Searching...
No Matches
stride.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Inria
3 * Copyright (c) 2012-2013, 2015, 2022-2024 Arm Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
48
49#include <cassert>
50
51#include "base/intmath.hh"
52#include "base/logging.hh"
53#include "base/random.hh"
54#include "base/trace.hh"
55#include "debug/HWPrefetch.hh"
57#include "params/StridePrefetcher.hh"
58
59namespace gem5
60{
61
62namespace prefetch
63{
64
67 : TaggedEntry(), confidence(init_confidence)
68{
70 invalidate();
71}
72
73void
75{
77 lastAddr = 0;
78 stride = 0;
79 confidence.reset();
80}
81
82Stride::Stride(const StridePrefetcherParams &p)
83 : Queued(p),
84 initConfidence(p.confidence_counter_bits, p.initial_confidence),
85 threshConf(p.confidence_threshold/100.0),
86 useRequestorId(p.use_requestor_id),
89 pcTableInfo(p.table_assoc, p.table_entries, p.table_indexing_policy,
90 p.table_replacement_policy),
91 useCachelineAddr(p.use_cache_line_address)
92{
93}
94
97{
98 // Check if table for given context exists
99 auto it = pcTables.find(context);
100 if (it != pcTables.end())
101 return *(it->second);
102
103 // If table does not exist yet, create one
104 return allocateNewContext(context);
105}
106
109{
110 std::string table_name = name() + ".PCTable" + std::to_string(context);
111 // Create new table
112 pcTables[context].reset(new PCTable(
113 table_name.c_str(),
120
121 DPRINTF(HWPrefetch, "Adding context %i with stride entries\n", context);
122
123 // return a reference to the new table
124 return *(pcTables[context]);
125}
126
127void
129 std::vector<AddrPriority> &addresses,
130 const CacheAccessor &cache)
131{
132 if (!pfi.hasPC()) {
133 DPRINTF(HWPrefetch, "Ignoring request with no PC.\n");
134 return;
135 }
136
137 // Get required packet info
138 Addr pf_addr = useCachelineAddr ? blockAddress(pfi.getAddr())
139 : pfi.getAddr();
140 Addr pc = pfi.getPC();
141 bool is_secure = pfi.isSecure();
142 RequestorID requestor_id = useRequestorId ? pfi.getRequestorId() : 0;
143
144 // Get corresponding pc table
145 PCTable& pc_table = findTable(requestor_id);
146
147 // Search for entry in the pc table
148 const StrideEntry::KeyType key{pc, is_secure};
149 StrideEntry *entry = pc_table.findEntry(key);
150
151 if (entry != nullptr) {
152 pc_table.accessEntry(entry);
153
154 // Hit in table
155 int new_stride = pf_addr - entry->lastAddr;
156
157 // Do nothing on repeated memory access
158 if (useCachelineAddr && new_stride == 0)
159 return;
160
161 bool stride_match = (new_stride == entry->stride);
162
163 // Adjust confidence for stride entry
164 if (stride_match) {
165 entry->confidence++;
166 } else {
167 entry->confidence--;
168 // If confidence has dropped below the threshold, train new stride
169 if (entry->confidence.calcSaturation() < threshConf) {
170 entry->stride = new_stride;
171 }
172 }
173
174 DPRINTF(HWPrefetch, "Hit: PC %x pkt_addr %x (%s) stride %d (%s), "
175 "conf %d\n", pc, pf_addr, is_secure ? "s" : "ns",
176 new_stride, stride_match ? "match" : "change",
177 (int)entry->confidence);
178
179 entry->lastAddr = pf_addr;
180
181 // Abort prefetch generation if below confidence threshold
182 if (entry->confidence.calcSaturation() < threshConf) {
183 return;
184 }
185
186 // Round strides up to at least 1 cacheline
187 int prefetch_stride = entry->stride;
188 if (abs(prefetch_stride) < blkSize) {
189 prefetch_stride = (prefetch_stride < 0) ? -blkSize : blkSize;
190 }
191
192 Addr new_addr = pf_addr + distance * prefetch_stride;
193 // Generate up to degree prefetches
194 for (int d = 1; d <= degree; d++) {
195 new_addr += prefetch_stride;
196 addresses.push_back(AddrPriority(new_addr, 0));
197 }
198 } else {
199 // Miss in table
200 DPRINTF(HWPrefetch, "Miss: PC %x pkt_addr %x (%s)\n", pc, pf_addr,
201 is_secure ? "s" : "ns");
202
203 StrideEntry* entry = pc_table.findVictim(key);
204
205 // Insert new entry's data
206 entry->lastAddr = pf_addr;
207 pc_table.insertEntry(key, entry);
208 }
209}
210
211uint32_t
213{
214 const Addr pc = key.address;
215 const Addr hash1 = pc >> 1;
216 const Addr hash2 = hash1 >> tagShift;
217 return (hash1 ^ hash2) & setMask;
218}
219
220Addr
225
226} // namespace prefetch
227} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
virtual Entry * findVictim(const KeyType &key)
Find a victim to be replaced.
virtual Entry * findEntry(const KeyType &key) const
Find an entry within the set.
virtual void insertEntry(const KeyType &key, Entry *entry)
Indicate that an entry has just been inserted.
virtual Entry * accessEntry(const KeyType &key)
Do an access to the entry if it exists.
const int tagShift
The amount to shift the address to get the tag.
Definition base.hh:106
typename Types::KeyType KeyType
Definition base.hh:75
const unsigned setMask
Mask out all bits that aren't part of the set index.
Definition base.hh:96
virtual std::string name() const
Definition named.hh:47
A tagged entry is an entry containing a tag.
void registerTagExtractor(TagExtractor ext)
virtual void invalidate()
Invalidate the block.
std::function< Addr(Addr)> TagExtractor
Class containing the information needed by the prefetch to train and generate new prefetch requests.
Definition base.hh:111
Addr getPC() const
Returns the program counter that generated this request.
Definition base.hh:156
bool isSecure() const
Returns true if the address targets the secure memory space.
Definition base.hh:147
Addr getAddr() const
Obtains the address value of this Prefetcher address.
Definition base.hh:138
bool hasPC() const
Returns true if the associated program counter is valid.
Definition base.hh:166
RequestorID getRequestorId() const
Gets the requestor ID that generated this address.
Definition base.hh:175
unsigned blkSize
The block size of the parent cache.
Definition base.hh:286
Addr blockAddress(Addr a) const
Determine the address of the block in which a lays.
Definition base.cc:202
std::pair< Addr, int32_t > AddrPriority
Definition queued.hh:192
Addr extractTag(const Addr addr) const override
Generate the tag from the given address.
Definition stride.cc:221
uint32_t extractSet(const KeyType &key) const override
Definition stride.cc:212
const SatCounter8 initConfidence
Initial confidence counter value for the pc tables.
Definition stride.hh:99
const double threshConf
Confidence threshold for prefetch generation.
Definition stride.hh:102
void calculatePrefetch(const PrefetchInfo &pfi, std::vector< AddrPriority > &addresses, const CacheAccessor &cache) override
Definition stride.cc:128
const bool useCachelineAddr
If this parameter is set to true, then the prefetcher will operate at the granularity of cache line.
Definition stride.hh:155
PCTable & allocateNewContext(int context)
Create a PC table for the given context.
Definition stride.cc:108
PCTable & findTable(int context)
Try to find a table of entries for the given context.
Definition stride.cc:96
std::unordered_map< int, std::unique_ptr< PCTable > > pcTables
Definition stride.hh:148
AssociativeCache< StrideEntry > PCTable
Definition stride.hh:147
const int distance
How far ahead of the demand stream to start prefetching.
Definition stride.hh:114
Stride(const StridePrefetcherParams &p)
Definition stride.cc:82
const bool useRequestorId
Definition stride.hh:104
const struct gem5::prefetch::Stride::PCTableInfo pcTableInfo
STL vector class.
Definition stl.hh:37
double calcSaturation() const
Calculate saturation percentile of the current counter's value with regard to its maximum possible va...
Bitfield< 21, 20 > stride
Bitfield< 9 > d
Definition misc_types.hh:64
Bitfield< 12 > ext
Bitfield< 4 > pc
Bitfield< 0 > p
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
uint16_t RequestorID
Definition request.hh:95
static constexpr auto genTagExtractor(BTBIndexingPolicy *ip)
This helper generates a tag extractor function object which will be typically used by Replaceable ent...
Definition btb_entry.hh:281
Describes a strided prefetcher.
Provides generic cache lookup functions.
TaggedIndexingPolicy *const indexingPolicy
Definition stride.hh:124
replacement_policy::Base *const replacementPolicy
Definition stride.hh:125
Tagged by hashed PCs.
Definition stride.hh:138
StrideEntry(const SatCounter8 &init_confidence, TagExtractor ext)
Definition stride.cc:65
void invalidate() override
Invalidate the block.
Definition stride.cc:74

Generated on Mon Jan 13 2025 04:28:38 for gem5 by doxygen 1.9.8