lfu_rp.cc revision 12684
112628Sodanrc@yahoo.com.br/** 212628Sodanrc@yahoo.com.br * Copyright (c) 2018 Inria 312628Sodanrc@yahoo.com.br * All rights reserved. 412628Sodanrc@yahoo.com.br * 512628Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without 612628Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are 712628Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright 812628Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer; 912628Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright 1012628Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the 1112628Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution; 1212628Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its 1312628Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from 1412628Sodanrc@yahoo.com.br * this software without specific prior written permission. 1512628Sodanrc@yahoo.com.br * 1612628Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712628Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812628Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912628Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012628Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112628Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212628Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312628Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412628Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512628Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612628Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712628Sodanrc@yahoo.com.br * 2812628Sodanrc@yahoo.com.br * Authors: Daniel Carvalho 2912628Sodanrc@yahoo.com.br */ 3012628Sodanrc@yahoo.com.br 3112628Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/lfu_rp.hh" 3212628Sodanrc@yahoo.com.br 3312684Sodanrc@yahoo.com.br#include <memory> 3412628Sodanrc@yahoo.com.br 3512628Sodanrc@yahoo.com.brLFURP::LFURP(const Params *p) 3612628Sodanrc@yahoo.com.br : BaseReplacementPolicy(p) 3712628Sodanrc@yahoo.com.br{ 3812628Sodanrc@yahoo.com.br} 3912628Sodanrc@yahoo.com.br 4012684Sodanrc@yahoo.com.brvoid 4112684Sodanrc@yahoo.com.brLFURP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data) 4212684Sodanrc@yahoo.com.brconst 4312684Sodanrc@yahoo.com.br{ 4412684Sodanrc@yahoo.com.br // Reset reference count 4512684Sodanrc@yahoo.com.br std::static_pointer_cast<LFUReplData>(replacement_data)->refCount = 0; 4612684Sodanrc@yahoo.com.br} 4712684Sodanrc@yahoo.com.br 4812684Sodanrc@yahoo.com.brvoid 4912684Sodanrc@yahoo.com.brLFURP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const 5012684Sodanrc@yahoo.com.br{ 5112684Sodanrc@yahoo.com.br // Update reference count 5212684Sodanrc@yahoo.com.br std::static_pointer_cast<LFUReplData>(replacement_data)->refCount++; 5312684Sodanrc@yahoo.com.br} 5412684Sodanrc@yahoo.com.br 5512684Sodanrc@yahoo.com.brvoid 5612684Sodanrc@yahoo.com.brLFURP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const 5712684Sodanrc@yahoo.com.br{ 5812684Sodanrc@yahoo.com.br // Reset reference count 5912684Sodanrc@yahoo.com.br std::static_pointer_cast<LFUReplData>(replacement_data)->refCount = 1; 6012684Sodanrc@yahoo.com.br} 6112684Sodanrc@yahoo.com.br 6212684Sodanrc@yahoo.com.brReplaceableEntry* 6312684Sodanrc@yahoo.com.brLFURP::getVictim(const ReplacementCandidates& candidates) const 6412628Sodanrc@yahoo.com.br{ 6512628Sodanrc@yahoo.com.br // There must be at least one replacement candidate 6612628Sodanrc@yahoo.com.br assert(candidates.size() > 0); 6712628Sodanrc@yahoo.com.br 6812628Sodanrc@yahoo.com.br // Visit all candidates to find victim 6912684Sodanrc@yahoo.com.br ReplaceableEntry* victim = candidates[0]; 7012628Sodanrc@yahoo.com.br for (const auto& candidate : candidates) { 7112684Sodanrc@yahoo.com.br // Update victim entry if necessary 7212684Sodanrc@yahoo.com.br if (std::static_pointer_cast<LFUReplData>( 7312684Sodanrc@yahoo.com.br candidate->replacementData)->refCount < 7412684Sodanrc@yahoo.com.br std::static_pointer_cast<LFUReplData>( 7512684Sodanrc@yahoo.com.br victim->replacementData)->refCount) { 7612684Sodanrc@yahoo.com.br victim = candidate; 7712628Sodanrc@yahoo.com.br } 7812628Sodanrc@yahoo.com.br } 7912628Sodanrc@yahoo.com.br 8012684Sodanrc@yahoo.com.br return victim; 8112684Sodanrc@yahoo.com.br} 8212628Sodanrc@yahoo.com.br 8312684Sodanrc@yahoo.com.brstd::shared_ptr<ReplacementData> 8412684Sodanrc@yahoo.com.brLFURP::instantiateEntry() 8512684Sodanrc@yahoo.com.br{ 8612684Sodanrc@yahoo.com.br return std::shared_ptr<ReplacementData>(new LFUReplData()); 8712628Sodanrc@yahoo.com.br} 8812628Sodanrc@yahoo.com.br 8912628Sodanrc@yahoo.com.brLFURP* 9012628Sodanrc@yahoo.com.brLFURPParams::create() 9112628Sodanrc@yahoo.com.br{ 9212628Sodanrc@yahoo.com.br return new LFURP(this); 9312628Sodanrc@yahoo.com.br} 94