gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
mem_pool.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "sim/mem_pool.hh"
33
34#include "base/addr_range.hh"
35#include "base/logging.hh"
36
37namespace gem5
38{
39
41 : pageShift(page_shift), startPageNum(ptr >> page_shift),
42 _totalPages((limit - ptr) >> page_shift),
44{
45}
46
49{
50 return startPageNum;
51}
52
55{
56 return _totalPages;
57}
58
61{
62 return totalPages() - freePages();
63}
64
67{
68 return freePhysPages.size();
69}
70
71Addr
73{
74 return startPage() << pageShift;
75}
76
77Addr
79{
80 return allocatedPages() << pageShift;
81}
82
83Addr
85{
86 return freePages() << pageShift;
87}
88
89Addr
91{
92 return totalPages() << pageShift;
93}
94
95Addr
97{
98 const std::optional<Addr> page = freePhysPages.allocate(npages);
99 if (!page)
100 fatal("Out of memory, please increase size of physical memory.");
101 return *page << pageShift;
102}
103
104void
105MemPool::deallocate(Addr dealloc_start, Addr dealloc_npages)
106{
107 freePhysPages.insert(dealloc_start >> pageShift, dealloc_npages);
108}
109
110void
112{
113 paramOut(cp, "page_shift", pageShift);
114 paramOut(cp, "start_page", startPageNum);
115 paramOut(cp, "total_pages", _totalPages);
116
117 ScopedCheckpointSection sec(cp, "free_list");
118 paramOut(cp, "size", freePhysPages.ranges().size());
119 int i = 0;
120 for (const auto& range : freePhysPages.ranges()) {
121 ScopedCheckpointSection sec(cp, csprintf("free%d", i++));
122 paramOut(cp, "base", range.base);
123 paramOut(cp, "size", range.size);
124 }
125}
126
127void
129{
130 paramIn(cp, "page_shift", pageShift);
131 paramIn(cp, "start_page", startPageNum);
132 paramIn(cp, "total_pages", _totalPages);
133
134 Counter free_page_num;
135 if (optParamIn(cp, "free_page_num", free_page_num)) {
136 // Handle checkpoints generated by legacy page allocator
137 // for backwards compatibility.
138 warn("Checkpoint contains legacy MemPool state (free_page_num)\n");
139 freePhysPages.insert(free_page_num, _totalPages - free_page_num);
140 } else {
141 ScopedCheckpointSection sec(cp, "free_list");
142 int n = 0;
143 optParamIn(cp, "size", n);
144 for (int i = 0; i < n; ++i) {
145 ScopedCheckpointSection sec(cp, csprintf("free%d", i));
146 Addr base;
147 Addr size;
148 paramIn(cp, "base", base);
149 paramIn(cp, "size", size);
150 freePhysPages.insert(base, size);
151 }
152 }
153}
154
155void
157{
158 for (const auto &mem : memories)
159 pools.emplace_back(pageShift, mem.start(), mem.end());
160}
161
162Addr
163MemPools::allocPhysPages(int npages, int pool_id)
164{
165 return pools[pool_id].allocate(npages);
166}
167
168void
169MemPools::deallocPhysPages(Addr page_addr, int npages, int pool_id)
170{
171 pools[pool_id].deallocate(page_addr, npages);
172}
173
174Addr
175MemPools::memSize(int pool_id) const
176{
177 return pools[pool_id].totalBytes();
178}
179
180Addr
181MemPools::freeMemSize(int pool_id) const
182{
183 return pools[pool_id].freeBytes();
184}
185
186void
188{
189 ScopedCheckpointSection sec(cp, "mempools");
190 int num_pools = pools.size();
191 SERIALIZE_SCALAR(num_pools);
192
193 for (int i = 0; i < num_pools; i++)
194 pools[i].serializeSection(cp, csprintf("pool%d", i));
195}
196
197void
199{
200 // Delete previous mem_pools
201 pools.clear();
202
203 ScopedCheckpointSection sec(cp, "mempools");
204 int num_pools = 0;
205 UNSERIALIZE_SCALAR(num_pools);
206
207 for (int i = 0; i < num_pools; i++) {
208 MemPool pool;
209 pool.unserializeSection(cp, csprintf("pool%d", i));
210 pools.push_back(pool);
211 }
212}
213
214} // namespace gem5
Class for handling allocation of physical pages in SE mode.
Definition mem_pool.hh:47
Addr totalBytes() const
Definition mem_pool.cc:90
Addr startAddr() const
Definition mem_pool.cc:72
FreeList< Addr > freePhysPages
Definition mem_pool.hh:57
Counter totalPages() const
Definition mem_pool.cc:54
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition mem_pool.cc:111
Counter _totalPages
The size of the pool, in number of pages.
Definition mem_pool.hh:55
Counter allocatedPages() const
Definition mem_pool.cc:60
void deallocate(Addr start, Addr npages)
Definition mem_pool.cc:105
MemPool()=default
Counter startPageNum
Start page of pool.
Definition mem_pool.hh:52
Addr allocatedBytes() const
Definition mem_pool.cc:78
Counter startPage() const
Definition mem_pool.cc:48
Addr allocate(Addr npages)
Definition mem_pool.cc:96
Addr freeBytes() const
Definition mem_pool.cc:84
Counter freePages() const
Definition mem_pool.cc:66
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition mem_pool.cc:128
Addr allocPhysPages(int npages, int pool_id=0)
Allocate npages contiguous unused physical pages.
Definition mem_pool.cc:163
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition mem_pool.cc:187
void populate(const AddrRangeList &memories)
Definition mem_pool.cc:156
Addr freeMemSize(int pool_id=0) const
Amount of physical memory that is still free in a pool.
Definition mem_pool.cc:181
std::vector< MemPool > pools
Definition mem_pool.hh:89
Addr memSize(int pool_id=0) const
Amount of physical memory that exists in a pool.
Definition mem_pool.cc:175
void deallocPhysPages(Addr page_addr, int npages, int pool_id=0)
Deallocate physical pages.
Definition mem_pool.cc:169
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition mem_pool.cc:198
std::list< AddrRange > AddrRangeList
Convenience typedef for a collection of address ranges.
Definition addr_range.hh:64
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:232
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool do_warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition serialize.hh:357
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition serialize.cc:74
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition serialize.cc:81
#define warn(...)
Definition logging.hh:288
Bitfield< 31 > n
Bitfield< 7 > i
Definition misc_types.hh:67
BitfieldType< SegDescriptorLimit > limit
Definition misc.hh:959
double Counter
All counters are of 64-bit values.
Definition types.hh:46
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::ostream CheckpointOut
Definition serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition types.cc:40
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition types.cc:72
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568
bool_vector8 mem[]
Definition reset_stim.h:43

Generated on Mon May 26 2025 09:19:13 for gem5 by doxygen 1.13.2