eventq.cc revision 5769
12SN/A/* 21762SN/A * Copyright (c) 2000-2005 The Regents of The University of Michigan 35502Snate@binkert.org * Copyright (c) 2008 The Hewlett-Packard Development Company 42SN/A * All rights reserved. 52SN/A * 62SN/A * Redistribution and use in source and binary forms, with or without 72SN/A * modification, are permitted provided that the following conditions are 82SN/A * met: redistributions of source code must retain the above copyright 92SN/A * notice, this list of conditions and the following disclaimer; 102SN/A * redistributions in binary form must reproduce the above copyright 112SN/A * notice, this list of conditions and the following disclaimer in the 122SN/A * documentation and/or other materials provided with the distribution; 132SN/A * neither the name of the copyright holders nor the names of its 142SN/A * contributors may be used to endorse or promote products derived from 152SN/A * this software without specific prior written permission. 162SN/A * 172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 282665Ssaidi@eecs.umich.edu * 292665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 302665Ssaidi@eecs.umich.edu * Nathan Binkert 312665Ssaidi@eecs.umich.edu * Steve Raasch 322SN/A */ 332SN/A 345501Snate@binkert.org#include <cassert> 352SN/A#include <iostream> 362SN/A#include <string> 372SN/A#include <vector> 382SN/A 395502Snate@binkert.org#include "base/hashmap.hh" 405501Snate@binkert.org#include "base/misc.hh" 415501Snate@binkert.org#include "base/trace.hh" 421717SN/A#include "cpu/smt.hh" 435501Snate@binkert.org#include "sim/core.hh" 4456SN/A#include "sim/eventq.hh" 452SN/A 462SN/Ausing namespace std; 472SN/A 482SN/A// 492SN/A// Main Event Queue 502SN/A// 512SN/A// Events on this queue are processed at the *beginning* of each 522SN/A// cycle, before the pipeline simulation is performed. 532SN/A// 545605Snate@binkert.orgEventQueue mainEventQueue("Main Event Queue"); 552SN/A 564017Sstever@eecs.umich.edu#ifndef NDEBUG 574016Sstever@eecs.umich.eduCounter Event::instanceCounter = 0; 584017Sstever@eecs.umich.edu#endif 594016Sstever@eecs.umich.edu 605768Snate@binkert.orgEvent::~Event() 615768Snate@binkert.org{ 625768Snate@binkert.org} 635768Snate@binkert.org 645768Snate@binkert.orgconst std::string 655768Snate@binkert.orgEvent::name() const 665768Snate@binkert.org{ 675768Snate@binkert.org#ifndef NDEBUG 685768Snate@binkert.org return csprintf("Event_%d", instance); 695768Snate@binkert.org#else 705768Snate@binkert.org return csprintf("Event_%x", (uintptr_t)this); 715768Snate@binkert.org#endif 725768Snate@binkert.org} 735768Snate@binkert.org 745768Snate@binkert.org 755602Snate@binkert.orgEvent * 765602Snate@binkert.orgEvent::insertBefore(Event *event, Event *curr) 775502Snate@binkert.org{ 785503Snate@binkert.org // Either way, event will be the top element in the 'in bin' list 795502Snate@binkert.org // which is the pointer we need in order to look into the list, so 805502Snate@binkert.org // we need to insert that into the bin list. 815502Snate@binkert.org if (!curr || *event < *curr) { 825502Snate@binkert.org // Insert the event before the current list since it is in the future. 835502Snate@binkert.org event->nextBin = curr; 845503Snate@binkert.org event->nextInBin = NULL; 855502Snate@binkert.org } else { 865502Snate@binkert.org // Since we're on the correct list, we need to point to the next list 875502Snate@binkert.org event->nextBin = curr->nextBin; // curr->nextBin can now become stale 885502Snate@binkert.org 895503Snate@binkert.org // Insert event at the top of the stack 905503Snate@binkert.org event->nextInBin = curr; 915503Snate@binkert.org } 925502Snate@binkert.org 935503Snate@binkert.org return event; 945502Snate@binkert.org} 955502Snate@binkert.org 962SN/Avoid 972SN/AEventQueue::insert(Event *event) 982SN/A{ 995502Snate@binkert.org // Deal with the head case 1005502Snate@binkert.org if (!head || *event <= *head) { 1015602Snate@binkert.org head = Event::insertBefore(event, head); 1025502Snate@binkert.org return; 1035502Snate@binkert.org } 1042SN/A 1055502Snate@binkert.org // Figure out either which 'in bin' list we are on, or where a new list 1065502Snate@binkert.org // needs to be inserted 1075503Snate@binkert.org Event *prev = head; 1085503Snate@binkert.org Event *curr = head->nextBin; 1095503Snate@binkert.org while (curr && *curr < *event) { 1105503Snate@binkert.org prev = curr; 1115503Snate@binkert.org curr = curr->nextBin; 1125502Snate@binkert.org } 1132SN/A 1145503Snate@binkert.org // Note: this operation may render all nextBin pointers on the 1155503Snate@binkert.org // prev 'in bin' list stale (except for the top one) 1165602Snate@binkert.org prev->nextBin = Event::insertBefore(event, curr); 1175502Snate@binkert.org} 1182SN/A 1195602Snate@binkert.orgEvent * 1205602Snate@binkert.orgEvent::removeItem(Event *event, Event *top) 1215502Snate@binkert.org{ 1225503Snate@binkert.org Event *curr = top; 1235503Snate@binkert.org Event *next = top->nextInBin; 1245502Snate@binkert.org 1255503Snate@binkert.org // if we removed the top item, we need to handle things specially 1265503Snate@binkert.org // and just remove the top item, fixing up the next bin pointer of 1275503Snate@binkert.org // the new top item 1285503Snate@binkert.org if (event == top) { 1295503Snate@binkert.org if (!next) 1305503Snate@binkert.org return top->nextBin; 1315503Snate@binkert.org next->nextBin = top->nextBin; 1325503Snate@binkert.org return next; 1335503Snate@binkert.org } 1345503Snate@binkert.org 1355503Snate@binkert.org // Since we already checked the current element, we're going to 1365503Snate@binkert.org // keep checking event against the next element. 1375503Snate@binkert.org while (event != next) { 1385503Snate@binkert.org if (!next) 1395502Snate@binkert.org panic("event not found!"); 1405502Snate@binkert.org 1415503Snate@binkert.org curr = next; 1425503Snate@binkert.org next = next->nextInBin; 1432SN/A } 1445502Snate@binkert.org 1455503Snate@binkert.org // remove next from the 'in bin' list since it's what we're looking for 1465503Snate@binkert.org curr->nextInBin = next->nextInBin; 1475503Snate@binkert.org return top; 1482SN/A} 1492SN/A 1502SN/Avoid 1512SN/AEventQueue::remove(Event *event) 1522SN/A{ 1532SN/A if (head == NULL) 1545502Snate@binkert.org panic("event not found!"); 1552SN/A 1565502Snate@binkert.org // deal with an event on the head's 'in bin' list (event has the same 1575502Snate@binkert.org // time as the head) 1585502Snate@binkert.org if (*head == *event) { 1595602Snate@binkert.org head = Event::removeItem(event, head); 1602SN/A return; 1612SN/A } 1622SN/A 1635502Snate@binkert.org // Find the 'in bin' list that this event belongs on 1642SN/A Event *prev = head; 1655502Snate@binkert.org Event *curr = head->nextBin; 1665502Snate@binkert.org while (curr && *curr < *event) { 1672SN/A prev = curr; 1685502Snate@binkert.org curr = curr->nextBin; 1692SN/A } 1702SN/A 1715502Snate@binkert.org if (!curr || *curr != *event) 1725502Snate@binkert.org panic("event not found!"); 1735502Snate@binkert.org 1745503Snate@binkert.org // curr points to the top item of the the correct 'in bin' list, when 1755503Snate@binkert.org // we remove an item, it returns the new top item (which may be 1765502Snate@binkert.org // unchanged) 1775602Snate@binkert.org prev->nextBin = Event::removeItem(event, curr); 1782SN/A} 1792SN/A 1802667Sstever@eecs.umich.eduEvent * 1812SN/AEventQueue::serviceOne() 1822SN/A{ 1835503Snate@binkert.org Event *event = head; 1845503Snate@binkert.org Event *next = head->nextInBin; 1855769Snate@binkert.org event->flags.clear(Event::Scheduled); 1865502Snate@binkert.org 1875503Snate@binkert.org if (next) { 1885503Snate@binkert.org // update the next bin pointer since it could be stale 1895503Snate@binkert.org next->nextBin = head->nextBin; 1905503Snate@binkert.org 1915503Snate@binkert.org // pop the stack 1925503Snate@binkert.org head = next; 1935503Snate@binkert.org } else { 1945502Snate@binkert.org // this was the only element on the 'in bin' list, so get rid of 1955502Snate@binkert.org // the 'in bin' list and point to the next bin list 1965503Snate@binkert.org head = head->nextBin; 1975502Snate@binkert.org } 1982SN/A 1992SN/A // handle action 2002667Sstever@eecs.umich.edu if (!event->squashed()) { 2012SN/A event->process(); 2022667Sstever@eecs.umich.edu if (event->isExitEvent()) { 2035769Snate@binkert.org assert(!event->flags.isSet(Event::AutoDelete)); // would be silly 2042667Sstever@eecs.umich.edu return event; 2052667Sstever@eecs.umich.edu } 2062667Sstever@eecs.umich.edu } else { 2075769Snate@binkert.org event->flags.clear(Event::Squashed); 2082667Sstever@eecs.umich.edu } 2092SN/A 2105769Snate@binkert.org if (event->flags.isSet(Event::AutoDelete) && !event->scheduled()) 2112SN/A delete event; 2122667Sstever@eecs.umich.edu 2132667Sstever@eecs.umich.edu return NULL; 2142SN/A} 2152SN/A 216224SN/Avoid 217224SN/AEvent::serialize(std::ostream &os) 218224SN/A{ 219224SN/A SERIALIZE_SCALAR(_when); 220224SN/A SERIALIZE_SCALAR(_priority); 2215769Snate@binkert.org short _flags = flags; 2225769Snate@binkert.org SERIALIZE_SCALAR(_flags); 223224SN/A} 224224SN/A 225224SN/Avoid 226237SN/AEvent::unserialize(Checkpoint *cp, const string §ion) 227224SN/A{ 2285695Snate@binkert.org if (scheduled()) 2295695Snate@binkert.org mainEventQueue.deschedule(this); 230224SN/A 231224SN/A UNSERIALIZE_SCALAR(_when); 232224SN/A UNSERIALIZE_SCALAR(_priority); 233224SN/A 234224SN/A // need to see if original event was in a scheduled, unsquashed 235224SN/A // state, but don't want to restore those flags in the current 236224SN/A // object itself (since they aren't immediately true) 2375769Snate@binkert.org short _flags; 2385769Snate@binkert.org UNSERIALIZE_SCALAR(_flags); 2395769Snate@binkert.org flags = _flags; 2405769Snate@binkert.org 2415769Snate@binkert.org bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed); 2425769Snate@binkert.org flags.clear(Squashed | Scheduled); 243224SN/A 244224SN/A if (wasScheduled) { 245224SN/A DPRINTF(Config, "rescheduling at %d\n", _when); 2465695Snate@binkert.org mainEventQueue.schedule(this, _when); 247224SN/A } 248224SN/A} 249224SN/A 2502SN/Avoid 251217SN/AEventQueue::serialize(ostream &os) 2522SN/A{ 253265SN/A std::list<Event *> eventPtrs; 254237SN/A 255237SN/A int numEvents = 0; 2565502Snate@binkert.org Event *nextBin = head; 2575502Snate@binkert.org while (nextBin) { 2585503Snate@binkert.org Event *nextInBin = nextBin; 2595502Snate@binkert.org 2605503Snate@binkert.org while (nextInBin) { 2615769Snate@binkert.org if (nextInBin->flags.isSet(Event::AutoSerialize)) { 2625502Snate@binkert.org eventPtrs.push_back(nextInBin); 2635502Snate@binkert.org paramOut(os, csprintf("event%d", numEvents++), 2645502Snate@binkert.org nextInBin->name()); 2655502Snate@binkert.org } 2665502Snate@binkert.org nextInBin = nextInBin->nextInBin; 2675503Snate@binkert.org } 2685502Snate@binkert.org 2695502Snate@binkert.org nextBin = nextBin->nextBin; 2702SN/A } 271237SN/A 272265SN/A SERIALIZE_SCALAR(numEvents); 273265SN/A 2745502Snate@binkert.org for (std::list<Event *>::iterator it = eventPtrs.begin(); 275265SN/A it != eventPtrs.end(); ++it) { 276265SN/A (*it)->nameOut(os); 277265SN/A (*it)->serialize(os); 278265SN/A } 2792SN/A} 2802SN/A 281237SN/Avoid 282237SN/AEventQueue::unserialize(Checkpoint *cp, const std::string §ion) 283237SN/A{ 284265SN/A int numEvents; 285265SN/A UNSERIALIZE_SCALAR(numEvents); 286265SN/A 287270SN/A std::string eventName; 288265SN/A for (int i = 0; i < numEvents; i++) { 289265SN/A // get the pointer value associated with the event 290270SN/A paramIn(cp, section, csprintf("event%d", i), eventName); 291265SN/A 292265SN/A // create the event based on its pointer value 293395SN/A Serializable::create(cp, eventName); 294237SN/A } 295237SN/A} 296237SN/A 2972SN/Avoid 2985501Snate@binkert.orgEventQueue::dump() const 2992SN/A{ 3002SN/A cprintf("============================================================\n"); 3012SN/A cprintf("EventQueue Dump (cycle %d)\n", curTick); 3022SN/A cprintf("------------------------------------------------------------\n"); 3032SN/A 3042SN/A if (empty()) 3052SN/A cprintf("<No Events>\n"); 3062SN/A else { 3075502Snate@binkert.org Event *nextBin = head; 3085502Snate@binkert.org while (nextBin) { 3095502Snate@binkert.org Event *nextInBin = nextBin; 3105503Snate@binkert.org while (nextInBin) { 3115503Snate@binkert.org nextInBin->dump(); 3125502Snate@binkert.org nextInBin = nextInBin->nextInBin; 3135503Snate@binkert.org } 3145502Snate@binkert.org 3155502Snate@binkert.org nextBin = nextBin->nextBin; 3162SN/A } 3172SN/A } 3182SN/A 3192SN/A cprintf("============================================================\n"); 3202SN/A} 3212SN/A 3225502Snate@binkert.orgbool 3235502Snate@binkert.orgEventQueue::debugVerify() const 3245502Snate@binkert.org{ 3255502Snate@binkert.org m5::hash_map<long, bool> map; 3265502Snate@binkert.org 3275502Snate@binkert.org Tick time = 0; 3285502Snate@binkert.org short priority = 0; 3295502Snate@binkert.org 3305502Snate@binkert.org Event *nextBin = head; 3315502Snate@binkert.org while (nextBin) { 3325503Snate@binkert.org Event *nextInBin = nextBin; 3335503Snate@binkert.org while (nextInBin) { 3345502Snate@binkert.org if (nextInBin->when() < time) { 3355502Snate@binkert.org cprintf("time goes backwards!"); 3365502Snate@binkert.org nextInBin->dump(); 3375502Snate@binkert.org return false; 3385502Snate@binkert.org } else if (nextInBin->when() == time && 3395502Snate@binkert.org nextInBin->priority() < priority) { 3405502Snate@binkert.org cprintf("priority inverted!"); 3415502Snate@binkert.org nextInBin->dump(); 3425502Snate@binkert.org return false; 3435502Snate@binkert.org } 3445502Snate@binkert.org 3455502Snate@binkert.org if (map[reinterpret_cast<long>(nextInBin)]) { 3465502Snate@binkert.org cprintf("Node already seen"); 3475502Snate@binkert.org nextInBin->dump(); 3485502Snate@binkert.org return false; 3495502Snate@binkert.org } 3505502Snate@binkert.org map[reinterpret_cast<long>(nextInBin)] = true; 3515502Snate@binkert.org 3525502Snate@binkert.org time = nextInBin->when(); 3535502Snate@binkert.org priority = nextInBin->priority(); 3545502Snate@binkert.org 3555502Snate@binkert.org nextInBin = nextInBin->nextInBin; 3565503Snate@binkert.org } 3575502Snate@binkert.org 3585502Snate@binkert.org nextBin = nextBin->nextBin; 3595502Snate@binkert.org } 3605502Snate@binkert.org 3615502Snate@binkert.org return true; 3625502Snate@binkert.org} 3635502Snate@binkert.org 3641019SN/Avoid 3651019SN/AdumpMainQueue() 3661019SN/A{ 3671019SN/A mainEventQueue.dump(); 3681019SN/A} 3691019SN/A 3702SN/A 3712SN/Aconst char * 3725336Shines@cs.fsu.eduEvent::description() const 3732SN/A{ 3742SN/A return "generic"; 3752SN/A} 3762SN/A 3772SN/Avoid 3782SN/AEvent::trace(const char *action) 3792SN/A{ 3802SN/A // This DPRINTF is unconditional because calls to this function 3812SN/A // are protected by an 'if (DTRACE(Event))' in the inlined Event 3822SN/A // methods. 3832SN/A // 3842SN/A // This is just a default implementation for derived classes where 3852SN/A // it's not worth doing anything special. If you want to put a 3862SN/A // more informative message in the trace, override this method on 3872SN/A // the particular subclass where you have the information that 3882SN/A // needs to be printed. 3892SN/A DPRINTFN("%s event %s @ %d\n", description(), action, when()); 3902SN/A} 3912SN/A 3922SN/Avoid 3935501Snate@binkert.orgEvent::dump() const 3942SN/A{ 3955501Snate@binkert.org cprintf("Event %s (%s)\n", name(), description()); 3965769Snate@binkert.org cprintf("Flags: %#x\n", flags); 3975501Snate@binkert.org#ifdef EVENTQ_DEBUG 3985501Snate@binkert.org cprintf("Created: %d\n", whenCreated); 3992SN/A#endif 4002SN/A if (scheduled()) { 4015501Snate@binkert.org#ifdef EVENTQ_DEBUG 4025501Snate@binkert.org cprintf("Scheduled at %d\n", whenScheduled); 4032SN/A#endif 4041019SN/A cprintf("Scheduled for %d, priority %d\n", when(), _priority); 4055501Snate@binkert.org } else { 4061019SN/A cprintf("Not Scheduled\n"); 4072SN/A } 4082SN/A} 409