summaryrefslogtreecommitdiff
path: root/src/python/pybind11/event.cc
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commit60e6e785f970578eba6dbee9330eaecf514b23c8 (patch)
tree9c47c06d22a39b9467d3d4e35c11849a17df7815 /src/python/pybind11/event.cc
parentba42457254cc362eddc099f22b60d469cc6369e0 (diff)
downloadgem5-60e6e785f970578eba6dbee9330eaecf514b23c8.tar.xz
python: Use PyBind11 instead of SWIG for Python wrappers
Use the PyBind11 wrapping infrastructure instead of SWIG to generate wrappers for functionality that needs to be exported to Python. This has several benefits: * PyBind11 can be redistributed with gem5, which means that we have full control of the version used. This avoid a large number of hard-to-debug SWIG issues we have seen in the past. * PyBind11 doesn't rely on a custom C++ parser, instead it relies on wrappers being explicitly declared in C++. The leads to slightly more boiler-plate code in manually created wrappers, but doesn't doesn't increase the overall code size. A big benefit is that this avoids strange compilation errors when SWIG doesn't understand modern language features. * Unlike SWIG, there is no risk that the wrapper code incorporates incorrect type casts (this has happened on numerous occasions in the past) since these will result in compile-time errors. As a part of this change, the mechanism to define exported methods has been redesigned slightly. New methods can be exported either by declaring them in the SimObject declaration and decorating them with the cxxMethod decorator or by adding an instance of PyBindMethod/PyBindProperty to the cxx_exports class variable. The decorator has the added benefit of making it possible to add a docstring and naming the method's parameters. The new wrappers have the following known issues: * Global events can't be memory managed correctly. This was the case in SWIG as well. Change-Id: I88c5a95b6cf6c32fa9e1ad31dfc08b2e8199a763 Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Andrew Bardsley <andrew.bardsley@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2231 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr> Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src/python/pybind11/event.cc')
-rw-r--r--src/python/pybind11/event.cc180
1 files changed, 180 insertions, 0 deletions
diff --git a/src/python/pybind11/event.cc b/src/python/pybind11/event.cc
new file mode 100644
index 000000000..45a2d46a0
--- /dev/null
+++ b/src/python/pybind11/event.cc
@@ -0,0 +1,180 @@
+/*
+ * Copyright (c) 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Copyright (c) 2006 The Regents of The University of Michigan
+ * Copyright (c) 2013 Advanced Micro Devices, Inc.
+ * Copyright (c) 2013 Mark D. Hill and David A. Wood
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Nathan Binkert
+ * Andreas Sandberg
+ */
+
+#include "pybind11/pybind11.h"
+#include "pybind11/stl.h"
+
+#include "sim/eventq.hh"
+#include "sim/sim_events.hh"
+#include "sim/sim_exit.hh"
+#include "sim/simulate.hh"
+
+namespace py = pybind11;
+
+/**
+ * PyBind wrapper for Events
+ *
+ * We need to wrap the Event class with some Python glue code to
+ * enable method overrides in Python and memory management. Unlike its
+ * C++ cousin, PyEvents need to override __call__ instead of
+ * Event::process().
+ *
+ * Memory management is mostly done using reference counting in
+ * Python. However, PyBind can't keep track of the reference the event
+ * queue holds to a scheduled event. We therefore need to inhibit
+ * deletion and hand over ownership to the event queue in case a
+ * scheduled event loses all of its Python references.
+ */
+class PyEvent : public Event
+{
+ public:
+ struct Deleter {
+ void operator()(PyEvent *ev) {
+ assert(!ev->isAutoDelete());
+ if (ev->scheduled()) {
+ // The event is scheduled, give ownership to the event
+ // queue.
+ ev->setFlags(Event::AutoDelete);
+ } else {
+ // The event isn't scheduled, hence Python owns it and
+ // we need to free it here.
+ delete ev;
+ }
+ }
+ };
+
+ PyEvent(Event::Priority priority)
+ : Event(priority)
+ { }
+
+ void process() override {
+ if (isAutoDelete()) {
+ // Ownership of the event was handed over to the event queue
+ // because the last revference in Python land was GCed. We
+ // need to claim the object again since we're creating a new
+ // Python reference.
+ clearFlags(AutoDelete);
+ }
+
+ // Call the Python implementation as __call__. This provides a
+ // slightly more Python-friendly interface.
+ PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
+ }
+};
+
+void
+pybind_init_event(py::module &m_native)
+{
+ py::module m = m_native.def_submodule("event");
+
+ m.def("simulate", &simulate,
+ py::arg("ticks") = MaxTick);
+ m.def("exitSimLoop", &exitSimLoop);
+ m.def("getEventQueue", []() { return curEventQueue(); },
+ py::return_value_policy::reference);
+ m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
+ m.def("getEventQueue", &getEventQueue,
+ py::return_value_policy::reference);
+
+ py::class_<EventQueue>(m, "EventQueue")
+ .def("name", [](EventQueue *eq) { return eq->name(); })
+ .def("dump", &EventQueue::dump)
+ .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
+ eq->schedule(e, t);
+ }, py::arg("event"), py::arg("when"))
+ .def("deschedule", [](EventQueue *eq, PyEvent *e) {
+ eq->deschedule(e);
+ }, py::arg("event"))
+ .def("reschedule", [](EventQueue *eq, PyEvent *e, Tick t, bool alw) {
+ eq->reschedule(e, t, alw);
+ }, py::arg("event"), py::arg("tick"), py::arg("always") = false)
+ ;
+
+ // TODO: Ownership of global exit events has always been a bit
+ // questionable. We currently assume they are owned by the C++
+ // word. This is what the old SWIG code did, but that will result
+ // in memory leaks.
+ py::class_<GlobalSimLoopExitEvent,
+ std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
+ m, "GlobalSimLoopExitEvent")
+ .def("getCause", &GlobalSimLoopExitEvent::getCause)
+ .def("getCode", &GlobalSimLoopExitEvent::getCode)
+ ;
+
+ // TODO: We currently export a wrapper class and not the Event
+ // base class. This wil be problematic if we ever return an event
+ // from C++.
+ py::class_<PyEvent, std::unique_ptr<PyEvent, PyEvent::Deleter>>
+ c_event(m, "Event");
+ c_event
+ .def(py::init<Event::Priority>(),
+ py::arg("priority") = (int)Event::Default_Pri)
+ .def("name", &Event::name)
+ .def("dump", &Event::dump)
+ .def("scheduled", &Event::scheduled)
+ .def("squash", &Event::squash)
+ .def("squashed", &Event::squashed)
+ .def("isExitEvent", &Event::isExitEvent)
+ .def("isAutoDelete", &Event::isAutoDelete)
+ .def("when", &Event::when)
+ .def("priority", &Event::priority)
+ ;
+
+#define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
+ PRIO(Minimum_Pri);
+ PRIO(Minimum_Pri);
+ PRIO(Debug_Enable_Pri);
+ PRIO(Debug_Break_Pri);
+ PRIO(CPU_Switch_Pri);
+ PRIO(Delayed_Writeback_Pri);
+ PRIO(Default_Pri);
+ PRIO(DVFS_Update_Pri);
+ PRIO(Serialize_Pri);
+ PRIO(CPU_Tick_Pri);
+ PRIO(Stat_Event_Pri);
+ PRIO(Progress_Event_Pri);
+ PRIO(Sim_Exit_Pri);
+ PRIO(Maximum_Pri);
+}