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),
4214211Sodanrc@yahoo.com.br      numRRPVBits(p->num_bits), hitPriority(p->hit_priority), btp(p->btp)
4312626Sodanrc@yahoo.com.br{
4414211Sodanrc@yahoo.com.br    fatal_if(numRRPVBits <= 0, "There should be at least one bit per RRPV.\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
5414211Sodanrc@yahoo.com.br    // Invalidate entry
5514211Sodanrc@yahoo.com.br    casted_replacement_data->valid = false;
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) {
6814211Sodanrc@yahoo.com.br        casted_replacement_data->rrpv.reset();
6914211Sodanrc@yahoo.com.br    } else {
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
8314211Sodanrc@yahoo.com.br    casted_replacement_data->rrpv.saturate();
8412684Sodanrc@yahoo.com.br    if (random_mt.random<unsigned>(1, 100) <= btp) {
8514211Sodanrc@yahoo.com.br        casted_replacement_data->rrpv--;
8612626Sodanrc@yahoo.com.br    }
8714211Sodanrc@yahoo.com.br
8814211Sodanrc@yahoo.com.br    // Mark entry as ready to be used
8914211Sodanrc@yahoo.com.br    casted_replacement_data->valid = true;
9012626Sodanrc@yahoo.com.br}
9112626Sodanrc@yahoo.com.br
9212684Sodanrc@yahoo.com.brReplaceableEntry*
9312684Sodanrc@yahoo.com.brBRRIPRP::getVictim(const ReplacementCandidates& candidates) const
9412626Sodanrc@yahoo.com.br{
9512626Sodanrc@yahoo.com.br    // There must be at least one replacement candidate
9612626Sodanrc@yahoo.com.br    assert(candidates.size() > 0);
9712626Sodanrc@yahoo.com.br
9812684Sodanrc@yahoo.com.br    // Use first candidate as dummy victim
9912684Sodanrc@yahoo.com.br    ReplaceableEntry* victim = candidates[0];
10012684Sodanrc@yahoo.com.br
10112684Sodanrc@yahoo.com.br    // Store victim->rrpv in a variable to improve code readability
10212684Sodanrc@yahoo.com.br    int victim_RRPV = std::static_pointer_cast<BRRIPReplData>(
10312684Sodanrc@yahoo.com.br                        victim->replacementData)->rrpv;
10412684Sodanrc@yahoo.com.br
10512684Sodanrc@yahoo.com.br    // Visit all candidates to find victim
10612626Sodanrc@yahoo.com.br    for (const auto& candidate : candidates) {
10714211Sodanrc@yahoo.com.br        std::shared_ptr<BRRIPReplData> candidate_repl_data =
10814211Sodanrc@yahoo.com.br            std::static_pointer_cast<BRRIPReplData>(
10914211Sodanrc@yahoo.com.br                candidate->replacementData);
11012684Sodanrc@yahoo.com.br
11112684Sodanrc@yahoo.com.br        // Stop searching for victims if an invalid entry is found
11214211Sodanrc@yahoo.com.br        if (!candidate_repl_data->valid) {
11312684Sodanrc@yahoo.com.br            return candidate;
11414211Sodanrc@yahoo.com.br        }
11514211Sodanrc@yahoo.com.br
11612684Sodanrc@yahoo.com.br        // Update victim entry if necessary
11714211Sodanrc@yahoo.com.br        int candidate_RRPV = candidate_repl_data->rrpv;
11814211Sodanrc@yahoo.com.br        if (candidate_RRPV > victim_RRPV) {
11912684Sodanrc@yahoo.com.br            victim = candidate;
12012684Sodanrc@yahoo.com.br            victim_RRPV = candidate_RRPV;
12112626Sodanrc@yahoo.com.br        }
12212626Sodanrc@yahoo.com.br    }
12312626Sodanrc@yahoo.com.br
12412684Sodanrc@yahoo.com.br    // Get difference of victim's RRPV to the highest possible RRPV in
12512684Sodanrc@yahoo.com.br    // order to update the RRPV of all the other entries accordingly
12614211Sodanrc@yahoo.com.br    int diff = std::static_pointer_cast<BRRIPReplData>(
12714211Sodanrc@yahoo.com.br        victim->replacementData)->rrpv.saturate();
12812626Sodanrc@yahoo.com.br
12912626Sodanrc@yahoo.com.br    // No need to update RRPV if there is no difference
13012626Sodanrc@yahoo.com.br    if (diff > 0){
13112626Sodanrc@yahoo.com.br        // Update RRPV of all candidates
13212626Sodanrc@yahoo.com.br        for (const auto& candidate : candidates) {
13312684Sodanrc@yahoo.com.br            std::static_pointer_cast<BRRIPReplData>(
13412684Sodanrc@yahoo.com.br                candidate->replacementData)->rrpv += diff;
13512626Sodanrc@yahoo.com.br        }
13612626Sodanrc@yahoo.com.br    }
13712626Sodanrc@yahoo.com.br
13812684Sodanrc@yahoo.com.br    return victim;
13912684Sodanrc@yahoo.com.br}
14012626Sodanrc@yahoo.com.br
14112684Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData>
14212684Sodanrc@yahoo.com.brBRRIPRP::instantiateEntry()
14312684Sodanrc@yahoo.com.br{
14414211Sodanrc@yahoo.com.br    return std::shared_ptr<ReplacementData>(new BRRIPReplData(numRRPVBits));
14512626Sodanrc@yahoo.com.br}
14612626Sodanrc@yahoo.com.br
14712626Sodanrc@yahoo.com.brBRRIPRP*
14812626Sodanrc@yahoo.com.brBRRIPRPParams::create()
14912626Sodanrc@yahoo.com.br{
15012626Sodanrc@yahoo.com.br    return new BRRIPRP(this);
15112626Sodanrc@yahoo.com.br}
152