eventq.cc revision 7452
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{
625774Snate@binkert.org    assert(!scheduled());
637059Snate@binkert.org    flags = 0;
645768Snate@binkert.org}
655768Snate@binkert.org
665768Snate@binkert.orgconst std::string
675768Snate@binkert.orgEvent::name() const
685768Snate@binkert.org{
695768Snate@binkert.org#ifndef NDEBUG
705768Snate@binkert.org    return csprintf("Event_%d", instance);
715768Snate@binkert.org#else
725768Snate@binkert.org    return csprintf("Event_%x", (uintptr_t)this);
735768Snate@binkert.org#endif
745768Snate@binkert.org}
755768Snate@binkert.org
765768Snate@binkert.org
775602Snate@binkert.orgEvent *
785602Snate@binkert.orgEvent::insertBefore(Event *event, Event *curr)
795502Snate@binkert.org{
805503Snate@binkert.org    // Either way, event will be the top element in the 'in bin' list
815502Snate@binkert.org    // which is the pointer we need in order to look into the list, so
825502Snate@binkert.org    // we need to insert that into the bin list.
835502Snate@binkert.org    if (!curr || *event < *curr) {
845502Snate@binkert.org        // Insert the event before the current list since it is in the future.
855502Snate@binkert.org        event->nextBin = curr;
865503Snate@binkert.org        event->nextInBin = NULL;
875502Snate@binkert.org    } else {
885502Snate@binkert.org        // Since we're on the correct list, we need to point to the next list
895502Snate@binkert.org        event->nextBin = curr->nextBin;  // curr->nextBin can now become stale
905502Snate@binkert.org
915503Snate@binkert.org        // Insert event at the top of the stack
925503Snate@binkert.org        event->nextInBin = curr;
935503Snate@binkert.org    }
945502Snate@binkert.org
955503Snate@binkert.org    return event;
965502Snate@binkert.org}
975502Snate@binkert.org
982SN/Avoid
992SN/AEventQueue::insert(Event *event)
1002SN/A{
1015502Snate@binkert.org    // Deal with the head case
1025502Snate@binkert.org    if (!head || *event <= *head) {
1035602Snate@binkert.org        head = Event::insertBefore(event, head);
1045502Snate@binkert.org        return;
1055502Snate@binkert.org    }
1062SN/A
1075502Snate@binkert.org    // Figure out either which 'in bin' list we are on, or where a new list
1085502Snate@binkert.org    // needs to be inserted
1095503Snate@binkert.org    Event *prev = head;
1105503Snate@binkert.org    Event *curr = head->nextBin;
1115503Snate@binkert.org    while (curr && *curr < *event) {
1125503Snate@binkert.org        prev = curr;
1135503Snate@binkert.org        curr = curr->nextBin;
1145502Snate@binkert.org    }
1152SN/A
1165503Snate@binkert.org    // Note: this operation may render all nextBin pointers on the
1175503Snate@binkert.org    // prev 'in bin' list stale (except for the top one)
1185602Snate@binkert.org    prev->nextBin = Event::insertBefore(event, curr);
1195502Snate@binkert.org}
1202SN/A
1215602Snate@binkert.orgEvent *
1225602Snate@binkert.orgEvent::removeItem(Event *event, Event *top)
1235502Snate@binkert.org{
1245503Snate@binkert.org    Event *curr = top;
1255503Snate@binkert.org    Event *next = top->nextInBin;
1265502Snate@binkert.org
1275503Snate@binkert.org    // if we removed the top item, we need to handle things specially
1285503Snate@binkert.org    // and just remove the top item, fixing up the next bin pointer of
1295503Snate@binkert.org    // the new top item
1305503Snate@binkert.org    if (event == top) {
1315503Snate@binkert.org        if (!next)
1325503Snate@binkert.org            return top->nextBin;
1335503Snate@binkert.org        next->nextBin = top->nextBin;
1345503Snate@binkert.org        return next;
1355503Snate@binkert.org    }
1365503Snate@binkert.org
1375503Snate@binkert.org    // Since we already checked the current element, we're going to
1385503Snate@binkert.org    // keep checking event against the next element.
1395503Snate@binkert.org    while (event != next) {
1405503Snate@binkert.org        if (!next)
1415502Snate@binkert.org            panic("event not found!");
1425502Snate@binkert.org
1435503Snate@binkert.org        curr = next;
1445503Snate@binkert.org        next = next->nextInBin;
1452SN/A    }
1465502Snate@binkert.org
1475503Snate@binkert.org    // remove next from the 'in bin' list since it's what we're looking for
1485503Snate@binkert.org    curr->nextInBin = next->nextInBin;
1495503Snate@binkert.org    return top;
1502SN/A}
1512SN/A
1522SN/Avoid
1532SN/AEventQueue::remove(Event *event)
1542SN/A{
1552SN/A    if (head == NULL)
1565502Snate@binkert.org        panic("event not found!");
1572SN/A
1585502Snate@binkert.org    // deal with an event on the head's 'in bin' list (event has the same
1595502Snate@binkert.org    // time as the head)
1605502Snate@binkert.org    if (*head == *event) {
1615602Snate@binkert.org        head = Event::removeItem(event, head);
1622SN/A        return;
1632SN/A    }
1642SN/A
1655502Snate@binkert.org    // Find the 'in bin' list that this event belongs on
1662SN/A    Event *prev = head;
1675502Snate@binkert.org    Event *curr = head->nextBin;
1685502Snate@binkert.org    while (curr && *curr < *event) {
1692SN/A        prev = curr;
1705502Snate@binkert.org        curr = curr->nextBin;
1712SN/A    }
1722SN/A
1735502Snate@binkert.org    if (!curr || *curr != *event)
1745502Snate@binkert.org        panic("event not found!");
1755502Snate@binkert.org
1765503Snate@binkert.org    // curr points to the top item of the the correct 'in bin' list, when
1775503Snate@binkert.org    // we remove an item, it returns the new top item (which may be
1785502Snate@binkert.org    // unchanged)
1795602Snate@binkert.org    prev->nextBin = Event::removeItem(event, curr);
1802SN/A}
1812SN/A
1822667Sstever@eecs.umich.eduEvent *
1832SN/AEventQueue::serviceOne()
1842SN/A{
1855503Snate@binkert.org    Event *event = head;
1865503Snate@binkert.org    Event *next = head->nextInBin;
1875769Snate@binkert.org    event->flags.clear(Event::Scheduled);
1885502Snate@binkert.org
1895503Snate@binkert.org    if (next) {
1905503Snate@binkert.org        // update the next bin pointer since it could be stale
1915503Snate@binkert.org        next->nextBin = head->nextBin;
1925503Snate@binkert.org
1935503Snate@binkert.org        // pop the stack
1945503Snate@binkert.org        head = next;
1955503Snate@binkert.org    } else {
1965502Snate@binkert.org        // this was the only element on the 'in bin' list, so get rid of
1975502Snate@binkert.org        // the 'in bin' list and point to the next bin list
1985503Snate@binkert.org        head = head->nextBin;
1995502Snate@binkert.org    }
2002SN/A
2012SN/A    // handle action
2022667Sstever@eecs.umich.edu    if (!event->squashed()) {
2032SN/A        event->process();
2042667Sstever@eecs.umich.edu        if (event->isExitEvent()) {
2055769Snate@binkert.org            assert(!event->flags.isSet(Event::AutoDelete)); // would be silly
2062667Sstever@eecs.umich.edu            return event;
2072667Sstever@eecs.umich.edu        }
2082667Sstever@eecs.umich.edu    } else {
2095769Snate@binkert.org        event->flags.clear(Event::Squashed);
2102667Sstever@eecs.umich.edu    }
2112SN/A
2125769Snate@binkert.org    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
2132SN/A        delete event;
2142667Sstever@eecs.umich.edu
2152667Sstever@eecs.umich.edu    return NULL;
2162SN/A}
2172SN/A
218224SN/Avoid
219224SN/AEvent::serialize(std::ostream &os)
220224SN/A{
221224SN/A    SERIALIZE_SCALAR(_when);
222224SN/A    SERIALIZE_SCALAR(_priority);
2235769Snate@binkert.org    short _flags = flags;
2245769Snate@binkert.org    SERIALIZE_SCALAR(_flags);
225224SN/A}
226224SN/A
227224SN/Avoid
228237SN/AEvent::unserialize(Checkpoint *cp, const string &section)
229224SN/A{
2305695Snate@binkert.org    if (scheduled())
2315695Snate@binkert.org        mainEventQueue.deschedule(this);
232224SN/A
233224SN/A    UNSERIALIZE_SCALAR(_when);
234224SN/A    UNSERIALIZE_SCALAR(_priority);
235224SN/A
2365769Snate@binkert.org    short _flags;
2375769Snate@binkert.org    UNSERIALIZE_SCALAR(_flags);
2387452SLisa.Hsu@amd.com
2397452SLisa.Hsu@amd.com    // Old checkpoints had no concept of the Initialized flag
2407452SLisa.Hsu@amd.com    // so restoring from old checkpoints always fail.
2417452SLisa.Hsu@amd.com    // Events are initialized on construction but original code
2427452SLisa.Hsu@amd.com    // "flags = _flags" would just overwrite the initialization.
2437452SLisa.Hsu@amd.com    // So, read in the checkpoint flags, but then set the Initialized
2447452SLisa.Hsu@amd.com    // flag on top of it in order to avoid failures.
2457451SLisa.Hsu@amd.com    assert(initialized());
2465769Snate@binkert.org    flags = _flags;
2477451SLisa.Hsu@amd.com    flags.set(Initialized);
2485769Snate@binkert.org
2497452SLisa.Hsu@amd.com    // need to see if original event was in a scheduled, unsquashed
2507452SLisa.Hsu@amd.com    // state, but don't want to restore those flags in the current
2517452SLisa.Hsu@amd.com    // object itself (since they aren't immediately true)
2525769Snate@binkert.org    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
2535769Snate@binkert.org    flags.clear(Squashed | Scheduled);
254224SN/A
255224SN/A    if (wasScheduled) {
256224SN/A        DPRINTF(Config, "rescheduling at %d\n", _when);
2575695Snate@binkert.org        mainEventQueue.schedule(this, _when);
258224SN/A    }
259224SN/A}
260224SN/A
2612SN/Avoid
262217SN/AEventQueue::serialize(ostream &os)
2632SN/A{
264265SN/A    std::list<Event *> eventPtrs;
265237SN/A
266237SN/A    int numEvents = 0;
2675502Snate@binkert.org    Event *nextBin = head;
2685502Snate@binkert.org    while (nextBin) {
2695503Snate@binkert.org        Event *nextInBin = nextBin;
2705502Snate@binkert.org
2715503Snate@binkert.org        while (nextInBin) {
2725769Snate@binkert.org            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
2735502Snate@binkert.org                eventPtrs.push_back(nextInBin);
2745502Snate@binkert.org                paramOut(os, csprintf("event%d", numEvents++),
2755502Snate@binkert.org                         nextInBin->name());
2765502Snate@binkert.org            }
2775502Snate@binkert.org            nextInBin = nextInBin->nextInBin;
2785503Snate@binkert.org        }
2795502Snate@binkert.org
2805502Snate@binkert.org        nextBin = nextBin->nextBin;
2812SN/A    }
282237SN/A
283265SN/A    SERIALIZE_SCALAR(numEvents);
284265SN/A
2855502Snate@binkert.org    for (std::list<Event *>::iterator it = eventPtrs.begin();
286265SN/A         it != eventPtrs.end(); ++it) {
287265SN/A        (*it)->nameOut(os);
288265SN/A        (*it)->serialize(os);
289265SN/A    }
2902SN/A}
2912SN/A
292237SN/Avoid
293237SN/AEventQueue::unserialize(Checkpoint *cp, const std::string &section)
294237SN/A{
295265SN/A    int numEvents;
296265SN/A    UNSERIALIZE_SCALAR(numEvents);
297265SN/A
298270SN/A    std::string eventName;
299265SN/A    for (int i = 0; i < numEvents; i++) {
300265SN/A        // get the pointer value associated with the event
301270SN/A        paramIn(cp, section, csprintf("event%d", i), eventName);
302265SN/A
303265SN/A        // create the event based on its pointer value
304395SN/A        Serializable::create(cp, eventName);
305237SN/A    }
306237SN/A}
307237SN/A
3082SN/Avoid
3095501Snate@binkert.orgEventQueue::dump() const
3102SN/A{
3112SN/A    cprintf("============================================================\n");
3122SN/A    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
3132SN/A    cprintf("------------------------------------------------------------\n");
3142SN/A
3152SN/A    if (empty())
3162SN/A        cprintf("<No Events>\n");
3172SN/A    else {
3185502Snate@binkert.org        Event *nextBin = head;
3195502Snate@binkert.org        while (nextBin) {
3205502Snate@binkert.org            Event *nextInBin = nextBin;
3215503Snate@binkert.org            while (nextInBin) {
3225503Snate@binkert.org                nextInBin->dump();
3235502Snate@binkert.org                nextInBin = nextInBin->nextInBin;
3245503Snate@binkert.org            }
3255502Snate@binkert.org
3265502Snate@binkert.org            nextBin = nextBin->nextBin;
3272SN/A        }
3282SN/A    }
3292SN/A
3302SN/A    cprintf("============================================================\n");
3312SN/A}
3322SN/A
3335502Snate@binkert.orgbool
3345502Snate@binkert.orgEventQueue::debugVerify() const
3355502Snate@binkert.org{
3365502Snate@binkert.org    m5::hash_map<long, bool> map;
3375502Snate@binkert.org
3385502Snate@binkert.org    Tick time = 0;
3395502Snate@binkert.org    short priority = 0;
3405502Snate@binkert.org
3415502Snate@binkert.org    Event *nextBin = head;
3425502Snate@binkert.org    while (nextBin) {
3435503Snate@binkert.org        Event *nextInBin = nextBin;
3445503Snate@binkert.org        while (nextInBin) {
3455502Snate@binkert.org            if (nextInBin->when() < time) {
3465502Snate@binkert.org                cprintf("time goes backwards!");
3475502Snate@binkert.org                nextInBin->dump();
3485502Snate@binkert.org                return false;
3495502Snate@binkert.org            } else if (nextInBin->when() == time &&
3505502Snate@binkert.org                       nextInBin->priority() < priority) {
3515502Snate@binkert.org                cprintf("priority inverted!");
3525502Snate@binkert.org                nextInBin->dump();
3535502Snate@binkert.org                return false;
3545502Snate@binkert.org            }
3555502Snate@binkert.org
3565502Snate@binkert.org            if (map[reinterpret_cast<long>(nextInBin)]) {
3575502Snate@binkert.org                cprintf("Node already seen");
3585502Snate@binkert.org                nextInBin->dump();
3595502Snate@binkert.org                return false;
3605502Snate@binkert.org            }
3615502Snate@binkert.org            map[reinterpret_cast<long>(nextInBin)] = true;
3625502Snate@binkert.org
3635502Snate@binkert.org            time = nextInBin->when();
3645502Snate@binkert.org            priority = nextInBin->priority();
3655502Snate@binkert.org
3665502Snate@binkert.org            nextInBin = nextInBin->nextInBin;
3675503Snate@binkert.org        }
3685502Snate@binkert.org
3695502Snate@binkert.org        nextBin = nextBin->nextBin;
3705502Snate@binkert.org    }
3715502Snate@binkert.org
3725502Snate@binkert.org    return true;
3735502Snate@binkert.org}
3745502Snate@binkert.org
3751019SN/Avoid
3761019SN/AdumpMainQueue()
3771019SN/A{
3781019SN/A    mainEventQueue.dump();
3791019SN/A}
3801019SN/A
3812SN/A
3822SN/Aconst char *
3835336Shines@cs.fsu.eduEvent::description() const
3842SN/A{
3852SN/A    return "generic";
3862SN/A}
3872SN/A
3882SN/Avoid
3892SN/AEvent::trace(const char *action)
3902SN/A{
3912SN/A    // This DPRINTF is unconditional because calls to this function
3922SN/A    // are protected by an 'if (DTRACE(Event))' in the inlined Event
3932SN/A    // methods.
3942SN/A    //
3952SN/A    // This is just a default implementation for derived classes where
3962SN/A    // it's not worth doing anything special.  If you want to put a
3972SN/A    // more informative message in the trace, override this method on
3982SN/A    // the particular subclass where you have the information that
3992SN/A    // needs to be printed.
4002SN/A    DPRINTFN("%s event %s @ %d\n", description(), action, when());
4012SN/A}
4022SN/A
4032SN/Avoid
4045501Snate@binkert.orgEvent::dump() const
4052SN/A{
4065501Snate@binkert.org    cprintf("Event %s (%s)\n", name(), description());
4075769Snate@binkert.org    cprintf("Flags: %#x\n", flags);
4085501Snate@binkert.org#ifdef EVENTQ_DEBUG
4095501Snate@binkert.org    cprintf("Created: %d\n", whenCreated);
4102SN/A#endif
4112SN/A    if (scheduled()) {
4125501Snate@binkert.org#ifdef EVENTQ_DEBUG
4135501Snate@binkert.org        cprintf("Scheduled at  %d\n", whenScheduled);
4142SN/A#endif
4151019SN/A        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
4165501Snate@binkert.org    } else {
4171019SN/A        cprintf("Not Scheduled\n");
4182SN/A    }
4192SN/A}
4207063Snate@binkert.org
4217063Snate@binkert.orgEventQueue::EventQueue(const string &n)
4227063Snate@binkert.org    : objName(n), head(NULL)
4237063Snate@binkert.org{}
424