diff options
author | Andreas Sandberg <andreas.sandberg@arm.com> | 2017-02-27 13:17:51 +0000 |
---|---|---|
committer | Andreas Sandberg <andreas.sandberg@arm.com> | 2017-05-02 12:37:32 +0000 |
commit | 60e6e785f970578eba6dbee9330eaecf514b23c8 (patch) | |
tree | 9c47c06d22a39b9467d3d4e35c11849a17df7815 /src/python/pybind11 | |
parent | ba42457254cc362eddc099f22b60d469cc6369e0 (diff) | |
download | gem5-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')
-rw-r--r-- | src/python/pybind11/core.cc | 272 | ||||
-rw-r--r-- | src/python/pybind11/debug.cc | 119 | ||||
-rw-r--r-- | src/python/pybind11/event.cc | 180 | ||||
-rw-r--r-- | src/python/pybind11/pybind.hh | 52 | ||||
-rw-r--r-- | src/python/pybind11/pyobject.cc | 152 | ||||
-rw-r--r-- | src/python/pybind11/stats.cc | 113 |
6 files changed, 888 insertions, 0 deletions
diff --git a/src/python/pybind11/core.cc b/src/python/pybind11/core.cc new file mode 100644 index 000000000..9e97df2b7 --- /dev/null +++ b/src/python/pybind11/core.cc @@ -0,0 +1,272 @@ +/* + * 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) 2010 Advanced Micro Devices, Inc. + * Copyright (c) 2006 The Regents of The University of Michigan + * 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 + * Steve Reinhardt + * Gabe Black + * Andreas Sandberg + */ + +#include "pybind11/pybind11.h" + +#include <ctime> + +#include "base/addr_range.hh" +#include "base/inet.hh" +#include "base/misc.hh" +#include "base/random.hh" +#include "base/socket.hh" +#include "base/types.hh" +#include "sim/core.hh" +#include "sim/drain.hh" +#include "sim/serialize.hh" +#include "sim/sim_object.hh" + +namespace py = pybind11; + +/** Resolve a SimObject name using the Pybind configuration */ +class PybindSimObjectResolver : public SimObjectResolver +{ + SimObject *resolveSimObject(const std::string &name); +}; + +PybindSimObjectResolver pybindSimObjectResolver; + +SimObject * +PybindSimObjectResolver::resolveSimObject(const std::string &name) +{ + // TODO + py::module m = py::module::import("m5.SimObject"); + auto f = m.attr("resolveSimObject"); + + return f(name).cast<SimObject *>(); +} + +extern const char *compileDate; + +#ifdef DEBUG +const bool flag_DEBUG = true; +#else +const bool flag_DEBUG = false; +#endif +#ifdef NDEBUG +const bool flag_NDEBUG = true; +#else +const bool flag_NDEBUG = false; +#endif +const bool flag_TRACING_ON = TRACING_ON; + +static void +init_drain(py::module &m_native) +{ + py::module m = m_native.def_submodule("drain"); + + py::enum_<DrainState>(m, "DrainState") + .value("Running", DrainState::Running) + .value("Draining", DrainState::Draining) + .value("Drained", DrainState::Drained) + ; + + py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>( + m, "Drainable") + .def("drainState", &Drainable::drainState) + .def("notifyFork", &Drainable::notifyFork) + ; + + // The drain manager is a singleton with a private + // destructor. Disable deallocation from the Python binding. + py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>( + m, "DrainManager") + .def("tryDrain", &DrainManager::tryDrain) + .def("resume", &DrainManager::resume) + .def("preCheckpointRestore", &DrainManager::preCheckpointRestore) + .def("isDrained", &DrainManager::isDrained) + .def("state", &DrainManager::state) + .def("signalDrainDone", &DrainManager::signalDrainDone) + .def_static("instance", &DrainManager::instance, + py::return_value_policy::reference) + ; +} + +static void +init_serialize(py::module &m_native) +{ + py::module m = m_native.def_submodule("serialize"); + + py::class_<Serializable>(m, "Serializable") + ; + + py::class_<CheckpointIn>(m, "CheckpointIn") + ; +} + +static void +init_range(py::module &m_native) +{ + py::module m = m_native.def_submodule("range"); + + py::class_<AddrRange>(m, "AddrRange") + .def(py::init<>()) + .def(py::init<Addr &, Addr &>()) + .def(py::init<const std::vector<AddrRange> &>()) + .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>()) + + .def("__str__", &AddrRange::to_string) + + .def("interleaved", &AddrRange::interleaved) + .def("hashed", &AddrRange::hashed) + .def("granularity", &AddrRange::granularity) + .def("stripes", &AddrRange::stripes) + .def("size", &AddrRange::size) + .def("valid", &AddrRange::valid) + .def("start", &AddrRange::start) + .def("end", &AddrRange::end) + .def("mergesWith", &AddrRange::mergesWith) + .def("intersects", &AddrRange::intersects) + .def("isSubset", &AddrRange::isSubset) + ; + + m.def("RangeEx", &RangeEx); + m.def("RangeIn", &RangeIn); + m.def("RangeSize", &RangeSize); +} + +static void +init_net(py::module &m_native) +{ + py::module m = m_native.def_submodule("net"); + + py::class_<Net::EthAddr>(m, "EthAddr") + .def(py::init<>()) + .def(py::init<const std::string &>()) + ; + + py::class_<Net::IpAddress>(m, "IpAddress") + .def(py::init<>()) + .def(py::init<uint32_t>()) + ; + + py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask") + .def(py::init<>()) + .def(py::init<uint32_t, uint8_t>()) + ; + + py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort") + .def(py::init<>()) + .def(py::init<uint32_t, uint16_t>()) + ; +} + +void +pybind_init_core(py::module &m_native) +{ + py::module m_core = m_native.def_submodule("core"); + + py::class_<Cycles>(m_core, "Cycles") + .def(py::init<>()) + .def(py::init<uint64_t>()) + .def("__int__", &Cycles::operator uint64_t) + .def("__add__", &Cycles::operator+) + .def("__sub__", &Cycles::operator-) + ; + + py::class_<tm>(m_core, "tm") + .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); }) + .def_readwrite("tm_sec", &tm::tm_sec) + .def_readwrite("tm_min", &tm::tm_min) + .def_readwrite("tm_hour", &tm::tm_hour) + .def_readwrite("tm_mday", &tm::tm_mday) + .def_readwrite("tm_mon", &tm::tm_mon) + .def_readwrite("tm_wday", &tm::tm_wday) + .def_readwrite("tm_yday", &tm::tm_yday) + .def_readwrite("tm_isdst", &tm::tm_isdst) + ; + + py::enum_<Logger::LogLevel>(m_core, "LogLevel") + .value("PANIC", Logger::PANIC) + .value("FATAL", Logger::FATAL) + .value("WARN", Logger::WARN) + .value("INFO", Logger::INFO) + .value("HACK", Logger::HACK) + ; + + m_core + .def("setLogLevel", &Logger::setLevel) + .def("setOutputDir", &setOutputDir) + .def("doExitCleanup", &doExitCleanup) + + .def("disableAllListeners", &ListenSocket::disableAll) + .def("listenersDisabled", &ListenSocket::allDisabled) + .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); }) + + + .def("setClockFrequency", &setClockFrequency) + .def("curTick", curTick) + ; + + /* TODO: These should be read-only */ + m_core.attr("compileDate") = py::cast(compileDate); + + m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG); + m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG); + m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG); + m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON); + + m_core.attr("MaxTick") = py::cast(MaxTick); + + /* + * Serialization helpers + */ + m_core + .def("serializeAll", &Serializable::serializeAll) + .def("unserializeGlobals", &Serializable::unserializeGlobals) + .def("getCheckpoint", [](const std::string &cpt_dir) { + return new CheckpointIn(cpt_dir, pybindSimObjectResolver); + }) + + ; + + + init_drain(m_native); + init_serialize(m_native); + init_range(m_native); + init_net(m_native); +} + diff --git a/src/python/pybind11/debug.cc b/src/python/pybind11/debug.cc new file mode 100644 index 000000000..8fcd0cdbc --- /dev/null +++ b/src/python/pybind11/debug.cc @@ -0,0 +1,119 @@ +/* + * 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) 2010 The Hewlett-Packard Development Company + * 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 <map> +#include <vector> + +#include "base/debug.hh" +#include "base/output.hh" +#include "base/trace.hh" +#include "sim/debug.hh" + +namespace py = pybind11; + +namespace Debug { +extern int allFlagsVersion; +} + +static void +output(const char *filename) +{ + OutputStream *file_stream = simout.find(filename); + + if (!file_stream) + file_stream = simout.create(filename); + + Trace::setDebugLogger(new Trace::OstreamLogger(*file_stream->stream())); +} + +static void +ignore(const char *expr) +{ + ObjectMatch ignore(expr); + + Trace::getDebugLogger()->setIgnore(ignore); +} + +void +pybind_init_debug(py::module &m_native) +{ + py::module m_debug = m_native.def_submodule("debug"); + + m_debug + .def("getAllFlagsVersion", []() { return Debug::allFlagsVersion; }) + .def("allFlags", &Debug::allFlags, py::return_value_policy::reference) + .def("findFlag", &Debug::findFlag) + .def("setDebugFlag", &setDebugFlag) + .def("clearDebugFlag", &clearDebugFlag) + .def("dumpDebugFlags", &dumpDebugFlags) + + .def("schedBreak", &schedBreak) + .def("setRemoteGDBPort", &setRemoteGDBPort) + ; + + py::class_<Debug::Flag> c_flag(m_debug, "Flag"); + c_flag + .def("name", &Debug::Flag::name) + .def("desc", &Debug::Flag::desc) + .def("kids", &Debug::Flag::kids) + .def("enable", &Debug::Flag::enable) + .def("disable", &Debug::Flag::disable) + .def("sync", &Debug::Flag::sync) + ; + + py::class_<Debug::SimpleFlag>(m_debug, "SimpleFlag", c_flag); + py::class_<Debug::CompoundFlag>(m_debug, "CompoundFlag", c_flag); + + + py::module m_trace = m_native.def_submodule("trace"); + m_trace + .def("output", &output) + .def("ignore", &ignore) + .def("enable", &Trace::enable) + .def("disable", &Trace::disable) + ; +} 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); +} diff --git a/src/python/pybind11/pybind.hh b/src/python/pybind11/pybind.hh new file mode 100644 index 000000000..9a0643c5a --- /dev/null +++ b/src/python/pybind11/pybind.hh @@ -0,0 +1,52 @@ +/* + * 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. + * + * 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: Andreas Sandberg + */ + +#ifndef __PYTHON_PYBIND11_PYBIND_HH__ +#define __PYTHON_PYBIND11_PYBIND_HH__ + +#include "pybind11/pybind11.h" + +void pybind_init_core(pybind11::module &m_native); +void pybind_init_debug(pybind11::module &m_native); + +void pybind_init_event(pybind11::module &m_native); +void pybind_init_pyobject(pybind11::module &m_native); +void pybind_init_stats(pybind11::module &m_native); + +#endif diff --git a/src/python/pybind11/pyobject.cc b/src/python/pybind11/pyobject.cc new file mode 100644 index 000000000..fe0ecba78 --- /dev/null +++ b/src/python/pybind11/pyobject.cc @@ -0,0 +1,152 @@ +/* + * 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 + * 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 + */ + +#include "pybind11/pybind11.h" + +#include <string> + +#include "config/the_isa.hh" + +#if THE_ISA != NULL_ISA +#include "dev/net/etherdevice.hh" +#include "dev/net/etherint.hh" +#include "dev/net/etherobject.hh" + +#endif + +#include "mem/mem_object.hh" +#include "mem/ruby/slicc_interface/AbstractController.hh" +#include "sim/full_system.hh" + +namespace py = pybind11; + +#if THE_ISA != NULL_ISA +static EtherInt * +lookupEthPort(SimObject *so, const std::string &name, int i) +{ + EtherObject *eo = dynamic_cast<EtherObject *>(so); + EtherDevice *ed = dynamic_cast<EtherDevice *>(so); + if (eo == NULL && ed == NULL) { + warn("error casting SimObject %s", so->name()); + return NULL; + } + + EtherInt *p = NULL; + if (eo) + p = eo->getEthPort(name, i); + else + p = ed->getEthPort(name, i); + return p; +} +#endif + +/** + * Connect the described MemObject ports. Called from Python via SWIG. + * The indices i1 & i2 will be -1 for regular ports, >= 0 for vector ports. + * SimObject1 is the master, and SimObject2 is the slave + */ +static int +connectPorts(SimObject *o1, const std::string &name1, int i1, + SimObject *o2, const std::string &name2, int i2) +{ +#if THE_ISA != NULL_ISA + EtherObject *eo1, *eo2; + EtherDevice *ed1, *ed2; + eo1 = dynamic_cast<EtherObject*>(o1); + ed1 = dynamic_cast<EtherDevice*>(o1); + eo2 = dynamic_cast<EtherObject*>(o2); + ed2 = dynamic_cast<EtherDevice*>(o2); + + if ((eo1 || ed1) && (eo2 || ed2)) { + EtherInt *p1 = lookupEthPort(o1, name1, i1); + EtherInt *p2 = lookupEthPort(o2, name2, i2); + + if (p1 != NULL && p2 != NULL) { + + p1->setPeer(p2); + p2->setPeer(p1); + + return 1; + } + } +#endif + + // These could be MessageBuffers from the ruby memory system. If so, they + // need not be connected to anything currently. + MessageBuffer *mb1, *mb2; + mb1 = dynamic_cast<MessageBuffer*>(o1); + mb2 = dynamic_cast<MessageBuffer*>(o2); + + if (mb1 || mb2) { + // No need to connect anything here currently. MessageBuffer + // connections in Python only serve to print the connections in + // the config output. + // TODO: Add real ports to MessageBuffers and use MemObject connect + // code below to bind MessageBuffer senders and receivers + return 1; + } + + MemObject *mo1, *mo2; + mo1 = dynamic_cast<MemObject*>(o1); + mo2 = dynamic_cast<MemObject*>(o2); + + if (mo1 == NULL || mo2 == NULL) { + panic ("Error casting SimObjects %s and %s to MemObject", o1->name(), + o2->name()); + } + + // generic master/slave port connection + BaseMasterPort& masterPort = mo1->getMasterPort(name1, i1); + BaseSlavePort& slavePort = mo2->getSlavePort(name2, i2); + + masterPort.bind(slavePort); + + return 1; +} + +void +pybind_init_pyobject(py::module &m_native) +{ + py::module m = m_native.def_submodule("pyobject"); + + m.def("connectPorts", &connectPorts); +} diff --git a/src/python/pybind11/stats.cc b/src/python/pybind11/stats.cc new file mode 100644 index 000000000..39d9ce873 --- /dev/null +++ b/src/python/pybind11/stats.cc @@ -0,0 +1,113 @@ +/* + * 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 + * 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 "base/statistics.hh" +#include "base/stats/text.hh" +#include "sim/stat_control.hh" +#include "sim/stat_register.hh" + +namespace py = pybind11; + +namespace Stats { + +void +pythonDump() +{ + py::module m = py::module::import("m5.stats"); + m.attr("dump")(); +} + +void +pythonReset() +{ + py::module m = py::module::import("m5.stats"); + m.attr("reset")(); +} + +} + +void +pybind_init_stats(py::module &m_native) +{ + py::module m = m_native.def_submodule("stats"); + + m + .def("initSimStats", &Stats::initSimStats) + .def("initText", &Stats::initText, py::return_value_policy::reference) + .def("registerPythonStatsHandlers", + &Stats::registerPythonStatsHandlers) + .def("schedStatEvent", &Stats::schedStatEvent) + .def("periodicStatDump", &Stats::periodicStatDump) + .def("updateEvents", &Stats::updateEvents) + .def("processResetQueue", &Stats::processResetQueue) + .def("processDumpQueue", &Stats::processDumpQueue) + .def("enable", &Stats::enable) + .def("enabled", &Stats::enabled) + .def("statsList", &Stats::statsList) + ; + + py::class_<Stats::Output>(m, "Output") + .def("begin", &Stats::Output::begin) + .def("end", &Stats::Output::end) + .def("valid", &Stats::Output::valid) + ; + + py::class_<Stats::Info>(m, "Info") + .def_readwrite("name", &Stats::Info::name) + .def_readonly("desc", &Stats::Info::desc) + .def_readonly("id", &Stats::Info::id) + .def_property_readonly("flags", [](const Stats::Info &info) { + return (Stats::FlagsType)info.flags; + }) + .def("check", &Stats::Info::check) + .def("baseCheck", &Stats::Info::baseCheck) + .def("enable", &Stats::Info::enable) + .def("prepare", &Stats::Info::prepare) + .def("reset", &Stats::Info::reset) + .def("zero", &Stats::Info::zero) + .def("visit", &Stats::Info::visit) + ; +} |