eventq.hh revision 571
12SN/A/*
21762SN/A * Copyright (c) 2003 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu/* @file
302SN/A * EventQueue interfaces
312SN/A */
322SN/A
3356SN/A#ifndef __EVENTQ_HH__
341696SN/A#define __EVENTQ_HH__
352SN/A
362107SN/A#include <assert.h>
372SN/A
382SN/A#include <algorithm>
392107SN/A#include <map>
402SN/A#include <string>
412SN/A#include <vector>
422107SN/A
432SN/A#include "sim/host.hh"	// for Tick
442SN/A
452SN/A#include "base/fast_alloc.hh"
462SN/A#include "sim/serialize.hh"
472SN/A#include "base/trace.hh"
482SN/A
492SN/Aclass EventQueue;	// forward declaration
502SN/A
512SN/A/*
522SN/A * An item on an event queue.  The action caused by a given
532SN/A * event is specified by deriving a subclass and overriding the
542SN/A * process() member function.
552SN/A */
562SN/Aclass Event : public Serializable, public FastAlloc
572SN/A{
582SN/A    friend class EventQueue;
592SN/A
602SN/A  private:
612SN/A    /// queue to which this event belongs (though it may or may not be
622SN/A    /// scheduled on this queue yet)
632680Sktlim@umich.edu    EventQueue *queue;
642SN/A
652SN/A    Event *next;
662SN/A
672SN/A    Tick _when;	//!< timestamp when event should be processed
682SN/A    int _priority;	//!< event priority
692SN/A    char _flags;
702SN/A
712680Sktlim@umich.edu  protected:
722SN/A    enum Flags {
732SN/A        None = 0x0,
742SN/A        Squashed = 0x1,
752SN/A        Scheduled = 0x2,
762SN/A        AutoDelete = 0x4,
772SN/A        AutoSerialize = 0x8
783271Sgblack@eecs.umich.edu    };
793271Sgblack@eecs.umich.edu
803271Sgblack@eecs.umich.edu    bool getFlags(Flags f) const { return (_flags & f) == f; }
813271Sgblack@eecs.umich.edu    void setFlags(Flags f) { _flags |= f; }
823271Sgblack@eecs.umich.edu    void clearFlags(Flags f) { _flags &= ~f; }
833271Sgblack@eecs.umich.edu
843271Sgblack@eecs.umich.edu  protected:
85    EventQueue *theQueue() const { return queue; }
86
87#if TRACING_ON
88    Tick when_created;	//!< Keep track of creation time For debugging
89    Tick when_scheduled;	//!< Keep track of creation time For debugging
90
91    virtual void trace(const char *action);	//!< trace event activity
92#else
93    void trace(const char *) {}
94#endif
95
96    unsigned annotated_value;
97
98  public:
99
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.
104    enum Priority {
105        /// Breakpoints should happen before anything else, so we
106        /// don't miss any action when debugging.
107        Debug_Break_Pri		= -100,
108
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,
113
114        /// Default is zero for historical reasons.
115        Default_Pri		=    0,
116
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.
121        CPU_Switch_Pri		=   31,
122
123        /// Serailization needs to occur before tick events also, so
124        /// that a serialize/unserialize is identical to an on-line
125        /// CPU switch.
126        Serialize_Pri		=   32,
127
128        /// CPU ticks must come after other associated CPU events
129        /// (such as writebacks).
130        CPU_Tick_Pri		=   50,
131
132        /// Statistics events (dump, reset, etc.) come after
133        /// everything else, but before exit.
134        Stat_Event_Pri		=   90,
135
136        /// If we want to exit on this cycle, it's the very last thing
137        /// we do.
138        Sim_Exit_Pri		=  100
139    };
140
141    /*
142     * Event constructor
143     * @param queue that the event gets scheduled on
144     */
145    Event(EventQueue *q, Priority p = Default_Pri)
146        : queue(q), next(NULL), _priority(p), _flags(None),
147#if TRACING_ON
148          when_created(curTick), when_scheduled(0),
149#endif
150          annotated_value(0)
151    {
152    }
153
154    ~Event() {}
155
156    virtual const std::string name() const {
157        return csprintf("Event_%x", (uintptr_t)this);
158    }
159
160    /// Determine if the current event is scheduled
161    bool scheduled() const { return getFlags(Scheduled); }
162
163    /// Schedule the event with the current priority or default priority
164    void schedule(Tick t);
165
166    /// Reschedule the event with the current priority
167    void reschedule(Tick t);
168
169    /// Remove the event from the current schedule
170    void deschedule();
171
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();
176
177    /// Dump the current event data
178    void dump();
179
180    /*
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).
186     *
187     * If the AutoDestroy flag is set, the object is deleted once it
188     * is processed.
189     */
190    virtual void process() = 0;
191
192    void annotate(unsigned value) { annotated_value = value; };
193    unsigned annotation() { return annotated_value; }
194
195    /// Squash the current event
196    void squash() { setFlags(Squashed); }
197
198    /// Check whether the event is squashed
199    bool squashed() { return getFlags(Squashed); }
200
201    /// Get the time that the event is scheduled
202    Tick when() const { return _when; }
203
204    /// Get the event priority
205    int priority() const { return _priority; }
206
207    struct priority_compare :
208    public std::binary_function<Event *, Event *, bool>
209    {
210        bool operator()(const Event *l, const Event *r) const {
211            return l->when() >= r->when() || l->priority() >= r->priority();
212        }
213    };
214
215    virtual void serialize(std::ostream &os);
216    virtual void unserialize(Checkpoint *cp, const std::string &section);
217};
218
219template <class T, void (T::* F)()>
220void
221DelayFunction(Tick when, T *object)
222{
223    class DelayEvent : public Event
224    {
225      private:
226        T *object;
227
228      public:
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"; }
234    };
235
236    new DelayEvent(when, object);
237}
238
239template <class T, void (T::* F)()>
240class EventWrapper : public Event
241{
242  private:
243    T *object;
244
245  public:
246    EventWrapper(T *obj, EventQueue *q = &mainEventQueue,
247                 Priority p = Default_Pri)
248        : Event(q, p), object(obj)
249    {}
250    void process() { (object->*F)(); }
251};
252
253/*
254 * Queue of events sorted in time order
255 */
256class EventQueue : public Serializable
257{
258  protected:
259    std::string objName;
260
261  private:
262    Event *head;
263
264    void insert(Event *event);
265    void remove(Event *event);
266
267  public:
268
269    // constructor
270    EventQueue(const std::string &n)
271        : objName(n), head(NULL)
272    {}
273
274    virtual const std::string name() const { return objName; }
275
276    // schedule the given event on this queue
277    void schedule(Event *ev);
278    void deschedule(Event *ev);
279    void reschedule(Event *ev);
280
281    Tick nextTick() { return head->when(); }
282    void serviceOne();
283
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) {
288        while (!empty()) {
289            if (nextTick() > when)
290                break;
291
292            assert(head->when() >= when && "event scheduled in the past");
293            serviceOne();
294        }
295    }
296
297    // default: process all events up to 'now' (curTick)
298    void serviceEvents() { serviceEvents(curTick); }
299
300    // return true if no events are queued
301    bool empty() { return head == NULL; }
302
303    void dump();
304
305    Tick nextEventTime() { return empty() ? curTick : head->when(); }
306
307    virtual void serialize(std::ostream &os);
308    virtual void unserialize(Checkpoint *cp, const std::string &section);
309};
310
311
312//////////////////////
313//
314// inline functions
315//
316// can't put these inside declaration due to circular dependence
317// between Event and EventQueue classes.
318//
319//////////////////////
320
321// schedule at specified time (place on event queue specified via
322// constructor)
323inline void
324Event::schedule(Tick t)
325{
326    assert(!scheduled());
327    setFlags(Scheduled);
328#if TRACING_ON
329    when_scheduled = curTick;
330#endif
331    _when = t;
332    queue->schedule(this);
333}
334
335inline void
336Event::deschedule()
337{
338    assert(scheduled());
339
340    clearFlags(Squashed);
341    clearFlags(Scheduled);
342    queue->deschedule(this);
343}
344
345inline void
346Event::reschedule(Tick t)
347{
348    assert(scheduled());
349    clearFlags(Squashed);
350
351#if TRACING_ON
352    when_scheduled = curTick;
353#endif
354    _when = t;
355    queue->reschedule(this);
356}
357
358inline void
359EventQueue::schedule(Event *event)
360{
361    insert(event);
362    if (DTRACE(Event))
363        event->trace("scheduled");
364}
365
366inline void
367EventQueue::deschedule(Event *event)
368{
369    remove(event);
370    if (DTRACE(Event))
371        event->trace("descheduled");
372}
373
374inline void
375EventQueue::reschedule(Event *event)
376{
377    remove(event);
378    insert(event);
379    if (DTRACE(Event))
380        event->trace("rescheduled");
381}
382
383
384//////////////////////
385//
386// Main Event Queue
387//
388// Events on this queue are processed at the *beginning* of each
389// cycle, before the pipeline simulation is performed.
390//
391// defined in eventq.cc
392//
393//////////////////////
394extern EventQueue mainEventQueue;
395
396#endif // __EVENTQ_HH__
397