summaryrefslogtreecommitdiff
path: root/src/mem/cache/replacement_policies
diff options
context:
space:
mode:
authorDaniel R. Carvalho <odanrc@yahoo.com.br>2018-02-19 15:13:11 +0100
committerDaniel Carvalho <odanrc@yahoo.com.br>2018-03-22 14:50:23 +0000
commitd207e9ccee411877fdeac80bb68a27900560f50f (patch)
tree120810cf72c52ed5df29436552e06f1fb11aa5ce /src/mem/cache/replacement_policies
parent0473286ab1e9992a906eff380000bf90c82eeccb (diff)
downloadgem5-d207e9ccee411877fdeac80bb68a27900560f50f.tar.xz
mem-cache: Split array indexing and replacement policies.
Replacement policies (LRU, Random) are currently considered as array indexing methods, but have completely different functionalities: - Array indexers determine the possible locations for block allocation. This information is used to generate replacement candidates when conflicts happen. - Replacement policies determine which of the replacement candidates should be evicted to make room for new allocations. For this reason, they were split into different classes. Advantages: - Easier and more straightforward to implement other replacement policies (RRIP, LFU, ARC, ...) - Allow easier future implementation of cache organization schemes As now we can't assure the use of sets, the previous way to create a true LRU is not viable. Now a timestamp_bits parameter controls how many bits are dedicated for the timestamp, and a true LRU can be achieved through an infinite number of bits (although a few bits suffice in practice). Change-Id: I23750db121f1474d17831137e6ff618beb2b3eda Reviewed-on: https://gem5-review.googlesource.com/8501 Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
Diffstat (limited to 'src/mem/cache/replacement_policies')
-rw-r--r--src/mem/cache/replacement_policies/ReplacementPolicies.py46
-rw-r--r--src/mem/cache/replacement_policies/SConscript37
-rw-r--r--src/mem/cache/replacement_policies/base.cc66
-rw-r--r--src/mem/cache/replacement_policies/base.hh105
-rw-r--r--src/mem/cache/replacement_policies/lru_rp.cc87
-rw-r--r--src/mem/cache/replacement_policies/lru_rp.hh84
-rw-r--r--src/mem/cache/replacement_policies/random_rp.cc71
-rw-r--r--src/mem/cache/replacement_policies/random_rp.hh67
8 files changed, 563 insertions, 0 deletions
diff --git a/src/mem/cache/replacement_policies/ReplacementPolicies.py b/src/mem/cache/replacement_policies/ReplacementPolicies.py
new file mode 100644
index 000000000..64743819b
--- /dev/null
+++ b/src/mem/cache/replacement_policies/ReplacementPolicies.py
@@ -0,0 +1,46 @@
+# Copyright (c) 2018 Inria
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Daniel Carvalho
+
+from m5.params import *
+from m5.proxy import *
+from m5.SimObject import SimObject
+
+class BaseReplacementPolicy(SimObject):
+ type = 'BaseReplacementPolicy'
+ abstract = True
+ cxx_header = "mem/cache/replacement_policies/base.hh"
+
+class LRURP(BaseReplacementPolicy):
+ type = 'LRURP'
+ cxx_class = 'LRURP'
+ cxx_header = "mem/cache/replacement_policies/lru_rp.hh"
+
+class RandomRP(BaseReplacementPolicy):
+ type = 'RandomRP'
+ cxx_class = 'RandomRP'
+ cxx_header = "mem/cache/replacement_policies/random_rp.hh"
diff --git a/src/mem/cache/replacement_policies/SConscript b/src/mem/cache/replacement_policies/SConscript
new file mode 100644
index 000000000..c3c89923f
--- /dev/null
+++ b/src/mem/cache/replacement_policies/SConscript
@@ -0,0 +1,37 @@
+# -*- mode:python -*-
+
+# Copyright (c) 2018 Inria
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Daniel Carvalho
+
+Import('*')
+
+SimObject('ReplacementPolicies.py')
+
+Source('base.cc')
+Source('lru_rp.cc')
+Source('random_rp.cc')
diff --git a/src/mem/cache/replacement_policies/base.cc b/src/mem/cache/replacement_policies/base.cc
new file mode 100644
index 000000000..c422a75d8
--- /dev/null
+++ b/src/mem/cache/replacement_policies/base.cc
@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+/**
+ * @file
+ * Declaration of a common base class for cache replacement policy objects.
+ * In general replacement policies try to use invalid entries as victims,
+ * and if no such blocks exist the replacement policy is applied.
+ */
+
+#include "mem/cache/replacement_policies/base.hh"
+
+BaseReplacementPolicy::BaseReplacementPolicy(const Params *p)
+ : SimObject(p)
+{
+}
+
+void
+BaseReplacementPolicy::touch(CacheBlk *blk)
+{
+ // Inform block has been touched
+ blk->isTouched = true;
+
+ // Update frequency counter
+ blk->refCount++;
+}
+
+void
+BaseReplacementPolicy::reset(CacheBlk *blk)
+{
+ // Inform block has been touched
+ blk->isTouched = true;
+
+ // Set insertion tick
+ blk->tickInserted = curTick();
+
+ // Reset frequency counter
+ blk->refCount = 0;
+}
diff --git a/src/mem/cache/replacement_policies/base.hh b/src/mem/cache/replacement_policies/base.hh
new file mode 100644
index 000000000..383f3f01a
--- /dev/null
+++ b/src/mem/cache/replacement_policies/base.hh
@@ -0,0 +1,105 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
+#define __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
+
+#include "mem/cache/base.hh"
+#include "mem/cache/blk.hh"
+#include "params/BaseReplacementPolicy.hh"
+#include "sim/sim_object.hh"
+
+/**
+ * Replacement candidates as chosen by the indexing policy.
+ *
+ * The base functions touch() and reset() must be called by all subclasses
+ * that override them.
+ *
+ * @todo
+ * Currently the replacement candidates are simply the cache blocks
+ * derived from the possible placement locations of an address, as
+ * defined by the getPossibleLocations() from BaseTags. In a future
+ * patch it should be an inheritable class to allow the replacement
+ * policies to be used with any table-like structure that needs to
+ * replace its entries.
+ */
+typedef std::vector<CacheBlk*> ReplacementCandidates;
+
+/**
+ * A common base class of cache replacement policy objects.
+ */
+class BaseReplacementPolicy : public SimObject
+{
+ public:
+ /**
+ * Convenience typedef.
+ */
+ typedef BaseReplacementPolicyParams Params;
+
+ /**
+ * Construct and initiliaze this replacement policy.
+ */
+ BaseReplacementPolicy(const Params *p);
+
+ /**
+ * Destructor.
+ */
+ virtual ~BaseReplacementPolicy() {}
+
+ /**
+ * Touch a block to update its replacement data.
+ * Updates number of references.
+ *
+ * This base function must be called by all subclasses that override it.
+ *
+ * @param blk Cache block to be touched.
+ */
+ virtual void touch(CacheBlk *blk);
+
+ /**
+ * Reset replacement data for a block. Used when a block is inserted.
+ * Sets the insertion tick, and update number of references.
+ *
+ * This base function must be called by all subclasses that override it.
+ *
+ * @param blk Cache block to be reset.
+ */
+ virtual void reset(CacheBlk *blk);
+
+ /**
+ * Find replacement victim among candidates.
+ *
+ * @param candidates Replacement candidates, selected by indexing policy.
+ * @return Cache block to be replaced.
+ */
+ virtual CacheBlk* getVictim(const ReplacementCandidates& candidates) = 0;
+};
+
+#endif // __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
diff --git a/src/mem/cache/replacement_policies/lru_rp.cc b/src/mem/cache/replacement_policies/lru_rp.cc
new file mode 100644
index 000000000..b2fa20b6f
--- /dev/null
+++ b/src/mem/cache/replacement_policies/lru_rp.cc
@@ -0,0 +1,87 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+#include "mem/cache/replacement_policies/lru_rp.hh"
+
+#include "debug/CacheRepl.hh"
+
+LRURP::LRURP(const Params *p)
+ : BaseReplacementPolicy(p)
+{
+}
+
+void
+LRURP::touch(CacheBlk *blk)
+{
+ BaseReplacementPolicy::touch(blk);
+
+ // Update last touch timestamp
+ blk->lastTouchTick = curTick();
+}
+
+void
+LRURP::reset(CacheBlk *blk)
+{
+ BaseReplacementPolicy::reset(blk);
+
+ // Set last touch timestamp
+ blk->lastTouchTick = blk->tickInserted;
+}
+
+CacheBlk*
+LRURP::getVictim(const ReplacementCandidates& candidates)
+{
+ // There must be at least one replacement candidate
+ assert(candidates.size() > 0);
+
+ // Visit all candidates to find victim
+ CacheBlk* blk = candidates[0];
+ for (const auto& candidate : candidates) {
+ // Stop iteration if found an invalid block
+ if (!candidate->isValid()) {
+ blk = candidate;
+ break;
+ // Update victim block if necessary
+ } else if (candidate->lastTouchTick < blk->lastTouchTick) {
+ blk = candidate;
+ }
+ }
+
+ DPRINTF(CacheRepl, "set %x, way %x: selecting blk for replacement\n",
+ blk->set, blk->way);
+
+ return blk;
+}
+
+LRURP*
+LRURPParams::create()
+{
+ return new LRURP(this);
+}
diff --git a/src/mem/cache/replacement_policies/lru_rp.hh b/src/mem/cache/replacement_policies/lru_rp.hh
new file mode 100644
index 000000000..3e5efe61f
--- /dev/null
+++ b/src/mem/cache/replacement_policies/lru_rp.hh
@@ -0,0 +1,84 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+/**
+ * @file
+ * Declaration of a Least Recently Used replacement policy.
+ * The victim is chosen using the timestamp. The timestamp may be true or
+ * pseudo, depending on the quantity of bits allocated for that.
+ */
+
+#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_LRU_RP_HH__
+#define __MEM_CACHE_REPLACEMENT_POLICIES_LRU_RP_HH__
+
+#include "mem/cache/replacement_policies/base.hh"
+#include "params/LRURP.hh"
+
+class LRURP : public BaseReplacementPolicy
+{
+ public:
+ /** Convenience typedef. */
+ typedef LRURPParams Params;
+
+ /**
+ * Construct and initiliaze this replacement policy.
+ */
+ LRURP(const Params *p);
+
+ /**
+ * Destructor.
+ */
+ ~LRURP() {}
+
+ /**
+ * Touch a block to update its last touch tick.
+ *
+ * @param blk Cache block to be touched.
+ */
+ void touch(CacheBlk *blk);
+
+ /**
+ * Reset replacement data for a block. Used when a block is inserted.
+ * Sets its last touch tick as the current tick.
+ *
+ * @param blk Cache block to be reset.
+ */
+ void reset(CacheBlk *blk);
+
+ /**
+ * Find replacement victim using LRU timestamps.
+ *
+ * @param candidates Replacement candidates, selected by indexing policy.
+ * @return Cache block to be replaced.
+ */
+ CacheBlk* getVictim(const ReplacementCandidates& candidates) override;
+};
+
+#endif // __MEM_CACHE_REPLACEMENT_POLICIES_LRU_RP_HH__
diff --git a/src/mem/cache/replacement_policies/random_rp.cc b/src/mem/cache/replacement_policies/random_rp.cc
new file mode 100644
index 000000000..1c54bdac6
--- /dev/null
+++ b/src/mem/cache/replacement_policies/random_rp.cc
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+#include "mem/cache/replacement_policies/random_rp.hh"
+
+#include "base/random.hh"
+#include "debug/CacheRepl.hh"
+
+RandomRP::RandomRP(const Params *p)
+ : BaseReplacementPolicy(p)
+{
+}
+
+CacheBlk*
+RandomRP::getVictim(const ReplacementCandidates& candidates)
+{
+ // There must be at least one replacement candidate
+ assert(candidates.size() > 0);
+
+ // Choose one candidate at random
+ CacheBlk* blk = candidates[random_mt.random<unsigned>(0,
+ candidates.size() - 1)];
+
+ // Visit all candidates to find an invalid entry
+ for (const auto& candidate : candidates) {
+ // Give priority to victimise invalid entries
+ if (!candidate->isValid()){
+ blk = candidate;
+ break;
+ }
+ }
+
+ // If no invalid blocks were found, choose one of the candidates randomly
+ DPRINTF(CacheRepl, "set %x, way %x: selecting blk for replacement\n",
+ blk->set, blk->way);
+
+ return blk;
+}
+
+RandomRP*
+RandomRPParams::create()
+{
+ return new RandomRP(this);
+}
diff --git a/src/mem/cache/replacement_policies/random_rp.hh b/src/mem/cache/replacement_policies/random_rp.hh
new file mode 100644
index 000000000..338b26ff2
--- /dev/null
+++ b/src/mem/cache/replacement_policies/random_rp.hh
@@ -0,0 +1,67 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+/**
+ * @file
+ * Declaration of a random replacement policy.
+ * The victim is chosen at random, if there are no invalid entries.
+ */
+
+#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_RANDOM_RP_HH__
+#define __MEM_CACHE_REPLACEMENT_POLICIES_RANDOM_RP_HH__
+
+#include "mem/cache/replacement_policies/base.hh"
+#include "params/RandomRP.hh"
+
+class RandomRP : public BaseReplacementPolicy
+{
+ public:
+ /** Convenience typedef. */
+ typedef RandomRPParams Params;
+
+ /**
+ * Construct and initiliaze this replacement policy.
+ */
+ RandomRP(const Params *p);
+
+ /**
+ * Destructor.
+ */
+ ~RandomRP() {}
+
+ /**
+ * Find replacement victim at random.
+ * @param candidates Replacement candidates, selected by indexing policy.
+ * @return Cache block to be replaced.
+ */
+ CacheBlk* getVictim(const ReplacementCandidates& candidates) override;
+};
+
+#endif // __MEM_CACHE_REPLACEMENT_POLICIES_RANDOM_RP_HH__