Compare commits

...

2 Commits

Author SHA1 Message Date
LYC cee11fface 更改log 2024-10-31 10:56:17 +08:00
LYC 52ac2e49dc fix 6 2024-10-31 10:56:07 +08:00
4 changed files with 76 additions and 103 deletions

View File

@ -6,32 +6,40 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int CacheConfig::getBlockSize() { return blockSize; } CacheConfig::CacheConfig(int blockSize, int size, int assoc, int victimSize, int replicementPolicy, int writePolicy, string level)
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 = l; this->level = level;
this->blockSize = blockSize; this->blockSize = blockSize;
this->s = size; this->size = size;
this->assoc = ass; this->assoc = assoc;
this->victimSize = vis; this->victimSize = victimSize;
this->replicementPolicy = rep; this->replicementPolicy = replicementPolicy;
this->writePolicy = wrp; this->writePolicy = writePolicy;
setupTIO(); 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;
} }
double CacheConfig::getHT() double CacheConfig::getHT()
{ {
return 0.25 + 2.5 * (s / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc; return 0.25 + 2.5 * (size / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
} }
double CacheConfig::getHT2() double CacheConfig::getHT2()
{ {
return 2.5 + 2.5 * (s / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc; return 2.5 + 2.5 * (size / (512.0 * 1024)) + 0.025 * (blockSize / 16.0) + 0.025 * assoc;
} }
double CacheConfig::getMP() double CacheConfig::getMP()
@ -41,6 +49,7 @@ double CacheConfig::getMP()
int CacheConfig::log2(uint32_t x) int CacheConfig::log2(uint32_t x)
{ {
return x == 0 ? -1 : static_cast<int>(std::log2(x));
return x == 0 ? -1 : 31 - __builtin_clz(x); return x == 0 ? -1 : 31 - __builtin_clz(x);
} }
@ -49,60 +58,27 @@ uint32_t CacheConfig::getMaxIndex()
return (1 << index); 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) CacheIndex CacheConfig::parserVictimAddr(uint32_t addr)
{ {
uint32_t offset = addr & vom; uint32_t offset = addr & victimOffsetMask;
// uint32_t index = (addr & vim) >> victimOffset;
uint32_t tag = addr >> victimOffset; uint32_t tag = addr >> victimOffset;
return {tag, 0, offset, addr}; return {tag, 0, offset, addr};
} }
CacheIndex CacheConfig::AddrParser(uint32_t addr) CacheIndex CacheConfig::addrParser(uint32_t addr)
{ {
uint32_t offset = addr & offsetMask; uint32_t offset = addr & offsetMask;
uint32_t index = (addr & indexMask) >> o; uint32_t index = (addr & indexMask) >> this->offset;
uint32_t tag = addr >> (o + this->index); uint32_t tag = addr >> (this->offset + this->index);
return {tag, index, offset, addr}; return {tag, index, offset, addr};
} }
uint32_t CacheConfig::UnParser(uint32_t tag, CacheIndex tio) uint32_t CacheConfig::reparserAddr(uint32_t tag, CacheIndex tio)
{ {
return (tag << (o + index)) | (tio.index << o) | (tio.offset); return (tag << (this->offset + index)) | (tio.index << this->offset) | (tio.offset);
} }
uint32_t CacheConfig::UnParserVic(uint32_t tag, CacheIndex tio) uint32_t CacheConfig::reparserVictimAddr(uint32_t tag, CacheIndex tio)
{ {
return (tag << (victimOffset)) | (tio.offset); return (tag << (victimOffset)) | (tio.offset);
} }
@ -113,10 +89,10 @@ Cache::Cache(CacheConfig* config, Cache* nextCache)
this->config = config; this->config = config;
cache.resize(config->getMaxIndex()); cache.resize(config->getMaxIndex());
for (auto &b : cache) for (auto &b : cache)
b.resize(config->getAs()); b.resize(config->assoc);
if (config->victimSize) if (config->victimSize)
victimCache.resize(config->getVictimAs()); victimCache.resize(config->victimAssoc);
this->nextCache = nextCache; this->nextCache = nextCache;
} }
@ -126,20 +102,20 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
isWrite ? writeCount++ : readCount++; isWrite ? writeCount++ : readCount++;
auto addr = config->AddrParser(rawAddr); auto addr = config->addrParser(rawAddr);
auto s = &cache[addr.index]; auto set = &cache[addr.index];
auto cl = find_if( auto cl = find_if(
s->begin(), set->begin(),
s->end(), set->end(),
[=](const Block &b) [=](const Block &b)
{ {
return b.valid && b.tag == addr.tag; return b.valid && b.tag == addr.tag;
}); });
if (cl != s->end()) if (cl != set->end())
{ {
for (auto &b : (*s)) for (auto &b : (*set))
b.lru += (b.lru < cl->lru); b.lru += (b.lru < cl->lru);
cl->lru = 0; cl->lru = 0;
cl->dirty |= isWrite; cl->dirty |= isWrite;
@ -148,19 +124,19 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
// Main Cache Miss // Main Cache Miss
cl = find_if( cl = find_if(
s->begin(), set->begin(),
s->end(), set->end(),
[](const Block &b) [](const Block &b)
{ {
return !b.valid; return !b.valid;
}); });
if (cl == s->end()) if (cl == set->end())
{ {
// Replace // Replace
cl = max_element( cl = max_element(
s->begin(), set->begin(),
s->end()); set->end());
} }
if (!victimCache.size()) if (!victimCache.size())
@ -174,7 +150,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
writeBack++; writeBack++;
if (nextCache != nullptr) if (nextCache != nullptr)
{ {
nextCache->useCache(config->UnParser(cl->tag, addr), true); nextCache->useCache(config->reparserAddr(cl->tag, addr), true);
} }
} }
@ -184,7 +160,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
nextCache->useCache(rawAddr, false); nextCache->useCache(rawAddr, false);
} }
for (auto &b : (*s)) for (auto &b : (*set))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite, true}; *cl = {addr.tag, 0, isWrite, true};
} }
@ -209,10 +185,10 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
{ {
for (auto &b : *vcp) for (auto &b : *vcp)
b.lru += (b.lru < tmp.lru); b.lru += (b.lru < tmp.lru);
*vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true}; *vic = {config->parserVictimAddr(config->reparserAddr(cl->tag, addr)).tag, 0, cl->dirty, true};
} }
for (auto &b : (*s)) for (auto &b : (*set))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite || tmp.dirty, true}; *cl = {addr.tag, 0, isWrite || tmp.dirty, true};
exchange++; exchange++;
@ -227,7 +203,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
{ {
nextCache->useCache(rawAddr, false); nextCache->useCache(rawAddr, false);
} }
for (auto &b : (*s)) for (auto &b : (*set))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite, true}; *cl = {addr.tag, 0, isWrite, true};
return; return;
@ -251,15 +227,15 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
writeBack++; writeBack++;
if (nextCache != nullptr) if (nextCache != nullptr)
{ {
nextCache->useCache(config->UnParserVic(vic->tag, vaddr), true); nextCache->useCache(config->reparserVictimAddr(vic->tag, vaddr), true);
} }
} }
} }
for (auto &b : *vcp) for (auto &b : *vcp)
b.lru++; b.lru++;
*vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true}; *vic = {config->parserVictimAddr(config->reparserAddr(cl->tag, addr)).tag, 0, cl->dirty, true};
for (auto &b : (*s)) for (auto &b : (*set))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite, true}; *cl = {addr.tag, 0, isWrite, true};
if (nextCache != nullptr) if (nextCache != nullptr)
@ -273,7 +249,7 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
void Cache::printCache() void Cache::printCache()
{ {
if (!this->config->Size()) if (!this->config->size)
return; return;
cout << "===== " << this->config->level << " contents =====" << endl; cout << "===== " << this->config->level << " contents =====" << endl;
for (unsigned i = 0; i != this->cache.size(); i++) for (unsigned i = 0; i != this->cache.size(); i++)
@ -317,4 +293,7 @@ double Cache::getAAT()
return HT + (MP * MR); return HT + (MP * MR);
} }
int Cache::getCommunication() { return readMiss + writeMiss + writeBack; } int Cache::getCommunication()
{
return readMiss + writeMiss + writeBack;
}

