sim: Move serialization logic for MemPools out of System.

And move it into the MemPools class itself. The MemPools class should be
self contained, and be able to manage its own state. That should not be
the responsibility of another containing class.

Change-Id: Ib0bf70b57e92698f15bea0cc217dd98ee816d57b
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/50340
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Maintainer: Giacomo Travaglini <giacomo.travaglini@arm.com>
This commit is contained in:
Gabe Black
2021-09-11 06:06:28 -07:00
parent 0a2ba189d4
commit 61a6ec7b71
4 changed files with 81 additions and 22 deletions

View File

@@ -124,4 +124,22 @@ MemPool::allocate(Addr npages)
return return_addr;
}
void
MemPool::serialize(CheckpointOut &cp) const
{
paramOut(cp, "page_shift", pageShift);
paramOut(cp, "start_page", startPageNum);
paramOut(cp, "free_page_num", freePageNum);
paramOut(cp, "total_pages", _totalPages);
}
void
MemPool::unserialize(CheckpointIn &cp)
{
paramIn(cp, "page_shift", pageShift);
paramIn(cp, "start_page", startPageNum);
paramIn(cp, "free_page_num", freePageNum);
paramIn(cp, "total_pages", _totalPages);
}
} // namespace gem5

View File

@@ -35,6 +35,7 @@
#define __MEM_POOL_HH__
#include "base/types.hh"
#include "sim/serialize.hh"
namespace gem5
{
@@ -42,21 +43,23 @@ namespace gem5
class System;
/** Class for handling allocation of physical pages in SE mode. */
class MemPool
class MemPool : public Serializable
{
private:
Addr pageShift;
Addr pageShift = 0;
/** Start page of pool. */
Counter startPageNum;
Counter startPageNum = 0;
/** Page number of free memory. */
Counter freePageNum;
Counter freePageNum = 0;
/** The size of the pool, in number of pages. */
Counter _totalPages;
Counter _totalPages = 0;
public:
MemPool() {}
MemPool(Addr page_shift, Addr ptr, Addr limit);
Counter startPage() const;
@@ -74,6 +77,9 @@ class MemPool
Addr totalBytes() const;
Addr allocate(Addr npages);
void serialize(CheckpointOut &cp) const override;
void unserialize(CheckpointIn &cp) override;
};
} // namespace gem5

View File

@@ -44,6 +44,7 @@
#include <algorithm>
#include "base/compiler.hh"
#include "base/cprintf.hh"
#include "base/loader/object_file.hh"
#include "base/loader/symtab.hh"
#include "base/str.hh"
@@ -68,6 +69,7 @@
#include "sim/debug.hh"
#include "sim/full_system.hh"
#include "sim/redirect_path.hh"
#include "sim/serialize_handlers.hh"
namespace gem5
{
@@ -430,16 +432,11 @@ System::serialize(CheckpointOut &cp) const
paramOut(cp, csprintf("quiesceEndTick_%d", id), when);
}
std::vector<Addr> ptrs;
std::vector<Addr> limits;
int num_mem_pools = memPools.size();
SERIALIZE_SCALAR(num_mem_pools);
for (const auto& memPool : memPools) {
ptrs.push_back(memPool.freePageAddr());
limits.push_back(memPool.totalBytes() + memPool.startAddr());
}
SERIALIZE_CONTAINER(ptrs);
SERIALIZE_CONTAINER(limits);
for (int i = 0; i < num_mem_pools; i++)
memPools[i].serializeSection(cp, csprintf("memPool%d", i));
// also serialize the memories in the system
physmem.serializeSection(cp, "physmem");
@@ -461,15 +458,14 @@ System::unserialize(CheckpointIn &cp)
# endif
}
std::vector<Addr> ptrs;
std::vector<Addr> limits;
int num_mem_pools = 0;
UNSERIALIZE_SCALAR(num_mem_pools);
UNSERIALIZE_CONTAINER(ptrs);
UNSERIALIZE_CONTAINER(limits);
assert(ptrs.size() == limits.size());
for (size_t i = 0; i < ptrs.size(); i++)
memPools.emplace_back(getPageShift(), ptrs[i], limits[i]);
for (int i = 0; i < num_mem_pools; i++) {
MemPool pool;
pool.unserializeSection(cp, csprintf("memPool%d", i));
memPools.push_back(pool);
}
// also unserialize the memories in the system
physmem.unserializeSection(cp, "physmem");

View File

@@ -0,0 +1,39 @@
# This upgrader moves memory pool attributes from a vector of pointers to the
# next free page and a vector of the limit of each pool to sections which each
# represent one pool and which hold the page shift amount, the next free page,
# and the total pages in the pool.
def upgrader(cpt):
systems = {}
for sec in cpt.sections():
ptrs = cpt.get(sec, 'ptrs', fallback=None)
limits = cpt.get(sec, 'limits', fallback=None)
if ptrs and limits:
systems[sec] = ptrs, limits
for sec, (ptrs, limits) in systems.items():
ptrs = list(map(int, ptrs.split()))
limits = list(map(int, limits.split()))
if len(ptrs) != len(limits):
print(
f"'{sec}ptrs' and '{limits}limits' were not the same length!")
cpt.set(sec, 'num_mem_pools', str(len(ptrs)))
cpt.remove_option(sec, 'ptrs')
cpt.remove_option(sec, 'limits')
# Assume the page shift is 12, for a 4KiB page.
page_shift = 12
for idx, (ptr, limit) in enumerate(zip(ptrs, limits)):
new_sec = f'{sec}.memPool{idx}'
cpt.add_section(new_sec)
cpt.set(new_sec, 'page_shift', str(page_shift))
# Since there's no way to tell where the pool actually started,
# just assume it started wherever it is right now.
cpt.set(new_sec, 'start_page', str(ptr >> page_shift))
cpt.set(new_sec, 'free_page_num', str(ptr >> page_shift))
cpt.set(new_sec, 'total_pages', str((limit - ptr) >> page_shift))