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__
45 #include "base/fast_alloc.hh"
46 #include "base/flags.hh"
47 #include "base/misc.hh"
48 #include "base/trace.hh"
49 #include "base/types.hh"
50 #include "sim/serialize.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 typedef short FlagsType;
69 typedef ::Flags<FlagsType> Flags;
71 static const FlagsType PublicRead = 0x003f; // public readable flags
72 static const FlagsType PublicWrite = 0x001d; // public writable flags
73 static const FlagsType Squashed = 0x0001; // has been squashed
74 static const FlagsType Scheduled = 0x0002; // has been scheduled
75 static const FlagsType AutoDelete = 0x0004; // delete after dispatch
76 static const FlagsType AutoSerialize = 0x0008; // must be serialized
77 static const FlagsType IsExitEvent = 0x0010; // special exit event
78 static const FlagsType IsMainQueue = 0x0020; // on main event queue
79 static const FlagsType Initialized = 0x7a40; // somewhat random bits
80 static const FlagsType InitMask = 0xffc0; // mask for init bits
85 return this && (flags & InitMask) == Initialized;
89 typedef int8_t Priority;
92 // The event queue is now a linked list of linked lists. The
93 // 'nextBin' pointer is to find the bin, where a bin is defined as
94 // when+priority. All events in the same bin will be stored in a
95 // second linked list (a stack) maintained by the 'nextInBin'
96 // pointer. The list will be accessed in LIFO order. The end
97 // result is that the insert/removal in 'nextBin' is
98 // linear/constant, and the lookup/removal in 'nextInBin' is
99 // constant/constant. Hopefully this is a significant improvement
100 // over the current fully linear insertion.
104 static Event *insertBefore(Event *event, Event *curr);
105 static Event *removeItem(Event *event, Event *last);
107 Tick _when; //!< timestamp when event should be processed
108 Priority _priority; //!< event priority
112 /// Global counter to generate unique IDs for Event instances
113 static Counter instanceCounter;
115 /// This event's unique ID. We can also use pointer values for
116 /// this but they're not consistent across runs making debugging
117 /// more difficult. Thus we use a global counter value when
121 /// queue to which this event belongs (though it may or may not be
122 /// scheduled on this queue yet)
127 Tick whenCreated; //!< time created
128 Tick whenScheduled; //!< time scheduled
132 setWhen(Tick when, EventQueue *q)
139 whenScheduled = curTick();
144 /// Accessor for flags.
148 return flags & PublicRead;
152 getFlags(Flags _flags) const
154 assert(_flags.noneSet(~PublicRead));
155 return flags.isSet(_flags);
159 allFlags(Flags _flags) const
161 assert(_flags.noneSet(~PublicRead));
162 return flags.allSet(_flags);
165 /// Accessor for flags.
167 setFlags(Flags _flags)
169 assert(_flags.noneSet(~PublicWrite));
174 clearFlags(Flags _flags)
176 assert(_flags.noneSet(~PublicWrite));
183 flags.clear(PublicWrite);
186 // This function isn't really useful if TRACING_ON is not defined
187 virtual void trace(const char *action); //!< trace event activity
190 /// Event priorities, to provide tie-breakers for events scheduled
191 /// at the same cycle. Most events are scheduled at the default
192 /// priority; these values are used to control events that need to
193 /// be ordered within a cycle.
196 static const Priority Minimum_Pri = SCHAR_MIN;
198 /// If we enable tracing on a particular cycle, do that as the
199 /// very first thing so we don't miss any of the events on
200 /// that cycle (even if we enter the debugger).
201 static const Priority Trace_Enable_Pri = -101;
203 /// Breakpoints should happen before anything else (except
204 /// enabling trace output), so we don't miss any action when
206 static const Priority Debug_Break_Pri = -100;
208 /// CPU switches schedule the new CPU's tick event for the
209 /// same cycle (after unscheduling the old CPU's tick event).
210 /// The switch needs to come before any tick events to make
211 /// sure we don't tick both CPUs in the same cycle.
212 static const Priority CPU_Switch_Pri = -31;
214 /// For some reason "delayed" inter-cluster writebacks are
215 /// scheduled before regular writebacks (which have default
216 /// priority). Steve?
217 static const Priority Delayed_Writeback_Pri = -1;
219 /// Default is zero for historical reasons.
220 static const Priority Default_Pri = 0;
222 /// Serailization needs to occur before tick events also, so
223 /// that a serialize/unserialize is identical to an on-line
225 static const Priority Serialize_Pri = 32;
227 /// CPU ticks must come after other associated CPU events
228 /// (such as writebacks).
229 static const Priority CPU_Tick_Pri = 50;
231 /// Statistics events (dump, reset, etc.) come after
232 /// everything else, but before exit.
233 static const Priority Stat_Event_Pri = 90;
235 /// Progress events come at the end.
236 static const Priority Progress_Event_Pri = 95;
238 /// If we want to exit on this cycle, it's the very last thing
240 static const Priority Sim_Exit_Pri = 100;
243 static const Priority Maximum_Pri = SCHAR_MAX;
247 * @param queue that the event gets scheduled on
249 Event(Priority p = Default_Pri)
250 : nextBin(NULL), nextInBin(NULL), _priority(p), flags(Initialized)
253 instance = ++instanceCounter;
257 whenCreated = curTick();
263 virtual const std::string name() const;
265 /// Return a C string describing the event. This string should
266 /// *not* be dynamically allocated; just a const char array
267 /// describing the event class.
268 virtual const char *description() const;
270 /// Dump the current event data
275 * This member function is invoked when the event is processed
276 * (occurs). There is no default implementation; each subclass
277 * must provide its own implementation. The event is not
278 * automatically deleted after it is processed (to allow for
279 * statically allocated event objects).
281 * If the AutoDestroy flag is set, the object is deleted once it
284 virtual void process() = 0;
286 /// Determine if the current event is scheduled
287 bool scheduled() const { return flags.isSet(Scheduled); }
289 /// Squash the current event
290 void squash() { flags.set(Squashed); }
292 /// Check whether the event is squashed
293 bool squashed() const { return flags.isSet(Squashed); }
295 /// See if this is a SimExitEvent (without resorting to RTTI)
296 bool isExitEvent() const { return flags.isSet(IsExitEvent); }
298 /// Get the time that the event is scheduled
299 Tick when() const { return _when; }
301 /// Get the event priority
302 Priority priority() const { return _priority; }
305 struct priority_compare
306 : public std::binary_function<Event *, Event *, bool>
309 operator()(const Event *l, const Event *r) const
311 return l->when() >= r->when() || l->priority() >= r->priority();
315 virtual void serialize(std::ostream &os);
316 virtual void unserialize(Checkpoint *cp, const std::string §ion);
322 operator<(const Event &l, const Event &r)
324 return l.when() < r.when() ||
325 (l.when() == r.when() && l.priority() < r.priority());
329 operator>(const Event &l, const Event &r)
331 return l.when() > r.when() ||
332 (l.when() == r.when() && l.priority() > r.priority());
336 operator<=(const Event &l, const Event &r)
338 return l.when() < r.when() ||
339 (l.when() == r.when() && l.priority() <= r.priority());
342 operator>=(const Event &l, const Event &r)
344 return l.when() > r.when() ||
345 (l.when() == r.when() && l.priority() >= r.priority());
349 operator==(const Event &l, const Event &r)
351 return l.when() == r.when() && l.priority() == r.priority();
355 operator!=(const Event &l, const Event &r)
357 return l.when() != r.when() || l.priority() != r.priority();
362 * Queue of events sorted in time order
364 class EventQueue : public Serializable
370 void insert(Event *event);
371 void remove(Event *event);
373 EventQueue(const EventQueue &);
374 const EventQueue &operator=(const EventQueue &);
377 EventQueue(const std::string &n);
379 virtual const std::string name() const { return objName; }
381 // schedule the given event on this queue
382 void schedule(Event *event, Tick when);
383 void deschedule(Event *event);
384 void reschedule(Event *event, Tick when, bool always = false);
386 Tick nextTick() const { return head->when(); }
389 // process all events up to the given timestamp. we inline a
390 // quick test to see if there are any events to process; if so,
391 // call the internal out-of-line version to process them all.
393 serviceEvents(Tick when)
396 if (nextTick() > when)
400 * @todo this assert is a good bug catcher. I need to
401 * make it true again.
403 //assert(head->when() >= when && "event scheduled in the past");
408 // default: process all events up to 'now' (curTick())
409 void serviceEvents() { serviceEvents(curTick()); }
411 // return true if no events are queued
412 bool empty() const { return head == NULL; }
416 Tick nextEventTime() { return empty() ? curTick() : head->when(); }
418 bool debugVerify() const;
421 virtual void serialize(std::ostream &os);
422 virtual void unserialize(Checkpoint *cp, const std::string §ion);
430 /** A pointer to this object's event queue */
434 EventManager(EventManager &em) : eventq(em.queue()) {}
435 EventManager(EventManager *em) : eventq(em ? em->queue() : NULL) {}
436 EventManager(EventQueue *eq) : eventq(eq) {}
444 operator EventQueue *() const
450 schedule(Event &event, Tick when)
452 eventq->schedule(&event, when);
456 deschedule(Event &event)
458 eventq->deschedule(&event);
462 reschedule(Event &event, Tick when, bool always = false)
464 eventq->reschedule(&event, when, always);
468 schedule(Event *event, Tick when)
470 eventq->schedule(event, when);
474 deschedule(Event *event)
476 eventq->deschedule(event);
480 reschedule(Event *event, Tick when, bool always = false)
482 eventq->reschedule(event, when, always);
487 EventQueue::schedule(Event *event, Tick when)
489 assert((UTick)when >= (UTick)curTick());
490 assert(!event->scheduled());
491 assert(event->initialized());
493 event->setWhen(when, this);
495 event->flags.set(Event::Scheduled);
496 if (this == &mainEventQueue)
497 event->flags.set(Event::IsMainQueue);
499 event->flags.clear(Event::IsMainQueue);
502 event->trace("scheduled");
506 EventQueue::deschedule(Event *event)
508 assert(event->scheduled());
509 assert(event->initialized());
513 event->flags.clear(Event::Squashed);
514 event->flags.clear(Event::Scheduled);
516 if (event->flags.isSet(Event::AutoDelete))
520 event->trace("descheduled");
524 EventQueue::reschedule(Event *event, Tick when, bool always)
526 assert(when >= curTick());
527 assert(always || event->scheduled());
528 assert(event->initialized());
530 if (event->scheduled())
533 event->setWhen(when, this);
535 event->flags.clear(Event::Squashed);
536 event->flags.set(Event::Scheduled);
537 if (this == &mainEventQueue)
538 event->flags.set(Event::IsMainQueue);
540 event->flags.clear(Event::IsMainQueue);
543 event->trace("rescheduled");
546 template <class T, void (T::* F)()>
548 DelayFunction(EventQueue *eventq, Tick when, T *object)
550 class DelayEvent : public Event
558 { this->setFlags(AutoDelete); }
559 void process() { (object->*F)(); }
560 const char *description() const { return "delay"; }
563 eventq->schedule(new DelayEvent(object), when);
566 template <class T, void (T::* F)()>
567 class EventWrapper : public Event
573 EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
574 : Event(p), object(obj)
577 setFlags(AutoDelete);
580 EventWrapper(T &obj, bool del = false, Priority p = Default_Pri)
581 : Event(p), object(&obj)
584 setFlags(AutoDelete);
587 void process() { (object->*F)(); }
592 return object->name() + ".wrapped_event";
595 const char *description() const { return "EventWrapped"; }
599 #endif // __SIM_EVENTQ_HH__