sim: Make the drain state a global typed enum
[gem5.git] / src / sim / eventq.cc
index b32b330e71995ba44f32097c34be5e9208108b1f..48664f8e8f06bf4bebbfdf0f766402c2743fb028 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2000-2005 The Regents of The University of Michigan
  * Copyright (c) 2008 The Hewlett-Packard Development Company
+ * Copyright (c) 2013 Advanced Micro Devices, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "base/misc.hh"
 #include "base/trace.hh"
 #include "cpu/smt.hh"
-#include "debug/Config.hh"
+#include "debug/Checkpoint.hh"
 #include "sim/core.hh"
-#include "sim/eventq.hh"
+#include "sim/eventq_impl.hh"
 
 using namespace std;
 
+Tick simQuantum = 0;
+
 //
-// Main Event Queue
+// Main Event Queues
 //
-// Events on this queue are processed at the *beginning* of each
+// Events on these queues are processed at the *beginning* of each
 // cycle, before the pipeline simulation is performed.
 //
-EventQueue mainEventQueue("Main Event Queue");
+uint32_t numMainEventQueues = 0;
+vector<EventQueue *> mainEventQueue;
+__thread EventQueue *_curEventQueue = NULL;
+bool inParallelMode = false;
+
+EventQueue *
+getEventQueue(uint32_t index)
+{
+    while (numMainEventQueues <= index) {
+        numMainEventQueues++;
+        mainEventQueue.push_back(
+            new EventQueue(csprintf("MainEventQueue-%d", index)));
+    }
+
+    return mainEventQueue[index];
+}
 
 #ifndef NDEBUG
 Counter Event::instanceCounter = 0;
@@ -156,6 +174,8 @@ EventQueue::remove(Event *event)
     if (head == NULL)
         panic("event not found!");
 
+    assert(event->queue == this);
+
     // deal with an event on the head's 'in bin' list (event has the same
     // time as the head)
     if (*head == *event) {
@@ -183,6 +203,7 @@ EventQueue::remove(Event *event)
 Event *
 EventQueue::serviceOne()
 {
+    std::lock_guard<EventQueue> lock(*this);
     Event *event = head;
     Event *next = head->nextInBin;
     event->flags.clear(Event::Scheduled);
@@ -201,6 +222,9 @@ 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->flags.isSet(Event::AutoDelete) ||
@@ -218,7 +242,7 @@ EventQueue::serviceOne()
 }
 
 void
-Event::serialize(std::ostream &os)
+Event::serialize(CheckpointOut &cp) const
 {
     SERIALIZE_SCALAR(_when);
     SERIALIZE_SCALAR(_priority);
@@ -227,15 +251,14 @@ Event::serialize(std::ostream &os)
 }
 
 void
-Event::unserialize(Checkpoint *cp, const string &section)
+Event::unserialize(CheckpointIn &cp)
 {
-    if (scheduled())
-        mainEventQueue.deschedule(this);
+    assert(!scheduled());
 
     UNSERIALIZE_SCALAR(_when);
     UNSERIALIZE_SCALAR(_priority);
 
-    short _flags;
+    FlagsType _flags;
     UNSERIALIZE_SCALAR(_flags);
 
     // Old checkpoints had no concept of the Initialized flag
@@ -251,17 +274,16 @@ Event::unserialize(Checkpoint *cp, const string &section)
     // 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)
-    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
-    flags.clear(Squashed | Scheduled);
-
-    if (wasScheduled) {
-        DPRINTF(Config, "rescheduling at %d\n", _when);
-        mainEventQueue.schedule(this, _when);
+    if (flags.isSet(Scheduled) && !flags.isSet(Squashed)) {
+        flags.clear(Squashed | Scheduled);
+    } else {
+        DPRINTF(Checkpoint, "Event '%s' need to be scheduled @%d\n",
+                name(), _when);
     }
 }
 
 void
-EventQueue::serialize(ostream &os)
+EventQueue::serialize(CheckpointOut &cp) const
 {
     std::list<Event *> eventPtrs;
 
@@ -273,7 +295,7 @@ EventQueue::serialize(ostream &os)
         while (nextInBin) {
             if (nextInBin->flags.isSet(Event::AutoSerialize)) {
                 eventPtrs.push_back(nextInBin);
-                paramOut(os, csprintf("event%d", numEvents++),
+                paramOut(cp, csprintf("event%d", numEvents++),
                          nextInBin->name());
             }
             nextInBin = nextInBin->nextInBin;
@@ -284,15 +306,12 @@ EventQueue::serialize(ostream &os)
 
     SERIALIZE_SCALAR(numEvents);
 
-    for (std::list<Event *>::iterator it = eventPtrs.begin();
-         it != eventPtrs.end(); ++it) {
-        (*it)->nameOut(os);
-        (*it)->serialize(os);
-    }
+    for (Event *ev : eventPtrs)
+        ev->serializeSection(cp, ev->name());
 }
 
 void
-EventQueue::unserialize(Checkpoint *cp, const std::string &section)
+EventQueue::unserialize(CheckpointIn &cp)
 {
     int numEvents;
     UNSERIALIZE_SCALAR(numEvents);
@@ -300,13 +319,27 @@ EventQueue::unserialize(Checkpoint *cp, const std::string &section)
     std::string eventName;
     for (int i = 0; i < numEvents; i++) {
         // get the pointer value associated with the event
-        paramIn(cp, section, csprintf("event%d", i), eventName);
+        paramIn(cp, csprintf("event%d", i), eventName);
 
         // create the event based on its pointer value
-        Serializable::create(cp, eventName);
+        Serializable *obj(Serializable::create(cp, eventName));
+        Event *event(dynamic_cast<Event *>(obj));
+        fatal_if(!event,
+                 "Event queue unserialized something that wasn't an event.\n");
+
+        checkpointReschedule(event);
     }
 }
 
+void
+EventQueue::checkpointReschedule(Event *event)
+{
+    // It's safe to call insert() directly here since this method
+    // should only be called when restoring from a checkpoint (which
+    // happens before thread creation).
+    if (event->flags.isSet(Event::Scheduled))
+        insert(event);
+}
 void
 EventQueue::dump() const
 {
@@ -385,7 +418,9 @@ EventQueue::replaceHead(Event* s)
 void
 dumpMainQueue()
 {
-    mainEventQueue.dump();
+    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
+        mainEventQueue[i]->dump();
+    }
 }
 
 
@@ -429,5 +464,28 @@ Event::dump() const
 }
 
 EventQueue::EventQueue(const string &n)
-    : objName(n), head(NULL)
-{}
+    : objName(n), head(NULL), _curTick(0)
+{
+}
+
+void
+EventQueue::asyncInsert(Event *event)
+{
+    async_queue_mutex.lock();
+    async_queue.push_back(event);
+    async_queue_mutex.unlock();
+}
+
+void
+EventQueue::handleAsyncInsertions()
+{
+    assert(this == curEventQueue());
+    async_queue_mutex.lock();
+
+    while (!async_queue.empty()) {
+        insert(async_queue.front());
+        async_queue.pop_front();
+    }
+
+    async_queue_mutex.unlock();
+}