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;
72 static const FlagsType PublicWrite = 0x001d;
73 static const FlagsType Squashed = 0x0001;
74 static const FlagsType Scheduled = 0x0002;
75 static const FlagsType AutoDelete = 0x0004;
76 static const FlagsType AutoSerialize = 0x0008;
77 static const FlagsType IsExitEvent = 0x0010;
78 static const FlagsType IsMainQueue = 0x0020;
80 static const FlagsType Initialized = 0xf000;
84 // The event queue is now a linked list of linked lists. The
85 // 'nextBin' pointer is to find the bin, where a bin is defined as
86 // when+priority. All events in the same bin will be stored in a
87 // second linked list (a stack) maintained by the 'nextInBin'
88 // pointer. The list will be accessed in LIFO order. The end
89 // result is that the insert/removal in 'nextBin' is
90 // linear/constant, and the lookup/removal in 'nextInBin' is
91 // constant/constant. Hopefully this is a significant improvement
92 // over the current fully linear insertion.
96 static Event *insertBefore(Event *event, Event *curr);
97 static Event *removeItem(Event *event, Event *last);
99 Tick _when; //!< timestamp when event should be processed
100 short _priority; //!< event priority
104 /// Global counter to generate unique IDs for Event instances
105 static Counter instanceCounter;
107 /// This event's unique ID. We can also use pointer values for
108 /// this but they're not consistent across runs making debugging
109 /// more difficult. Thus we use a global counter value when
113 /// queue to which this event belongs (though it may or may not be
114 /// scheduled on this queue yet)
119 Tick whenCreated; //!< time created
120 Tick whenScheduled; //!< time scheduled
124 setWhen(Tick when, EventQueue *q)
131 whenScheduled = curTick;
136 /// Accessor for flags.
140 return flags & PublicRead;
144 getFlags(Flags _flags) const
146 assert(flags.noneSet(~PublicRead));
147 return flags.isSet(_flags);
151 allFlags(Flags _flags) const
153 assert(_flags.noneSet(~PublicRead));
154 return flags.allSet(_flags);
157 /// Accessor for flags.
159 setFlags(Flags _flags)
161 assert(_flags.noneSet(~PublicWrite));
166 clearFlags(Flags _flags)
168 assert(_flags.noneSet(~PublicWrite));
175 flags.clear(PublicWrite);
178 // This function isn't really useful if TRACING_ON is not defined
179 virtual void trace(const char *action); //!< trace event activity
182 /// Event priorities, to provide tie-breakers for events scheduled
183 /// at the same cycle. Most events are scheduled at the default
184 /// priority; these values are used to control events that need to
185 /// be ordered within a cycle.
188 Minimum_Pri = SHRT_MIN,
190 /// If we enable tracing on a particular cycle, do that as the
191 /// very first thing so we don't miss any of the events on
192 /// that cycle (even if we enter the debugger).
193 Trace_Enable_Pri = -101,
195 /// Breakpoints should happen before anything else (except
196 /// enabling trace output), so we don't miss any action when
198 Debug_Break_Pri = -100,
200 /// CPU switches schedule the new CPU's tick event for the
201 /// same cycle (after unscheduling the old CPU's tick event).
202 /// The switch needs to come before any tick events to make
203 /// sure we don't tick both CPUs in the same cycle.
204 CPU_Switch_Pri = -31,
206 /// For some reason "delayed" inter-cluster writebacks are
207 /// scheduled before regular writebacks (which have default
208 /// priority). Steve?
209 Delayed_Writeback_Pri = -1,
211 /// Default is zero for historical reasons.
214 /// Serailization needs to occur before tick events also, so
215 /// that a serialize/unserialize is identical to an on-line
219 /// CPU ticks must come after other associated CPU events
220 /// (such as writebacks).
223 /// Statistics events (dump, reset, etc.) come after
224 /// everything else, but before exit.
227 /// Progress events come at the end.
228 Progress_Event_Pri = 95,
230 /// If we want to exit on this cycle, it's the very last thing
235 Maximum_Pri = SHRT_MAX
240 * @param queue that the event gets scheduled on
242 Event(Priority p = Default_Pri)
243 : nextBin(NULL), nextInBin(NULL), _priority(p)
246 instance = ++instanceCounter;
250 flags.set(Initialized);
251 whenCreated = curTick;
257 virtual const std::string name() const;
259 /// Return a C string describing the event. This string should
260 /// *not* be dynamically allocated; just a const char array
261 /// describing the event class.
262 virtual const char *description() const;
264 /// Dump the current event data
269 * This member function is invoked when the event is processed
270 * (occurs). There is no default implementation; each subclass
271 * must provide its own implementation. The event is not
272 * automatically deleted after it is processed (to allow for
273 * statically allocated event objects).
275 * If the AutoDestroy flag is set, the object is deleted once it
278 virtual void process() = 0;
280 /// Determine if the current event is scheduled
281 bool scheduled() const { return flags.isSet(Scheduled); }
283 /// Squash the current event
284 void squash() { flags.set(Squashed); }
286 /// Check whether the event is squashed
287 bool squashed() const { return flags.isSet(Squashed); }
289 /// See if this is a SimExitEvent (without resorting to RTTI)
290 bool isExitEvent() const { return flags.isSet(IsExitEvent); }
292 /// Get the time that the event is scheduled
293 Tick when() const { return _when; }
295 /// Get the event priority
296 int priority() const { return _priority; }
299 struct priority_compare
300 : public std::binary_function<Event *, Event *, bool>
303 operator()(const Event *l, const Event *r) const
305 return l->when() >= r->when() || l->priority() >= r->priority();
309 virtual void serialize(std::ostream &os);
310 virtual void unserialize(Checkpoint *cp, const std::string §ion);
315 * Queue of events sorted in time order
317 class EventQueue : public Serializable
323 void insert(Event *event);
324 void remove(Event *event);
327 EventQueue(const std::string &n)
328 : objName(n), head(NULL)
331 virtual const std::string name() const { return objName; }
333 // schedule the given event on this queue
334 void schedule(Event *event, Tick when);
335 void deschedule(Event *event);
336 void reschedule(Event *event, Tick when, bool always = false);
338 Tick nextTick() const { return head->when(); }
341 // process all events up to the given timestamp. we inline a
342 // quick test to see if there are any events to process; if so,
343 // call the internal out-of-line version to process them all.
345 serviceEvents(Tick when)
348 if (nextTick() > when)
352 * @todo this assert is a good bug catcher. I need to
353 * make it true again.
355 //assert(head->when() >= when && "event scheduled in the past");
360 // default: process all events up to 'now' (curTick)
361 void serviceEvents() { serviceEvents(curTick); }
363 // return true if no events are queued
364 bool empty() const { return head == NULL; }
368 Tick nextEventTime() { return empty() ? curTick : head->when(); }
370 bool debugVerify() const;
373 virtual void serialize(std::ostream &os);
374 virtual void unserialize(Checkpoint *cp, const std::string §ion);
382 /** A pointer to this object's event queue */
386 EventManager(EventManager &em) : eventq(em.queue()) {}
387 EventManager(EventManager *em) : eventq(em ? em->queue() : NULL) {}
388 EventManager(EventQueue *eq) : eventq(eq) {}
397 schedule(Event &event, Tick when)
399 eventq->schedule(&event, when);
403 deschedule(Event &event)
405 eventq->deschedule(&event);
409 reschedule(Event &event, Tick when, bool always = false)
411 eventq->reschedule(&event, when, always);
415 schedule(Event *event, Tick when)
417 eventq->schedule(event, when);
421 deschedule(Event *event)
423 eventq->deschedule(event);
427 reschedule(Event *event, Tick when, bool always = false)
429 eventq->reschedule(event, when, always);
433 template <class T, void (T::* F)()>
435 DelayFunction(EventQueue *eventq, Tick when, T *object)
437 class DelayEvent : public Event
445 { this->setFlags(AutoDelete); }
446 void process() { (object->*F)(); }
447 const char *description() const { return "delay"; }
450 eventq->schedule(new DelayEvent(object), when);
453 template <class T, void (T::* F)()>
454 class EventWrapper : public Event
460 EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
461 : Event(p), object(obj)
464 setFlags(AutoDelete);
467 void process() { (object->*F)(); }
472 return object->name() + ".wrapped_event";
475 const char *description() const { return "EventWrapped"; }
479 EventQueue::schedule(Event *event, Tick when)
481 assert((UTick)when >= (UTick)curTick);
482 assert(!event->scheduled());
484 assert((event->flags & Event::Initialized) == Event::Initialized);
487 event->setWhen(when, this);
489 event->flags.set(Event::Scheduled);
490 if (this == &mainEventQueue)
491 event->flags.set(Event::IsMainQueue);
493 event->flags.clear(Event::IsMainQueue);
496 event->trace("scheduled");
500 EventQueue::deschedule(Event *event)
502 assert(event->scheduled());
504 assert((event->flags & Event::Initialized) == Event::Initialized);
509 event->flags.clear(Event::Squashed);
510 event->flags.clear(Event::Scheduled);
512 if (event->flags.isSet(Event::AutoDelete))
516 event->trace("descheduled");
520 EventQueue::reschedule(Event *event, Tick when, bool always)
522 assert(when >= curTick);
523 assert(always || event->scheduled());
525 assert((event->flags & Event::Initialized) == Event::Initialized);
528 if (event->scheduled())
531 event->setWhen(when, this);
533 event->flags.clear(Event::Squashed);
534 event->flags.set(Event::Scheduled);
535 if (this == &mainEventQueue)
536 event->flags.set(Event::IsMainQueue);
538 event->flags.clear(Event::IsMainQueue);
541 event->trace("rescheduled");
545 operator<(const Event &l, const Event &r)
547 return l.when() < r.when() ||
548 (l.when() == r.when() && l.priority() < r.priority());
552 operator>(const Event &l, const Event &r)
554 return l.when() > r.when() ||
555 (l.when() == r.when() && l.priority() > r.priority());
559 operator<=(const Event &l, const Event &r)
561 return l.when() < r.when() ||
562 (l.when() == r.when() && l.priority() <= r.priority());
565 operator>=(const Event &l, const Event &r)
567 return l.when() > r.when() ||
568 (l.when() == r.when() && l.priority() >= r.priority());
572 operator==(const Event &l, const Event &r)
574 return l.when() == r.when() && l.priority() == r.priority();
578 operator!=(const Event &l, const Event &r)
580 return l.when() != r.when() || l.priority() != r.priority();
584 #endif // __SIM_EVENTQ_HH__