eventq.cc revision 9328
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) || 207 !event->flags.isSet(Event::IsMainQueue)); // would be silly 208 return event; 209 } 210 } else { 211 event->flags.clear(Event::Squashed); 212 } 213 214 if (event->flags.isSet(Event::AutoDelete) && !event->scheduled()) 215 delete event; 216 217 return NULL; 218} 219 220void 221Event::serialize(std::ostream &os) 222{ 223 SERIALIZE_SCALAR(_when); 224 SERIALIZE_SCALAR(_priority); 225 short _flags = flags; 226 SERIALIZE_SCALAR(_flags); 227} 228 229void 230Event::unserialize(Checkpoint *cp, const string §ion) 231{ 232 if (scheduled()) 233 mainEventQueue.deschedule(this); 234 235 UNSERIALIZE_SCALAR(_when); 236 UNSERIALIZE_SCALAR(_priority); 237 238 short _flags; 239 UNSERIALIZE_SCALAR(_flags); 240 241 // Old checkpoints had no concept of the Initialized flag 242 // so restoring from old checkpoints always fail. 243 // Events are initialized on construction but original code 244 // "flags = _flags" would just overwrite the initialization. 245 // So, read in the checkpoint flags, but then set the Initialized 246 // flag on top of it in order to avoid failures. 247 assert(initialized()); 248 flags = _flags; 249 flags.set(Initialized); 250 251 // need to see if original event was in a scheduled, unsquashed 252 // state, but don't want to restore those flags in the current 253 // object itself (since they aren't immediately true) 254 bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed); 255 flags.clear(Squashed | Scheduled); 256 257 if (wasScheduled) { 258 DPRINTF(Config, "rescheduling at %d\n", _when); 259 mainEventQueue.schedule(this, _when); 260 } 261} 262 263void 264EventQueue::serialize(ostream &os) 265{ 266 std::list<Event *> eventPtrs; 267 268 int numEvents = 0; 269 Event *nextBin = head; 270 while (nextBin) { 271 Event *nextInBin = nextBin; 272 273 while (nextInBin) { 274 if (nextInBin->flags.isSet(Event::AutoSerialize)) { 275 eventPtrs.push_back(nextInBin); 276 paramOut(os, csprintf("event%d", numEvents++), 277 nextInBin->name()); 278 } 279 nextInBin = nextInBin->nextInBin; 280 } 281 282 nextBin = nextBin->nextBin; 283 } 284 285 SERIALIZE_SCALAR(numEvents); 286 287 for (std::list<Event *>::iterator it = eventPtrs.begin(); 288 it != eventPtrs.end(); ++it) { 289 (*it)->nameOut(os); 290 (*it)->serialize(os); 291 } 292} 293 294void 295EventQueue::unserialize(Checkpoint *cp, const std::string §ion) 296{ 297 int numEvents; 298 UNSERIALIZE_SCALAR(numEvents); 299 300 std::string eventName; 301 for (int i = 0; i < numEvents; i++) { 302 // get the pointer value associated with the event 303 paramIn(cp, section, csprintf("event%d", i), eventName); 304 305 // create the event based on its pointer value 306 Serializable::create(cp, eventName); 307 } 308} 309 310void 311EventQueue::dump() const 312{ 313 cprintf("============================================================\n"); 314 cprintf("EventQueue Dump (cycle %d)\n", curTick()); 315 cprintf("------------------------------------------------------------\n"); 316 317 if (empty()) 318 cprintf("<No Events>\n"); 319 else { 320 Event *nextBin = head; 321 while (nextBin) { 322 Event *nextInBin = nextBin; 323 while (nextInBin) { 324 nextInBin->dump(); 325 nextInBin = nextInBin->nextInBin; 326 } 327 328 nextBin = nextBin->nextBin; 329 } 330 } 331 332 cprintf("============================================================\n"); 333} 334 335bool 336EventQueue::debugVerify() const 337{ 338 m5::hash_map<long, bool> map; 339 340 Tick time = 0; 341 short priority = 0; 342 343 Event *nextBin = head; 344 while (nextBin) { 345 Event *nextInBin = nextBin; 346 while (nextInBin) { 347 if (nextInBin->when() < time) { 348 cprintf("time goes backwards!"); 349 nextInBin->dump(); 350 return false; 351 } else if (nextInBin->when() == time && 352 nextInBin->priority() < priority) { 353 cprintf("priority inverted!"); 354 nextInBin->dump(); 355 return false; 356 } 357 358 if (map[reinterpret_cast<long>(nextInBin)]) { 359 cprintf("Node already seen"); 360 nextInBin->dump(); 361 return false; 362 } 363 map[reinterpret_cast<long>(nextInBin)] = true; 364 365 time = nextInBin->when(); 366 priority = nextInBin->priority(); 367 368 nextInBin = nextInBin->nextInBin; 369 } 370 371 nextBin = nextBin->nextBin; 372 } 373 374 return true; 375} 376 377Event* 378EventQueue::replaceHead(Event* s) 379{ 380 Event* t = head; 381 head = s; 382 return t; 383} 384 385void 386dumpMainQueue() 387{ 388 mainEventQueue.dump(); 389} 390 391 392const char * 393Event::description() const 394{ 395 return "generic"; 396} 397 398void 399Event::trace(const char *action) 400{ 401 // This DPRINTF is unconditional because calls to this function 402 // are protected by an 'if (DTRACE(Event))' in the inlined Event 403 // methods. 404 // 405 // This is just a default implementation for derived classes where 406 // it's not worth doing anything special. If you want to put a 407 // more informative message in the trace, override this method on 408 // the particular subclass where you have the information that 409 // needs to be printed. 410 DPRINTFN("%s event %s @ %d\n", description(), action, when()); 411} 412 413void 414Event::dump() const 415{ 416 cprintf("Event %s (%s)\n", name(), description()); 417 cprintf("Flags: %#x\n", flags); 418#ifdef EVENTQ_DEBUG 419 cprintf("Created: %d\n", whenCreated); 420#endif 421 if (scheduled()) { 422#ifdef EVENTQ_DEBUG 423 cprintf("Scheduled at %d\n", whenScheduled); 424#endif 425 cprintf("Scheduled for %d, priority %d\n", when(), _priority); 426 } else { 427 cprintf("Not Scheduled\n"); 428 } 429} 430 431EventQueue::EventQueue(const string &n) 432 : objName(n), head(NULL) 433{} 434