Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Not Most Recently Used Cache replacement policy #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/gui/dialogs/new/NewDialogCache.ui
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<string>Pseudo Least Recently Used (PLRU)</string>
</property>
</item>
<item>
<property name="text">
<string>Not Most Recently Used (NMRU)</string>
</property>
</item>
</widget>
</item>
<item row="4" column="0">
Expand Down
3 changes: 2 additions & 1 deletion src/machine/machineconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class CacheConfig {
RP_RAND, // Random
RP_LRU, // Least recently used
RP_LFU, // Least frequently used
RP_PLRU // Pseudo Least recently used
RP_PLRU, // Pseudo Least recently used
RP_NMRU // Not most recently used
};

enum WritePolicy {
Expand Down
6 changes: 3 additions & 3 deletions src/machine/memory/cache/cache.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ constexpr array<uint64_t, 1> values { 0x4142434445464748 };
* Cache configuration parameters for testing
* (all combinations are tested)
*/
constexpr array<CacheConfig::ReplacementPolicy, 4> replacement_policies {
CacheConfig::RP_RAND, CacheConfig::RP_LFU, CacheConfig::RP_LRU, CacheConfig::RP_PLRU
constexpr array<CacheConfig::ReplacementPolicy, 5> replacement_policies {
CacheConfig::RP_RAND, CacheConfig::RP_LFU, CacheConfig::RP_LRU, CacheConfig::RP_PLRU, CacheConfig::RP_NMRU
};
constexpr array<CacheConfig::WritePolicy, 3> write_policies {
CacheConfig::WP_THROUGH_NOALLOC, // THIS
Expand Down Expand Up @@ -242,7 +242,7 @@ void TestCache::cache_correctness() {
// stderr, "{ %d, %d }, ", cache.get_hit_count(),
// cache.get_miss_count());

if (cache_config.replacement_policy() != CacheConfig::RP_RAND) {
if ((cache_config.replacement_policy() != CacheConfig::RP_RAND) && (cache_config.replacement_policy() != CacheConfig::RP_NMRU)) {
// Performance of random policy is implementation dependant and
// meaningless.
QCOMPARE(performance, cache_test_performance_data.at(case_number));
Expand Down
27 changes: 27 additions & 0 deletions src/machine/memory/cache/cache_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CachePolicy::get_policy_instance(const CacheConfig *config) {
config->associativity(), config->set_count());
case CacheConfig::RP_PLRU:
return std::make_unique<CachePolicyPLRU>(config->associativity(), config->set_count());
case CacheConfig::RP_NMRU:
return std::make_unique<CachePolicyNMRU>(config->associativity(), config->set_count());
}
} else {
// Disabled cache will never use it.
Expand Down Expand Up @@ -172,4 +174,29 @@ size_t CachePolicyPLRU::select_way_to_evict(size_t row) const {
}
return (idx >= associativity) ? (associativity - 1) : idx;
}

CachePolicyNMRU::CachePolicyNMRU(size_t associativity, size_t set_count)
: associativity(associativity) {
mru_ptr.resize(set_count);
for (auto &row : mru_ptr) {
row = 0; // Initially point to block 0
}
std::srand(1); // NOLINT(cert-msc51-cpp)
}

void CachePolicyNMRU::update_stats(size_t way, size_t row, bool is_valid) {
UNUSED(is_valid)
auto &row_ptr = mru_ptr.at(row); // Set currently accessed block to most recently used
row_ptr = way;
}

size_t CachePolicyNMRU::select_way_to_evict(size_t row) const {
if(associativity == 1) {
return 0;
}
uint32_t idx = std::rand() % (associativity - 1);
auto &row_ptr = mru_ptr.at(row);
idx = (idx < row_ptr) ? idx : idx + 1;
return idx;
}
} // namespace machine
26 changes: 26 additions & 0 deletions src/machine/memory/cache/cache_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,32 @@ class CachePolicyPLRU final : public CachePolicy {
const size_t associativityCLog2;
};

/**
* Not Most Recently Used
*
* Select Randomly from Not Most
* Recently Used Blocks
*/
class CachePolicyNMRU final : public CachePolicy {
public:
/**
* @param associativity degree of assiciaivity
* @param set_count number of blocks / rows in a way (or sets in
* cache)
*/
CachePolicyNMRU(size_t associativity, size_t set_count);

[[nodiscard]] size_t select_way_to_evict(size_t row) const final;

void update_stats(size_t way, size_t row, bool is_valid) final;

private:
/**
* Pointer to Most Recently Used Block
*/
std::vector<uint32_t> mru_ptr;
const size_t associativity;
};
} // namespace machine

#endif // CACHE_POLICY_H
Loading