eventq.cc revision 7451
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 "sim/core.hh" 44#include "sim/eventq.hh" 45 46using namespace std; 47 48// 49// Main Event Queue 50// 51// Events on this queue are processed at the *beginning* of each 52// cycle, before the pipeline simulation is performed. 53// 54EventQueue mainEventQueue("Main Event Queue"); 55 56#ifndef NDEBUG 57Counter Event::instanceCounter = 0; 58#endif 59 60Event::~Event() 61{ 62 assert(!scheduled()); 63 flags = 0; 64} 65 66const std::string 67Event::name() const 68{ 69#ifndef NDEBUG 70 return csprintf("Event_%d", instance); 71#else 72 return csprintf("Event_%x", (uintptr_t)this); 73#endif 74} 75 76 77Event * 78Event::insertBefore(Event *event, Event *curr) 79{ 80 // Either way, event will be the top element in the 'in bin' list 81 // which is the pointer we need in order to look into the list, so 82 // we need to insert that into the bin list. 83 if (!curr || *event < *curr) { 84 // Insert the event before the current list since it is in the future. 85 event->nextBin = curr; 86 event->nextInBin = NULL; 87 } else { 88 // Since we're on the correct list, we need to point to the next list 89 event->nextBin = curr->nextBin; // curr->nextBin can now become stale 90 91 // Insert event at the top of the stack 92 event->nextInBin = curr; 93 } 94 95 return event; 96} 97 98void 99EventQueue::insert(Event *event) 100{ 101 // Deal with the head case 102 if (!head || *event <= *head) { 103 head = Event::insertBefore(event, head); 104 return; 105 } 106 107 // Figure out either which 'in bin' list we are on, or where a new list 108 // needs to be inserted 109 Event *prev = head; 110 Event *curr = head->nextBin; 111 while (curr && *curr < *event) { 112 prev = curr; 113 curr = curr->nextBin; 114 } 115 116 // Note: this operation may render all nextBin pointers on the 117 // prev 'in bin' list stale (except for the top one) 118 prev->nextBin = Event::insertBefore(event, curr); 119} 120 121Event * 122Event::removeItem(Event *event, Event *top) 123{ 124 Event *curr = top; 125 Event *next = top->nextInBin; 126 127 // if we removed the top item, we need to handle things specially 128 // and just remove the top item, fixing up the next bin pointer of 129 // the new top item 130 if (event == top) { 131 if (!next) 132 return top->nextBin; 133 next->nextBin = top->nextBin; 134 return next; 135 } 136 137 // Since we already checked the current element, we're going to 138 // keep checking event against the next element. 139 while (event != next) { 140 if (!next) 141 panic("event not found!"); 142 143 curr = next; 144 next = next->nextInBin; 145 } 146 147 // remove next from the 'in bin' list since it's what we're looking for 148 curr->nextInBin = next->nextInBin; 149 return top; 150} 151 152void 153EventQueue::remove(Event *event) 154{ 155 if (head == NULL) 156 panic("event not found!"); 157 158 // deal with an event on the head's 'in bin' list (event has the same 159 // time as the head) 160 if (*head == *event) { 161 head = Event::removeItem(event, head); 162 return; 163 } 164 165 // Find the 'in bin' list that this event belongs on 166 Event *prev = head; 167 Event *curr = head->nextBin; 168 while (curr && *curr < *event) { 169 prev = curr; 170 curr = curr->nextBin; 171 } 172 173 if (!curr || *curr != *event) 174 panic("event not found!"); 175 176 // curr points to the top item of the the correct 'in bin' list, when 177 // we remove an item, it returns the new top item (which may be 178 // unchanged) 179 prev->nextBin = Event::removeItem(event, curr); 180} 181 182Event * 183EventQueue::serviceOne() 184{ 185 Event *event = head; 186 Event *next = head->nextInBin; 187 event->flags.clear(Event::Scheduled); 188 189 if (next) { 190 // update the next bin pointer since it could be stale 191 next->nextBin = head->nextBin; 192 193 // pop the stack 194 head = next; 195 } else { 196 // this was the only element on the 'in bin' list, so get rid of 197 // the 'in bin' list and point to the next bin list 198 head = head->nextBin; 199 } 200 201 // handle action 202 if (!event->squashed()) { 203 event->process(); 204 if (event->isExitEvent()) { 205 assert(!event->flags.isSet(Event::AutoDelete)); // would be silly 206 return event; 207 } 208 } else { 209 event->flags.clear(Event::Squashed); 210 } 211 212 if (event->flags.isSet(Event::AutoDelete) && !event->scheduled()) 213 delete event; 214 215 return NULL; 216} 217 218void 219Event::serialize(std::ostream &os) 220{ 221 SERIALIZE_SCALAR(_when); 222 SERIALIZE_SCALAR(_priority); 223 short _flags = flags; 224 SERIALIZE_SCALAR(_flags); 225} 226 227void 228Event::unserialize(Checkpoint *cp, const string §ion) 229{ 230 if (scheduled()) 231 mainEventQueue.deschedule(this); 232 233 UNSERIALIZE_SCALAR(_when); 234 UNSERIALIZE_SCALAR(_priority); 235 236 // need to see if original event was in a scheduled, unsquashed 237 // state, but don't want to restore those flags in the current 238 // object itself (since they aren't immediately true) 239 short _flags; 240 UNSERIALIZE_SCALAR(_flags); 241 assert(initialized()); 242 flags = _flags; 243 flags.set(Initialized); 244 245 bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed); 246 flags.clear(Squashed | Scheduled); 247 248 if (wasScheduled) { 249 DPRINTF(Config, "rescheduling at %d\n", _when); 250 mainEventQueue.schedule(this, _when); 251 } 252} 253 254void 255EventQueue::serialize(ostream &os) 256{ 257 std::list<Event *> eventPtrs; 258 259 int numEvents = 0; 260 Event *nextBin = head; 261 while (nextBin) { 262 Event *nextInBin = nextBin; 263 264 while (nextInBin) { 265 if (nextInBin->flags.isSet(Event::AutoSerialize)) { 266 eventPtrs.push_back(nextInBin); 267 paramOut(os, csprintf("event%d", numEvents++), 268 nextInBin->name()); 269 } 270 nextInBin = nextInBin->nextInBin; 271 } 272 273 nextBin = nextBin->nextBin; 274 } 275 276 SERIALIZE_SCALAR(numEvents); 277 278 for (std::list<Event *>::iterator it = eventPtrs.begin(); 279 it != eventPtrs.end(); ++it) { 280 (*it)->nameOut(os); 281 (*it)->serialize(os); 282 } 283} 284 285void 286EventQueue::unserialize(Checkpoint *cp, const std::string §ion) 287{ 288 int numEvents; 289 UNSERIALIZE_SCALAR(numEvents); 290 291 std::string eventName; 292 for (int i = 0; i < numEvents; i++) { 293 // get the pointer value associated with the event 294 paramIn(cp, section, csprintf("event%d", i), eventName); 295 296 // create the event based on its pointer value 297 Serializable::create(cp, eventName); 298 } 299} 300 301void 302EventQueue::dump() const 303{ 304 cprintf("============================================================\n"); 305 cprintf("EventQueue Dump (cycle %d)\n", curTick); 306 cprintf("------------------------------------------------------------\n"); 307 308 if (empty()) 309 cprintf("<No Events>\n"); 310 else { 311 Event *nextBin = head; 312 while (nextBin) { 313 Event *nextInBin = nextBin; 314 while (nextInBin) { 315 nextInBin->dump(); 316 nextInBin = nextInBin->nextInBin; 317 } 318 319 nextBin = nextBin->nextBin; 320 } 321 } 322 323 cprintf("============================================================\n"); 324} 325 326bool 327EventQueue::debugVerify() const 328{ 329 m5::hash_map<long, bool> map; 330 331 Tick time = 0; 332 short priority = 0; 333 334 Event *nextBin = head; 335 while (nextBin) { 336 Event *nextInBin = nextBin; 337 while (nextInBin) { 338 if (nextInBin->when() < time) { 339 cprintf("time goes backwards!"); 340 nextInBin->dump(); 341 return false; 342 } else if (nextInBin->when() == time && 343 nextInBin->priority() < priority) { 344 cprintf("priority inverted!"); 345 nextInBin->dump(); 346 return false; 347 } 348 349 if (map[reinterpret_cast<long>(nextInBin)]) { 350 cprintf("Node already seen"); 351 nextInBin->dump(); 352 return false; 353 } 354 map[reinterpret_cast<long>(nextInBin)] = true; 355 356 time = nextInBin->when(); 357 priority = nextInBin->priority(); 358 359 nextInBin = nextInBin->nextInBin; 360 } 361 362 nextBin = nextBin->nextBin; 363 } 364 365 return true; 366} 367 368void 369dumpMainQueue() 370{ 371 mainEventQueue.dump(); 372} 373 374 375const char * 376Event::description() const 377{ 378 return "generic"; 379} 380 381void 382Event::trace(const char *action) 383{ 384 // This DPRINTF is unconditional because calls to this function 385 // are protected by an 'if (DTRACE(Event))' in the inlined Event 386 // methods. 387 // 388 // This is just a default implementation for derived classes where 389 // it's not worth doing anything special. If you want to put a 390 // more informative message in the trace, override this method on 391 // the particular subclass where you have the information that 392 // needs to be printed. 393 DPRINTFN("%s event %s @ %d\n", description(), action, when()); 394} 395 396void 397Event::dump() const 398{ 399 cprintf("Event %s (%s)\n", name(), description()); 400 cprintf("Flags: %#x\n", flags); 401#ifdef EVENTQ_DEBUG 402 cprintf("Created: %d\n", whenCreated); 403#endif 404 if (scheduled()) { 405#ifdef EVENTQ_DEBUG 406 cprintf("Scheduled at %d\n", whenScheduled); 407#endif 408 cprintf("Scheduled for %d, priority %d\n", when(), _priority); 409 } else { 410 cprintf("Not Scheduled\n"); 411 } 412} 413 414EventQueue::EventQueue(const string &n) 415 : objName(n), head(NULL) 416{} 417