gem5  v20.1.0.0
event.cc
Go to the documentation of this file.
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) 2006 The Regents of The University of Michigan
15  * Copyright (c) 2013 Advanced Micro Devices, Inc.
16  * Copyright (c) 2013 Mark D. Hill and David A. Wood
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are
21  * met: redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer;
23  * redistributions in binary form must reproduce the above copyright
24  * notice, this list of conditions and the following disclaimer in the
25  * documentation and/or other materials provided with the distribution;
26  * neither the name of the copyright holders nor the names of its
27  * contributors may be used to endorse or promote products derived from
28  * this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 #include "pybind11/pybind11.h"
44 #include "pybind11/stl.h"
45 
46 #include "base/logging.hh"
47 #include "sim/eventq.hh"
48 #include "sim/sim_events.hh"
49 #include "sim/sim_exit.hh"
50 #include "sim/simulate.hh"
51 
52 namespace py = pybind11;
53 
54 
65 class PyEvent : public Event
66 {
67  public:
70  {
71  }
72 
73  void process() override {
74  // Call the Python implementation as __call__. This provides a
75  // slightly more Python-friendly interface.
76  PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
77  }
78 
79  protected:
80  void acquireImpl() override {
81  py::object obj = py::cast(this);
82 
83  if (obj) {
84  obj.inc_ref();
85  } else {
86  panic("Failed to get PyBind object to increase ref count\n");
87  }
88  }
89 
90  void releaseImpl() override {
91  py::object obj = py::cast(this);
92 
93  if (obj) {
94  obj.dec_ref();
95  } else {
96  panic("Failed to get PyBind object to decrease ref count\n");
97  }
98  }
99 };
100 
101 void
102 pybind_init_event(py::module &m_native)
103 {
104  py::module m = m_native.def_submodule("event");
105 
106  m.def("simulate", &simulate,
107  py::arg("ticks") = MaxTick);
108  m.def("exitSimLoop", &exitSimLoop);
109  m.def("getEventQueue", []() { return curEventQueue(); },
110  py::return_value_policy::reference);
111  m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
112  m.def("getEventQueue", &getEventQueue,
113  py::return_value_policy::reference);
114 
115  py::class_<EventQueue>(m, "EventQueue")
116  .def("name", [](EventQueue *eq) { return eq->name(); })
117  .def("dump", &EventQueue::dump)
118  .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
119  eq->schedule(e, t);
120  }, py::arg("event"), py::arg("when"))
121  .def("deschedule", &EventQueue::deschedule,
122  py::arg("event"))
123  .def("reschedule", &EventQueue::reschedule,
124  py::arg("event"), py::arg("tick"), py::arg("always") = false)
125  ;
126 
127  // TODO: Ownership of global exit events has always been a bit
128  // questionable. We currently assume they are owned by the C++
129  // world. This is what the old SWIG code did, but that will result
130  // in memory leaks.
131  py::class_<GlobalSimLoopExitEvent,
132  std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
133  m, "GlobalSimLoopExitEvent")
134  .def("getCause", &GlobalSimLoopExitEvent::getCause)
135 #if PY_MAJOR_VERSION >= 3
136  .def("getCode", &GlobalSimLoopExitEvent::getCode)
137 #else
138  // Workaround for an issue where PyBind11 converts the exit
139  // code to a long. This is normally fine, but sys.exit treats
140  // any non-int type as an error and exits with status 1 if it
141  // is passed a long.
142  .def("getCode", [](GlobalSimLoopExitEvent *e) {
143  return py::reinterpret_steal<py::object>(
144  PyInt_FromLong(e->getCode()));
145  })
146 #endif
147  ;
148 
149  // Event base class. These should never be returned directly to
150  // Python since they don't have a well-defined life cycle. Python
151  // events should be derived from PyEvent instead.
152  py::class_<Event> c_event(
153  m, "Event");
154  c_event
155  .def("name", &Event::name)
156  .def("dump", &Event::dump)
157  .def("scheduled", &Event::scheduled)
158  .def("squash", &Event::squash)
159  .def("squashed", &Event::squashed)
160  .def("isExitEvent", &Event::isExitEvent)
161  .def("when", &Event::when)
162  .def("priority", &Event::priority)
163  ;
164 
165  py::class_<PyEvent, Event>(m, "PyEvent")
166  .def(py::init<Event::Priority>(),
167  py::arg("priority") = (int)Event::Default_Pri)
168  ;
169 
170 #define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
171  PRIO(Minimum_Pri);
172  PRIO(Minimum_Pri);
173  PRIO(Debug_Enable_Pri);
174  PRIO(Debug_Break_Pri);
175  PRIO(CPU_Switch_Pri);
176  PRIO(Delayed_Writeback_Pri);
177  PRIO(Default_Pri);
178  PRIO(DVFS_Update_Pri);
179  PRIO(Serialize_Pri);
180  PRIO(CPU_Tick_Pri);
181  PRIO(Stat_Event_Pri);
182  PRIO(Progress_Event_Pri);
183  PRIO(Sim_Exit_Pri);
184  PRIO(Maximum_Pri);
185 }
Event::scheduled
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:460
pybind_init_event
void pybind_init_event(py::module &m_native)
Definition: event.cc:102
PyEvent::process
void process() override
Definition: event.cc:73
getEventQueue
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:61
EventQueue::reschedule
void reschedule(Event *event, Tick when, bool always=false)
Reschedule the specified event.
Definition: eventq.hh:814
Event::isExitEvent
bool isExitEvent() const
See if this is a SimExitEvent (without resorting to RTTI)
Definition: eventq.hh:481
Event::squashed
bool squashed() const
Check whether the event is squashed.
Definition: eventq.hh:474
sim_events.hh
simulate.hh
PyEvent::acquireImpl
void acquireImpl() override
Definition: event.cc:80
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
ArmISA::q
Bitfield< 27 > q
Definition: miscregs_types.hh:52
Event::dump
void dump() const
Dump the current event data.
Definition: eventq.cc:404
EventQueue::dump
void dump() const
This is a debugging function which will print everything on the event queue.
Definition: eventq.cc:288
Event::when
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:503
sim_exit.hh
PyEvent::PyEvent
PyEvent(Event::Priority priority)
Definition: event.cc:68
EventBase::Managed
static const FlagsType Managed
Definition: eventq.hh:101
PyEvent::releaseImpl
void releaseImpl() override
Definition: event.cc:90
Event
Definition: eventq.hh:246
GlobalSimLoopExitEvent::getCode
int getCode() const
Definition: sim_events.hh:66
GlobalSimLoopExitEvent
Definition: sim_events.hh:52
exitSimLoop
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
Schedule an event to exit the simulation loop (returning to Python) at the end of the current cycle (...
Definition: sim_events.cc:88
curEventQueue
EventQueue * curEventQueue()
Definition: eventq.hh:83
simulate
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Simulate for num_cycles additional cycles.
Definition: simulate.cc:80
Event::priority
Priority priority() const
Get the event priority.
Definition: eventq.hh:510
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
PowerISA::eq
Bitfield< 29 > eq
Definition: miscregs.hh:48
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
PyEvent
PyBind wrapper for Events.
Definition: event.cc:65
GlobalSimLoopExitEvent::getCause
const std::string getCause() const
Definition: sim_events.hh:65
EventQueue::deschedule
void deschedule(Event *event)
Deschedule the specified event.
Definition: eventq.hh:790
EventBase::Default_Pri
static const Priority Default_Pri
Default is zero for historical reasons.
Definition: eventq.hh:174
Event::squash
void squash()
Squash the current event.
Definition: eventq.hh:467
logging.hh
EventBase::Priority
int8_t Priority
Definition: eventq.hh:118
EventQueue
Queue of events sorted in time order.
Definition: eventq.hh:617
Event::name
virtual const std::string name() const
Definition: eventq.cc:83
MaxTick
const Tick MaxTick
Definition: types.hh:65
ArmISA::m
Bitfield< 0 > m
Definition: miscregs_types.hh:389
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
PRIO
#define PRIO(n)
eventq.hh

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17