mem: cache: tags: Implement LFU Cache replacement policy

Implement Least Frequently Used Cache replacement policy.
This commit is contained in:
Sanchayan Maity 2017-03-23 17:21:09 +05:30
parent f70c734a85
commit 6f9826e621
5 changed files with 132 additions and 1 deletions

View File

@ -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)

View File

@ -38,3 +38,4 @@ Source('lru.cc')
Source('random_repl.cc')
Source('fa_lru.cc')
Source('nmru.cc')
Source('lfu.cc')

View File

@ -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"

80
src/mem/cache/tags/lfu.cc vendored Normal file
View File

@ -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);
}

42
src/mem/cache/tags/lfu.hh vendored Normal file
View File

@ -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__