gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
base.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013-2014, 2022-2025 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
45
47
48#include <cassert>
49
50#include "base/intmath.hh"
51#include "params/BasePrefetcher.hh"
52#include "sim/system.hh"
53
54namespace gem5
55{
56
57namespace prefetch
58{
59
61 : address(addr), pc(pkt->req->hasPC() ? pkt->req->getPC() : 0),
62 requestorId(pkt->req->requestorId()), validPC(pkt->req->hasPC()),
63 secure(pkt->isSecure()), size(pkt->req->getSize()), write(pkt->isWrite()),
64 paddress(pkt->req->getPaddr()), cacheMiss(miss)
65{
66 unsigned int req_size = pkt->req->getSize();
67 if ((!write && miss) || !pkt->hasData()) {
68 data = nullptr;
69 } else {
70 data = new uint8_t[req_size];
71 Addr offset = pkt->req->getPaddr() - pkt->getAddr();
72 std::memcpy(data, &(pkt->getConstPtr<uint8_t>()[offset]), req_size);
73 }
74}
75
77 : address(addr), pc(pfi.pc), requestorId(pfi.requestorId),
78 validPC(pfi.validPC), secure(pfi.secure), size(pfi.size),
80 data(nullptr)
81{
82}
83
84void
86{
87 if (isFill) {
88 parent.notifyFill(arg);
89 } else {
90 parent.probeNotify(arg, miss);
91 }
92}
93
94void
96{
97 if (info.newData.empty())
98 parent.notifyEvict(info);
99}
100
101Base::Base(const BasePrefetcherParams &p)
102 : ClockedObject(p), listeners(), system(nullptr), probeManager(nullptr),
103 blkSize(p.block_size), lBlkSize(floorLog2(blkSize)),
104 onMiss(p.on_miss), onRead(p.on_read),
105 onWrite(p.on_write), onData(p.on_data), onInst(p.on_inst),
106 requestorId(p.sys->getRequestorId(this)),
107 pageBytes(p.page_bytes),
108 prefetchOnAccess(p.prefetch_on_access),
109 prefetchOnPfHit(p.prefetch_on_pf_hit),
110 useVirtualAddresses(p.use_virtual_addresses),
112 usefulPrefetches(0), mmu(nullptr)
113{
114}
115
116void
117Base::setParentInfo(System *sys, ProbeManager *pm, unsigned blk_size)
118{
119 assert(!system && !probeManager);
120 system = sys;
121 probeManager = pm;
122 // If the cache has a different block size from the system's, save it
123 blkSize = blk_size;
125}
126
128 : statistics::Group(parent),
130 "demands not covered by prefetchs"),
131 ADD_STAT(pfIssued, statistics::units::Count::get(),
132 "number of hwpf issued"),
133 ADD_STAT(pfUnused, statistics::units::Count::get(),
134 "number of HardPF blocks evicted w/o reference"),
135 ADD_STAT(pfUseful, statistics::units::Count::get(),
136 "number of useful prefetch"),
138 "number of hit on prefetch but cache block is not in an usable "
139 "state"),
140 ADD_STAT(accuracy, statistics::units::Count::get(),
141 "accuracy of the prefetcher"),
142 ADD_STAT(coverage, statistics::units::Count::get(),
143 "coverage brought by this prefetcher"),
144 ADD_STAT(pfHitInCache, statistics::units::Count::get(),
145 "number of prefetches hitting in cache"),
146 ADD_STAT(pfHitInMSHR, statistics::units::Count::get(),
147 "number of prefetches hitting in a MSHR"),
148 ADD_STAT(pfHitInWB, statistics::units::Count::get(),
149 "number of prefetches hit in the Write Buffer"),
150 ADD_STAT(pfLate, statistics::units::Count::get(),
151 "number of late prefetches (hitting in cache, MSHR or WB)")
152{
153 using namespace statistics;
154
155 pfUnused.flags(nozero);
156
157 accuracy.flags(total);
159
160 coverage.flags(total);
162
164}
165
166bool
167Base::observeAccess(const PacketPtr &pkt, bool miss, bool prefetched) const
168{
169 bool fetch = pkt->req->isInstFetch();
170 bool read = pkt->isRead();
171 bool inv = pkt->isInvalidate();
172
173 if (!miss) {
174 if (prefetchOnPfHit)
175 return prefetched;
176 if (!prefetchOnAccess)
177 return false;
178 }
179 if (pkt->req->isUncacheable()) return false;
180 if (fetch && !onInst) return false;
181 if (!fetch && !onData) return false;
182 if (!fetch && read && !onRead) return false;
183 if (!fetch && !read && !onWrite) return false;
184 if (!fetch && !read && inv) return false;
185 if (pkt->cmd == MemCmd::CleanEvict) return false;
186
187 if (onMiss) {
188 return miss;
189 }
190
191 return true;
192}
193
194bool
196{
198}
199
200Addr
202{
203 return a & ~((Addr)blkSize-1);
204}
205
206Addr
208{
209 return a >> lBlkSize;
210}
211
212Addr
214{
215 return roundDown(a, pageBytes);
216}
217
218Addr
220{
221 return a & (pageBytes - 1);
222}
223
224Addr
226{
227 return page + (blockIndex << lBlkSize);
228}
229
230void
232{
233 const PacketPtr pkt = acc.pkt;
234 const CacheAccessor &cache = acc.cache;
235
236 // Don't notify prefetcher on SWPrefetch, cache maintenance
237 // operations or for writes that we are coaslescing.
238 if (pkt->cmd.isSWPrefetch()) return;
239 if (pkt->req->isCacheMaintenance()) return;
240 if (pkt->isCleanEviction()) return;
241 if (pkt->isWrite() && cache.coalesce()) return;
242 if (!pkt->req->hasPaddr()) {
243 panic("Request must have a physical address");
244 }
245
246 bool has_been_prefetched =
247 acc.cache.hasBeenPrefetched(pkt->getAddr(), pkt->isSecure(),
249 if (has_been_prefetched) {
250 usefulPrefetches += 1;
251 prefetchStats.pfUseful++;
252 if (miss)
253 // This case happens when a demand hits on a prefetched line
254 // that's not in the requested coherency state.
255 prefetchStats.pfUsefulButMiss++;
256 }
257
258 // Verify this access type is observed by prefetcher
259 if (observeAccess(pkt, miss, has_been_prefetched)) {
260 if (useVirtualAddresses && pkt->req->hasVaddr()) {
261 PrefetchInfo pfi(pkt, pkt->req->getVaddr(), miss);
262 notify(acc, pfi);
263 } else if (!useVirtualAddresses) {
264 PrefetchInfo pfi(pkt, pkt->req->getPaddr(), miss);
265 notify(acc, pfi);
266 }
267 }
268}
269
270void
272{
278 if (listeners.empty() && probeManager != nullptr) {
279 listeners.push_back(probeManager->connect<PrefetchListener>(
280 *this, "Miss", false, true));
281 listeners.push_back(probeManager->connect<PrefetchListener>(
282 *this, "Fill", true, false));
283 listeners.push_back(probeManager->connect<PrefetchListener>(
284 *this, "Hit", false, false));
285 listeners.push_back(probeManager->connect<PrefetchEvictListener>(
286 *this, "Data Update"));
287 }
288}
289
290void
292{
293 ProbeManager *pm = obj->getProbeManager();
294 listeners.push_back(pm->connect<PrefetchListener>(*this, name));
295}
296
297void
299{
300 fatal_if(mmu != nullptr, "Only one MMU can be registered");
301 mmu = m;
302}
303
304} // namespace prefetch
305} // namespace gem5
Information provided to probes on a cache event.
PacketPtr pkt
Packet that triggered the cache access.
CacheAccessor & cache
Accessor for the cache.
ClockedObject(const ClockedObjectParams &p)
bool isSWPrefetch() const
Definition packet.hh:253
virtual std::string name() const
Definition named.hh:60
bool isRead() const
Definition packet.hh:593
bool isSecure() const
Definition packet.hh:836
Addr getAddr() const
Definition packet.hh:807
bool isCleanEviction() const
Is this packet a clean eviction, including both actual clean evict packets, but also clean writebacks...
Definition packet.hh:1435
bool hasData() const
Definition packet.hh:614
bool isWrite() const
Definition packet.hh:594
RequestPtr req
A pointer to the original request.
Definition packet.hh:377
MemCmd cmd
The command field of the packet.
Definition packet.hh:372
bool isInvalidate() const
Definition packet.hh:609
ProbeManager is a conduit class that lives on each SimObject, and is used to match up probe listeners...
Definition probe.hh:162
ProbeListenerPtr< Listener > connect(Args &&...args)
Definition probe.hh:198
void notify(const EvictionInfo &info) override
Definition base.cc:95
Class containing the information needed by the prefetch to train and generate new prefetch requests.
Definition base.hh:113
bool validPC
Validity bit for the PC of this address.
Definition base.hh:121
PrefetchInfo(PacketPtr pkt, Addr addr, bool miss)
Constructs a PrefetchInfo using a PacketPtr.
Definition base.cc:60
Addr getPC() const
Returns the program counter that generated this request.
Definition base.hh:158
bool isSecure() const
Returns true if the address targets the secure memory space.
Definition base.hh:149
unsigned int getSize() const
Gets the size of the request triggering this event.
Definition base.hh:186
Addr getPaddr() const
Gets the physical address of the request.
Definition base.hh:205
bool write
Whether this event comes from a write request.
Definition base.hh:127
bool isWrite() const
Checks if the request that caused this prefetch event was a write request.
Definition base.hh:196
RequestorID requestorId
The requestor ID that generated this address.
Definition base.hh:119
Addr address
The address used to train and generate prefetches.
Definition base.hh:115
bool cacheMiss
Whether this event comes from a cache miss.
Definition base.hh:131
Addr paddress
Physical address, needed because address can be virtual.
Definition base.hh:129
bool hasPC() const
Returns true if the associated program counter is valid.
Definition base.hh:168
Addr pc
The program counter that generated this address.
Definition base.hh:117
unsigned int size
Size in bytes of the request triggering this event.
Definition base.hh:125
uint8_t * data
Pointer to the associated request data.
Definition base.hh:133
bool secure
Whether this address targets the secure memory space.
Definition base.hh:123
void notify(const CacheAccessProbeArg &arg) override
Definition base.cc:85
Base(const BasePrefetcherParams &p)
Definition base.cc:101
const bool useVirtualAddresses
Use Virtual Addresses for prefetching.
Definition base.hh:320
const bool prefetchOnAccess
Prefetch on every access, not just misses.
Definition base.hh:314
const bool onRead
Consult prefetcher on reads?
Definition base.hh:297
unsigned blkSize
The block size of the parent cache.
Definition base.hh:288
uint64_t issuedPrefetches
Total prefetches issued.
Definition base.hh:374
void addEventProbe(SimObject *obj, const char *name)
Add a SimObject and a probe name to listen events from.
Definition base.cc:291
std::vector< ProbeListenerPtr<> > listeners
Definition base.hh:104
const RequestorID requestorId
Request id for prefetches.
Definition base.hh:309
void addMMU(BaseMMU *mmu)
Add a BaseMMU object to be used whenever a translation is needed.
Definition base.cc:298
void regProbeListeners() override
Register probe points for this object.
Definition base.cc:271
Addr pageAddress(Addr a) const
Determine the address of the page in which a lays.
Definition base.cc:213
ProbeManager * probeManager
Pointer to the parent cache's probe manager.
Definition base.hh:285
const bool onInst
Consult prefetcher on instruction accesses?
Definition base.hh:306
uint64_t usefulPrefetches
Total prefetches that has been useful.
Definition base.hh:376
gem5::prefetch::Base::StatGroup prefetchStats
virtual void setParentInfo(System *sys, ProbeManager *pm, unsigned blk_size)
Definition base.cc:117
virtual void notify(const CacheAccessProbeArg &acc, const PrefetchInfo &pfi)=0
Notify prefetcher of cache access (may be any access or just misses, depending on cache parameters....
CacheDataUpdateProbeArg EvictionInfo
Definition base.hh:91
Addr pageIthBlockAddress(Addr page, uint32_t i) const
Build the address of the i-th block inside the page.
Definition base.cc:225
Addr pageOffset(Addr a) const
Determine the page-offset of a.
Definition base.cc:219
BaseMMU * mmu
Registered mmu for address translations.
Definition base.hh:379
const bool onData
Consult prefetcher on data accesses?
Definition base.hh:303
const bool prefetchOnPfHit
Prefetch on hit on prefetched lines.
Definition base.hh:317
System * system
Pointer to the parent system.
Definition base.hh:282
const bool onWrite
Consult prefetcher on reads?
Definition base.hh:300
unsigned lBlkSize
log_2(block size of the parent cache).
Definition base.hh:291
bool observeAccess(const PacketPtr &pkt, bool miss, bool prefetched) const
Determine if this access should be observed.
Definition base.cc:167
const Addr pageBytes
Definition base.hh:311
Addr blockIndex(Addr a) const
Determine the address of a at block granularity.
Definition base.cc:207
Addr blockAddress(Addr a) const
Determine the address of the block in which a lays.
Definition base.cc:201
void probeNotify(const CacheAccessProbeArg &acc, bool miss)
Process a notification event from the ProbeListener.
Definition base.cc:231
const bool onMiss
Only consult prefetcher on cache misses?
Definition base.hh:294
bool samePage(Addr a, Addr b) const
Determine if addresses are on the same page.
Definition base.cc:195
Statistics container.
Definition group.hh:93
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition group.hh:75
static constexpr std::enable_if_t< std::is_integral_v< T >, int > floorLog2(T x)
Definition intmath.hh:59
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition intmath.hh:279
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:268
SimObject(const Params &p)
Definition sim_object.cc:58
ProbeManager * getProbeManager()
Get the probe manager for this object.
Miss and writeback queue declarations.
Bitfield< 7 > b
Bitfield< 8 > a
Definition misc_types.hh:66
Bitfield< 0 > m
Bitfield< 0 > p
Bitfield< 23 > inv
Definition misc.hh:843
Bitfield< 3 > addr
Definition types.hh:84
Units for Stats.
Definition units.hh:113
const FlagsType nozero
Don't print if this is zero.
Definition info.hh:67
const FlagsType total
Print the total.
Definition info.hh:59
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
Packet * PacketPtr
Provides generic cache lookup functions.
virtual bool coalesce() const =0
Determine if cache is coalescing writes.
virtual bool hasBeenPrefetched(Addr addr, bool is_secure) const =0
Determine if address has been prefetched.
std::vector< uint64_t > newData
The new data contents.
statistics::Formula accuracy
Definition base.hh:355
statistics::Scalar pfUsefulButMiss
The number of times there is a hit on prefetch but cache block is not in an usable state.
Definition base.hh:354
statistics::Scalar pfIssued
Definition base.hh:346
statistics::Scalar pfUseful
The number of times a HW-prefetch is useful.
Definition base.hh:351
statistics::Scalar pfHitInWB
The number of times a HW-prefetch hits in the Write Buffer (WB).
Definition base.hh:366
statistics::Scalar pfHitInMSHR
The number of times a HW-prefetch hits in a MSHR.
Definition base.hh:362
statistics::Formula pfLate
The number of times a HW-prefetch is late (hit in cache, MSHR, WB).
Definition base.hh:370
statistics::Scalar pfUnused
The number of times a HW-prefetched block is evicted w/o reference.
Definition base.hh:349
statistics::Scalar pfHitInCache
The number of times a HW-prefetch hits in cache.
Definition base.hh:359
statistics::Scalar demandMshrMisses
Definition base.hh:345
StatGroup(statistics::Group *parent)
Definition base.cc:127
statistics::Formula coverage
Definition base.hh:356

Generated on Mon Oct 27 2025 04:13:00 for gem5 by doxygen 1.14.0