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 "mem/cache/tags/iic.hh"
41 #include "mem/cache/tags/repl/gen.hh"
42 #include "params/GenRepl.hh"
43 #include "sim/host.hh"
47 GenRepl::GenRepl(const string
&_name
,
50 int _pool_res
) // fix this, should be set by cache
53 num_pools
= _num_pools
;
54 fresh_res
= _fresh_res
;
59 pools
= new GenPool
[num_pools
+1];
74 if (!(num_pool_entries
>0)) {
75 fatal("No blks available to replace");
79 for (i
= 0; i
< num_pools
; i
++) {
80 while ((re
= pools
[i
].pop())) {
82 // Remove invalidated entries
87 if (iic
->clearRef(re
->tag_ptr
)) {
88 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
100 fatal("No replacement found");
105 GenRepl::getNRepl(int n
)
110 if (!(num_pool_entries
>(n
-1))) {
111 fatal("Not enough blks available to replace");
114 num_pool_entries
-= n
;
115 tmp
= new unsigned long[n
]; /* array of cache_blk pointers */
117 for (i
= 0; i
< num_pools
&& blk_index
< n
; i
++) {
118 while (blk_index
< n
&& (re
= pools
[i
].pop())) {
119 // Remove invalidated entries
124 if (iic
->clearRef(re
->tag_ptr
)) {
125 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
128 tmp
[blk_index
] = re
->tag_ptr
;
137 /* search the fresh pool */
139 fatal("No N replacements found");
144 GenRepl::doAdvance(std::list
<unsigned long> &demoted
)
150 for (i
=0; i
<num_pools
; i
++) {
151 while (misses
-pools
[i
].oldest
> pool_res
&& (re
= pools
[i
].pop())!=NULL
) {
152 if (iic
->clearRef(re
->tag_ptr
)) {
153 pools
[(((i
+1)== num_pools
)? i
:i
+1)].push(re
, misses
);
154 /** @todo Not really demoted, but use it for now. */
155 demoted
.push_back(re
->tag_ptr
);
156 advance_pool
.sample(i
);
159 pools
[(((i
-1)<0)?i
:i
-1)].push(re
, misses
);
160 demoted
.push_back(re
->tag_ptr
);
161 demote_pool
.sample(i
);
164 num_seen
+= pools
[i
].size
;
166 while (misses
-pools
[num_pools
].oldest
> fresh_res
167 && (re
= pools
[num_pools
].pop())!=NULL
) {
169 if (iic
->clearRef(re
->tag_ptr
)) {
170 pools
[num_pools
/2].push(re
, misses
);
171 /** @todo Not really demoted, but use it for now. */
172 demoted
.push_back(re
->tag_ptr
);
173 advance_pool
.sample(num_pools
);
176 pools
[num_pools
/2-1].push(re
, misses
);
177 demoted
.push_back(re
->tag_ptr
);
178 demote_pool
.sample(num_pools
);
184 GenRepl::add(unsigned long tag_index
)
186 GenReplEntry
*re
= new GenReplEntry
;
187 re
->tag_ptr
= tag_index
;
189 pools
[num_pools
].push(re
, misses
);
195 GenRepl::regStats(const string name
)
197 using namespace Stats
;
199 /** GEN statistics */
202 .name(name
+ ".repl_pool_dist")
203 .desc("Dist. of Repl. across pools")
209 .name(name
+ ".advance_pool_dist")
210 .desc("Dist. of Repl. across pools")
216 .name(name
+ ".demote_pool_dist")
217 .desc("Dist. of Repl. across pools")
223 GenRepl::fixTag(void* _re
, unsigned long old_index
, unsigned long new_index
)
225 GenReplEntry
*re
= (GenReplEntry
*)_re
;
227 if (re
->tag_ptr
== old_index
) {
228 re
->tag_ptr
= new_index
;
231 fatal("Repl entry: tag ptrs do not match");
236 GenRepl::findTagPtr(unsigned long index
)
238 for (int i
= 0; i
< num_pools
+ 1; ++i
) {
239 list
<GenReplEntry
*>::const_iterator iter
= pools
[i
].entries
.begin();
240 list
<GenReplEntry
*>::const_iterator end
= pools
[i
].entries
.end();
241 for (; iter
!= end
; ++iter
) {
242 if ((*iter
)->valid
&& (*iter
)->tag_ptr
== index
) {
251 GenReplParams::create()
253 return new GenRepl(name
, num_pools
, fresh_res
, pool_res
);