ruby: initialize replacement policies with their own simobjs
[gem5.git] / src / mem / ruby / structures / PseudoLRUPolicy.cc
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
33
34
35 PseudoLRUPolicy::PseudoLRUPolicy(const Params * p)
36 : AbstractReplacementPolicy(p)
37 {
38 // associativity cannot exceed capacity of tree representation
39 assert(m_num_sets > 0 &&
40 m_assoc > 1 &&
41 m_assoc <= (int64) sizeof(uint64)*4);
42
43 m_trees = NULL;
44 m_num_levels = 0;
45
46 m_effective_assoc = 1;
47 while (m_effective_assoc < m_assoc) {
48 // effective associativity is ceiling power of 2
49 m_effective_assoc <<= 1;
50 }
51 int tmp_assoc = m_effective_assoc;
52 while (true) {
53 tmp_assoc /= 2;
54 if(!tmp_assoc) break;
55 m_num_levels++;
56 }
57 assert(m_num_levels < sizeof(unsigned int)*4);
58 m_trees = new uint64[m_num_sets];
59 for (unsigned i = 0; i < m_num_sets; i++) {
60 m_trees[i] = 0;
61 }
62 }
63
64 PseudoLRUPolicy *
65 PseudoLRUReplacementPolicyParams::create()
66 {
67 return new PseudoLRUPolicy(this);
68 }
69
70
71 PseudoLRUPolicy::~PseudoLRUPolicy()
72 {
73 if (m_trees != NULL)
74 delete[] m_trees;
75 }
76
77 void
78 PseudoLRUPolicy::touch(int64 set, int64 index, Tick time)
79 {
80 assert(index >= 0 && index < m_assoc);
81 assert(set >= 0 && set < m_num_sets);
82
83 int tree_index = 0;
84 int node_val;
85 for (int i = m_num_levels - 1; i >= 0; i--) {
86 node_val = (index >> i)&1;
87 if (node_val)
88 m_trees[set] |= node_val << tree_index;
89 else
90 m_trees[set] &= ~(1 << tree_index);
91 tree_index = node_val ? (tree_index*2)+2 : (tree_index*2)+1;
92 }
93 m_last_ref_ptr[set][index] = time;
94 }
95
96 int64
97 PseudoLRUPolicy::getVictim(int64 set) const
98 {
99 int64 index = 0;
100
101 int tree_index = 0;
102 int node_val;
103 for (unsigned i = 0; i < m_num_levels; i++){
104 node_val = (m_trees[set] >> tree_index) & 1;
105 index += node_val ? 0 : (m_effective_assoc >> (i + 1));
106 tree_index = node_val ? (tree_index * 2) + 1 : (tree_index * 2) + 2;
107 }
108 assert(index >= 0 && index < m_effective_assoc);
109
110 /* return either the found index or the max possible index */
111 /* NOTE: this is not a fair replacement when assoc is not a power of 2 */
112 return (index > (m_assoc - 1)) ? m_assoc - 1 : index;
113 }