gem5  v22.1.0.0
event.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 2021 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("setMaxTick", &set_max_tick, py::arg("tick"));
111  m.def("getMaxTick", &get_max_tick, py::return_value_policy::copy);
112  m.def("terminateEventQueueThreads", &terminateEventQueueThreads);
113  m.def("exitSimLoop", &exitSimLoop);
114  m.def("getEventQueue", []() { return curEventQueue(); },
115  py::return_value_policy::reference);
116  m.def("setEventQueue", [](EventQueue *q) { return curEventQueue(q); });
117  m.def("getEventQueue", &getEventQueue,
118  py::return_value_policy::reference);
119 
120  py::class_<EventQueue>(m, "EventQueue")
121  .def("name", [](EventQueue *eq) { return eq->name(); })
122  .def("dump", &EventQueue::dump)
123  .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
124  eq->schedule(e, t);
125  }, py::arg("event"), py::arg("when"))
126  .def("deschedule", &EventQueue::deschedule,
127  py::arg("event"))
128  .def("reschedule", &EventQueue::reschedule,
129  py::arg("event"), py::arg("tick"), py::arg("always") = false)
130  ;
131 
132  // TODO: Ownership of global exit events has always been a bit
133  // questionable. We currently assume they are owned by the C++
134  // world. This is what the old SWIG code did, but that will result
135  // in memory leaks.
136  py::class_<GlobalSimLoopExitEvent,
137  std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
138  m, "GlobalSimLoopExitEvent")
139  .def("getCause", &GlobalSimLoopExitEvent::getCause)
140  .def("getCode", &GlobalSimLoopExitEvent::getCode)
141  ;
142 
143  // Event base class. These should never be returned directly to
144  // Python since they don't have a well-defined life cycle. Python
145  // events should be derived from PyEvent instead.
146  py::class_<Event> c_event(
147  m, "Event");
148  c_event
149  .def("name", &Event::name)
150  .def("dump", &Event::dump)
151  .def("scheduled", &Event::scheduled)
152  .def("squash", &Event::squash)
153  .def("squashed", &Event::squashed)
154  .def("isExitEvent", &Event::isExitEvent)
155  .def("when", &Event::when)
156  .def("priority", &Event::priority)
157  ;
158 
159  py::class_<PyEvent, Event>(m, "PyEvent")
160  .def(py::init<Event::Priority>(),
161  py::arg("priority") = (int)Event::Default_Pri)
162  ;
163 
164 #define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
165  PRIO(Minimum_Pri);
166  PRIO(Minimum_Pri);
167  PRIO(Debug_Enable_Pri);
168  PRIO(Debug_Break_Pri);
169  PRIO(CPU_Switch_Pri);
170  PRIO(Delayed_Writeback_Pri);
171  PRIO(Default_Pri);
172  PRIO(DVFS_Update_Pri);
173  PRIO(Serialize_Pri);
174  PRIO(CPU_Tick_Pri);
175  PRIO(Stat_Event_Pri);
176  PRIO(Progress_Event_Pri);
177  PRIO(Sim_Exit_Pri);
178  PRIO(Maximum_Pri);
179 }
180 
181 } // namespace gem5
static const FlagsType Managed
Definition: eventq.hh:106
Queue of events sorted in time order.
Definition: eventq.hh:623
const std::string getCause() const
Definition: sim_events.hh:68
PyBind wrapper for Events.
Definition: event.cc:68
PyEvent(Event::Priority priority)
Definition: event.cc:70
void process() override
Definition: event.cc:75
void acquireImpl() override
Definition: event.cc:82
void releaseImpl() override
Definition: event.cc:92
int8_t Priority
Definition: eventq.hh:123
void dump() const
This is a debugging function which will print everything on the event queue.
Definition: eventq.cc:289
void reschedule(Event *event, Tick when, bool always=false)
Reschedule the specified event.
Definition: eventq.hh:821
void deschedule(Event *event)
Deschedule the specified event.
Definition: eventq.hh:797
virtual const std::string name() const
Definition: eventq.cc:84
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:465
void squash()
Squash the current event.
Definition: eventq.hh:472
void dump() const
Dump the current event data.
Definition: eventq.cc:401
Priority priority() const
Get the event priority.
Definition: eventq.hh:515
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:508
static const Priority Default_Pri
Default is zero for historical reasons.
Definition: eventq.hh:179
bool squashed() const
Check whether the event is squashed.
Definition: eventq.hh:479
bool isExitEvent() const
See if this is a SimExitEvent (without resorting to RTTI)
Definition: eventq.hh:486
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
Bitfield< 27 > q
Definition: misc_types.hh:55
Bitfield< 9 > e
Definition: misc_types.hh:65
Bitfield< 29 > eq
Definition: misc.hh:58
Bitfield< 51 > t
Definition: pagetable.hh:56
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
void set_max_tick(Tick tick)
Set the maximum tick.
Definition: simulate.cc:243
void pybind_init_event(py::module_ &m_native)
Definition: event.cc:104
Tick get_max_tick()
Get the maximum simulation tick.
Definition: simulate.cc:254
uint64_t Tick
Tick count type.
Definition: types.hh:58
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
EventQueue * curEventQueue()
Definition: eventq.hh:88
const Tick MaxTick
Definition: types.hh:60
void terminateEventQueueThreads()
Terminate helper threads when running in parallel mode.
Definition: simulate.cc:267
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Simulate for num_cycles additional cycles.
Definition: simulate.cc:188
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:62
#define PRIO(n)

Generated on Wed Dec 21 2022 10:22:39 for gem5 by doxygen 1.9.1