eventq.hh revision 4016:1d09f041eefa
1/* 2 * Copyright (c) 2000-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 28 * Authors: Steve Reinhardt 29 * Nathan Binkert 30 */ 31 32/* @file 33 * EventQueue interfaces 34 */ 35 36#ifndef __SIM_EVENTQ_HH__ 37#define __SIM_EVENTQ_HH__ 38 39#include <assert.h> 40 41#include <algorithm> 42#include <map> 43#include <string> 44#include <vector> 45 46#include "sim/host.hh" // for Tick 47 48#include "base/fast_alloc.hh" 49#include "base/misc.hh" 50#include "base/trace.hh" 51#include "sim/serialize.hh" 52 53class EventQueue; // forward declaration 54 55////////////////////// 56// 57// Main Event Queue 58// 59// Events on this queue are processed at the *beginning* of each 60// cycle, before the pipeline simulation is performed. 61// 62// defined in eventq.cc 63// 64////////////////////// 65extern EventQueue mainEventQueue; 66 67 68/* 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. 72 */ 73class Event : public Serializable, public FastAlloc 74{ 75 friend class EventQueue; 76 77 private: 78 79#ifndef NDEBUG 80 /// Global counter to generate unique IDs for Event instances 81 static Counter instanceCounter; 82 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 86 /// debugging. 87 Counter instanceId; 88#endif // NDEBUG 89 90 /// queue to which this event belongs (though it may or may not be 91 /// scheduled on this queue yet) 92 EventQueue *queue; 93 94 Event *next; 95 96 Tick _when; //!< timestamp when event should be processed 97 int _priority; //!< event priority 98 char _flags; 99 100 protected: 101 enum Flags { 102 None = 0x0, 103 Squashed = 0x1, 104 Scheduled = 0x2, 105 AutoDelete = 0x4, 106 AutoSerialize = 0x8, 107 IsExitEvent = 0x10 108 }; 109 110 bool getFlags(Flags f) const { return (_flags & f) == f; } 111 void setFlags(Flags f) { _flags |= f; } 112 void clearFlags(Flags f) { _flags &= ~f; } 113 114 protected: 115 EventQueue *theQueue() const { return queue; } 116 117#if TRACING_ON 118 Tick when_created; //!< Keep track of creation time For debugging 119 Tick when_scheduled; //!< Keep track of creation time For debugging 120 121 virtual void trace(const char *action); //!< trace event activity 122#else 123 void trace(const char *) {} 124#endif 125 126 unsigned annotated_value; 127 128 public: 129 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. 134 enum Priority { 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, 139 140 /// Breakpoints should happen before anything else (except 141 /// enabling trace output), so we don't miss any action when 142 /// debugging. 143 Debug_Break_Pri = -100, 144 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, 150 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, 155 156 /// Default is zero for historical reasons. 157 Default_Pri = 0, 158 159 /// Serailization needs to occur before tick events also, so 160 /// that a serialize/unserialize is identical to an on-line 161 /// CPU switch. 162 Serialize_Pri = 32, 163 164 /// CPU ticks must come after other associated CPU events 165 /// (such as writebacks). 166 CPU_Tick_Pri = 50, 167 168 /// Statistics events (dump, reset, etc.) come after 169 /// everything else, but before exit. 170 Stat_Event_Pri = 90, 171 172 /// If we want to exit on this cycle, it's the very last thing 173 /// we do. 174 Sim_Exit_Pri = 100 175 }; 176 177 /* 178 * Event constructor 179 * @param queue that the event gets scheduled on 180 */ 181 Event(EventQueue *q, Priority p = Default_Pri) 182 : queue(q), next(NULL), _priority(p), _flags(None), 183#if TRACING_ON 184 when_created(curTick), when_scheduled(0), 185#endif 186 annotated_value(0) 187 { 188#ifndef NDEBUG 189 instanceId = ++instanceCounter; 190#endif 191 } 192 193 ~Event() {} 194 195 virtual const std::string name() const { 196#ifndef NDEBUG 197 return csprintf("Event_%d", instanceId); 198#else 199 return csprintf("Event_%x", (uintptr_t)this); 200#endif 201 } 202 203 /// Determine if the current event is scheduled 204 bool scheduled() const { return getFlags(Scheduled); } 205 206 /// Schedule the event with the current priority or default priority 207 void schedule(Tick t); 208 209 /// Reschedule the event with the current priority 210 void reschedule(Tick t); 211 212 /// Remove the event from the current schedule 213 void deschedule(); 214 215 /// Return a C string describing the event. This string should 216 /// *not* be dynamically allocated; just a const char array 217 /// describing the event class. 218 virtual const char *description(); 219 220 /// Dump the current event data 221 void dump(); 222 223 /* 224 * This member function is invoked when the event is processed 225 * (occurs). There is no default implementation; each subclass 226 * must provide its own implementation. The event is not 227 * automatically deleted after it is processed (to allow for 228 * statically allocated event objects). 229 * 230 * If the AutoDestroy flag is set, the object is deleted once it 231 * is processed. 232 */ 233 virtual void process() = 0; 234 235 void annotate(unsigned value) { annotated_value = value; }; 236 unsigned annotation() { return annotated_value; } 237 238 /// Squash the current event 239 void squash() { setFlags(Squashed); } 240 241 /// Check whether the event is squashed 242 bool squashed() { return getFlags(Squashed); } 243 244 /// See if this is a SimExitEvent (without resorting to RTTI) 245 bool isExitEvent() { return getFlags(IsExitEvent); } 246 247 /// Get the time that the event is scheduled 248 Tick when() const { return _when; } 249 250 /// Get the event priority 251 int priority() const { return _priority; } 252 253 struct priority_compare : 254 public std::binary_function<Event *, Event *, bool> 255 { 256 bool operator()(const Event *l, const Event *r) const { 257 return l->when() >= r->when() || l->priority() >= r->priority(); 258 } 259 }; 260 261 virtual void serialize(std::ostream &os); 262 virtual void unserialize(Checkpoint *cp, const std::string §ion); 263}; 264 265template <class T, void (T::* F)()> 266void 267DelayFunction(Tick when, T *object) 268{ 269 class DelayEvent : public Event 270 { 271 private: 272 T *object; 273 274 public: 275 DelayEvent(Tick when, T *o) 276 : Event(&mainEventQueue), object(o) 277 { setFlags(this->AutoDestroy); schedule(when); } 278 void process() { (object->*F)(); } 279 const char *description() { return "delay"; } 280 }; 281 282 new DelayEvent(when, object); 283} 284 285template <class T, void (T::* F)()> 286class EventWrapper : public Event 287{ 288 private: 289 T *object; 290 291 public: 292 EventWrapper(T *obj, bool del = false, EventQueue *q = &mainEventQueue, 293 Priority p = Default_Pri) 294 : Event(q, p), object(obj) 295 { 296 if (del) 297 setFlags(AutoDelete); 298 } 299 void process() { (object->*F)(); } 300}; 301 302/* 303 * Queue of events sorted in time order 304 */ 305class EventQueue : public Serializable 306{ 307 protected: 308 std::string objName; 309 310 private: 311 Event *head; 312 313 void insert(Event *event); 314 void remove(Event *event); 315 316 public: 317 318 // constructor 319 EventQueue(const std::string &n) 320 : objName(n), head(NULL) 321 {} 322 323 virtual const std::string name() const { return objName; } 324 325 // schedule the given event on this queue 326 void schedule(Event *ev); 327 void deschedule(Event *ev); 328 void reschedule(Event *ev); 329 330 Tick nextTick() { return head->when(); } 331 Event *serviceOne(); 332 333 // process all events up to the given timestamp. we inline a 334 // quick test to see if there are any events to process; if so, 335 // call the internal out-of-line version to process them all. 336 void serviceEvents(Tick when) { 337 while (!empty()) { 338 if (nextTick() > when) 339 break; 340 341 /** 342 * @todo this assert is a good bug catcher. I need to 343 * make it true again. 344 */ 345 //assert(head->when() >= when && "event scheduled in the past"); 346 serviceOne(); 347 } 348 } 349 350 // default: process all events up to 'now' (curTick) 351 void serviceEvents() { serviceEvents(curTick); } 352 353 // return true if no events are queued 354 bool empty() { return head == NULL; } 355 356 void dump(); 357 358 Tick nextEventTime() { return empty() ? curTick : head->when(); } 359 360 virtual void serialize(std::ostream &os); 361 virtual void unserialize(Checkpoint *cp, const std::string §ion); 362}; 363 364 365////////////////////// 366// 367// inline functions 368// 369// can't put these inside declaration due to circular dependence 370// between Event and EventQueue classes. 371// 372////////////////////// 373 374// schedule at specified time (place on event queue specified via 375// constructor) 376inline void 377Event::schedule(Tick t) 378{ 379 assert(!scheduled()); 380// if (t < curTick) 381// warn("t is less than curTick, ensure you don't want cycles"); 382 383 setFlags(Scheduled); 384#if TRACING_ON 385 when_scheduled = curTick; 386#endif 387 _when = t; 388 queue->schedule(this); 389} 390 391inline void 392Event::deschedule() 393{ 394 assert(scheduled()); 395 396 clearFlags(Squashed); 397 clearFlags(Scheduled); 398 queue->deschedule(this); 399} 400 401inline void 402Event::reschedule(Tick t) 403{ 404 assert(scheduled()); 405 clearFlags(Squashed); 406 407#if TRACING_ON 408 when_scheduled = curTick; 409#endif 410 _when = t; 411 queue->reschedule(this); 412} 413 414inline void 415EventQueue::schedule(Event *event) 416{ 417 insert(event); 418 if (DTRACE(Event)) 419 event->trace("scheduled"); 420} 421 422inline void 423EventQueue::deschedule(Event *event) 424{ 425 remove(event); 426 if (DTRACE(Event)) 427 event->trace("descheduled"); 428} 429 430inline void 431EventQueue::reschedule(Event *event) 432{ 433 remove(event); 434 insert(event); 435 if (DTRACE(Event)) 436 event->trace("rescheduled"); 437} 438 439 440 441#endif // __SIM_EVENTQ_HH__ 442