queued.cc revision 10942:224c85495f96
1/* 2 * Copyright (c) 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 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Mitch Hayenga 38 */ 39 40#include "debug/HWPrefetch.hh" 41#include "mem/cache/prefetch/queued.hh" 42#include "mem/cache/base.hh" 43 44QueuedPrefetcher::QueuedPrefetcher(const QueuedPrefetcherParams *p) 45 : BasePrefetcher(p), queueSize(p->queue_size), latency(p->latency), 46 queueSquash(p->queue_squash), queueFilter(p->queue_filter), 47 cacheSnoop(p->cache_snoop), tagPrefetch(p->tag_prefetch) 48{ 49 50} 51 52QueuedPrefetcher::~QueuedPrefetcher() 53{ 54 // Delete the queued prefetch packets 55 for (DeferredPacket &p : pfq) { 56 delete p.pkt->req; 57 delete p.pkt; 58 } 59} 60 61Tick 62QueuedPrefetcher::notify(const PacketPtr &pkt) 63{ 64 // Verify this access type is observed by prefetcher 65 if (observeAccess(pkt)) { 66 Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize - 1); 67 bool is_secure = pkt->isSecure(); 68 69 // Squash queued prefetches if demand miss to same line 70 if (queueSquash) { 71 auto itr = pfq.begin(); 72 while (itr != pfq.end()) { 73 if (itr->pkt->getAddr() == blk_addr && 74 itr->pkt->isSecure() == is_secure) { 75 delete itr->pkt->req; 76 delete itr->pkt; 77 itr = pfq.erase(itr); 78 } else { 79 ++itr; 80 } 81 } 82 } 83 84 // Calculate prefetches given this access 85 std::vector<Addr> addresses; 86 calculatePrefetch(pkt, addresses); 87 88 // Queue up generated prefetches 89 for (Addr pf_addr : addresses) { 90 91 // Block align prefetch address 92 pf_addr = pf_addr & ~(Addr)(blkSize - 1); 93 94 pfIdentified++; 95 DPRINTF(HWPrefetch, "Found a pf candidate addr: %#x, " 96 "inserting into prefetch queue.\n", pf_addr); 97 98 if (queueFilter && inPrefetch(pf_addr, is_secure)) { 99 pfBufferHit++; 100 DPRINTF(HWPrefetch, "Prefetch addr already in " 101 "prefetch queue\n"); 102 continue; 103 } 104 105 if (cacheSnoop && (inCache(pf_addr, is_secure) || 106 inMissQueue(pf_addr, is_secure))) { 107 pfInCache++; 108 DPRINTF(HWPrefetch, "Dropping redundant in " 109 "cache/MSHR prefetch addr:%#x\n", pf_addr); 110 continue; 111 } 112 113 // Create a prefetch memory request 114 Request *pf_req = 115 new Request(pf_addr, blkSize, 0, masterId); 116 117 if (is_secure) { 118 pf_req->setFlags(Request::SECURE); 119 } 120 pf_req->taskId(ContextSwitchTaskId::Prefetcher); 121 PacketPtr pf_pkt = new Packet(pf_req, MemCmd::HardPFReq); 122 pf_pkt->allocate(); 123 124 if (pkt->req->hasContextId()) { 125 pf_req->setThreadContext(pkt->req->contextId(), 126 pkt->req->threadId()); 127 } 128 129 if (tagPrefetch && pkt->req->hasPC()) { 130 // Tag prefetch packet with accessing pc 131 pf_pkt->req->setPC(pkt->req->getPC()); 132 } 133 134 // Verify prefetch buffer space for request 135 if (pfq.size() == queueSize) { 136 pfRemovedFull++; 137 PacketPtr old_pkt = pfq.begin()->pkt; 138 DPRINTF(HWPrefetch, "Prefetch queue full, removing " 139 "oldest packet addr: %#x", old_pkt->getAddr()); 140 delete old_pkt->req; 141 delete old_pkt; 142 pfq.pop_front(); 143 } 144 145 Tick pf_time = curTick() + clockPeriod() * latency; 146 DPRINTF(HWPrefetch, "Prefetch queued. " 147 "addr:%#x tick:%lld.\n", pf_addr, pf_time); 148 149 pfq.emplace_back(pf_time, pf_pkt); 150 } 151 } 152 153 return pfq.empty() ? MaxTick : pfq.front().tick; 154} 155 156PacketPtr 157QueuedPrefetcher::getPacket() 158{ 159 DPRINTF(HWPrefetch, "Requesting a prefetch to issue.\n"); 160 161 if (pfq.empty()) { 162 DPRINTF(HWPrefetch, "No hardware prefetches available.\n"); 163 return NULL; 164 } 165 166 PacketPtr pkt = pfq.begin()->pkt; 167 pfq.pop_front(); 168 169 pfIssued++; 170 assert(pkt != NULL); 171 DPRINTF(HWPrefetch, "Generating prefetch for %#x.\n", pkt->getAddr()); 172 return pkt; 173} 174 175bool 176QueuedPrefetcher::inPrefetch(Addr address, bool is_secure) const 177{ 178 for (const DeferredPacket &dp : pfq) { 179 if (dp.pkt->getAddr() == address && 180 dp.pkt->isSecure() == is_secure) return true; 181 } 182 183 return false; 184} 185 186void 187QueuedPrefetcher::regStats() 188{ 189 BasePrefetcher::regStats(); 190 191 pfIdentified 192 .name(name() + ".pfIdentified") 193 .desc("number of prefetch candidates identified"); 194 195 pfBufferHit 196 .name(name() + ".pfBufferHit") 197 .desc("number of redundant prefetches already in prefetch queue"); 198 199 pfInCache 200 .name(name() + ".pfInCache") 201 .desc("number of redundant prefetches already in cache/mshr dropped"); 202 203 pfRemovedFull 204 .name(name() + ".pfRemovedFull") 205 .desc("number of prefetches dropped due to prefetch queue size"); 206 207 pfSpanPage 208 .name(name() + ".pfSpanPage") 209 .desc("number of prefetches not generated due to page crossing"); 210} 211