1/* 2 * Copyright (c) 2013 Advanced Micro Devices, Inc 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; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Author: Derek Hower 29 */ 30 31#include "mem/ruby/structures/PseudoLRUPolicy.hh" 32 33PseudoLRUPolicy::PseudoLRUPolicy(const Params * p) 34 : AbstractReplacementPolicy(p) 35{ 36 // associativity cannot exceed capacity of tree representation 37 assert(m_num_sets > 0 && 38 m_assoc > 1 && 39 m_assoc <= (int64_t) sizeof(uint64_t)*4); 40 41 m_trees = NULL; 42 m_num_levels = 0; 43 44 m_effective_assoc = 1; 45 while (m_effective_assoc < m_assoc) { 46 // effective associativity is ceiling power of 2 47 m_effective_assoc <<= 1; 48 } 49 int tmp_assoc = m_effective_assoc; 50 while (true) { 51 tmp_assoc /= 2; 52 if (!tmp_assoc) break; 53 m_num_levels++; 54 } 55 assert(m_num_levels < sizeof(unsigned int)*4); 56 m_trees = new uint64_t[m_num_sets]; 57 for (unsigned i = 0; i < m_num_sets; i++) { 58 m_trees[i] = 0; 59 } 60} 61 62PseudoLRUPolicy * 63PseudoLRUReplacementPolicyParams::create() 64{ 65 return new PseudoLRUPolicy(this); 66} 67 68 69PseudoLRUPolicy::~PseudoLRUPolicy() 70{ 71 if (m_trees != NULL) 72 delete[] m_trees; 73} 74 75void 76PseudoLRUPolicy::touch(int64_t set, int64_t index, Tick time) 77{ 78 assert(index >= 0 && index < m_assoc); 79 assert(set >= 0 && set < m_num_sets); 80 81 int tree_index = 0; 82 int node_val; 83 for (int i = m_num_levels - 1; i >= 0; i--) { 84 node_val = (index >> i)&1; 85 if (node_val) 86 m_trees[set] |= node_val << tree_index; 87 else 88 m_trees[set] &= ~(1 << tree_index); 89 tree_index = node_val ? (tree_index*2)+2 : (tree_index*2)+1; 90 } 91 m_last_ref_ptr[set][index] = time; 92} 93 94int64_t 95PseudoLRUPolicy::getVictim(int64_t set) const 96{ 97 int64_t index = 0; 98 99 int tree_index = 0; 100 int node_val; 101 for (unsigned i = 0; i < m_num_levels; i++){ 102 node_val = (m_trees[set] >> tree_index) & 1; 103 index += node_val ? 0 : (m_effective_assoc >> (i + 1)); 104 tree_index = node_val ? (tree_index * 2) + 1 : (tree_index * 2) + 2; 105 } 106 assert(index >= 0 && index < m_effective_assoc); 107 108 /* return either the found index or the max possible index */ 109 /* NOTE: this is not a fair replacement when assoc is not a power of 2 */ 110 return (index > (m_assoc - 1)) ? m_assoc - 1 : index; 111} 112