ruby: set: corrects csprintf() call introduced by 7d95b650c9b6
[gem5.git] / src / sim / eventq.cc
index 4f0b51e08739f86431ca65a372355d934d6d3ec4..d81937a864564e5d004a4a3ab16e30d541b2d18f 100644 (file)
@@ -40,8 +40,9 @@
 #include "base/misc.hh"
 #include "base/trace.hh"
 #include "cpu/smt.hh"
+#include "debug/Config.hh"
 #include "sim/core.hh"
-#include "sim/eventq.hh"
+#include "sim/eventq_impl.hh"
 
 using namespace std;
 
@@ -51,14 +52,31 @@ using namespace std;
 // 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 Event *
-insertBefore(Event *event, Event *curr)
+Event::~Event()
+{
+    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
@@ -83,7 +101,7 @@ EventQueue::insert(Event *event)
 {
     // Deal with the head case
     if (!head || *event <= *head) {
-        head = insertBefore(event, head);
+        head = Event::insertBefore(event, head);
         return;
     }
 
@@ -98,11 +116,11 @@ EventQueue::insert(Event *event)
 
     // Note: this operation may render all nextBin pointers on the
     // prev 'in bin' list stale (except for the top one)
-    prev->nextBin = insertBefore(event, curr);
+    prev->nextBin = Event::insertBefore(event, curr);
 }
 
-inline Event *
-removeItem(Event *event, Event *top)
+Event *
+Event::removeItem(Event *event, Event *top)
 {
     Event *curr = top;
     Event *next = top->nextInBin;
@@ -141,7 +159,7 @@ EventQueue::remove(Event *event)
     // 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);
+        head = Event::removeItem(event, head);
         return;
     }
 
@@ -159,7 +177,7 @@ EventQueue::remove(Event *event)
     // 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)
-    prev->nextBin = removeItem(event, curr);
+    prev->nextBin = Event::removeItem(event, curr);
 }
 
 Event *
@@ -167,7 +185,7 @@ EventQueue::serviceOne()
 {
     Event *event = head;
     Event *next = head->nextInBin;
-    event->clearFlags(Event::Scheduled);
+    event->flags.clear(Event::Scheduled);
 
     if (next) {
         // update the next bin pointer since it could be stale
@@ -183,16 +201,20 @@ EventQueue::serviceOne()
 
     // handle action
     if (!event->squashed()) {
+        // forward current cycle to the time when this event occurs.
+        setCurTick(event->when());
+
         event->process();
         if (event->isExitEvent()) {
-            assert(!event->getFlags(Event::AutoDelete)); // would be silly
+            assert(!event->flags.isSet(Event::AutoDelete) ||
+                   !event->flags.isSet(Event::IsMainQueue)); // 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;
@@ -203,28 +225,41 @@ Event::serialize(std::ostream &os)
 {
     SERIALIZE_SCALAR(_when);
     SERIALIZE_SCALAR(_priority);
-    SERIALIZE_ENUM(_flags);
+    short _flags = flags;
+    SERIALIZE_SCALAR(_flags);
 }
 
 void
 Event::unserialize(Checkpoint *cp, const string &section)
 {
     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);
     }
 }
 
@@ -239,7 +274,7 @@ EventQueue::serialize(ostream &os)
         Event *nextInBin = nextBin;
 
         while (nextInBin) {
-            if (nextInBin->getFlags(Event::AutoSerialize)) {
+            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
                 eventPtrs.push_back(nextInBin);
                 paramOut(os, csprintf("event%d", numEvents++),
                          nextInBin->name());
@@ -279,7 +314,7 @@ void
 EventQueue::dump() const
 {
     cprintf("============================================================\n");
-    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
+    cprintf("EventQueue Dump  (cycle %d)\n", curTick());
     cprintf("------------------------------------------------------------\n");
 
     if (empty())
@@ -342,6 +377,14 @@ EventQueue::debugVerify() const
     return true;
 }
 
+Event*
+EventQueue::replaceHead(Event* s)
+{
+    Event* t = head;
+    head = s;
+    return t;
+}
+
 void
 dumpMainQueue()
 {
@@ -374,7 +417,7 @@ void
 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
@@ -387,3 +430,7 @@ Event::dump() const
         cprintf("Not Scheduled\n");
     }
 }
+
+EventQueue::EventQueue(const string &n)
+    : objName(n), head(NULL), _curTick(0)
+{}