112600Sodanrc@yahoo.com.br/**
212600Sodanrc@yahoo.com.br * Copyright (c) 2018 Inria
312600Sodanrc@yahoo.com.br * All rights reserved.
412600Sodanrc@yahoo.com.br *
512600Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
612600Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
712600Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
812600Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
912600Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1012600Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1112600Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1212600Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1312600Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1412600Sodanrc@yahoo.com.br * this software without specific prior written permission.
1512600Sodanrc@yahoo.com.br *
1612600Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712600Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812600Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912600Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012600Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112600Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212600Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312600Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412600Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512600Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612600Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712600Sodanrc@yahoo.com.br *
2812600Sodanrc@yahoo.com.br * Authors: Daniel Carvalho
2912600Sodanrc@yahoo.com.br */
3012600Sodanrc@yahoo.com.br
3112600Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/random_rp.hh"
3212600Sodanrc@yahoo.com.br
3312727Snikos.nikoleris@arm.com#include <cassert>
3412727Snikos.nikoleris@arm.com#include <memory>
3512727Snikos.nikoleris@arm.com
3612600Sodanrc@yahoo.com.br#include "base/random.hh"
3712727Snikos.nikoleris@arm.com#include "params/RandomRP.hh"
3812600Sodanrc@yahoo.com.br
3912600Sodanrc@yahoo.com.brRandomRP::RandomRP(const Params *p)
4012600Sodanrc@yahoo.com.br    : BaseReplacementPolicy(p)
4112600Sodanrc@yahoo.com.br{
4212600Sodanrc@yahoo.com.br}
4312600Sodanrc@yahoo.com.br
4412684Sodanrc@yahoo.com.brvoid
4512684Sodanrc@yahoo.com.brRandomRP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
4612684Sodanrc@yahoo.com.brconst
4712684Sodanrc@yahoo.com.br{
4812684Sodanrc@yahoo.com.br    // Unprioritize replacement data victimization
4912684Sodanrc@yahoo.com.br    std::static_pointer_cast<RandomReplData>(
5012684Sodanrc@yahoo.com.br        replacement_data)->valid = false;
5112684Sodanrc@yahoo.com.br}
5212684Sodanrc@yahoo.com.br
5312684Sodanrc@yahoo.com.brvoid
5412684Sodanrc@yahoo.com.brRandomRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const
5512684Sodanrc@yahoo.com.br{
5612684Sodanrc@yahoo.com.br}
5712684Sodanrc@yahoo.com.br
5812684Sodanrc@yahoo.com.brvoid
5912684Sodanrc@yahoo.com.brRandomRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const
6012684Sodanrc@yahoo.com.br{
6112684Sodanrc@yahoo.com.br    // Unprioritize replacement data victimization
6212684Sodanrc@yahoo.com.br    std::static_pointer_cast<RandomReplData>(
6312684Sodanrc@yahoo.com.br        replacement_data)->valid = true;
6412684Sodanrc@yahoo.com.br}
6512684Sodanrc@yahoo.com.br
6612684Sodanrc@yahoo.com.brReplaceableEntry*
6712684Sodanrc@yahoo.com.brRandomRP::getVictim(const ReplacementCandidates& candidates) const
6812600Sodanrc@yahoo.com.br{
6912600Sodanrc@yahoo.com.br    // There must be at least one replacement candidate
7012600Sodanrc@yahoo.com.br    assert(candidates.size() > 0);
7112600Sodanrc@yahoo.com.br
7212600Sodanrc@yahoo.com.br    // Choose one candidate at random
7312684Sodanrc@yahoo.com.br    ReplaceableEntry* victim = candidates[random_mt.random<unsigned>(0,
7412600Sodanrc@yahoo.com.br                                    candidates.size() - 1)];
7512600Sodanrc@yahoo.com.br
7612684Sodanrc@yahoo.com.br    // Visit all candidates to search for an invalid entry. If one is found,
7712684Sodanrc@yahoo.com.br    // its eviction is prioritized
7812600Sodanrc@yahoo.com.br    for (const auto& candidate : candidates) {
7912684Sodanrc@yahoo.com.br        if (!std::static_pointer_cast<RandomReplData>(
8012684Sodanrc@yahoo.com.br                    candidate->replacementData)->valid) {
8112684Sodanrc@yahoo.com.br            victim = candidate;
8212600Sodanrc@yahoo.com.br            break;
8312600Sodanrc@yahoo.com.br        }
8412600Sodanrc@yahoo.com.br    }
8512600Sodanrc@yahoo.com.br
8612684Sodanrc@yahoo.com.br    return victim;
8712684Sodanrc@yahoo.com.br}
8812600Sodanrc@yahoo.com.br
8912684Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData>
9012684Sodanrc@yahoo.com.brRandomRP::instantiateEntry()
9112684Sodanrc@yahoo.com.br{
9212718Sodanrc@yahoo.com.br    return std::shared_ptr<ReplacementData>(new RandomReplData());
9312600Sodanrc@yahoo.com.br}
9412600Sodanrc@yahoo.com.br
9512600Sodanrc@yahoo.com.brRandomRP*
9612600Sodanrc@yahoo.com.brRandomRPParams::create()
9712600Sodanrc@yahoo.com.br{
9812600Sodanrc@yahoo.com.br    return new RandomRP(this);
9912600Sodanrc@yahoo.com.br}
100