Skip to content

Commit

Permalink
Update cachesim.
Browse files Browse the repository at this point in the history
Add Policy FIFO for cache replacement.
  • Loading branch information
tinhanho committed Dec 24, 2023
1 parent bf63ddc commit 101dc8c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/cachesim/cachesim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,19 @@ CacheSim::locateEvictionWay(const CacheTransaction &transaction) {
std::pair<unsigned, CacheSim::CacheWay *> ew;
ew.first = s_invalidIndex;
ew.second = nullptr;

// Locate a new way based on replacement policy.
if (m_replPolicy == ReplPolicy::Random) {
// Select a random way
ew.first = std::rand() % getWays();
ew.second = &cacheLine[ew.first];
} else if (m_replPolicy == ReplPolicy::LRU) {
}
else if (m_replPolicy == ReplPolicy::FIFO){
ew.first = CacheSim::counter;
ew.second = &cacheLine[ew.first];
CacheSim::counter += 1;
CacheSim::counter %= getWays();
}
else if (m_replPolicy == ReplPolicy::LRU) {
if (getWays() == 1) {
// Nothing to do if we are in LRU and only have 1 set.
ew.first = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/cachesim/cachesim.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CacheSim;

enum WriteAllocPolicy { WriteAllocate, NoWriteAllocate };
enum WritePolicy { WriteThrough, WriteBack };
enum ReplPolicy { Random, LRU };
enum ReplPolicy { Random, LRU, FIFO};

struct CachePreset {
QString name;
Expand Down Expand Up @@ -91,7 +91,7 @@ class CacheSim : public CacheInterface {
Q_OBJECT
public:
static constexpr unsigned s_invalidIndex = static_cast<unsigned>(-1);

int counter = 0;
struct CacheSize {
unsigned bits = 0;
std::vector<QString> components;
Expand Down Expand Up @@ -329,7 +329,7 @@ public slots:
};

const static std::map<ReplPolicy, QString> s_cacheReplPolicyStrings{
{ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}};
{ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}, {ReplPolicy::FIFO, "FIFO"}};
const static std::map<WriteAllocPolicy, QString> s_cacheWriteAllocateStrings{
{WriteAllocPolicy::WriteAllocate, "Write allocate"},
{WriteAllocPolicy::NoWriteAllocate, "No write allocate"}};
Expand Down

0 comments on commit 101dc8c

Please sign in to comment.