base.cc revision 13225:8d1621fc586e
11060SN/A/*
27944SGiacomo.Gabrielli@arm.com * Copyright (c) 2013,2016,2018 ARM Limited
37944SGiacomo.Gabrielli@arm.com * All rights reserved.
47944SGiacomo.Gabrielli@arm.com *
57944SGiacomo.Gabrielli@arm.com * The license below extends only to copyright in the software and shall
67944SGiacomo.Gabrielli@arm.com * not be construed as granting a license to any other intellectual
77944SGiacomo.Gabrielli@arm.com * property including but not limited to intellectual property relating
87944SGiacomo.Gabrielli@arm.com * to a hardware implementation of the functionality of the software
97944SGiacomo.Gabrielli@arm.com * licensed hereunder.  You may use the software subject to the license
107944SGiacomo.Gabrielli@arm.com * terms below provided that you ensure that this notice is replicated
117944SGiacomo.Gabrielli@arm.com * unmodified and in its entirety in all distributions of the software,
127944SGiacomo.Gabrielli@arm.com * modified or unmodified, in source code or in binary form.
137944SGiacomo.Gabrielli@arm.com *
142702Sktlim@umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
156973Stjones1@inf.ed.ac.uk * All rights reserved.
161060SN/A *
171060SN/A * Redistribution and use in source and binary forms, with or without
181060SN/A * modification, are permitted provided that the following conditions are
191060SN/A * met: redistributions of source code must retain the above copyright
201060SN/A * notice, this list of conditions and the following disclaimer;
211060SN/A * redistributions in binary form must reproduce the above copyright
221060SN/A * notice, this list of conditions and the following disclaimer in the
231060SN/A * documentation and/or other materials provided with the distribution;
241060SN/A * neither the name of the copyright holders nor the names of its
251060SN/A * contributors may be used to endorse or promote products derived from
261060SN/A * this software without specific prior written permission.
271060SN/A *
281060SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291060SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301060SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311060SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321060SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331060SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341060SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351060SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361060SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371060SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381060SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
391060SN/A *
402665Ssaidi@eecs.umich.edu * Authors: Erik Hallnor
412665Ssaidi@eecs.umich.edu *          Ron Dreslinski
426973Stjones1@inf.ed.ac.uk */
431060SN/A
441060SN/A/**
451464SN/A * @file
461464SN/A * Definitions of BaseTags.
471060SN/A */
482731Sktlim@umich.edu
492292SN/A#include "mem/cache/tags/base.hh"
501464SN/A
511060SN/A#include <cassert>
522669Sktlim@umich.edu
537720Sgblack@eecs.umich.edu#include "base/types.hh"
541060SN/A#include "mem/cache/base.hh"
551060SN/A#include "mem/cache/replacement_policies/replaceable_entry.hh"
561858SN/A#include "mem/cache/tags/indexing_policies/base.hh"
576658Snate@binkert.org#include "mem/request.hh"
583770Sgblack@eecs.umich.edu#include "sim/core.hh"
591464SN/A#include "sim/sim_exit.hh"
601464SN/A#include "sim/system.hh"
612669Sktlim@umich.edu
621060SN/ABaseTags::BaseTags(const Params *p)
636973Stjones1@inf.ed.ac.uk    : ClockedObject(p), blkSize(p->block_size), blkMask(blkSize - 1),
642669Sktlim@umich.edu      size(p->size),
657678Sgblack@eecs.umich.edu      lookupLatency(p->tag_latency),
662292SN/A      accessLatency(p->sequential_access ?
676023Snate@binkert.org                    p->tag_latency + p->data_latency :
681060SN/A                    std::max(p->tag_latency, p->data_latency)),
691060SN/A      cache(nullptr), indexingPolicy(p->indexing_policy),
701060SN/A      warmupBound((p->warmup_percentage/100.0) * (p->size / p->block_size)),
711060SN/A      warmedUp(false), numBlocks(p->size / p->block_size),
721060SN/A      dataBlks(new uint8_t[p->size]) // Allocate data storage in one big chunk
731060SN/A{
741061SN/A}
751061SN/A
761060SN/Avoid
771060SN/ABaseTags::setCache(BaseCache *_cache)
781061SN/A{
791060SN/A    assert(!cache);
801060SN/A    cache = _cache;
811060SN/A}
822733Sktlim@umich.edu
832733Sktlim@umich.eduReplaceableEntry*
841060SN/ABaseTags::findBlockBySetAndWay(int set, int way) const
852292SN/A{
862107SN/A    return indexingPolicy->getEntry(set, way);
872690Sktlim@umich.edu}
882107SN/A
892690Sktlim@umich.eduCacheBlk*
902690Sktlim@umich.eduBaseTags::findBlock(Addr addr, bool is_secure) const
911060SN/A{
922292SN/A    // Extract block tag
932292SN/A    Addr tag = extractTag(addr);
942292SN/A
952292SN/A    // Find possible entries that may contain the given address
962292SN/A    const std::vector<ReplaceableEntry*> entries =
972292SN/A        indexingPolicy->getPossibleEntries(addr);
981060SN/A
995543Ssaidi@eecs.umich.edu    // Search for block
1005543Ssaidi@eecs.umich.edu    for (const auto& location : entries) {
1011060SN/A        CacheBlk* blk = static_cast<CacheBlk*>(location);
1021060SN/A        if ((blk->tag == tag) && blk->isValid() &&
1032292SN/A            (blk->isSecure() == is_secure)) {
1042107SN/A            return blk;
1051060SN/A        }
1061060SN/A    }
1071060SN/A
1081060SN/A    // Did not find block
1091060SN/A    return nullptr;
1101060SN/A}
1112292SN/A
1121060SN/Avoid
1131060SN/ABaseTags::insertBlock(const Addr addr, const bool is_secure,
1145358Sgblack@eecs.umich.edu                      const int src_master_ID, const uint32_t task_ID,
1155358Sgblack@eecs.umich.edu                      CacheBlk *blk)
1165358Sgblack@eecs.umich.edu{
1175358Sgblack@eecs.umich.edu    assert(!blk->isValid());
1185358Sgblack@eecs.umich.edu
1195358Sgblack@eecs.umich.edu    // Previous block, if existed, has been removed, and now we have
1205358Sgblack@eecs.umich.edu    // to insert the new one
1215358Sgblack@eecs.umich.edu    // Deal with what we are bringing in
1225358Sgblack@eecs.umich.edu    assert(src_master_ID < cache->system->maxMasters());
1235358Sgblack@eecs.umich.edu    occupancies[src_master_ID]++;
1245358Sgblack@eecs.umich.edu
1255358Sgblack@eecs.umich.edu    // Insert block with tag, src master id and task id
1265358Sgblack@eecs.umich.edu    blk->insert(extractTag(addr), is_secure, src_master_ID, task_ID);
1272292SN/A
1282292SN/A    // Check if cache warm up is done
1292292SN/A    if (!warmedUp && tagsInUse.value() >= warmupBound) {
1302292SN/A        warmedUp = true;
1312292SN/A        warmupCycle = curTick();
1322292SN/A    }
1332292SN/A
1341060SN/A    // We only need to write into one tag and one data block.
1352132SN/A    tagAccesses += 1;
1361060SN/A    dataAccesses += 1;
1377520Sgblack@eecs.umich.edu}
1387520Sgblack@eecs.umich.edu
1392292SN/AAddr
1402292SN/ABaseTags::extractTag(const Addr addr) const
1412292SN/A{
1422292SN/A    return indexingPolicy->extractTag(addr);
1432292SN/A}
1442292SN/A
1452292SN/Avoid
1462292SN/ABaseTags::cleanupRefsVisitor(CacheBlk &blk)
1471060SN/A{
1486973Stjones1@inf.ed.ac.uk    if (blk.isValid()) {
1496973Stjones1@inf.ed.ac.uk        totalRefs += blk.refCount;
1507520Sgblack@eecs.umich.edu        ++sampledRefs;
1517520Sgblack@eecs.umich.edu    }
1527520Sgblack@eecs.umich.edu}
1536974Stjones1@inf.ed.ac.uk
1546974Stjones1@inf.ed.ac.ukvoid
1556974Stjones1@inf.ed.ac.ukBaseTags::cleanupRefs()
1566974Stjones1@inf.ed.ac.uk{
1576973Stjones1@inf.ed.ac.uk    forEachBlk([this](CacheBlk &blk) { cleanupRefsVisitor(blk); });
1586974Stjones1@inf.ed.ac.uk}
1596974Stjones1@inf.ed.ac.uk
1606973Stjones1@inf.ed.ac.ukvoid
1616973Stjones1@inf.ed.ac.ukBaseTags::computeStatsVisitor(CacheBlk &blk)
1626973Stjones1@inf.ed.ac.uk{
1636973Stjones1@inf.ed.ac.uk    if (blk.isValid()) {
1641060SN/A        assert(blk.task_id < ContextSwitchTaskId::NumTaskId);
1657944SGiacomo.Gabrielli@arm.com        occupanciesTaskId[blk.task_id]++;
1667944SGiacomo.Gabrielli@arm.com        assert(blk.tickInserted <= curTick());
1677944SGiacomo.Gabrielli@arm.com        Tick age = curTick() - blk.tickInserted;
1687944SGiacomo.Gabrielli@arm.com
1697944SGiacomo.Gabrielli@arm.com        int age_index;
1707944SGiacomo.Gabrielli@arm.com        if (age / SimClock::Int::us < 10) { // <10us
1717944SGiacomo.Gabrielli@arm.com            age_index = 0;
1727944SGiacomo.Gabrielli@arm.com        } else if (age / SimClock::Int::us < 100) { // <100us
1737944SGiacomo.Gabrielli@arm.com            age_index = 1;
1747944SGiacomo.Gabrielli@arm.com        } else if (age / SimClock::Int::ms < 1) { // <1ms
1757944SGiacomo.Gabrielli@arm.com            age_index = 2;
1767944SGiacomo.Gabrielli@arm.com        } else if (age / SimClock::Int::ms < 10) { // <10ms
1777944SGiacomo.Gabrielli@arm.com            age_index = 3;
1787944SGiacomo.Gabrielli@arm.com        } else
1797944SGiacomo.Gabrielli@arm.com            age_index = 4; // >10ms
1807944SGiacomo.Gabrielli@arm.com
1817944SGiacomo.Gabrielli@arm.com        ageTaskId[blk.task_id][age_index]++;
1827944SGiacomo.Gabrielli@arm.com    }
1837944SGiacomo.Gabrielli@arm.com}
1847944SGiacomo.Gabrielli@arm.com
1857944SGiacomo.Gabrielli@arm.comvoid
1867944SGiacomo.Gabrielli@arm.comBaseTags::computeStats()
1877944SGiacomo.Gabrielli@arm.com{
1881684SN/A    for (unsigned i = 0; i < ContextSwitchTaskId::NumTaskId; ++i) {
1891060SN/A        occupanciesTaskId[i] = 0;
1901060SN/A        for (unsigned j = 0; j < 5; ++j) {
1911060SN/A            ageTaskId[i][j] = 0;
1921060SN/A        }
1932731Sktlim@umich.edu    }
1942731Sktlim@umich.edu
1952731Sktlim@umich.edu    forEachBlk([this](CacheBlk &blk) { computeStatsVisitor(blk); });
1962731Sktlim@umich.edu}
1972731Sktlim@umich.edu
1982731Sktlim@umich.edustd::string
1992731Sktlim@umich.eduBaseTags::print()
2002731Sktlim@umich.edu{
2012731Sktlim@umich.edu    std::string str;
2022731Sktlim@umich.edu
2032731Sktlim@umich.edu    auto print_blk = [&str](CacheBlk &blk) {
2042731Sktlim@umich.edu        if (blk.isValid())
2052731Sktlim@umich.edu            str += csprintf("\tBlock: %s\n", blk.print());
2062731Sktlim@umich.edu    };
2072731Sktlim@umich.edu    forEachBlk(print_blk);
2082731Sktlim@umich.edu
2092731Sktlim@umich.edu    if (str.empty())
2102731Sktlim@umich.edu        str = "no valid tags\n";
2112731Sktlim@umich.edu
2122731Sktlim@umich.edu    return str;
2132731Sktlim@umich.edu}
2142731Sktlim@umich.edu
2152731Sktlim@umich.eduvoid
2162731Sktlim@umich.eduBaseTags::regStats()
2172731Sktlim@umich.edu{
2182292SN/A    ClockedObject::regStats();
2192731Sktlim@umich.edu
2202731Sktlim@umich.edu    using namespace Stats;
2211060SN/A
2221060SN/A    tagsInUse
2236221Snate@binkert.org        .name(name() + ".tagsinuse")
2241060SN/A        .desc("Cycle average of tags in use")
2251060SN/A        ;
2261060SN/A
2271060SN/A    totalRefs
2282292SN/A        .name(name() + ".total_refs")
2292292SN/A        .desc("Total number of references to valid blocks.")
2302292SN/A        ;
2312733Sktlim@umich.edu
2322733Sktlim@umich.edu    sampledRefs
2331060SN/A        .name(name() + ".sampled_refs")
2342680Sktlim@umich.edu        .desc("Sample count of references to valid blocks.")
2352292SN/A        ;
2361060SN/A
2371060SN/A    avgRefs
2382132SN/A        .name(name() + ".avg_refs")
2391060SN/A        .desc("Average number of references to valid blocks.")
2402702Sktlim@umich.edu        ;
2412669Sktlim@umich.edu
2422292SN/A    avgRefs = totalRefs/sampledRefs;
2431060SN/A
2441060SN/A    warmupCycle
2451060SN/A        .name(name() + ".warmup_cycle")
2464032Sktlim@umich.edu        .desc("Cycle when the warmup percentage was hit.")
2474032Sktlim@umich.edu        ;
2484032Sktlim@umich.edu
2491060SN/A    occupancies
2501060SN/A        .init(cache->system->maxMasters())
2511060SN/A        .name(name() + ".occ_blocks")
2521060SN/A        .desc("Average occupied blocks per requestor")
2531060SN/A        .flags(nozero | nonan)
2541060SN/A        ;
2551060SN/A    for (int i = 0; i < cache->system->maxMasters(); i++) {
2561060SN/A        occupancies.subname(i, cache->system->getMasterName(i));
2571060SN/A    }
2581060SN/A
2591060SN/A    avgOccs
2601060SN/A        .name(name() + ".occ_percent")
2611464SN/A        .desc("Average percentage of cache occupancy")
2621464SN/A        .flags(nozero | total)
2632356SN/A        ;
2641464SN/A    for (int i = 0; i < cache->system->maxMasters(); i++) {
2651464SN/A        avgOccs.subname(i, cache->system->getMasterName(i));
2661060SN/A    }
2671464SN/A
2681464SN/A    avgOccs = occupancies / Stats::constant(numBlocks);
2691464SN/A
2701464SN/A    occupanciesTaskId
2711060SN/A        .init(ContextSwitchTaskId::NumTaskId)
2723326Sktlim@umich.edu        .name(name() + ".occ_task_id_blocks")
2733326Sktlim@umich.edu        .desc("Occupied blocks per task id")
2743326Sktlim@umich.edu        .flags(nozero | nonan)
2757597Sminkyu.jeong@arm.com        ;
2767597Sminkyu.jeong@arm.com
2777597Sminkyu.jeong@arm.com    ageTaskId
2783965Sgblack@eecs.umich.edu        .init(ContextSwitchTaskId::NumTaskId, 5)
2797720Sgblack@eecs.umich.edu        .name(name() + ".age_task_id_blocks")
2807720Sgblack@eecs.umich.edu        .desc("Occupied blocks per task id")
2811060SN/A        .flags(nozero | nonan)
2827720Sgblack@eecs.umich.edu        ;
2837720Sgblack@eecs.umich.edu
2844636Sgblack@eecs.umich.edu    percentOccsTaskId
2853794Sgblack@eecs.umich.edu        .name(name() + ".occ_task_id_percent")
2863794Sgblack@eecs.umich.edu        .desc("Percentage of cache occupancy per task id")
2873794Sgblack@eecs.umich.edu        .flags(nozero)
2883965Sgblack@eecs.umich.edu        ;
2893965Sgblack@eecs.umich.edu
2902292SN/A    percentOccsTaskId = occupanciesTaskId / Stats::constant(numBlocks);
2912292SN/A
2922292SN/A    tagAccesses
2932292SN/A        .name(name() + ".tag_accesses")
2942292SN/A        .desc("Number of tag accesses")
2952292SN/A        ;
2961060SN/A
2971060SN/A    dataAccesses
2981060SN/A        .name(name() + ".data_accesses")
2993770Sgblack@eecs.umich.edu        .desc("Number of data accesses")
3003770Sgblack@eecs.umich.edu        ;
3013770Sgblack@eecs.umich.edu
3023770Sgblack@eecs.umich.edu    registerDumpCallback(new BaseTagsDumpCallback(this));
3033770Sgblack@eecs.umich.edu    registerExitCallback(new BaseTagsCallback(this));
3043770Sgblack@eecs.umich.edu}
3053770Sgblack@eecs.umich.edu