second_chance_rp.cc revision 12685
112685Sodanrc@yahoo.com.br/** 212685Sodanrc@yahoo.com.br * Copyright (c) 2018 Inria 312685Sodanrc@yahoo.com.br * All rights reserved. 412685Sodanrc@yahoo.com.br * 512685Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without 612685Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are 712685Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright 812685Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer; 912685Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright 1012685Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the 1112685Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution; 1212685Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its 1312685Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from 1412685Sodanrc@yahoo.com.br * this software without specific prior written permission. 1512685Sodanrc@yahoo.com.br * 1612685Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712685Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812685Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912685Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012685Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112685Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212685Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312685Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412685Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512685Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612685Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712685Sodanrc@yahoo.com.br * 2812685Sodanrc@yahoo.com.br * Authors: Daniel Carvalho 2912685Sodanrc@yahoo.com.br */ 3012685Sodanrc@yahoo.com.br 3112685Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/second_chance_rp.hh" 3212685Sodanrc@yahoo.com.br 3312685Sodanrc@yahoo.com.brSecondChanceRP::SecondChanceRP(const Params *p) 3412685Sodanrc@yahoo.com.br : FIFORP(p) 3512685Sodanrc@yahoo.com.br{ 3612685Sodanrc@yahoo.com.br} 3712685Sodanrc@yahoo.com.br 3812685Sodanrc@yahoo.com.brvoid 3912685Sodanrc@yahoo.com.brSecondChanceRP::useSecondChance( 4012685Sodanrc@yahoo.com.br const std::shared_ptr<SecondChanceReplData>& replacement_data) const 4112685Sodanrc@yahoo.com.br{ 4212685Sodanrc@yahoo.com.br // Reset FIFO data 4312685Sodanrc@yahoo.com.br FIFORP::reset(replacement_data); 4412685Sodanrc@yahoo.com.br 4512685Sodanrc@yahoo.com.br // Use second chance 4612685Sodanrc@yahoo.com.br replacement_data->hasSecondChance = false; 4712685Sodanrc@yahoo.com.br} 4812685Sodanrc@yahoo.com.br 4912685Sodanrc@yahoo.com.brvoid 5012685Sodanrc@yahoo.com.brSecondChanceRP::invalidate( 5112685Sodanrc@yahoo.com.br const std::shared_ptr<ReplacementData>& replacement_data) const 5212685Sodanrc@yahoo.com.br{ 5312685Sodanrc@yahoo.com.br FIFORP::invalidate(replacement_data); 5412685Sodanrc@yahoo.com.br 5512685Sodanrc@yahoo.com.br // Do not give a second chance to invalid entries 5612685Sodanrc@yahoo.com.br std::static_pointer_cast<SecondChanceReplData>( 5712685Sodanrc@yahoo.com.br replacement_data)->hasSecondChance = false; 5812685Sodanrc@yahoo.com.br} 5912685Sodanrc@yahoo.com.br 6012685Sodanrc@yahoo.com.brvoid 6112685Sodanrc@yahoo.com.brSecondChanceRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const 6212685Sodanrc@yahoo.com.br{ 6312685Sodanrc@yahoo.com.br FIFORP::touch(replacement_data); 6412685Sodanrc@yahoo.com.br 6512685Sodanrc@yahoo.com.br // Whenever an entry is touched, it is given a second chance 6612685Sodanrc@yahoo.com.br std::static_pointer_cast<SecondChanceReplData>( 6712685Sodanrc@yahoo.com.br replacement_data)->hasSecondChance = true; 6812685Sodanrc@yahoo.com.br} 6912685Sodanrc@yahoo.com.br 7012685Sodanrc@yahoo.com.brvoid 7112685Sodanrc@yahoo.com.brSecondChanceRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const 7212685Sodanrc@yahoo.com.br{ 7312685Sodanrc@yahoo.com.br FIFORP::reset(replacement_data); 7412685Sodanrc@yahoo.com.br 7512685Sodanrc@yahoo.com.br // Entries are inserted with a second chance 7612685Sodanrc@yahoo.com.br std::static_pointer_cast<SecondChanceReplData>( 7712685Sodanrc@yahoo.com.br replacement_data)->hasSecondChance = true; 7812685Sodanrc@yahoo.com.br} 7912685Sodanrc@yahoo.com.br 8012685Sodanrc@yahoo.com.brReplaceableEntry* 8112685Sodanrc@yahoo.com.brSecondChanceRP::getVictim(const ReplacementCandidates& candidates) const 8212685Sodanrc@yahoo.com.br{ 8312685Sodanrc@yahoo.com.br // There must be at least one replacement candidate 8412685Sodanrc@yahoo.com.br assert(candidates.size() > 0); 8512685Sodanrc@yahoo.com.br 8612685Sodanrc@yahoo.com.br // Search for invalid entries, as they have the eviction priority 8712685Sodanrc@yahoo.com.br for (const auto& candidate : candidates) { 8812685Sodanrc@yahoo.com.br // Cast candidate's replacement data 8912685Sodanrc@yahoo.com.br std::shared_ptr<SecondChanceReplData> candidate_replacement_data = 9012685Sodanrc@yahoo.com.br std::static_pointer_cast<SecondChanceReplData>( 9112685Sodanrc@yahoo.com.br candidate->replacementData); 9212685Sodanrc@yahoo.com.br 9312685Sodanrc@yahoo.com.br // Stop iteration if found an invalid entry 9412685Sodanrc@yahoo.com.br if ((candidate_replacement_data->tickInserted == Tick(0)) && 9512685Sodanrc@yahoo.com.br !candidate_replacement_data->hasSecondChance) { 9612685Sodanrc@yahoo.com.br return candidate; 9712685Sodanrc@yahoo.com.br } 9812685Sodanrc@yahoo.com.br } 9912685Sodanrc@yahoo.com.br 10012685Sodanrc@yahoo.com.br // Visit all candidates to find victim 10112685Sodanrc@yahoo.com.br ReplaceableEntry* victim = candidates[0]; 10212685Sodanrc@yahoo.com.br bool search_victim = true; 10312685Sodanrc@yahoo.com.br while (search_victim) { 10412685Sodanrc@yahoo.com.br // Do a FIFO victim search 10512685Sodanrc@yahoo.com.br victim = FIFORP::getVictim(candidates); 10612685Sodanrc@yahoo.com.br 10712685Sodanrc@yahoo.com.br // Cast victim's replacement data for code readability 10812685Sodanrc@yahoo.com.br std::shared_ptr<SecondChanceReplData> victim_replacement_data = 10912685Sodanrc@yahoo.com.br std::static_pointer_cast<SecondChanceReplData>( 11012685Sodanrc@yahoo.com.br victim->replacementData); 11112685Sodanrc@yahoo.com.br 11212685Sodanrc@yahoo.com.br // If victim has a second chance, use it and repeat search 11312685Sodanrc@yahoo.com.br if (victim_replacement_data->hasSecondChance) { 11412685Sodanrc@yahoo.com.br useSecondChance(victim_replacement_data); 11512685Sodanrc@yahoo.com.br } else { 11612685Sodanrc@yahoo.com.br // Found victim 11712685Sodanrc@yahoo.com.br search_victim = false; 11812685Sodanrc@yahoo.com.br } 11912685Sodanrc@yahoo.com.br } 12012685Sodanrc@yahoo.com.br 12112685Sodanrc@yahoo.com.br return victim; 12212685Sodanrc@yahoo.com.br} 12312685Sodanrc@yahoo.com.br 12412685Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData> 12512685Sodanrc@yahoo.com.brSecondChanceRP::instantiateEntry() 12612685Sodanrc@yahoo.com.br{ 12712685Sodanrc@yahoo.com.br return std::shared_ptr<ReplacementData>(new SecondChanceReplData()); 12812685Sodanrc@yahoo.com.br} 12912685Sodanrc@yahoo.com.br 13012685Sodanrc@yahoo.com.brSecondChanceRP* 13112685Sodanrc@yahoo.com.brSecondChanceRPParams::create() 13212685Sodanrc@yahoo.com.br{ 13312685Sodanrc@yahoo.com.br return new SecondChanceRP(this); 13412685Sodanrc@yahoo.com.br} 135