eventq.cc revision 8232
1/* 2 * Copyright (c) 2000-2005 The Regents of The University of Michigan 3 * Copyright (c) 2008 The Hewlett-Packard Development Company 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * Authors: Steve Reinhardt 30 * Nathan Binkert 31 * Steve Raasch 32 */ 33 34#include <cassert> 35#include <iostream> 36#include <string> 37#include <vector> 38 39#include "base/hashmap.hh" 40#include "base/misc.hh" 41#include "base/trace.hh" 42#include "cpu/smt.hh" 43#include "debug/Config.hh" 44#include "sim/core.hh" 45#include "sim/eventq.hh" 46 47using namespace std; 48 49// 50// Main Event Queue 51// 52// Events on this queue are processed at the *beginning* of each 53// cycle, before the pipeline simulation is performed. 54// 55EventQueue mainEventQueue("Main Event Queue"); 56 57#ifndef NDEBUG 58Counter Event::instanceCounter = 0; 59#endif 60 61Event::~Event() 62{ 63 assert(!scheduled()); 64 flags = 0; 65} 66 67const std::string 68Event::name() const 69{ 70#ifndef NDEBUG 71 return csprintf("Event_%d", instance); 72#else 73 return csprintf("Event_%x", (uintptr_t)this); 74#endif 75} 76 77 78Event * 79Event::insertBefore(Event *event, Event *curr) 80{ 81 // Either way, event will be the top element in the 'in bin' list 82 // which is the pointer we need in order to look into the list, so 83 // we need to insert that into the bin list. 84 if (!curr || *event < *curr) { 85 // Insert the event before the current list since it is in the future. 86 event->nextBin = curr; 87 event->nextInBin = NULL; 88 } else { 89 // Since we're on the correct list, we need to point to the next list 90 event->nextBin = curr->nextBin; // curr->nextBin can now become stale 91 92 // Insert event at the top of the stack 93 event->nextInBin = curr; 94 } 95 96 return event; 97} 98 99void 100EventQueue::insert(Event *event) 101{ 102 // Deal with the head case 103 if (!head || *event <= *head) { 104 head = Event::insertBefore(event, head); 105 return; 106 } 107 108 // Figure out either which 'in bin' list we are on, or where a new list 109 // needs to be inserted 110 Event *prev = head; 111 Event *curr = head->nextBin; 112 while (curr && *curr < *event) { 113 prev = curr; 114 curr = curr->nextBin; 115 } 116 117 // Note: this operation may render all nextBin pointers on the 118 // prev 'in bin' list stale (except for the top one) 119 prev->nextBin = Event::insertBefore(event, curr); 120} 121 122Event * 123Event::removeItem(Event *event, Event *top) 124{ 125 Event *curr = top; 126 Event *next = top->nextInBin; 127 128 // if we removed the top item, we need to handle things specially 129 // and just remove the top item, fixing up the next bin pointer of 130 // the new top item 131 if (event == top) { 132 if (!next) 133 return top->nextBin; 134 next->nextBin = top->nextBin; 135 return next; 136 } 137 138 // Since we already checked the current element, we're going to 139 // keep checking event against the next element. 140 while (event != next) { 141 if (!next) 142 panic("event not found!"); 143 144 curr = next; 145 next = next->nextInBin; 146 } 147 148 // remove next from the 'in bin' list since it's what we're looking for 149 curr->nextInBin = next->nextInBin; 150 return top; 151} 152 153void 154EventQueue::remove(Event *event) 155{ 156 if (head == NULL) 157 panic("event not found!"); 158 159 // deal with an event on the head's 'in bin' list (event has the same 160 // time as the head) 161 if (*head == *event) { 162 head = Event::removeItem(event, head); 163 return; 164 } 165 166 // Find the 'in bin' list that this event belongs on 167 Event *prev = head; 168 Event *curr = head->nextBin; 169 while (curr && *curr < *event) { 170 prev = curr; 171 curr = curr->nextBin; 172 } 173 174 if (!curr || *curr != *event) 175 panic("event not found!"); 176 177 // curr points to the top item of the the correct 'in bin' list, when 178 // we remove an item, it returns the new top item (which may be 179 // unchanged) 180 prev->nextBin = Event::removeItem(event, curr); 181} 182 183Event * 184EventQueue::serviceOne() 185{ 186 Event *event = head; 187 Event *next = head->nextInBin; 188 event->flags.clear(Event::Scheduled); 189 190 if (next) { 191 // update the next bin pointer since it could be stale 192 next->nextBin = head->nextBin; 193 194 // pop the stack 195 head = next; 196 } else { 197 // this was the only element on the 'in bin' list, so get rid of 198 // the 'in bin' list and point to the next bin list 199 head = head->nextBin; 200 } 201 202 // handle action 203 if (!event->squashed()) { 204 event->process(); 205 if (event->isExitEvent()) { 206 assert(!event->flags.isSet(Event::AutoDelete)); // would be silly 207 return event; 208 } 209 } else { 210 event->flags.clear(Event::Squashed); 211 } 212 213 if (event->flags.isSet(Event::AutoDelete) && !event->scheduled()) 214 delete event; 215 216 return NULL; 217} 218 219void 220Event::serialize(std::ostream &os) 221{ 222 SERIALIZE_SCALAR(_when); 223 SERIALIZE_SCALAR(_priority); 224 short _flags = flags; 225 SERIALIZE_SCALAR(_flags); 226} 227 228void 229Event::unserialize(Checkpoint *cp, const string §ion) 230{ 231 if (scheduled()) 232 mainEventQueue.deschedule(this); 233 234 UNSERIALIZE_SCALAR(_when); 235 UNSERIALIZE_SCALAR(_priority); 236 237 short _flags; 238 UNSERIALIZE_SCALAR(_flags); 239 240 // Old checkpoints had no concept of the Initialized flag 241 // so restoring from old checkpoints always fail. 242 // Events are initialized on construction but original code 243 // "flags = _flags" would just overwrite the initialization. 244 // So, read in the checkpoint flags, but then set the Initialized 245 // flag on top of it in order to avoid failures. 246 assert(initialized()); 247 flags = _flags; 248 flags.set(Initialized); 249 250 // need to see if original event was in a scheduled, unsquashed 251 // state, but don't want to restore those flags in the current 252 // object itself (since they aren't immediately true) 253 bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed); 254 flags.clear(Squashed | Scheduled); 255 256 if (wasScheduled) { 257 DPRINTF(Config, "rescheduling at %d\n", _when); 258 mainEventQueue.schedule(this, _when); 259 } 260} 261 262void 263EventQueue::serialize(ostream &os) 264{ 265 std::list<Event *> eventPtrs; 266 267 int numEvents = 0; 268 Event *nextBin = head; 269 while (nextBin) { 270 Event *nextInBin = nextBin; 271 272 while (nextInBin) { 273 if (nextInBin->flags.isSet(Event::AutoSerialize)) { 274 eventPtrs.push_back(nextInBin); 275 paramOut(os, csprintf("event%d", numEvents++), 276 nextInBin->name()); 277 } 278 nextInBin = nextInBin->nextInBin; 279 } 280 281 nextBin = nextBin->nextBin; 282 } 283 284 SERIALIZE_SCALAR(numEvents); 285 286 for (std::list<Event *>::iterator it = eventPtrs.begin(); 287 it != eventPtrs.end(); ++it) { 288 (*it)->nameOut(os); 289 (*it)->serialize(os); 290 } 291} 292 293void 294EventQueue::unserialize(Checkpoint *cp, const std::string §ion) 295{ 296 int numEvents; 297 UNSERIALIZE_SCALAR(numEvents); 298 299 std::string eventName; 300 for (int i = 0; i < numEvents; i++) { 301 // get the pointer value associated with the event 302 paramIn(cp, section, csprintf("event%d", i), eventName); 303 304 // create the event based on its pointer value 305 Serializable::create(cp, eventName); 306 } 307} 308 309void 310EventQueue::dump() const 311{ 312 cprintf("============================================================\n"); 313 cprintf("EventQueue Dump (cycle %d)\n", curTick()); 314 cprintf("------------------------------------------------------------\n"); 315 316 if (empty()) 317 cprintf("<No Events>\n"); 318 else { 319 Event *nextBin = head; 320 while (nextBin) { 321 Event *nextInBin = nextBin; 322 while (nextInBin) { 323 nextInBin->dump(); 324 nextInBin = nextInBin->nextInBin; 325 } 326 327 nextBin = nextBin->nextBin; 328 } 329 } 330 331 cprintf("============================================================\n"); 332} 333 334bool 335EventQueue::debugVerify() const 336{ 337 m5::hash_map<long, bool> map; 338 339 Tick time = 0; 340 short priority = 0; 341 342 Event *nextBin = head; 343 while (nextBin) { 344 Event *nextInBin = nextBin; 345 while (nextInBin) { 346 if (nextInBin->when() < time) { 347 cprintf("time goes backwards!"); 348 nextInBin->dump(); 349 return false; 350 } else if (nextInBin->when() == time && 351 nextInBin->priority() < priority) { 352 cprintf("priority inverted!"); 353 nextInBin->dump(); 354 return false; 355 } 356 357 if (map[reinterpret_cast<long>(nextInBin)]) { 358 cprintf("Node already seen"); 359 nextInBin->dump(); 360 return false; 361 } 362 map[reinterpret_cast<long>(nextInBin)] = true; 363 364 time = nextInBin->when(); 365 priority = nextInBin->priority(); 366 367 nextInBin = nextInBin->nextInBin; 368 } 369 370 nextBin = nextBin->nextBin; 371 } 372 373 return true; 374} 375 376void 377dumpMainQueue() 378{ 379 mainEventQueue.dump(); 380} 381 382 383const char * 384Event::description() const 385{ 386 return "generic"; 387} 388 389void 390Event::trace(const char *action) 391{ 392 // This DPRINTF is unconditional because calls to this function 393 // are protected by an 'if (DTRACE(Event))' in the inlined Event 394 // methods. 395 // 396 // This is just a default implementation for derived classes where 397 // it's not worth doing anything special. If you want to put a 398 // more informative message in the trace, override this method on 399 // the particular subclass where you have the information that 400 // needs to be printed. 401 DPRINTFN("%s event %s @ %d\n", description(), action, when()); 402} 403 404void 405Event::dump() const 406{ 407 cprintf("Event %s (%s)\n", name(), description()); 408 cprintf("Flags: %#x\n", flags); 409#ifdef EVENTQ_DEBUG 410 cprintf("Created: %d\n", whenCreated); 411#endif 412 if (scheduled()) { 413#ifdef EVENTQ_DEBUG 414 cprintf("Scheduled at %d\n", whenScheduled); 415#endif 416 cprintf("Scheduled for %d, priority %d\n", when(), _priority); 417 } else { 418 cprintf("Not Scheduled\n"); 419 } 420} 421 422EventQueue::EventQueue(const string &n) 423 : objName(n), head(NULL) 424{} 425