From 627f5dabdb4b511abac5137376213388e16d0b23 Mon Sep 17 00:00:00 2001 From: Prashanth Ramaprasad Date: Sun, 9 Apr 2017 16:18:41 +0530 Subject: [PATCH] mem: cache: tags: Implement LIFO Cache Replacement Policy --- configs/learning_gem5/part1/caches.py | 6 +- src/mem/cache/tags/SConscript | 1 + src/mem/cache/tags/Tags.py | 5 ++ src/mem/cache/tags/lifo.cc | 82 +++++++++++++++++++++++++++ src/mem/cache/tags/lifo.hh | 41 ++++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/mem/cache/tags/lifo.cc create mode 100644 src/mem/cache/tags/lifo.hh diff --git a/configs/learning_gem5/part1/caches.py b/configs/learning_gem5/part1/caches.py index 2a69bdb82..79e33d0a7 100644 --- a/configs/learning_gem5/part1/caches.py +++ b/configs/learning_gem5/part1/caches.py @@ -95,7 +95,8 @@ 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,LFU,LRU,Random]") + help="L1 cache replacement policy. [NMRU,LFU,LIFO,LRU," + "Random]") def __init__(self, opts=None): super(L1DCache, self).__init__(opts) @@ -120,6 +121,9 @@ class L1DCache(L1Cache): elif opts.replacement_policy == "LFU": from m5.objects import LFU self.tags = LFU() + elif opts.replacement_policy == "LIFO": + from m5.objects import LIFO + self.tags = LIFO() 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 8a4abb068..85a2059c8 100644 --- a/src/mem/cache/tags/SConscript +++ b/src/mem/cache/tags/SConscript @@ -39,3 +39,4 @@ Source('random_repl.cc') Source('fa_lru.cc') Source('nmru.cc') Source('lfu.cc') +Source('lifo.cc') diff --git a/src/mem/cache/tags/Tags.py b/src/mem/cache/tags/Tags.py index 561b9c8e9..72509f2ff 100644 --- a/src/mem/cache/tags/Tags.py +++ b/src/mem/cache/tags/Tags.py @@ -90,3 +90,8 @@ class LFU(BaseSetAssoc): type = 'LFU' cxx_class = 'LFU' cxx_header = "mem/cache/tags/lfu.hh" + +class LIFO(BaseSetAssoc): + type = 'LIFO' + cxx_class = 'LIFO' + cxx_header = "mem/cache/tags/lifo.hh" diff --git a/src/mem/cache/tags/lifo.cc b/src/mem/cache/tags/lifo.cc new file mode 100644 index 000000000..510776d2a --- /dev/null +++ b/src/mem/cache/tags/lifo.cc @@ -0,0 +1,82 @@ +/* + * Authors: Prashanth Ramaprasad + */ + +/** + * @file + * Definitions of a LIFO tag store. + */ + +#include "mem/cache/tags/lifo.hh" + +#include "debug/CacheRepl.hh" +#include "mem/cache/base.hh" + +LIFO::LIFO(const Params *p) + : BaseSetAssoc(p) +{ +} + +CacheBlk* +LIFO::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) +{ + CacheBlk *blk = BaseSetAssoc::accessBlock(addr, is_secure, lat, master_id); + + if (blk != nullptr) { + // move this block to head of the MRU list + sets[blk->set].moveToHead(blk); + DPRINTF(CacheRepl, "set %x: moving blk %x (%s) to MRU\n", + blk->set, regenerateBlkAddr(blk->tag, blk->set), + is_secure ? "s" : "ns"); + } + + return blk; +} + +CacheBlk* +LIFO::findVictim(Addr addr) +{ + int set = extractSet(addr); + // grab a replacement candidate + BlkType *blk = nullptr; + for (int i = 0; i <= assoc - 1; i++) { + BlkType *b = sets[set].blks[i]; + if (b->way < allocAssoc) { + blk = b; + break; + } + } + 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 +LIFO::insertBlock(PacketPtr pkt, BlkType *blk) +{ + BaseSetAssoc::insertBlock(pkt, blk); + + int set = extractSet(pkt->getAddr()); + sets[set].moveToHead(blk); +} + +void +LIFO::invalidate(CacheBlk *blk) +{ + BaseSetAssoc::invalidate(blk); + + // should be evicted before valid blocks + int set = blk->set; + sets[set].moveToTail(blk); +} + +LIFO* +LIFOParams::create() +{ + return new LIFO(this); +} diff --git a/src/mem/cache/tags/lifo.hh b/src/mem/cache/tags/lifo.hh new file mode 100644 index 000000000..5b495b89c --- /dev/null +++ b/src/mem/cache/tags/lifo.hh @@ -0,0 +1,41 @@ +/* + * Authors: Prashanth Ramaprasad + */ + +/** + * @file + * Declaration of a LIFO tag store. + * The LIFO tags guarantee that the true least-recently-used way in + * a set will always be evicted. + */ + +#ifndef __MEM_CACHE_TAGS_LIFO_HH__ +#define __MEM_CACHE_TAGS_LIFO_HH__ + +#include "mem/cache/tags/base_set_assoc.hh" +#include "params/LIFO.hh" + +class LIFO : public BaseSetAssoc +{ + public: + /** Convenience typedef. */ + typedef LIFOParams Params; + + /** + * Construct and initialize this tag store. + */ + LIFO(const Params *p); + + /** + * Destructor + */ + ~LIFO() {} + + 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_LIFO_HH__