gem5  v22.1.0.0
core.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 2019, 2021 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2010 Advanced Micro Devices, Inc.
15  * Copyright (c) 2006 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "pybind11/operators.h"
43 #include "pybind11/pybind11.h"
44 #include "pybind11/stl.h"
45 
46 #include <ctime>
47 
48 #include "base/addr_range.hh"
49 #include "base/inet.hh"
51 #include "base/logging.hh"
52 #include "base/random.hh"
53 #include "base/socket.hh"
54 #include "base/temperature.hh"
55 #include "base/types.hh"
56 #include "sim/core.hh"
57 #include "sim/cur_tick.hh"
58 #include "sim/drain.hh"
59 #include "sim/serialize.hh"
60 #include "sim/sim_object.hh"
61 
62 namespace py = pybind11;
63 
64 namespace gem5
65 {
66 
69 {
70  SimObject *resolveSimObject(const std::string &name);
71 };
72 
74 
75 SimObject *
77 {
78  // TODO
79  py::module_ m = py::module_::import("m5.SimObject");
80  auto f = m.attr("resolveSimObject");
81 
82  return f(name).cast<SimObject *>();
83 }
84 
85 extern const char *compileDate;
86 extern const char *gem5Version;
87 
88 static void
89 init_drain(py::module_ &m_native)
90 {
91  py::module_ m = m_native.def_submodule("drain");
92 
93  py::enum_<DrainState>(m, "DrainState")
94  .value("Running", DrainState::Running)
95  .value("Draining", DrainState::Draining)
96  .value("Drained", DrainState::Drained)
97  ;
98 
99  py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
100  m, "Drainable")
101  .def("drainState", &Drainable::drainState)
102  .def("notifyFork", &Drainable::notifyFork)
103  ;
104 
105  // The drain manager is a singleton with a private
106  // destructor. Disable deallocation from the Python binding.
107  py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
108  m, "DrainManager")
109  .def("tryDrain", &DrainManager::tryDrain)
110  .def("resume", &DrainManager::resume)
111  .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
112  .def("isDrained", &DrainManager::isDrained)
113  .def("state", &DrainManager::state)
114  .def("signalDrainDone", &DrainManager::signalDrainDone)
115  .def_static("instance", &DrainManager::instance,
116  py::return_value_policy::reference)
117  ;
118 }
119 
120 static void
121 init_serialize(py::module_ &m_native)
122 {
123  py::module_ m = m_native.def_submodule("serialize");
124 
125  py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
126  m, "Serializable")
127  ;
128 
129  py::class_<CheckpointIn>(m, "CheckpointIn")
130  ;
131 }
132 
133 static void
134 init_range(py::module_ &m_native)
135 {
136  py::module_ m = m_native.def_submodule("range");
137 
138  py::class_<AddrRange>(m, "AddrRange")
139  .def(py::init<>())
140  .def(py::init<Addr &, Addr &>())
141  .def(py::init<Addr, Addr, const std::vector<Addr> &, uint8_t>())
142  .def(py::init<const std::vector<AddrRange> &>())
143  .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
144 
145  .def("__str__", &AddrRange::to_string)
146 
147  .def("interleaved", &AddrRange::interleaved)
148  .def("granularity", &AddrRange::granularity)
149  .def("stripes", &AddrRange::stripes)
150  .def("size", &AddrRange::size)
151  .def("valid", &AddrRange::valid)
152  .def("start", &AddrRange::start)
153  .def("end", &AddrRange::end)
154  .def("mergesWith", &AddrRange::mergesWith)
155  .def("intersects", &AddrRange::intersects)
156  .def("isSubset", &AddrRange::isSubset)
157  .def("exclude", static_cast<AddrRangeList (AddrRange::*)(
158  const AddrRangeList &) const>(&AddrRange::exclude))
159  ;
160 
161  m.def("RangeEx", &RangeEx);
162  m.def("RangeIn", &RangeIn);
163  m.def("RangeSize", &RangeSize);
164 }
165 
166 static void
167 init_net(py::module_ &m_native)
168 {
169  py::module_ m = m_native.def_submodule("net");
170 
171  py::class_<networking::EthAddr>(m, "EthAddr")
172  .def(py::init<>())
173  .def(py::init<const std::string &>())
174  ;
175 
176  py::class_<networking::IpAddress>(m, "IpAddress")
177  .def(py::init<>())
178  .def(py::init<uint32_t>())
179  ;
180 
181  py::class_<networking::IpNetmask, networking::IpAddress>(m, "IpNetmask")
182  .def(py::init<>())
183  .def(py::init<uint32_t, uint8_t>())
184  ;
185 
186  py::class_<networking::IpWithPort, networking::IpAddress>(m, "IpWithPort")
187  .def(py::init<>())
188  .def(py::init<uint32_t, uint16_t>())
189  ;
190 }
191 
192 static void
193 init_loader(py::module_ &m_native)
194 {
195  py::module_ m = m_native.def_submodule("loader");
196 
197  m.def("setInterpDir", &loader::setInterpDir);
198 }
199 
200 void
201 pybind_init_core(py::module_ &m_native)
202 {
203  py::module_ m_core = m_native.def_submodule("core");
204 
205  py::class_<Cycles>(m_core, "Cycles")
206  .def(py::init<>())
207  .def(py::init<uint64_t>())
208  .def("__int__", &Cycles::operator uint64_t)
209  .def("__add__", &Cycles::operator+)
210  .def("__sub__", &Cycles::operator-)
211  ;
212 
213  py::class_<Temperature>(m_core, "Temperature")
214  .def(py::init<>())
215  .def(py::init<double>())
216  .def_static("from_celsius", &Temperature::fromCelsius)
217  .def_static("from_kelvin", &Temperature::fromKelvin)
218  .def_static("from_fahrenheit", &Temperature::fromFahrenheit)
219  .def("celsius", &Temperature::toCelsius)
220  .def("kelvin", &Temperature::toKelvin)
221  .def("fahrenheit", &Temperature::toFahrenheit)
222  .def(py::self == py::self)
223  .def(py::self != py::self)
224  .def(py::self < py::self)
225  .def(py::self <= py::self)
226  .def(py::self > py::self)
227  .def(py::self >= py::self)
228  .def(py::self + py::self)
229  .def(py::self - py::self)
230  .def(py::self * float())
231  .def(float() * py::self)
232  .def(py::self / float())
233  .def("__str__", [](const Temperature &t) {
234  std::stringstream s;
235  s << t;
236  return s.str();
237  })
238  .def("__repr__", [](const Temperature &t) {
239  std::stringstream s;
240  s << "Temperature(" << t.toKelvin() << ")";
241  return s.str();
242  })
243  ;
244 
245  py::class_<tm>(m_core, "tm")
246  .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
247  .def_readwrite("tm_sec", &tm::tm_sec)
248  .def_readwrite("tm_min", &tm::tm_min)
249  .def_readwrite("tm_hour", &tm::tm_hour)
250  .def_readwrite("tm_mday", &tm::tm_mday)
251  .def_readwrite("tm_mon", &tm::tm_mon)
252  .def_readwrite("tm_wday", &tm::tm_wday)
253  .def_readwrite("tm_yday", &tm::tm_yday)
254  .def_readwrite("tm_isdst", &tm::tm_isdst)
255  ;
256 
257  py::enum_<Logger::LogLevel>(m_core, "LogLevel")
258  .value("PANIC", Logger::PANIC)
259  .value("FATAL", Logger::FATAL)
260  .value("WARN", Logger::WARN)
261  .value("INFO", Logger::INFO)
262  .value("HACK", Logger::HACK)
263  ;
264 
265  m_core
266  .def("setLogLevel", &Logger::setLevel)
267  .def("setOutputDir", &setOutputDir)
268  .def("doExitCleanup", &doExitCleanup)
269 
270  .def("disableAllListeners", &ListenSocket::disableAll)
271  .def("listenersDisabled", &ListenSocket::allDisabled)
272  .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
273  .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
274 
275 
276  .def("fixClockFrequency", &fixClockFrequency)
277  .def("clockFrequencyFixed", &clockFrequencyFixed)
278 
279  .def("setClockFrequency", &setClockFrequency)
280  .def("getClockFrequency", &getClockFrequency)
281  .def("curTick", curTick)
282  ;
283 
284  /* TODO: These should be read-only */
285  m_core.attr("compileDate") = py::cast(compileDate);
286  m_core.attr("gem5Version") = py::cast(gem5Version);
287 
288  m_core.attr("TRACING_ON") = py::cast(TRACING_ON);
289 
290  m_core.attr("MaxTick") = py::cast(MaxTick);
291 
292  /*
293  * Serialization helpers
294  */
295  m_core
296  .def("serializeAll", &SimObject::serializeAll)
297  .def("getCheckpoint", [](const std::string &cpt_dir) {
299  return new CheckpointIn(cpt_dir);
300  })
301 
302  ;
303 
304 
305  init_drain(m_native);
306  init_serialize(m_native);
307  init_range(m_native);
308  init_net(m_native);
309  init_loader(m_native);
310 }
311 
312 } // namespace gem5
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:82
static DrainManager & instance()
Get the singleton DrainManager instance.
Definition: drain.hh:91
static void loopbackOnly()
Definition: socket.cc:77
static bool allDisabled()
Definition: socket.cc:71
static void disableAll()
Definition: socket.cc:63
static void setLevel(LogLevel ll)
Definition: logging.hh:75
Resolve a SimObject name using the Pybind configuration.
Definition: core.cc:69
SimObject * resolveSimObject(const std::string &name)
Find a SimObject given a full path name.
Definition: core.cc:76
void init(uint32_t s)
Definition: random.cc:67
Base class to wrap object resolving functionality.
Definition: sim_object.hh:386
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
static void serializeAll(const std::string &cpt_dir)
Create a checkpoint by serializing all SimObjects in the system.
Definition: sim_object.cc:135
static void setSimObjectResolver(SimObjectResolver *resolver)
There is a single object name resolver, and it is only set when simulation is restoring from checkpoi...
Definition: sim_object.cc:191
The class stores temperatures in Kelvin and provides helper methods to convert to/from Celsius.
Definition: temperature.hh:51
double toFahrenheit() const
Definition: temperature.cc:62
static Temperature fromCelsius(double _value)
Definition: temperature.cc:50
constexpr double toKelvin() const
Definition: temperature.hh:68
constexpr double toCelsius() const
Definition: temperature.hh:69
static Temperature fromFahrenheit(double _value)
Definition: temperature.cc:56
static Temperature fromKelvin(double _value)
Definition: temperature.cc:44
bool interleaved() const
Determine if the range is interleaved or not.
Definition: addr_range.hh:284
bool isSubset(const AddrRange &r) const
Determine if this range is a subset of another range, i.e.
Definition: addr_range.hh:445
AddrRange RangeEx(Addr start, Addr end)
Definition: addr_range.hh:797
AddrRange RangeSize(Addr start, Addr size)
Definition: addr_range.hh:815
uint64_t granularity() const
Determing the interleaving granularity of the range.
Definition: addr_range.hh:294
AddrRange RangeIn(Addr start, Addr end)
Definition: addr_range.hh:806
AddrRangeList exclude(const AddrRangeList &exclude_ranges) const
Subtract a list of intervals from the range and return the resulting collection of ranges,...
Definition: addr_range.hh:639
uint32_t stripes() const
Determine the number of interleaved address stripes this range is part of.
Definition: addr_range.hh:316
bool valid() const
Determine if the range is valid.
Definition: addr_range.hh:336
bool intersects(const AddrRange &r) const
Determine if another range intersects this one, i.e.
Definition: addr_range.hh:408
Addr end() const
Get the end address of the range.
Definition: addr_range.hh:350
bool mergesWith(const AddrRange &r) const
Determine if another range merges with the current one, i.e.
Definition: addr_range.hh:391
Addr start() const
Get the start address of the range.
Definition: addr_range.hh:343
Addr size() const
Get the size of the address range.
Definition: addr_range.hh:326
std::string to_string() const
Get a string representation of the range.
Definition: addr_range.hh:360
const char * gem5Version
Definition: version.cc:35
Random random_mt
Definition: random.cc:99
const char * compileDate
Definition: date.cc:35
void preCheckpointRestore()
Run state fixups before a checkpoint restore operation.
Definition: drain.cc:135
DrainState state() const
Get the simulators global drain state.
Definition: drain.hh:152
void signalDrainDone()
Notify the DrainManager that a Drainable object has finished draining.
Definition: drain.cc:149
virtual void notifyFork()
Notify a child process of a fork.
Definition: drain.hh:344
void resume()
Resume normal simulation in a Drained system.
Definition: drain.cc:96
DrainState drainState() const
Return the current drain state of an object.
Definition: drain.hh:324
bool isDrained() const
Check if the system is drained.
Definition: drain.hh:145
bool tryDrain()
Try to drain the system.
Definition: drain.cc:64
@ Draining
Draining buffers pending serialization/handover.
@ Running
Running normally.
@ Drained
Buffers drained, ready for serialization/handover.
Bitfield< 51 > t
Definition: pagetable.hh:56
Bitfield< 56 > f
Definition: pagetable.hh:53
void setInterpDir(const std::string &dirname)
This is the interface for setting up a base path for the elf interpreter.
Definition: elf_object.cc:103
Tick s
second
Definition: core.cc:68
const FlagsType init
This Stat is Initialized.
Definition: info.hh:56
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick getClockFrequency()
Definition: core.cc:124
static void init_drain(py::module_ &m_native)
Definition: core.cc:89
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
void pybind_init_core(py::module_ &m_native)
Definition: core.cc:201
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
bool clockFrequencyFixed()
Definition: core.cc:115
void fixClockFrequency()
Definition: core.cc:87
static void init_loader(py::module_ &m_native)
Definition: core.cc:193
const Tick MaxTick
Definition: types.hh:60
void setOutputDir(const std::string &dir)
Definition: core.cc:127
PybindSimObjectResolver pybindSimObjectResolver
Definition: core.cc:73
void setClockFrequency(Tick tps)
Definition: core.cc:118
void doExitCleanup()
Do C++ simulator exit processing.
Definition: core.cc:156
static void init_net(py::module_ &m_native)
Definition: core.cc:167
static void init_serialize(py::module_ &m_native)
Definition: core.cc:121
static void init_range(py::module_ &m_native)
Definition: core.cc:134
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:39 for gem5 by doxygen 1.9.1