queued.cc revision 13552:86c9a15aa4ef
16019Shines@cs.fsu.edu/*
26019Shines@cs.fsu.edu * Copyright (c) 2014-2015 ARM Limited
36019Shines@cs.fsu.edu * All rights reserved
46019Shines@cs.fsu.edu *
56019Shines@cs.fsu.edu * The license below extends only to copyright in the software and shall
66019Shines@cs.fsu.edu * not be construed as granting a license to any other intellectual
76019Shines@cs.fsu.edu * property including but not limited to intellectual property relating
86019Shines@cs.fsu.edu * to a hardware implementation of the functionality of the software
96019Shines@cs.fsu.edu * licensed hereunder.  You may use the software subject to the license
106019Shines@cs.fsu.edu * terms below provided that you ensure that this notice is replicated
116019Shines@cs.fsu.edu * unmodified and in its entirety in all distributions of the software,
126019Shines@cs.fsu.edu * modified or unmodified, in source code or in binary form.
136019Shines@cs.fsu.edu *
146019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
156019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
166019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
176019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
186019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
196019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
206019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
216019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
226019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
236019Shines@cs.fsu.edu * this software without specific prior written permission.
246019Shines@cs.fsu.edu *
256019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
266019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
276019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
286019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
296019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
306019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
316019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
326019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
336019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
346019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
356019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
366019Shines@cs.fsu.edu *
376019Shines@cs.fsu.edu * Authors: Mitch Hayenga
386216Snate@binkert.org */
396019Shines@cs.fsu.edu
406019Shines@cs.fsu.edu#include "mem/cache/prefetch/queued.hh"
416019Shines@cs.fsu.edu
426019Shines@cs.fsu.edu#include <cassert>
436019Shines@cs.fsu.edu
446019Shines@cs.fsu.edu#include "base/logging.hh"
456019Shines@cs.fsu.edu#include "base/trace.hh"
466019Shines@cs.fsu.edu#include "debug/HWPrefetch.hh"
476019Shines@cs.fsu.edu#include "mem/request.hh"
486019Shines@cs.fsu.edu#include "params/QueuedPrefetcher.hh"
496019Shines@cs.fsu.edu
506019Shines@cs.fsu.eduQueuedPrefetcher::QueuedPrefetcher(const QueuedPrefetcherParams *p)
516019Shines@cs.fsu.edu    : BasePrefetcher(p), queueSize(p->queue_size), latency(p->latency),
526019Shines@cs.fsu.edu      queueSquash(p->queue_squash), queueFilter(p->queue_filter),
536019Shines@cs.fsu.edu      cacheSnoop(p->cache_snoop), tagPrefetch(p->tag_prefetch)
546019Shines@cs.fsu.edu{
556019Shines@cs.fsu.edu
566019Shines@cs.fsu.edu}
576019Shines@cs.fsu.edu
586019Shines@cs.fsu.eduQueuedPrefetcher::~QueuedPrefetcher()
596019Shines@cs.fsu.edu{
606019Shines@cs.fsu.edu    // Delete the queued prefetch packets
616019Shines@cs.fsu.edu    for (DeferredPacket &p : pfq) {
626019Shines@cs.fsu.edu        delete p.pkt;
636019Shines@cs.fsu.edu    }
646019Shines@cs.fsu.edu}
656019Shines@cs.fsu.edu
666019Shines@cs.fsu.eduvoid
676019Shines@cs.fsu.eduQueuedPrefetcher::notify(const PacketPtr &pkt, const PrefetchInfo &pfi)
686019Shines@cs.fsu.edu{
696019Shines@cs.fsu.edu    Addr blk_addr = blockAddress(pfi.getAddr());
706019Shines@cs.fsu.edu    bool is_secure = pfi.isSecure();
716019Shines@cs.fsu.edu
726019Shines@cs.fsu.edu    // Squash queued prefetches if demand miss to same line
736019Shines@cs.fsu.edu    if (queueSquash) {
746019Shines@cs.fsu.edu        auto itr = pfq.begin();
756019Shines@cs.fsu.edu        while (itr != pfq.end()) {
766019Shines@cs.fsu.edu            if (itr->pfInfo.getAddr() == blk_addr &&
776019Shines@cs.fsu.edu                itr->pfInfo.isSecure() == is_secure) {
786019Shines@cs.fsu.edu                delete itr->pkt;
796019Shines@cs.fsu.edu                itr = pfq.erase(itr);
806019Shines@cs.fsu.edu            } else {
816019Shines@cs.fsu.edu                ++itr;
826019Shines@cs.fsu.edu            }
836019Shines@cs.fsu.edu        }
846019Shines@cs.fsu.edu    }
856019Shines@cs.fsu.edu
866019Shines@cs.fsu.edu    // Calculate prefetches given this access
876019Shines@cs.fsu.edu    std::vector<AddrPriority> addresses;
886019Shines@cs.fsu.edu    calculatePrefetch(pfi, addresses);
896019Shines@cs.fsu.edu
906019Shines@cs.fsu.edu    // Queue up generated prefetches
916019Shines@cs.fsu.edu    for (AddrPriority& addr_prio : addresses) {
926019Shines@cs.fsu.edu
93        // Block align prefetch address
94        addr_prio.first = blockAddress(addr_prio.first);
95
96        if (samePage(pfi.getAddr(), addr_prio.first)) {
97            PrefetchInfo new_pfi(pfi,addr_prio.first);
98
99            pfIdentified++;
100            DPRINTF(HWPrefetch, "Found a pf candidate addr: %#x, "
101                    "inserting into prefetch queue.\n", new_pfi.getAddr());
102
103            // Create and insert the request
104            insert(pkt, new_pfi, addr_prio.second);
105        } else {
106            // Record the number of page crossing prefetches generate
107            pfSpanPage += 1;
108            DPRINTF(HWPrefetch, "Ignoring page crossing prefetch.\n");
109        }
110    }
111}
112
113PacketPtr
114QueuedPrefetcher::getPacket()
115{
116    DPRINTF(HWPrefetch, "Requesting a prefetch to issue.\n");
117
118    if (pfq.empty()) {
119        DPRINTF(HWPrefetch, "No hardware prefetches available.\n");
120        return nullptr;
121    }
122
123    PacketPtr pkt = pfq.front().pkt;
124    pfq.pop_front();
125
126    pfIssued++;
127    assert(pkt != nullptr);
128    DPRINTF(HWPrefetch, "Generating prefetch for %#x.\n", pkt->getAddr());
129    return pkt;
130}
131
132QueuedPrefetcher::const_iterator
133QueuedPrefetcher::inPrefetch(const PrefetchInfo &pfi) const
134{
135    for (const_iterator dp = pfq.begin(); dp != pfq.end(); dp++) {
136        if (dp->pfInfo.sameAddr(pfi)) return dp;
137    }
138
139    return pfq.end();
140}
141
142QueuedPrefetcher::iterator
143QueuedPrefetcher::inPrefetch(const PrefetchInfo &pfi)
144{
145    for (iterator dp = pfq.begin(); dp != pfq.end(); dp++) {
146        if (dp->pfInfo.sameAddr(pfi)) return dp;
147    }
148
149    return pfq.end();
150}
151
152void
153QueuedPrefetcher::regStats()
154{
155    BasePrefetcher::regStats();
156
157    pfIdentified
158        .name(name() + ".pfIdentified")
159        .desc("number of prefetch candidates identified");
160
161    pfBufferHit
162        .name(name() + ".pfBufferHit")
163        .desc("number of redundant prefetches already in prefetch queue");
164
165    pfInCache
166        .name(name() + ".pfInCache")
167        .desc("number of redundant prefetches already in cache/mshr dropped");
168
169    pfRemovedFull
170        .name(name() + ".pfRemovedFull")
171        .desc("number of prefetches dropped due to prefetch queue size");
172
173    pfSpanPage
174        .name(name() + ".pfSpanPage")
175        .desc("number of prefetches not generated due to page crossing");
176}
177
178void
179QueuedPrefetcher::insert(const PacketPtr &pkt, PrefetchInfo &new_pfi,
180                         int32_t priority)
181{
182    if (queueFilter) {
183        iterator it = inPrefetch(new_pfi);
184        /* If the address is already in the queue, update priority and leave */
185        if (it != pfq.end()) {
186            pfBufferHit++;
187            if (it->priority < priority) {
188                /* Update priority value and position in the queue */
189                it->priority = priority;
190                iterator prev = it;
191                bool cont = true;
192                while (cont && prev != pfq.begin()) {
193                    prev--;
194                    /* If the packet has higher priority, swap */
195                    if (*it > *prev) {
196                        std::swap(*it, *prev);
197                        it = prev;
198                    }
199                }
200                DPRINTF(HWPrefetch, "Prefetch addr already in "
201                    "prefetch queue, priority updated\n");
202            } else {
203                DPRINTF(HWPrefetch, "Prefetch addr already in "
204                    "prefetch queue\n");
205            }
206            return;
207        }
208    }
209
210    Addr target_addr = new_pfi.getAddr();
211    if (useVirtualAddresses) {
212        assert(pkt->req->hasPaddr());
213        //if we trained with virtual addresses, compute the phsysical address
214        if (new_pfi.getAddr() >= pkt->req->getVaddr()) {
215            //positive stride
216            target_addr = pkt->req->getPaddr() +
217                (new_pfi.getAddr() - pkt->req->getVaddr());
218        } else {
219            //negative stride
220            target_addr = pkt->req->getPaddr() -
221                (pkt->req->getVaddr() - new_pfi.getAddr());
222        }
223    }
224
225    if (cacheSnoop && (inCache(target_addr, new_pfi.isSecure()) ||
226                inMissQueue(target_addr, new_pfi.isSecure()))) {
227        pfInCache++;
228        DPRINTF(HWPrefetch, "Dropping redundant in "
229                "cache/MSHR prefetch addr:%#x\n", target_addr);
230        return;
231    }
232
233    /* Create a prefetch memory request */
234    RequestPtr pf_req =
235        std::make_shared<Request>(target_addr, blkSize, 0, masterId);
236
237    if (new_pfi.isSecure()) {
238        pf_req->setFlags(Request::SECURE);
239    }
240    pf_req->taskId(ContextSwitchTaskId::Prefetcher);
241    PacketPtr pf_pkt = new Packet(pf_req, MemCmd::HardPFReq);
242    pf_pkt->allocate();
243    if (tagPrefetch && new_pfi.hasPC()) {
244        // Tag prefetch packet with  accessing pc
245        pf_pkt->req->setPC(new_pfi.getPC());
246    }
247
248    /* Verify prefetch buffer space for request */
249    if (pfq.size() == queueSize) {
250        pfRemovedFull++;
251        /* Lowest priority packet */
252        iterator it = pfq.end();
253        panic_if (it == pfq.begin(), "Prefetch queue is both full and empty!");
254        --it;
255        /* Look for oldest in that level of priority */
256        panic_if (it == pfq.begin(), "Prefetch queue is full with 1 element!");
257        iterator prev = it;
258        bool cont = true;
259        /* While not at the head of the queue */
260        while (cont && prev != pfq.begin()) {
261            prev--;
262            /* While at the same level of priority */
263            cont = prev->priority == it->priority;
264            if (cont)
265                /* update pointer */
266                it = prev;
267        }
268        DPRINTF(HWPrefetch, "Prefetch queue full, removing lowest priority "
269                            "oldest packet, addr: %#x", it->pfInfo.getAddr());
270        delete it->pkt;
271        pfq.erase(it);
272    }
273
274    Tick pf_time = curTick() + clockPeriod() * latency;
275    DPRINTF(HWPrefetch, "Prefetch queued. "
276            "addr:%#x priority: %3d tick:%lld.\n",
277            target_addr, priority, pf_time);
278
279    /* Create the packet and find the spot to insert it */
280    DeferredPacket dpp(new_pfi, pf_time, pf_pkt, priority);
281    if (pfq.size() == 0) {
282        pfq.emplace_back(dpp);
283    } else {
284        iterator it = pfq.end();
285        do {
286            --it;
287        } while (it != pfq.begin() && dpp > *it);
288        /* If we reach the head, we have to see if the new element is new head
289         * or not */
290        if (it == pfq.begin() && dpp <= *it)
291            it++;
292        pfq.insert(it, dpp);
293    }
294}
295