12810SN/A/*
212728Snikos.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 *
142810SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
152810SN/A * All rights reserved.
162810SN/A *
172810SN/A * Redistribution and use in source and binary forms, with or without
182810SN/A * modification, are permitted provided that the following conditions are
192810SN/A * met: redistributions of source code must retain the above copyright
202810SN/A * notice, this list of conditions and the following disclaimer;
212810SN/A * redistributions in binary form must reproduce the above copyright
222810SN/A * notice, this list of conditions and the following disclaimer in the
232810SN/A * documentation and/or other materials provided with the distribution;
242810SN/A * neither the name of the copyright holders nor the names of its
252810SN/A * contributors may be used to endorse or promote products derived from
262810SN/A * this software without specific prior written permission.
272810SN/A *
282810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392810SN/A *
402810SN/A * Authors: Erik Hallnor
412810SN/A *          Ron Dreslinski
422810SN/A */
432810SN/A
442810SN/A/**
452810SN/A * @file
462810SN/A * Definitions of BaseTags.
472810SN/A */
482810SN/A
4911486Snikos.nikoleris@arm.com#include "mem/cache/tags/base.hh"
5011486Snikos.nikoleris@arm.com
5112727Snikos.nikoleris@arm.com#include <cassert>
5212727Snikos.nikoleris@arm.com
5312727Snikos.nikoleris@arm.com#include "base/types.hh"
5413225Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/replaceable_entry.hh"
5513219Sodanrc@yahoo.com.br#include "mem/cache/tags/indexing_policies/base.hh"
5612727Snikos.nikoleris@arm.com#include "mem/request.hh"
5712727Snikos.nikoleris@arm.com#include "sim/core.hh"
582810SN/A#include "sim/sim_exit.hh"
5912727Snikos.nikoleris@arm.com#include "sim/system.hh"
602810SN/A
619796Sprakash.ramrakhyani@arm.comBaseTags::BaseTags(const Params *p)
6211893Snikos.nikoleris@arm.com    : ClockedObject(p), blkSize(p->block_size), blkMask(blkSize - 1),
6313418Sodanrc@yahoo.com.br      size(p->size), lookupLatency(p->tag_latency),
6413419Sodanrc@yahoo.com.br      system(p->system), indexingPolicy(p->indexing_policy),
6512513Sodanrc@yahoo.com.br      warmupBound((p->warmup_percentage/100.0) * (p->size / p->block_size)),
6612629Sodanrc@yahoo.com.br      warmedUp(false), numBlocks(p->size / p->block_size),
6712629Sodanrc@yahoo.com.br      dataBlks(new uint8_t[p->size]) // Allocate data storage in one big chunk
689796Sprakash.ramrakhyani@arm.com{
699796Sprakash.ramrakhyani@arm.com}
709796Sprakash.ramrakhyani@arm.com
7113219Sodanrc@yahoo.com.brReplaceableEntry*
7213219Sodanrc@yahoo.com.brBaseTags::findBlockBySetAndWay(int set, int way) const
7313217Sodanrc@yahoo.com.br{
7413219Sodanrc@yahoo.com.br    return indexingPolicy->getEntry(set, way);
7513217Sodanrc@yahoo.com.br}
7613217Sodanrc@yahoo.com.br
7713217Sodanrc@yahoo.com.brCacheBlk*
7813217Sodanrc@yahoo.com.brBaseTags::findBlock(Addr addr, bool is_secure) const
7913217Sodanrc@yahoo.com.br{
8013217Sodanrc@yahoo.com.br    // Extract block tag
8113217Sodanrc@yahoo.com.br    Addr tag = extractTag(addr);
8213217Sodanrc@yahoo.com.br
8313219Sodanrc@yahoo.com.br    // Find possible entries that may contain the given address
8413219Sodanrc@yahoo.com.br    const std::vector<ReplaceableEntry*> entries =
8513219Sodanrc@yahoo.com.br        indexingPolicy->getPossibleEntries(addr);
8613217Sodanrc@yahoo.com.br
8713217Sodanrc@yahoo.com.br    // Search for block
8813219Sodanrc@yahoo.com.br    for (const auto& location : entries) {
8913217Sodanrc@yahoo.com.br        CacheBlk* blk = static_cast<CacheBlk*>(location);
9013217Sodanrc@yahoo.com.br        if ((blk->tag == tag) && blk->isValid() &&
9113217Sodanrc@yahoo.com.br            (blk->isSecure() == is_secure)) {
9213217Sodanrc@yahoo.com.br            return blk;
9313217Sodanrc@yahoo.com.br        }
9413217Sodanrc@yahoo.com.br    }
9513217Sodanrc@yahoo.com.br
9613217Sodanrc@yahoo.com.br    // Did not find block
9713217Sodanrc@yahoo.com.br    return nullptr;
9813217Sodanrc@yahoo.com.br}
9913217Sodanrc@yahoo.com.br
1002810SN/Avoid
10113752Sodanrc@yahoo.com.brBaseTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
10212636Sodanrc@yahoo.com.br{
10312722Snikos.nikoleris@arm.com    assert(!blk->isValid());
10412722Snikos.nikoleris@arm.com
10512636Sodanrc@yahoo.com.br    // Previous block, if existed, has been removed, and now we have
10612636Sodanrc@yahoo.com.br    // to insert the new one
10713752Sodanrc@yahoo.com.br
10812636Sodanrc@yahoo.com.br    // Deal with what we are bringing in
10913752Sodanrc@yahoo.com.br    MasterID master_id = pkt->req->masterId();
11013752Sodanrc@yahoo.com.br    assert(master_id < system->maxMasters());
11113752Sodanrc@yahoo.com.br    occupancies[master_id]++;
11212636Sodanrc@yahoo.com.br
11312691Sodanrc@yahoo.com.br    // Insert block with tag, src master id and task id
11413752Sodanrc@yahoo.com.br    blk->insert(extractTag(pkt->getAddr()), pkt->isSecure(), master_id,
11513752Sodanrc@yahoo.com.br                pkt->req->taskId());
11612636Sodanrc@yahoo.com.br
11713215Sodanrc@yahoo.com.br    // Check if cache warm up is done
11812703Snikos.nikoleris@arm.com    if (!warmedUp && tagsInUse.value() >= warmupBound) {
11912703Snikos.nikoleris@arm.com        warmedUp = true;
12012703Snikos.nikoleris@arm.com        warmupCycle = curTick();
12112703Snikos.nikoleris@arm.com    }
12212703Snikos.nikoleris@arm.com
12312636Sodanrc@yahoo.com.br    // We only need to write into one tag and one data block.
12412636Sodanrc@yahoo.com.br    tagAccesses += 1;
12512636Sodanrc@yahoo.com.br    dataAccesses += 1;
12612636Sodanrc@yahoo.com.br}
12712636Sodanrc@yahoo.com.br
12813219Sodanrc@yahoo.com.brAddr
12913219Sodanrc@yahoo.com.brBaseTags::extractTag(const Addr addr) const
13013219Sodanrc@yahoo.com.br{
13113219Sodanrc@yahoo.com.br    return indexingPolicy->extractTag(addr);
13213219Sodanrc@yahoo.com.br}
13313219Sodanrc@yahoo.com.br
13412636Sodanrc@yahoo.com.brvoid
13512728Snikos.nikoleris@arm.comBaseTags::cleanupRefsVisitor(CacheBlk &blk)
13612728Snikos.nikoleris@arm.com{
13712728Snikos.nikoleris@arm.com    if (blk.isValid()) {
13812728Snikos.nikoleris@arm.com        totalRefs += blk.refCount;
13912728Snikos.nikoleris@arm.com        ++sampledRefs;
14012728Snikos.nikoleris@arm.com    }
14112728Snikos.nikoleris@arm.com}
14212728Snikos.nikoleris@arm.com
14312728Snikos.nikoleris@arm.comvoid
14412728Snikos.nikoleris@arm.comBaseTags::cleanupRefs()
14512728Snikos.nikoleris@arm.com{
14612728Snikos.nikoleris@arm.com    forEachBlk([this](CacheBlk &blk) { cleanupRefsVisitor(blk); });
14712728Snikos.nikoleris@arm.com}
14812728Snikos.nikoleris@arm.com
14912728Snikos.nikoleris@arm.comvoid
15012728Snikos.nikoleris@arm.comBaseTags::computeStatsVisitor(CacheBlk &blk)
15112728Snikos.nikoleris@arm.com{
15212728Snikos.nikoleris@arm.com    if (blk.isValid()) {
15312728Snikos.nikoleris@arm.com        assert(blk.task_id < ContextSwitchTaskId::NumTaskId);
15412728Snikos.nikoleris@arm.com        occupanciesTaskId[blk.task_id]++;
15512728Snikos.nikoleris@arm.com        assert(blk.tickInserted <= curTick());
15612728Snikos.nikoleris@arm.com        Tick age = curTick() - blk.tickInserted;
15712728Snikos.nikoleris@arm.com
15812728Snikos.nikoleris@arm.com        int age_index;
15912728Snikos.nikoleris@arm.com        if (age / SimClock::Int::us < 10) { // <10us
16012728Snikos.nikoleris@arm.com            age_index = 0;
16112728Snikos.nikoleris@arm.com        } else if (age / SimClock::Int::us < 100) { // <100us
16212728Snikos.nikoleris@arm.com            age_index = 1;
16312728Snikos.nikoleris@arm.com        } else if (age / SimClock::Int::ms < 1) { // <1ms
16412728Snikos.nikoleris@arm.com            age_index = 2;
16512728Snikos.nikoleris@arm.com        } else if (age / SimClock::Int::ms < 10) { // <10ms
16612728Snikos.nikoleris@arm.com            age_index = 3;
16712728Snikos.nikoleris@arm.com        } else
16812728Snikos.nikoleris@arm.com            age_index = 4; // >10ms
16912728Snikos.nikoleris@arm.com
17012728Snikos.nikoleris@arm.com        ageTaskId[blk.task_id][age_index]++;
17112728Snikos.nikoleris@arm.com    }
17212728Snikos.nikoleris@arm.com}
17312728Snikos.nikoleris@arm.com
17412728Snikos.nikoleris@arm.comvoid
17512728Snikos.nikoleris@arm.comBaseTags::computeStats()
17612728Snikos.nikoleris@arm.com{
17712728Snikos.nikoleris@arm.com    for (unsigned i = 0; i < ContextSwitchTaskId::NumTaskId; ++i) {
17812728Snikos.nikoleris@arm.com        occupanciesTaskId[i] = 0;
17912728Snikos.nikoleris@arm.com        for (unsigned j = 0; j < 5; ++j) {
18012728Snikos.nikoleris@arm.com            ageTaskId[i][j] = 0;
18112728Snikos.nikoleris@arm.com        }
18212728Snikos.nikoleris@arm.com    }
18312728Snikos.nikoleris@arm.com
18412728Snikos.nikoleris@arm.com    forEachBlk([this](CacheBlk &blk) { computeStatsVisitor(blk); });
18512728Snikos.nikoleris@arm.com}
18612728Snikos.nikoleris@arm.com
18712728Snikos.nikoleris@arm.comstd::string
18812728Snikos.nikoleris@arm.comBaseTags::print()
18912728Snikos.nikoleris@arm.com{
19012728Snikos.nikoleris@arm.com    std::string str;
19112728Snikos.nikoleris@arm.com
19212728Snikos.nikoleris@arm.com    auto print_blk = [&str](CacheBlk &blk) {
19312728Snikos.nikoleris@arm.com        if (blk.isValid())
19413222Sodanrc@yahoo.com.br            str += csprintf("\tBlock: %s\n", blk.print());
19512728Snikos.nikoleris@arm.com    };
19612728Snikos.nikoleris@arm.com    forEachBlk(print_blk);
19712728Snikos.nikoleris@arm.com
19812728Snikos.nikoleris@arm.com    if (str.empty())
19912728Snikos.nikoleris@arm.com        str = "no valid tags\n";
20012728Snikos.nikoleris@arm.com
20112728Snikos.nikoleris@arm.com    return str;
20212728Snikos.nikoleris@arm.com}
20312728Snikos.nikoleris@arm.com
20412728Snikos.nikoleris@arm.comvoid
2059796Sprakash.ramrakhyani@arm.comBaseTags::regStats()
2062810SN/A{
20711522Sstephan.diestelhorst@arm.com    ClockedObject::regStats();
20811522Sstephan.diestelhorst@arm.com
2092810SN/A    using namespace Stats;
21011522Sstephan.diestelhorst@arm.com
2112810SN/A    tagsInUse
2129796Sprakash.ramrakhyani@arm.com        .name(name() + ".tagsinuse")
2132810SN/A        .desc("Cycle average of tags in use")
2142810SN/A        ;
2152810SN/A
2162810SN/A    totalRefs
2179796Sprakash.ramrakhyani@arm.com        .name(name() + ".total_refs")
2182810SN/A        .desc("Total number of references to valid blocks.")
2192810SN/A        ;
2202810SN/A
2212810SN/A    sampledRefs
2229796Sprakash.ramrakhyani@arm.com        .name(name() + ".sampled_refs")
2232810SN/A        .desc("Sample count of references to valid blocks.")
2242810SN/A        ;
2252810SN/A
2262810SN/A    avgRefs
2279796Sprakash.ramrakhyani@arm.com        .name(name() + ".avg_refs")
2282810SN/A        .desc("Average number of references to valid blocks.")
2292810SN/A        ;
2302810SN/A
2312810SN/A    avgRefs = totalRefs/sampledRefs;
2322810SN/A
2332810SN/A    warmupCycle
2349796Sprakash.ramrakhyani@arm.com        .name(name() + ".warmup_cycle")
2352810SN/A        .desc("Cycle when the warmup percentage was hit.")
2362810SN/A        ;
2372810SN/A
2386978SLisa.Hsu@amd.com    occupancies
23913419Sodanrc@yahoo.com.br        .init(system->maxMasters())
2409796Sprakash.ramrakhyani@arm.com        .name(name() + ".occ_blocks")
2418833Sdam.sunwoo@arm.com        .desc("Average occupied blocks per requestor")
2426978SLisa.Hsu@amd.com        .flags(nozero | nonan)
2436978SLisa.Hsu@amd.com        ;
24413419Sodanrc@yahoo.com.br    for (int i = 0; i < system->maxMasters(); i++) {
24513419Sodanrc@yahoo.com.br        occupancies.subname(i, system->getMasterName(i));
2468833Sdam.sunwoo@arm.com    }
2476978SLisa.Hsu@amd.com
2486978SLisa.Hsu@amd.com    avgOccs
2499796Sprakash.ramrakhyani@arm.com        .name(name() + ".occ_percent")
2506978SLisa.Hsu@amd.com        .desc("Average percentage of cache occupancy")
2518833Sdam.sunwoo@arm.com        .flags(nozero | total)
2526978SLisa.Hsu@amd.com        ;
25313419Sodanrc@yahoo.com.br    for (int i = 0; i < system->maxMasters(); i++) {
25413419Sodanrc@yahoo.com.br        avgOccs.subname(i, system->getMasterName(i));
2558833Sdam.sunwoo@arm.com    }
2566978SLisa.Hsu@amd.com
2576978SLisa.Hsu@amd.com    avgOccs = occupancies / Stats::constant(numBlocks);
2586978SLisa.Hsu@amd.com
25910024Sdam.sunwoo@arm.com    occupanciesTaskId
26010024Sdam.sunwoo@arm.com        .init(ContextSwitchTaskId::NumTaskId)
26110024Sdam.sunwoo@arm.com        .name(name() + ".occ_task_id_blocks")
26210024Sdam.sunwoo@arm.com        .desc("Occupied blocks per task id")
26310024Sdam.sunwoo@arm.com        .flags(nozero | nonan)
26410024Sdam.sunwoo@arm.com        ;
26510024Sdam.sunwoo@arm.com
26610024Sdam.sunwoo@arm.com    ageTaskId
26710024Sdam.sunwoo@arm.com        .init(ContextSwitchTaskId::NumTaskId, 5)
26810024Sdam.sunwoo@arm.com        .name(name() + ".age_task_id_blocks")
26910024Sdam.sunwoo@arm.com        .desc("Occupied blocks per task id")
27010024Sdam.sunwoo@arm.com        .flags(nozero | nonan)
27110024Sdam.sunwoo@arm.com        ;
27210024Sdam.sunwoo@arm.com
27310024Sdam.sunwoo@arm.com    percentOccsTaskId
27410024Sdam.sunwoo@arm.com        .name(name() + ".occ_task_id_percent")
27510024Sdam.sunwoo@arm.com        .desc("Percentage of cache occupancy per task id")
27610024Sdam.sunwoo@arm.com        .flags(nozero)
27710024Sdam.sunwoo@arm.com        ;
27810024Sdam.sunwoo@arm.com
27910024Sdam.sunwoo@arm.com    percentOccsTaskId = occupanciesTaskId / Stats::constant(numBlocks);
28010024Sdam.sunwoo@arm.com
28110025Stimothy.jones@arm.com    tagAccesses
28210025Stimothy.jones@arm.com        .name(name() + ".tag_accesses")
28310025Stimothy.jones@arm.com        .desc("Number of tag accesses")
28410025Stimothy.jones@arm.com        ;
28510025Stimothy.jones@arm.com
28610025Stimothy.jones@arm.com    dataAccesses
28710025Stimothy.jones@arm.com        .name(name() + ".data_accesses")
28810025Stimothy.jones@arm.com        .desc("Number of data accesses")
28910025Stimothy.jones@arm.com        ;
29010025Stimothy.jones@arm.com
29110024Sdam.sunwoo@arm.com    registerDumpCallback(new BaseTagsDumpCallback(this));
2922810SN/A    registerExitCallback(new BaseTagsCallback(this));
2932810SN/A}
294