gem5  v22.0.0.1
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
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::VegaISA::f
Bitfield< 56 > f
Definition: pagetable.hh:53
gem5::AddrRange::to_string
std::string to_string() const
Get a string representation of the range.
Definition: addr_range.hh:360
socket.hh
gem5::AddrRange::granularity
uint64_t granularity() const
Determing the interleaving granularity of the range.
Definition: addr_range.hh:294
gem5::AddrRange::start
Addr start() const
Get the start address of the range.
Definition: addr_range.hh:343
gem5::VegaISA::m
m
Definition: pagetable.hh:52
gem5::Logger::PANIC
@ PANIC
Definition: logging.hh:70
serialize.hh
gem5::init_loader
static void init_loader(py::module_ &m_native)
Definition: core.cc:193
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:815
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
cur_tick.hh
random.hh
gem5::init_net
static void init_net(py::module_ &m_native)
Definition: core.cc:167
gem5::MaxTick
const Tick MaxTick
Definition: types.hh:60
gem5::init_serialize
static void init_serialize(py::module_ &m_native)
Definition: core.cc:121
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:408
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:445
gem5::pybind_init_core
void pybind_init_core(py::module_ &m_native)
Definition: core.cc:201
gem5::init_range
static void init_range(py::module_ &m_native)
Definition: core.cc:134
gem5::RangeIn
AddrRange RangeIn(Addr start, Addr end)
Definition: addr_range.hh:806
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:797
gem5::Logger::INFO
@ INFO
Definition: logging.hh:70
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::VegaISA::t
Bitfield< 51 > t
Definition: pagetable.hh:56
gem5::AddrRange::interleaved
bool interleaved() const
Determine if the range is interleaved or not.
Definition: addr_range.hh:284
sim_object.hh
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:326
gem5::AddrRange::end
Addr end() const
Get the end address of the range.
Definition: addr_range.hh:350
gem5::AddrRange::valid
bool valid() const
Determine if the range is valid.
Definition: addr_range.hh:336
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:76
gem5::Logger::HACK
@ HACK
Definition: logging.hh:70
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::DrainState::Drained
@ Drained
Buffers drained, ready for serialization/handover.
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
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:89
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:391
gem5::DrainManager::tryDrain
bool tryDrain()
Try to drain the system.
Definition: drain.cc:64
gem5::pybindSimObjectResolver
PybindSimObjectResolver pybindSimObjectResolver
Definition: core.cc:73
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:68
gem5::Logger::FATAL
@ FATAL
Definition: logging.hh:70
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:75
gem5::AddrRange::exclude
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
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:70
gem5::Temperature::fromCelsius
static Temperature fromCelsius(double _value)
Definition: temperature.cc:50
gem5::AddrRange
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:81
std::list< AddrRange >
inet.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
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:316

Generated on Sat Jun 18 2022 08:12:30 for gem5 by doxygen 1.8.17