fa_lru.cc revision 12743
12810Srdreslin@umich.edu/*
212665Snikos.nikoleris@arm.com * Copyright (c) 2013,2016-2018 ARM Limited
39796Sprakash.ramrakhyani@arm.com * All rights reserved.
49796Sprakash.ramrakhyani@arm.com *
59796Sprakash.ramrakhyani@arm.com * The license below extends only to copyright in the software and shall
69796Sprakash.ramrakhyani@arm.com * not be construed as granting a license to any other intellectual
79796Sprakash.ramrakhyani@arm.com * property including but not limited to intellectual property relating
89796Sprakash.ramrakhyani@arm.com * to a hardware implementation of the functionality of the software
99796Sprakash.ramrakhyani@arm.com * licensed hereunder.  You may use the software subject to the license
109796Sprakash.ramrakhyani@arm.com * terms below provided that you ensure that this notice is replicated
119796Sprakash.ramrakhyani@arm.com * unmodified and in its entirety in all distributions of the software,
129796Sprakash.ramrakhyani@arm.com * modified or unmodified, in source code or in binary form.
139796Sprakash.ramrakhyani@arm.com *
142810Srdreslin@umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
152810Srdreslin@umich.edu * All rights reserved.
162810Srdreslin@umich.edu *
172810Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without
182810Srdreslin@umich.edu * modification, are permitted provided that the following conditions are
192810Srdreslin@umich.edu * met: redistributions of source code must retain the above copyright
202810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer;
212810Srdreslin@umich.edu * redistributions in binary form must reproduce the above copyright
222810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer in the
232810Srdreslin@umich.edu * documentation and/or other materials provided with the distribution;
242810Srdreslin@umich.edu * neither the name of the copyright holders nor the names of its
252810Srdreslin@umich.edu * contributors may be used to endorse or promote products derived from
262810Srdreslin@umich.edu * this software without specific prior written permission.
272810Srdreslin@umich.edu *
282810Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292810Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302810Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312810Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322810Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332810Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342810Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352810Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362810Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372810Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382810Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392810Srdreslin@umich.edu *
402810Srdreslin@umich.edu * Authors: Erik Hallnor
4112665Snikos.nikoleris@arm.com *          Nikos Nikoleris
422810Srdreslin@umich.edu */
432810Srdreslin@umich.edu
442810Srdreslin@umich.edu/**
452810Srdreslin@umich.edu * @file
462810Srdreslin@umich.edu * Definitions a fully associative LRU tagstore.
472810Srdreslin@umich.edu */
482810Srdreslin@umich.edu
4911486Snikos.nikoleris@arm.com#include "mem/cache/tags/fa_lru.hh"
5011486Snikos.nikoleris@arm.com
516216Snate@binkert.org#include <cassert>
522810Srdreslin@umich.edu#include <sstream>
532810Srdreslin@umich.edu
542810Srdreslin@umich.edu#include "base/intmath.hh"
5512334Sgabeblack@google.com#include "base/logging.hh"
5612727Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
572810Srdreslin@umich.edu
589796Sprakash.ramrakhyani@arm.comFALRU::FALRU(const Params *p)
5912665Snikos.nikoleris@arm.com    : BaseTags(p),
6012665Snikos.nikoleris@arm.com
6112665Snikos.nikoleris@arm.com      cacheTracking(p->min_tracked_cache_size, size, blkSize)
622810Srdreslin@umich.edu{
632810Srdreslin@umich.edu    if (!isPowerOf2(blkSize))
642810Srdreslin@umich.edu        fatal("cache block size (in bytes) `%d' must be a power of two",
652810Srdreslin@umich.edu              blkSize);
662810Srdreslin@umich.edu    if (!isPowerOf2(size))
672810Srdreslin@umich.edu        fatal("Cache Size must be power of 2 for now");
682810Srdreslin@umich.edu
6912665Snikos.nikoleris@arm.com    blks = new FALRUBlk[numBlocks];
702810Srdreslin@umich.edu
712810Srdreslin@umich.edu    head = &(blks[0]);
7211484Snikos.nikoleris@arm.com    head->prev = nullptr;
732810Srdreslin@umich.edu    head->next = &(blks[1]);
7412665Snikos.nikoleris@arm.com    head->set = 0;
7512665Snikos.nikoleris@arm.com    head->way = 0;
7612629Sodanrc@yahoo.com.br    head->data = &dataBlks[0];
772810Srdreslin@umich.edu
786978SLisa.Hsu@amd.com    for (unsigned i = 1; i < numBlocks - 1; i++) {
792810Srdreslin@umich.edu        blks[i].prev = &(blks[i-1]);
802810Srdreslin@umich.edu        blks[i].next = &(blks[i+1]);
8110941Sdavid.guillen@arm.com        blks[i].set = 0;
8210941Sdavid.guillen@arm.com        blks[i].way = i;
8312629Sodanrc@yahoo.com.br
8412629Sodanrc@yahoo.com.br        // Associate a data chunk to the block
8512629Sodanrc@yahoo.com.br        blks[i].data = &dataBlks[blkSize*i];
862810Srdreslin@umich.edu    }
8712665Snikos.nikoleris@arm.com
8812665Snikos.nikoleris@arm.com    tail = &(blks[numBlocks - 1]);
8912665Snikos.nikoleris@arm.com    tail->prev = &(blks[numBlocks - 2]);
9012665Snikos.nikoleris@arm.com    tail->next = nullptr;
9112665Snikos.nikoleris@arm.com    tail->set = 0;
9212665Snikos.nikoleris@arm.com    tail->way = numBlocks - 1;
9312665Snikos.nikoleris@arm.com    tail->data = &dataBlks[(numBlocks - 1) * blkSize];
9412665Snikos.nikoleris@arm.com
9512665Snikos.nikoleris@arm.com    cacheTracking.init(head, tail);
962810Srdreslin@umich.edu}
972810Srdreslin@umich.edu
989086Sandreas.hansson@arm.comFALRU::~FALRU()
999086Sandreas.hansson@arm.com{
1009086Sandreas.hansson@arm.com    delete[] blks;
1019086Sandreas.hansson@arm.com}
1029086Sandreas.hansson@arm.com
1032810Srdreslin@umich.eduvoid
1049796Sprakash.ramrakhyani@arm.comFALRU::regStats()
1052810Srdreslin@umich.edu{
1069796Sprakash.ramrakhyani@arm.com    BaseTags::regStats();
10712665Snikos.nikoleris@arm.com    cacheTracking.regStats(name());
1082810Srdreslin@umich.edu}
1092810Srdreslin@umich.edu
1102810Srdreslin@umich.eduFALRUBlk *
1112810Srdreslin@umich.eduFALRU::hashLookup(Addr addr) const
1122810Srdreslin@umich.edu{
1132810Srdreslin@umich.edu    tagIterator iter = tagHash.find(addr);
1142810Srdreslin@umich.edu    if (iter != tagHash.end()) {
1152810Srdreslin@umich.edu        return (*iter).second;
1162810Srdreslin@umich.edu    }
11711484Snikos.nikoleris@arm.com    return nullptr;
1182810Srdreslin@umich.edu}
1192810Srdreslin@umich.edu
1202810Srdreslin@umich.eduvoid
12110815Sdavid.guillen@arm.comFALRU::invalidate(CacheBlk *blk)
1222810Srdreslin@umich.edu{
12312566Snikos.nikoleris@arm.com    BaseTags::invalidate(blk);
12412636Sodanrc@yahoo.com.br
12512648Sodanrc@yahoo.com.br    // Move the block to the tail to make it the next victim
12612648Sodanrc@yahoo.com.br    moveToTail((FALRUBlk*)blk);
12712648Sodanrc@yahoo.com.br
12812636Sodanrc@yahoo.com.br    // Erase block entry in the hash table
12912636Sodanrc@yahoo.com.br    tagHash.erase(blk->tag);
1302810Srdreslin@umich.edu}
1312810Srdreslin@umich.edu
13210815Sdavid.guillen@arm.comCacheBlk*
13311870Snikos.nikoleris@arm.comFALRU::accessBlock(Addr addr, bool is_secure, Cycles &lat)
13410815Sdavid.guillen@arm.com{
13511870Snikos.nikoleris@arm.com    return accessBlock(addr, is_secure, lat, 0);
13610815Sdavid.guillen@arm.com}
13710815Sdavid.guillen@arm.com
13810815Sdavid.guillen@arm.comCacheBlk*
13912665Snikos.nikoleris@arm.comFALRU::accessBlock(Addr addr, bool is_secure, Cycles &lat,
14012665Snikos.nikoleris@arm.com                   CachesMask *in_caches_mask)
1412810Srdreslin@umich.edu{
14212665Snikos.nikoleris@arm.com    CachesMask mask = 0;
14312677Sodanrc@yahoo.com.br    FALRUBlk* blk = static_cast<FALRUBlk*>(findBlock(addr, is_secure));
1442810Srdreslin@umich.edu
14512677Sodanrc@yahoo.com.br    if (blk != nullptr) {
14611722Ssophiane.senni@gmail.com        // If a cache hit
14711722Ssophiane.senni@gmail.com        lat = accessLatency;
14811722Ssophiane.senni@gmail.com        // Check if the block to be accessed is available. If not,
14911722Ssophiane.senni@gmail.com        // apply the accessLatency on top of block->whenReady.
15011722Ssophiane.senni@gmail.com        if (blk->whenReady > curTick() &&
15111722Ssophiane.senni@gmail.com            cache->ticksToCycles(blk->whenReady - curTick()) >
15211722Ssophiane.senni@gmail.com            accessLatency) {
15311722Ssophiane.senni@gmail.com            lat = cache->ticksToCycles(blk->whenReady - curTick()) +
15411722Ssophiane.senni@gmail.com            accessLatency;
15511722Ssophiane.senni@gmail.com        }
15612665Snikos.nikoleris@arm.com        mask = blk->inCachesMask;
15712677Sodanrc@yahoo.com.br
15812665Snikos.nikoleris@arm.com        moveToHead(blk);
1592810Srdreslin@umich.edu    } else {
16011722Ssophiane.senni@gmail.com        // If a cache miss
16111722Ssophiane.senni@gmail.com        lat = lookupLatency;
1622810Srdreslin@umich.edu    }
16312665Snikos.nikoleris@arm.com    if (in_caches_mask) {
16412665Snikos.nikoleris@arm.com        *in_caches_mask = mask;
1652810Srdreslin@umich.edu    }
1662810Srdreslin@umich.edu
16712665Snikos.nikoleris@arm.com    cacheTracking.recordAccess(blk);
16812665Snikos.nikoleris@arm.com
1692810Srdreslin@umich.edu    return blk;
1702810Srdreslin@umich.edu}
1712810Srdreslin@umich.edu
1722810Srdreslin@umich.edu
17310815Sdavid.guillen@arm.comCacheBlk*
17410028SGiacomo.Gabrielli@arm.comFALRU::findBlock(Addr addr, bool is_secure) const
1752810Srdreslin@umich.edu{
17612677Sodanrc@yahoo.com.br    Addr tag = extractTag(addr);
17712677Sodanrc@yahoo.com.br    FALRUBlk* blk = hashLookup(tag);
1782810Srdreslin@umich.edu
1792810Srdreslin@umich.edu    if (blk && blk->isValid()) {
18012677Sodanrc@yahoo.com.br        assert(blk->tag == tag);
18112676Sodanrc@yahoo.com.br        assert(blk->isSecure() == is_secure);
1822810Srdreslin@umich.edu    } else {
18311484Snikos.nikoleris@arm.com        blk = nullptr;
1842810Srdreslin@umich.edu    }
1852810Srdreslin@umich.edu    return blk;
1862810Srdreslin@umich.edu}
1872810Srdreslin@umich.edu
18812743Sodanrc@yahoo.com.brReplaceableEntry*
18910941Sdavid.guillen@arm.comFALRU::findBlockBySetAndWay(int set, int way) const
19010941Sdavid.guillen@arm.com{
19110941Sdavid.guillen@arm.com    assert(set == 0);
19210941Sdavid.guillen@arm.com    return &blks[way];
19310941Sdavid.guillen@arm.com}
19410941Sdavid.guillen@arm.com
19510941Sdavid.guillen@arm.comCacheBlk*
19610048Saminfar@gmail.comFALRU::findVictim(Addr addr)
1972810Srdreslin@umich.edu{
19812636Sodanrc@yahoo.com.br    return tail;
1992810Srdreslin@umich.edu}
2002810Srdreslin@umich.edu
2012810Srdreslin@umich.eduvoid
20210815Sdavid.guillen@arm.comFALRU::insertBlock(PacketPtr pkt, CacheBlk *blk)
2035717Shsul@eecs.umich.edu{
20412636Sodanrc@yahoo.com.br    FALRUBlk* falruBlk = static_cast<FALRUBlk*>(blk);
20512636Sodanrc@yahoo.com.br
20612636Sodanrc@yahoo.com.br    // Make sure block is not present in the cache
20712665Snikos.nikoleris@arm.com    assert(falruBlk->inCachesMask == 0);
20812636Sodanrc@yahoo.com.br
20912636Sodanrc@yahoo.com.br    // Do common block insertion functionality
21012636Sodanrc@yahoo.com.br    BaseTags::insertBlock(pkt, blk);
21112636Sodanrc@yahoo.com.br
21212636Sodanrc@yahoo.com.br    // New block is the MRU
21312636Sodanrc@yahoo.com.br    moveToHead(falruBlk);
21412636Sodanrc@yahoo.com.br
21512636Sodanrc@yahoo.com.br    // Insert new block in the hash table
21612636Sodanrc@yahoo.com.br    tagHash[falruBlk->tag] = falruBlk;
2175717Shsul@eecs.umich.edu}
2185717Shsul@eecs.umich.edu
2195717Shsul@eecs.umich.eduvoid
2202810Srdreslin@umich.eduFALRU::moveToHead(FALRUBlk *blk)
2212810Srdreslin@umich.edu{
22212648Sodanrc@yahoo.com.br    // If block is not already head, do the moving
22312648Sodanrc@yahoo.com.br    if (blk != head) {
22412665Snikos.nikoleris@arm.com        cacheTracking.moveBlockToHead(blk);
22512648Sodanrc@yahoo.com.br        // If block is tail, set previous block as new tail
2262810Srdreslin@umich.edu        if (blk == tail){
22711484Snikos.nikoleris@arm.com            assert(blk->next == nullptr);
2282810Srdreslin@umich.edu            tail = blk->prev;
22911484Snikos.nikoleris@arm.com            tail->next = nullptr;
23012648Sodanrc@yahoo.com.br        // Inform block's surrounding blocks that it has been moved
2312810Srdreslin@umich.edu        } else {
2322810Srdreslin@umich.edu            blk->prev->next = blk->next;
2332810Srdreslin@umich.edu            blk->next->prev = blk->prev;
2342810Srdreslin@umich.edu        }
23512648Sodanrc@yahoo.com.br
23612648Sodanrc@yahoo.com.br        // Swap pointers
2372810Srdreslin@umich.edu        blk->next = head;
23811484Snikos.nikoleris@arm.com        blk->prev = nullptr;
2392810Srdreslin@umich.edu        head->prev = blk;
2402810Srdreslin@umich.edu        head = blk;
24112665Snikos.nikoleris@arm.com
24212665Snikos.nikoleris@arm.com        cacheTracking.check(head, tail);
2432810Srdreslin@umich.edu    }
2442810Srdreslin@umich.edu}
2452810Srdreslin@umich.edu
24612648Sodanrc@yahoo.com.brvoid
24712648Sodanrc@yahoo.com.brFALRU::moveToTail(FALRUBlk *blk)
24812648Sodanrc@yahoo.com.br{
24912648Sodanrc@yahoo.com.br    // If block is not already tail, do the moving
25012648Sodanrc@yahoo.com.br    if (blk != tail) {
25112665Snikos.nikoleris@arm.com        cacheTracking.moveBlockToTail(blk);
25212648Sodanrc@yahoo.com.br        // If block is head, set next block as new head
25312648Sodanrc@yahoo.com.br        if (blk == head){
25412648Sodanrc@yahoo.com.br            assert(blk->prev == nullptr);
25512648Sodanrc@yahoo.com.br            head = blk->next;
25612648Sodanrc@yahoo.com.br            head->prev = nullptr;
25712648Sodanrc@yahoo.com.br        // Inform block's surrounding blocks that it has been moved
25812648Sodanrc@yahoo.com.br        } else {
25912648Sodanrc@yahoo.com.br            blk->prev->next = blk->next;
26012648Sodanrc@yahoo.com.br            blk->next->prev = blk->prev;
26112648Sodanrc@yahoo.com.br        }
26212648Sodanrc@yahoo.com.br
26312648Sodanrc@yahoo.com.br        // Swap pointers
26412648Sodanrc@yahoo.com.br        blk->prev = tail;
26512648Sodanrc@yahoo.com.br        blk->next = nullptr;
26612648Sodanrc@yahoo.com.br        tail->next = blk;
26712648Sodanrc@yahoo.com.br        tail = blk;
26812665Snikos.nikoleris@arm.com
26912665Snikos.nikoleris@arm.com        cacheTracking.check(head, tail);
27012648Sodanrc@yahoo.com.br    }
27112648Sodanrc@yahoo.com.br}
27212648Sodanrc@yahoo.com.br
2739796Sprakash.ramrakhyani@arm.comFALRU *
2749796Sprakash.ramrakhyani@arm.comFALRUParams::create()
2759796Sprakash.ramrakhyani@arm.com{
2769796Sprakash.ramrakhyani@arm.com    return new FALRU(this);
2779796Sprakash.ramrakhyani@arm.com}
2789796Sprakash.ramrakhyani@arm.com
27912665Snikos.nikoleris@arm.comvoid
28012665Snikos.nikoleris@arm.comFALRU::CacheTracking::check(FALRUBlk *head, FALRUBlk *tail)
28112665Snikos.nikoleris@arm.com{
28212665Snikos.nikoleris@arm.com#ifdef FALRU_DEBUG
28312665Snikos.nikoleris@arm.com    FALRUBlk* blk = head;
28412665Snikos.nikoleris@arm.com    unsigned curr_size = 0;
28512665Snikos.nikoleris@arm.com    unsigned tracked_cache_size = minTrackedSize;
28612665Snikos.nikoleris@arm.com    CachesMask in_caches_mask = inAllCachesMask;
28712665Snikos.nikoleris@arm.com    int j = 0;
28812665Snikos.nikoleris@arm.com
28912665Snikos.nikoleris@arm.com    while (blk) {
29012665Snikos.nikoleris@arm.com        panic_if(blk->inCachesMask != in_caches_mask, "Expected cache mask "
29112665Snikos.nikoleris@arm.com                 "%x found %x", blk->inCachesMask, in_caches_mask);
29212665Snikos.nikoleris@arm.com
29312665Snikos.nikoleris@arm.com        curr_size += blkSize;
29412665Snikos.nikoleris@arm.com        if (curr_size == tracked_cache_size && blk != tail) {
29512665Snikos.nikoleris@arm.com            panic_if(boundaries[j] != blk, "Unexpected boundary for the %d-th "
29612665Snikos.nikoleris@arm.com                     "cache", j);
29712665Snikos.nikoleris@arm.com            tracked_cache_size <<= 1;
29812665Snikos.nikoleris@arm.com            // from this point, blocks fit only in the larger caches
29912665Snikos.nikoleris@arm.com            in_caches_mask &= ~(1U << j);
30012665Snikos.nikoleris@arm.com            ++j;
30112665Snikos.nikoleris@arm.com        }
30212665Snikos.nikoleris@arm.com        blk = blk->next;
30312665Snikos.nikoleris@arm.com    }
30412665Snikos.nikoleris@arm.com#endif // FALRU_DEBUG
30512665Snikos.nikoleris@arm.com}
30612665Snikos.nikoleris@arm.com
30712665Snikos.nikoleris@arm.comvoid
30812665Snikos.nikoleris@arm.comFALRU::CacheTracking::init(FALRUBlk *head, FALRUBlk *tail)
30912665Snikos.nikoleris@arm.com{
31012665Snikos.nikoleris@arm.com    // early exit if we are not tracking any extra caches
31112665Snikos.nikoleris@arm.com    FALRUBlk* blk = numTrackedCaches ? head : nullptr;
31212665Snikos.nikoleris@arm.com    unsigned curr_size = 0;
31312665Snikos.nikoleris@arm.com    unsigned tracked_cache_size = minTrackedSize;
31412665Snikos.nikoleris@arm.com    CachesMask in_caches_mask = inAllCachesMask;
31512665Snikos.nikoleris@arm.com    int j = 0;
31612665Snikos.nikoleris@arm.com
31712665Snikos.nikoleris@arm.com    while (blk) {
31812665Snikos.nikoleris@arm.com        blk->inCachesMask = in_caches_mask;
31912665Snikos.nikoleris@arm.com
32012665Snikos.nikoleris@arm.com        curr_size += blkSize;
32112665Snikos.nikoleris@arm.com        if (curr_size == tracked_cache_size && blk != tail) {
32212665Snikos.nikoleris@arm.com            boundaries[j] = blk;
32312665Snikos.nikoleris@arm.com
32412665Snikos.nikoleris@arm.com            tracked_cache_size <<= 1;
32512665Snikos.nikoleris@arm.com            // from this point, blocks fit only in the larger caches
32612665Snikos.nikoleris@arm.com            in_caches_mask &= ~(1U << j);
32712665Snikos.nikoleris@arm.com            ++j;
32812665Snikos.nikoleris@arm.com        }
32912665Snikos.nikoleris@arm.com        blk = blk->next;
33012665Snikos.nikoleris@arm.com    }
33112665Snikos.nikoleris@arm.com}
33212665Snikos.nikoleris@arm.com
33312665Snikos.nikoleris@arm.com
33412665Snikos.nikoleris@arm.comvoid
33512665Snikos.nikoleris@arm.comFALRU::CacheTracking::moveBlockToHead(FALRUBlk *blk)
33612665Snikos.nikoleris@arm.com{
33712665Snikos.nikoleris@arm.com    // Get the mask of all caches, in which the block didn't fit
33812665Snikos.nikoleris@arm.com    // before moving it to the head
33912665Snikos.nikoleris@arm.com    CachesMask update_caches_mask = inAllCachesMask ^ blk->inCachesMask;
34012665Snikos.nikoleris@arm.com
34112665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
34212665Snikos.nikoleris@arm.com        CachesMask current_cache_mask = 1U << i;
34312665Snikos.nikoleris@arm.com        if (current_cache_mask & update_caches_mask) {
34412665Snikos.nikoleris@arm.com            // if the ith cache didn't fit the block (before it is moved to
34512665Snikos.nikoleris@arm.com            // the head), move the ith boundary 1 block closer to the
34612665Snikos.nikoleris@arm.com            // MRU
34712665Snikos.nikoleris@arm.com            boundaries[i]->inCachesMask &= ~current_cache_mask;
34812665Snikos.nikoleris@arm.com            boundaries[i] = boundaries[i]->prev;
34912665Snikos.nikoleris@arm.com        } else if (boundaries[i] == blk) {
35012665Snikos.nikoleris@arm.com            // Make sure the boundary doesn't point to the block
35112665Snikos.nikoleris@arm.com            // we are about to move
35212665Snikos.nikoleris@arm.com            boundaries[i] = blk->prev;
35312665Snikos.nikoleris@arm.com        }
35412665Snikos.nikoleris@arm.com    }
35512665Snikos.nikoleris@arm.com
35612665Snikos.nikoleris@arm.com    // Make block reside in all caches
35712665Snikos.nikoleris@arm.com    blk->inCachesMask = inAllCachesMask;
35812665Snikos.nikoleris@arm.com}
35912665Snikos.nikoleris@arm.com
36012665Snikos.nikoleris@arm.comvoid
36112665Snikos.nikoleris@arm.comFALRU::CacheTracking::moveBlockToTail(FALRUBlk *blk)
36212665Snikos.nikoleris@arm.com{
36312665Snikos.nikoleris@arm.com    CachesMask update_caches_mask = blk->inCachesMask;
36412665Snikos.nikoleris@arm.com
36512665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
36612665Snikos.nikoleris@arm.com        CachesMask current_cache_mask = 1U << i;
36712665Snikos.nikoleris@arm.com        if (current_cache_mask & update_caches_mask) {
36812665Snikos.nikoleris@arm.com            // if the ith cache fitted the block (before it is moved to
36912665Snikos.nikoleris@arm.com            // the tail), move the ith boundary 1 block closer to the
37012665Snikos.nikoleris@arm.com            // LRU
37112665Snikos.nikoleris@arm.com            boundaries[i] = boundaries[i]->next;
37212665Snikos.nikoleris@arm.com            if (boundaries[i] == blk) {
37312665Snikos.nikoleris@arm.com                // Make sure the boundary doesn't point to the block
37412665Snikos.nikoleris@arm.com                // we are about to move
37512665Snikos.nikoleris@arm.com                boundaries[i] = blk->next;
37612665Snikos.nikoleris@arm.com            }
37712665Snikos.nikoleris@arm.com            boundaries[i]->inCachesMask |= current_cache_mask;
37812665Snikos.nikoleris@arm.com        }
37912665Snikos.nikoleris@arm.com    }
38012665Snikos.nikoleris@arm.com
38112665Snikos.nikoleris@arm.com    // The block now fits only in the actual cache
38212665Snikos.nikoleris@arm.com    blk->inCachesMask = 0;
38312665Snikos.nikoleris@arm.com}
38412665Snikos.nikoleris@arm.com
38512665Snikos.nikoleris@arm.comvoid
38612665Snikos.nikoleris@arm.comFALRU::CacheTracking::recordAccess(FALRUBlk *blk)
38712665Snikos.nikoleris@arm.com{
38812665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
38912665Snikos.nikoleris@arm.com        if (blk && ((1U << i) & blk->inCachesMask)) {
39012665Snikos.nikoleris@arm.com            hits[i]++;
39112665Snikos.nikoleris@arm.com        } else {
39212665Snikos.nikoleris@arm.com            misses[i]++;
39312665Snikos.nikoleris@arm.com        }
39412665Snikos.nikoleris@arm.com    }
39512665Snikos.nikoleris@arm.com
39612665Snikos.nikoleris@arm.com    // Record stats for the actual cache too
39712665Snikos.nikoleris@arm.com    if (blk) {
39812665Snikos.nikoleris@arm.com        hits[numTrackedCaches]++;
39912665Snikos.nikoleris@arm.com    } else {
40012665Snikos.nikoleris@arm.com        misses[numTrackedCaches]++;
40112665Snikos.nikoleris@arm.com    }
40212665Snikos.nikoleris@arm.com
40312665Snikos.nikoleris@arm.com    accesses++;
40412665Snikos.nikoleris@arm.com}
40512665Snikos.nikoleris@arm.com
40612665Snikos.nikoleris@arm.comvoid
40712665Snikos.nikoleris@arm.comprintSize(std::ostream &stream, size_t size)
40812665Snikos.nikoleris@arm.com{
40912665Snikos.nikoleris@arm.com    static const char *SIZES[] = { "B", "kB", "MB", "GB", "TB", "ZB" };
41012665Snikos.nikoleris@arm.com    int div = 0;
41112665Snikos.nikoleris@arm.com    while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
41212665Snikos.nikoleris@arm.com        div++;
41312665Snikos.nikoleris@arm.com        size >>= 10;
41412665Snikos.nikoleris@arm.com    }
41512665Snikos.nikoleris@arm.com    stream << size << SIZES[div];
41612665Snikos.nikoleris@arm.com}
41712665Snikos.nikoleris@arm.com
41812665Snikos.nikoleris@arm.comvoid
41912665Snikos.nikoleris@arm.comFALRU::CacheTracking::regStats(std::string name)
42012665Snikos.nikoleris@arm.com{
42112665Snikos.nikoleris@arm.com    hits
42212665Snikos.nikoleris@arm.com        .init(numTrackedCaches + 1)
42312665Snikos.nikoleris@arm.com        .name(name + ".falru_hits")
42412665Snikos.nikoleris@arm.com        .desc("The number of hits in each cache size.")
42512665Snikos.nikoleris@arm.com        ;
42612665Snikos.nikoleris@arm.com    misses
42712665Snikos.nikoleris@arm.com        .init(numTrackedCaches + 1)
42812665Snikos.nikoleris@arm.com        .name(name + ".falru_misses")
42912665Snikos.nikoleris@arm.com        .desc("The number of misses in each cache size.")
43012665Snikos.nikoleris@arm.com        ;
43112665Snikos.nikoleris@arm.com    accesses
43212665Snikos.nikoleris@arm.com        .name(name + ".falru_accesses")
43312665Snikos.nikoleris@arm.com        .desc("The number of accesses to the FA LRU cache.")
43412665Snikos.nikoleris@arm.com        ;
43512665Snikos.nikoleris@arm.com
43612665Snikos.nikoleris@arm.com    for (unsigned i = 0; i < numTrackedCaches + 1; ++i) {
43712665Snikos.nikoleris@arm.com        std::stringstream size_str;
43812665Snikos.nikoleris@arm.com        printSize(size_str, minTrackedSize << i);
43912665Snikos.nikoleris@arm.com        hits.subname(i, size_str.str());
44012665Snikos.nikoleris@arm.com        hits.subdesc(i, "Hits in a " + size_str.str() + " cache");
44112665Snikos.nikoleris@arm.com        misses.subname(i, size_str.str());
44212665Snikos.nikoleris@arm.com        misses.subdesc(i, "Misses in a " + size_str.str() + " cache");
44312665Snikos.nikoleris@arm.com    }
44412665Snikos.nikoleris@arm.com}
445