From d204e56b2b9b8ad561fc258ebdc50ae8365159e1 Mon Sep 17 00:00:00 2001 From: "Daniel R. Carvalho" Date: Fri, 9 Mar 2018 15:04:20 +0100 Subject: [PATCH] mem-cache: Use possible locations to find block Use possible locations to find block to make it placement policy independent. Change-Id: I4c9d9e1e1ff91ce12e85ca1970f927d8f4f5a93b Reviewed-on: https://gem5-review.googlesource.com/c/8884 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power --- src/mem/cache/tags/base.cc | 29 +++++++++++++++++ src/mem/cache/tags/base.hh | 20 ++++++++++-- src/mem/cache/tags/base_set_assoc.cc | 17 ++-------- src/mem/cache/tags/base_set_assoc.hh | 48 ++++++++++++---------------- src/mem/cache/tags/sector_tags.cc | 29 ++++++++++------- src/mem/cache/tags/sector_tags.hh | 24 +++++++------- src/mem/cache/tags/skewed_assoc.cc | 13 ++++---- src/mem/cache/tags/skewed_assoc.hh | 25 ++++++++------- 8 files changed, 120 insertions(+), 85 deletions(-) diff --git a/src/mem/cache/tags/base.cc b/src/mem/cache/tags/base.cc index 7f848e0d8..9358652bf 100644 --- a/src/mem/cache/tags/base.cc +++ b/src/mem/cache/tags/base.cc @@ -78,6 +78,35 @@ BaseTags::setCache(BaseCache *_cache) cache = _cache; } +std::vector +BaseTags::getPossibleLocations(const Addr addr) const +{ + panic("Unimplemented getPossibleLocations for tags subclass"); +} + +CacheBlk* +BaseTags::findBlock(Addr addr, bool is_secure) const +{ + // Extract block tag + Addr tag = extractTag(addr); + + // Find possible locations for the given address + const std::vector locations = + getPossibleLocations(addr); + + // Search for block + for (const auto& location : locations) { + CacheBlk* blk = static_cast(location); + if ((blk->tag == tag) && blk->isValid() && + (blk->isSecure() == is_secure)) { + return blk; + } + } + + // Did not find block + return nullptr; +} + void BaseTags::insertBlock(const Addr addr, const bool is_secure, const int src_master_ID, const uint32_t task_ID, diff --git a/src/mem/cache/tags/base.hh b/src/mem/cache/tags/base.hh index 385d25c9f..7badc46c2 100644 --- a/src/mem/cache/tags/base.hh +++ b/src/mem/cache/tags/base.hh @@ -161,6 +161,18 @@ class BaseTags : public ClockedObject */ void setCache(BaseCache *_cache); + /** + * Find all possible block locations for insertion and replacement of + * an address. Should be called immediately before ReplacementPolicy's + * findVictim() not to break cache resizing. + * Returns blocks in all ways belonging to the set of the address. + * + * @param addr The addr to a find possible locations for. + * @return The possible locations. + */ + virtual std::vector getPossibleLocations( + const Addr addr) const; + public: typedef BaseTagsParams Params; BaseTags(const Params *p); @@ -199,9 +211,13 @@ class BaseTags : public ClockedObject std::string print(); /** - * Find a block using the memory address + * Finds the block in the cache without touching it. + * + * @param addr The address to look for. + * @param is_secure True if the target memory space is secure. + * @return Pointer to the cache block. */ - virtual CacheBlk * findBlock(Addr addr, bool is_secure) const = 0; + virtual CacheBlk *findBlock(Addr addr, bool is_secure) const; /** * Find a block given set and way. diff --git a/src/mem/cache/tags/base_set_assoc.cc b/src/mem/cache/tags/base_set_assoc.cc index 8bd65e03b..05712ec8f 100644 --- a/src/mem/cache/tags/base_set_assoc.cc +++ b/src/mem/cache/tags/base_set_assoc.cc @@ -84,14 +84,12 @@ BaseSetAssoc::init(BaseCache* cache) // Initialize blocks unsigned blkIndex = 0; // index into blks array for (unsigned i = 0; i < numSets; ++i) { - sets[i].assoc = assoc; - - sets[i].blks.resize(assoc); + sets[i].resize(assoc); // link in the data blocks for (unsigned j = 0; j < assoc; ++j) { // Select block within the set to be linked - BlkType*& blk = sets[i].blks[j]; + BlkType*& blk = sets[i][j]; // Locate next cache block blk = &blks[blkIndex]; @@ -128,19 +126,10 @@ BaseSetAssoc::invalidate(CacheBlk *blk) replacementPolicy->invalidate(blk->replacementData); } -CacheBlk* -BaseSetAssoc::findBlock(Addr addr, bool is_secure) const -{ - Addr tag = extractTag(addr); - unsigned set = extractSet(addr); - BlkType *blk = sets[set].findBlk(tag, is_secure); - return blk; -} - ReplaceableEntry* BaseSetAssoc::findBlockBySetAndWay(int set, int way) const { - return sets[set].blks[way]; + return sets[set][way]; } BaseSetAssoc * diff --git a/src/mem/cache/tags/base_set_assoc.hh b/src/mem/cache/tags/base_set_assoc.hh index a3826499f..26cc6bcf0 100644 --- a/src/mem/cache/tags/base_set_assoc.hh +++ b/src/mem/cache/tags/base_set_assoc.hh @@ -59,7 +59,6 @@ #include "mem/cache/blk.hh" #include "mem/cache/replacement_policies/base.hh" #include "mem/cache/tags/base.hh" -#include "mem/cache/tags/cacheset.hh" #include "params/BaseSetAssoc.hh" /** @@ -76,7 +75,7 @@ class BaseSetAssoc : public BaseTags /** Typedef the block type used in this tag store. */ typedef CacheBlk BlkType; /** Typedef the set type used in this tag store. */ - typedef CacheSet SetType; + typedef std::vector SetType; protected: /** The associativity of the cache. */ @@ -106,6 +105,25 @@ class BaseSetAssoc : public BaseTags /** Replacement policy */ BaseReplacementPolicy *replacementPolicy; + /** + * Find all possible block locations for insertion and replacement of + * an address. Should be called immediately before ReplacementPolicy's + * findVictim() not to break cache resizing. + * Returns blocks in all ways belonging to the set of the address. + * + * @param addr The addr to a find possible locations for. + * @return The possible locations. + */ + std::vector getPossibleLocations(const Addr addr) const + override + { + std::vector locations; + for (const auto& blk : sets[extractSet(addr)]) { + locations.push_back(static_cast(blk)); + } + return locations; + } + public: /** Convenience typedef. */ typedef BaseSetAssocParams Params; @@ -186,16 +204,6 @@ class BaseSetAssoc : public BaseTags return blk; } - /** - * Finds the given address in the cache, do not update replacement data. - * i.e. This is a no-side-effect find of a block. - * - * @param addr The address to find. - * @param is_secure True if the target memory space is secure. - * @return Pointer to the cache block if found. - */ - CacheBlk* findBlock(Addr addr, bool is_secure) const override; - /** * Find a block given set and way. * @@ -218,7 +226,7 @@ class BaseSetAssoc : public BaseTags std::vector& evict_blks) const override { // Get possible locations for the victim block - std::vector locations = getPossibleLocations(addr); + std::vector locations = getPossibleLocations(addr); // Choose replacement victim from replacement candidates CacheBlk* victim = static_cast(replacementPolicy->getVictim( @@ -234,20 +242,6 @@ class BaseSetAssoc : public BaseTags return victim; } - /** - * Find all possible block locations for insertion and replacement of - * an address. Should be called immediately before ReplacementPolicy's - * findVictim() not to break cache resizing. - * Returns blocks in all ways belonging to the set of the address. - * - * @param addr The addr to a find possible locations for. - * @return The possible locations. - */ - virtual const std::vector getPossibleLocations(Addr addr) const - { - return sets[extractSet(addr)].blks; - } - /** * Insert the new block into the cache and update replacement data. * diff --git a/src/mem/cache/tags/sector_tags.cc b/src/mem/cache/tags/sector_tags.cc index 988fda540..ef3fec2b9 100644 --- a/src/mem/cache/tags/sector_tags.cc +++ b/src/mem/cache/tags/sector_tags.cc @@ -194,10 +194,14 @@ SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat) return blk; } -const std::vector -SectorTags::getPossibleLocations(Addr addr) const +std::vector +SectorTags::getPossibleLocations(const Addr addr) const { - return sets[extractSet(addr)]; + std::vector locations; + for (const auto& blk : sets[extractSet(addr)]) { + locations.push_back(static_cast(blk)); + } + return locations; } void @@ -238,11 +242,12 @@ SectorTags::findBlock(Addr addr, bool is_secure) const const Addr offset = extractSectorOffset(addr); // Find all possible sector locations for the given address - const std::vector locations = getPossibleLocations(addr); + const std::vector locations = + getPossibleLocations(addr); // Search for block for (const auto& sector : locations) { - auto blk = sector->blks[offset]; + auto blk = static_cast(sector)->blks[offset]; if (blk->getTag() == tag && blk->isValid() && blk->isSecure() == is_secure) { return blk; @@ -264,16 +269,17 @@ SectorTags::findVictim(Addr addr, const bool is_secure, std::vector& evict_blks) const { // Get all possible locations of this sector - const std::vector sector_locations = + const std::vector sector_locations = getPossibleLocations(addr); // Check if the sector this address belongs to has been allocated Addr tag = extractTag(addr); SectorBlk* victim_sector = nullptr; - for (const auto& sector : sector_locations){ - if ((tag == sector->getTag()) && sector->isValid() && - (is_secure == sector->isSecure())){ - victim_sector = sector; + for (const auto& sector : sector_locations) { + SectorBlk* sector_blk = static_cast(sector); + if ((tag == sector_blk->getTag()) && sector_blk->isValid() && + (is_secure == sector_blk->isSecure())){ + victim_sector = sector_blk; break; } } @@ -282,8 +288,7 @@ SectorTags::findVictim(Addr addr, const bool is_secure, if (victim_sector == nullptr){ // Choose replacement victim from replacement candidates victim_sector = static_cast(replacementPolicy->getVictim( - std::vector( - sector_locations.begin(), sector_locations.end()))); + sector_locations)); } // Get the location of the victim block within the sector diff --git a/src/mem/cache/tags/sector_tags.hh b/src/mem/cache/tags/sector_tags.hh index 7a2cddae9..c3c3bc8f9 100644 --- a/src/mem/cache/tags/sector_tags.hh +++ b/src/mem/cache/tags/sector_tags.hh @@ -100,6 +100,18 @@ class SectorTags : public BaseTags /** Mask out all bits that aren't part of the set index. */ const unsigned setMask; + /** + * Find all possible block locations for insertion and replacement of + * an address. Should be called immediately before ReplacementPolicy's + * findVictim() not to break cache resizing. + * Returns sector blocks in all ways belonging to the set of the address. + * + * @param addr The addr to a find possible locations for. + * @return The possible locations. + */ + std::vector getPossibleLocations(const Addr addr) const + override; + public: /** Convenience typedef. */ typedef SectorTagsParams Params; @@ -142,18 +154,6 @@ class SectorTags : public BaseTags */ CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override; - /** - * Find all possible block locations for insertion and replacement of - * an address. Should be called immediately before ReplacementPolicy's - * findVictim() not to break cache resizing. - * Returns sector blocks in all ways belonging to the set of the address. - * - * @param addr The addr to a find possible locations for. - * @return The possible locations. - */ - virtual const std::vector getPossibleLocations(Addr addr) - const; - /** * Insert the new block into the cache and update replacement data. * diff --git a/src/mem/cache/tags/skewed_assoc.cc b/src/mem/cache/tags/skewed_assoc.cc index b3945d0c4..89ab61588 100644 --- a/src/mem/cache/tags/skewed_assoc.cc +++ b/src/mem/cache/tags/skewed_assoc.cc @@ -204,15 +204,15 @@ SkewedAssoc::regenerateBlkAddr(const CacheBlk* blk) const return (blk->tag << tagShift) | (set << setShift); } -const std::vector -SkewedAssoc::getPossibleLocations(Addr addr) const +std::vector +SkewedAssoc::getPossibleLocations(const Addr addr) const { - std::vector locations; + std::vector locations; // Parse all ways for (int way = 0; way < assoc; ++way) { // Apply hash to get set, and get way entry in it - locations.push_back(sets[extractSet(addr, way)].blks[way]); + locations.push_back(sets[extractSet(addr, way)][way]); } return locations; @@ -225,10 +225,11 @@ SkewedAssoc::findBlock(Addr addr, bool is_secure) const Addr tag = extractTag(addr); // Find possible locations for the given address - std::vector locations = getPossibleLocations(addr); + std::vector locations = getPossibleLocations(addr); // Search for block - for (const auto& blk : locations) { + for (const auto& location : locations) { + CacheBlk* blk = static_cast(location); if ((blk->tag == tag) && blk->isValid() && (blk->isSecure() == is_secure)) { return blk; diff --git a/src/mem/cache/tags/skewed_assoc.hh b/src/mem/cache/tags/skewed_assoc.hh index 9fc39e297..597c32fcd 100644 --- a/src/mem/cache/tags/skewed_assoc.hh +++ b/src/mem/cache/tags/skewed_assoc.hh @@ -120,6 +120,19 @@ class SkewedAssoc : public BaseSetAssoc */ unsigned extractSet(Addr addr, unsigned way) const; + protected: + /** + * Find all possible block locations for insertion and replacement of + * an address. Should be called immediately before ReplacementPolicy's + * findVictim() not to break cache resizing. + * Returns blocks in all ways belonging to the set of the address. + * + * @param addr The addr to a find possible locations for. + * @return The possible locations. + */ + std::vector getPossibleLocations(const Addr addr) const + override; + public: /** Convenience typedef. */ typedef SkewedAssocParams Params; @@ -134,18 +147,6 @@ class SkewedAssoc : public BaseSetAssoc */ ~SkewedAssoc() {}; - /** - * Find all possible block locations for insertion and replacement of - * an address. Should be called immediately before ReplacementPolicy's - * findVictim() not to break cache resizing. - * Returns blocks in all ways belonging to the set of the address. - * - * @param addr The addr to a find possible locations for. - * @return The possible locations. - */ - const std::vector getPossibleLocations(Addr addr) const - override; - /** * Finds the given address in the cache, do not update replacement data. * i.e. This is a no-side-effect find of a block. -- 2.30.2