fa_lru.cc revision 13164
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.eduvoid
11110815Sdavid.guillen@arm.comFALRU::invalidate(CacheBlk *blk)
1122810Srdreslin@umich.edu{
11312566Snikos.nikoleris@arm.com    BaseTags::invalidate(blk);
11412636Sodanrc@yahoo.com.br
11512745Sodanrc@yahoo.com.br    // Decrease the number of tags in use
11612745Sodanrc@yahoo.com.br    tagsInUse--;
11712745Sodanrc@yahoo.com.br
11812648Sodanrc@yahoo.com.br    // Move the block to the tail to make it the next victim
11912648Sodanrc@yahoo.com.br    moveToTail((FALRUBlk*)blk);
12012648Sodanrc@yahoo.com.br
12112636Sodanrc@yahoo.com.br    // Erase block entry in the hash table
12212775Snikos.nikoleris@arm.com    tagHash.erase(std::make_pair(blk->tag, blk->isSecure()));
1232810Srdreslin@umich.edu}
1242810Srdreslin@umich.edu
12510815Sdavid.guillen@arm.comCacheBlk*
12611870Snikos.nikoleris@arm.comFALRU::accessBlock(Addr addr, bool is_secure, Cycles &lat)
12710815Sdavid.guillen@arm.com{
12811870Snikos.nikoleris@arm.com    return accessBlock(addr, is_secure, lat, 0);
12910815Sdavid.guillen@arm.com}
13010815Sdavid.guillen@arm.com
13110815Sdavid.guillen@arm.comCacheBlk*
13212665Snikos.nikoleris@arm.comFALRU::accessBlock(Addr addr, bool is_secure, Cycles &lat,
13312665Snikos.nikoleris@arm.com                   CachesMask *in_caches_mask)
1342810Srdreslin@umich.edu{
13512665Snikos.nikoleris@arm.com    CachesMask mask = 0;
13612677Sodanrc@yahoo.com.br    FALRUBlk* blk = static_cast<FALRUBlk*>(findBlock(addr, is_secure));
1372810Srdreslin@umich.edu
13812775Snikos.nikoleris@arm.com    if (blk && blk->isValid()) {
13911722Ssophiane.senni@gmail.com        // If a cache hit
14011722Ssophiane.senni@gmail.com        lat = accessLatency;
14111722Ssophiane.senni@gmail.com        // Check if the block to be accessed is available. If not,
14211722Ssophiane.senni@gmail.com        // apply the accessLatency on top of block->whenReady.
14311722Ssophiane.senni@gmail.com        if (blk->whenReady > curTick() &&
14411722Ssophiane.senni@gmail.com            cache->ticksToCycles(blk->whenReady - curTick()) >
14511722Ssophiane.senni@gmail.com            accessLatency) {
14611722Ssophiane.senni@gmail.com            lat = cache->ticksToCycles(blk->whenReady - curTick()) +
14711722Ssophiane.senni@gmail.com            accessLatency;
14811722Ssophiane.senni@gmail.com        }
14912665Snikos.nikoleris@arm.com        mask = blk->inCachesMask;
15012677Sodanrc@yahoo.com.br
15112665Snikos.nikoleris@arm.com        moveToHead(blk);
1522810Srdreslin@umich.edu    } else {
15311722Ssophiane.senni@gmail.com        // If a cache miss
15411722Ssophiane.senni@gmail.com        lat = lookupLatency;
1552810Srdreslin@umich.edu    }
15612665Snikos.nikoleris@arm.com    if (in_caches_mask) {
15712665Snikos.nikoleris@arm.com        *in_caches_mask = mask;
1582810Srdreslin@umich.edu    }
1592810Srdreslin@umich.edu
16012665Snikos.nikoleris@arm.com    cacheTracking.recordAccess(blk);
16112665Snikos.nikoleris@arm.com
1622810Srdreslin@umich.edu    return blk;
1632810Srdreslin@umich.edu}
1642810Srdreslin@umich.edu
16510815Sdavid.guillen@arm.comCacheBlk*
16610028SGiacomo.Gabrielli@arm.comFALRU::findBlock(Addr addr, bool is_secure) const
1672810Srdreslin@umich.edu{
16812775Snikos.nikoleris@arm.com    FALRUBlk* blk = nullptr;
16912775Snikos.nikoleris@arm.com
17012677Sodanrc@yahoo.com.br    Addr tag = extractTag(addr);
17112775Snikos.nikoleris@arm.com    auto iter = tagHash.find(std::make_pair(tag, is_secure));
17212775Snikos.nikoleris@arm.com    if (iter != tagHash.end()) {
17312775Snikos.nikoleris@arm.com        blk = (*iter).second;
17412775Snikos.nikoleris@arm.com    }
1752810Srdreslin@umich.edu
1762810Srdreslin@umich.edu    if (blk && blk->isValid()) {
17712677Sodanrc@yahoo.com.br        assert(blk->tag == tag);
17812676Sodanrc@yahoo.com.br        assert(blk->isSecure() == is_secure);
1792810Srdreslin@umich.edu    }
18012775Snikos.nikoleris@arm.com
1812810Srdreslin@umich.edu    return blk;
1822810Srdreslin@umich.edu}
1832810Srdreslin@umich.edu
18412743Sodanrc@yahoo.com.brReplaceableEntry*
18510941Sdavid.guillen@arm.comFALRU::findBlockBySetAndWay(int set, int way) const
18610941Sdavid.guillen@arm.com{
18710941Sdavid.guillen@arm.com    assert(set == 0);
18810941Sdavid.guillen@arm.com    return &blks[way];
18910941Sdavid.guillen@arm.com}
19010941Sdavid.guillen@arm.com
19110941Sdavid.guillen@arm.comCacheBlk*
19212746Sodanrc@yahoo.com.brFALRU::findVictim(Addr addr, const bool is_secure,
19312746Sodanrc@yahoo.com.br                  std::vector<CacheBlk*>& evict_blks) const
1942810Srdreslin@umich.edu{
19512744Sodanrc@yahoo.com.br    // The victim is always stored on the tail for the FALRU
19612744Sodanrc@yahoo.com.br    FALRUBlk* victim = tail;
19712744Sodanrc@yahoo.com.br
19812744Sodanrc@yahoo.com.br    // There is only one eviction for this replacement
19912744Sodanrc@yahoo.com.br    evict_blks.push_back(victim);
20012744Sodanrc@yahoo.com.br
20112744Sodanrc@yahoo.com.br    return victim;
2022810Srdreslin@umich.edu}
2032810Srdreslin@umich.edu
2042810Srdreslin@umich.eduvoid
20512753Sodanrc@yahoo.com.brFALRU::insertBlock(const PacketPtr pkt, CacheBlk *blk)
2065717Shsul@eecs.umich.edu{
20712636Sodanrc@yahoo.com.br    FALRUBlk* falruBlk = static_cast<FALRUBlk*>(blk);
20812636Sodanrc@yahoo.com.br
20912636Sodanrc@yahoo.com.br    // Make sure block is not present in the cache
21012665Snikos.nikoleris@arm.com    assert(falruBlk->inCachesMask == 0);
21112636Sodanrc@yahoo.com.br
21212636Sodanrc@yahoo.com.br    // Do common block insertion functionality
21312636Sodanrc@yahoo.com.br    BaseTags::insertBlock(pkt, blk);
21412636Sodanrc@yahoo.com.br
21512745Sodanrc@yahoo.com.br    // Increment tag counter
21612745Sodanrc@yahoo.com.br    tagsInUse++;
21712745Sodanrc@yahoo.com.br
21812636Sodanrc@yahoo.com.br    // New block is the MRU
21912636Sodanrc@yahoo.com.br    moveToHead(falruBlk);
22012636Sodanrc@yahoo.com.br
22112636Sodanrc@yahoo.com.br    // Insert new block in the hash table
22212775Snikos.nikoleris@arm.com    tagHash[std::make_pair(blk->tag, blk->isSecure())] = falruBlk;
2235717Shsul@eecs.umich.edu}
2245717Shsul@eecs.umich.edu
2255717Shsul@eecs.umich.eduvoid
2262810Srdreslin@umich.eduFALRU::moveToHead(FALRUBlk *blk)
2272810Srdreslin@umich.edu{
22812648Sodanrc@yahoo.com.br    // If block is not already head, do the moving
22912648Sodanrc@yahoo.com.br    if (blk != head) {
23012665Snikos.nikoleris@arm.com        cacheTracking.moveBlockToHead(blk);
23112648Sodanrc@yahoo.com.br        // If block is tail, set previous block as new tail
2322810Srdreslin@umich.edu        if (blk == tail){
23311484Snikos.nikoleris@arm.com            assert(blk->next == nullptr);
2342810Srdreslin@umich.edu            tail = blk->prev;
23511484Snikos.nikoleris@arm.com            tail->next = nullptr;
23612648Sodanrc@yahoo.com.br        // Inform block's surrounding blocks that it has been moved
2372810Srdreslin@umich.edu        } else {
2382810Srdreslin@umich.edu            blk->prev->next = blk->next;
2392810Srdreslin@umich.edu            blk->next->prev = blk->prev;
2402810Srdreslin@umich.edu        }
24112648Sodanrc@yahoo.com.br
24212648Sodanrc@yahoo.com.br        // Swap pointers
2432810Srdreslin@umich.edu        blk->next = head;
24411484Snikos.nikoleris@arm.com        blk->prev = nullptr;
2452810Srdreslin@umich.edu        head->prev = blk;
2462810Srdreslin@umich.edu        head = blk;
24712665Snikos.nikoleris@arm.com
24812665Snikos.nikoleris@arm.com        cacheTracking.check(head, tail);
2492810Srdreslin@umich.edu    }
2502810Srdreslin@umich.edu}
2512810Srdreslin@umich.edu
25212648Sodanrc@yahoo.com.brvoid
25312648Sodanrc@yahoo.com.brFALRU::moveToTail(FALRUBlk *blk)
25412648Sodanrc@yahoo.com.br{
25512648Sodanrc@yahoo.com.br    // If block is not already tail, do the moving
25612648Sodanrc@yahoo.com.br    if (blk != tail) {
25712665Snikos.nikoleris@arm.com        cacheTracking.moveBlockToTail(blk);
25812648Sodanrc@yahoo.com.br        // If block is head, set next block as new head
25912648Sodanrc@yahoo.com.br        if (blk == head){
26012648Sodanrc@yahoo.com.br            assert(blk->prev == nullptr);
26112648Sodanrc@yahoo.com.br            head = blk->next;
26212648Sodanrc@yahoo.com.br            head->prev = nullptr;
26312648Sodanrc@yahoo.com.br        // Inform block's surrounding blocks that it has been moved
26412648Sodanrc@yahoo.com.br        } else {
26512648Sodanrc@yahoo.com.br            blk->prev->next = blk->next;
26612648Sodanrc@yahoo.com.br            blk->next->prev = blk->prev;
26712648Sodanrc@yahoo.com.br        }
26812648Sodanrc@yahoo.com.br
26912648Sodanrc@yahoo.com.br        // Swap pointers
27012648Sodanrc@yahoo.com.br        blk->prev = tail;
27112648Sodanrc@yahoo.com.br        blk->next = nullptr;
27212648Sodanrc@yahoo.com.br        tail->next = blk;
27312648Sodanrc@yahoo.com.br        tail = blk;
27412665Snikos.nikoleris@arm.com
27512665Snikos.nikoleris@arm.com        cacheTracking.check(head, tail);
27612648Sodanrc@yahoo.com.br    }
27712648Sodanrc@yahoo.com.br}
27812648Sodanrc@yahoo.com.br
2799796Sprakash.ramrakhyani@arm.comFALRU *
2809796Sprakash.ramrakhyani@arm.comFALRUParams::create()
2819796Sprakash.ramrakhyani@arm.com{
2829796Sprakash.ramrakhyani@arm.com    return new FALRU(this);
2839796Sprakash.ramrakhyani@arm.com}
2849796Sprakash.ramrakhyani@arm.com
28512665Snikos.nikoleris@arm.comvoid
28613164Sodanrc@yahoo.com.brFALRU::CacheTracking::check(const FALRUBlk *head, const FALRUBlk *tail) const
28712665Snikos.nikoleris@arm.com{
28812665Snikos.nikoleris@arm.com#ifdef FALRU_DEBUG
28913164Sodanrc@yahoo.com.br    const FALRUBlk* blk = head;
29012665Snikos.nikoleris@arm.com    unsigned curr_size = 0;
29112665Snikos.nikoleris@arm.com    unsigned tracked_cache_size = minTrackedSize;
29212665Snikos.nikoleris@arm.com    CachesMask in_caches_mask = inAllCachesMask;
29312665Snikos.nikoleris@arm.com    int j = 0;
29412665Snikos.nikoleris@arm.com
29512665Snikos.nikoleris@arm.com    while (blk) {
29612665Snikos.nikoleris@arm.com        panic_if(blk->inCachesMask != in_caches_mask, "Expected cache mask "
29712665Snikos.nikoleris@arm.com                 "%x found %x", blk->inCachesMask, in_caches_mask);
29812665Snikos.nikoleris@arm.com
29912665Snikos.nikoleris@arm.com        curr_size += blkSize;
30012665Snikos.nikoleris@arm.com        if (curr_size == tracked_cache_size && blk != tail) {
30112665Snikos.nikoleris@arm.com            panic_if(boundaries[j] != blk, "Unexpected boundary for the %d-th "
30212665Snikos.nikoleris@arm.com                     "cache", j);
30312665Snikos.nikoleris@arm.com            tracked_cache_size <<= 1;
30412665Snikos.nikoleris@arm.com            // from this point, blocks fit only in the larger caches
30512665Snikos.nikoleris@arm.com            in_caches_mask &= ~(1U << j);
30612665Snikos.nikoleris@arm.com            ++j;
30712665Snikos.nikoleris@arm.com        }
30812665Snikos.nikoleris@arm.com        blk = blk->next;
30912665Snikos.nikoleris@arm.com    }
31012665Snikos.nikoleris@arm.com#endif // FALRU_DEBUG
31112665Snikos.nikoleris@arm.com}
31212665Snikos.nikoleris@arm.com
31312665Snikos.nikoleris@arm.comvoid
31412665Snikos.nikoleris@arm.comFALRU::CacheTracking::init(FALRUBlk *head, FALRUBlk *tail)
31512665Snikos.nikoleris@arm.com{
31612665Snikos.nikoleris@arm.com    // early exit if we are not tracking any extra caches
31712665Snikos.nikoleris@arm.com    FALRUBlk* blk = numTrackedCaches ? head : nullptr;
31812665Snikos.nikoleris@arm.com    unsigned curr_size = 0;
31912665Snikos.nikoleris@arm.com    unsigned tracked_cache_size = minTrackedSize;
32012665Snikos.nikoleris@arm.com    CachesMask in_caches_mask = inAllCachesMask;
32112665Snikos.nikoleris@arm.com    int j = 0;
32212665Snikos.nikoleris@arm.com
32312665Snikos.nikoleris@arm.com    while (blk) {
32412665Snikos.nikoleris@arm.com        blk->inCachesMask = in_caches_mask;
32512665Snikos.nikoleris@arm.com
32612665Snikos.nikoleris@arm.com        curr_size += blkSize;
32712665Snikos.nikoleris@arm.com        if (curr_size == tracked_cache_size && blk != tail) {
32812665Snikos.nikoleris@arm.com            boundaries[j] = blk;
32912665Snikos.nikoleris@arm.com
33012665Snikos.nikoleris@arm.com            tracked_cache_size <<= 1;
33112665Snikos.nikoleris@arm.com            // from this point, blocks fit only in the larger caches
33212665Snikos.nikoleris@arm.com            in_caches_mask &= ~(1U << j);
33312665Snikos.nikoleris@arm.com            ++j;
33412665Snikos.nikoleris@arm.com        }
33512665Snikos.nikoleris@arm.com        blk = blk->next;
33612665Snikos.nikoleris@arm.com    }
33712665Snikos.nikoleris@arm.com}
33812665Snikos.nikoleris@arm.com
33912665Snikos.nikoleris@arm.com
34012665Snikos.nikoleris@arm.comvoid
34112665Snikos.nikoleris@arm.comFALRU::CacheTracking::moveBlockToHead(FALRUBlk *blk)
34212665Snikos.nikoleris@arm.com{
34312665Snikos.nikoleris@arm.com    // Get the mask of all caches, in which the block didn't fit
34412665Snikos.nikoleris@arm.com    // before moving it to the head
34512665Snikos.nikoleris@arm.com    CachesMask update_caches_mask = inAllCachesMask ^ blk->inCachesMask;
34612665Snikos.nikoleris@arm.com
34712665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
34812665Snikos.nikoleris@arm.com        CachesMask current_cache_mask = 1U << i;
34912665Snikos.nikoleris@arm.com        if (current_cache_mask & update_caches_mask) {
35012665Snikos.nikoleris@arm.com            // if the ith cache didn't fit the block (before it is moved to
35112665Snikos.nikoleris@arm.com            // the head), move the ith boundary 1 block closer to the
35212665Snikos.nikoleris@arm.com            // MRU
35312665Snikos.nikoleris@arm.com            boundaries[i]->inCachesMask &= ~current_cache_mask;
35412665Snikos.nikoleris@arm.com            boundaries[i] = boundaries[i]->prev;
35512665Snikos.nikoleris@arm.com        } else if (boundaries[i] == blk) {
35612665Snikos.nikoleris@arm.com            // Make sure the boundary doesn't point to the block
35712665Snikos.nikoleris@arm.com            // we are about to move
35812665Snikos.nikoleris@arm.com            boundaries[i] = blk->prev;
35912665Snikos.nikoleris@arm.com        }
36012665Snikos.nikoleris@arm.com    }
36112665Snikos.nikoleris@arm.com
36212665Snikos.nikoleris@arm.com    // Make block reside in all caches
36312665Snikos.nikoleris@arm.com    blk->inCachesMask = inAllCachesMask;
36412665Snikos.nikoleris@arm.com}
36512665Snikos.nikoleris@arm.com
36612665Snikos.nikoleris@arm.comvoid
36712665Snikos.nikoleris@arm.comFALRU::CacheTracking::moveBlockToTail(FALRUBlk *blk)
36812665Snikos.nikoleris@arm.com{
36912665Snikos.nikoleris@arm.com    CachesMask update_caches_mask = blk->inCachesMask;
37012665Snikos.nikoleris@arm.com
37112665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
37212665Snikos.nikoleris@arm.com        CachesMask current_cache_mask = 1U << i;
37312665Snikos.nikoleris@arm.com        if (current_cache_mask & update_caches_mask) {
37412665Snikos.nikoleris@arm.com            // if the ith cache fitted the block (before it is moved to
37512665Snikos.nikoleris@arm.com            // the tail), move the ith boundary 1 block closer to the
37612665Snikos.nikoleris@arm.com            // LRU
37712665Snikos.nikoleris@arm.com            boundaries[i] = boundaries[i]->next;
37812665Snikos.nikoleris@arm.com            if (boundaries[i] == blk) {
37912665Snikos.nikoleris@arm.com                // Make sure the boundary doesn't point to the block
38012665Snikos.nikoleris@arm.com                // we are about to move
38112665Snikos.nikoleris@arm.com                boundaries[i] = blk->next;
38212665Snikos.nikoleris@arm.com            }
38312665Snikos.nikoleris@arm.com            boundaries[i]->inCachesMask |= current_cache_mask;
38412665Snikos.nikoleris@arm.com        }
38512665Snikos.nikoleris@arm.com    }
38612665Snikos.nikoleris@arm.com
38712665Snikos.nikoleris@arm.com    // The block now fits only in the actual cache
38812665Snikos.nikoleris@arm.com    blk->inCachesMask = 0;
38912665Snikos.nikoleris@arm.com}
39012665Snikos.nikoleris@arm.com
39112665Snikos.nikoleris@arm.comvoid
39212665Snikos.nikoleris@arm.comFALRU::CacheTracking::recordAccess(FALRUBlk *blk)
39312665Snikos.nikoleris@arm.com{
39412665Snikos.nikoleris@arm.com    for (int i = 0; i < numTrackedCaches; i++) {
39512665Snikos.nikoleris@arm.com        if (blk && ((1U << i) & blk->inCachesMask)) {
39612665Snikos.nikoleris@arm.com            hits[i]++;
39712665Snikos.nikoleris@arm.com        } else {
39812665Snikos.nikoleris@arm.com            misses[i]++;
39912665Snikos.nikoleris@arm.com        }
40012665Snikos.nikoleris@arm.com    }
40112665Snikos.nikoleris@arm.com
40212665Snikos.nikoleris@arm.com    // Record stats for the actual cache too
40312775Snikos.nikoleris@arm.com    if (blk && blk->isValid()) {
40412665Snikos.nikoleris@arm.com        hits[numTrackedCaches]++;
40512665Snikos.nikoleris@arm.com    } else {
40612665Snikos.nikoleris@arm.com        misses[numTrackedCaches]++;
40712665Snikos.nikoleris@arm.com    }
40812665Snikos.nikoleris@arm.com
40912665Snikos.nikoleris@arm.com    accesses++;
41012665Snikos.nikoleris@arm.com}
41112665Snikos.nikoleris@arm.com
41212665Snikos.nikoleris@arm.comvoid
41312665Snikos.nikoleris@arm.comprintSize(std::ostream &stream, size_t size)
41412665Snikos.nikoleris@arm.com{
41512665Snikos.nikoleris@arm.com    static const char *SIZES[] = { "B", "kB", "MB", "GB", "TB", "ZB" };
41612665Snikos.nikoleris@arm.com    int div = 0;
41712665Snikos.nikoleris@arm.com    while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
41812665Snikos.nikoleris@arm.com        div++;
41912665Snikos.nikoleris@arm.com        size >>= 10;
42012665Snikos.nikoleris@arm.com    }
42112665Snikos.nikoleris@arm.com    stream << size << SIZES[div];
42212665Snikos.nikoleris@arm.com}
42312665Snikos.nikoleris@arm.com
42412665Snikos.nikoleris@arm.comvoid
42512665Snikos.nikoleris@arm.comFALRU::CacheTracking::regStats(std::string name)
42612665Snikos.nikoleris@arm.com{
42712665Snikos.nikoleris@arm.com    hits
42812665Snikos.nikoleris@arm.com        .init(numTrackedCaches + 1)
42912665Snikos.nikoleris@arm.com        .name(name + ".falru_hits")
43012665Snikos.nikoleris@arm.com        .desc("The number of hits in each cache size.")
43112665Snikos.nikoleris@arm.com        ;
43212665Snikos.nikoleris@arm.com    misses
43312665Snikos.nikoleris@arm.com        .init(numTrackedCaches + 1)
43412665Snikos.nikoleris@arm.com        .name(name + ".falru_misses")
43512665Snikos.nikoleris@arm.com        .desc("The number of misses in each cache size.")
43612665Snikos.nikoleris@arm.com        ;
43712665Snikos.nikoleris@arm.com    accesses
43812665Snikos.nikoleris@arm.com        .name(name + ".falru_accesses")
43912665Snikos.nikoleris@arm.com        .desc("The number of accesses to the FA LRU cache.")
44012665Snikos.nikoleris@arm.com        ;
44112665Snikos.nikoleris@arm.com
44212665Snikos.nikoleris@arm.com    for (unsigned i = 0; i < numTrackedCaches + 1; ++i) {
44312665Snikos.nikoleris@arm.com        std::stringstream size_str;
44412665Snikos.nikoleris@arm.com        printSize(size_str, minTrackedSize << i);
44512665Snikos.nikoleris@arm.com        hits.subname(i, size_str.str());
44612665Snikos.nikoleris@arm.com        hits.subdesc(i, "Hits in a " + size_str.str() + " cache");
44712665Snikos.nikoleris@arm.com        misses.subname(i, size_str.str());
44812665Snikos.nikoleris@arm.com        misses.subdesc(i, "Misses in a " + size_str.str() + " cache");
44912665Snikos.nikoleris@arm.com    }
45012665Snikos.nikoleris@arm.com}
451