gem5  v19.0.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  * Authors: Nathan Binkert
43  * Andreas Sandberg
44  */
45 
46 #include "pybind11/pybind11.h"
47 #include "pybind11/stl.h"
48 
49 #include "base/logging.hh"
50 #include "sim/eventq.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/simulate.hh"
54 
55 namespace py = pybind11;
56 
57 
68 class PyEvent : public Event
69 {
70  public:
72  : Event(priority, Event::Managed)
73  {
74  }
75 
76  void process() override {
77  // Call the Python implementation as __call__. This provides a
78  // slightly more Python-friendly interface.
79  PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
80  }
81 
82  protected:
83  void acquireImpl() override {
84  py::object obj = py::cast(this);
85 
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 
104 void
105 pybind_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 #if PY_MAJOR_VERSION >= 3
139  .def("getCode", &GlobalSimLoopExitEvent::getCode)
140 #else
141  // Workaround for an issue where PyBind11 converts the exit
142  // code to a long. This is normally fine, but sys.exit treats
143  // any non-int type as an error and exits with status 1 if it
144  // is passed a long.
145  .def("getCode", [](GlobalSimLoopExitEvent *e) {
146  return py::reinterpret_steal<py::object>(
147  PyInt_FromLong(e->getCode()));
148  })
149 #endif
150  ;
151 
152  // Event base class. These should never be returned directly to
153  // Python since they don't have a well-defined life cycle. Python
154  // events should be derived from PyEvent instead.
155  py::class_<Event> c_event(
156  m, "Event");
157  c_event
158  .def("name", &Event::name)
159  .def("dump", &Event::dump)
160  .def("scheduled", &Event::scheduled)
161  .def("squash", &Event::squash)
162  .def("squashed", &Event::squashed)
163  .def("isExitEvent", &Event::isExitEvent)
164  .def("when", &Event::when)
165  .def("priority", &Event::priority)
166  ;
167 
168  py::class_<PyEvent, Event>(m, "PyEvent")
169  .def(py::init<Event::Priority>(),
170  py::arg("priority") = (int)Event::Default_Pri)
171  ;
172 
173 #define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
174  PRIO(Minimum_Pri);
175  PRIO(Minimum_Pri);
180  PRIO(Default_Pri);
187  PRIO(Maximum_Pri);
188 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
static const Priority Progress_Event_Pri
Progress events come at the end.
Definition: eventq.hh:172
Bitfield< 29 > eq
Definition: miscregs.hh:50
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:64
void dump() const
Dump the current event data.
Definition: eventq.cc:400
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Simulate for num_cycles additional cycles.
Definition: simulate.cc:83
PyBind wrapper for Events.
Definition: event.cc:68
void deschedule(Event *event)
Deschedule the specified event.
Definition: eventq_impl.hh:69
Bitfield< 0 > m
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:401
static const FlagsType Managed
Definition: eventq.hh:103
void pybind_init_event(py::module &m_native)
Definition: event.cc:105
static const Priority CPU_Tick_Pri
CPU ticks must come after other associated CPU events (such as writebacks).
Definition: eventq.hh:162
static const Priority CPU_Switch_Pri
CPU switches schedule the new CPU&#39;s tick event for the same cycle (after unscheduling the old CPU&#39;s t...
Definition: eventq.hh:141
virtual const std::string name() const
Definition: eventq.cc:86
int8_t Priority
Definition: eventq.hh:117
void process() override
Definition: event.cc:76
static const Priority Debug_Break_Pri
Breakpoints should happen before anything else (except enabling trace output), so we don&#39;t miss any a...
Definition: eventq.hh:135
#define PRIO(n)
void acquireImpl() override
Definition: event.cc:83
bool squashed() const
Check whether the event is squashed.
Definition: eventq.hh:391
static const Priority Serialize_Pri
Serailization needs to occur before tick events also, so that a serialize/unserialize is identical to...
Definition: eventq.hh:158
const Tick MaxTick
Definition: types.hh:65
void reschedule(Event *event, Tick when, bool always=false)
Reschedule the specified event.
Definition: eventq_impl.hh:87
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:385
Queue of events sorted in time order.
Definition: eventq.hh:492
uint64_t Tick
Tick count type.
Definition: types.hh:63
Bitfield< 27 > q
static const Priority Sim_Exit_Pri
If we want to exit on this cycle, it&#39;s the very last thing we do.
Definition: eventq.hh:176
EventQueue * curEventQueue()
Definition: eventq.hh:85
void dump() const
Definition: eventq.cc:294
void squash()
Squash the current event.
Definition: eventq.hh:388
Priority priority() const
Get the event priority.
Definition: eventq.hh:404
PyEvent(Event::Priority priority)
Definition: event.cc:71
Bitfield< 9 > e
bool isExitEvent() const
See if this is a SimExitEvent (without resorting to RTTI)
Definition: eventq.hh:394
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:90
const std::string getCause() const
Definition: sim_events.hh:67
void releaseImpl() override
Definition: event.cc:93
Definition: eventq.hh:189
void schedule(Event *event, Tick when, bool global=false)
Schedule the given event on this queue.
Definition: eventq_impl.hh:42
static const Priority Default_Pri
Default is zero for historical reasons.
Definition: eventq.hh:149
static const Priority Minimum_Pri
Event priorities, to provide tie-breakers for events scheduled at the same cycle. ...
Definition: eventq.hh:125
virtual const std::string name() const
Definition: eventq.hh:610
static const Priority DVFS_Update_Pri
DVFS update event leads to stats dump therefore given a lower priority to ensure all relevant states ...
Definition: eventq.hh:153
Bitfield< 5 > t
static const Priority Delayed_Writeback_Pri
For some reason "delayed" inter-cluster writebacks are scheduled before regular writebacks (which hav...
Definition: eventq.hh:146
static const Priority Debug_Enable_Pri
If we enable tracing on a particular cycle, do that as the very first thing so we don&#39;t miss any of t...
Definition: eventq.hh:130
static const Priority Stat_Event_Pri
Statistics events (dump, reset, etc.) come after everything else, but before exit.
Definition: eventq.hh:169
static const Priority Maximum_Pri
Maximum priority.
Definition: eventq.hh:179

Generated on Fri Feb 28 2020 16:27:02 for gem5 by doxygen 1.8.13