brrip_rp.cc revision 12684
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
3312684Sodanrc@yahoo.com.br#include <memory>
3412684Sodanrc@yahoo.com.br
3512684Sodanrc@yahoo.com.br#include "base/logging.hh" // For fatal_if
3612626Sodanrc@yahoo.com.br#include "base/random.hh"
3712626Sodanrc@yahoo.com.br
3812626Sodanrc@yahoo.com.brBRRIPRP::BRRIPRP(const Params *p)
3912626Sodanrc@yahoo.com.br    : BaseReplacementPolicy(p),
4012626Sodanrc@yahoo.com.br      maxRRPV(p->max_RRPV), hitPriority(p->hit_priority), btp(p->btp)
4112626Sodanrc@yahoo.com.br{
4212684Sodanrc@yahoo.com.br    fatal_if(maxRRPV <= 0, "max_RRPV should be greater than zero.\n");
4312684Sodanrc@yahoo.com.br}
4412684Sodanrc@yahoo.com.br
4512684Sodanrc@yahoo.com.brvoid
4612684Sodanrc@yahoo.com.brBRRIPRP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
4712684Sodanrc@yahoo.com.brconst
4812684Sodanrc@yahoo.com.br{
4912684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
5012684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
5112684Sodanrc@yahoo.com.br
5212684Sodanrc@yahoo.com.br    // Set RRPV to an invalid distance
5312684Sodanrc@yahoo.com.br    casted_replacement_data->rrpv = maxRRPV + 1;
5412684Sodanrc@yahoo.com.br}
5512684Sodanrc@yahoo.com.br
5612684Sodanrc@yahoo.com.brvoid
5712684Sodanrc@yahoo.com.brBRRIPRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const
5812684Sodanrc@yahoo.com.br{
5912684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
6012684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
6112684Sodanrc@yahoo.com.br
6212684Sodanrc@yahoo.com.br    // Update RRPV if not 0 yet
6312684Sodanrc@yahoo.com.br    // Every hit in HP mode makes the entry the last to be evicted, while
6412684Sodanrc@yahoo.com.br    // in FP mode a hit makes the entry less likely to be evicted
6512684Sodanrc@yahoo.com.br    if (hitPriority) {
6612684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = 0;
6712684Sodanrc@yahoo.com.br    } else if (casted_replacement_data->rrpv > 0) {
6812684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv--;
6912626Sodanrc@yahoo.com.br    }
7012626Sodanrc@yahoo.com.br}
7112626Sodanrc@yahoo.com.br
7212626Sodanrc@yahoo.com.brvoid
7312684Sodanrc@yahoo.com.brBRRIPRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const
7412626Sodanrc@yahoo.com.br{
7512684Sodanrc@yahoo.com.br    std::shared_ptr<BRRIPReplData> casted_replacement_data =
7612684Sodanrc@yahoo.com.br        std::static_pointer_cast<BRRIPReplData>(replacement_data);
7712626Sodanrc@yahoo.com.br
7812684Sodanrc@yahoo.com.br    // Reset RRPV
7912684Sodanrc@yahoo.com.br    // Replacement data is inserted as "long re-reference" if lower than btp,
8012684Sodanrc@yahoo.com.br    // "distant re-reference" otherwise
8112684Sodanrc@yahoo.com.br    if (random_mt.random<unsigned>(1, 100) <= btp) {
8212684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = maxRRPV-1;
8312684Sodanrc@yahoo.com.br    } else {
8412684Sodanrc@yahoo.com.br        casted_replacement_data->rrpv = maxRRPV;
8512626Sodanrc@yahoo.com.br    }
8612626Sodanrc@yahoo.com.br}
8712626Sodanrc@yahoo.com.br
8812684Sodanrc@yahoo.com.brReplaceableEntry*
8912684Sodanrc@yahoo.com.brBRRIPRP::getVictim(const ReplacementCandidates& candidates) const
9012626Sodanrc@yahoo.com.br{
9112626Sodanrc@yahoo.com.br    // There must be at least one replacement candidate
9212626Sodanrc@yahoo.com.br    assert(candidates.size() > 0);
9312626Sodanrc@yahoo.com.br
9412684Sodanrc@yahoo.com.br    // Use first candidate as dummy victim
9512684Sodanrc@yahoo.com.br    ReplaceableEntry* victim = candidates[0];
9612684Sodanrc@yahoo.com.br
9712684Sodanrc@yahoo.com.br    // Store victim->rrpv in a variable to improve code readability
9812684Sodanrc@yahoo.com.br    int victim_RRPV = std::static_pointer_cast<BRRIPReplData>(
9912684Sodanrc@yahoo.com.br                        victim->replacementData)->rrpv;
10012684Sodanrc@yahoo.com.br
10112684Sodanrc@yahoo.com.br    // Visit all candidates to find victim
10212626Sodanrc@yahoo.com.br    for (const auto& candidate : candidates) {
10312684Sodanrc@yahoo.com.br        // Get candidate's rrpv
10412684Sodanrc@yahoo.com.br        int candidate_RRPV = std::static_pointer_cast<BRRIPReplData>(
10512684Sodanrc@yahoo.com.br                                    candidate->replacementData)->rrpv;
10612684Sodanrc@yahoo.com.br
10712684Sodanrc@yahoo.com.br        // Stop searching for victims if an invalid entry is found
10812684Sodanrc@yahoo.com.br        if (candidate_RRPV == maxRRPV + 1) {
10912684Sodanrc@yahoo.com.br            return candidate;
11012684Sodanrc@yahoo.com.br        // Update victim entry if necessary
11112684Sodanrc@yahoo.com.br        } else if (candidate_RRPV > victim_RRPV) {
11212684Sodanrc@yahoo.com.br            victim = candidate;
11312684Sodanrc@yahoo.com.br            victim_RRPV = candidate_RRPV;
11412626Sodanrc@yahoo.com.br        }
11512626Sodanrc@yahoo.com.br    }
11612626Sodanrc@yahoo.com.br
11712684Sodanrc@yahoo.com.br    // Get difference of victim's RRPV to the highest possible RRPV in
11812684Sodanrc@yahoo.com.br    // order to update the RRPV of all the other entries accordingly
11912684Sodanrc@yahoo.com.br    int diff = maxRRPV - victim_RRPV;
12012626Sodanrc@yahoo.com.br
12112626Sodanrc@yahoo.com.br    // No need to update RRPV if there is no difference
12212626Sodanrc@yahoo.com.br    if (diff > 0){
12312626Sodanrc@yahoo.com.br        // Update RRPV of all candidates
12412626Sodanrc@yahoo.com.br        for (const auto& candidate : candidates) {
12512684Sodanrc@yahoo.com.br            std::static_pointer_cast<BRRIPReplData>(
12612684Sodanrc@yahoo.com.br                candidate->replacementData)->rrpv += diff;
12712626Sodanrc@yahoo.com.br        }
12812626Sodanrc@yahoo.com.br    }
12912626Sodanrc@yahoo.com.br
13012684Sodanrc@yahoo.com.br    return victim;
13112684Sodanrc@yahoo.com.br}
13212626Sodanrc@yahoo.com.br
13312684Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData>
13412684Sodanrc@yahoo.com.brBRRIPRP::instantiateEntry()
13512684Sodanrc@yahoo.com.br{
13612684Sodanrc@yahoo.com.br    return std::shared_ptr<ReplacementData>(new BRRIPReplData(maxRRPV));
13712626Sodanrc@yahoo.com.br}
13812626Sodanrc@yahoo.com.br
13912626Sodanrc@yahoo.com.brBRRIPRP*
14012626Sodanrc@yahoo.com.brBRRIPRPParams::create()
14112626Sodanrc@yahoo.com.br{
14212626Sodanrc@yahoo.com.br    return new BRRIPRP(this);
14312626Sodanrc@yahoo.com.br}
144