gem5  v22.1.0.0
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 
37 namespace gem5
38 {
39 
40 MemPool::MemPool(Addr page_shift, Addr ptr, Addr limit)
41  : pageShift(page_shift), startPageNum(ptr >> page_shift),
42  freePageNum(ptr >> page_shift),
43  _totalPages((limit - ptr) >> page_shift)
44 {
46 }
47 
48 Counter
50 {
51  return startPageNum;
52 }
53 
54 Counter
56 {
57  return freePageNum;
58 }
59 
60 void
62 {
63  freePageNum = value;
64 }
65 
66 Addr
68 {
69  return freePageNum << pageShift;
70 }
71 
72 Counter
74 {
75  return _totalPages;
76 }
77 
78 Counter
80 {
81  return freePageNum - startPageNum;
82 }
83 
84 Counter
86 {
87  return _totalPages - allocatedPages();
88 }
89 
90 Addr
92 {
93  return startPage() << pageShift;
94 }
95 
96 Addr
98 {
99  return allocatedPages() << pageShift;
100 }
101 
102 Addr
104 {
105  return freePages() << pageShift;
106 }
107 
108 Addr
110 {
111  return totalPages() << pageShift;
112 }
113 
114 Addr
116 {
117  Addr return_addr = freePageAddr();
118  freePageNum += npages;
119 
120  fatal_if(freePages() <= 0,
121  "Out of memory, please increase size of physical memory.");
122 
123  return return_addr;
124 }
125 
126 void
128 {
129  paramOut(cp, "page_shift", pageShift);
130  paramOut(cp, "start_page", startPageNum);
131  paramOut(cp, "free_page_num", freePageNum);
132  paramOut(cp, "total_pages", _totalPages);
133 }
134 
135 void
137 {
138  paramIn(cp, "page_shift", pageShift);
139  paramIn(cp, "start_page", startPageNum);
140  paramIn(cp, "free_page_num", freePageNum);
141  paramIn(cp, "total_pages", _totalPages);
142 }
143 
144 void
146 {
147  for (const auto &mem : memories)
148  pools.emplace_back(pageShift, mem.start(), mem.end());
149 }
150 
151 Addr
152 MemPools::allocPhysPages(int npages, int pool_id)
153 {
154  return pools[pool_id].allocate(npages);
155 }
156 
157 Addr
158 MemPools::memSize(int pool_id) const
159 {
160  return pools[pool_id].totalBytes();
161 }
162 
163 Addr
164 MemPools::freeMemSize(int pool_id) const
165 {
166  return pools[pool_id].freeBytes();
167 }
168 
169 void
171 {
172  ScopedCheckpointSection sec(cp, "mempools");
173  int num_pools = pools.size();
174  SERIALIZE_SCALAR(num_pools);
175 
176  for (int i = 0; i < num_pools; i++)
177  pools[i].serializeSection(cp, csprintf("pool%d", i));
178 }
179 
180 void
182 {
183  // Delete previous mem_pools
184  pools.clear();
185 
186  ScopedCheckpointSection sec(cp, "mempools");
187  int num_pools = 0;
188  UNSERIALIZE_SCALAR(num_pools);
189 
190  for (int i = 0; i < num_pools; i++) {
191  MemPool pool;
192  pool.unserializeSection(cp, csprintf("pool%d", i));
193  pools.push_back(pool);
194  }
195 }
196 
197 } // namespace gem5
Class for handling allocation of physical pages in SE mode.
Definition: mem_pool.hh:46
Addr totalBytes() const
Definition: mem_pool.cc:109
Addr startAddr() const
Definition: mem_pool.cc:91
void setFreePage(Counter value)
Definition: mem_pool.cc:61
Counter totalPages() const
Definition: mem_pool.cc:73
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: mem_pool.cc:127
Addr freePageAddr() const
Definition: mem_pool.cc:67
Counter _totalPages
The size of the pool, in number of pages.
Definition: mem_pool.hh:57
Counter allocatedPages() const
Definition: mem_pool.cc:79
Addr pageShift
Definition: mem_pool.hh:48
Counter startPageNum
Start page of pool.
Definition: mem_pool.hh:51
Addr allocatedBytes() const
Definition: mem_pool.cc:97
Counter startPage() const
Definition: mem_pool.cc:49
Addr allocate(Addr npages)
Definition: mem_pool.cc:115
Addr freeBytes() const
Definition: mem_pool.cc:103
Counter freePage() const
Definition: mem_pool.cc:55
Counter freePages() const
Definition: mem_pool.cc:85
Counter freePageNum
Page number of free memory.
Definition: mem_pool.hh:54
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: mem_pool.cc:136
Addr allocPhysPages(int npages, int pool_id=0)
Allocate npages contiguous unused physical pages.
Definition: mem_pool.cc:152
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: mem_pool.cc:170
void populate(const AddrRangeList &memories)
Definition: mem_pool.cc:145
Addr freeMemSize(int pool_id=0) const
Amount of physical memory that is still free in a pool.
Definition: mem_pool.cc:164
Addr pageShift
Definition: mem_pool.hh:89
std::vector< MemPool > pools
Definition: mem_pool.hh:91
Addr memSize(int pool_id=0) const
Amount of physical memory that exists in a pool.
Definition: mem_pool.cc:158
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: mem_pool.cc:181
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
#define gem5_assert(cond,...)
The assert macro will function like a normal assert, but will use panic instead of straight abort().
Definition: logging.hh:318
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
Bitfield< 7 > i
Definition: misc_types.hh:67
BitfieldType< SegDescriptorLimit > limit
Definition: misc.hh:931
double Counter
All counters are of 64-bit values.
Definition: types.hh:47
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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 Wed Dec 21 2022 10:22:39 for gem5 by doxygen 1.9.1