gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PseudoLRUPolicy.cc
Go to the documentation of this file.
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 
32 
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 
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 
63 PseudoLRUReplacementPolicyParams::create()
64 {
65  return new PseudoLRUPolicy(this);
66 }
67 
68 
70 {
71  if (m_trees != NULL)
72  delete[] m_trees;
73 }
74 
75 void
76 PseudoLRUPolicy::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 
94 int64_t
95 PseudoLRUPolicy::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 }
unsigned m_assoc
total number of sets
Bitfield< 30, 0 > index
Bitfield< 7 > i
Definition: miscregs.hh:1378
Implementation of tree-based pseudo-LRU replacement.
Tick ** m_last_ref_ptr
set associativity
uint64_t * m_trees
number of levels in the tree
uint64_t Tick
Tick count type.
Definition: types.hh:63
unsigned int m_num_levels
nearest (to ceiling) power of 2
int64_t getVictim(int64_t set) const
PseudoLRUPolicy(const Params *p)
void touch(int64_t set, int64_t way, Tick time)
unsigned int m_effective_assoc
Bitfield< 0 > p

Generated on Fri Jun 9 2017 13:03:50 for gem5 by doxygen 1.8.6