event.cc revision 11988
14486Sbinkertn@umich.edu/*
27897Shestness@cs.utexas.edu * Copyright (c) 2017 ARM Limited
34486Sbinkertn@umich.edu * All rights reserved
44486Sbinkertn@umich.edu *
54486Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
64486Sbinkertn@umich.edu * not be construed as granting a license to any other intellectual
74486Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84486Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94486Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
104486Sbinkertn@umich.edu * terms below provided that you ensure that this notice is replicated
114486Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124486Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134486Sbinkertn@umich.edu *
144486Sbinkertn@umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
154486Sbinkertn@umich.edu * Copyright (c) 2013 Advanced Micro Devices, Inc.
164486Sbinkertn@umich.edu * Copyright (c) 2013 Mark D. Hill and David A. Wood
174486Sbinkertn@umich.edu * All rights reserved.
184486Sbinkertn@umich.edu *
194486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
204486Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
214486Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
224486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
234486Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
244486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
254486Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
264486Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
274486Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
284486Sbinkertn@umich.edu * this software without specific prior written permission.
297897Shestness@cs.utexas.edu *
304486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
313102SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
326654Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
333102SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
343102SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
356654Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3610249Sstephan.diestelhorst@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
378931Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
382212SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
399524SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
409524SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
412902SN/A *
428703Sandreas.hansson@arm.com * Authors: Nathan Binkert
431783SN/A *          Andreas Sandberg
449338SAndreas.Sandberg@arm.com */
458839Sandreas.hansson@arm.com
467673Snate@binkert.org#include "pybind11/pybind11.h"
477673Snate@binkert.org#include "pybind11/stl.h"
488597Ssteve.reinhardt@amd.com
498597Ssteve.reinhardt@amd.com#include "sim/eventq.hh"
508597Ssteve.reinhardt@amd.com#include "sim/sim_events.hh"
518597Ssteve.reinhardt@amd.com#include "sim/sim_exit.hh"
528597Ssteve.reinhardt@amd.com#include "sim/simulate.hh"
538597Ssteve.reinhardt@amd.com
549524SAndreas.Sandberg@ARM.comnamespace py = pybind11;
558597Ssteve.reinhardt@amd.com
568597Ssteve.reinhardt@amd.com/**
574859Snate@binkert.org * PyBind wrapper for Events
588931Sandreas.hansson@arm.com *
598931Sandreas.hansson@arm.com * We need to wrap the Event class with some Python glue code to
602902SN/A * enable method overrides in Python and memory management. Unlike its
619408Sandreas.hansson@arm.com * C++ cousin, PyEvents need to override __call__ instead of
6210700Sandreas.hansson@arm.com * Event::process().
6310700Sandreas.hansson@arm.com *
6410700Sandreas.hansson@arm.com * Memory management is mostly done using reference counting in
6510700Sandreas.hansson@arm.com * Python. However, PyBind can't keep track of the reference the event
6610700Sandreas.hansson@arm.com * queue holds to a scheduled event. We therefore need to inhibit
6710700Sandreas.hansson@arm.com * deletion and hand over ownership to the event queue in case a
6810700Sandreas.hansson@arm.com * scheduled event loses all of its Python references.
699408Sandreas.hansson@arm.com */
709408Sandreas.hansson@arm.comclass PyEvent : public Event
719408Sandreas.hansson@arm.com{
729408Sandreas.hansson@arm.com  public:
739408Sandreas.hansson@arm.com    struct Deleter {
749814Sandreas.hansson@arm.com        void operator()(PyEvent *ev) {
759814Sandreas.hansson@arm.com            assert(!ev->isAutoDelete());
767914SBrad.Beckmann@amd.com            if (ev->scheduled()) {
778666SPrakash.Ramrakhyani@arm.com                // The event is scheduled, give ownership to the event
787914SBrad.Beckmann@amd.com                // queue.
797914SBrad.Beckmann@amd.com                ev->setFlags(Event::AutoDelete);
807914SBrad.Beckmann@amd.com            } else {
817914SBrad.Beckmann@amd.com                // The event isn't scheduled, hence Python owns it and
827914SBrad.Beckmann@amd.com                // we need to free it here.
837914SBrad.Beckmann@amd.com                delete ev;
847914SBrad.Beckmann@amd.com            }
857914SBrad.Beckmann@amd.com        }
867914SBrad.Beckmann@amd.com    };
877914SBrad.Beckmann@amd.com
887914SBrad.Beckmann@amd.com    PyEvent(Event::Priority priority)
897914SBrad.Beckmann@amd.com        : Event(priority)
907914SBrad.Beckmann@amd.com    { }
918769Sgblack@eecs.umich.edu
928769Sgblack@eecs.umich.edu    void process() override {
938769Sgblack@eecs.umich.edu        if (isAutoDelete()) {
9410282Sdam.sunwoo@arm.com            // Ownership of the event was handed over to the event queue
9510282Sdam.sunwoo@arm.com            // because the last revference in Python land was GCed. We
968769Sgblack@eecs.umich.edu            // need to claim the object again since we're creating a new
978769Sgblack@eecs.umich.edu            // Python reference.
988769Sgblack@eecs.umich.edu            clearFlags(AutoDelete);
9910037SARM gem5 Developers        }
10010037SARM gem5 Developers
10110249Sstephan.diestelhorst@arm.com        // Call the Python implementation as __call__. This provides a
10210249Sstephan.diestelhorst@arm.com        // slightly more Python-friendly interface.
10310249Sstephan.diestelhorst@arm.com        PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
10410249Sstephan.diestelhorst@arm.com    }
105};
106
107void
108pybind_init_event(py::module &m_native)
109{
110    py::module m = m_native.def_submodule("event");
111
112    m.def("simulate", &simulate,
113          py::arg("ticks") = MaxTick);
114    m.def("exitSimLoop", &exitSimLoop);
115    m.def("getEventQueue", []() { return curEventQueue(); },
116          py::return_value_policy::reference);
117    m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
118    m.def("getEventQueue", &getEventQueue,
119          py::return_value_policy::reference);
120
121    py::class_<EventQueue>(m, "EventQueue")
122        .def("name",  [](EventQueue *eq) { return eq->name(); })
123        .def("dump", &EventQueue::dump)
124        .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
125                eq->schedule(e, t);
126            }, py::arg("event"), py::arg("when"))
127        .def("deschedule", [](EventQueue *eq, PyEvent *e) {
128                eq->deschedule(e);
129            }, py::arg("event"))
130        .def("reschedule", [](EventQueue *eq, PyEvent *e, Tick t, bool alw) {
131                eq->reschedule(e, t, alw);
132            }, py::arg("event"), py::arg("tick"), py::arg("always") = false)
133        ;
134
135    // TODO: Ownership of global exit events has always been a bit
136    // questionable. We currently assume they are owned by the C++
137    // word. This is what the old SWIG code did, but that will result
138    // in memory leaks.
139    py::class_<GlobalSimLoopExitEvent,
140               std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
141               m, "GlobalSimLoopExitEvent")
142        .def("getCause", &GlobalSimLoopExitEvent::getCause)
143        .def("getCode", &GlobalSimLoopExitEvent::getCode)
144        ;
145
146    // TODO: We currently export a wrapper class and not the Event
147    // base class. This wil be problematic if we ever return an event
148    // from C++.
149    py::class_<PyEvent, std::unique_ptr<PyEvent, PyEvent::Deleter>>
150        c_event(m, "Event");
151    c_event
152        .def(py::init<Event::Priority>(),
153             py::arg("priority") = (int)Event::Default_Pri)
154        .def("name", &Event::name)
155        .def("dump", &Event::dump)
156        .def("scheduled", &Event::scheduled)
157        .def("squash", &Event::squash)
158        .def("squashed", &Event::squashed)
159        .def("isExitEvent", &Event::isExitEvent)
160        .def("isAutoDelete", &Event::isAutoDelete)
161        .def("when", &Event::when)
162        .def("priority", &Event::priority)
163        ;
164
165#define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
166    PRIO(Minimum_Pri);
167    PRIO(Minimum_Pri);
168    PRIO(Debug_Enable_Pri);
169    PRIO(Debug_Break_Pri);
170    PRIO(CPU_Switch_Pri);
171    PRIO(Delayed_Writeback_Pri);
172    PRIO(Default_Pri);
173    PRIO(DVFS_Update_Pri);
174    PRIO(Serialize_Pri);
175    PRIO(CPU_Tick_Pri);
176    PRIO(Stat_Event_Pri);
177    PRIO(Progress_Event_Pri);
178    PRIO(Sim_Exit_Pri);
179    PRIO(Maximum_Pri);
180}
181