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/misc.hh"
47 #include "base/trace.hh"
48 #include "sim/serialize.hh"
49 #include "sim/host.hh"
51 class EventQueue; // forward declaration
53 //////////////////////
57 // Events on this queue are processed at the *beginning* of each
58 // cycle, before the pipeline simulation is performed.
60 // defined in eventq.cc
62 //////////////////////
63 extern EventQueue mainEventQueue;
66 * An item on an event queue. The action caused by a given
67 * event is specified by deriving a subclass and overriding the
68 * process() member function.
70 * Caution, the order of members is chosen to maximize data packing.
72 class Event : public Serializable, public FastAlloc
74 friend class EventQueue;
79 /// queue to which this event belongs (though it may or may not be
80 /// scheduled on this queue yet)
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
99 Tick whenCreated; //!< time created
100 Tick whenScheduled; //!< time scheduled
112 bool getFlags(Flags f) const { return (_flags & f) == f; }
113 void setFlags(Flags f) { _flags |= f; }
114 void clearFlags(Flags f) { _flags &= ~f; }
117 EventQueue *queue() const { return _queue; }
119 // This function isn't really useful if TRACING_ON is not defined
120 virtual void trace(const char *action); //!< trace event activity
123 /// Event priorities, to provide tie-breakers for events scheduled
124 /// at the same cycle. Most events are scheduled at the default
125 /// priority; these values are used to control events that need to
126 /// be ordered within a cycle.
129 Minimum_Pri = SHRT_MIN,
131 /// If we enable tracing on a particular cycle, do that as the
132 /// very first thing so we don't miss any of the events on
133 /// that cycle (even if we enter the debugger).
134 Trace_Enable_Pri = -101,
136 /// Breakpoints should happen before anything else (except
137 /// enabling trace output), so we don't miss any action when
139 Debug_Break_Pri = -100,
141 /// CPU switches schedule the new CPU's tick event for the
142 /// same cycle (after unscheduling the old CPU's tick event).
143 /// The switch needs to come before any tick events to make
144 /// sure we don't tick both CPUs in the same cycle.
145 CPU_Switch_Pri = -31,
147 /// For some reason "delayed" inter-cluster writebacks are
148 /// scheduled before regular writebacks (which have default
149 /// priority). Steve?
150 Delayed_Writeback_Pri = -1,
152 /// Default is zero for historical reasons.
155 /// Serailization needs to occur before tick events also, so
156 /// that a serialize/unserialize is identical to an on-line
160 /// CPU ticks must come after other associated CPU events
161 /// (such as writebacks).
164 /// Statistics events (dump, reset, etc.) come after
165 /// everything else, but before exit.
168 /// Progress events come at the end.
169 Progress_Event_Pri = 95,
171 /// If we want to exit on this cycle, it's the very last thing
176 Maximum_Pri = SHRT_MAX
181 * @param queue that the event gets scheduled on
183 Event(EventQueue *q, Priority p = Default_Pri)
184 : next(NULL), _queue(q), _priority(p), _flags(None)
187 instance = ++instanceCounter;
190 whenCreated = curTick;
200 virtual const std::string
204 return csprintf("Event_%d", instance);
206 return csprintf("Event_%x", (uintptr_t)this);
210 /// Return a C string describing the event. This string should
211 /// *not* be dynamically allocated; just a const char array
212 /// describing the event class.
213 virtual const char *description() const;
215 /// Dump the current event data
220 * This member function is invoked when the event is processed
221 * (occurs). There is no default implementation; each subclass
222 * must provide its own implementation. The event is not
223 * automatically deleted after it is processed (to allow for
224 * statically allocated event objects).
226 * If the AutoDestroy flag is set, the object is deleted once it
229 virtual void process() = 0;
231 /// Determine if the current event is scheduled
232 bool scheduled() const { return getFlags(Scheduled); }
234 /// Schedule the event with the current priority or default priority
235 void schedule(Tick t);
237 /// Reschedule the event with the current priority
238 // always parameter means to schedule if not already scheduled
239 void reschedule(Tick t, bool always = false);
241 /// Remove the event from the current schedule
244 /// Squash the current event
245 void squash() { setFlags(Squashed); }
247 /// Check whether the event is squashed
248 bool squashed() const { return getFlags(Squashed); }
250 /// See if this is a SimExitEvent (without resorting to RTTI)
251 bool isExitEvent() const { return getFlags(IsExitEvent); }
253 /// Get the time that the event is scheduled
254 Tick when() const { return _when; }
256 /// Get the event priority
257 int priority() const { return _priority; }
259 struct priority_compare
260 : public std::binary_function<Event *, Event *, bool>
263 operator()(const Event *l, const Event *r) const
265 return l->when() >= r->when() || l->priority() >= r->priority();
269 virtual void serialize(std::ostream &os);
270 virtual void unserialize(Checkpoint *cp, const std::string §ion);
273 template <class T, void (T::* F)()>
275 DelayFunction(Tick when, T *object)
277 class DelayEvent : public Event
283 DelayEvent(Tick when, T *o)
284 : Event(&mainEventQueue), object(o)
285 { setFlags(this->AutoDestroy); schedule(when); }
286 void process() { (object->*F)(); }
287 const char *description() const { return "delay"; }
290 new DelayEvent(when, object);
293 template <class T, void (T::* F)()>
294 class EventWrapper : public Event
300 EventWrapper(T *obj, bool del = false,
301 EventQueue *q = &mainEventQueue,
302 Priority p = Default_Pri)
303 : Event(q, p), object(obj)
306 setFlags(AutoDelete);
309 EventWrapper(T *obj, Tick t, bool del = false,
310 EventQueue *q = &mainEventQueue,
311 Priority p = Default_Pri)
312 : Event(q, p), object(obj)
315 setFlags(AutoDelete);
319 void process() { (object->*F)(); }
323 * Queue of events sorted in time order
325 class EventQueue : public Serializable
333 void insert(Event *event);
334 void remove(Event *event);
339 EventQueue(const std::string &n)
340 : objName(n), head(NULL)
343 virtual const std::string name() const { return objName; }
345 // schedule the given event on this queue
346 void schedule(Event *ev);
347 void deschedule(Event *ev);
348 void reschedule(Event *ev);
350 Tick nextTick() const { return head->when(); }
353 // process all events up to the given timestamp. we inline a
354 // quick test to see if there are any events to process; if so,
355 // call the internal out-of-line version to process them all.
357 serviceEvents(Tick when)
360 if (nextTick() > when)
364 * @todo this assert is a good bug catcher. I need to
365 * make it true again.
367 //assert(head->when() >= when && "event scheduled in the past");
372 // default: process all events up to 'now' (curTick)
373 void serviceEvents() { serviceEvents(curTick); }
375 // return true if no events are queued
376 bool empty() const { return head == NULL; }
380 Tick nextEventTime() { return empty() ? curTick : head->when(); }
382 virtual void serialize(std::ostream &os);
383 virtual void unserialize(Checkpoint *cp, const std::string §ion);
387 //////////////////////
391 // can't put these inside declaration due to circular dependence
392 // between Event and EventQueue classes.
394 //////////////////////
396 // schedule at specified time (place on event queue specified via
399 Event::schedule(Tick t)
401 assert(!scheduled());
402 assert(t >= curTick);
406 whenScheduled = curTick;
409 _queue->schedule(this);
417 clearFlags(Squashed);
418 clearFlags(Scheduled);
419 _queue->deschedule(this);
423 Event::reschedule(Tick t, bool always)
425 assert(scheduled() || always);
426 assert(t >= curTick);
429 whenScheduled = curTick;
434 clearFlags(Squashed);
435 _queue->reschedule(this);
438 _queue->schedule(this);
443 EventQueue::schedule(Event *event)
447 event->trace("scheduled");
451 EventQueue::deschedule(Event *event)
455 event->trace("descheduled");
457 if (event->getFlags(Event::AutoDelete))
462 EventQueue::reschedule(Event *event)
467 event->trace("rescheduled");
470 #endif // __SIM_EVENTQ_HH__