Compare commits
No commits in common. "cee11fface0bd57918bf2828425d302bc1e09988" and "c03fafcf952ebd67d7a72bc6998d41d3d95c8462" have entirely different histories.
cee11fface
...
c03fafcf95
|
|
@ -6,40 +6,32 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
CacheConfig::CacheConfig(int blockSize, int size, int assoc, int victimSize, int replicementPolicy, int writePolicy, string level)
|
||||
int CacheConfig::getBlockSize() { return blockSize; }
|
||||
int CacheConfig::Size() { return s; }
|
||||
int CacheConfig::Assoc() { return assoc; }
|
||||
int CacheConfig::ReplicementPolicy() { return replicementPolicy; }
|
||||
int CacheConfig::WritePolicy() { return writePolicy; }
|
||||
|
||||
CacheConfig::CacheConfig() {}
|
||||
CacheConfig::CacheConfig(int blockSize, int size, int ass, int vis, int rep, int wrp, string l)
|
||||
{
|
||||
this->level = level;
|
||||
this->level = l;
|
||||
this->blockSize = blockSize;
|
||||
this->size = size;
|
||||
this->assoc = assoc;
|
||||
this->victimSize = victimSize;
|
||||
this->replicementPolicy = replicementPolicy;
|
||||
this->writePolicy = writePolicy;
|
||||
this->offset = log2(blockSize);
|
||||
index = log2(size / (blockSize * assoc));
|
||||
tag = 32 - this->offset - index;
|
||||
|
||||
offsetMask = (1 << this->offset) - 1;
|
||||
indexMask = ((1 << index) - 1) << this->offset;
|
||||
|
||||
if (!victimSize)
|
||||
return;
|
||||
|
||||
victimOffset = log2(blockSize);
|
||||
victimIndex = 0;
|
||||
victimTag = 32 - victimOffset - victimIndex;
|
||||
victimAssoc = victimSize / blockSize;
|
||||
|
||||
victimOffsetMask = (1 << victimOffset) - 1;
|
||||
this->s = size;
|
||||
this->assoc = ass;
|
||||
this->victimSize = vis;
|
||||
this->replicementPolicy = rep;
|
||||
this->writePolicy = wrp;
|
||||
setupTIO();
|
||||
}
|
||||
double CacheConfig::getHT()
|
||||
{
|
||||
return 0.25 + 2.5 * (size / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
|
||||
return 0.25 + 2.5 * (s / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
|
||||
}
|
||||
|
||||
double CacheConfig::getHT2()
|
||||
{
|
||||
return 2.5 + 2.5 * (size / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
|
||||
return 2.5 + 2.5 * (s / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
|
||||
}
|
||||
|
||||
double CacheConfig::getMP()
|
||||
|
|
@ -49,7 +41,6 @@ double CacheConfig::getMP()
|
|||
|
||||
int CacheConfig::log2(uint32_t x)
|
||||
{
|
||||
return x == 0 ? -1 : static_cast<int>(std::log2(x));
|
||||
return x == 0 ? -1 : 31 - __builtin_clz(x);
|
||||
}
|
||||
|
||||
|
|
@ -58,41 +49,74 @@ uint32_t CacheConfig::getMaxIndex()
|
|||
return (1 << index);
|
||||
}
|
||||
|
||||
uint32_t CacheConfig::getVictimAs()
|
||||
{
|
||||
return victimAsso;
|
||||
}
|
||||
|
||||
uint32_t CacheConfig::getAs()
|
||||
{
|
||||
return assoc;
|
||||
}
|
||||
|
||||
void CacheConfig::setupTIO()
|
||||
{
|
||||
// setup tag, index, b
|
||||
o = log2(blockSize);
|
||||
index = log2(s / (blockSize * assoc));
|
||||
tag = 32 - o - index;
|
||||
|
||||
offsetMask = (1 << o) - 1;
|
||||
indexMask = ((1 << index) - 1) << o;
|
||||
|
||||
if (!victimSize)
|
||||
return;
|
||||
|
||||
victimOffset = log2(blockSize);
|
||||
victimIndex = 0;
|
||||
victimTag = 32 - victimOffset - victimIndex;
|
||||
victimAsso = victimSize / blockSize;
|
||||
|
||||
vom = (1 << victimOffset) - 1;
|
||||
// vim = ((1<<victimIndex)-1) << victimOffset;
|
||||
}
|
||||
|
||||
CacheIndex CacheConfig::parserVictimAddr(uint32_t addr)
|
||||
{
|
||||
uint32_t offset = addr & victimOffsetMask;
|
||||
uint32_t offset = addr & vom;
|
||||
// uint32_t index = (addr & vim) >> victimOffset;
|
||||
uint32_t tag = addr >> victimOffset;
|
||||
return {tag, 0, offset, addr};
|
||||
}
|
||||
|
||||
CacheIndex CacheConfig::addrParser(uint32_t addr)
|
||||
CacheIndex CacheConfig::AddrParser(uint32_t addr)
|
||||
{
|
||||
uint32_t offset = addr & offsetMask;
|
||||
uint32_t index = (addr & indexMask) >> this->offset;
|
||||
uint32_t tag = addr >> (this->offset + this->index);
|
||||
uint32_t index = (addr & indexMask) >> o;
|
||||
uint32_t tag = addr >> (o + this->index);
|
||||
return {tag, index, offset, addr};
|
||||
}
|
||||
|
||||
uint32_t CacheConfig::reparserAddr(uint32_t tag, CacheIndex tio)
|
||||
uint32_t CacheConfig::UnParser(uint32_t tag, CacheIndex tio)
|
||||
{
|
||||
return (tag << (this->offset + index)) | (tio.index << this->offset) | (tio.offset);
|
||||
return (tag << (o + index)) | (tio.index << o) | (tio.offset);
|
||||
}
|
||||
|
||||
uint32_t CacheConfig::reparserVictimAddr(uint32_t tag, CacheIndex tio)
|
||||
uint32_t CacheConfig::UnParserVic(uint32_t tag, CacheIndex tio)
|
||||
{
|
||||
return (tag << (victimOffset)) | (tio.offset);
|
||||
}
|
||||
|
||||
Cache::Cache(CacheConfig *config, Cache *nextCache)
|
||||
Cache::Cache(CacheConfig* config, Cache* nextCache)
|
||||
{
|
||||
|
||||
this->config = config;
|
||||
cache.resize(config->getMaxIndex());
|
||||
for (auto &b : cache)
|
||||
b.resize(config->assoc);
|
||||
b.resize(config->getAs());
|
||||
|
||||
if (config->victimSize)
|
||||
victimCache.resize(config->victimAssoc);
|
||||
victimCache.resize(config->getVictimAs());
|
||||
|
||||
this->nextCache = nextCache;
|
||||
}
|
||||
|
|
@ -102,20 +126,20 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
|
||||
isWrite ? writeCount++ : readCount++;
|
||||
|
||||
auto addr = config->addrParser(rawAddr);
|
||||
auto set = &cache[addr.index];
|
||||
auto addr = config->AddrParser(rawAddr);
|
||||
auto s = &cache[addr.index];
|
||||
|
||||
auto cl = find_if(
|
||||
set->begin(),
|
||||
set->end(),
|
||||
s->begin(),
|
||||
s->end(),
|
||||
[=](const Block &b)
|
||||
{
|
||||
return b.valid && b.tag == addr.tag;
|
||||
});
|
||||
|
||||
if (cl != set->end())
|
||||
if (cl != s->end())
|
||||
{
|
||||
for (auto &b : (*set))
|
||||
for (auto &b : (*s))
|
||||
b.lru += (b.lru < cl->lru);
|
||||
cl->lru = 0;
|
||||
cl->dirty |= isWrite;
|
||||
|
|
@ -124,19 +148,19 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
|
||||
// Main Cache Miss
|
||||
cl = find_if(
|
||||
set->begin(),
|
||||
set->end(),
|
||||
s->begin(),
|
||||
s->end(),
|
||||
[](const Block &b)
|
||||
{
|
||||
return !b.valid;
|
||||
});
|
||||
|
||||
if (cl == set->end())
|
||||
if (cl == s->end())
|
||||
{
|
||||
// Replace
|
||||
cl = max_element(
|
||||
set->begin(),
|
||||
set->end());
|
||||
s->begin(),
|
||||
s->end());
|
||||
}
|
||||
|
||||
if (!victimCache.size())
|
||||
|
|
@ -150,7 +174,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
writeBack++;
|
||||
if (nextCache != nullptr)
|
||||
{
|
||||
nextCache->useCache(config->reparserAddr(cl->tag, addr), true);
|
||||
nextCache->useCache(config->UnParser(cl->tag, addr), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -160,7 +184,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
nextCache->useCache(rawAddr, false);
|
||||
}
|
||||
|
||||
for (auto &b : (*set))
|
||||
for (auto &b : (*s))
|
||||
b.lru++;
|
||||
*cl = {addr.tag, 0, isWrite, true};
|
||||
}
|
||||
|
|
@ -185,10 +209,10 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
{
|
||||
for (auto &b : *vcp)
|
||||
b.lru += (b.lru < tmp.lru);
|
||||
*vic = {config->parserVictimAddr(config->reparserAddr(cl->tag, addr)).tag, 0, cl->dirty, true};
|
||||
*vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true};
|
||||
}
|
||||
|
||||
for (auto &b : (*set))
|
||||
for (auto &b : (*s))
|
||||
b.lru++;
|
||||
*cl = {addr.tag, 0, isWrite || tmp.dirty, true};
|
||||
exchange++;
|
||||
|
|
@ -203,7 +227,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
{
|
||||
nextCache->useCache(rawAddr, false);
|
||||
}
|
||||
for (auto &b : (*set))
|
||||
for (auto &b : (*s))
|
||||
b.lru++;
|
||||
*cl = {addr.tag, 0, isWrite, true};
|
||||
return;
|
||||
|
|
@ -227,15 +251,15 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
writeBack++;
|
||||
if (nextCache != nullptr)
|
||||
{
|
||||
nextCache->useCache(config->reparserVictimAddr(vic->tag, vaddr), true);
|
||||
nextCache->useCache(config->UnParserVic(vic->tag, vaddr), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto &b : *vcp)
|
||||
b.lru++;
|
||||
*vic = {config->parserVictimAddr(config->reparserAddr(cl->tag, addr)).tag, 0, cl->dirty, true};
|
||||
*vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true};
|
||||
|
||||
for (auto &b : (*set))
|
||||
for (auto &b : (*s))
|
||||
b.lru++;
|
||||
*cl = {addr.tag, 0, isWrite, true};
|
||||
if (nextCache != nullptr)
|
||||
|
|
@ -249,7 +273,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
|
|||
|
||||
void Cache::printCache()
|
||||
{
|
||||
if (!this->config->size)
|
||||
if (!this->config->Size())
|
||||
return;
|
||||
cout << "===== " << this->config->level << " contents =====" << endl;
|
||||
for (unsigned i = 0; i != this->cache.size(); i++)
|
||||
|
|
@ -293,7 +317,4 @@ double Cache::getAAT()
|
|||
return HT + (MP * MR);
|
||||
}
|
||||
|
||||
int Cache::getCommunication()
|
||||
{
|
||||
return readMiss + writeMiss + writeBack;
|
||||
}
|
||||
int Cache::getCommunication() { return readMiss + writeMiss + writeBack; }
|
||||
|
|
@ -7,7 +7,6 @@
|
|||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
struct CacheIndex
|
||||
{
|
||||
|
|
@ -20,17 +19,17 @@ public:
|
|||
string level;
|
||||
|
||||
int blockSize;
|
||||
int size;
|
||||
int assoc;
|
||||
int s; // Size
|
||||
int assoc; // Assoc
|
||||
int replicementPolicy;
|
||||
int writePolicy;
|
||||
|
||||
int victimSize;
|
||||
int victimAssoc;
|
||||
int victimAsso;
|
||||
|
||||
int tag;
|
||||
int index;
|
||||
int offset;
|
||||
int tag; // tag bit
|
||||
int index; // index
|
||||
int o; // offset
|
||||
|
||||
uint32_t offsetMask;
|
||||
uint32_t indexMask;
|
||||
|
|
@ -39,22 +38,29 @@ public:
|
|||
int victimIndex;
|
||||
int victimOffset;
|
||||
|
||||
uint32_t victimOffsetMask;
|
||||
uint32_t victimIndexMask;
|
||||
|
||||
CacheConfig(int blockSize, int size, int assoc, int victimSize, int replicementPolicy, int writePolicy, string level);
|
||||
uint32_t vom; // victim offset mask
|
||||
uint32_t vim; // victim index mask
|
||||
|
||||
CacheConfig();
|
||||
CacheConfig(int blockSize, int size, int ass, int vis, int rep, int wrp, string l);
|
||||
|
||||
double getHT();
|
||||
double getHT2();
|
||||
double getMP();
|
||||
int log2(uint32_t x);
|
||||
uint32_t getMaxIndex();
|
||||
|
||||
uint32_t getVictimAs();
|
||||
uint32_t getAs();
|
||||
void setupTIO();
|
||||
CacheIndex parserVictimAddr(uint32_t addr);
|
||||
CacheIndex addrParser(uint32_t addr);
|
||||
uint32_t reparserAddr(uint32_t tag, CacheIndex tio);
|
||||
uint32_t reparserVictimAddr(uint32_t tag, CacheIndex tio);
|
||||
|
||||
CacheIndex AddrParser(uint32_t addr);
|
||||
uint32_t UnParser(uint32_t tag, CacheIndex tio);
|
||||
uint32_t UnParserVic(uint32_t tag, CacheIndex tio);
|
||||
int getBlockSize();
|
||||
int Size();
|
||||
int Assoc();
|
||||
int ReplicementPolicy();
|
||||
int WritePolicy();
|
||||
};
|
||||
|
||||
struct Block
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue