sbooe.cc revision 13735:52ab3bab4f28
1/**
2 * Copyright (c) 2018 Metempsy Technology Consulting
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: Ivan Pizarro
29 */
30
31#include "mem/cache/prefetch/sbooe.hh"
32
33#include "debug/HWPrefetch.hh"
34#include "params/SBOOEPrefetcher.hh"
35
36SBOOEPrefetcher::SBOOEPrefetcher(const SBOOEPrefetcherParams *p)
37    : QueuedPrefetcher(p),
38      latencyBufferSize(p->latency_buffer_size),
39      sequentialPrefetchers(p->sequential_prefetchers),
40      scoreThreshold((p->sandbox_entries*p->score_threshold_pct)/100),
41      averageAccessLatency(0), latencyBufferSum(0),
42      bestSandbox(NULL),
43      accesses(0)
44{
45    if (!(p->score_threshold_pct >= 0 && p->score_threshold_pct <= 100)) {
46        fatal("%s: the score threshold should be between 0 and 100\n", name());
47    }
48
49    // Initialize a sandbox for every sequential prefetcher between
50    // -1 and the number of sequential prefetchers defined
51    for (int i = 0; i < sequentialPrefetchers; i++) {
52        sandboxes.push_back(Sandbox(p->sandbox_entries, i-1));
53    }
54}
55
56void
57SBOOEPrefetcher::Sandbox::insert(Addr addr, Tick tick)
58{
59    entries[index].valid = true;
60    entries[index].line = addr + stride;
61    entries[index].expectedArrivalTick = tick;
62
63    index++;
64
65    if (index == entries.size()) {
66        index = 0;
67    }
68}
69
70bool
71SBOOEPrefetcher::access(Addr access_line)
72{
73    for (Sandbox &sb : sandboxes) {
74        // Search for the address in the FIFO queue
75        for (const SandboxEntry &entry: sb.entries) {
76            if (entry.valid && entry.line == access_line) {
77                sb.sandboxScore++;
78                if (entry.expectedArrivalTick > curTick()) {
79                    sb.lateScore++;
80                }
81            }
82        }
83
84        sb.insert(access_line, curTick() + averageAccessLatency);
85
86        if (bestSandbox == NULL || sb.score() > bestSandbox->score()) {
87            bestSandbox = &sb;
88        }
89    }
90
91    accesses++;
92
93    return (accesses >= sandboxes.size());
94}
95
96void
97SBOOEPrefetcher::notifyFill(const PacketPtr& pkt)
98{
99    // (1) Look for the address in the demands list
100    // (2) Calculate the elapsed cycles until it was filled (curTick)
101    // (3) Insert the latency into the latency buffer (FIFO)
102    // (4) Calculate the new average access latency
103
104    auto it = demandAddresses.find(pkt->getAddr());
105
106    if (it != demandAddresses.end()) {
107        Tick elapsed_ticks = curTick() - it->second;
108
109        latencyBuffer.push_back(elapsed_ticks);
110        latencyBufferSum += elapsed_ticks;
111
112        if (latencyBuffer.size() > latencyBufferSize) {
113            latencyBufferSum -= latencyBuffer.front();
114            latencyBuffer.pop_front();
115        }
116
117        averageAccessLatency = latencyBufferSum / latencyBuffer.size();
118
119        demandAddresses.erase(it);
120    }
121}
122
123void
124SBOOEPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
125                                   std::vector<AddrPriority> &addresses)
126{
127    const Addr pfi_addr = pfi.getAddr();
128    const Addr pfi_line = pfi_addr >> lBlkSize;
129
130    auto it = demandAddresses.find(pfi_addr);
131
132    if (it == demandAddresses.end()) {
133        demandAddresses.insert(std::pair<Addr, Tick>(pfi_addr, curTick()));
134    }
135
136    const bool evaluationFinished = access(pfi_line);
137
138    if (evaluationFinished && bestSandbox->score() > scoreThreshold) {
139        Addr pref_line = pfi_line + bestSandbox->stride;
140        addresses.push_back(AddrPriority(pref_line << lBlkSize, 0));
141    }
142}
143
144SBOOEPrefetcher*
145SBOOEPrefetcherParams::create()
146{
147    return new SBOOEPrefetcher(this);
148}
149