stride.cc revision 10627:63edd4a1243f
1/*
2 * Copyright (c) 2012-2013 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 * Authors: Ron Dreslinski
41 *          Steve Reinhardt
42 */
43
44/**
45 * @file
46 * Stride Prefetcher template instantiations.
47 */
48
49#include "base/random.hh"
50#include "debug/HWPrefetch.hh"
51#include "mem/cache/prefetch/stride.hh"
52
53StridePrefetcher::StridePrefetcher(const StridePrefetcherParams *p)
54    : QueuedPrefetcher(p),
55      maxConf(p->max_conf),
56      threshConf(p->thresh_conf),
57      minConf(p->min_conf),
58      startConf(p->start_conf),
59      pcTableAssoc(p->table_assoc),
60      pcTableSets(p->table_sets),
61      useMasterId(p->use_master_id),
62      degree(p->degree)
63{
64    // Don't consult stride prefetcher on instruction accesses
65    onInst = false;
66
67    assert(isPowerOf2(pcTableSets));
68
69    for (int c = 0; c < maxContexts; c++) {
70        pcTable[c] = new StrideEntry*[pcTableSets];
71        for (int s = 0; s < pcTableSets; s++) {
72            pcTable[c][s] = new StrideEntry[pcTableAssoc];
73        }
74    }
75}
76
77StridePrefetcher::~StridePrefetcher()
78{
79    for (int c = 0; c < maxContexts; c++) {
80        for (int s = 0; s < pcTableSets; s++) {
81            delete[] pcTable[c][s];
82        }
83    }
84}
85
86void
87StridePrefetcher::calculatePrefetch(const PacketPtr &pkt,
88                                    std::vector<Addr> &addresses)
89{
90    if (!pkt->req->hasPC()) {
91        DPRINTF(HWPrefetch, "Ignoring request with no PC.\n");
92        return;
93    }
94
95    // Get required packet info
96    Addr pkt_addr = pkt->getAddr();
97    Addr pc = pkt->req->getPC();
98    bool is_secure = pkt->isSecure();
99    MasterID master_id = useMasterId ? pkt->req->masterId() : 0;
100
101    assert(master_id < maxContexts);
102
103    // Lookup pc-based information
104    StrideEntry *entry;
105
106    if(pcTableHit(pc, is_secure, master_id, entry)) {
107        // Hit in table
108        int new_stride = pkt_addr - entry->lastAddr;
109        bool stride_match = (new_stride == entry->stride);
110
111        // Adjust confidence for stride entry
112        if (stride_match && new_stride != 0) {
113            if (entry->confidence < maxConf)
114                entry->confidence++;
115        } else {
116            if (entry->confidence > minConf)
117                entry->confidence--;
118            // If confidence has dropped below the threshold, train new stride
119            if (entry->confidence < threshConf)
120                entry->stride = new_stride;
121        }
122
123        DPRINTF(HWPrefetch, "Hit: PC %x pkt_addr %x (%s) stride %d (%s), "
124                "conf %d\n", pc, pkt_addr, is_secure ? "s" : "ns", new_stride,
125                stride_match ? "match" : "change",
126                entry->confidence);
127
128        entry->lastAddr = pkt_addr;
129
130        // Abort prefetch generation if below confidence threshold
131        if (entry->confidence < threshConf)
132            return;
133
134        // Generate up to degree prefetches
135        for (int d = 1; d <= degree; d++) {
136            // Round strides up to atleast 1 cacheline
137            int prefetch_stride = new_stride;
138            if (abs(new_stride) < blkSize) {
139                prefetch_stride = (new_stride < 0) ? -blkSize : blkSize;
140            }
141
142            Addr new_addr = pkt_addr + d * prefetch_stride;
143            if (samePage(pkt_addr, new_addr)) {
144                DPRINTF(HWPrefetch, "Queuing prefetch to %#x.\n", new_addr);
145                addresses.push_back(new_addr);
146            } else {
147                // Record the number of page crossing prefetches generated
148                pfSpanPage += degree - d + 1;
149                DPRINTF(HWPrefetch, "Ignoring page crossing prefetch.\n");
150                return;
151            }
152        }
153    } else {
154        // Miss in table
155        DPRINTF(HWPrefetch, "Miss: PC %x pkt_addr %x (%s)\n", pc, pkt_addr,
156                is_secure ? "s" : "ns");
157
158        StrideEntry* entry = pcTableVictim(pc, master_id);
159        entry->instAddr = pc;
160        entry->lastAddr = pkt_addr;
161        entry->isSecure= is_secure;
162        entry->stride = 0;
163        entry->confidence = startConf;
164    }
165}
166
167inline Addr
168StridePrefetcher::pcHash(Addr pc) const
169{
170    Addr hash1 = pc >> 1;
171    Addr hash2 = hash1 >> floorLog2(pcTableSets);
172    return (hash1 ^ hash2) & (Addr)(pcTableSets - 1);
173}
174
175inline StridePrefetcher::StrideEntry*
176StridePrefetcher::pcTableVictim(Addr pc, int master_id)
177{
178    // Rand replacement for now
179    int set = pcHash(pc);
180    int way = random_mt.random<int>(0, pcTableAssoc - 1);
181
182    DPRINTF(HWPrefetch, "Victimizing lookup table[%d][%d].\n", set, way);
183    return &pcTable[master_id][set][way];
184}
185
186inline bool
187StridePrefetcher::pcTableHit(Addr pc, bool is_secure, int master_id,
188                             StrideEntry* &entry)
189{
190    int set = pcHash(pc);
191    StrideEntry* set_entries = pcTable[master_id][set];
192    for (int way = 0; way < pcTableAssoc; way++) {
193        // Search ways for match
194        if (set_entries[way].instAddr == pc &&
195            set_entries[way].isSecure == is_secure) {
196            DPRINTF(HWPrefetch, "Lookup hit table[%d][%d].\n", set, way);
197            entry = &set_entries[way];
198            return true;
199        }
200    }
201    return false;
202}
203
204StridePrefetcher*
205StridePrefetcherParams::create()
206{
207    return new StridePrefetcher(this);
208}
209