2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
28 * Authors: Erik Hallnor
34 * Definitions of the Generational replacement policy.
39 #include "base/misc.hh"
40 #include "base/types.hh"
41 #include "mem/cache/tags/iic_repl/gen.hh"
42 #include "mem/cache/tags/iic.hh"
43 #include "params/GenRepl.hh"
47 GenRepl::GenRepl(const Params
*p
) // fix this, should be set by cache
48 : Repl(p
), num_pools(p
->num_pools
), fresh_res(p
->fresh_res
),
49 pool_res(p
->pool_res
), num_entries(0), num_pool_entries(0), misses(0),
50 pools(new GenPool
[num_pools
+1])
66 if (!(num_pool_entries
>0)) {
67 fatal("No blks available to replace");
71 for (i
= 0; i
< num_pools
; i
++) {
72 while ((re
= pools
[i
].pop())) {
74 // Remove invalidated entries
79 if (iic
->clearRef(re
->tag_ptr
)) {
80 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
92 fatal("No replacement found");
97 GenRepl::getNRepl(int n
)
102 if (!(num_pool_entries
>(n
-1))) {
103 fatal("Not enough blks available to replace");
106 num_pool_entries
-= n
;
107 tmp
= new unsigned long[n
]; /* array of cache_blk pointers */
109 for (i
= 0; i
< num_pools
&& blk_index
< n
; i
++) {
110 while (blk_index
< n
&& (re
= pools
[i
].pop())) {
111 // Remove invalidated entries
116 if (iic
->clearRef(re
->tag_ptr
)) {
117 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
120 tmp
[blk_index
] = re
->tag_ptr
;
129 /* search the fresh pool */
131 fatal("No N replacements found");
136 GenRepl::doAdvance(std::list
<unsigned long> &demoted
)
142 for (i
=0; i
<num_pools
; i
++) {
143 while (misses
-pools
[i
].oldest
> pool_res
&& (re
= pools
[i
].pop())!=NULL
) {
144 if (iic
->clearRef(re
->tag_ptr
)) {
145 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
146 /** @todo Not really demoted, but use it for now. */
147 demoted
.push_back(re
->tag_ptr
);
148 advance_pool
.sample(i
);
151 pools
[(((i
-1)<0)?i
:i
-1)].push(re
, misses
);
152 demoted
.push_back(re
->tag_ptr
);
153 demote_pool
.sample(i
);
156 num_seen
+= pools
[i
].size
;
158 while (misses
-pools
[num_pools
].oldest
> fresh_res
159 && (re
= pools
[num_pools
].pop())!=NULL
) {
161 if (iic
->clearRef(re
->tag_ptr
)) {
162 pools
[num_pools
/2].push(re
, misses
);
163 /** @todo Not really demoted, but use it for now. */
164 demoted
.push_back(re
->tag_ptr
);
165 advance_pool
.sample(num_pools
);
168 pools
[num_pools
/2-1].push(re
, misses
);
169 demoted
.push_back(re
->tag_ptr
);
170 demote_pool
.sample(num_pools
);
176 GenRepl::add(unsigned long tag_index
)
178 GenReplEntry
*re
= new GenReplEntry
;
179 re
->tag_ptr
= tag_index
;
181 pools
[num_pools
].push(re
, misses
);
187 GenRepl::regStats(const string name
)
189 using namespace Stats
;
191 /** GEN statistics */
194 .name(name
+ ".repl_pool_dist")
195 .desc("Dist. of Repl. across pools")
201 .name(name
+ ".advance_pool_dist")
202 .desc("Dist. of Repl. across pools")
208 .name(name
+ ".demote_pool_dist")
209 .desc("Dist. of Repl. across pools")
215 GenRepl::fixTag(void* _re
, unsigned long old_index
, unsigned long new_index
)
217 GenReplEntry
*re
= (GenReplEntry
*)_re
;
219 if (re
->tag_ptr
== old_index
) {
220 re
->tag_ptr
= new_index
;
223 fatal("Repl entry: tag ptrs do not match");
228 GenRepl::findTagPtr(unsigned long index
)
230 for (int i
= 0; i
< num_pools
+ 1; ++i
) {
231 list
<GenReplEntry
*>::const_iterator iter
= pools
[i
].entries
.begin();
232 list
<GenReplEntry
*>::const_iterator end
= pools
[i
].entries
.end();
233 for (; iter
!= end
; ++iter
) {
234 if ((*iter
)->valid
&& (*iter
)->tag_ptr
== index
) {
243 GenReplParams::create()
245 return new GenRepl(this);