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/misc.hh"
50 #include "base/trace.hh"
51 #include "sim/serialize.hh"
53 class EventQueue; // forward declaration
55 //////////////////////
59 // Events on this queue are processed at the *beginning* of each
60 // cycle, before the pipeline simulation is performed.
62 // defined in eventq.cc
64 //////////////////////
65 extern EventQueue mainEventQueue;
69 * An item on an event queue. The action caused by a given
70 * event is specified by deriving a subclass and overriding the
71 * process() member function.
73 class Event : public Serializable, public FastAlloc
75 friend class EventQueue;
80 /// Global counter to generate unique IDs for Event instances
81 static Counter instanceCounter;
83 /// This event's unique ID. We can also use pointer values for
84 /// this but they're not consistent across runs making debugging
85 /// more difficult. Thus we use a global counter value when
90 /// queue to which this event belongs (though it may or may not be
91 /// scheduled on this queue yet)
96 Tick _when; //!< timestamp when event should be processed
97 int _priority; //!< event priority
110 bool getFlags(Flags f) const { return (_flags & f) == f; }
111 void setFlags(Flags f) { _flags |= f; }
112 void clearFlags(Flags f) { _flags &= ~f; }
115 EventQueue *theQueue() const { return queue; }
118 Tick when_created; //!< Keep track of creation time For debugging
119 Tick when_scheduled; //!< Keep track of creation time For debugging
121 virtual void trace(const char *action); //!< trace event activity
123 void trace(const char *) {}
126 unsigned annotated_value;
130 /// Event priorities, to provide tie-breakers for events scheduled
131 /// at the same cycle. Most events are scheduled at the default
132 /// priority; these values are used to control events that need to
133 /// be ordered within a cycle.
135 /// If we enable tracing on a particular cycle, do that as the
136 /// very first thing so we don't miss any of the events on
137 /// that cycle (even if we enter the debugger).
138 Trace_Enable_Pri = -101,
140 /// Breakpoints should happen before anything else (except
141 /// enabling trace output), so we don't miss any action when
143 Debug_Break_Pri = -100,
145 /// CPU switches schedule the new CPU's tick event for the
146 /// same cycle (after unscheduling the old CPU's tick event).
147 /// The switch needs to come before any tick events to make
148 /// sure we don't tick both CPUs in the same cycle.
149 CPU_Switch_Pri = -31,
151 /// For some reason "delayed" inter-cluster writebacks are
152 /// scheduled before regular writebacks (which have default
153 /// priority). Steve?
154 Delayed_Writeback_Pri = -1,
156 /// Default is zero for historical reasons.
159 /// Serailization needs to occur before tick events also, so
160 /// that a serialize/unserialize is identical to an on-line
164 /// CPU ticks must come after other associated CPU events
165 /// (such as writebacks).
168 /// Statistics events (dump, reset, etc.) come after
169 /// everything else, but before exit.
172 /// Progress events come at the end.
173 Progress_Event_Pri = 95,
175 /// If we want to exit on this cycle, it's the very last thing
182 * @param queue that the event gets scheduled on
184 Event(EventQueue *q, Priority p = Default_Pri)
185 : queue(q), next(NULL), _priority(p), _flags(None),
187 when_created(curTick), when_scheduled(0),
192 instanceId = ++instanceCounter;
198 virtual const std::string name() const {
200 return csprintf("Event_%d", instanceId);
202 return csprintf("Event_%x", (uintptr_t)this);
206 /// Determine if the current event is scheduled
207 bool scheduled() const { return getFlags(Scheduled); }
209 /// Schedule the event with the current priority or default priority
210 void schedule(Tick t);
212 /// Reschedule the event with the current priority
213 // always parameter means to schedule if not already scheduled
214 void reschedule(Tick t, bool always = false);
216 /// Remove the event from the current schedule
219 /// Return a C string describing the event. This string should
220 /// *not* be dynamically allocated; just a const char array
221 /// describing the event class.
222 virtual const char *description();
224 /// Dump the current event data
228 * This member function is invoked when the event is processed
229 * (occurs). There is no default implementation; each subclass
230 * must provide its own implementation. The event is not
231 * automatically deleted after it is processed (to allow for
232 * statically allocated event objects).
234 * If the AutoDestroy flag is set, the object is deleted once it
237 virtual void process() = 0;
239 void annotate(unsigned value) { annotated_value = value; };
240 unsigned annotation() { return annotated_value; }
242 /// Squash the current event
243 void squash() { setFlags(Squashed); }
245 /// Check whether the event is squashed
246 bool squashed() { return getFlags(Squashed); }
248 /// See if this is a SimExitEvent (without resorting to RTTI)
249 bool isExitEvent() { return getFlags(IsExitEvent); }
251 /// Get the time that the event is scheduled
252 Tick when() const { return _when; }
254 /// Get the event priority
255 int priority() const { return _priority; }
257 struct priority_compare :
258 public std::binary_function<Event *, Event *, bool>
260 bool operator()(const Event *l, const Event *r) const {
261 return l->when() >= r->when() || l->priority() >= r->priority();
265 virtual void serialize(std::ostream &os);
266 virtual void unserialize(Checkpoint *cp, const std::string §ion);
269 template <class T, void (T::* F)()>
271 DelayFunction(Tick when, T *object)
273 class DelayEvent : public Event
279 DelayEvent(Tick when, T *o)
280 : Event(&mainEventQueue), object(o)
281 { setFlags(this->AutoDestroy); schedule(when); }
282 void process() { (object->*F)(); }
283 const char *description() { return "delay"; }
286 new DelayEvent(when, object);
289 template <class T, void (T::* F)()>
290 class EventWrapper : public Event
296 EventWrapper(T *obj, bool del = false,
297 EventQueue *q = &mainEventQueue,
298 Priority p = Default_Pri)
299 : Event(q, p), object(obj)
302 setFlags(AutoDelete);
305 EventWrapper(T *obj, Tick t, bool del = false,
306 EventQueue *q = &mainEventQueue,
307 Priority p = Default_Pri)
308 : Event(q, p), object(obj)
311 setFlags(AutoDelete);
315 void process() { (object->*F)(); }
319 * Queue of events sorted in time order
321 class EventQueue : public Serializable
329 void insert(Event *event);
330 void remove(Event *event);
335 EventQueue(const std::string &n)
336 : objName(n), head(NULL)
339 virtual const std::string name() const { return objName; }
341 // schedule the given event on this queue
342 void schedule(Event *ev);
343 void deschedule(Event *ev);
344 void reschedule(Event *ev);
346 Tick nextTick() { return head->when(); }
349 // process all events up to the given timestamp. we inline a
350 // quick test to see if there are any events to process; if so,
351 // call the internal out-of-line version to process them all.
352 void serviceEvents(Tick when) {
354 if (nextTick() > when)
358 * @todo this assert is a good bug catcher. I need to
359 * make it true again.
361 //assert(head->when() >= when && "event scheduled in the past");
366 // default: process all events up to 'now' (curTick)
367 void serviceEvents() { serviceEvents(curTick); }
369 // return true if no events are queued
370 bool empty() { return head == NULL; }
374 Tick nextEventTime() { return empty() ? curTick : head->when(); }
376 virtual void serialize(std::ostream &os);
377 virtual void unserialize(Checkpoint *cp, const std::string §ion);
381 //////////////////////
385 // can't put these inside declaration due to circular dependence
386 // between Event and EventQueue classes.
388 //////////////////////
390 // schedule at specified time (place on event queue specified via
393 Event::schedule(Tick t)
395 assert(!scheduled());
397 // warn("t is less than curTick, ensure you don't want cycles");
401 when_scheduled = curTick;
404 queue->schedule(this);
412 clearFlags(Squashed);
413 clearFlags(Scheduled);
414 queue->deschedule(this);
418 Event::reschedule(Tick t, bool always)
420 assert(scheduled() || always);
423 when_scheduled = curTick;
428 clearFlags(Squashed);
429 queue->reschedule(this);
432 queue->schedule(this);
437 EventQueue::schedule(Event *event)
441 event->trace("scheduled");
445 EventQueue::deschedule(Event *event)
449 event->trace("descheduled");
453 EventQueue::reschedule(Event *event)
458 event->trace("rescheduled");
463 #endif // __SIM_EVENTQ_HH__