base.cc revision 8533
12810SN/A/* 22810SN/A * Copyright (c) 2005 The Regents of The University of Michigan 32810SN/A * All rights reserved. 42810SN/A * 52810SN/A * Redistribution and use in source and binary forms, with or without 62810SN/A * modification, are permitted provided that the following conditions are 72810SN/A * met: redistributions of source code must retain the above copyright 82810SN/A * notice, this list of conditions and the following disclaimer; 92810SN/A * redistributions in binary form must reproduce the above copyright 102810SN/A * notice, this list of conditions and the following disclaimer in the 112810SN/A * documentation and/or other materials provided with the distribution; 122810SN/A * neither the name of the copyright holders nor the names of its 132810SN/A * contributors may be used to endorse or promote products derived from 142810SN/A * this software without specific prior written permission. 152810SN/A * 162810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272810SN/A * 282810SN/A * Authors: Ron Dreslinski 292810SN/A */ 302810SN/A 312810SN/A/** 322810SN/A * @file 332810SN/A * Hardware Prefetcher Definition. 342810SN/A */ 352810SN/A 366658Snate@binkert.org#include <list> 376658Snate@binkert.org 385875Ssteve.reinhardt@amd.com#include "arch/isa_traits.hh" 392810SN/A#include "base/trace.hh" 406658Snate@binkert.org#include "config/the_isa.hh" 418232Snate@binkert.org#include "debug/HWPrefetch.hh" 428229Snate@binkert.org#include "mem/cache/prefetch/base.hh" 435338Sstever@gmail.com#include "mem/cache/base.hh" 442814SN/A#include "mem/request.hh" 452810SN/A 465034SN/ABasePrefetcher::BasePrefetcher(const BaseCacheParams *p) 475034SN/A : size(p->prefetcher_size), pageStop(!p->prefetch_past_page), 485034SN/A serialSquash(p->prefetch_serial_squash), 495875Ssteve.reinhardt@amd.com onlyData(p->prefetch_data_accesses_only) 502810SN/A{ 512810SN/A} 522810SN/A 532810SN/Avoid 542810SN/ABasePrefetcher::setCache(BaseCache *_cache) 552810SN/A{ 562810SN/A cache = _cache; 572810SN/A blkSize = cache->getBlockSize(); 585875Ssteve.reinhardt@amd.com _name = cache->name() + "-pf"; 592810SN/A} 602810SN/A 612810SN/Avoid 622810SN/ABasePrefetcher::regStats(const std::string &name) 632810SN/A{ 642810SN/A pfIdentified 652810SN/A .name(name + ".prefetcher.num_hwpf_identified") 662810SN/A .desc("number of hwpf identified") 672810SN/A ; 682810SN/A 692810SN/A pfMSHRHit 702810SN/A .name(name + ".prefetcher.num_hwpf_already_in_mshr") 712810SN/A .desc("number of hwpf that were already in mshr") 722810SN/A ; 732810SN/A 742810SN/A pfCacheHit 752810SN/A .name(name + ".prefetcher.num_hwpf_already_in_cache") 762810SN/A .desc("number of hwpf that were already in the cache") 772810SN/A ; 782810SN/A 792810SN/A pfBufferHit 802810SN/A .name(name + ".prefetcher.num_hwpf_already_in_prefetcher") 812810SN/A .desc("number of hwpf that were already in the prefetch queue") 822810SN/A ; 832810SN/A 842810SN/A pfRemovedFull 852810SN/A .name(name + ".prefetcher.num_hwpf_evicted") 862810SN/A .desc("number of hwpf removed due to no buffer left") 872810SN/A ; 882810SN/A 892810SN/A pfRemovedMSHR 902810SN/A .name(name + ".prefetcher.num_hwpf_removed_MSHR_hit") 912810SN/A .desc("number of hwpf removed because MSHR allocated") 922810SN/A ; 932810SN/A 942810SN/A pfIssued 952810SN/A .name(name + ".prefetcher.num_hwpf_issued") 962810SN/A .desc("number of hwpf issued") 972810SN/A ; 982810SN/A 992810SN/A pfSpanPage 1002810SN/A .name(name + ".prefetcher.num_hwpf_span_page") 1012810SN/A .desc("number of hwpf spanning a virtual page") 1022810SN/A ; 1032810SN/A 1042810SN/A pfSquashed 1052810SN/A .name(name + ".prefetcher.num_hwpf_squashed_from_miss") 1065875Ssteve.reinhardt@amd.com .desc("number of hwpf that got squashed due to a miss " 1075875Ssteve.reinhardt@amd.com "aborting calculation time") 1082810SN/A ; 1092810SN/A} 1102810SN/A 1113861SN/Ainline bool 1123861SN/ABasePrefetcher::inCache(Addr addr) 1133861SN/A{ 1143861SN/A if (cache->inCache(addr)) { 1153861SN/A pfCacheHit++; 1163861SN/A return true; 1173861SN/A } 1183861SN/A return false; 1193861SN/A} 1203861SN/A 1213861SN/Ainline bool 1223861SN/ABasePrefetcher::inMissQueue(Addr addr) 1233861SN/A{ 1243861SN/A if (cache->inMissQueue(addr)) { 1253861SN/A pfMSHRHit++; 1263861SN/A return true; 1273861SN/A } 1283861SN/A return false; 1293861SN/A} 1303861SN/A 1313349SN/APacketPtr 1322810SN/ABasePrefetcher::getPacket() 1332810SN/A{ 1345875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Requesting a hw_pf to issue\n"); 1352810SN/A 1362810SN/A if (pf.empty()) { 1375875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "No HW_PF found\n"); 1382810SN/A return NULL; 1392810SN/A } 1402810SN/A 1418533SLisa.Hsu@amd.com PacketPtr pkt = *pf.begin(); 1428509SAli.Saidi@ARM.com while (!pf.empty()) { 1432810SN/A pkt = *pf.begin(); 1442810SN/A pf.pop_front(); 1455875Ssteve.reinhardt@amd.com 1468509SAli.Saidi@ARM.com Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1); 1478509SAli.Saidi@ARM.com 1488509SAli.Saidi@ARM.com if (!inCache(blk_addr) && !inMissQueue(blk_addr)) 1498509SAli.Saidi@ARM.com // we found a prefetch, return it 1508509SAli.Saidi@ARM.com break; 1518509SAli.Saidi@ARM.com 1528509SAli.Saidi@ARM.com DPRINTF(HWPrefetch, "addr 0x%x in cache, skipping\n", pkt->getAddr()); 1538509SAli.Saidi@ARM.com delete pkt->req; 1548509SAli.Saidi@ARM.com delete pkt; 1555875Ssteve.reinhardt@amd.com 1562810SN/A if (pf.empty()) { 1574628SN/A cache->deassertMemSideBusRequest(BaseCache::Request_PF); 1588509SAli.Saidi@ARM.com return NULL; // None left, all were in cache 1592810SN/A } 1608509SAli.Saidi@ARM.com } 1612810SN/A 1622810SN/A pfIssued++; 1635875Ssteve.reinhardt@amd.com assert(pkt != NULL); 1645875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "returning 0x%x\n", pkt->getAddr()); 1652810SN/A return pkt; 1662810SN/A} 1672810SN/A 1685875Ssteve.reinhardt@amd.com 1695875Ssteve.reinhardt@amd.comTick 1705875Ssteve.reinhardt@amd.comBasePrefetcher::notify(PacketPtr &pkt, Tick time) 1712810SN/A{ 1726105Ssteve.reinhardt@amd.com if (!pkt->req->isUncacheable() && !(pkt->req->isInstFetch() && onlyData)) { 1735875Ssteve.reinhardt@amd.com // Calculate the blk address 1745875Ssteve.reinhardt@amd.com Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1); 1752810SN/A 1765875Ssteve.reinhardt@amd.com // Check if miss is in pfq, if so remove it 1775875Ssteve.reinhardt@amd.com std::list<PacketPtr>::iterator iter = inPrefetch(blk_addr); 1782810SN/A if (iter != pf.end()) { 1795875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Saw a miss to a queued prefetch addr: " 1805875Ssteve.reinhardt@amd.com "0x%x, removing it\n", blk_addr); 1812810SN/A pfRemovedMSHR++; 1825875Ssteve.reinhardt@amd.com delete (*iter)->req; 1835875Ssteve.reinhardt@amd.com delete (*iter); 1842810SN/A pf.erase(iter); 1852810SN/A if (pf.empty()) 1864628SN/A cache->deassertMemSideBusRequest(BaseCache::Request_PF); 1872810SN/A } 1882810SN/A 1895875Ssteve.reinhardt@amd.com // Remove anything in queue with delay older than time 1905875Ssteve.reinhardt@amd.com // since everything is inserted in time order, start from end 1915875Ssteve.reinhardt@amd.com // and work until pf.empty() or time is earlier 1925875Ssteve.reinhardt@amd.com // This is done to emulate Aborting the previous work on a new miss 1935875Ssteve.reinhardt@amd.com // Needed for serial calculators like GHB 1942810SN/A if (serialSquash) { 1952810SN/A iter = pf.end(); 1962810SN/A iter--; 1972810SN/A while (!pf.empty() && ((*iter)->time >= time)) { 1982810SN/A pfSquashed++; 1995875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Squashing old prefetch addr: 0x%x\n", 2005875Ssteve.reinhardt@amd.com (*iter)->getAddr()); 2015875Ssteve.reinhardt@amd.com delete (*iter)->req; 2025875Ssteve.reinhardt@amd.com delete (*iter); 2035875Ssteve.reinhardt@amd.com pf.erase(iter); 2042810SN/A iter--; 2052810SN/A } 2062810SN/A if (pf.empty()) 2074628SN/A cache->deassertMemSideBusRequest(BaseCache::Request_PF); 2082810SN/A } 2092810SN/A 2102810SN/A 2112810SN/A std::list<Addr> addresses; 2122810SN/A std::list<Tick> delays; 2132810SN/A calculatePrefetch(pkt, addresses, delays); 2142810SN/A 2155875Ssteve.reinhardt@amd.com std::list<Addr>::iterator addrIter = addresses.begin(); 2165875Ssteve.reinhardt@amd.com std::list<Tick>::iterator delayIter = delays.begin(); 2175875Ssteve.reinhardt@amd.com for (; addrIter != addresses.end(); ++addrIter, ++delayIter) { 2185875Ssteve.reinhardt@amd.com Addr addr = *addrIter; 2195875Ssteve.reinhardt@amd.com 2202810SN/A pfIdentified++; 2215875Ssteve.reinhardt@amd.com 2225875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Found a pf candidate addr: 0x%x, " 2235875Ssteve.reinhardt@amd.com "inserting into prefetch queue with delay %d time %d\n", 2245875Ssteve.reinhardt@amd.com addr, *delayIter, time); 2255875Ssteve.reinhardt@amd.com 2265875Ssteve.reinhardt@amd.com // Check if it is already in the pf buffer 2275875Ssteve.reinhardt@amd.com if (inPrefetch(addr) != pf.end()) { 2285875Ssteve.reinhardt@amd.com pfBufferHit++; 2295875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Prefetch addr already in pf buffer\n"); 2305875Ssteve.reinhardt@amd.com continue; 2315875Ssteve.reinhardt@amd.com } 2325875Ssteve.reinhardt@amd.com 2335875Ssteve.reinhardt@amd.com // create a prefetch memreq 2345875Ssteve.reinhardt@amd.com Request *prefetchReq = new Request(*addrIter, blkSize, 0); 2355875Ssteve.reinhardt@amd.com PacketPtr prefetch = 2365875Ssteve.reinhardt@amd.com new Packet(prefetchReq, MemCmd::HardPFReq, Packet::Broadcast); 2372825SN/A prefetch->allocate(); 2385714Shsul@eecs.umich.edu prefetch->req->setThreadContext(pkt->req->contextId(), 2395714Shsul@eecs.umich.edu pkt->req->threadId()); 2402814SN/A 2415875Ssteve.reinhardt@amd.com prefetch->time = time + (*delayIter); // @todo ADD LATENCY HERE 2422810SN/A 2435875Ssteve.reinhardt@amd.com // We just remove the head if we are full 2445875Ssteve.reinhardt@amd.com if (pf.size() == size) { 2452810SN/A pfRemovedFull++; 2465875Ssteve.reinhardt@amd.com PacketPtr old_pkt = *pf.begin(); 2475875Ssteve.reinhardt@amd.com DPRINTF(HWPrefetch, "Prefetch queue full, " 2485875Ssteve.reinhardt@amd.com "removing oldest 0x%x\n", old_pkt->getAddr()); 2495875Ssteve.reinhardt@amd.com delete old_pkt->req; 2505875Ssteve.reinhardt@amd.com delete old_pkt; 2512810SN/A pf.pop_front(); 2522810SN/A } 2532810SN/A 2542810SN/A pf.push_back(prefetch); 2552810SN/A } 2562810SN/A } 2575875Ssteve.reinhardt@amd.com 2585875Ssteve.reinhardt@amd.com return pf.empty() ? 0 : pf.front()->time; 2592810SN/A} 2602810SN/A 2613349SN/Astd::list<PacketPtr>::iterator 2622810SN/ABasePrefetcher::inPrefetch(Addr address) 2632810SN/A{ 2645875Ssteve.reinhardt@amd.com // Guaranteed to only be one match, we always check before inserting 2653349SN/A std::list<PacketPtr>::iterator iter; 2665875Ssteve.reinhardt@amd.com for (iter = pf.begin(); iter != pf.end(); iter++) { 2672814SN/A if (((*iter)->getAddr() & ~(Addr)(blkSize-1)) == address) { 2682810SN/A return iter; 2692810SN/A } 2702810SN/A } 2712810SN/A return pf.end(); 2722810SN/A} 2732810SN/A 2745875Ssteve.reinhardt@amd.combool 2755875Ssteve.reinhardt@amd.comBasePrefetcher::samePage(Addr a, Addr b) 2765875Ssteve.reinhardt@amd.com{ 2775875Ssteve.reinhardt@amd.com return roundDown(a, TheISA::VMPageSize) == roundDown(b, TheISA::VMPageSize); 2785875Ssteve.reinhardt@amd.com} 279