View File

@ -7,6 +7,7 @@
#include <iomanip> #include <iomanip>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#include <cmath>
using namespace std; using namespace std;
struct CacheIndex struct CacheIndex
{ {
@ -19,17 +20,17 @@ public:
string level; string level;
int blockSize; int blockSize;
int s; // Size int size;
int assoc; // Assoc int assoc;
int replicementPolicy; int replicementPolicy;
int writePolicy; int writePolicy;
int victimSize; int victimSize;
int victimAsso; int victimAssoc;
int tag; // tag bit int tag;
int index; // index int index;
int o; // offset int offset;
uint32_t offsetMask; uint32_t offsetMask;
uint32_t indexMask; uint32_t indexMask;
@ -38,29 +39,22 @@ public:
int victimIndex; int victimIndex;
int victimOffset; int victimOffset;
uint32_t vom; // victim offset mask uint32_t victimOffsetMask;
uint32_t vim; // victim index mask uint32_t victimIndexMask;
CacheConfig(); CacheConfig(int blockSize, int size, int assoc, int victimSize, int replicementPolicy, int writePolicy, string level);
CacheConfig(int blockSize, int size, int ass, int vis, int rep, int wrp, string l);
double getHT(); double getHT();
double getHT2(); double getHT2();
double getMP(); double getMP();
int log2(uint32_t x); int log2(uint32_t x);
uint32_t getMaxIndex(); uint32_t getMaxIndex();
uint32_t getVictimAs();
uint32_t getAs();
void setupTIO();
CacheIndex parserVictimAddr(uint32_t addr); CacheIndex parserVictimAddr(uint32_t addr);
CacheIndex AddrParser(uint32_t addr); CacheIndex addrParser(uint32_t addr);
uint32_t UnParser(uint32_t tag, CacheIndex tio); uint32_t reparserAddr(uint32_t tag, CacheIndex tio);
uint32_t UnParserVic(uint32_t tag, CacheIndex tio); uint32_t reparserVictimAddr(uint32_t tag, CacheIndex tio);
int getBlockSize();
int Size();
int Assoc();
int ReplicementPolicy();
int WritePolicy();
}; };
struct Block struct Block

Binary file not shown.

Binary file not shown.