2 * Copyright (c) 2013 Advanced Micro Devices, Inc
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.
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.
31 #include "mem/ruby/structures/PseudoLRUPolicy.hh"
35 PseudoLRUPolicy::PseudoLRUPolicy(const Params
* p
)
36 : AbstractReplacementPolicy(p
)
38 // associativity cannot exceed capacity of tree representation
39 assert(m_num_sets
> 0 &&
41 m_assoc
<= (int64
) sizeof(uint64
)*4);
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;
51 int tmp_assoc
= m_effective_assoc
;
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
++) {
65 PseudoLRUReplacementPolicyParams::create()
67 return new PseudoLRUPolicy(this);
71 PseudoLRUPolicy::~PseudoLRUPolicy()
78 PseudoLRUPolicy::touch(int64 set
, int64 index
, Tick time
)
80 assert(index
>= 0 && index
< m_assoc
);
81 assert(set
>= 0 && set
< m_num_sets
);
85 for (int i
= m_num_levels
- 1; i
>= 0; i
--) {
86 node_val
= (index
>> i
)&1;
88 m_trees
[set
] |= node_val
<< tree_index
;
90 m_trees
[set
] &= ~(1 << tree_index
);
91 tree_index
= node_val
? (tree_index
*2)+2 : (tree_index
*2)+1;
93 m_last_ref_ptr
[set
][index
] = time
;
97 PseudoLRUPolicy::getVictim(int64 set
) const
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;
108 assert(index
>= 0 && index
< m_effective_assoc
);
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
;