stride.cc revision 5875:d82be3235ab4
1/*
2 * Copyright (c) 2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ron Dreslinski
29 *          Steve Reinhardt
30 */
31
32/**
33 * @file
34 * Stride Prefetcher template instantiations.
35 */
36
37#include "base/trace.hh"
38#include "mem/cache/prefetch/stride.hh"
39
40void
41StridePrefetcher::calculatePrefetch(PacketPtr &pkt, std::list<Addr> &addresses,
42                                    std::list<Tick> &delays)
43{
44    if (!pkt->req->hasPC()) {
45        DPRINTF(HWPrefetch, "ignoring request with no PC");
46        return;
47    }
48
49    Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
50    int ctx_id = useContextId ? pkt->req->contextId() : 0;
51    Addr pc = pkt->req->getPC();
52    assert(ctx_id < Max_Contexts);
53    std::list<StrideEntry*> &tab = table[ctx_id];
54
55    /* Scan Table for instAddr Match */
56    std::list<StrideEntry*>::iterator iter;
57    for (iter = tab.begin(); iter != tab.end(); iter++) {
58        if ((*iter)->instAddr == pc)
59            break;
60    }
61
62    if (iter != tab.end()) {
63        // Hit in table
64
65        int new_stride = blk_addr - (*iter)->missAddr;
66        bool stride_match = (new_stride == (*iter)->stride);
67
68        if (stride_match && new_stride != 0) {
69            if ((*iter)->confidence < Max_Conf)
70                (*iter)->confidence++;
71        } else {
72            (*iter)->stride = new_stride;
73            if ((*iter)->confidence > Min_Conf)
74                (*iter)->confidence = 0;
75        }
76
77        DPRINTF(HWPrefetch, "hit: PC %x blk_addr %x stride %d (%s), conf %d\n",
78                pc, blk_addr, new_stride, stride_match ? "match" : "change",
79                (*iter)->confidence);
80
81        (*iter)->missAddr = blk_addr;
82
83        if ((*iter)->confidence <= 0)
84            return;
85
86        for (int d = 1; d <= degree; d++) {
87            Addr new_addr = blk_addr + d * new_stride;
88            if (pageStop && !samePage(blk_addr, new_addr)) {
89                // Spanned the page, so now stop
90                pfSpanPage += degree - d + 1;
91                return;
92            } else {
93                DPRINTF(HWPrefetch, "  queuing prefetch to %x @ %d\n",
94                        new_addr, latency);
95                addresses.push_back(new_addr);
96                delays.push_back(latency);
97            }
98        }
99    } else {
100        // Miss in table
101        // Find lowest confidence and replace
102
103        DPRINTF(HWPrefetch, "miss: PC %x blk_addr %x\n", pc, blk_addr);
104
105        if (tab.size() >= 256) { //set default table size is 256
106            std::list<StrideEntry*>::iterator min_pos = tab.begin();
107            int min_conf = (*min_pos)->confidence;
108            for (iter = min_pos, ++iter; iter != tab.end(); ++iter) {
109                if ((*iter)->confidence < min_conf){
110                    min_pos = iter;
111                    min_conf = (*iter)->confidence;
112                }
113            }
114            DPRINTF(HWPrefetch, "  replacing PC %x\n", (*min_pos)->instAddr);
115            tab.erase(min_pos);
116        }
117
118        StrideEntry *new_entry = new StrideEntry;
119        new_entry->instAddr = pc;
120        new_entry->missAddr = blk_addr;
121        new_entry->stride = 0;
122        new_entry->confidence = 0;
123        tab.push_back(new_entry);
124    }
125}
126