Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/9441

Change subject: mem-cache: Create Second-Chance replacement policy
......................................................................

mem-cache: Create Second-Chance replacement policy

Implementation of a Second-Chance replacement policy. Similar to FIFO,
but every block is given a second chance if it has been touched.

Change-Id: Id4d52b698d0045a4914a4d848fdf9c3c00a28508
---
M src/mem/cache/replacement_policies/ReplacementPolicies.py
M src/mem/cache/replacement_policies/SConscript
A src/mem/cache/replacement_policies/second_chance_rp.cc
A src/mem/cache/replacement_policies/second_chance_rp.hh
4 files changed, 278 insertions(+), 0 deletions(-)



diff --git a/src/mem/cache/replacement_policies/ReplacementPolicies.py b/src/mem/cache/replacement_policies/ReplacementPolicies.py
index 5f7ecfc..481e035 100644
--- a/src/mem/cache/replacement_policies/ReplacementPolicies.py
+++ b/src/mem/cache/replacement_policies/ReplacementPolicies.py
@@ -67,3 +67,8 @@

 class RRIPRP(BRRIPRP):
     btp = 0
+
+class SecondChanceRP(BaseReplacementPolicy):
+    type = 'SecondChanceRP'
+    cxx_class = 'SecondChanceRP'
+    cxx_header = "mem/cache/replacement_policies/second_chance_rp.hh"
diff --git a/src/mem/cache/replacement_policies/SConscript b/src/mem/cache/replacement_policies/SConscript
index 7c316dc..9f5eb38 100644
--- a/src/mem/cache/replacement_policies/SConscript
+++ b/src/mem/cache/replacement_policies/SConscript
@@ -38,3 +38,4 @@
 Source('lru_rp.cc')
 Source('mru_rp.cc')
 Source('random_rp.cc')
