From 0a2ba189d4e17aea6390cb17fdb1724a66fecb82 Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Mon, 20 Sep 2021 15:02:56 -0700 Subject: [PATCH] sim: Fix a faulty assumption in MemPool. In the MemPool object, the idea of a limit of the pool (largest page) and the total number of pages were conflated, as was the page number of the next "free" page and the total number of pages allocated. Both of those would only be equivalent if the memory pool starts at address zero, which is not generally true and could be true for at most one pool at a time even when it is occasionally true. Instead, this change fixes up MemPool to keep tree values, a starting page number, the page number of the next free page, and the total number of pages in the pool, both allocated and unallocated. With those three values, we can accurately report the number of allocated pages (not just the number of pages of any kind below the next free one), the total number of free pages, and the total number of pages in general (not the largest numbered page in the pool). The value serialized by the System class was adjusted so that it will stay compatible with previous checkpoints. The value unserialized by the system class is passed to the MemPool as a limit, which has not changed and so doesn't need to be updated. It gets translated into the total number of pages in the MemPool constructor. Change-Id: I8268ef410b41bf757df9ee5585ec2f6b0d8499e1 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/50687 Reviewed-by: Giacomo Travaglini Maintainer: Giacomo Travaglini Tested-by: kokoro --- src/sim/mem_pool.cc | 22 ++++++++++++++++++---- src/sim/mem_pool.hh | 7 ++++++- src/sim/system.cc | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/sim/mem_pool.cc b/src/sim/mem_pool.cc index f568e28a4a..bde3e67b91 100644 --- a/src/sim/mem_pool.cc +++ b/src/sim/mem_pool.cc @@ -39,9 +39,17 @@ namespace gem5 { MemPool::MemPool(Addr page_shift, Addr ptr, Addr limit) - : pageShift(page_shift), freePageNum(ptr >> page_shift), - _totalPages(limit >> page_shift) + : pageShift(page_shift), startPageNum(ptr >> page_shift), + freePageNum(ptr >> page_shift), + _totalPages((limit - ptr) >> page_shift) { + gem5_assert(_totalPages > 0); +} + +Counter +MemPool::startPage() const +{ + return startPageNum; } Counter @@ -71,13 +79,19 @@ MemPool::totalPages() const Counter MemPool::allocatedPages() const { - return freePageNum; + return freePageNum - startPageNum; } Counter MemPool::freePages() const { - return _totalPages - freePageNum; + return _totalPages - allocatedPages(); +} + +Addr +MemPool::startAddr() const +{ + return startPage() << pageShift; } Addr diff --git a/src/sim/mem_pool.hh b/src/sim/mem_pool.hh index 49523f4e10..3cba3b6987 100644 --- a/src/sim/mem_pool.hh +++ b/src/sim/mem_pool.hh @@ -47,7 +47,10 @@ class MemPool private: Addr pageShift; - /** Page number to free memory. */ + /** Start page of pool. */ + Counter startPageNum; + + /** Page number of free memory. */ Counter freePageNum; /** The size of the pool, in number of pages. */ @@ -56,6 +59,7 @@ class MemPool public: MemPool(Addr page_shift, Addr ptr, Addr limit); + Counter startPage() const; Counter freePage() const; void setFreePage(Counter value); Addr freePageAddr() const; @@ -64,6 +68,7 @@ class MemPool Counter allocatedPages() const; Counter freePages() const; + Addr startAddr() const; Addr allocatedBytes() const; Addr freeBytes() const; Addr totalBytes() const; diff --git a/src/sim/system.cc b/src/sim/system.cc index 38952762b1..225262a4ab 100644 --- a/src/sim/system.cc +++ b/src/sim/system.cc @@ -435,7 +435,7 @@ System::serialize(CheckpointOut &cp) const for (const auto& memPool : memPools) { ptrs.push_back(memPool.freePageAddr()); - limits.push_back(memPool.totalBytes()); + limits.push_back(memPool.totalBytes() + memPool.startAddr()); } SERIALIZE_CONTAINER(ptrs);