2 * Copyright (c) 2003 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.
30 * EventQueue interfaces
43 #include "sim/host.hh" // for Tick
45 #include "base/fast_alloc.hh"
46 #include "sim/serialize.hh"
47 #include "base/trace.hh"
49 class EventQueue; // forward declaration
52 * An item on an event queue. The action caused by a given
53 * event is specified by deriving a subclass and overriding the
54 * process() member function.
56 class Event : public Serializable, public FastAlloc
58 friend class EventQueue;
61 /// queue to which this event belongs (though it may or may not be
62 /// scheduled on this queue yet)
67 Tick _when; //!< timestamp when event should be processed
68 int _priority; //!< event priority
80 bool getFlags(Flags f) const { return (_flags & f) == f; }
81 void setFlags(Flags f) { _flags |= f; }
82 void clearFlags(Flags f) { _flags &= ~f; }
85 EventQueue *theQueue() const { return queue; }
88 Tick when_created; //!< Keep track of creation time For debugging
89 Tick when_scheduled; //!< Keep track of creation time For debugging
91 virtual void trace(const char *action); //!< trace event activity
93 void trace(const char *) {}
96 unsigned annotated_value;
100 /// Event priorities, to provide tie-breakers for events scheduled
101 /// at the same cycle. Most events are scheduled at the default
102 /// priority; these values are used to control events that need to
103 /// be ordered within a cycle.
105 /// Breakpoints should happen before anything else, so we
106 /// don't miss any action when debugging.
107 Debug_Break_Pri = -100,
109 /// For some reason "delayed" inter-cluster writebacks are
110 /// scheduled before regular writebacks (which have default
111 /// priority). Steve?
112 Delayed_Writeback_Pri = -1,
114 /// Default is zero for historical reasons.
117 /// CPU switches schedule the new CPU's tick event for the
118 /// same cycle (after unscheduling the old CPU's tick event).
119 /// The switch needs to come before any tick events to make
120 /// sure we don't tick both CPUs in the same cycle.
123 /// Serailization needs to occur before tick events also, so
124 /// that a serialize/unserialize is identical to an on-line
128 /// CPU ticks must come after other associated CPU events
129 /// (such as writebacks).
132 /// Statistics events (dump, reset, etc.) come after
133 /// everything else, but before exit.
136 /// If we want to exit on this cycle, it's the very last thing
143 * @param queue that the event gets scheduled on
145 Event(EventQueue *q, Priority p = Default_Pri)
146 : queue(q), next(NULL), _priority(p), _flags(None),
148 when_created(curTick), when_scheduled(0),
156 virtual const std::string name() const {
157 return csprintf("Event_%x", (uintptr_t)this);
160 /// Determine if the current event is scheduled
161 bool scheduled() const { return getFlags(Scheduled); }
163 /// Schedule the event with the current priority or default priority
164 void schedule(Tick t);
166 /// Reschedule the event with the current priority
167 void reschedule(Tick t);
169 /// Remove the event from the current schedule
172 /// Return a C string describing the event. This string should
173 /// *not* be dynamically allocated; just a const char array
174 /// describing the event class.
175 virtual const char *description();
177 /// Dump the current event data
181 * This member function is invoked when the event is processed
182 * (occurs). There is no default implementation; each subclass
183 * must provide its own implementation. The event is not
184 * automatically deleted after it is processed (to allow for
185 * statically allocated event objects).
187 * If the AutoDestroy flag is set, the object is deleted once it
190 virtual void process() = 0;
192 void annotate(unsigned value) { annotated_value = value; };
193 unsigned annotation() { return annotated_value; }
195 /// Squash the current event
196 void squash() { setFlags(Squashed); }
198 /// Check whether the event is squashed
199 bool squashed() { return getFlags(Squashed); }
201 /// Get the time that the event is scheduled
202 Tick when() const { return _when; }
204 /// Get the event priority
205 int priority() const { return _priority; }
207 struct priority_compare :
208 public std::binary_function<Event *, Event *, bool>
210 bool operator()(const Event *l, const Event *r) const {
211 return l->when() >= r->when() || l->priority() >= r->priority();
215 virtual void serialize(std::ostream &os);
216 virtual void unserialize(Checkpoint *cp, const std::string §ion);
219 template <class T, void (T::* F)()>
221 DelayFunction(Tick when, T *object)
223 class DelayEvent : public Event
229 DelayEvent(Tick when, T *o)
230 : Event(&mainEventQueue), object(o)
231 { setFlags(AutoDestroy); schedule(when); }
232 void process() { (object->*F)(); }
233 const char *description() { return "delay"; }
236 new DelayEvent(when, object);
239 template <class T, void (T::* F)()>
240 class EventWrapper : public Event
246 EventWrapper(T *obj, EventQueue *q = &mainEventQueue,
247 Priority p = Default_Pri)
248 : Event(q, p), object(obj)
250 void process() { (object->*F)(); }
254 * Queue of events sorted in time order
256 class EventQueue : public Serializable
264 void insert(Event *event);
265 void remove(Event *event);
270 EventQueue(const std::string &n)
271 : objName(n), head(NULL)
274 virtual const std::string name() const { return objName; }
276 // schedule the given event on this queue
277 void schedule(Event *ev);
278 void deschedule(Event *ev);
279 void reschedule(Event *ev);
281 Tick nextTick() { return head->when(); }
284 // process all events up to the given timestamp. we inline a
285 // quick test to see if there are any events to process; if so,
286 // call the internal out-of-line version to process them all.
287 void serviceEvents(Tick when) {
289 if (nextTick() > when)
292 assert(head->when() >= when && "event scheduled in the past");
297 // default: process all events up to 'now' (curTick)
298 void serviceEvents() { serviceEvents(curTick); }
300 // return true if no events are queued
301 bool empty() { return head == NULL; }
305 Tick nextEventTime() { return empty() ? curTick : head->when(); }
307 virtual void serialize(std::ostream &os);
308 virtual void unserialize(Checkpoint *cp, const std::string §ion);
312 //////////////////////
316 // can't put these inside declaration due to circular dependence
317 // between Event and EventQueue classes.
319 //////////////////////
321 // schedule at specified time (place on event queue specified via
324 Event::schedule(Tick t)
326 assert(!scheduled());
329 when_scheduled = curTick;
332 queue->schedule(this);
340 clearFlags(Squashed);
341 clearFlags(Scheduled);
342 queue->deschedule(this);
346 Event::reschedule(Tick t)
349 clearFlags(Squashed);
352 when_scheduled = curTick;
355 queue->reschedule(this);
359 EventQueue::schedule(Event *event)
363 event->trace("scheduled");
367 EventQueue::deschedule(Event *event)
371 event->trace("descheduled");
375 EventQueue::reschedule(Event *event)
380 event->trace("rescheduled");
384 //////////////////////
388 // Events on this queue are processed at the *beginning* of each
389 // cycle, before the pipeline simulation is performed.
391 // defined in eventq.cc
393 //////////////////////
394 extern EventQueue mainEventQueue;
396 #endif // __EVENTQ_HH__