+Source('second_chance_rp.cc')
diff --git a/src/mem/cache/replacement_policies/second_chance_rp.cc b/src/mem/cache/replacement_policies/second_chance_rp.cc
new file mode 100644
index 0000000..36070e9
--- /dev/null
+++ b/src/mem/cache/replacement_policies/second_chance_rp.cc
@@ -0,0 +1,139 @@
+/**
+ * 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/second_chance_rp.hh"
+
+#include "debug/CacheRepl.hh"
+#include "mem/cache/blk.hh"
+
+SecondChanceRP::SecondChanceRP(const Params *p)
+    : BaseReplacementPolicy(p)
+{
+}
+
+void
+SecondChanceRP::touch(ReplacementData* replacementData)
+{
+    BaseReplacementPolicy::touch(replacementData);
+
+ // If block hasn't been touched since last re-insertion, inform it now has
+    ((SecondChanceReplData*)replacementData)->hasSecondChance = true;
+}
+
+void
+SecondChanceRP::reset(ReplacementData* replacementData)
+{
+    BaseReplacementPolicy::reset(replacementData);
+
+    // Blocks are inserted as touched
+    ((SecondChanceReplData*)replacementData)->hasSecondChance = true;
+
+    // Set this queue run's touch timestamp
+    ((SecondChanceReplData*)replacementData)->lastTouchTick =
+        replacementData->tickInserted;
+}
+
+CacheBlk*
+SecondChanceRP::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 {
+            SecondChanceReplData* candidateReplData =
+                (SecondChanceReplData*)candidate->replacementData.get();
+            SecondChanceReplData* blkReplData =
+                (SecondChanceReplData*)blk->replacementData.get();
+
+            if (candidateReplData->lastTouchTick <
+                blkReplData->lastTouchTick) {
+                // Candidate is older, so it must use its second chance
+                if (candidateReplData->hasSecondChance) {
+                    // Use candidate's second chance
+                    candidateReplData->hasSecondChance = false;
+                    candidateReplData->lastTouchTick = curTick();
+
+                    // Candidate can only be spared if blk does not have a
+                    // second chance
+                    if (blkReplData->hasSecondChance) {
+                        // Use blk's second chance
+                        blkReplData->hasSecondChance = false;
+                        blkReplData->lastTouchTick = curTick();
+
+                        blk = candidate;
+                    }
+                } else {
+ // If candidate does not have a second chance, and has an
+                    // older timestamp, it's the new victim
+                    blk = candidate;
+                }
+            } else {
+ // Candidate is newer, however blk uses second chance to make
+                // itself look even newer
+                if (blkReplData->hasSecondChance) {
+                    // Use second chance of blk
+                    blkReplData->hasSecondChance = false;
+                    blkReplData->lastTouchTick = curTick();
+
+                    // Use candidate's second chance
+                    if (candidateReplData->hasSecondChance) {
+                        candidateReplData->hasSecondChance = false;
+                        candidateReplData->lastTouchTick = curTick();
+                    } else {
+                        // Blk used its second chance to save itself
+                        blk = candidate;
+                    }
+                }
+            }
+        }
+    }
+
+    return blk;
+}
+
+std::unique_ptr<ReplacementData>
+SecondChanceRP::instantiateEntry()
+{
+    return std::unique_ptr<ReplacementData>(new SecondChanceReplData());
+}
+
+SecondChanceRP*
+SecondChanceRPParams::create()
+{
+    return new SecondChanceRP(this);
+}
diff --git a/src/mem/cache/replacement_policies/second_chance_rp.hh b/src/mem/cache/replacement_policies/second_chance_rp.hh
new file mode 100644
index 0000000..a867c98
--- /dev/null
+++ b/src/mem/cache/replacement_policies/second_chance_rp.hh
@@ -0,0 +1,133 @@
+/**
+ * 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 Second-Chance replacement policy.
+ * The victim is chosen using the timestamp. The oldest block is chosen
+ * to be evicted, if it hasn't been touched since its insertion. If it
+ * has been touched, it is given a second chance and re-inserted at the
+ * end of the queue.
+ */
+
+#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__
+#define __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__
+
+#include "mem/cache/replacement_policies/base.hh"
+#include "params/SecondChanceRP.hh"
+
+class SecondChanceRP : public BaseReplacementPolicy
+{
+  protected:
+    /** MRU-specific implementation of replacement data. */
+    class SecondChanceReplData : public ReplacementData
+    {
+      public:
+        /** Tick on which the block was last touched. */
+        Tick lastTouchTick;
+
+        /**
+         * This is different from isTouched because isTouched accounts only
+         * for insertion, while this bit is reset every new re-insertion.
+         * @sa SecondChanceRP.
+         */
+        bool hasSecondChance;
+
+        /**
+         * Default constructor.
+         */
+        SecondChanceReplData(){
+            clear();
+        }
+
+        /**
+         * Default destructor.
+         */
+        ~SecondChanceReplData(){}
+
+        /**
+         * Clear replacement data.
+         */
+        void clear() override
+        {
+            ReplacementData::clear();
+            lastTouchTick = 0;
+            hasSecondChance = true;
+        }
+    };
+
+  public:
+    /** Convenience typedef. */
+    typedef SecondChanceRPParams Params;
+
+    /**
+     * Construct and initiliaze this replacement policy.
+     */
+    SecondChanceRP(const Params *p);
+
+    /**
+     * Destructor.
+     */
+    ~SecondChanceRP() {}
+
+    /**
+     * Touch a block to update its re-insertion tick and second chance bit.
+     *
+     * @param blk Cache block to be touched.
+     */
+    void touch(ReplacementData* replacementData);
+
+    /**
+     * Reset replacement data. Used when a block is inserted or re-inserted
+     * in the queue.
+     * Sets its insertion tick and second chance bit.
+     *
+     * @param blk Cache block to be reset.
+     */
+    void reset(ReplacementData* replacementData);
+
+    /**
+     * Find replacement victim using insertion timestamps and second chance
+     * bit.
+     *
+     * @param cands Replacement candidates, selected by indexing policy.
+     * @return Cache block to be replaced.
+     */
+    CacheBlk* getVictim(const ReplacementCandidates& cands) override;
+
+    /**
+     * Instantiate a replacement data entry.
+     *
+     * @return A unique pointer to the new replacement data.
+     */
+    std::unique_ptr<ReplacementData> instantiateEntry() override;
+};
+
+#endif // __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__

--
To view, visit https://gem5-review.googlesource.com/9441
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Id4d52b698d0045a4914a4d848fdf9c3c00a28508
Gerrit-Change-Number: 9441
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to