PseudoLRUPolicy.cc (11049:dfb0aa3f0649) PseudoLRUPolicy.cc (11061:25b53a7195f7)
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;

--- 24 unchanged lines hidden (view full) ---

33
34
35PseudoLRUPolicy::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 &&
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;

--- 24 unchanged lines hidden (view full) ---

33
34
35PseudoLRUPolicy::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);
41 m_assoc <= (int64_t) sizeof(uint64_t)*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);
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];
58 m_trees = new uint64_t[m_num_sets];
59 for (unsigned i = 0; i < m_num_sets; i++) {
60 m_trees[i] = 0;
61 }
62}
63
64PseudoLRUPolicy *
65PseudoLRUReplacementPolicyParams::create()
66{
67 return new PseudoLRUPolicy(this);
68}
69
70
71PseudoLRUPolicy::~PseudoLRUPolicy()
72{
73 if (m_trees != NULL)
74 delete[] m_trees;
75}
76
77void
59 for (unsigned i = 0; i < m_num_sets; i++) {
60 m_trees[i] = 0;
61 }
62}
63
64PseudoLRUPolicy *
65PseudoLRUReplacementPolicyParams::create()
66{
67 return new PseudoLRUPolicy(this);
68}
69
70
71PseudoLRUPolicy::~PseudoLRUPolicy()
72{
73 if (m_trees != NULL)
74 delete[] m_trees;
75}
76
77void
78PseudoLRUPolicy::touch(int64 set, int64 index, Tick time)
78PseudoLRUPolicy::touch(int64_t set, int64_t 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
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
96int64
97PseudoLRUPolicy::getVictim(int64 set) const
96int64_t
97PseudoLRUPolicy::getVictim(int64_t set) const
98{
98{
99 int64 index = 0;
99 int64_t 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}
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}