changeset b02075171b57 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=b02075171b57
description:
mem: Add support for writing back and flushing caches
This patch adds support for the following optional drain methods in
the classical memory system's cache model:
memWriteback() - Write back all dirty cache lines to memory using
functional accesses.
memInvalidate() - Invalidate all cache lines. Dirty cache lines
are lost unless a writeback is requested.
Since memWriteback() is called when checkpointing systems, this patch
adds support for checkpointing systems with caches. The serialization
code now checks whether there are any dirty lines in the cache. If
there are dirty lines in the cache, the checkpoint is flagged as bad
and a warning is printed.
diffstat:
src/mem/cache/base.cc | 3 +-
src/mem/cache/base.hh | 19 +++++++++
src/mem/cache/blk.hh | 71 ++++++++++++++++++++++++++++++++++
src/mem/cache/cache.hh | 22 ++++++++++
src/mem/cache/cache_impl.hh | 90 ++++++++++++++++++++++++++++++++++++++-----
src/mem/cache/mshr_queue.cc | 35 ++++++++++++++++-
src/mem/cache/mshr_queue.hh | 21 +++++++++-
src/mem/cache/tags/base.hh | 12 +++++
src/mem/cache/tags/fa_lru.hh | 32 +++++++++++++++
src/mem/cache/tags/iic.hh | 32 +++++++++++++++
src/mem/cache/tags/lru.hh | 32 +++++++++++++++
11 files changed, 355 insertions(+), 14 deletions(-)
diffs (truncated from 587 to 300 lines):
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/base.cc
--- a/src/mem/cache/base.cc Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/base.cc Fri Nov 02 11:32:02 2012 -0500
@@ -751,7 +751,8 @@
unsigned int
BaseCache::drain(DrainManager *dm)
{
- int count = memSidePort->drain(dm) + cpuSidePort->drain(dm);
+ int count = memSidePort->drain(dm) + cpuSidePort->drain(dm) +
+ mshrQueue.drain(dm) + writeBuffer.drain(dm);
// Set status
if (count != 0) {
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/base.hh
--- a/src/mem/cache/base.hh Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/base.hh Fri Nov 02 11:32:02 2012 -0500
@@ -223,6 +223,25 @@
}
}
+ /**
+ * Write back dirty blocks in the cache using functional accesses.
+ */
+ virtual void memWriteback() = 0;
+ /**
+ * Invalidates all blocks in the cache.
+ *
+ * @warn Dirty cache lines will not be written back to
+ * memory. Make sure to call functionalWriteback() first if you
+ * want the to write them to memory.
+ */
+ virtual void memInvalidate() = 0;
+ /**
+ * Determine if there are any dirty blocks in the cache.
+ *
+ * \return true if at least one block is dirty, false otherwise.
+ */
+ virtual bool isDirty() const = 0;
+
/** Block size of this cache */
const unsigned blkSize;
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/blk.hh
--- a/src/mem/cache/blk.hh Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/blk.hh Fri Nov 02 11:32:02 2012 -0500
@@ -1,4 +1,16 @@
/*
+ * Copyright (c) 2012 ARM Limited
+ * All rights reserved.
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
@@ -26,6 +38,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Erik Hallnor
+ * Andreas Sandberg
*/
/** @file
@@ -297,6 +310,64 @@
const std::string &prefix = "") const;
};
+/**
+ * Wrap a method and present it as a cache block visitor.
+ *
+ * For example the forEachBlk method in the tag arrays expects a
+ * callable object/function as their parameter. This class wraps a
+ * method in an object and presents callable object that adheres to
+ * the cache block visitor protocol.
+ */
+template <typename T, typename BlkType>
+class CacheBlkVisitorWrapper
+{
+ public:
+ typedef bool (T::*visitorPtr)(BlkType &blk);
+ CacheBlkVisitorWrapper(T &_obj, visitorPtr _visitor)
+ : obj(_obj), visitor(_visitor) {}
+
+ bool operator()(BlkType &blk) {
+ return (obj.*visitor)(blk);
+ }
+
+ private:
+ T &obj;
+ visitorPtr visitor;
+};
+
+/**
+ * Cache block visitor that determines if there are dirty blocks in a
+ * cache.
+ *
+ * Use with the forEachBlk method in the tag array to determine if the
+ * array contains dirty blocks.
+ */
+template <typename BlkType>
+class CacheBlkIsDirtyVisitor
+{
+ public:
+ CacheBlkIsDirtyVisitor()
+ : _isDirty(false) {}
+
+ bool operator()(BlkType &blk) {
+ if (blk.isDirty()) {
+ _isDirty = true;
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Does the array contain a dirty line?
+ *
+ * \return true if yes, false otherwise.
+ */
+ bool isDirty() const { return _isDirty; };
+
+ private:
+ bool _isDirty;
+};
#endif //__CACHE_BLK_HH__
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/cache.hh
--- a/src/mem/cache/cache.hh Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/cache.hh Fri Nov 02 11:32:02 2012 -0500
@@ -76,6 +76,7 @@
typedef typename TagStore::BlkList BlkList;
protected:
+ typedef CacheBlkVisitorWrapper<Cache<TagStore>, BlkType> WrappedBlkVisitor;
/**
* The CPU-side port extends the base cache slave port with access
@@ -256,6 +257,27 @@
*/
PacketPtr writebackBlk(BlkType *blk);
+
+ void memWriteback();
+ void memInvalidate();
+ bool isDirty() const;
+
+ /**
+ * Cache block visitor that writes back dirty cache blocks using
+ * functional writes.
+ *
+ * \return Always returns true.
+ */
+ bool writebackVisitor(BlkType &blk);
+ /**
+ * Cache block visitor that invalidates all blocks in the cache.
+ *
+ * @warn Dirty cache lines will not be written back to memory.
+ *
+ * \return Always returns true.
+ */
+ bool invalidateVisitor(BlkType &blk);
+
public:
/** Instantiates a basic cache object. */
Cache(const Params *p, TagStore *tags);
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/cache_impl.hh
--- a/src/mem/cache/cache_impl.hh Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/cache_impl.hh Fri Nov 02 11:32:02 2012 -0500
@@ -43,6 +43,7 @@
* Nathan Binkert
* Steve Reinhardt
* Ron Dreslinski
+ * Andreas Sandberg
*/
/**
@@ -1035,6 +1036,69 @@
return writeback;
}
+template<class TagStore>
+void
+Cache<TagStore>::memWriteback()
+{
+ WrappedBlkVisitor visitor(*this, &Cache<TagStore>::writebackVisitor);
+ tags->forEachBlk(visitor);
+}
+
+template<class TagStore>
+void
+Cache<TagStore>::memInvalidate()
+{
+ WrappedBlkVisitor visitor(*this, &Cache<TagStore>::invalidateVisitor);
+ tags->forEachBlk(visitor);
+}
+
+template<class TagStore>
+bool
+Cache<TagStore>::isDirty() const
+{
+ CacheBlkIsDirtyVisitor<BlkType> visitor;
+ tags->forEachBlk(visitor);
+
+ return visitor.isDirty();
+}
+
+template<class TagStore>
+bool
+Cache<TagStore>::writebackVisitor(BlkType &blk)
+{
+ if (blk.isDirty()) {
+ assert(blk.isValid());
+
+ Request request(tags->regenerateBlkAddr(blk.tag, blk.set),
+ blkSize, 0, Request::funcMasterId);
+
+ Packet packet(&request, MemCmd::WriteReq);
+ packet.dataStatic(blk.data);
+
+ memSidePort->sendFunctional(&packet);
+
+ blk.status &= ~BlkDirty;
+ }
+
+ return true;
+}
+
+template<class TagStore>
+bool
+Cache<TagStore>::invalidateVisitor(BlkType &blk)
+{
+
+ if (blk.isDirty())
+ warn_once("Invalidating dirty cache lines. Expect things to break.\n");
+
+ if (blk.isValid()) {
+ assert(!blk.isDirty());
+ tags->invalidate(dynamic_cast< BlkType *>(&blk));
+ blk.invalidate();
+ }
+
+ return true;
+}
template<class TagStore>
typename Cache<TagStore>::BlkType*
@@ -1565,16 +1629,20 @@
void
Cache<TagStore>::serialize(std::ostream &os)
{
- warn("*** Creating checkpoints with caches is not supported. ***\n");
- warn(" Remove any caches before taking checkpoints\n");
- warn(" This checkpoint will not restore correctly and dirty data in "
- "the cache will be lost!\n");
+ bool dirty(isDirty());
- // Since we don't write back the data dirty in the caches to the physical
- // memory if caches exist in the system we won't be able to restore
- // from the checkpoint as any data dirty in the caches will be lost.
+ if (dirty) {
+ warn("*** The cache still contains dirty data. ***\n");
+ warn(" Make sure to drain the system using the correct flags.\n");
+ warn(" This checkpoint will not restore correctly and dirty data in
"
+ "the cache will be lost!\n");
+ }
- bool bad_checkpoint = true;
+ // Since we don't checkpoint the data in the cache, any dirty data
+ // will be lost when restoring from a checkpoint of a system that
+ // wasn't drained properly. Flag the checkpoint as invalid if the
+ // cache contains dirty data.
+ bool bad_checkpoint(dirty);
SERIALIZE_SCALAR(bad_checkpoint);
}
@@ -1585,9 +1653,9 @@
bool bad_checkpoint;
UNSERIALIZE_SCALAR(bad_checkpoint);
if (bad_checkpoint) {
- fatal("Restoring from checkpoints with caches is not supported in the "
- "classic memory system. Please remove any caches before taking "
- "checkpoints.\n");
+ fatal("Restoring from checkpoints with dirty caches is not supported "
+ "in the classic memory system. Please remove any caches or "
+ " drain them properly before taking checkpoints.\n");
}
}
diff -r 7a9b5e0335a6 -r b02075171b57 src/mem/cache/mshr_queue.cc
--- a/src/mem/cache/mshr_queue.cc Fri Nov 02 11:32:02 2012 -0500
+++ b/src/mem/cache/mshr_queue.cc Fri Nov 02 11:32:02 2012 -0500
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev