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;
78 /// queue to which this event belongs (though it may or may not be
79 /// scheduled on this queue yet)
84 Tick _when; //!< timestamp when event should be processed
85 int _priority; //!< event priority
98 bool getFlags(Flags f) const { return (_flags & f) == f; }
99 void setFlags(Flags f) { _flags |= f; }
100 void clearFlags(Flags f) { _flags &= ~f; }
103 EventQueue *theQueue() const { return queue; }
106 Tick when_created; //!< Keep track of creation time For debugging
107 Tick when_scheduled; //!< Keep track of creation time For debugging
109 virtual void trace(const char *action); //!< trace event activity
111 void trace(const char *) {}
114 unsigned annotated_value;
118 /// Event priorities, to provide tie-breakers for events scheduled
119 /// at the same cycle. Most events are scheduled at the default
120 /// priority; these values are used to control events that need to
121 /// be ordered within a cycle.
123 /// Breakpoints should happen before anything else, so we
124 /// don't miss any action when debugging.
125 Debug_Break_Pri = -100,
127 /// For some reason "delayed" inter-cluster writebacks are
128 /// scheduled before regular writebacks (which have default
129 /// priority). Steve?
130 Delayed_Writeback_Pri = -1,
132 /// Default is zero for historical reasons.
135 /// CPU switches schedule the new CPU's tick event for the
136 /// same cycle (after unscheduling the old CPU's tick event).
137 /// The switch needs to come before any tick events to make
138 /// sure we don't tick both CPUs in the same cycle.
139 CPU_Switch_Pri = -31,
141 /// Serailization needs to occur before tick events also, so
142 /// that a serialize/unserialize is identical to an on-line
146 /// CPU ticks must come after other associated CPU events
147 /// (such as writebacks).
150 /// Statistics events (dump, reset, etc.) come after
151 /// everything else, but before exit.
154 /// If we want to exit on this cycle, it's the very last thing
161 * @param queue that the event gets scheduled on
163 Event(EventQueue *q, Priority p = Default_Pri)
164 : queue(q), next(NULL), _priority(p), _flags(None),
166 when_created(curTick), when_scheduled(0),
174 virtual const std::string name() const {
175 return csprintf("Event_%x", (uintptr_t)this);
178 /// Determine if the current event is scheduled
179 bool scheduled() const { return getFlags(Scheduled); }
181 /// Schedule the event with the current priority or default priority
182 void schedule(Tick t);
184 /// Reschedule the event with the current priority
185 void reschedule(Tick t);
187 /// Remove the event from the current schedule
190 /// Return a C string describing the event. This string should
191 /// *not* be dynamically allocated; just a const char array
192 /// describing the event class.
193 virtual const char *description();
195 /// Dump the current event data
199 * This member function is invoked when the event is processed
200 * (occurs). There is no default implementation; each subclass
201 * must provide its own implementation. The event is not
202 * automatically deleted after it is processed (to allow for
203 * statically allocated event objects).
205 * If the AutoDestroy flag is set, the object is deleted once it
208 virtual void process() = 0;
210 void annotate(unsigned value) { annotated_value = value; };
211 unsigned annotation() { return annotated_value; }
213 /// Squash the current event
214 void squash() { setFlags(Squashed); }
216 /// Check whether the event is squashed
217 bool squashed() { return getFlags(Squashed); }
219 /// See if this is a SimExitEvent (without resorting to RTTI)
220 bool isExitEvent() { return getFlags(IsExitEvent); }
222 /// Get the time that the event is scheduled
223 Tick when() const { return _when; }
225 /// Get the event priority
226 int priority() const { return _priority; }
228 struct priority_compare :
229 public std::binary_function<Event *, Event *, bool>
231 bool operator()(const Event *l, const Event *r) const {
232 return l->when() >= r->when() || l->priority() >= r->priority();
236 virtual void serialize(std::ostream &os);
237 virtual void unserialize(Checkpoint *cp, const std::string §ion);
240 template <class T, void (T::* F)()>
242 DelayFunction(Tick when, T *object)
244 class DelayEvent : public Event
250 DelayEvent(Tick when, T *o)
251 : Event(&mainEventQueue), object(o)
252 { setFlags(this->AutoDestroy); schedule(when); }
253 void process() { (object->*F)(); }
254 const char *description() { return "delay"; }
257 new DelayEvent(when, object);
260 template <class T, void (T::* F)()>
261 class EventWrapper : public Event
267 EventWrapper(T *obj, bool del = false, EventQueue *q = &mainEventQueue,
268 Priority p = Default_Pri)
269 : Event(q, p), object(obj)
272 setFlags(AutoDelete);
274 void process() { (object->*F)(); }
278 * Queue of events sorted in time order
280 class EventQueue : public Serializable
288 void insert(Event *event);
289 void remove(Event *event);
294 EventQueue(const std::string &n)
295 : objName(n), head(NULL)
298 virtual const std::string name() const { return objName; }
300 // schedule the given event on this queue
301 void schedule(Event *ev);
302 void deschedule(Event *ev);
303 void reschedule(Event *ev);
305 Tick nextTick() { return head->when(); }
308 // process all events up to the given timestamp. we inline a
309 // quick test to see if there are any events to process; if so,
310 // call the internal out-of-line version to process them all.
311 void serviceEvents(Tick when) {
313 if (nextTick() > when)
317 * @todo this assert is a good bug catcher. I need to
318 * make it true again.
320 //assert(head->when() >= when && "event scheduled in the past");
325 // default: process all events up to 'now' (curTick)
326 void serviceEvents() { serviceEvents(curTick); }
328 // return true if no events are queued
329 bool empty() { return head == NULL; }
333 Tick nextEventTime() { return empty() ? curTick : head->when(); }
335 virtual void serialize(std::ostream &os);
336 virtual void unserialize(Checkpoint *cp, const std::string §ion);
340 //////////////////////
344 // can't put these inside declaration due to circular dependence
345 // between Event and EventQueue classes.
347 //////////////////////
349 // schedule at specified time (place on event queue specified via
352 Event::schedule(Tick t)
354 assert(!scheduled());
356 // warn("t is less than curTick, ensure you don't want cycles");
360 when_scheduled = curTick;
363 queue->schedule(this);
371 clearFlags(Squashed);
372 clearFlags(Scheduled);
373 queue->deschedule(this);
377 Event::reschedule(Tick t)
380 clearFlags(Squashed);
383 when_scheduled = curTick;
386 queue->reschedule(this);
390 EventQueue::schedule(Event *event)
394 event->trace("scheduled");
398 EventQueue::deschedule(Event *event)
402 event->trace("descheduled");
406 EventQueue::reschedule(Event *event)
411 event->trace("rescheduled");
416 #endif // __SIM_EVENTQ_HH__