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

Generated on Fri Feb 28 2020 16:27:02 for gem5 by doxygen 1.8.13