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 "base/fast_alloc.hh"
47 #include "base/misc.hh"
48 #include "base/trace.hh"
49 #include "sim/serialize.hh"
50 #include "sim/host.hh"
52 class EventQueue; // forward declaration
54 extern EventQueue mainEventQueue;
57 * An item on an event queue. The action caused by a given
58 * event is specified by deriving a subclass and overriding the
59 * process() member function.
61 * Caution, the order of members is chosen to maximize data packing.
63 class Event : public Serializable, public FastAlloc
65 friend class EventQueue;
68 // The event queue is now a linked list of linked lists. The
69 // 'nextBin' pointer is to find the bin, where a bin is defined as
70 // when+priority. All events in the same bin will be stored in a
71 // second linked list (a stack) maintained by the 'nextInBin'
72 // pointer. The list will be accessed in LIFO order. The end
73 // result is that the insert/removal in 'nextBin' is
74 // linear/constant, and the lookup/removal in 'nextInBin' is
75 // constant/constant. Hopefully this is a significant improvement
76 // over the current fully linear insertion.
80 static Event *insertBefore(Event *event, Event *curr);
81 static Event *removeItem(Event *event, Event *last);
83 Tick _when; //!< timestamp when event should be processed
84 short _priority; //!< event priority
88 /// Global counter to generate unique IDs for Event instances
89 static Counter instanceCounter;
91 /// This event's unique ID. We can also use pointer values for
92 /// this but they're not consistent across runs making debugging
93 /// more difficult. Thus we use a global counter value when
97 /// queue to which this event belongs (though it may or may not be
98 /// scheduled on this queue yet)
103 Tick whenCreated; //!< time created
104 Tick whenScheduled; //!< time scheduled
108 setWhen(Tick when, EventQueue *q)
115 whenScheduled = curTick;
130 bool getFlags(Flags f) const { return (_flags & f) == f; }
131 void setFlags(Flags f) { _flags |= f; }
132 void clearFlags(Flags f) { _flags &= ~f; }
135 // This function isn't really useful if TRACING_ON is not defined
136 virtual void trace(const char *action); //!< trace event activity
139 /// Event priorities, to provide tie-breakers for events scheduled
140 /// at the same cycle. Most events are scheduled at the default
141 /// priority; these values are used to control events that need to
142 /// be ordered within a cycle.
145 Minimum_Pri = SHRT_MIN,
147 /// If we enable tracing on a particular cycle, do that as the
148 /// very first thing so we don't miss any of the events on
149 /// that cycle (even if we enter the debugger).
150 Trace_Enable_Pri = -101,
152 /// Breakpoints should happen before anything else (except
153 /// enabling trace output), so we don't miss any action when
155 Debug_Break_Pri = -100,
157 /// CPU switches schedule the new CPU's tick event for the
158 /// same cycle (after unscheduling the old CPU's tick event).
159 /// The switch needs to come before any tick events to make
160 /// sure we don't tick both CPUs in the same cycle.
161 CPU_Switch_Pri = -31,
163 /// For some reason "delayed" inter-cluster writebacks are
164 /// scheduled before regular writebacks (which have default
165 /// priority). Steve?
166 Delayed_Writeback_Pri = -1,
168 /// Default is zero for historical reasons.
171 /// Serailization needs to occur before tick events also, so
172 /// that a serialize/unserialize is identical to an on-line
176 /// CPU ticks must come after other associated CPU events
177 /// (such as writebacks).
180 /// Statistics events (dump, reset, etc.) come after
181 /// everything else, but before exit.
184 /// Progress events come at the end.
185 Progress_Event_Pri = 95,
187 /// If we want to exit on this cycle, it's the very last thing
192 Maximum_Pri = SHRT_MAX
197 * @param queue that the event gets scheduled on
199 Event(Priority p = Default_Pri)
200 : nextBin(NULL), nextInBin(NULL), _priority(p), _flags(None)
203 instance = ++instanceCounter;
207 whenCreated = curTick;
217 virtual const std::string
221 return csprintf("Event_%d", instance);
223 return csprintf("Event_%x", (uintptr_t)this);
227 /// Return a C string describing the event. This string should
228 /// *not* be dynamically allocated; just a const char array
229 /// describing the event class.
230 virtual const char *description() const;
232 /// Dump the current event data
237 * This member function is invoked when the event is processed
238 * (occurs). There is no default implementation; each subclass
239 * must provide its own implementation. The event is not
240 * automatically deleted after it is processed (to allow for
241 * statically allocated event objects).
243 * If the AutoDestroy flag is set, the object is deleted once it
246 virtual void process() = 0;
248 /// Determine if the current event is scheduled
249 bool scheduled() const { return getFlags(Scheduled); }
251 /// Squash the current event
252 void squash() { setFlags(Squashed); }
254 /// Check whether the event is squashed
255 bool squashed() const { return getFlags(Squashed); }
257 /// See if this is a SimExitEvent (without resorting to RTTI)
258 bool isExitEvent() const { return getFlags(IsExitEvent); }
260 /// Get the time that the event is scheduled
261 Tick when() const { return _when; }
263 /// Get the event priority
264 int priority() const { return _priority; }
267 struct priority_compare
268 : public std::binary_function<Event *, Event *, bool>
271 operator()(const Event *l, const Event *r) const
273 return l->when() >= r->when() || l->priority() >= r->priority();
277 virtual void serialize(std::ostream &os);
278 virtual void unserialize(Checkpoint *cp, const std::string §ion);
283 * Queue of events sorted in time order
285 class EventQueue : public Serializable
291 void insert(Event *event);
292 void remove(Event *event);
295 EventQueue(const std::string &n)
296 : objName(n), head(NULL)
299 virtual const std::string name() const { return objName; }
301 // schedule the given event on this queue
302 void schedule(Event *event, Tick when);
303 void deschedule(Event *event);
304 void reschedule(Event *event, Tick when, bool always = false);
306 Tick nextTick() const { return head->when(); }
309 // process all events up to the given timestamp. we inline a
310 // quick test to see if there are any events to process; if so,
311 // call the internal out-of-line version to process them all.
313 serviceEvents(Tick when)
316 if (nextTick() > when)
320 * @todo this assert is a good bug catcher. I need to
321 * make it true again.
323 //assert(head->when() >= when && "event scheduled in the past");
328 // default: process all events up to 'now' (curTick)
329 void serviceEvents() { serviceEvents(curTick); }
331 // return true if no events are queued
332 bool empty() const { return head == NULL; }
336 Tick nextEventTime() { return empty() ? curTick : head->when(); }
338 bool debugVerify() const;
341 virtual void serialize(std::ostream &os);
342 virtual void unserialize(Checkpoint *cp, const std::string §ion);
350 /** A pointer to this object's event queue */
354 EventManager(EventManager &em) : eventq(em.queue()) {}
355 EventManager(EventManager *em) : eventq(em ? em->queue() : NULL) {}
356 EventManager(EventQueue *eq) : eventq(eq) {}
365 schedule(Event &event, Tick when)
367 eventq->schedule(&event, when);
371 deschedule(Event &event)
373 eventq->deschedule(&event);
377 reschedule(Event &event, Tick when, bool always = false)
379 eventq->reschedule(&event, when, always);
383 schedule(Event *event, Tick when)
385 eventq->schedule(event, when);
389 deschedule(Event *event)
391 eventq->deschedule(event);
395 reschedule(Event *event, Tick when, bool always = false)
397 eventq->reschedule(event, when, always);
401 template <class T, void (T::* F)()>
403 DelayFunction(EventQueue *eventq, Tick when, T *object)
405 class DelayEvent : public Event
413 { setFlags(this->AutoDestroy); }
414 void process() { (object->*F)(); }
415 const char *description() const { return "delay"; }
418 eventq->schedule(new DelayEvent(object), when);
421 template <class T, void (T::* F)()>
422 class EventWrapper : public Event
428 EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
429 : Event(p), object(obj)
432 setFlags(AutoDelete);
435 void process() { (object->*F)(); }
439 EventQueue::schedule(Event *event, Tick when)
441 assert(when >= curTick);
442 assert(!event->scheduled());
444 event->setWhen(when, this);
446 event->setFlags(Event::Scheduled);
447 if (this == &mainEventQueue)
448 event->setFlags(Event::IsMainQueue);
450 event->clearFlags(Event::IsMainQueue);
453 event->trace("scheduled");
457 EventQueue::deschedule(Event *event)
459 assert(event->scheduled());
463 event->clearFlags(Event::Squashed);
464 event->clearFlags(Event::Scheduled);
466 if (event->getFlags(Event::AutoDelete))
470 event->trace("descheduled");
474 EventQueue::reschedule(Event *event, Tick when, bool always)
476 assert(when >= curTick);
477 assert(always || event->scheduled());
479 if (event->scheduled())
482 event->setWhen(when, this);
484 event->clearFlags(Event::Squashed);
485 event->setFlags(Event::Scheduled);
486 if (this == &mainEventQueue)
487 event->setFlags(Event::IsMainQueue);
489 event->clearFlags(Event::IsMainQueue);
492 event->trace("rescheduled");
496 operator<(const Event &l, const Event &r)
498 return l.when() < r.when() ||
499 (l.when() == r.when() && l.priority() < r.priority());
503 operator>(const Event &l, const Event &r)
505 return l.when() > r.when() ||
506 (l.when() == r.when() && l.priority() > r.priority());
510 operator<=(const Event &l, const Event &r)
512 return l.when() < r.when() ||
513 (l.when() == r.when() && l.priority() <= r.priority());
516 operator>=(const Event &l, const Event &r)
518 return l.when() > r.when() ||
519 (l.when() == r.when() && l.priority() >= r.priority());
523 operator==(const Event &l, const Event &r)
525 return l.when() == r.when() && l.priority() == r.priority();
529 operator!=(const Event &l, const Event &r)
531 return l.when() != r.when() || l.priority() != r.priority();
535 #endif // __SIM_EVENTQ_HH__