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
33 * Declarations of generational replacement policy
41 #include "base/statistics.hh"
42 #include "mem/cache/tags/repl/repl.hh"
45 * Generational Replacement entry.
50 /** Valid flag, used to quickly invalidate bogus entries. */
52 /** The difference between this entry and the previous in the pool. */
54 /** Pointer to the corresponding tag in the IIC. */
55 unsigned long tag_ptr;
59 * Generational replacement pool
64 /** The time the last entry was added. */
66 /** The time the oldest entry was added. */
68 /** List of the replacement entries in this pool. */
69 std::list<GenReplEntry*> entries;
71 /** The number of entries in this pool. */
84 * Add an entry to this pool.
85 * @param re The entry to add.
86 * @param now The current time.
88 void push(GenReplEntry *re, Tick now) {
90 if (!entries.empty()) {
91 re->delta = now - newest;
95 newest = oldest = now;
97 entries.push_back(re);
101 * Remove an entry from the pool.
102 * @return The entry at the front of the list.
104 GenReplEntry* pop() {
105 GenReplEntry *tmp = NULL;
106 if (!entries.empty()) {
108 tmp = entries.front();
110 oldest += tmp->delta;
116 * Return the entry at the front of the list.
117 * @return the entry at the front of the list.
119 GenReplEntry* top() {
120 return entries.front();
127 while (!entries.empty()) {
128 GenReplEntry *tmp = entries.front();
136 * Generational replacement policy for use with the IIC.
137 * @todo update to use STL and for efficiency
139 class GenRepl : public Repl
142 /** The array of pools. */
144 /** The number of pools. */
146 /** The amount of time to stay in the fresh pool. */
148 /** The amount of time to stay in the normal pools. */
150 /** The maximum number of entries */
152 /** The number of entries currently in the pools. */
153 int num_pool_entries;
154 /** The number of misses. Used as the internal time. */
160 * @addtogroup CacheStatistics
163 /** The number of replacements from each pool. */
164 Stats::Distribution<> repl_pool;
165 /** The number of advances out of each pool. */
166 Stats::Distribution<> advance_pool;
167 /** The number of demotions from each pool. */
168 Stats::Distribution<> demote_pool;
174 * Constructs and initializes this replacement policy.
175 * @param name The name of the policy.
176 * @param num_pools The number of pools to use.
177 * @param fresh_res The amount of time to wait in the fresh pool.
178 * @param pool_res The amount of time to wait in the normal pools.
180 GenRepl(const std::string &name, int num_pools,
181 int fresh_res, int pool_res);
189 * Returns the tag pointer of the cache block to replace.
190 * @return The tag to replace.
192 virtual unsigned long getRepl();
195 * Return an array of N tag pointers to replace.
196 * @param n The number of tag pointer to return.
197 * @return An array of tag pointers to replace.
199 virtual unsigned long *getNRepl(int n);
202 * Update replacement data
204 virtual void doAdvance(std::list<unsigned long> &demoted);
207 * Add a tag to the replacement policy and return a pointer to the
209 * @param tag_index The tag to add.
210 * @return The replacement entry.
212 virtual void* add(unsigned long tag_index);
215 * Register statistics.
216 * @param name The name to prepend to statistic descriptions.
218 virtual void regStats(const std::string name);
221 * Update the tag pointer to when the tag moves.
222 * @param re The replacement entry of the tag.
223 * @param old_index The old tag pointer.
224 * @param new_index The new tag pointer.
225 * @return 1 if successful, 0 otherwise.
227 virtual int fixTag(void *re, unsigned long old_index,
228 unsigned long new_index);
231 * Remove this entry from the replacement policy.
232 * @param re The replacement entry to remove
234 virtual void removeEntry(void *re)
236 ((GenReplEntry*)re)->valid = false;
241 * Debug function to verify that there is only one repl entry per tag.
242 * @param index The tag index to check.
244 bool findTagPtr(unsigned long index);
247 #endif /* __GEN_HH__ */