gem5  v21.1.0.2
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 namespace gem5
55 {
56 
67 class PyEvent : public Event
68 {
69  public:
72  {
73  }
74 
75  void process() override {
76  // Call the Python implementation as __call__. This provides a
77  // slightly more Python-friendly interface.
78  PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
79  }
80 
81  protected:
82  void acquireImpl() override {
83  py::object obj = py::cast(this);
84 
85  if (obj) {
86  obj.inc_ref();
87  } else {
88  panic("Failed to get PyBind object to increase ref count\n");
89  }
90  }
91 
92  void releaseImpl() override {
93  py::object obj = py::cast(this);
94 
95  if (obj) {
96  obj.dec_ref();
97  } else {
98  panic("Failed to get PyBind object to decrease ref count\n");
99  }
100  }
101 };
102 
103 void
104 pybind_init_event(py::module_ &m_native)
105 {
106  py::module_ m = m_native.def_submodule("event");
107 
108  m.def("simulate", &simulate,
109  py::arg("ticks") = MaxTick);
110  m.def("exitSimLoop", &exitSimLoop);
111  m.def("getEventQueue", []() { return curEventQueue(); },
112  py::return_value_policy::reference);
113  m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
114  m.def("getEventQueue", &getEventQueue,
115  py::return_value_policy::reference);
116 
117  py::class_<EventQueue>(m, "EventQueue")
118  .def("name", [](EventQueue *eq) { return eq->name(); })
119  .def("dump", &EventQueue::dump)
120  .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
121  eq->schedule(e, t);
122  }, py::arg("event"), py::arg("when"))
123  .def("deschedule", &EventQueue::deschedule,
124  py::arg("event"))
125  .def("reschedule", &EventQueue::reschedule,
126  py::arg("event"), py::arg("tick"), py::arg("always") = false)
127  ;
128 
129  // TODO: Ownership of global exit events has always been a bit
130  // questionable. We currently assume they are owned by the C++
131  // world. This is what the old SWIG code did, but that will result
132  // in memory leaks.
133  py::class_<GlobalSimLoopExitEvent,
134  std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
135  m, "GlobalSimLoopExitEvent")
136  .def("getCause", &GlobalSimLoopExitEvent::getCause)
137 #if PY_MAJOR_VERSION >= 3
138  .def("getCode", &GlobalSimLoopExitEvent::getCode)
139 #else
140  // Workaround for an issue where PyBind11 converts the exit
141  // code to a long. This is normally fine, but sys.exit treats
142  // any non-int type as an error and exits with status 1 if it
143  // is passed a long.
144  .def("getCode", [](GlobalSimLoopExitEvent *e) {
145  return py::reinterpret_steal<py::object>(
146  PyInt_FromLong(e->getCode()));
147  })
148 #endif
149  ;
150 
151  // Event base class. These should never be returned directly to
152  // Python since they don't have a well-defined life cycle. Python
153  // events should be derived from PyEvent instead.
154  py::class_<Event> c_event(
155  m, "Event");
156  c_event
157  .def("name", &Event::name)
158  .def("dump", &Event::dump)
159  .def("scheduled", &Event::scheduled)
160  .def("squash", &Event::squash)
161  .def("squashed", &Event::squashed)
162  .def("isExitEvent", &Event::isExitEvent)
163  .def("when", &Event::when)
164  .def("priority", &Event::priority)
165  ;
166 
167  py::class_<PyEvent, Event>(m, "PyEvent")
168  .def(py::init<Event::Priority>(),
169  py::arg("priority") = (int)Event::Default_Pri)
170  ;
171 
172 #define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
173  PRIO(Minimum_Pri);
174  PRIO(Minimum_Pri);
175  PRIO(Debug_Enable_Pri);
176  PRIO(Debug_Break_Pri);
177  PRIO(CPU_Switch_Pri);
178  PRIO(Delayed_Writeback_Pri);
179  PRIO(Default_Pri);
180  PRIO(DVFS_Update_Pri);
181  PRIO(Serialize_Pri);
182  PRIO(CPU_Tick_Pri);
183  PRIO(Stat_Event_Pri);
184  PRIO(Progress_Event_Pri);
185  PRIO(Sim_Exit_Pri);
186  PRIO(Maximum_Pri);
187 }
188 
189 } // namespace gem5
gem5::Event::isExitEvent
bool isExitEvent() const
See if this is a SimExitEvent (without resorting to RTTI)
Definition: eventq.hh:486
gem5::Event::when
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:508
gem5::Event::name
virtual const std::string name() const
Definition: eventq.cc:84
gem5::pybind_init_event
void pybind_init_event(py::module_ &m_native)
Definition: event.cc:104
sim_events.hh
gem5::PyEvent::PyEvent
PyEvent(Event::Priority priority)
Definition: event.cc:70
simulate.hh
gem5::getEventQueue
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:62
gem5::GlobalSimLoopExitEvent::getCode
int getCode() const
Definition: sim_events.hh:69
gem5::EventQueue::reschedule
void reschedule(Event *event, Tick when, bool always=false)
Reschedule the specified event.
Definition: eventq.hh:821
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:64
gem5::MaxTick
const Tick MaxTick
Definition: types.hh:60
gem5::PyEvent::acquireImpl
void acquireImpl() override
Definition: event.cc:82
gem5::GlobalSimLoopExitEvent
Definition: sim_events.hh:55
sim_exit.hh
gem5::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
gem5::Event::priority
Priority priority() const
Get the event priority.
Definition: eventq.hh:515
gem5::Event
Definition: eventq.hh:251
gem5::GlobalSimLoopExitEvent::getCause
const std::string getCause() const
Definition: sim_events.hh:68
gem5::EventQueue::deschedule
void deschedule(Event *event)
Deschedule the specified event.
Definition: eventq.hh:797
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::EventQueue
Queue of events sorted in time order.
Definition: eventq.hh:622
gem5::ArmISA::t
Bitfield< 5 > t
Definition: misc_types.hh:70
gem5::EventBase::Managed
static const FlagsType Managed
Definition: eventq.hh:106
gem5::PyEvent
PyBind wrapper for Events.
Definition: event.cc:67
gem5::simulate
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Simulate for num_cycles additional cycles.
Definition: simulate.cc:83
gem5::ArmISA::q
Bitfield< 27 > q
Definition: misc_types.hh:55
gem5::ArmISA::m
Bitfield< 0 > m
Definition: misc_types.hh:394
gem5::curEventQueue
EventQueue * curEventQueue()
Definition: eventq.hh:88
gem5::PowerISA::eq
Bitfield< 29 > eq
Definition: misc.hh:53
gem5::EventBase::Priority
int8_t Priority
Definition: eventq.hh:123
gem5::Event::squashed
bool squashed() const
Check whether the event is squashed.
Definition: eventq.hh:479
gem5::PyEvent::process
void process() override
Definition: event.cc:75
logging.hh
gem5::Event::squash
void squash()
Squash the current event.
Definition: eventq.hh:472
gem5::EventBase::Default_Pri
static const Priority Default_Pri
Default is zero for historical reasons.
Definition: eventq.hh:179
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::PyEvent::releaseImpl
void releaseImpl() override
Definition: event.cc:92
gem5::Event::scheduled
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:465
gem5::Event::dump
void dump() const
Dump the current event data.
Definition: eventq.cc:401
gem5::EventQueue::dump
void dump() const
This is a debugging function which will print everything on the event queue.
Definition: eventq.cc:289
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177
PRIO
#define PRIO(n)
eventq.hh

Generated on Tue Sep 21 2021 12:25:47 for gem5 by doxygen 1.8.17