diff --git a/configs/learning_gem5/part1/caches.py b/configs/learning_gem5/part1/caches.py index 71fa32ae3..2a69bdb82 100644 --- a/configs/learning_gem5/part1/caches.py +++ b/configs/learning_gem5/part1/caches.py @@ -95,7 +95,7 @@ class L1DCache(L1Cache): SimpleOpts.add_option('--l1d_assoc', help="L1 data cache associativity. Default: %s" % assoc) SimpleOpts.add_option('--replacement_policy', - help="L1 cache replacement policy. [NMRU,LRU,Random]") + help="L1 cache replacement policy. [NMRU,LFU,LRU,Random]") def __init__(self, opts=None): super(L1DCache, self).__init__(opts) @@ -117,6 +117,9 @@ class L1DCache(L1Cache): elif opts.replacement_policy == "LRU": from m5.objects import LRU self.tags = LRU() + elif opts.replacement_policy == "LFU": + from m5.objects import LFU + self.tags = LFU() elif opts.replacement_policy: fatal("Unsupported replacement policy: %s" % opts.replacement_policy) diff --git a/src/mem/cache/tags/SConscript b/src/mem/cache/tags/SConscript index d593be86e..8a4abb068 100644 --- a/src/mem/cache/tags/SConscript +++ b/src/mem/cache/tags/SConscript @@ -38,3 +38,4 @@ Source('lru.cc') Source('random_repl.cc') Source('fa_lru.cc') Source('nmru.cc') +Source('lfu.cc') diff --git a/src/mem/cache/tags/Tags.py b/src/mem/cache/tags/Tags.py index 68e5fe8f0..561b9c8e9 100644 --- a/src/mem/cache/tags/Tags.py +++ b/src/mem/cache/tags/Tags.py @@ -85,3 +85,8 @@ class NMRU(BaseSetAssoc): type = 'NMRU' cxx_class = 'NMRU' cxx_header = "mem/cache/tags/nmru.hh" + +class LFU(BaseSetAssoc): + type = 'LFU' + cxx_class = 'LFU' + cxx_header = "mem/cache/tags/lfu.hh" diff --git a/src/mem/cache/tags/lfu.cc b/src/mem/cache/tags/lfu.cc new file mode 100644 index 000000000..390785267 --- /dev/null +++ b/src/mem/cache/tags/lfu.cc @@ -0,0 +1,80 @@ +/* + * Authors: Sanchayan Maity + */ + +/** + * @file + * Definitions of a LFU tag store. + */ + +#include "mem/cache/tags/lfu.hh" + +#include "debug/CacheRepl.hh" +#include "mem/cache/base.hh" + +LFU::LFU(const Params *p) : BaseSetAssoc(p) +{ + +} + +CacheBlk* +LFU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) +{ + CacheBlk *blk = BaseSetAssoc::accessBlock(addr, + is_secure, lat, master_id); + + return blk; +} + +CacheBlk* +LFU::findVictim(Addr addr) +{ + int set = extractSet(addr); + int minimum; + + BlkType *blk = nullptr; + for (int i = 0; i < allocAssoc; ++i) { + BlkType *b = sets[set].blks[i]; + if (i == 0) { + minimum = b->refCount; + blk = b; + } + else if (b->refCount < minimum) { + minimum = b->refCount; + blk = b; + } + } + assert(!blk || blk->way < allocAssoc); + + if (blk && blk->isValid()) { + DPRINTF(CacheRepl, + "set %x: selecting blk %x for replacement\n", + set, regenerateBlkAddr(blk->tag, set)); + } + + return blk; +} + +void +LFU::insertBlock(PacketPtr pkt, BlkType *blk) +{ + BaseSetAssoc::insertBlock(pkt, blk); + + int set = extractSet(pkt->getAddr()); + sets[set].moveToHead(blk); +} + +void +LFU::invalidate(CacheBlk *blk) +{ + BaseSetAssoc::invalidate(blk); + + int set = blk->set; + sets[set].moveToTail(blk); +} + +LFU* +LFUParams::create() +{ + return new LFU(this); +} diff --git a/src/mem/cache/tags/lfu.hh b/src/mem/cache/tags/lfu.hh new file mode 100644 index 000000000..5b50f452e --- /dev/null +++ b/src/mem/cache/tags/lfu.hh @@ -0,0 +1,42 @@ +/* + * Authors: Sanchayan Maity + */ + + +/** + * @file + * Declaration of a LFU tag store. + * The LFU tags guarantee that the system will + * purge the item with the lowest reference frequency. + * */ + +#ifndef __MEM_CACHE_TAGS_LFU_HH__ +#define __MEM_CACHE_TAGS_LFU_HH__ + +#include "mem/cache/tags/base_set_assoc.hh" +#include "params/LFU.hh" + +class LFU : public BaseSetAssoc +{ + public: + /** Convenience typedef. */ + typedef LFUParams Params; + + /** + * Construct and initialize this tag store. + */ + LFU(const Params *p); + + /** + * Destructor + */ + ~LFU() {} + + CacheBlk* accessBlock(Addr addr, bool is_secure, + Cycles &lat, int context_src); + CacheBlk* findVictim(Addr addr); + void insertBlock(PacketPtr pkt, BlkType *blk); + void invalidate(CacheBlk *blk); +}; + +#endif // __MEM_CACHE_TAGS_LFU_HH__