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

Generated on Tue Sep 21 2021 12:25:47 for gem5 by doxygen 1.8.17