event.cc revision 12041
19155SN/A/*
29155SN/A * Copyright (c) 2017 ARM Limited
39155SN/A * All rights reserved
49155SN/A *
59155SN/A * The license below extends only to copyright in the software and shall
69155SN/A * not be construed as granting a license to any other intellectual
79155SN/A * property including but not limited to intellectual property relating
89155SN/A * to a hardware implementation of the functionality of the software
99155SN/A * licensed hereunder.  You may use the software subject to the license
109155SN/A * terms below provided that you ensure that this notice is replicated
119155SN/A * unmodified and in its entirety in all distributions of the software,
129155SN/A * modified or unmodified, in source code or in binary form.
139155SN/A *
149155SN/A * Copyright (c) 2006 The Regents of The University of Michigan
159155SN/A * Copyright (c) 2013 Advanced Micro Devices, Inc.
169155SN/A * Copyright (c) 2013 Mark D. Hill and David A. Wood
179155SN/A * All rights reserved.
189155SN/A *
199155SN/A * Redistribution and use in source and binary forms, with or without
209155SN/A * modification, are permitted provided that the following conditions are
219155SN/A * met: redistributions of source code must retain the above copyright
229155SN/A * notice, this list of conditions and the following disclaimer;
239155SN/A * redistributions in binary form must reproduce the above copyright
249155SN/A * notice, this list of conditions and the following disclaimer in the
259155SN/A * documentation and/or other materials provided with the distribution;
269155SN/A * neither the name of the copyright holders nor the names of its
279155SN/A * contributors may be used to endorse or promote products derived from
289155SN/A * this software without specific prior written permission.
299155SN/A *
309155SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
319105SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
329297SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3310301Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
349297SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
359105SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
369297SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
379297SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
389105SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
399105SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
409105SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
419105SN/A *
429105SN/A * Authors: Nathan Binkert
439105SN/A *          Andreas Sandberg
449105SN/A */
459105SN/A
469105SN/A#include "pybind11/pybind11.h"
479105SN/A#include "pybind11/stl.h"
489105SN/A
499105SN/A#include "base/misc.hh"
509105SN/A#include "sim/eventq.hh"
519105SN/A#include "sim/sim_events.hh"
529105SN/A#include "sim/sim_exit.hh"
539105SN/A#include "sim/simulate.hh"
549105SN/A
559105SN/Anamespace py = pybind11;
569105SN/A
579105SN/A
589105SN/A/**
599297SN/A * PyBind wrapper for Events
609297SN/A *
619297SN/A * We need to wrap the Event class with some Python glue code to
629105SN/A * enable method overrides in Python and memory management. Unlike its
639105SN/A * C++ cousin, PyEvents need to override __call__ instead of
649297SN/A * Event::process().
659297SN/A *
669297SN/A * Memory management is done using reference counting in Python.
679105SN/A */
689105SN/Aclass PyEvent : public Event
699105SN/A{
709105SN/A  public:
719105SN/A    PyEvent(Event::Priority priority)
729297SN/A        : Event(priority, Event::Managed)
739297SN/A    {
749105SN/A    }
759105SN/A
769105SN/A    void process() override {
779105SN/A        // Call the Python implementation as __call__. This provides a
789105SN/A        // slightly more Python-friendly interface.
799105SN/A        PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
809105SN/A    }
819105SN/A
829105SN/A  protected:
839105SN/A    void acquireImpl() override {
849105SN/A        py::object obj = py::cast(this);
859105SN/A
86        if (obj) {
87            obj.inc_ref();
88        } else {
89            panic("Failed to get PyBind object to increase ref count\n");
90        }
91    }
92
93    void releaseImpl() override {
94        py::object obj = py::cast(this);
95
96        if (obj) {
97            obj.dec_ref();
98        } else {
99            panic("Failed to get PyBind object to decrease ref count\n");
100        }
101    }
102};
103
104void
105pybind_init_event(py::module &m_native)
106{
107    py::module m = m_native.def_submodule("event");
108
109    m.def("simulate", &simulate,
110          py::arg("ticks") = MaxTick);
111    m.def("exitSimLoop", &exitSimLoop);
112    m.def("getEventQueue", []() { return curEventQueue(); },
113          py::return_value_policy::reference);
114    m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
115    m.def("getEventQueue", &getEventQueue,
116          py::return_value_policy::reference);
117
118    py::class_<EventQueue>(m, "EventQueue")
119        .def("name",  [](EventQueue *eq) { return eq->name(); })
120        .def("dump", &EventQueue::dump)
121        .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
122                eq->schedule(e, t);
123            }, py::arg("event"), py::arg("when"))
124        .def("deschedule", &EventQueue::deschedule,
125             py::arg("event"))
126        .def("reschedule", &EventQueue::reschedule,
127             py::arg("event"), py::arg("tick"), py::arg("always") = false)
128        ;
129
130    // TODO: Ownership of global exit events has always been a bit
131    // questionable. We currently assume they are owned by the C++
132    // world. This is what the old SWIG code did, but that will result
133    // in memory leaks.
134    py::class_<GlobalSimLoopExitEvent,
135               std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
136               m, "GlobalSimLoopExitEvent")
137        .def("getCause", &GlobalSimLoopExitEvent::getCause)
138        .def("getCode", &GlobalSimLoopExitEvent::getCode)
139        ;
140
141    // Event base class. These should never be returned directly to
142    // Python since they don't have a well-defined life cycle. Python
143    // events should be derived from PyEvent instead.
144    py::class_<Event> c_event(
145        m, "Event");
146    c_event
147        .def("name", &Event::name)
148        .def("dump", &Event::dump)
149        .def("scheduled", &Event::scheduled)
150        .def("squash", &Event::squash)
151        .def("squashed", &Event::squashed)
152        .def("isExitEvent", &Event::isExitEvent)
153        .def("when", &Event::when)
154        .def("priority", &Event::priority)
155        ;
156
157    py::class_<PyEvent, Event>(m, "PyEvent")
158        .def(py::init<Event::Priority>(),
159             py::arg("priority") = (int)Event::Default_Pri)
160        ;
161
162#define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
163    PRIO(Minimum_Pri);
164    PRIO(Minimum_Pri);
165    PRIO(Debug_Enable_Pri);
166    PRIO(Debug_Break_Pri);
167    PRIO(CPU_Switch_Pri);
168    PRIO(Delayed_Writeback_Pri);
169    PRIO(Default_Pri);
170    PRIO(DVFS_Update_Pri);
171    PRIO(Serialize_Pri);
172    PRIO(CPU_Tick_Pri);
173    PRIO(Stat_Event_Pri);
174    PRIO(Progress_Event_Pri);
175    PRIO(Sim_Exit_Pri);
176    PRIO(Maximum_Pri);
177}
178