brrip_rp.cc revision 12727
112626Sodanrc@yahoo.com.br/**
212626Sodanrc@yahoo.com.br * Copyright (c) 2018 Inria
312626Sodanrc@yahoo.com.br * All rights reserved.
412626Sodanrc@yahoo.com.br *
512626Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
612626Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
712626Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
812626Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
912626Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1012626Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1112626Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1212626Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1312626Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1412626Sodanrc@yahoo.com.br * this software without specific prior written permission.
1512626Sodanrc@yahoo.com.br *
1612626Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712626Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812626Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912626Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012626Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112626Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212626Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312626Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412626Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512626Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612626Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712626Sodanrc@yahoo.com.br *
2812626Sodanrc@yahoo.com.br * Authors: Daniel Carvalho
2912626Sodanrc@yahoo.com.br */
3012626Sodanrc@yahoo.com.br
3112626Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/brrip_rp.hh"
3212626Sodanrc@yahoo.com.br
3312727Snikos.nikoleris@arm.com#include <cassert>
3412684Sodanrc@yahoo.com.br#include <memory>
3512684Sodanrc@yahoo.com.br
3612684Sodanrc@yahoo.com.br#include "base/logging.hh" // For fatal_if
3712626Sodanrc@yahoo.com.br#include "base/random.hh"
3812727Snikos.nikoleris@arm.com#include "params/BRRIPRP.hh"
3912626Sodanrc@yahoo.com.br
4012626Sodanrc@yahoo.com.brBRRIPRP::BRRIPRP(const Params *p)
4112626Sodanrc@yahoo.com.br    : BaseReplacementPolicy(p),
4212626Sodanrc@yahoo.com.br      maxRRPV(p->max_RRPV), hitPriority(p->hit_priority), btp(p->btp)
4312626Sodanrc@yahoo.com.br{
4412684Sodanrc@yahoo.com.br    fatal_if(maxRRPV <= 0, "max_RRPV should be greater than zero.\n");
4512684Sodanrc@yahoo.com.br}
4612684Sodanrc@yahoo.com.br
4712684Sodanrc@yahoo.com.brvoid
4812684Sodanrc@yahoo.com.brBRRIPRP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
4912684Sodanrc@yahoo.com.brconst
5012684Sodanrc@yahoo.com.br{
5112684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
5212684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
5312684Sodanrc@yahoo.com.br
5412684Sodanrc@yahoo.com.br    // Set RRPV to an invalid distance
5512684Sodanrc@yahoo.com.br    casted_replacement_data->rrpv = maxRRPV + 1;
5612684Sodanrc@yahoo.com.br}
5712684Sodanrc@yahoo.com.br
5812684Sodanrc@yahoo.com.brvoid
5912684Sodanrc@yahoo.com.brBRRIPRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const
6012684Sodanrc@yahoo.com.br{
6112684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
6212684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
6312684Sodanrc@yahoo.com.br
6412684Sodanrc@yahoo.com.br    // Update RRPV if not 0 yet
6512684Sodanrc@yahoo.com.br    // Every hit in HP mode makes the entry the last to be evicted, while
6612684Sodanrc@yahoo.com.br    // in FP mode a hit makes the entry less likely to be evicted
6712684Sodanrc@yahoo.com.br    if (hitPriority) {
6812684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = 0;
6912684Sodanrc@yahoo.com.br    } else if (casted_replacement_data->rrpv > 0) {
7012684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv--;
7112626Sodanrc@yahoo.com.br    }
7212626Sodanrc@yahoo.com.br}
7312626Sodanrc@yahoo.com.br
7412626Sodanrc@yahoo.com.brvoid
7512684Sodanrc@yahoo.com.brBRRIPRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const
7612626Sodanrc@yahoo.com.br{
7712684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
7812684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
7912626Sodanrc@yahoo.com.br
8012684Sodanrc@yahoo.com.br    // Reset RRPV
8112684Sodanrc@yahoo.com.br    // Replacement data is inserted as "long re-reference" if lower than btp,
8212684Sodanrc@yahoo.com.br    // "distant re-reference" otherwise
8312684Sodanrc@yahoo.com.br    if (random_mt.random<unsigned>(1, 100) <= btp) {
8412684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = maxRRPV-1;
8512684Sodanrc@yahoo.com.br    } else {
8612684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = maxRRPV;
8712626Sodanrc@yahoo.com.br    }
8812626Sodanrc@yahoo.com.br}
8912626Sodanrc@yahoo.com.br
9012684Sodanrc@yahoo.com.brReplaceableEntry*
9112684Sodanrc@yahoo.com.brBRRIPRP::getVictim(const ReplacementCandidates& candidates) const
9212626Sodanrc@yahoo.com.br{
9312626Sodanrc@yahoo.com.br    // There must be at least one replacement candidate
9412626Sodanrc@yahoo.com.br    assert(candidates.size() > 0);
9512626Sodanrc@yahoo.com.br
9612684Sodanrc@yahoo.com.br    // Use first candidate as dummy victim
9712684Sodanrc@yahoo.com.br    ReplaceableEntry* victim = candidates[0];
9812684Sodanrc@yahoo.com.br
9912684Sodanrc@yahoo.com.br    // Store victim->rrpv in a variable to improve code readability
10012684Sodanrc@yahoo.com.br    int victim_RRPV = std::static_pointer_cast<BRRIPReplData>(
10112684Sodanrc@yahoo.com.br                        victim->replacementData)->rrpv;
10212684Sodanrc@yahoo.com.br
10312684Sodanrc@yahoo.com.br    // Visit all candidates to find victim
10412626Sodanrc@yahoo.com.br    for (const auto& candidate : candidates) {
10512684Sodanrc@yahoo.com.br        // Get candidate's rrpv
10612684Sodanrc@yahoo.com.br        int candidate_RRPV = std::static_pointer_cast<BRRIPReplData>(
10712684Sodanrc@yahoo.com.br                                    candidate->replacementData)->rrpv;
10812684Sodanrc@yahoo.com.br
10912684Sodanrc@yahoo.com.br        // Stop searching for victims if an invalid entry is found
11012684Sodanrc@yahoo.com.br        if (candidate_RRPV == maxRRPV + 1) {
11112684Sodanrc@yahoo.com.br            return candidate;
11212684Sodanrc@yahoo.com.br        // Update victim entry if necessary
11312684Sodanrc@yahoo.com.br        } else if (candidate_RRPV > victim_RRPV) {
11412684Sodanrc@yahoo.com.br            victim = candidate;
11512684Sodanrc@yahoo.com.br            victim_RRPV = candidate_RRPV;
11612626Sodanrc@yahoo.com.br        }
11712626Sodanrc@yahoo.com.br    }
11812626Sodanrc@yahoo.com.br
11912684Sodanrc@yahoo.com.br    // Get difference of victim's RRPV to the highest possible RRPV in
12012684Sodanrc@yahoo.com.br    // order to update the RRPV of all the other entries accordingly
12112684Sodanrc@yahoo.com.br    int diff = maxRRPV - victim_RRPV;
12212626Sodanrc@yahoo.com.br
12312626Sodanrc@yahoo.com.br    // No need to update RRPV if there is no difference
12412626Sodanrc@yahoo.com.br    if (diff > 0){
12512626Sodanrc@yahoo.com.br        // Update RRPV of all candidates
12612626Sodanrc@yahoo.com.br        for (const auto& candidate : candidates) {
12712684Sodanrc@yahoo.com.br            std::static_pointer_cast<BRRIPReplData>(
12812684Sodanrc@yahoo.com.br                candidate->replacementData)->rrpv += diff;
12912626Sodanrc@yahoo.com.br        }
13012626Sodanrc@yahoo.com.br    }
13112626Sodanrc@yahoo.com.br
13212684Sodanrc@yahoo.com.br    return victim;
13312684Sodanrc@yahoo.com.br}
13412626Sodanrc@yahoo.com.br
13512684Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData>
13612684Sodanrc@yahoo.com.brBRRIPRP::instantiateEntry()
13712684Sodanrc@yahoo.com.br{
13812684Sodanrc@yahoo.com.br    return std::shared_ptr<ReplacementData>(new BRRIPReplData(maxRRPV));
13912626Sodanrc@yahoo.com.br}
14012626Sodanrc@yahoo.com.br
14112626Sodanrc@yahoo.com.brBRRIPRP*
14212626Sodanrc@yahoo.com.brBRRIPRPParams::create()
14312626Sodanrc@yahoo.com.br{
14412626Sodanrc@yahoo.com.br    return new BRRIPRP(this);
14512626Sodanrc@yahoo.com.br}
146