sim-se: Add default to SyscallDesc constructor
[gem5.git] / src / sim / eventq.cc
index 78d6a458f22a9d0c7ffcc0ae72564eb384b93228..3e27bcf1cc925750d7c52768a4991d341a86c41f 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 <cassert>
 #include <iostream>
 #include <string>
+#include <unordered_map>
 #include <vector>
 
-#include "base/hashmap.hh"
 #include "base/misc.hh"
 #include "base/trace.hh"
 #include "cpu/smt.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("MainEventQueue");
+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;
 #endif
 
+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)
 {
@@ -138,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) {
@@ -165,9 +203,10 @@ EventQueue::remove(Event *event)
 Event *
 EventQueue::serviceOne()
 {
+    std::lock_guard<EventQueue> lock(*this);
     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,103 +222,79 @@ 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::Managed) ||
+                   !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())
-        delete event;
+    event->release();
 
     return NULL;
 }
 
 void
-Event::serialize(std::ostream &os)
+Event::serialize(CheckpointOut &cp) const
 {
     SERIALIZE_SCALAR(_when);
     SERIALIZE_SCALAR(_priority);
-    SERIALIZE_ENUM(_flags);
+    short _flags = flags;
+    SERIALIZE_SCALAR(_flags);
 }
 
 void
-Event::unserialize(Checkpoint *cp, const string &section)
+Event::unserialize(CheckpointIn &cp)
 {
-    if (scheduled())
-        deschedule();
+    assert(!scheduled());
 
     UNSERIALIZE_SCALAR(_when);
     UNSERIALIZE_SCALAR(_priority);
 
+    FlagsType _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);
-
-    if (wasScheduled) {
-        DPRINTF(Config, "rescheduling at %d\n", _when);
-        schedule(_when);
-    }
-}
-
-void
-EventQueue::serialize(ostream &os)
-{
-    std::list<Event *> eventPtrs;
-
-    int numEvents = 0;
-    Event *nextBin = head;
-    while (nextBin) {
-        Event *nextInBin = nextBin;
-
-        while (nextInBin) {
-            if (nextInBin->getFlags(Event::AutoSerialize)) {
-                eventPtrs.push_back(nextInBin);
-                paramOut(os, csprintf("event%d", numEvents++),
-                         nextInBin->name());
-            }
-            nextInBin = nextInBin->nextInBin;
-        }
-
-        nextBin = nextBin->nextBin;
-    }
-
-    SERIALIZE_SCALAR(numEvents);
-
-    for (std::list<Event *>::iterator it = eventPtrs.begin();
-         it != eventPtrs.end(); ++it) {
-        (*it)->nameOut(os);
-        (*it)->serialize(os);
+    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::unserialize(Checkpoint *cp, const std::string &section)
+EventQueue::checkpointReschedule(Event *event)
 {
-    int numEvents;
-    UNSERIALIZE_SCALAR(numEvents);
-
-    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);
-
-        // create the event based on its pointer value
-        Serializable::create(cp, eventName);
-    }
+    // 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
 {
     cprintf("============================================================\n");
-    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
+    cprintf("EventQueue Dump  (cycle %d)\n", curTick());
     cprintf("------------------------------------------------------------\n");
 
     if (empty())
@@ -303,7 +318,7 @@ EventQueue::dump() const
 bool
 EventQueue::debugVerify() const
 {
-    m5::hash_map<long, bool> map;
+    std::unordered_map<long, bool> map;
 
     Tick time = 0;
     short priority = 0;
@@ -342,10 +357,20 @@ EventQueue::debugVerify() const
     return true;
 }
 
+Event*
+EventQueue::replaceHead(Event* s)
+{
+    Event* t = head;
+    head = s;
+    return t;
+}
+
 void
 dumpMainQueue()
 {
-    mainEventQueue.dump();
+    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
+        mainEventQueue[i]->dump();
+    }
 }
 
 
@@ -374,7 +399,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 +412,30 @@ Event::dump() const
         cprintf("Not Scheduled\n");
     }
 }
+
+EventQueue::EventQueue(const string &n)
+    : 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();
+}