core.cc (13409:071d5425ce37) core.cc (14047:91279ed7ec5e)
1/*
2 * Copyright (c) 2017 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
49#include "python/pybind11/core.hh"
50
51#include <ctime>
52
53#include "base/addr_range.hh"
54#include "base/inet.hh"
55#include "base/logging.hh"
56#include "base/random.hh"
57#include "base/socket.hh"
58#include "base/types.hh"
59#include "sim/core.hh"
60#include "sim/drain.hh"
61#include "sim/serialize.hh"
62#include "sim/sim_object.hh"
63
64namespace py = pybind11;
65
66/** Resolve a SimObject name using the Pybind configuration */
67class PybindSimObjectResolver : public SimObjectResolver
68{
69 SimObject *resolveSimObject(const std::string &name);
70};
71
72PybindSimObjectResolver pybindSimObjectResolver;
73
74SimObject *
75PybindSimObjectResolver::resolveSimObject(const std::string &name)
76{
77 // TODO
78 py::module m = py::module::import("m5.SimObject");
79 auto f = m.attr("resolveSimObject");
80
81 return f(name).cast<SimObject *>();
82}
83
84extern const char *compileDate;
85
86#ifdef DEBUG
87const bool flag_DEBUG = true;
88#else
89const bool flag_DEBUG = false;
90#endif
91#ifdef NDEBUG
92const bool flag_NDEBUG = true;
93#else
94const bool flag_NDEBUG = false;
95#endif
96const bool flag_TRACING_ON = TRACING_ON;
97
98static void
99init_drain(py::module &m_native)
100{
101 py::module m = m_native.def_submodule("drain");
102
103 py::enum_<DrainState>(m, "DrainState")
104 .value("Running", DrainState::Running)
105 .value("Draining", DrainState::Draining)
106 .value("Drained", DrainState::Drained)
107 ;
108
109 py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
110 m, "Drainable")
111 .def("drainState", &Drainable::drainState)
112 .def("notifyFork", &Drainable::notifyFork)
113 ;
114
115 // The drain manager is a singleton with a private
116 // destructor. Disable deallocation from the Python binding.
117 py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
118 m, "DrainManager")
119 .def("tryDrain", &DrainManager::tryDrain)
120 .def("resume", &DrainManager::resume)
121 .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
122 .def("isDrained", &DrainManager::isDrained)
123 .def("state", &DrainManager::state)
124 .def("signalDrainDone", &DrainManager::signalDrainDone)
125 .def_static("instance", &DrainManager::instance,
126 py::return_value_policy::reference)
127 ;
128}
129
130static void
131init_serialize(py::module &m_native)
132{
133 py::module m = m_native.def_submodule("serialize");
134
135 py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
136 m, "Serializable")
137 ;
138
139 py::class_<CheckpointIn>(m, "CheckpointIn")
140 ;
141}
142
143static void
144init_range(py::module &m_native)
145{
146 py::module m = m_native.def_submodule("range");
147
148 py::class_<AddrRange>(m, "AddrRange")
149 .def(py::init<>())
150 .def(py::init<Addr &, Addr &>())
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)
1/*
2 * Copyright (c) 2017 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
49#include "python/pybind11/core.hh"
50
51#include <ctime>
52
53#include "base/addr_range.hh"
54#include "base/inet.hh"
55#include "base/logging.hh"
56#include "base/random.hh"
57#include "base/socket.hh"
58#include "base/types.hh"
59#include "sim/core.hh"
60#include "sim/drain.hh"
61#include "sim/serialize.hh"
62#include "sim/sim_object.hh"
63
64namespace py = pybind11;
65
66/** Resolve a SimObject name using the Pybind configuration */
67class PybindSimObjectResolver : public SimObjectResolver
68{
69 SimObject *resolveSimObject(const std::string &name);
70};
71
72PybindSimObjectResolver pybindSimObjectResolver;
73
74SimObject *
75PybindSimObjectResolver::resolveSimObject(const std::string &name)
76{
77 // TODO
78 py::module m = py::module::import("m5.SimObject");
79 auto f = m.attr("resolveSimObject");
80
81 return f(name).cast<SimObject *>();
82}
83
84extern const char *compileDate;
85
86#ifdef DEBUG
87const bool flag_DEBUG = true;
88#else
89const bool flag_DEBUG = false;
90#endif
91#ifdef NDEBUG
92const bool flag_NDEBUG = true;
93#else
94const bool flag_NDEBUG = false;
95#endif
96const bool flag_TRACING_ON = TRACING_ON;
97
98static void
99init_drain(py::module &m_native)
100{
101 py::module m = m_native.def_submodule("drain");
102
103 py::enum_<DrainState>(m, "DrainState")
104 .value("Running", DrainState::Running)
105 .value("Draining", DrainState::Draining)
106 .value("Drained", DrainState::Drained)
107 ;
108
109 py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
110 m, "Drainable")
111 .def("drainState", &Drainable::drainState)
112 .def("notifyFork", &Drainable::notifyFork)
113 ;
114
115 // The drain manager is a singleton with a private
116 // destructor. Disable deallocation from the Python binding.
117 py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
118 m, "DrainManager")
119 .def("tryDrain", &DrainManager::tryDrain)
120 .def("resume", &DrainManager::resume)
121 .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
122 .def("isDrained", &DrainManager::isDrained)
123 .def("state", &DrainManager::state)
124 .def("signalDrainDone", &DrainManager::signalDrainDone)
125 .def_static("instance", &DrainManager::instance,
126 py::return_value_policy::reference)
127 ;
128}
129
130static void
131init_serialize(py::module &m_native)
132{
133 py::module m = m_native.def_submodule("serialize");
134
135 py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
136 m, "Serializable")
137 ;
138
139 py::class_<CheckpointIn>(m, "CheckpointIn")
140 ;
141}
142
143static void
144init_range(py::module &m_native)
145{
146 py::module m = m_native.def_submodule("range");
147
148 py::class_<AddrRange>(m, "AddrRange")
149 .def(py::init<>())
150 .def(py::init<Addr &, Addr &>())
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("hashed", &AddrRange::hashed)
158 .def("granularity", &AddrRange::granularity)
159 .def("stripes", &AddrRange::stripes)
160 .def("size", &AddrRange::size)
161 .def("valid", &AddrRange::valid)
162 .def("start", &AddrRange::start)
163 .def("end", &AddrRange::end)
164 .def("mergesWith", &AddrRange::mergesWith)
165 .def("intersects", &AddrRange::intersects)
166 .def("isSubset", &AddrRange::isSubset)
167 ;
168
169 // We need to make vectors of AddrRange opaque to avoid weird
170 // memory allocation issues in PyBind's STL wrappers.
171 py::bind_vector<std::vector<AddrRange>>(m, "AddrRangeVector");
172
173 m.def("RangeEx", &RangeEx);
174 m.def("RangeIn", &RangeIn);
175 m.def("RangeSize", &RangeSize);
176}
177
178static void
179init_net(py::module &m_native)
180{
181 py::module m = m_native.def_submodule("net");
182
183 py::class_<Net::EthAddr>(m, "EthAddr")
184 .def(py::init<>())
185 .def(py::init<const std::string &>())
186 ;
187
188 py::class_<Net::IpAddress>(m, "IpAddress")
189 .def(py::init<>())
190 .def(py::init<uint32_t>())
191 ;
192
193 py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
194 .def(py::init<>())
195 .def(py::init<uint32_t, uint8_t>())
196 ;
197
198 py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
199 .def(py::init<>())
200 .def(py::init<uint32_t, uint16_t>())
201 ;
202}
203
204void
205pybind_init_core(py::module &m_native)
206{
207 py::module m_core = m_native.def_submodule("core");
208
209 py::class_<Cycles>(m_core, "Cycles")
210 .def(py::init<>())
211 .def(py::init<uint64_t>())
212 .def("__int__", &Cycles::operator uint64_t)
213 .def("__add__", &Cycles::operator+)
214 .def("__sub__", &Cycles::operator-)
215 ;
216
217 py::class_<tm>(m_core, "tm")
218 .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
219 .def_readwrite("tm_sec", &tm::tm_sec)
220 .def_readwrite("tm_min", &tm::tm_min)
221 .def_readwrite("tm_hour", &tm::tm_hour)
222 .def_readwrite("tm_mday", &tm::tm_mday)
223 .def_readwrite("tm_mon", &tm::tm_mon)
224 .def_readwrite("tm_wday", &tm::tm_wday)
225 .def_readwrite("tm_yday", &tm::tm_yday)
226 .def_readwrite("tm_isdst", &tm::tm_isdst)
227 ;
228
229 py::enum_<Logger::LogLevel>(m_core, "LogLevel")
230 .value("PANIC", Logger::PANIC)
231 .value("FATAL", Logger::FATAL)
232 .value("WARN", Logger::WARN)
233 .value("INFO", Logger::INFO)
234 .value("HACK", Logger::HACK)
235 ;
236
237 m_core
238 .def("setLogLevel", &Logger::setLevel)
239 .def("setOutputDir", &setOutputDir)
240 .def("doExitCleanup", &doExitCleanup)
241
242 .def("disableAllListeners", &ListenSocket::disableAll)
243 .def("listenersDisabled", &ListenSocket::allDisabled)
244 .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
245 .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
246
247
248 .def("fixClockFrequency", &fixClockFrequency)
249 .def("clockFrequencyFixed", &clockFrequencyFixed)
250
251 .def("setClockFrequency", &setClockFrequency)
252 .def("getClockFrequency", &getClockFrequency)
253 .def("curTick", curTick)
254 ;
255
256 /* TODO: These should be read-only */
257 m_core.attr("compileDate") = py::cast(compileDate);
258
259 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
260 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
261 m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
262 m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
263
264 m_core.attr("MaxTick") = py::cast(MaxTick);
265
266 /*
267 * Serialization helpers
268 */
269 m_core
270 .def("serializeAll", &Serializable::serializeAll)
271 .def("unserializeGlobals", &Serializable::unserializeGlobals)
272 .def("getCheckpoint", [](const std::string &cpt_dir) {
273 return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
274 })
275
276 ;
277
278
279 init_drain(m_native);
280 init_serialize(m_native);
281 init_range(m_native);
282 init_net(m_native);
283}
284
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
177static void
178init_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
203void
204pybind_init_core(py::module &m_native)
205{
206 py::module m_core = m_native.def_submodule("core");
207
208 py::class_<Cycles>(m_core, "Cycles")
209 .def(py::init<>())
210 .def(py::init<uint64_t>())
211 .def("__int__", &Cycles::operator uint64_t)
212 .def("__add__", &Cycles::operator+)
213 .def("__sub__", &Cycles::operator-)
214 ;
215
216 py::class_<tm>(m_core, "tm")
217 .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
218 .def_readwrite("tm_sec", &tm::tm_sec)
219 .def_readwrite("tm_min", &tm::tm_min)
220 .def_readwrite("tm_hour", &tm::tm_hour)
221 .def_readwrite("tm_mday", &tm::tm_mday)
222 .def_readwrite("tm_mon", &tm::tm_mon)
223 .def_readwrite("tm_wday", &tm::tm_wday)
224 .def_readwrite("tm_yday", &tm::tm_yday)
225 .def_readwrite("tm_isdst", &tm::tm_isdst)
226 ;
227
228 py::enum_<Logger::LogLevel>(m_core, "LogLevel")
229 .value("PANIC", Logger::PANIC)
230 .value("FATAL", Logger::FATAL)
231 .value("WARN", Logger::WARN)
232 .value("INFO", Logger::INFO)
233 .value("HACK", Logger::HACK)
234 ;
235
236 m_core
237 .def("setLogLevel", &Logger::setLevel)
238 .def("setOutputDir", &setOutputDir)
239 .def("doExitCleanup", &doExitCleanup)
240
241 .def("disableAllListeners", &ListenSocket::disableAll)
242 .def("listenersDisabled", &ListenSocket::allDisabled)
243 .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
244 .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
245
246
247 .def("fixClockFrequency", &fixClockFrequency)
248 .def("clockFrequencyFixed", &clockFrequencyFixed)
249
250 .def("setClockFrequency", &setClockFrequency)
251 .def("getClockFrequency", &getClockFrequency)
252 .def("curTick", curTick)
253 ;
254
255 /* TODO: These should be read-only */
256 m_core.attr("compileDate") = py::cast(compileDate);
257
258 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
259 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
260 m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
261 m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
262
263 m_core.attr("MaxTick") = py::cast(MaxTick);
264
265 /*
266 * Serialization helpers
267 */
268 m_core
269 .def("serializeAll", &Serializable::serializeAll)
270 .def("unserializeGlobals", &Serializable::unserializeGlobals)
271 .def("getCheckpoint", [](const std::string &cpt_dir) {
272 return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
273 })
274
275 ;
276
277
278 init_drain(m_native);
279 init_serialize(m_native);
280 init_range(m_native);
281 init_net(m_native);
282}
283