eventq.cc revision 5774
17202Sgblack@eecs.umich.edu/*
27202Sgblack@eecs.umich.edu * Copyright (c) 2000-2005 The Regents of The University of Michigan
37202Sgblack@eecs.umich.edu * Copyright (c) 2008 The Hewlett-Packard Development Company
47202Sgblack@eecs.umich.edu * All rights reserved.
57202Sgblack@eecs.umich.edu *
67202Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
77202Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
87202Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
97202Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
107202Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
117202Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
127202Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
137202Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
147202Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
157202Sgblack@eecs.umich.edu * this software without specific prior written permission.
167202Sgblack@eecs.umich.edu *
177202Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187202Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197202Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
207202Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
217202Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
227202Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
237202Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247202Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257202Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267202Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
277202Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287202Sgblack@eecs.umich.edu *
297202Sgblack@eecs.umich.edu * Authors: Steve Reinhardt
307202Sgblack@eecs.umich.edu *          Nathan Binkert
317202Sgblack@eecs.umich.edu *          Steve Raasch
327202Sgblack@eecs.umich.edu */
337202Sgblack@eecs.umich.edu
347202Sgblack@eecs.umich.edu#include <cassert>
357202Sgblack@eecs.umich.edu#include <iostream>
367202Sgblack@eecs.umich.edu#include <string>
377202Sgblack@eecs.umich.edu#include <vector>
387202Sgblack@eecs.umich.edu
397202Sgblack@eecs.umich.edu#include "base/hashmap.hh"
407202Sgblack@eecs.umich.edu#include "base/misc.hh"
417202Sgblack@eecs.umich.edu#include "base/trace.hh"
427202Sgblack@eecs.umich.edu#include "cpu/smt.hh"
437202Sgblack@eecs.umich.edu#include "sim/core.hh"
447202Sgblack@eecs.umich.edu#include "sim/eventq.hh"
457202Sgblack@eecs.umich.edu
467202Sgblack@eecs.umich.eduusing namespace std;
477202Sgblack@eecs.umich.edu
487202Sgblack@eecs.umich.edu//
497202Sgblack@eecs.umich.edu// Main Event Queue
507202Sgblack@eecs.umich.edu//
517202Sgblack@eecs.umich.edu// Events on this queue are processed at the *beginning* of each
527202Sgblack@eecs.umich.edu// cycle, before the pipeline simulation is performed.
537202Sgblack@eecs.umich.edu//
547202Sgblack@eecs.umich.eduEventQueue mainEventQueue("Main Event Queue");
557202Sgblack@eecs.umich.edu
567202Sgblack@eecs.umich.edu#ifndef NDEBUG
577202Sgblack@eecs.umich.eduCounter Event::instanceCounter = 0;
587202Sgblack@eecs.umich.edu#endif
597202Sgblack@eecs.umich.edu
607202Sgblack@eecs.umich.eduEvent::~Event()
617202Sgblack@eecs.umich.edu{
627202Sgblack@eecs.umich.edu    assert(!scheduled());
637202Sgblack@eecs.umich.edu}
647202Sgblack@eecs.umich.edu
657202Sgblack@eecs.umich.educonst std::string
667202Sgblack@eecs.umich.eduEvent::name() const
677202Sgblack@eecs.umich.edu{
687202Sgblack@eecs.umich.edu#ifndef NDEBUG
697202Sgblack@eecs.umich.edu    return csprintf("Event_%d", instance);
707202Sgblack@eecs.umich.edu#else
717202Sgblack@eecs.umich.edu    return csprintf("Event_%x", (uintptr_t)this);
727202Sgblack@eecs.umich.edu#endif
737202Sgblack@eecs.umich.edu}
747202Sgblack@eecs.umich.edu
757202Sgblack@eecs.umich.edu
767202Sgblack@eecs.umich.eduEvent *
777202Sgblack@eecs.umich.eduEvent::insertBefore(Event *event, Event *curr)
787202Sgblack@eecs.umich.edu{
797202Sgblack@eecs.umich.edu    // Either way, event will be the top element in the 'in bin' list
807202Sgblack@eecs.umich.edu    // which is the pointer we need in order to look into the list, so
817202Sgblack@eecs.umich.edu    // we need to insert that into the bin list.
827202Sgblack@eecs.umich.edu    if (!curr || *event < *curr) {
837202Sgblack@eecs.umich.edu        // Insert the event before the current list since it is in the future.
847202Sgblack@eecs.umich.edu        event->nextBin = curr;
857202Sgblack@eecs.umich.edu        event->nextInBin = NULL;
867202Sgblack@eecs.umich.edu    } else {
877202Sgblack@eecs.umich.edu        // Since we're on the correct list, we need to point to the next list
887202Sgblack@eecs.umich.edu        event->nextBin = curr->nextBin;  // curr->nextBin can now become stale
897202Sgblack@eecs.umich.edu
907202Sgblack@eecs.umich.edu        // Insert event at the top of the stack
917202Sgblack@eecs.umich.edu        event->nextInBin = curr;
927202Sgblack@eecs.umich.edu    }
937202Sgblack@eecs.umich.edu
947202Sgblack@eecs.umich.edu    return event;
957202Sgblack@eecs.umich.edu}
967202Sgblack@eecs.umich.edu
977202Sgblack@eecs.umich.eduvoid
987202Sgblack@eecs.umich.eduEventQueue::insert(Event *event)
997202Sgblack@eecs.umich.edu{
1007202Sgblack@eecs.umich.edu    // Deal with the head case
1017208Sgblack@eecs.umich.edu    if (!head || *event <= *head) {
1027208Sgblack@eecs.umich.edu        head = Event::insertBefore(event, head);
1037208Sgblack@eecs.umich.edu        return;
1047208Sgblack@eecs.umich.edu    }
1057208Sgblack@eecs.umich.edu
1067208Sgblack@eecs.umich.edu    // Figure out either which 'in bin' list we are on, or where a new list
1077208Sgblack@eecs.umich.edu    // needs to be inserted
1087208Sgblack@eecs.umich.edu    Event *prev = head;
1097208Sgblack@eecs.umich.edu    Event *curr = head->nextBin;
1107208Sgblack@eecs.umich.edu    while (curr && *curr < *event) {
1117208Sgblack@eecs.umich.edu        prev = curr;
1127208Sgblack@eecs.umich.edu        curr = curr->nextBin;
1137208Sgblack@eecs.umich.edu    }
1147208Sgblack@eecs.umich.edu
1157208Sgblack@eecs.umich.edu    // Note: this operation may render all nextBin pointers on the
1167208Sgblack@eecs.umich.edu    // prev 'in bin' list stale (except for the top one)
1177208Sgblack@eecs.umich.edu    prev->nextBin = Event::insertBefore(event, curr);
1187208Sgblack@eecs.umich.edu}
1197208Sgblack@eecs.umich.edu
1207208Sgblack@eecs.umich.eduEvent *
1217208Sgblack@eecs.umich.eduEvent::removeItem(Event *event, Event *top)
1227225Sgblack@eecs.umich.edu{
1237233Sgblack@eecs.umich.edu    Event *curr = top;
1247233Sgblack@eecs.umich.edu    Event *next = top->nextInBin;
1257233Sgblack@eecs.umich.edu
1267233Sgblack@eecs.umich.edu    // if we removed the top item, we need to handle things specially
1277233Sgblack@eecs.umich.edu    // and just remove the top item, fixing up the next bin pointer of
1287233Sgblack@eecs.umich.edu    // the new top item
1297233Sgblack@eecs.umich.edu    if (event == top) {
1307233Sgblack@eecs.umich.edu        if (!next)
1317233Sgblack@eecs.umich.edu            return top->nextBin;
1327233Sgblack@eecs.umich.edu        next->nextBin = top->nextBin;
1337233Sgblack@eecs.umich.edu        return next;
1347233Sgblack@eecs.umich.edu    }
1357233Sgblack@eecs.umich.edu
1367233Sgblack@eecs.umich.edu    // Since we already checked the current element, we're going to
1377233Sgblack@eecs.umich.edu    // keep checking event against the next element.
1387233Sgblack@eecs.umich.edu    while (event != next) {
1397233Sgblack@eecs.umich.edu        if (!next)
1407233Sgblack@eecs.umich.edu            panic("event not found!");
1417233Sgblack@eecs.umich.edu
1427233Sgblack@eecs.umich.edu        curr = next;
1437233Sgblack@eecs.umich.edu        next = next->nextInBin;
1447233Sgblack@eecs.umich.edu    }
1457233Sgblack@eecs.umich.edu
1467233Sgblack@eecs.umich.edu    // remove next from the 'in bin' list since it's what we're looking for
1477233Sgblack@eecs.umich.edu    curr->nextInBin = next->nextInBin;
1487233Sgblack@eecs.umich.edu    return top;
1497232Sgblack@eecs.umich.edu}
1507225Sgblack@eecs.umich.edu
1517225Sgblack@eecs.umich.eduvoid
1527225Sgblack@eecs.umich.eduEventQueue::remove(Event *event)
1537225Sgblack@eecs.umich.edu{
1547225Sgblack@eecs.umich.edu    if (head == NULL)
1557225Sgblack@eecs.umich.edu        panic("event not found!");
1567232Sgblack@eecs.umich.edu
1577225Sgblack@eecs.umich.edu    // deal with an event on the head's 'in bin' list (event has the same
1587225Sgblack@eecs.umich.edu    // time as the head)
1597225Sgblack@eecs.umich.edu    if (*head == *event) {
1607225Sgblack@eecs.umich.edu        head = Event::removeItem(event, head);
1617232Sgblack@eecs.umich.edu        return;
1627225Sgblack@eecs.umich.edu    }
1637225Sgblack@eecs.umich.edu
1647232Sgblack@eecs.umich.edu    // Find the 'in bin' list that this event belongs on
1657225Sgblack@eecs.umich.edu    Event *prev = head;
1667225Sgblack@eecs.umich.edu    Event *curr = head->nextBin;
1677232Sgblack@eecs.umich.edu    while (curr && *curr < *event) {
1687225Sgblack@eecs.umich.edu        prev = curr;
1697225Sgblack@eecs.umich.edu        curr = curr->nextBin;
1707225Sgblack@eecs.umich.edu    }
1717225Sgblack@eecs.umich.edu
1727225Sgblack@eecs.umich.edu    if (!curr || *curr != *event)
1737232Sgblack@eecs.umich.edu        panic("event not found!");
1747225Sgblack@eecs.umich.edu
1757225Sgblack@eecs.umich.edu    // curr points to the top item of the the correct 'in bin' list, when
1767225Sgblack@eecs.umich.edu    // we remove an item, it returns the new top item (which may be
1777225Sgblack@eecs.umich.edu    // unchanged)
1787225Sgblack@eecs.umich.edu    prev->nextBin = Event::removeItem(event, curr);
1797225Sgblack@eecs.umich.edu}
1807232Sgblack@eecs.umich.edu
1817225Sgblack@eecs.umich.eduEvent *
1827225Sgblack@eecs.umich.eduEventQueue::serviceOne()
1837225Sgblack@eecs.umich.edu{
1847225Sgblack@eecs.umich.edu    Event *event = head;
1857225Sgblack@eecs.umich.edu    Event *next = head->nextInBin;
1867232Sgblack@eecs.umich.edu    event->flags.clear(Event::Scheduled);
1877225Sgblack@eecs.umich.edu
1887225Sgblack@eecs.umich.edu    if (next) {
1897232Sgblack@eecs.umich.edu        // update the next bin pointer since it could be stale
1907225Sgblack@eecs.umich.edu        next->nextBin = head->nextBin;
1917225Sgblack@eecs.umich.edu
1927225Sgblack@eecs.umich.edu        // pop the stack
1937225Sgblack@eecs.umich.edu        head = next;
1947232Sgblack@eecs.umich.edu    } else {
1957225Sgblack@eecs.umich.edu        // this was the only element on the 'in bin' list, so get rid of
1967225Sgblack@eecs.umich.edu        // the 'in bin' list and point to the next bin list
1977225Sgblack@eecs.umich.edu        head = head->nextBin;
1987225Sgblack@eecs.umich.edu    }
199
200    // handle action
201    if (!event->squashed()) {
202        event->process();
203        if (event->isExitEvent()) {
204            assert(!event->flags.isSet(Event::AutoDelete)); // would be silly
205            return event;
206        }
207    } else {
208        event->flags.clear(Event::Squashed);
209    }
210
211    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
212        delete event;
213
214    return NULL;
215}
216
217void
218Event::serialize(std::ostream &os)
219{
220    SERIALIZE_SCALAR(_when);
221    SERIALIZE_SCALAR(_priority);
222    short _flags = flags;
223    SERIALIZE_SCALAR(_flags);
224}
225
226void
227Event::unserialize(Checkpoint *cp, const string &section)
228{
229    if (scheduled())
230        mainEventQueue.deschedule(this);
231
232    UNSERIALIZE_SCALAR(_when);
233    UNSERIALIZE_SCALAR(_priority);
234
235    // need to see if original event was in a scheduled, unsquashed
236    // state, but don't want to restore those flags in the current
237    // object itself (since they aren't immediately true)
238    short _flags;
239    UNSERIALIZE_SCALAR(_flags);
240    flags = _flags;
241
242    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
243    flags.clear(Squashed | Scheduled);
244
245    if (wasScheduled) {
246        DPRINTF(Config, "rescheduling at %d\n", _when);
247        mainEventQueue.schedule(this, _when);
248    }
249}
250
251void
252EventQueue::serialize(ostream &os)
253{
254    std::list<Event *> eventPtrs;
255
256    int numEvents = 0;
257    Event *nextBin = head;
258    while (nextBin) {
259        Event *nextInBin = nextBin;
260
261        while (nextInBin) {
262            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
263                eventPtrs.push_back(nextInBin);
264                paramOut(os, csprintf("event%d", numEvents++),
265                         nextInBin->name());
266            }
267            nextInBin = nextInBin->nextInBin;
268        }
269
270        nextBin = nextBin->nextBin;
271    }
272
273    SERIALIZE_SCALAR(numEvents);
274
275    for (std::list<Event *>::iterator it = eventPtrs.begin();
276         it != eventPtrs.end(); ++it) {
277        (*it)->nameOut(os);
278        (*it)->serialize(os);
279    }
280}
281
282void
283EventQueue::unserialize(Checkpoint *cp, const std::string &section)
284{
285    int numEvents;
286    UNSERIALIZE_SCALAR(numEvents);
287
288    std::string eventName;
289    for (int i = 0; i < numEvents; i++) {
290        // get the pointer value associated with the event
291        paramIn(cp, section, csprintf("event%d", i), eventName);
292
293        // create the event based on its pointer value
294        Serializable::create(cp, eventName);
295    }
296}
297
298void
299EventQueue::dump() const
300{
301    cprintf("============================================================\n");
302    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
303    cprintf("------------------------------------------------------------\n");
304
305    if (empty())
306        cprintf("<No Events>\n");
307    else {
308        Event *nextBin = head;
309        while (nextBin) {
310            Event *nextInBin = nextBin;
311            while (nextInBin) {
312                nextInBin->dump();
313                nextInBin = nextInBin->nextInBin;
314            }
315
316            nextBin = nextBin->nextBin;
317        }
318    }
319
320    cprintf("============================================================\n");
321}
322
323bool
324EventQueue::debugVerify() const
325{
326    m5::hash_map<long, bool> map;
327
328    Tick time = 0;
329    short priority = 0;
330
331    Event *nextBin = head;
332    while (nextBin) {
333        Event *nextInBin = nextBin;
334        while (nextInBin) {
335            if (nextInBin->when() < time) {
336                cprintf("time goes backwards!");
337                nextInBin->dump();
338                return false;
339            } else if (nextInBin->when() == time &&
340                       nextInBin->priority() < priority) {
341                cprintf("priority inverted!");
342                nextInBin->dump();
343                return false;
344            }
345
346            if (map[reinterpret_cast<long>(nextInBin)]) {
347                cprintf("Node already seen");
348                nextInBin->dump();
349                return false;
350            }
351            map[reinterpret_cast<long>(nextInBin)] = true;
352
353            time = nextInBin->when();
354            priority = nextInBin->priority();
355
356            nextInBin = nextInBin->nextInBin;
357        }
358
359        nextBin = nextBin->nextBin;
360    }
361
362    return true;
363}
364
365void
366dumpMainQueue()
367{
368    mainEventQueue.dump();
369}
370
371
372const char *
373Event::description() const
374{
375    return "generic";
376}
377
378void
379Event::trace(const char *action)
380{
381    // This DPRINTF is unconditional because calls to this function
382    // are protected by an 'if (DTRACE(Event))' in the inlined Event
383    // methods.
384    //
385    // This is just a default implementation for derived classes where
386    // it's not worth doing anything special.  If you want to put a
387    // more informative message in the trace, override this method on
388    // the particular subclass where you have the information that
389    // needs to be printed.
390    DPRINTFN("%s event %s @ %d\n", description(), action, when());
391}
392
393void
394Event::dump() const
395{
396    cprintf("Event %s (%s)\n", name(), description());
397    cprintf("Flags: %#x\n", flags);
398#ifdef EVENTQ_DEBUG
399    cprintf("Created: %d\n", whenCreated);
400#endif
401    if (scheduled()) {
402#ifdef EVENTQ_DEBUG
403        cprintf("Scheduled at  %d\n", whenScheduled);
404#endif
405        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
406    } else {
407        cprintf("Not Scheduled\n");
408    }
409}
410