#include "base/misc.hh"
#include "base/trace.hh"
#include "cpu/smt.hh"
+#include "debug/Config.hh"
#include "sim/core.hh"
#include "sim/eventq.hh"
// Events on this queue are processed at the *beginning* of each
// cycle, before the pipeline simulation is performed.
//
-EventQueue mainEventQueue("MainEventQueue");
+EventQueue mainEventQueue("Main Event Queue");
#ifndef NDEBUG
Counter Event::instanceCounter = 0;
#endif
-inline void
-insertBefore(Event *event, Event *curr)
+Event::~Event()
{
- // Either way, event will be the last element in the 'in bin' list
+ assert(!scheduled());
+ flags = 0;
+}
+
+const std::string
+Event::name() const
+{
+#ifndef NDEBUG
+ return csprintf("Event_%d", instance);
+#else
+ return csprintf("Event_%x", (uintptr_t)this);
+#endif
+}
+
+
+Event *
+Event::insertBefore(Event *event, Event *curr)
+{
+ // Either way, event will be the top element in the 'in bin' list
// which is the pointer we need in order to look into the list, so
// we need to insert that into the bin list.
if (!curr || *event < *curr) {
// Insert the event before the current list since it is in the future.
event->nextBin = curr;
-
- // We need to create a new 'in bin' list
- event->nextInBin = event;
+ event->nextInBin = NULL;
} else {
// Since we're on the correct list, we need to point to the next list
event->nextBin = curr->nextBin; // curr->nextBin can now become stale
- // Insert event at the end of the 'nextInBin' curr is the last
- // element on the 'in bin' list and curr->nextInBin is the first
-
- event->nextInBin = curr->nextInBin; // event->nextInBin needs to
- // point to the first
- curr->nextInBin = event; // curr->nextInBin is now second to last
+ // Insert event at the top of the stack
+ event->nextInBin = curr;
}
+
+ return event;
}
void
{
// Deal with the head case
if (!head || *event <= *head) {
- insertBefore(event, head);
- head = event;
+ head = Event::insertBefore(event, head);
return;
}
// Figure out either which 'in bin' list we are on, or where a new list
// needs to be inserted
- Event *curr = head;
- Event *next = head->nextBin;
- while (next && *next < *event) {
- curr = next;
- next = next->nextBin;
+ Event *prev = head;
+ Event *curr = head->nextBin;
+ while (curr && *curr < *event) {
+ prev = curr;
+ curr = curr->nextBin;
}
- insertBefore(event, next);
- curr->nextBin = event; // all nextBin pointers on the curr
- // 'in bin' list are now stale
+ // Note: this operation may render all nextBin pointers on the
+ // prev 'in bin' list stale (except for the top one)
+ prev->nextBin = Event::insertBefore(event, curr);
}
-inline Event *
-removeItem(Event *event, Event *last)
+Event *
+Event::removeItem(Event *event, Event *top)
{
- Event *prev = last;
- Event *curr = last->nextInBin;
+ Event *curr = top;
+ Event *next = top->nextInBin;
+
+ // if we removed the top item, we need to handle things specially
+ // and just remove the top item, fixing up the next bin pointer of
+ // the new top item
+ if (event == top) {
+ if (!next)
+ return top->nextBin;
+ next->nextBin = top->nextBin;
+ return next;
+ }
- while (event != curr) {
- if (curr == last)
+ // Since we already checked the current element, we're going to
+ // keep checking event against the next element.
+ while (event != next) {
+ if (!next)
panic("event not found!");
- prev = curr;
- curr = curr->nextInBin;
+ curr = next;
+ next = next->nextInBin;
}
- // If this was the only item in this list, we're done.
- if (prev == curr)
- return NULL;
-
- // remove curr from the 'in bin' list since it's what we're looking for
- prev->nextInBin = curr->nextInBin;
-
- // If we didn't remove the last item, we're done
- if (curr != last)
- return last;
-
- // if we removed the last item, the new last item is prev
- // fix it up since it might be stale and return it
- prev->nextBin = last->nextBin;
- return prev;
+ // remove next from the 'in bin' list since it's what we're looking for
+ curr->nextInBin = next->nextInBin;
+ return top;
}
void
// deal with an event on the head's 'in bin' list (event has the same
// time as the head)
if (*head == *event) {
- head = removeItem(event, head);
- if (!head)
- head = event->nextBin;
+ head = Event::removeItem(event, head);
return;
}
if (!curr || *curr != *event)
panic("event not found!");
- // curr points to the last item of the the correct 'in bin' list, when
- // we remove an item, it returns the new last item (which may be
+ // curr points to the top item of the the correct 'in bin' list, when
+ // we remove an item, it returns the new top item (which may be
// unchanged)
- Event *last = removeItem(event, curr);
- if (!last) {
- // The current item was removed, so we need to fix the bin list
- prev->nextBin = curr->nextBin;
- } else if (last != curr) {
- // We have a new last item, so we need to update the bin list
- prev->nextBin = last;
- }
+ prev->nextBin = Event::removeItem(event, curr);
}
Event *
EventQueue::serviceOne()
{
- // grab the first element
- Event *event = head->nextInBin;
- event->clearFlags(Event::Scheduled);
+ Event *event = head;
+ Event *next = head->nextInBin;
+ event->flags.clear(Event::Scheduled);
+
+ if (next) {
+ // update the next bin pointer since it could be stale
+ next->nextBin = head->nextBin;
- if (head == event) {
+ // pop the stack
+ head = next;
+ } else {
// this was the only element on the 'in bin' list, so get rid of
// the 'in bin' list and point to the next bin list
- head = event->nextBin;
- } else {
- // maintain head->nextInBin as the first element
- head->nextInBin = event->nextInBin;
+ head = head->nextBin;
}
// handle action
if (!event->squashed()) {
event->process();
if (event->isExitEvent()) {
- assert(!event->getFlags(Event::AutoDelete)); // would be silly
+ assert(!event->flags.isSet(Event::AutoDelete)); // would be silly
return event;
}
} else {
- event->clearFlags(Event::Squashed);
+ event->flags.clear(Event::Squashed);
}
- if (event->getFlags(Event::AutoDelete) && !event->scheduled())
+ if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
delete event;
return NULL;
{
SERIALIZE_SCALAR(_when);
SERIALIZE_SCALAR(_priority);
- SERIALIZE_ENUM(_flags);
+ short _flags = flags;
+ SERIALIZE_SCALAR(_flags);
}
void
Event::unserialize(Checkpoint *cp, const string §ion)
{
if (scheduled())
- deschedule();
+ mainEventQueue.deschedule(this);
UNSERIALIZE_SCALAR(_when);
UNSERIALIZE_SCALAR(_priority);
+ short _flags;
+ UNSERIALIZE_SCALAR(_flags);
+
+ // Old checkpoints had no concept of the Initialized flag
+ // so restoring from old checkpoints always fail.
+ // Events are initialized on construction but original code
+ // "flags = _flags" would just overwrite the initialization.
+ // So, read in the checkpoint flags, but then set the Initialized
+ // flag on top of it in order to avoid failures.
+ assert(initialized());
+ flags = _flags;
+ flags.set(Initialized);
+
// need to see if original event was in a scheduled, unsquashed
// state, but don't want to restore those flags in the current
// object itself (since they aren't immediately true)
- UNSERIALIZE_ENUM(_flags);
- bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
- _flags &= ~(Squashed | Scheduled);
+ bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
+ flags.clear(Squashed | Scheduled);
if (wasScheduled) {
DPRINTF(Config, "rescheduling at %d\n", _when);
- schedule(_when);
+ mainEventQueue.schedule(this, _when);
}
}
int numEvents = 0;
Event *nextBin = head;
while (nextBin) {
- Event *nextInBin = nextBin->nextInBin;
+ Event *nextInBin = nextBin;
- do {
- if (nextInBin->getFlags(Event::AutoSerialize)) {
+ while (nextInBin) {
+ if (nextInBin->flags.isSet(Event::AutoSerialize)) {
eventPtrs.push_back(nextInBin);
paramOut(os, csprintf("event%d", numEvents++),
nextInBin->name());
}
nextInBin = nextInBin->nextInBin;
- } while (nextInBin != nextBin);
+ }
nextBin = nextBin->nextBin;
}
EventQueue::dump() const
{
cprintf("============================================================\n");
- cprintf("EventQueue Dump (cycle %d)\n", curTick);
+ cprintf("EventQueue Dump (cycle %d)\n", curTick());
cprintf("------------------------------------------------------------\n");
- m5::hash_map<long, bool> map;
-
if (empty())
cprintf("<No Events>\n");
else {
Event *nextBin = head;
while (nextBin) {
Event *nextInBin = nextBin;
- if (map[reinterpret_cast<long>(nextInBin)])
- break;
- map[reinterpret_cast<long>(nextInBin)] = true;
- do {
- nextInBin = nextInBin->nextInBin;
+ while (nextInBin) {
nextInBin->dump();
- } while (nextInBin != nextBin);
+ nextInBin = nextInBin->nextInBin;
+ }
nextBin = nextBin->nextBin;
}
Event *nextBin = head;
while (nextBin) {
- Event *nextInBin = nextBin->nextInBin;
- do {
+ Event *nextInBin = nextBin;
+ while (nextInBin) {
if (nextInBin->when() < time) {
cprintf("time goes backwards!");
nextInBin->dump();
priority = nextInBin->priority();
nextInBin = nextInBin->nextInBin;
- } while (nextInBin != nextBin);
+ }
nextBin = nextBin->nextBin;
}
Event::dump() const
{
cprintf("Event %s (%s)\n", name(), description());
- cprintf("Flags: %#x\n", _flags);
+ cprintf("Flags: %#x\n", flags);
#ifdef EVENTQ_DEBUG
cprintf("Created: %d\n", whenCreated);
#endif
cprintf("Not Scheduled\n");
}
}
+
+EventQueue::EventQueue(const string &n)
+ : objName(n), head(NULL)
+{}