2 * Copyright (c) 2003-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 * Definitions a fully associative LRU tagstore.
40 #include "mem/cache/tags/fa_lru.hh"
41 #include "base/intmath.hh"
42 #include "base/misc.hh"
46 FALRU::FALRU(int _blkSize
, int _size
, int hit_latency
)
47 : blkSize(_blkSize
), size(_size
),
48 numBlks(size
/blkSize
), hitLatency(hit_latency
)
50 if (!isPowerOf2(blkSize
))
51 fatal("cache block size (in bytes) `%d' must be a power of two",
53 if (!(hitLatency
> 0))
54 fatal("Access latency in cycles must be at least one cycle");
55 if (!isPowerOf2(size
))
56 fatal("Cache Size must be power of 2 for now");
58 // Track all cache sizes from 128K up by powers of 2
59 numCaches
= floorLog2(size
) - 17;
61 cacheBoundaries
= new FALRUBlk
*[numCaches
];
62 cacheMask
= (1 << numCaches
) - 1;
68 warmupBound
= size
/blkSize
;
70 blks
= new FALRUBlk
[numBlks
];
72 tail
= &(blks
[numBlks
-1]);
75 head
->next
= &(blks
[1]);
76 head
->inCache
= cacheMask
;
78 tail
->prev
= &(blks
[numBlks
-2]);
82 int index
= (1 << 17) / blkSize
;
84 int flags
= cacheMask
;
85 for (int i
= 1; i
< numBlks
-1; i
++) {
86 blks
[i
].inCache
= flags
;
88 cacheBoundaries
[j
] = &(blks
[i
]);
93 blks
[i
].prev
= &(blks
[i
-1]);
94 blks
[i
].next
= &(blks
[i
+1]);
95 blks
[i
].isTouched
= false;
97 assert(j
== numCaches
);
98 assert(index
== numBlks
);
103 FALRU::regStats(const string
&name
)
105 using namespace Stats
;
106 BaseTags::regStats(name
);
109 .name(name
+ ".falru_hits")
110 .desc("The number of hits in each cache size.")
114 .name(name
+ ".falru_misses")
115 .desc("The number of misses in each cache size.")
118 .name(name
+ ".falru_accesses")
119 .desc("The number of accesses to the FA LRU cache.")
122 for (int i
= 0; i
< numCaches
+1; ++i
) {
123 stringstream size_str
;
125 size_str
<< (1<<(i
+7)) <<"K";
127 size_str
<< (1<<(i
-3)) <<"M";
130 hits
.subname(i
, size_str
.str());
131 hits
.subdesc(i
, "Hits in a " + size_str
.str() +" cache");
132 misses
.subname(i
, size_str
.str());
133 misses
.subdesc(i
, "Misses in a " + size_str
.str() +" cache");
138 FALRU::hashLookup(Addr addr
) const
140 tagIterator iter
= tagHash
.find(addr
);
141 if (iter
!= tagHash
.end()) {
142 return (*iter
).second
;
148 FALRU::probe(Addr addr
) const
150 Addr blkAddr
= blkAlign(addr
);
151 FALRUBlk
* blk
= hashLookup(blkAddr
);
152 return blk
&& blk
->tag
== blkAddr
&& blk
->isValid();
156 FALRU::invalidateBlk(Addr addr
)
158 Addr blkAddr
= blkAlign(addr
);
159 FALRUBlk
* blk
= (*tagHash
.find(blkAddr
)).second
;
161 assert(blk
->tag
== blkAddr
);
163 blk
->isTouched
= false;
169 FALRU::findBlock(Addr addr
, int &lat
, int *inCache
)
172 int tmp_in_cache
= 0;
173 Addr blkAddr
= blkAlign(addr
);
174 FALRUBlk
* blk
= hashLookup(blkAddr
);
176 if (blk
&& blk
->isValid()) {
177 assert(blk
->tag
== blkAddr
);
178 tmp_in_cache
= blk
->inCache
;
179 for (int i
= 0; i
< numCaches
; i
++) {
180 if (1<<i
& blk
->inCache
) {
192 for (int i
= 0; i
< numCaches
+1; ++i
) {
197 *inCache
= tmp_in_cache
;
206 FALRU::findBlock(Packet
* &pkt
, int &lat
, int *inCache
)
208 Addr addr
= pkt
->getAddr();
211 int tmp_in_cache
= 0;
212 Addr blkAddr
= blkAlign(addr
);
213 FALRUBlk
* blk
= hashLookup(blkAddr
);
215 if (blk
&& blk
->isValid()) {
216 assert(blk
->tag
== blkAddr
);
217 tmp_in_cache
= blk
->inCache
;
218 for (int i
= 0; i
< numCaches
; i
++) {
219 if (1<<i
& blk
->inCache
) {
231 for (int i
= 0; i
< numCaches
+1; ++i
) {
236 *inCache
= tmp_in_cache
;
245 FALRU::findBlock(Addr addr
) const
247 Addr blkAddr
= blkAlign(addr
);
248 FALRUBlk
* blk
= hashLookup(blkAddr
);
250 if (blk
&& blk
->isValid()) {
251 assert(blk
->tag
== blkAddr
);
259 FALRU::findReplacement(Packet
* &pkt
, PacketList
&writebacks
,
260 BlkList
&compress_blocks
)
262 FALRUBlk
* blk
= tail
;
263 assert(blk
->inCache
== 0);
265 tagHash
.erase(blk
->tag
);
266 tagHash
[blkAlign(pkt
->getAddr())] = blk
;
267 if (blk
->isValid()) {
271 blk
->isTouched
= true;
272 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
274 warmupCycle
= curTick
;
282 FALRU::moveToHead(FALRUBlk
*blk
)
284 int updateMask
= blk
->inCache
^ cacheMask
;
285 for (int i
= 0; i
< numCaches
; i
++){
286 if ((1<<i
) & updateMask
) {
287 cacheBoundaries
[i
]->inCache
&= ~(1<<i
);
288 cacheBoundaries
[i
] = cacheBoundaries
[i
]->prev
;
289 } else if (cacheBoundaries
[i
] == blk
) {
290 cacheBoundaries
[i
] = blk
->prev
;
293 blk
->inCache
= cacheMask
;
296 assert(blk
->next
== NULL
);
300 blk
->prev
->next
= blk
->next
;
301 blk
->next
->prev
= blk
->prev
;
313 FALRUBlk
* blk
= head
;
315 int boundary
= 1<<17;
317 int flags
= cacheMask
;
320 if (blk
->inCache
!= flags
) {
323 if (size
== boundary
&& blk
!= tail
) {
324 if (cacheBoundaries
[j
] != blk
) {
328 boundary
= boundary
<<1;