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