gem5  v22.1.0.0
base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 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 
47 
48 #include <cassert>
49 
50 #include "base/intmath.hh"
51 #include "mem/cache/base.hh"
52 #include "params/BasePrefetcher.hh"
53 #include "sim/system.hh"
54 
55 namespace gem5
56 {
57 
59 namespace prefetch
60 {
61 
63  : address(addr), pc(pkt->req->hasPC() ? pkt->req->getPC() : 0),
64  requestorId(pkt->req->requestorId()), validPC(pkt->req->hasPC()),
65  secure(pkt->isSecure()), size(pkt->req->getSize()), write(pkt->isWrite()),
66  paddress(pkt->req->getPaddr()), cacheMiss(miss)
67 {
68  unsigned int req_size = pkt->req->getSize();
69  if (!write && miss) {
70  data = nullptr;
71  } else {
72  data = new uint8_t[req_size];
73  Addr offset = pkt->req->getPaddr() - pkt->getAddr();
74  std::memcpy(data, &(pkt->getConstPtr<uint8_t>()[offset]), req_size);
75  }
76 }
77 
79  : address(addr), pc(pfi.pc), requestorId(pfi.requestorId),
80  validPC(pfi.validPC), secure(pfi.secure), size(pfi.size),
81  write(pfi.write), paddress(pfi.paddress), cacheMiss(pfi.cacheMiss),
82  data(nullptr)
83 {
84 }
85 
86 void
88 {
89  if (isFill) {
90  parent.notifyFill(pkt);
91  } else {
92  parent.probeNotify(pkt, miss);
93  }
94 }
95 
96 Base::Base(const BasePrefetcherParams &p)
97  : ClockedObject(p), listeners(), cache(nullptr), blkSize(p.block_size),
98  lBlkSize(floorLog2(blkSize)), onMiss(p.on_miss), onRead(p.on_read),
99  onWrite(p.on_write), onData(p.on_data), onInst(p.on_inst),
100  requestorId(p.sys->getRequestorId(this)),
101  pageBytes(p.page_bytes),
102  prefetchOnAccess(p.prefetch_on_access),
103  prefetchOnPfHit(p.prefetch_on_pf_hit),
104  useVirtualAddresses(p.use_virtual_addresses),
106  usefulPrefetches(0), tlb(nullptr)
107 {
108 }
109 
110 void
112 {
113  assert(!cache);
114  cache = _cache;
115 
116  // If the cache has a different block size from the system's, save it
119 }
120 
122  : statistics::Group(parent),
123  ADD_STAT(demandMshrMisses, statistics::units::Count::get(),
124  "demands not covered by prefetchs"),
125  ADD_STAT(pfIssued, statistics::units::Count::get(),
126  "number of hwpf issued"),
127  ADD_STAT(pfUnused, statistics::units::Count::get(),
128  "number of HardPF blocks evicted w/o reference"),
129  ADD_STAT(pfUseful, statistics::units::Count::get(),
130  "number of useful prefetch"),
131  ADD_STAT(pfUsefulButMiss, statistics::units::Count::get(),
132  "number of hit on prefetch but cache block is not in an usable "
133  "state"),
134  ADD_STAT(accuracy, statistics::units::Count::get(),
135  "accuracy of the prefetcher"),
136  ADD_STAT(coverage, statistics::units::Count::get(),
137  "coverage brought by this prefetcher"),
138  ADD_STAT(pfHitInCache, statistics::units::Count::get(),
139  "number of prefetches hitting in cache"),
140  ADD_STAT(pfHitInMSHR, statistics::units::Count::get(),
141  "number of prefetches hitting in a MSHR"),
142  ADD_STAT(pfHitInWB, statistics::units::Count::get(),
143  "number of prefetches hit in the Write Buffer"),
144  ADD_STAT(pfLate, statistics::units::Count::get(),
145  "number of late prefetches (hitting in cache, MSHR or WB)")
146 {
147  using namespace statistics;
148 
150 
153 
156 
158 }
159 
160 bool
161 Base::observeAccess(const PacketPtr &pkt, bool miss) const
162 {
163  bool fetch = pkt->req->isInstFetch();
164  bool read = pkt->isRead();
165  bool inv = pkt->isInvalidate();
166 
167  if (!miss) {
168  if (prefetchOnPfHit)
169  return hasBeenPrefetched(pkt->getAddr(), pkt->isSecure());
170  if (!prefetchOnAccess)
171  return false;
172  }
173  if (pkt->req->isUncacheable()) return false;
174  if (fetch && !onInst) return false;
175  if (!fetch && !onData) return false;
176  if (!fetch && read && !onRead) return false;
177  if (!fetch && !read && !onWrite) return false;
178  if (!fetch && !read && inv) return false;
179  if (pkt->cmd == MemCmd::CleanEvict) return false;
180 
181  if (onMiss) {
182  return miss;
183  }
184 
185  return true;
186 }
187 
188 bool
189 Base::inCache(Addr addr, bool is_secure) const
190 {
191  return cache->inCache(addr, is_secure);
192 }
193 
194 bool
195 Base::inMissQueue(Addr addr, bool is_secure) const
196 {
197  return cache->inMissQueue(addr, is_secure);
198 }
199 
200 bool
201 Base::hasBeenPrefetched(Addr addr, bool is_secure) const
202 {
203  return cache->hasBeenPrefetched(addr, is_secure);
204 }
205 
206 bool
208 {
209  return roundDown(a, pageBytes) == roundDown(b, pageBytes);
210 }
211 
212 Addr
214 {
215  return a & ~((Addr)blkSize-1);
216 }
217 
218 Addr
220 {
221  return a >> lBlkSize;
222 }
223 
224 Addr
226 {
227  return roundDown(a, pageBytes);
228 }
229 
230 Addr
232 {
233  return a & (pageBytes - 1);
234 }
235 
236 Addr
238 {
239  return page + (blockIndex << lBlkSize);
240 }
241 
242 void
243 Base::probeNotify(const PacketPtr &pkt, bool miss)
244 {
245  // Don't notify prefetcher on SWPrefetch, cache maintenance
246  // operations or for writes that we are coaslescing.
247  if (pkt->cmd.isSWPrefetch()) return;
248  if (pkt->req->isCacheMaintenance()) return;
249  if (pkt->isWrite() && cache != nullptr && cache->coalesce()) return;
250  if (!pkt->req->hasPaddr()) {
251  panic("Request must have a physical address");
252  }
253 
254  if (hasBeenPrefetched(pkt->getAddr(), pkt->isSecure())) {
255  usefulPrefetches += 1;
257  if (miss)
258  // This case happens when a demand hits on a prefetched line
259  // that's not in the requested coherency state.
261  }
262 
263  // Verify this access type is observed by prefetcher
264  if (observeAccess(pkt, miss)) {
265  if (useVirtualAddresses && pkt->req->hasVaddr()) {
266  PrefetchInfo pfi(pkt, pkt->req->getVaddr(), miss);
267  notify(pkt, pfi);
268  } else if (!useVirtualAddresses) {
269  PrefetchInfo pfi(pkt, pkt->req->getPaddr(), miss);
270  notify(pkt, pfi);
271  }
272  }
273 }
274 
275 void
277 {
283  if (listeners.empty() && cache != nullptr) {
285  listeners.push_back(new PrefetchListener(*this, pm, "Miss", false,
286  true));
287  listeners.push_back(new PrefetchListener(*this, pm, "Fill", true,
288  false));
289  listeners.push_back(new PrefetchListener(*this, pm, "Hit", false,
290  false));
291  }
292 }
293 
294 void
296 {
297  ProbeManager *pm(obj->getProbeManager());
298  listeners.push_back(new PrefetchListener(*this, pm, name));
299 }
300 
301 void
303 {
304  fatal_if(tlb != nullptr, "Only one TLB can be registered");
305  tlb = t;
306 }
307 
308 } // namespace prefetch
309 } // namespace gem5
const char data[]
A basic cache interface.
Definition: base.hh:96
bool inMissQueue(Addr addr, bool is_secure) const
Definition: base.hh:1292
bool hasBeenPrefetched(Addr addr, bool is_secure) const
Definition: base.hh:1283
unsigned getBlockSize() const
Query block size of a cache.
Definition: base.hh:1166
bool inCache(Addr addr, bool is_secure) const
Definition: base.hh:1279
bool coalesce() const
Checks if the cache is coalescing writes.
Definition: base.cc:1806
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
bool isSWPrefetch() const
Definition: packet.hh:252
@ CleanEvict
Definition: packet.hh:95
virtual std::string name() const
Definition: named.hh:47
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
bool isRead() const
Definition: packet.hh:592
bool isSecure() const
Definition: packet.hh:834
Addr getAddr() const
Definition: packet.hh:805
bool isWrite() const
Definition: packet.hh:593
RequestPtr req
A pointer to the original request.
Definition: packet.hh:376
const T * getConstPtr() const
Definition: packet.hh:1221
MemCmd cmd
The command field of the packet.
Definition: packet.hh:371
bool isInvalidate() const
Definition: packet.hh:608
ProbeManager is a conduit class that lives on each SimObject, and is used to match up probe listeners...
Definition: probe.hh:164
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
Class containing the information needed by the prefetch to train and generate new prefetch requests.
Definition: base.hh:98
PrefetchInfo(PacketPtr pkt, Addr addr, bool miss)
Constructs a PrefetchInfo using a PacketPtr.
Definition: base.cc:62
bool write
Whether this event comes from a write request.
Definition: base.hh:112
uint8_t * data
Pointer to the associated request data.
Definition: base.hh:118
void notify(const PacketPtr &pkt) override
Definition: base.cc:87
Base(const BasePrefetcherParams &p)
Definition: base.cc:96
const bool useVirtualAddresses
Use Virtual Addresses for prefetching.
Definition: base.hh:302
void addTLB(BaseTLB *tlb)
Add a BaseTLB object to be used whenever a translation is needed.
Definition: base.cc:302
const bool prefetchOnAccess
Prefetch on every access, not just misses.
Definition: base.hh:296
const bool onRead
Consult prefetcher on reads?
Definition: base.hh:279
unsigned blkSize
The block size of the parent cache.
Definition: base.hh:270
uint64_t issuedPrefetches
Total prefetches issued.
Definition: base.hh:363
bool observeAccess(const PacketPtr &pkt, bool miss) const
Determine if this access should be observed.
Definition: base.cc:161
bool inCache(Addr addr, bool is_secure) const
Determine if address is in cache.
Definition: base.cc:189
void addEventProbe(SimObject *obj, const char *name)
Add a SimObject and a probe name to listen events from.
Definition: base.cc:295
const RequestorID requestorId
Request id for prefetches.
Definition: base.hh:291
std::vector< PrefetchListener * > listeners
Definition: base.hh:89
BaseTLB * tlb
Registered tlb for address translations.
Definition: base.hh:368
void regProbeListeners() override
Register probe points for this object.
Definition: base.cc:276
Addr pageAddress(Addr a) const
Determine the address of the page in which a lays.
Definition: base.cc:225
bool inMissQueue(Addr addr, bool is_secure) const
Determine if address is in cache miss queue.
Definition: base.cc:195
bool hasBeenPrefetched(Addr addr, bool is_secure) const
Definition: base.cc:201
const bool onInst
Consult prefetcher on instruction accesses?
Definition: base.hh:288
uint64_t usefulPrefetches
Total prefetches that has been useful.
Definition: base.hh:365
gem5::prefetch::Base::StatGroup prefetchStats
Addr pageIthBlockAddress(Addr page, uint32_t i) const
Build the address of the i-th block inside the page.
Definition: base.cc:237
BaseCache * cache
Pointr to the parent cache.
Definition: base.hh:267
Addr pageOffset(Addr a) const
Determine the page-offset of a
Definition: base.cc:231
virtual void setCache(BaseCache *_cache)
Definition: base.cc:111
const bool onData
Consult prefetcher on data accesses?
Definition: base.hh:285
const bool prefetchOnPfHit
Prefetch on hit on prefetched lines.
Definition: base.hh:299
void probeNotify(const PacketPtr &pkt, bool miss)
Process a notification event from the ProbeListener.
Definition: base.cc:243
const bool onWrite
Consult prefetcher on reads?
Definition: base.hh:282
virtual void notify(const PacketPtr &pkt, const PrefetchInfo &pfi)=0
Notify prefetcher of cache access (may be any access or just misses, depending on cache parameters....
unsigned lBlkSize
log_2(block size of the parent cache).
Definition: base.hh:273
const Addr pageBytes
Definition: base.hh:293
Addr blockIndex(Addr a) const
Determine the address of a at block granularity.
Definition: base.cc:219
Addr blockAddress(Addr a) const
Determine the address of the block in which a lays.
Definition: base.cc:213
const bool onMiss
Only consult prefetcher on cache misses?
Definition: base.hh:276
bool samePage(Addr a, Addr b) const
Determine if addresses are on the same page.
Definition: base.cc:207
Derived & flags(Flags _flags)
Set the flags and marks this stat to print at the end of simulation.
Definition: statistics.hh:358
Statistics container.
Definition: group.hh:94
#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:178
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
ProbeManager * getProbeManager()
Get the probe manager for this object.
Definition: sim_object.cc:120
Declares a basic cache interface BaseCache.
Miss and writeback queue declarations.
bool isSecure(ThreadContext *tc)
Definition: utility.cc:74
Bitfield< 7 > b
Definition: misc_types.hh:388
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 8 > a
Definition: misc_types.hh:66
Bitfield< 4 > pc
Bitfield< 51 > t
Definition: pagetable.hh:56
Bitfield< 54 > p
Definition: pagetable.hh:70
Bitfield< 23 > inv
Definition: misc.hh:815
Bitfield< 3 > addr
Definition: types.hh:84
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:68
const FlagsType total
Print the total.
Definition: info.hh:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
statistics::Formula accuracy
Definition: base.hh:344
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:343
statistics::Scalar pfIssued
Definition: base.hh:335
statistics::Scalar pfUseful
The number of times a HW-prefetch is useful.
Definition: base.hh:340
statistics::Scalar pfHitInWB
The number of times a HW-prefetch hits in the Write Buffer (WB).
Definition: base.hh:355
statistics::Scalar pfHitInMSHR
The number of times a HW-prefetch hits in a MSHR.
Definition: base.hh:351
statistics::Formula pfLate
The number of times a HW-prefetch is late (hit in cache, MSHR, WB).
Definition: base.hh:359
statistics::Scalar pfUnused
The number of times a HW-prefetched block is evicted w/o reference.
Definition: base.hh:338
statistics::Scalar pfHitInCache
The number of times a HW-prefetch hits in cache.
Definition: base.hh:348
statistics::Scalar demandMshrMisses
Definition: base.hh:334
StatGroup(statistics::Group *parent)
Definition: base.cc:121
statistics::Formula coverage
Definition: base.hh:345

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1