2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Reinhardt
33 * EventQueue interfaces
36 #ifndef __SIM_EVENTQ_HH__
37 #define __SIM_EVENTQ_HH__
46 #include "sim/host.hh" // for Tick
48 #include "base/fast_alloc.hh"
49 #include "base/trace.hh"
50 #include "sim/serialize.hh"
52 class EventQueue; // forward declaration
54 //////////////////////
58 // Events on this queue are processed at the *beginning* of each
59 // cycle, before the pipeline simulation is performed.
61 // defined in eventq.cc
63 //////////////////////
64 extern EventQueue mainEventQueue;
68 * An item on an event queue. The action caused by a given
69 * event is specified by deriving a subclass and overriding the
70 * process() member function.
72 class Event : public Serializable, public FastAlloc
74 friend class EventQueue;
77 /// queue to which this event belongs (though it may or may not be
78 /// scheduled on this queue yet)
83 Tick _when; //!< timestamp when event should be processed
84 int _priority; //!< event priority
97 bool getFlags(Flags f) const { return (_flags & f) == f; }
98 void setFlags(Flags f) { _flags |= f; }
99 void clearFlags(Flags f) { _flags &= ~f; }
102 EventQueue *theQueue() const { return queue; }
105 Tick when_created; //!< Keep track of creation time For debugging
106 Tick when_scheduled; //!< Keep track of creation time For debugging
108 virtual void trace(const char *action); //!< trace event activity
110 void trace(const char *) {}
113 unsigned annotated_value;
117 /// Event priorities, to provide tie-breakers for events scheduled
118 /// at the same cycle. Most events are scheduled at the default
119 /// priority; these values are used to control events that need to
120 /// be ordered within a cycle.
122 /// Breakpoints should happen before anything else, so we
123 /// don't miss any action when debugging.
124 Debug_Break_Pri = -100,
126 /// For some reason "delayed" inter-cluster writebacks are
127 /// scheduled before regular writebacks (which have default
128 /// priority). Steve?
129 Delayed_Writeback_Pri = -1,
131 /// Default is zero for historical reasons.
134 /// CPU switches schedule the new CPU's tick event for the
135 /// same cycle (after unscheduling the old CPU's tick event).
136 /// The switch needs to come before any tick events to make
137 /// sure we don't tick both CPUs in the same cycle.
140 /// Serailization needs to occur before tick events also, so
141 /// that a serialize/unserialize is identical to an on-line
145 /// CPU ticks must come after other associated CPU events
146 /// (such as writebacks).
149 /// Statistics events (dump, reset, etc.) come after
150 /// everything else, but before exit.
153 /// If we want to exit on this cycle, it's the very last thing
160 * @param queue that the event gets scheduled on
162 Event(EventQueue *q, Priority p = Default_Pri)
163 : queue(q), next(NULL), _priority(p), _flags(None),
165 when_created(curTick), when_scheduled(0),
173 virtual const std::string name() const {
174 return csprintf("Event_%x", (uintptr_t)this);
177 /// Determine if the current event is scheduled
178 bool scheduled() const { return getFlags(Scheduled); }
180 /// Schedule the event with the current priority or default priority
181 void schedule(Tick t);
183 /// Reschedule the event with the current priority
184 void reschedule(Tick t);
186 /// Remove the event from the current schedule
189 /// Return a C string describing the event. This string should
190 /// *not* be dynamically allocated; just a const char array
191 /// describing the event class.
192 virtual const char *description();
194 /// Dump the current event data
198 * This member function is invoked when the event is processed
199 * (occurs). There is no default implementation; each subclass
200 * must provide its own implementation. The event is not
201 * automatically deleted after it is processed (to allow for
202 * statically allocated event objects).
204 * If the AutoDestroy flag is set, the object is deleted once it
207 virtual void process() = 0;
209 void annotate(unsigned value) { annotated_value = value; };
210 unsigned annotation() { return annotated_value; }
212 /// Squash the current event
213 void squash() { setFlags(Squashed); }
215 /// Check whether the event is squashed
216 bool squashed() { return getFlags(Squashed); }
218 /// See if this is a SimExitEvent (without resorting to RTTI)
219 bool isExitEvent() { return getFlags(IsExitEvent); }
221 /// Get the time that the event is scheduled
222 Tick when() const { return _when; }
224 /// Get the event priority
225 int priority() const { return _priority; }
227 struct priority_compare :
228 public std::binary_function<Event *, Event *, bool>
230 bool operator()(const Event *l, const Event *r) const {
231 return l->when() >= r->when() || l->priority() >= r->priority();
235 virtual void serialize(std::ostream &os);
236 virtual void unserialize(Checkpoint *cp, const std::string §ion);
239 template <class T, void (T::* F)()>
241 DelayFunction(Tick when, T *object)
243 class DelayEvent : public Event
249 DelayEvent(Tick when, T *o)
250 : Event(&mainEventQueue), object(o)
251 { setFlags(this->AutoDestroy); schedule(when); }
252 void process() { (object->*F)(); }
253 const char *description() { return "delay"; }
256 new DelayEvent(when, object);
259 template <class T, void (T::* F)()>
260 class EventWrapper : public Event
266 EventWrapper(T *obj, bool del = false, EventQueue *q = &mainEventQueue,
267 Priority p = Default_Pri)
268 : Event(q, p), object(obj)
271 setFlags(AutoDelete);
273 void process() { (object->*F)(); }
277 * Queue of events sorted in time order
279 class EventQueue : public Serializable
287 void insert(Event *event);
288 void remove(Event *event);
293 EventQueue(const std::string &n)
294 : objName(n), head(NULL)
297 virtual const std::string name() const { return objName; }
299 // schedule the given event on this queue
300 void schedule(Event *ev);
301 void deschedule(Event *ev);
302 void reschedule(Event *ev);
304 Tick nextTick() { return head->when(); }
307 // process all events up to the given timestamp. we inline a
308 // quick test to see if there are any events to process; if so,
309 // call the internal out-of-line version to process them all.
310 void serviceEvents(Tick when) {
312 if (nextTick() > when)
316 * @todo this assert is a good bug catcher. I need to
317 * make it true again.
319 //assert(head->when() >= when && "event scheduled in the past");
324 // default: process all events up to 'now' (curTick)
325 void serviceEvents() { serviceEvents(curTick); }
327 // return true if no events are queued
328 bool empty() { return head == NULL; }
332 Tick nextEventTime() { return empty() ? curTick : head->when(); }
334 virtual void serialize(std::ostream &os);
335 virtual void unserialize(Checkpoint *cp, const std::string §ion);
339 //////////////////////
343 // can't put these inside declaration due to circular dependence
344 // between Event and EventQueue classes.
346 //////////////////////
348 // schedule at specified time (place on event queue specified via
351 Event::schedule(Tick t)
353 assert(!scheduled());
354 assert(t >= curTick);
358 when_scheduled = curTick;
361 queue->schedule(this);
369 clearFlags(Squashed);
370 clearFlags(Scheduled);
371 queue->deschedule(this);
375 Event::reschedule(Tick t)
378 clearFlags(Squashed);
381 when_scheduled = curTick;
384 queue->reschedule(this);
388 EventQueue::schedule(Event *event)
392 event->trace("scheduled");
396 EventQueue::deschedule(Event *event)
400 event->trace("descheduled");
404 EventQueue::reschedule(Event *event)
409 event->trace("rescheduled");
414 #endif // __SIM_EVENTQ_HH__