cvut / qtrvsim

RISC-V CPU simulator for education purposes
GNU General Public License v3.0
466 stars 56 forks source link

Add Pseudo Least Recently Used Cache replacement policy #139

Closed brsf11 closed 2 months ago

brsf11 commented 2 months ago

Add PLRU replacement policy, which is more widely used in real-world hardware than ideal LRU.

image

brsf11 commented 2 months ago

I've pushed the commit for the typo, comment and cache test. For the clang-format, I'm not sure if my tool works properly since I've seen some modification on previous code when I used the format tool.

e.g.

switch (config->replacement_policy()) {
case CacheConfig::RP_RAND:
    return std::make_unique<CachePolicyRAND>(config->associativity());
case CacheConfig::RP_LRU:
    return std::make_unique<CachePolicyLRU>(
        config->associativity(), config->set_count());
case CacheConfig::RP_LFU:
    return std::make_unique<CachePolicyLFU>(
        config->associativity(), config->set_count());
case CacheConfig::RP_PLRU:
    return std::make_unique<CachePolicyPLRU>(
        config->associativity(), config->set_count());
}

will be converted to

switch (config->replacement_policy()) {
case CacheConfig::RP_RAND:
    return std::make_unique<CachePolicyRAND>(config->associativity());
case CacheConfig::RP_LRU:
    return std::make_unique<CachePolicyLRU>(config->associativity(), config->set_count());
case CacheConfig::RP_LFU:
    return std::make_unique<CachePolicyLFU>(config->associativity(), config->set_count());
case CacheConfig::RP_PLRU:
    return std::make_unique<CachePolicyPLRU>(config->associativity(), config->set_count());
}
jdupak commented 2 months ago

I've pushed the commit for the typo, comment and cache test. For the clang-format, I'm not sure if my tool works properly since I've seen some modification on previous code when I used the format tool.

e.g.

switch (config->replacement_policy()) {
case CacheConfig::RP_RAND:
    return std::make_unique<CachePolicyRAND>(config->associativity());
case CacheConfig::RP_LRU:
    return std::make_unique<CachePolicyLRU>(
        config->associativity(), config->set_count());
case CacheConfig::RP_LFU:
    return std::make_unique<CachePolicyLFU>(
        config->associativity(), config->set_count());
case CacheConfig::RP_PLRU:
    return std::make_unique<CachePolicyPLRU>(
        config->associativity(), config->set_count());
}

will be converted to

switch (config->replacement_policy()) {
case CacheConfig::RP_RAND:
    return std::make_unique<CachePolicyRAND>(config->associativity());
case CacheConfig::RP_LRU:
    return std::make_unique<CachePolicyLRU>(config->associativity(), config->set_count());
case CacheConfig::RP_LFU:
    return std::make_unique<CachePolicyLFU>(config->associativity(), config->set_count());
case CacheConfig::RP_PLRU:
    return std::make_unique<CachePolicyPLRU>(config->associativity(), config->set_count());
}

Yeah, for some parts may not be correctly formatted now. That is ok.

ppisa commented 2 months ago

Thanks to @brsf11 for idea and implementation and @jdupak for help with integration.