gem5  v21.0.1.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/pybind11.h"
43 #include "pybind11/stl.h"
44 
45 #include "python/pybind11/core.hh"
46 
47 #include <ctime>
48 
49 #include "base/addr_range.hh"
50 #include "base/inet.hh"
52 #include "base/logging.hh"
53 #include "base/random.hh"
54 #include "base/socket.hh"
55 #include "base/temperature.hh"
56 #include "base/types.hh"
57 #include "sim/core.hh"
58 #include "sim/drain.hh"
59 #include "sim/serialize.hh"
60 #include "sim/sim_object.hh"
61 
62 namespace py = pybind11;
63 
66 {
67  SimObject *resolveSimObject(const std::string &name);
68 };
69 
71 
72 SimObject *
74 {
75  // TODO
76  py::module_ m = py::module_::import("m5.SimObject");
77  auto f = m.attr("resolveSimObject");
78 
79  return f(name).cast<SimObject *>();
80 }
81 
82 extern const char *compileDate;
83 extern const char *gem5Version;
84 
85 #ifdef DEBUG
86 const bool flag_DEBUG = true;
87 #else
88 const bool flag_DEBUG = false;
89 #endif
90 #ifdef NDEBUG
91 const bool flag_NDEBUG = true;
92 #else
93 const bool flag_NDEBUG = false;
94 #endif
95 const bool flag_TRACING_ON = TRACING_ON;
96 
97 static void
98 init_drain(py::module_ &m_native)
99 {
100  py::module_ m = m_native.def_submodule("drain");
101 
102  py::enum_<DrainState>(m, "DrainState")
103  .value("Running", DrainState::Running)
104  .value("Draining", DrainState::Draining)
105  .value("Drained", DrainState::Drained)
106  ;
107 
108  py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
109  m, "Drainable")
110  .def("drainState", &Drainable::drainState)
111  .def("notifyFork", &Drainable::notifyFork)
112  ;
113 
114  // The drain manager is a singleton with a private
115  // destructor. Disable deallocation from the Python binding.
116  py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
117  m, "DrainManager")
118  .def("tryDrain", &DrainManager::tryDrain)
119  .def("resume", &DrainManager::resume)
120  .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
121  .def("isDrained", &DrainManager::isDrained)
122  .def("state", &DrainManager::state)
123  .def("signalDrainDone", &DrainManager::signalDrainDone)
124  .def_static("instance", &DrainManager::instance,
125  py::return_value_policy::reference)
126  ;
127 }
128 
129 static void
130 init_serialize(py::module_ &m_native)
131 {
132  py::module_ m = m_native.def_submodule("serialize");
133 
134  py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
135  m, "Serializable")
136  ;
137 
138  py::class_<CheckpointIn>(m, "CheckpointIn")
139  ;
140 }
141 
142 static void
143 init_range(py::module_ &m_native)
144 {
145  py::module_ m = m_native.def_submodule("range");
146 
147  py::class_<AddrRange>(m, "AddrRange")
148  .def(py::init<>())
149  .def(py::init<Addr &, Addr &>())
150  .def(py::init<Addr, Addr, const std::vector<Addr> &, uint8_t>())
151  .def(py::init<const std::vector<AddrRange> &>())
152  .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
153 
154  .def("__str__", &AddrRange::to_string)
155 
156  .def("interleaved", &AddrRange::interleaved)
157  .def("granularity", &AddrRange::granularity)
158  .def("stripes", &AddrRange::stripes)
159  .def("size", &AddrRange::size)
160  .def("valid", &AddrRange::valid)
161  .def("start", &AddrRange::start)
162  .def("end", &AddrRange::end)
163  .def("mergesWith", &AddrRange::mergesWith)
164  .def("intersects", &AddrRange::intersects)
165  .def("isSubset", &AddrRange::isSubset)
166  ;
167 
168  // We need to make vectors of AddrRange opaque to avoid weird
169  // memory allocation issues in PyBind's STL wrappers.
170  py::bind_vector<std::vector<AddrRange>>(m, "AddrRangeVector");
171 
172  m.def("RangeEx", &RangeEx);
173  m.def("RangeIn", &RangeIn);
174  m.def("RangeSize", &RangeSize);
175 }
176 
177 static void
178 init_net(py::module_ &m_native)
179 {
180  py::module_ m = m_native.def_submodule("net");
181 
182  py::class_<Net::EthAddr>(m, "EthAddr")
183  .def(py::init<>())
184  .def(py::init<const std::string &>())
185  ;
186 
187  py::class_<Net::IpAddress>(m, "IpAddress")
188  .def(py::init<>())
189  .def(py::init<uint32_t>())
190  ;
191 
192  py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
193  .def(py::init<>())
194  .def(py::init<uint32_t, uint8_t>())
195  ;
196 
197  py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
198  .def(py::init<>())
199  .def(py::init<uint32_t, uint16_t>())
200  ;
201 }
202 
203 static void
204 init_loader(py::module_ &m_native)
205 {
206  py::module_ m = m_native.def_submodule("loader");
207 
208  m.def("setInterpDir", &Loader::setInterpDir);
209 }
210 
211 void
212 pybind_init_core(py::module_ &m_native)
213 {
214  py::module_ m_core = m_native.def_submodule("core");
215 
216  py::class_<Cycles>(m_core, "Cycles")
217  .def(py::init<>())
218  .def(py::init<uint64_t>())
219  .def("__int__", &Cycles::operator uint64_t)
220  .def("__add__", &Cycles::operator+)
221  .def("__sub__", &Cycles::operator-)
222  ;
223 
224  py::class_<Temperature>(m_core, "Temperature")
225  .def(py::init<>())
226  .def(py::init<double>())
227  .def_static("from_celsius", &Temperature::fromCelsius)
228  .def_static("from_kelvin", &Temperature::fromKelvin)
229  .def_static("from_fahrenheit", &Temperature::fromFahrenheit)
230  .def("celsius", &Temperature::toCelsius)
231  .def("kelvin", &Temperature::toKelvin)
232  .def("fahrenheit", &Temperature::toFahrenheit)
233  .def(py::self == py::self)
234  .def(py::self != py::self)
235  .def(py::self < py::self)
236  .def(py::self <= py::self)
237  .def(py::self > py::self)
238  .def(py::self >= py::self)
239  .def(py::self + py::self)
240  .def(py::self - py::self)
241  .def(py::self * float())
242  .def(float() * py::self)
243  .def(py::self / float())
244  .def("__str__", [](const Temperature &t) {
245  std::stringstream s;
246  s << t;
247  return s.str();
248  })
249  .def("__repr__", [](const Temperature &t) {
250  std::stringstream s;
251  s << "Temperature(" << t.toKelvin() << ")";
252  return s.str();
253  })
254  ;
255 
256  py::class_<tm>(m_core, "tm")
257  .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
258  .def_readwrite("tm_sec", &tm::tm_sec)
259  .def_readwrite("tm_min", &tm::tm_min)
260  .def_readwrite("tm_hour", &tm::tm_hour)
261  .def_readwrite("tm_mday", &tm::tm_mday)
262  .def_readwrite("tm_mon", &tm::tm_mon)
263  .def_readwrite("tm_wday", &tm::tm_wday)
264  .def_readwrite("tm_yday", &tm::tm_yday)
265  .def_readwrite("tm_isdst", &tm::tm_isdst)
266  ;
267 
268  py::enum_<Logger::LogLevel>(m_core, "LogLevel")
269  .value("PANIC", Logger::PANIC)
270  .value("FATAL", Logger::FATAL)
271  .value("WARN", Logger::WARN)
272  .value("INFO", Logger::INFO)
273  .value("HACK", Logger::HACK)
274  ;
275 
276  m_core
277  .def("setLogLevel", &Logger::setLevel)
278  .def("setOutputDir", &setOutputDir)
279  .def("doExitCleanup", &doExitCleanup)
280 
281  .def("disableAllListeners", &ListenSocket::disableAll)
282  .def("listenersDisabled", &ListenSocket::allDisabled)
283  .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
284  .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
285 
286 
287  .def("fixClockFrequency", &fixClockFrequency)
288  .def("clockFrequencyFixed", &clockFrequencyFixed)
289 
290  .def("setClockFrequency", &setClockFrequency)
291  .def("getClockFrequency", &getClockFrequency)
292  .def("curTick", curTick)
293  ;
294 
295  /* TODO: These should be read-only */
296  m_core.attr("compileDate") = py::cast(compileDate);
297  m_core.attr("gem5Version") = py::cast(gem5Version);
298 
299  m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
300  m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
301  m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
302  m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
303 
304  m_core.attr("MaxTick") = py::cast(MaxTick);
305 
306  /*
307  * Serialization helpers
308  */
309  m_core
310  .def("serializeAll", &Serializable::serializeAll)
311  .def("unserializeGlobals", &Serializable::unserializeGlobals)
312  .def("getCheckpoint", [](const std::string &cpt_dir) {
313  return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
314  })
315 
316  ;
317 
318 
319  init_drain(m_native);
320  init_serialize(m_native);
321  init_range(m_native);
322  init_net(m_native);
323  init_loader(m_native);
324 }
325 
SimObjectResolver
Base class to wrap object resolving functionality.
Definition: sim_object.hh:358
Stats::init
const FlagsType init
This Stat is Initialized.
Definition: info.hh:46
doExitCleanup
void doExitCleanup()
Do C++ simulator exit processing.
Definition: core.cc:147
socket.hh
DrainManager::signalDrainDone
void signalDrainDone()
Notify the DrainManager that a Drainable object has finished draining.
Definition: drain.cc:146
DrainState::Running
@ Running
Running normally.
serialize.hh
Temperature::fromCelsius
static Temperature fromCelsius(double _value)
Definition: temperature.cc:47
DrainManager::tryDrain
bool tryDrain()
Try to drain the system.
Definition: drain.cc:61
getClockFrequency
Tick getClockFrequency()
Definition: core.cc:115
Logger::INFO
@ INFO
Definition: logging.hh:65
AddrRange::interleaved
bool interleaved() const
Determine if the range is interleaved or not.
Definition: addr_range.hh:257
setClockFrequency
void setClockFrequency(Tick tps)
Definition: core.cc:109
Temperature::toCelsius
constexpr double toCelsius() const
Definition: temperature.hh:66
AddrRange::stripes
uint32_t stripes() const
Determine the number of interleaved address stripes this range is part of.
Definition: addr_range.hh:288
random.hh
AddrRange::end
Addr end() const
Get the end address of the range.
Definition: addr_range.hh:321
std::vector< Addr >
DrainManager::isDrained
bool isDrained() const
Check if the system is drained.
Definition: drain.hh:141
Temperature::fromFahrenheit
static Temperature fromFahrenheit(double _value)
Definition: temperature.cc:53
flag_DEBUG
const bool flag_DEBUG
Definition: core.cc:88
DrainManager::resume
void resume()
Resume normal simulation in a Drained system.
Definition: drain.cc:93
Drainable::notifyFork
virtual void notifyFork()
Notify a child process of a fork.
Definition: drain.hh:340
Logger::setLevel
static void setLevel(LogLevel ll)
Definition: logging.hh:70
Temperature
The class stores temperatures in Kelvin and provides helper methods to convert to/from Celsius.
Definition: temperature.hh:47
Loader::setInterpDir
void setInterpDir(const std::string &dirname)
This is the interface for setting up a base path for the elf interpreter.
Definition: elf_object.cc:98
DrainState::Drained
@ Drained
Buffers drained, ready for serialization/handover.
flag_TRACING_ON
const bool flag_TRACING_ON
Definition: core.cc:95
Temperature::toKelvin
constexpr double toKelvin() const
Definition: temperature.hh:65
random_mt
Random random_mt
Definition: random.cc:96
pybindSimObjectResolver
PybindSimObjectResolver pybindSimObjectResolver
Definition: core.cc:70
elf_object.hh
Logger::PANIC
@ PANIC
Definition: logging.hh:65
gem5Version
const char * gem5Version
Definition: version.cc:32
Random::init
void init(uint32_t s)
Definition: random.cc:64
sim_object.hh
compileDate
const char * compileDate
Definition: date.cc:32
init_serialize
static void init_serialize(py::module_ &m_native)
Definition: core.cc:130
Serializable::unserializeGlobals
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:203
AddrRange::valid
bool valid() const
Determine if the range is valid.
Definition: addr_range.hh:307
SimClock::Int::s
Tick s
second
Definition: core.cc:59
RangeSize
AddrRange RangeSize(Addr start, Addr size)
Definition: addr_range.hh:651
clockFrequencyFixed
bool clockFrequencyFixed()
Definition: core.cc:106
init_drain
static void init_drain(py::module_ &m_native)
Definition: core.cc:98
AddrRange::intersects
bool intersects(const AddrRange &r) const
Determine if another range intersects this one, i.e.
Definition: addr_range.hh:376
init_loader
static void init_loader(py::module_ &m_native)
Definition: core.cc:204
flag_NDEBUG
const bool flag_NDEBUG
Definition: core.cc:93
core.hh
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
PybindSimObjectResolver::resolveSimObject
SimObject * resolveSimObject(const std::string &name)
Find a SimObject given a full path name.
Definition: core.cc:73
name
const std::string & name()
Definition: trace.cc:48
addr_range.hh
temperature.hh
Drainable::drainState
DrainState drainState() const
Return the current drain state of an object.
Definition: drain.hh:320
DrainManager::instance
static DrainManager & instance()
Get the singleton DrainManager instance.
Definition: drain.hh:87
ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:68
DrainManager::state
DrainState state() const
Get the simulators global drain state.
Definition: drain.hh:148
PybindSimObjectResolver
Resolve a SimObject name using the Pybind configuration.
Definition: core.cc:65
fixClockFrequency
void fixClockFrequency()
Definition: core.cc:78
AddrRange::granularity
uint64_t granularity() const
Determing the interleaving granularity of the range.
Definition: addr_range.hh:266
AddrRange::start
Addr start() const
Get the start address of the range.
Definition: addr_range.hh:314
AddrRange::to_string
std::string to_string() const
Get a string representation of the range.
Definition: addr_range.hh:330
types.hh
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
DrainManager::preCheckpointRestore
void preCheckpointRestore()
Run state fixups before a checkpoint restore operation.
Definition: drain.cc:132
Temperature::toFahrenheit
double toFahrenheit() const
Definition: temperature.cc:59
Logger::WARN
@ WARN
Definition: logging.hh:65
Logger::HACK
@ HACK
Definition: logging.hh:65
core.hh
ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:74
AddrRange::mergesWith
bool mergesWith(const AddrRange &r) const
Determine if another range merges with the current one, i.e.
Definition: addr_range.hh:360
logging.hh
Serializable::serializeAll
static void serializeAll(const std::string &cpt_dir)
Serializes all the SimObjects.
Definition: serialize.cc:184
drain.hh
RangeEx
AddrRange RangeEx(Addr start, Addr end)
Definition: addr_range.hh:637
ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:60
init_range
static void init_range(py::module_ &m_native)
Definition: core.cc:143
setOutputDir
void setOutputDir(const std::string &dir)
Definition: core.cc:118
curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:43
AddrRange::isSubset
bool isSubset(const AddrRange &r) const
Determine if this range is a subset of another range, i.e.
Definition: addr_range.hh:410
RangeIn
AddrRange RangeIn(Addr start, Addr end)
Definition: addr_range.hh:644
inet.hh
CheckpointIn
Definition: serialize.hh:68
AddrRange::size
Addr size() const
Get the size of the address range.
Definition: addr_range.hh:297
Logger::FATAL
@ FATAL
Definition: logging.hh:65
MaxTick
const Tick MaxTick
Definition: types.hh:61
pybind_init_core
void pybind_init_core(py::module_ &m_native)
Definition: core.cc:212
init_net
static void init_net(py::module_ &m_native)
Definition: core.cc:178
DrainState::Draining
@ Draining
Draining buffers pending serialization/handover.
ArmISA::m
Bitfield< 0 > m
Definition: miscregs_types.hh:389
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64
Temperature::fromKelvin
static Temperature fromKelvin(double _value)
Definition: temperature.cc:41
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:141

Generated on Tue Jun 22 2021 15:28:30 for gem5 by doxygen 1.8.17