eventq.cc revision 10153
17754SWilliam.Wang@arm.com/*
27754SWilliam.Wang@arm.com * Copyright (c) 2000-2005 The Regents of The University of Michigan
37754SWilliam.Wang@arm.com * Copyright (c) 2008 The Hewlett-Packard Development Company
47754SWilliam.Wang@arm.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
57754SWilliam.Wang@arm.com * All rights reserved.
67754SWilliam.Wang@arm.com *
77754SWilliam.Wang@arm.com * Redistribution and use in source and binary forms, with or without
87754SWilliam.Wang@arm.com * modification, are permitted provided that the following conditions are
97754SWilliam.Wang@arm.com * met: redistributions of source code must retain the above copyright
107754SWilliam.Wang@arm.com * notice, this list of conditions and the following disclaimer;
117754SWilliam.Wang@arm.com * redistributions in binary form must reproduce the above copyright
127754SWilliam.Wang@arm.com * notice, this list of conditions and the following disclaimer in the
137754SWilliam.Wang@arm.com * documentation and/or other materials provided with the distribution;
147754SWilliam.Wang@arm.com * neither the name of the copyright holders nor the names of its
157754SWilliam.Wang@arm.com * contributors may be used to endorse or promote products derived from
167754SWilliam.Wang@arm.com * this software without specific prior written permission.
177754SWilliam.Wang@arm.com *
187754SWilliam.Wang@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197754SWilliam.Wang@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207754SWilliam.Wang@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
217754SWilliam.Wang@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227754SWilliam.Wang@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
237754SWilliam.Wang@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
247754SWilliam.Wang@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257754SWilliam.Wang@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267754SWilliam.Wang@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277754SWilliam.Wang@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287754SWilliam.Wang@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297754SWilliam.Wang@arm.com *
307754SWilliam.Wang@arm.com * Authors: Steve Reinhardt
317754SWilliam.Wang@arm.com *          Nathan Binkert
327754SWilliam.Wang@arm.com *          Steve Raasch
337754SWilliam.Wang@arm.com */
347754SWilliam.Wang@arm.com
357754SWilliam.Wang@arm.com#include <cassert>
367754SWilliam.Wang@arm.com#include <iostream>
377754SWilliam.Wang@arm.com#include <string>
387754SWilliam.Wang@arm.com#include <vector>
397754SWilliam.Wang@arm.com
407950SAli.Saidi@ARM.com#include "base/hashmap.hh"
417950SAli.Saidi@ARM.com#include "base/misc.hh"
427754SWilliam.Wang@arm.com#include "base/trace.hh"
437754SWilliam.Wang@arm.com#include "cpu/smt.hh"
4411793Sbrandon.potter@amd.com#include "debug/Config.hh"
4511793Sbrandon.potter@amd.com#include "sim/core.hh"
4611793Sbrandon.potter@amd.com#include "sim/eventq_impl.hh"
479330Schander.sudanthi@arm.com
488245Snate@binkert.orgusing namespace std;
497754SWilliam.Wang@arm.com
507950SAli.Saidi@ARM.comTick simQuantum = 0;
517754SWilliam.Wang@arm.com
527754SWilliam.Wang@arm.com//
537754SWilliam.Wang@arm.com// Main Event Queues
547754SWilliam.Wang@arm.com//
559808Sstever@gmail.com// Events on these queues are processed at the *beginning* of each
569808Sstever@gmail.com// cycle, before the pipeline simulation is performed.
579808Sstever@gmail.com//
587754SWilliam.Wang@arm.comuint32_t numMainEventQueues = 0;
597950SAli.Saidi@ARM.comvector<EventQueue *> mainEventQueue;
607950SAli.Saidi@ARM.com__thread EventQueue *_curEventQueue = NULL;
617950SAli.Saidi@ARM.combool inParallelMode = false;
627950SAli.Saidi@ARM.com
637950SAli.Saidi@ARM.comEventQueue *
647950SAli.Saidi@ARM.comgetEventQueue(uint32_t index)
657754SWilliam.Wang@arm.com{
667754SWilliam.Wang@arm.com    while (numMainEventQueues <= index) {
677754SWilliam.Wang@arm.com        numMainEventQueues++;
687754SWilliam.Wang@arm.com        mainEventQueue.push_back(
697754SWilliam.Wang@arm.com            new EventQueue(csprintf("MainEventQueue-%d", index)));
707754SWilliam.Wang@arm.com    }
717754SWilliam.Wang@arm.com
727754SWilliam.Wang@arm.com    return mainEventQueue[index];
737754SWilliam.Wang@arm.com}
747754SWilliam.Wang@arm.com
757754SWilliam.Wang@arm.com#ifndef NDEBUG
767754SWilliam.Wang@arm.comCounter Event::instanceCounter = 0;
777754SWilliam.Wang@arm.com#endif
787950SAli.Saidi@ARM.com
797754SWilliam.Wang@arm.comEvent::~Event()
807754SWilliam.Wang@arm.com{
817754SWilliam.Wang@arm.com    assert(!scheduled());
827950SAli.Saidi@ARM.com    flags = 0;
837950SAli.Saidi@ARM.com}
847950SAli.Saidi@ARM.com
857950SAli.Saidi@ARM.comconst std::string
867950SAli.Saidi@ARM.comEvent::name() const
877950SAli.Saidi@ARM.com{
887754SWilliam.Wang@arm.com#ifndef NDEBUG
897754SWilliam.Wang@arm.com    return csprintf("Event_%d", instance);
907754SWilliam.Wang@arm.com#else
917950SAli.Saidi@ARM.com    return csprintf("Event_%x", (uintptr_t)this);
927950SAli.Saidi@ARM.com#endif
937950SAli.Saidi@ARM.com}
947950SAli.Saidi@ARM.com
957950SAli.Saidi@ARM.com
967950SAli.Saidi@ARM.comEvent *
977950SAli.Saidi@ARM.comEvent::insertBefore(Event *event, Event *curr)
987950SAli.Saidi@ARM.com{
997754SWilliam.Wang@arm.com    // Either way, event will be the top element in the 'in bin' list
1007754SWilliam.Wang@arm.com    // which is the pointer we need in order to look into the list, so
1017754SWilliam.Wang@arm.com    // we need to insert that into the bin list.
1027754SWilliam.Wang@arm.com    if (!curr || *event < *curr) {
1037754SWilliam.Wang@arm.com        // Insert the event before the current list since it is in the future.
1047950SAli.Saidi@ARM.com        event->nextBin = curr;
1057950SAli.Saidi@ARM.com        event->nextInBin = NULL;
1067754SWilliam.Wang@arm.com    } else {
1077754SWilliam.Wang@arm.com        // Since we're on the correct list, we need to point to the next list
1089806Sstever@gmail.com        event->nextBin = curr->nextBin;  // curr->nextBin can now become stale
1097754SWilliam.Wang@arm.com
1107754SWilliam.Wang@arm.com        // Insert event at the top of the stack
1117754SWilliam.Wang@arm.com        event->nextInBin = curr;
1127754SWilliam.Wang@arm.com    }
1137754SWilliam.Wang@arm.com
1147754SWilliam.Wang@arm.com    return event;
1157754SWilliam.Wang@arm.com}
1167754SWilliam.Wang@arm.com
1177754SWilliam.Wang@arm.comvoid
1187754SWilliam.Wang@arm.comEventQueue::insert(Event *event)
1197754SWilliam.Wang@arm.com{
1207754SWilliam.Wang@arm.com    // Deal with the head case
1217754SWilliam.Wang@arm.com    if (!head || *event <= *head) {
1227754SWilliam.Wang@arm.com        head = Event::insertBefore(event, head);
1237754SWilliam.Wang@arm.com        return;
1247754SWilliam.Wang@arm.com    }
1257754SWilliam.Wang@arm.com
1267754SWilliam.Wang@arm.com    // Figure out either which 'in bin' list we are on, or where a new list
1277754SWilliam.Wang@arm.com    // needs to be inserted
1287754SWilliam.Wang@arm.com    Event *prev = head;
1297754SWilliam.Wang@arm.com    Event *curr = head->nextBin;
1307754SWilliam.Wang@arm.com    while (curr && *curr < *event) {
1317754SWilliam.Wang@arm.com        prev = curr;
1327754SWilliam.Wang@arm.com        curr = curr->nextBin;
1337754SWilliam.Wang@arm.com    }
1347754SWilliam.Wang@arm.com
1357754SWilliam.Wang@arm.com    // Note: this operation may render all nextBin pointers on the
1367754SWilliam.Wang@arm.com    // prev 'in bin' list stale (except for the top one)
1377754SWilliam.Wang@arm.com    prev->nextBin = Event::insertBefore(event, curr);
1387754SWilliam.Wang@arm.com}
1397754SWilliam.Wang@arm.com
1407754SWilliam.Wang@arm.comEvent *
1417754SWilliam.Wang@arm.comEvent::removeItem(Event *event, Event *top)
1427754SWilliam.Wang@arm.com{
1437754SWilliam.Wang@arm.com    Event *curr = top;
1447754SWilliam.Wang@arm.com    Event *next = top->nextInBin;
1457950SAli.Saidi@ARM.com
1467754SWilliam.Wang@arm.com    // if we removed the top item, we need to handle things specially
1477754SWilliam.Wang@arm.com    // and just remove the top item, fixing up the next bin pointer of
1487754SWilliam.Wang@arm.com    // the new top item
1497754SWilliam.Wang@arm.com    if (event == top) {
1507950SAli.Saidi@ARM.com        if (!next)
1517950SAli.Saidi@ARM.com            return top->nextBin;
1527950SAli.Saidi@ARM.com        next->nextBin = top->nextBin;
1537754SWilliam.Wang@arm.com        return next;
1547754SWilliam.Wang@arm.com    }
1557950SAli.Saidi@ARM.com
1567950SAli.Saidi@ARM.com    // Since we already checked the current element, we're going to
1577950SAli.Saidi@ARM.com    // keep checking event against the next element.
1587754SWilliam.Wang@arm.com    while (event != next) {
1597754SWilliam.Wang@arm.com        if (!next)
1607950SAli.Saidi@ARM.com            panic("event not found!");
1617754SWilliam.Wang@arm.com
1627754SWilliam.Wang@arm.com        curr = next;
1637754SWilliam.Wang@arm.com        next = next->nextInBin;
1647754SWilliam.Wang@arm.com    }
1657754SWilliam.Wang@arm.com
1667754SWilliam.Wang@arm.com    // remove next from the 'in bin' list since it's what we're looking for
1677754SWilliam.Wang@arm.com    curr->nextInBin = next->nextInBin;
1687754SWilliam.Wang@arm.com    return top;
1697754SWilliam.Wang@arm.com}
1707754SWilliam.Wang@arm.com
1717950SAli.Saidi@ARM.comvoid
1727950SAli.Saidi@ARM.comEventQueue::remove(Event *event)
1737950SAli.Saidi@ARM.com{
1747950SAli.Saidi@ARM.com    if (head == NULL)
1757950SAli.Saidi@ARM.com        panic("event not found!");
1767950SAli.Saidi@ARM.com
1777950SAli.Saidi@ARM.com    assert(event->queue == this);
1787950SAli.Saidi@ARM.com
1797950SAli.Saidi@ARM.com    // deal with an event on the head's 'in bin' list (event has the same
1807950SAli.Saidi@ARM.com    // time as the head)
1817950SAli.Saidi@ARM.com    if (*head == *event) {
1827950SAli.Saidi@ARM.com        head = Event::removeItem(event, head);
1837950SAli.Saidi@ARM.com        return;
1847950SAli.Saidi@ARM.com    }
1857950SAli.Saidi@ARM.com
1867950SAli.Saidi@ARM.com    // Find the 'in bin' list that this event belongs on
1877950SAli.Saidi@ARM.com    Event *prev = head;
1887950SAli.Saidi@ARM.com    Event *curr = head->nextBin;
1897950SAli.Saidi@ARM.com    while (curr && *curr < *event) {
1907950SAli.Saidi@ARM.com        prev = curr;
1917950SAli.Saidi@ARM.com        curr = curr->nextBin;
1927950SAli.Saidi@ARM.com    }
1937950SAli.Saidi@ARM.com
1947950SAli.Saidi@ARM.com    if (!curr || *curr != *event)
1957950SAli.Saidi@ARM.com        panic("event not found!");
1967950SAli.Saidi@ARM.com
1977950SAli.Saidi@ARM.com    // curr points to the top item of the the correct 'in bin' list, when
1987950SAli.Saidi@ARM.com    // we remove an item, it returns the new top item (which may be
1997950SAli.Saidi@ARM.com    // unchanged)
2007950SAli.Saidi@ARM.com    prev->nextBin = Event::removeItem(event, curr);
2017950SAli.Saidi@ARM.com}
2027950SAli.Saidi@ARM.com
2037950SAli.Saidi@ARM.comEvent *
2047950SAli.Saidi@ARM.comEventQueue::serviceOne()
2057950SAli.Saidi@ARM.com{
2067950SAli.Saidi@ARM.com    std::lock_guard<EventQueue> lock(*this);
2077950SAli.Saidi@ARM.com    Event *event = head;
2087950SAli.Saidi@ARM.com    Event *next = head->nextInBin;
2097950SAli.Saidi@ARM.com    event->flags.clear(Event::Scheduled);
2107950SAli.Saidi@ARM.com
2117950SAli.Saidi@ARM.com    if (next) {
2129049Schander.sudanthi@arm.com        // update the next bin pointer since it could be stale
2137950SAli.Saidi@ARM.com        next->nextBin = head->nextBin;
2147950SAli.Saidi@ARM.com
2157950SAli.Saidi@ARM.com        // pop the stack
2167950SAli.Saidi@ARM.com        head = next;
2177950SAli.Saidi@ARM.com    } else {
2187950SAli.Saidi@ARM.com        // this was the only element on the 'in bin' list, so get rid of
2197950SAli.Saidi@ARM.com        // the 'in bin' list and point to the next bin list
2207950SAli.Saidi@ARM.com        head = head->nextBin;
2217950SAli.Saidi@ARM.com    }
2227950SAli.Saidi@ARM.com
2237950SAli.Saidi@ARM.com    // handle action
2247950SAli.Saidi@ARM.com    if (!event->squashed()) {
2257950SAli.Saidi@ARM.com        // forward current cycle to the time when this event occurs.
2267950SAli.Saidi@ARM.com        setCurTick(event->when());
2277950SAli.Saidi@ARM.com
2287950SAli.Saidi@ARM.com        event->process();
2297950SAli.Saidi@ARM.com        if (event->isExitEvent()) {
2307950SAli.Saidi@ARM.com            assert(!event->flags.isSet(Event::AutoDelete) ||
2317950SAli.Saidi@ARM.com                   !event->flags.isSet(Event::IsMainQueue)); // would be silly
2327950SAli.Saidi@ARM.com            return event;
2337950SAli.Saidi@ARM.com        }
2347950SAli.Saidi@ARM.com    } else {
2357950SAli.Saidi@ARM.com        event->flags.clear(Event::Squashed);
2367950SAli.Saidi@ARM.com    }
2377950SAli.Saidi@ARM.com
2387950SAli.Saidi@ARM.com    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
2397950SAli.Saidi@ARM.com        delete event;
2407950SAli.Saidi@ARM.com
2417950SAli.Saidi@ARM.com    return NULL;
2427950SAli.Saidi@ARM.com}
2437950SAli.Saidi@ARM.com
2447950SAli.Saidi@ARM.comvoid
2457950SAli.Saidi@ARM.comEvent::serialize(std::ostream &os)
2467950SAli.Saidi@ARM.com{
2477950SAli.Saidi@ARM.com    SERIALIZE_SCALAR(_when);
2487950SAli.Saidi@ARM.com    SERIALIZE_SCALAR(_priority);
2497950SAli.Saidi@ARM.com    short _flags = flags;
2507950SAli.Saidi@ARM.com    SERIALIZE_SCALAR(_flags);
2517950SAli.Saidi@ARM.com}
2527950SAli.Saidi@ARM.com
2537950SAli.Saidi@ARM.comvoid
2547950SAli.Saidi@ARM.comEvent::unserialize(Checkpoint *cp, const string &section)
2557950SAli.Saidi@ARM.com{
2567950SAli.Saidi@ARM.com}
2577754SWilliam.Wang@arm.com
2587754SWilliam.Wang@arm.comvoid
2597950SAli.Saidi@ARM.comEvent::unserialize(Checkpoint *cp, const string &section, EventQueue *eventq)
2607950SAli.Saidi@ARM.com{
2617754SWilliam.Wang@arm.com    if (scheduled())
2627950SAli.Saidi@ARM.com        eventq->deschedule(this);
2637754SWilliam.Wang@arm.com
2647754SWilliam.Wang@arm.com    UNSERIALIZE_SCALAR(_when);
2657754SWilliam.Wang@arm.com    UNSERIALIZE_SCALAR(_priority);
2667950SAli.Saidi@ARM.com
2677950SAli.Saidi@ARM.com    short _flags;
2687950SAli.Saidi@ARM.com    UNSERIALIZE_SCALAR(_flags);
2697950SAli.Saidi@ARM.com
2707950SAli.Saidi@ARM.com    // Old checkpoints had no concept of the Initialized flag
2717950SAli.Saidi@ARM.com    // so restoring from old checkpoints always fail.
2727950SAli.Saidi@ARM.com    // Events are initialized on construction but original code
2737950SAli.Saidi@ARM.com    // "flags = _flags" would just overwrite the initialization.
2747950SAli.Saidi@ARM.com    // So, read in the checkpoint flags, but then set the Initialized
2757950SAli.Saidi@ARM.com    // flag on top of it in order to avoid failures.
2767950SAli.Saidi@ARM.com    assert(initialized());
2777950SAli.Saidi@ARM.com    flags = _flags;
2787950SAli.Saidi@ARM.com    flags.set(Initialized);
2797950SAli.Saidi@ARM.com
2807950SAli.Saidi@ARM.com    // need to see if original event was in a scheduled, unsquashed
2817950SAli.Saidi@ARM.com    // state, but don't want to restore those flags in the current
2827950SAli.Saidi@ARM.com    // object itself (since they aren't immediately true)
2837950SAli.Saidi@ARM.com    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
2847950SAli.Saidi@ARM.com    flags.clear(Squashed | Scheduled);
2857950SAli.Saidi@ARM.com
2867950SAli.Saidi@ARM.com    if (wasScheduled) {
2877950SAli.Saidi@ARM.com        DPRINTF(Config, "rescheduling at %d\n", _when);
2887950SAli.Saidi@ARM.com        eventq->schedule(this, _when);
2897950SAli.Saidi@ARM.com    }
2907950SAli.Saidi@ARM.com}
2917950SAli.Saidi@ARM.com
2927950SAli.Saidi@ARM.comvoid
2937950SAli.Saidi@ARM.comEventQueue::serialize(ostream &os)
2947950SAli.Saidi@ARM.com{
2957950SAli.Saidi@ARM.com    std::list<Event *> eventPtrs;
2967950SAli.Saidi@ARM.com
2977950SAli.Saidi@ARM.com    int numEvents = 0;
2987950SAli.Saidi@ARM.com    Event *nextBin = head;
2997950SAli.Saidi@ARM.com    while (nextBin) {
3007950SAli.Saidi@ARM.com        Event *nextInBin = nextBin;
3017950SAli.Saidi@ARM.com
3027950SAli.Saidi@ARM.com        while (nextInBin) {
3037950SAli.Saidi@ARM.com            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
3047950SAli.Saidi@ARM.com                eventPtrs.push_back(nextInBin);
3057950SAli.Saidi@ARM.com                paramOut(os, csprintf("event%d", numEvents++),
3067950SAli.Saidi@ARM.com                         nextInBin->name());
3077950SAli.Saidi@ARM.com            }
3087950SAli.Saidi@ARM.com            nextInBin = nextInBin->nextInBin;
3097950SAli.Saidi@ARM.com        }
3107950SAli.Saidi@ARM.com
3117950SAli.Saidi@ARM.com        nextBin = nextBin->nextBin;
31210905Sandreas.sandberg@arm.com    }
3137950SAli.Saidi@ARM.com
3147950SAli.Saidi@ARM.com    SERIALIZE_SCALAR(numEvents);
3157950SAli.Saidi@ARM.com
3167950SAli.Saidi@ARM.com    for (std::list<Event *>::iterator it = eventPtrs.begin();
3177950SAli.Saidi@ARM.com         it != eventPtrs.end(); ++it) {
3187950SAli.Saidi@ARM.com        (*it)->nameOut(os);
3197950SAli.Saidi@ARM.com        (*it)->serialize(os);
3207950SAli.Saidi@ARM.com    }
3217950SAli.Saidi@ARM.com}
3227950SAli.Saidi@ARM.com
3237950SAli.Saidi@ARM.comvoid
3247950SAli.Saidi@ARM.comEventQueue::unserialize(Checkpoint *cp, const std::string &section)
3257950SAli.Saidi@ARM.com{
3267950SAli.Saidi@ARM.com    int numEvents;
3277950SAli.Saidi@ARM.com    UNSERIALIZE_SCALAR(numEvents);
3287950SAli.Saidi@ARM.com
3297950SAli.Saidi@ARM.com    std::string eventName;
3307950SAli.Saidi@ARM.com    for (int i = 0; i < numEvents; i++) {
33110905Sandreas.sandberg@arm.com        // get the pointer value associated with the event
3327950SAli.Saidi@ARM.com        paramIn(cp, section, csprintf("event%d", i), eventName);
3337950SAli.Saidi@ARM.com
3347950SAli.Saidi@ARM.com        // create the event based on its pointer value
33510905Sandreas.sandberg@arm.com        Serializable::create(cp, eventName);
3367950SAli.Saidi@ARM.com    }
3377950SAli.Saidi@ARM.com}
3387950SAli.Saidi@ARM.com
3397950SAli.Saidi@ARM.comvoid
3407950SAli.Saidi@ARM.comEventQueue::dump() const
3417950SAli.Saidi@ARM.com{
3427950SAli.Saidi@ARM.com    cprintf("============================================================\n");
3437950SAli.Saidi@ARM.com    cprintf("EventQueue Dump  (cycle %d)\n", curTick());
3447950SAli.Saidi@ARM.com    cprintf("------------------------------------------------------------\n");
3457950SAli.Saidi@ARM.com
3467950SAli.Saidi@ARM.com    if (empty())
3477950SAli.Saidi@ARM.com        cprintf("<No Events>\n");
3487950SAli.Saidi@ARM.com    else {
3497950SAli.Saidi@ARM.com        Event *nextBin = head;
3507950SAli.Saidi@ARM.com        while (nextBin) {
3517950SAli.Saidi@ARM.com            Event *nextInBin = nextBin;
3527950SAli.Saidi@ARM.com            while (nextInBin) {
3537950SAli.Saidi@ARM.com                nextInBin->dump();
3547950SAli.Saidi@ARM.com                nextInBin = nextInBin->nextInBin;
3557950SAli.Saidi@ARM.com            }
3567950SAli.Saidi@ARM.com
3577950SAli.Saidi@ARM.com            nextBin = nextBin->nextBin;
3587950SAli.Saidi@ARM.com        }
35910905Sandreas.sandberg@arm.com    }
3607950SAli.Saidi@ARM.com
3617950SAli.Saidi@ARM.com    cprintf("============================================================\n");
3627950SAli.Saidi@ARM.com}
3637950SAli.Saidi@ARM.com
3647754SWilliam.Wang@arm.combool
3657754SWilliam.Wang@arm.comEventQueue::debugVerify() const
3667754SWilliam.Wang@arm.com{
3677754SWilliam.Wang@arm.com    m5::hash_map<long, bool> map;
3687754SWilliam.Wang@arm.com
369    Tick time = 0;
370    short priority = 0;
371
372    Event *nextBin = head;
373    while (nextBin) {
374        Event *nextInBin = nextBin;
375        while (nextInBin) {
376            if (nextInBin->when() < time) {
377                cprintf("time goes backwards!");
378                nextInBin->dump();
379                return false;
380            } else if (nextInBin->when() == time &&
381                       nextInBin->priority() < priority) {
382                cprintf("priority inverted!");
383                nextInBin->dump();
384                return false;
385            }
386
387            if (map[reinterpret_cast<long>(nextInBin)]) {
388                cprintf("Node already seen");
389                nextInBin->dump();
390                return false;
391            }
392            map[reinterpret_cast<long>(nextInBin)] = true;
393
394            time = nextInBin->when();
395            priority = nextInBin->priority();
396
397            nextInBin = nextInBin->nextInBin;
398        }
399
400        nextBin = nextBin->nextBin;
401    }
402
403    return true;
404}
405
406Event*
407EventQueue::replaceHead(Event* s)
408{
409    Event* t = head;
410    head = s;
411    return t;
412}
413
414void
415dumpMainQueue()
416{
417    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
418        mainEventQueue[i]->dump();
419    }
420}
421
422
423const char *
424Event::description() const
425{
426    return "generic";
427}
428
429void
430Event::trace(const char *action)
431{
432    // This DPRINTF is unconditional because calls to this function
433    // are protected by an 'if (DTRACE(Event))' in the inlined Event
434    // methods.
435    //
436    // This is just a default implementation for derived classes where
437    // it's not worth doing anything special.  If you want to put a
438    // more informative message in the trace, override this method on
439    // the particular subclass where you have the information that
440    // needs to be printed.
441    DPRINTFN("%s event %s @ %d\n", description(), action, when());
442}
443
444void
445Event::dump() const
446{
447    cprintf("Event %s (%s)\n", name(), description());
448    cprintf("Flags: %#x\n", flags);
449#ifdef EVENTQ_DEBUG
450    cprintf("Created: %d\n", whenCreated);
451#endif
452    if (scheduled()) {
453#ifdef EVENTQ_DEBUG
454        cprintf("Scheduled at  %d\n", whenScheduled);
455#endif
456        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
457    } else {
458        cprintf("Not Scheduled\n");
459    }
460}
461
462EventQueue::EventQueue(const string &n)
463    : objName(n), head(NULL), _curTick(0),
464    async_queue_mutex(new std::mutex())
465{
466}
467
468void
469EventQueue::asyncInsert(Event *event)
470{
471    async_queue_mutex->lock();
472    async_queue.push_back(event);
473    async_queue_mutex->unlock();
474}
475
476void
477EventQueue::handleAsyncInsertions()
478{
479    assert(this == curEventQueue());
480    async_queue_mutex->lock();
481
482    while (!async_queue.empty()) {
483        insert(async_queue.front());
484        async_queue.pop_front();
485    }
486
487    async_queue_mutex->unlock();
488}
489