From 101dc8c348b7102969cb0335abd63a7d497b21b0 Mon Sep 17 00:00:00 2001 From: tinhanho Date: Sat, 23 Dec 2023 17:18:45 +0800 Subject: [PATCH] Update cachesim. Add Policy FIFO for cache replacement. --- src/cachesim/cachesim.cpp | 10 ++++++++-- src/cachesim/cachesim.h | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cachesim/cachesim.cpp b/src/cachesim/cachesim.cpp index 89b39a6f3..2eb0b1a13 100644 --- a/src/cachesim/cachesim.cpp +++ b/src/cachesim/cachesim.cpp @@ -115,13 +115,19 @@ CacheSim::locateEvictionWay(const CacheTransaction &transaction) { std::pair 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; diff --git a/src/cachesim/cachesim.h b/src/cachesim/cachesim.h index ba8fd31ba..6c325b4be 100644 --- a/src/cachesim/cachesim.h +++ b/src/cachesim/cachesim.h @@ -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; @@ -91,7 +91,7 @@ class CacheSim : public CacheInterface { Q_OBJECT public: static constexpr unsigned s_invalidIndex = static_cast(-1); - + int counter = 0; struct CacheSize { unsigned bits = 0; std::vector components; @@ -329,7 +329,7 @@ public slots: }; const static std::map s_cacheReplPolicyStrings{ - {ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}}; + {ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}, {ReplPolicy::FIFO, "FIFO"}}; const static std::map s_cacheWriteAllocateStrings{ {WriteAllocPolicy::WriteAllocate, "Write allocate"}, {WriteAllocPolicy::NoWriteAllocate, "No write allocate"}};