gem5  v20.1.0.0
core.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 2019 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/types.hh"
56 #include "sim/core.hh"
57 #include "sim/drain.hh"
58 #include "sim/serialize.hh"
59 #include "sim/sim_object.hh"
60 
61 namespace py = pybind11;
62 
65 {
66  SimObject *resolveSimObject(const std::string &name);
67 };
68 
70 
71 SimObject *
73 {
74  // TODO
75  py::module m = py::module::import("m5.SimObject");
76  auto f = m.attr("resolveSimObject");
77 
78  return f(name).cast<SimObject *>();
79 }
80 
81 extern const char *compileDate;
82 extern const char *gem5Version;
83 
84 #ifdef DEBUG
85 const bool flag_DEBUG = true;
86 #else
87 const bool flag_DEBUG = false;
88 #endif
89 #ifdef NDEBUG
90 const bool flag_NDEBUG = true;
91 #else
92 const bool flag_NDEBUG = false;
93 #endif
94 const bool flag_TRACING_ON = TRACING_ON;
95 
96 static void
97 init_drain(py::module &m_native)
98 {
99  py::module m = m_native.def_submodule("drain");
100 
101  py::enum_<DrainState>(m, "DrainState")
102  .value("Running", DrainState::Running)
103  .value("Draining", DrainState::Draining)
104  .value("Drained", DrainState::Drained)
105  ;
106 
107  py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
108  m, "Drainable")
109  .def("drainState", &Drainable::drainState)
110  .def("notifyFork", &Drainable::notifyFork)
111  ;
112 
113  // The drain manager is a singleton with a private
114  // destructor. Disable deallocation from the Python binding.
115  py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
116  m, "DrainManager")
117  .def("tryDrain", &DrainManager::tryDrain)
118  .def("resume", &DrainManager::resume)
119  .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
120  .def("isDrained", &DrainManager::isDrained)
121  .def("state", &DrainManager::state)
122  .def("signalDrainDone", &DrainManager::signalDrainDone)
123  .def_static("instance", &DrainManager::instance,
124  py::return_value_policy::reference)
125  ;
126 }
127 
128 static void
129 init_serialize(py::module &m_native)
130 {
131  py::module m = m_native.def_submodule("serialize");
132 
133  py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
134  m, "Serializable")
135  ;
136 
137  py::class_<CheckpointIn>(m, "CheckpointIn")
138  ;
139 }
140 
141 static void
142 init_range(py::module &m_native)
143 {
144  py::module m = m_native.def_submodule("range");
145 
146  py::class_<AddrRange>(m, "AddrRange")
147  .def(py::init<>())
148  .def(py::init<Addr &, Addr &>())
149  .def(py::init<Addr, Addr, const std::vector<Addr> &, uint8_t>())
150  .def(py::init<const std::vector<AddrRange> &>())
151  .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
152 
153  .def("__str__", &AddrRange::to_string)
154 
155  .def("interleaved", &AddrRange::interleaved)
156  .def("granularity", &AddrRange::granularity)
157  .def("stripes", &AddrRange::stripes)
158  .def("size", &AddrRange::size)
159  .def("valid", &AddrRange::valid)
160  .def("start", &AddrRange::start)
161  .def("end", &AddrRange::end)
162  .def("mergesWith", &AddrRange::mergesWith)
163  .def("intersects", &AddrRange::intersects)
164  .def("isSubset", &AddrRange::isSubset)
165  ;
166 
167  // We need to make vectors of AddrRange opaque to avoid weird
168  // memory allocation issues in PyBind's STL wrappers.
169  py::bind_vector<std::vector<AddrRange>>(m, "AddrRangeVector");
170 
171  m.def("RangeEx", &RangeEx);
172  m.def("RangeIn", &RangeIn);
173  m.def("RangeSize", &RangeSize);
174 }
175 
176 static void
177 init_net(py::module &m_native)
178 {
179  py::module m = m_native.def_submodule("net");
180 
181  py::class_<Net::EthAddr>(m, "EthAddr")
182  .def(py::init<>())
183  .def(py::init<const std::string &>())
184  ;
185 
186  py::class_<Net::IpAddress>(m, "IpAddress")
187  .def(py::init<>())
188  .def(py::init<uint32_t>())
189  ;
190 
191  py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
192  .def(py::init<>())
193  .def(py::init<uint32_t, uint8_t>())
194  ;
195 
196  py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
197  .def(py::init<>())
198  .def(py::init<uint32_t, uint16_t>())
199  ;
200 }
201 
202 static void
203 init_loader(py::module &m_native)
204 {
205  py::module m = m_native.def_submodule("loader");
206 
207  m.def("setInterpDir", &Loader::setInterpDir);
208 }
209 
210 void
211 pybind_init_core(py::module &m_native)
212 {
213  py::module m_core = m_native.def_submodule("core");
214 
215  py::class_<Cycles>(m_core, "Cycles")
216  .def(py::init<>())
217  .def(py::init<uint64_t>())
218  .def("__int__", &Cycles::operator uint64_t)
219  .def("__add__", &Cycles::operator+)
220  .def("__sub__", &Cycles::operator-)
221  ;
222 
223  py::class_<tm>(m_core, "tm")
224  .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
225  .def_readwrite("tm_sec", &tm::tm_sec)
226  .def_readwrite("tm_min", &tm::tm_min)
227  .def_readwrite("tm_hour", &tm::tm_hour)
228  .def_readwrite("tm_mday", &tm::tm_mday)
229  .def_readwrite("tm_mon", &tm::tm_mon)
230  .def_readwrite("tm_wday", &tm::tm_wday)
231  .def_readwrite("tm_yday", &tm::tm_yday)
232  .def_readwrite("tm_isdst", &tm::tm_isdst)
233  ;
234 
235  py::enum_<Logger::LogLevel>(m_core, "LogLevel")
236  .value("PANIC", Logger::PANIC)
237  .value("FATAL", Logger::FATAL)
238  .value("WARN", Logger::WARN)
239  .value("INFO", Logger::INFO)
240  .value("HACK", Logger::HACK)
241  ;
242 
243  m_core
244  .def("setLogLevel", &Logger::setLevel)
245  .def("setOutputDir", &setOutputDir)
246  .def("doExitCleanup", &doExitCleanup)
247 
248  .def("disableAllListeners", &ListenSocket::disableAll)
249  .def("listenersDisabled", &ListenSocket::allDisabled)
250  .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
251  .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
252 
253 
254  .def("fixClockFrequency", &fixClockFrequency)
255  .def("clockFrequencyFixed", &clockFrequencyFixed)
256 
257  .def("setClockFrequency", &setClockFrequency)
258  .def("getClockFrequency", &getClockFrequency)
259  .def("curTick", curTick)
260  ;
261 
262  /* TODO: These should be read-only */
263  m_core.attr("compileDate") = py::cast(compileDate);
264  m_core.attr("gem5Version") = py::cast(gem5Version);
265 
266  m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
267  m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
268  m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
269  m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
270 
271  m_core.attr("MaxTick") = py::cast(MaxTick);
272 
273  /*
274  * Serialization helpers
275  */
276  m_core
277  .def("serializeAll", &Serializable::serializeAll)
278  .def("unserializeGlobals", &Serializable::unserializeGlobals)
279  .def("getCheckpoint", [](const std::string &cpt_dir) {
280  return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
281  })
282 
283  ;
284 
285 
286  init_drain(m_native);
287  init_serialize(m_native);
288  init_range(m_native);
289  init_net(m_native);
290  init_loader(m_native);
291 }
292 
SimObjectResolver
Base class to wrap object resolving functionality.
Definition: sim_object.hh:294
Stats::init
const FlagsType init
This Stat is Initialized.
Definition: info.hh:45
doExitCleanup
void doExitCleanup()
Do C++ simulator exit processing.
Definition: core.cc:150
socket.hh
DrainManager::signalDrainDone
void signalDrainDone()
Notify the DrainManager that a Drainable object has finished draining.
Definition: drain.cc:146
init_net
static void init_net(py::module &m_native)
Definition: core.cc:177
DrainState::Running
@ Running
Running normally.
serialize.hh
DrainManager::tryDrain
bool tryDrain()
Try to drain the system.
Definition: drain.cc:61
getClockFrequency
Tick getClockFrequency()
Definition: core.cc:118
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:112
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
flag_DEBUG
const bool flag_DEBUG
Definition: core.cc:87
DrainManager::resume
void resume()
Resume normal simulation in a Drained system.
Definition: drain.cc:93
init_loader
static void init_loader(py::module &m_native)
Definition: core.cc:203
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
setOutputDir
void setOutputDir(const string &dir)
Definition: core.cc:121
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:94
random_mt
Random random_mt
Definition: random.cc:96
pybindSimObjectResolver
PybindSimObjectResolver pybindSimObjectResolver
Definition: core.cc:69
init_drain
static void init_drain(py::module &m_native)
Definition: core.cc:97
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
Serializable::unserializeGlobals
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:204
AddrRange::valid
bool valid() const
Determine if the range is valid.
Definition: addr_range.hh:307
RangeSize
AddrRange RangeSize(Addr start, Addr size)
Definition: addr_range.hh:638
clockFrequencyFixed
bool clockFrequencyFixed()
Definition: core.cc:109
AddrRange::intersects
bool intersects(const AddrRange &r) const
Determine if another range intersects this one, i.e.
Definition: addr_range.hh:376
flag_NDEBUG
const bool flag_NDEBUG
Definition: core.cc:92
core.hh
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
PybindSimObjectResolver::resolveSimObject
SimObject * resolveSimObject(const std::string &name)
Find a SimObject given a full path name.
Definition: core.cc:72
name
const std::string & name()
Definition: trace.cc:50
init_serialize
static void init_serialize(py::module &m_native)
Definition: core.cc:129
addr_range.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:70
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:64
fixClockFrequency
void fixClockFrequency()
Definition: core.cc:81
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
pybind_init_core
void pybind_init_core(py::module &m_native)
Definition: core.cc:211
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
Logger::WARN
@ WARN
Definition: logging.hh:65
Logger::HACK
@ HACK
Definition: logging.hh:65
core.hh
ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:76
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:185
drain.hh
RangeEx
AddrRange RangeEx(Addr start, Addr end)
Definition: addr_range.hh:624
ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:62
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:631
init_range
static void init_range(py::module &m_native)
Definition: core.cc:142
inet.hh
CheckpointIn
Definition: serialize.hh:67
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:65
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
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17