brrip_rp.cc (12727:56c23b54bcb1) | brrip_rp.cc (14211:acfef4916339) |
---|---|
1/** 2 * Copyright (c) 2018 Inria 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 25 unchanged lines hidden (view full) --- 34#include <memory> 35 36#include "base/logging.hh" // For fatal_if 37#include "base/random.hh" 38#include "params/BRRIPRP.hh" 39 40BRRIPRP::BRRIPRP(const Params *p) 41 : BaseReplacementPolicy(p), | 1/** 2 * Copyright (c) 2018 Inria 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 25 unchanged lines hidden (view full) --- 34#include <memory> 35 36#include "base/logging.hh" // For fatal_if 37#include "base/random.hh" 38#include "params/BRRIPRP.hh" 39 40BRRIPRP::BRRIPRP(const Params *p) 41 : BaseReplacementPolicy(p), |
42 maxRRPV(p->max_RRPV), hitPriority(p->hit_priority), btp(p->btp) | 42 numRRPVBits(p->num_bits), hitPriority(p->hit_priority), btp(p->btp) |
43{ | 43{ |
44 fatal_if(maxRRPV <= 0, "max_RRPV should be greater than zero.\n"); | 44 fatal_if(numRRPVBits <= 0, "There should be at least one bit per RRPV.\n"); |
45} 46 47void 48BRRIPRP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data) 49const 50{ 51 std::shared_ptr<BRRIPReplData> casted_replacement_data = 52 std::static_pointer_cast<BRRIPReplData>(replacement_data); 53 | 45} 46 47void 48BRRIPRP::invalidate(const std::shared_ptr<ReplacementData>& replacement_data) 49const 50{ 51 std::shared_ptr<BRRIPReplData> casted_replacement_data = 52 std::static_pointer_cast<BRRIPReplData>(replacement_data); 53 |
54 // Set RRPV to an invalid distance 55 casted_replacement_data->rrpv = maxRRPV + 1; | 54 // Invalidate entry 55 casted_replacement_data->valid = false; |
56} 57 58void 59BRRIPRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const 60{ 61 std::shared_ptr<BRRIPReplData> casted_replacement_data = 62 std::static_pointer_cast<BRRIPReplData>(replacement_data); 63 64 // Update RRPV if not 0 yet 65 // Every hit in HP mode makes the entry the last to be evicted, while 66 // in FP mode a hit makes the entry less likely to be evicted 67 if (hitPriority) { | 56} 57 58void 59BRRIPRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const 60{ 61 std::shared_ptr<BRRIPReplData> casted_replacement_data = 62 std::static_pointer_cast<BRRIPReplData>(replacement_data); 63 64 // Update RRPV if not 0 yet 65 // Every hit in HP mode makes the entry the last to be evicted, while 66 // in FP mode a hit makes the entry less likely to be evicted 67 if (hitPriority) { |
68 casted_replacement_data->rrpv = 0; 69 } else if (casted_replacement_data->rrpv > 0) { | 68 casted_replacement_data->rrpv.reset(); 69 } else { |
70 casted_replacement_data->rrpv--; 71 } 72} 73 74void 75BRRIPRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const 76{ 77 std::shared_ptr<BRRIPReplData> casted_replacement_data = 78 std::static_pointer_cast<BRRIPReplData>(replacement_data); 79 80 // Reset RRPV 81 // Replacement data is inserted as "long re-reference" if lower than btp, 82 // "distant re-reference" otherwise | 70 casted_replacement_data->rrpv--; 71 } 72} 73 74void 75BRRIPRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const 76{ 77 std::shared_ptr<BRRIPReplData> casted_replacement_data = 78 std::static_pointer_cast<BRRIPReplData>(replacement_data); 79 80 // Reset RRPV 81 // Replacement data is inserted as "long re-reference" if lower than btp, 82 // "distant re-reference" otherwise |
83 casted_replacement_data->rrpv.saturate(); |
|
83 if (random_mt.random<unsigned>(1, 100) <= btp) { | 84 if (random_mt.random<unsigned>(1, 100) <= btp) { |
84 casted_replacement_data->rrpv = maxRRPV-1; 85 } else { 86 casted_replacement_data->rrpv = maxRRPV; | 85 casted_replacement_data->rrpv--; |
87 } | 86 } |
87 88 // Mark entry as ready to be used 89 casted_replacement_data->valid = true; |
|
88} 89 90ReplaceableEntry* 91BRRIPRP::getVictim(const ReplacementCandidates& candidates) const 92{ 93 // There must be at least one replacement candidate 94 assert(candidates.size() > 0); 95 96 // Use first candidate as dummy victim 97 ReplaceableEntry* victim = candidates[0]; 98 99 // Store victim->rrpv in a variable to improve code readability 100 int victim_RRPV = std::static_pointer_cast<BRRIPReplData>( 101 victim->replacementData)->rrpv; 102 103 // Visit all candidates to find victim 104 for (const auto& candidate : candidates) { | 90} 91 92ReplaceableEntry* 93BRRIPRP::getVictim(const ReplacementCandidates& candidates) const 94{ 95 // There must be at least one replacement candidate 96 assert(candidates.size() > 0); 97 98 // Use first candidate as dummy victim 99 ReplaceableEntry* victim = candidates[0]; 100 101 // Store victim->rrpv in a variable to improve code readability 102 int victim_RRPV = std::static_pointer_cast<BRRIPReplData>( 103 victim->replacementData)->rrpv; 104 105 // Visit all candidates to find victim 106 for (const auto& candidate : candidates) { |
105 // Get candidate's rrpv 106 int candidate_RRPV = std::static_pointer_cast<BRRIPReplData>( 107 candidate->replacementData)->rrpv; | 107 std::shared_ptr<BRRIPReplData> candidate_repl_data = 108 std::static_pointer_cast<BRRIPReplData>( 109 candidate->replacementData); |
108 109 // Stop searching for victims if an invalid entry is found | 110 111 // Stop searching for victims if an invalid entry is found |
110 if (candidate_RRPV == maxRRPV + 1) { | 112 if (!candidate_repl_data->valid) { |
111 return candidate; | 113 return candidate; |
114 } 115 |
|
112 // Update victim entry if necessary | 116 // Update victim entry if necessary |
113 } else if (candidate_RRPV > victim_RRPV) { | 117 int candidate_RRPV = candidate_repl_data->rrpv; 118 if (candidate_RRPV > victim_RRPV) { |
114 victim = candidate; 115 victim_RRPV = candidate_RRPV; 116 } 117 } 118 119 // Get difference of victim's RRPV to the highest possible RRPV in 120 // order to update the RRPV of all the other entries accordingly | 119 victim = candidate; 120 victim_RRPV = candidate_RRPV; 121 } 122 } 123 124 // Get difference of victim's RRPV to the highest possible RRPV in 125 // order to update the RRPV of all the other entries accordingly |
121 int diff = maxRRPV - victim_RRPV; | 126 int diff = std::static_pointer_cast<BRRIPReplData>( 127 victim->replacementData)->rrpv.saturate(); |
122 123 // No need to update RRPV if there is no difference 124 if (diff > 0){ 125 // Update RRPV of all candidates 126 for (const auto& candidate : candidates) { 127 std::static_pointer_cast<BRRIPReplData>( 128 candidate->replacementData)->rrpv += diff; 129 } 130 } 131 132 return victim; 133} 134 135std::shared_ptr<ReplacementData> 136BRRIPRP::instantiateEntry() 137{ | 128 129 // No need to update RRPV if there is no difference 130 if (diff > 0){ 131 // Update RRPV of all candidates 132 for (const auto& candidate : candidates) { 133 std::static_pointer_cast<BRRIPReplData>( 134 candidate->replacementData)->rrpv += diff; 135 } 136 } 137 138 return victim; 139} 140 141std::shared_ptr<ReplacementData> 142BRRIPRP::instantiateEntry() 143{ |
138 return std::shared_ptr<ReplacementData>(new BRRIPReplData(maxRRPV)); | 144 return std::shared_ptr<ReplacementData>(new BRRIPReplData(numRRPVBits)); |
139} 140 141BRRIPRP* 142BRRIPRPParams::create() 143{ 144 return new BRRIPRP(this); 145} | 145} 146 147BRRIPRP* 148BRRIPRPParams::create() 149{ 150 return new BRRIPRP(this); 151} |