# HG changeset patch
# User Nathan Binkert <[EMAIL PROTECTED]>
# Date 1213057781 25200
# Node ID ac6a6b3ef9362731553138e72974cd02f4c4169c
# Parent  ad5af63d60375b99e633ef71ef687bacab0c8a82
eventq: new eventq data structure.  The new data structure is singly
linked list sorted by time and priority.  For things of the same time
and priority, a second, circularly linked list maintains the data
structure.  Events of the same time and priority are now inserted in
FIFO order instead of LIFO order.  This dramatically improves the
performance of systems that schedule multiple events at the same time.

diff --git a/src/sim/eventq.cc b/src/sim/eventq.cc
--- a/src/sim/eventq.cc
+++ b/src/sim/eventq.cc
@@ -36,6 +36,7 @@
 #include <string>
 #include <vector>
 
+#include "base/hashmap.hh"
 #include "cpu/smt.hh"
 #include "base/misc.hh"
 
@@ -57,61 +58,137 @@ Counter Event::Debug::instanceCounter = 
 Counter Event::Debug::instanceCounter = 0;
 #endif
 
+inline void
+insert_before(Event *event, Event *curr)
+{
+    // Either way, event will be the last element in the two list
+    // which is the pointer we need in order to look into the list, so
+    // we need to insert that into the one list.
+    if (!curr || *event < *curr) {
+        // Insert the event before the current list since it is in the future.
+        event->one = curr;
+
+        // We need to create a new two list
+        event->two = event;
+    } else {
+        // Since we're on the correct list, we need to point to the next list
+        event->one = curr->one;     // curr->one can now become stale
+
+        // Insert event at the end of the 'two' curr is the last
+        // element on the two list and curr->two is the first
+        event->two = curr->two;     // event->two needs to point to the first
+        curr->two = event;          // curr->two is now second to last
+    }
+}
+
 void
 EventQueue::insert(Event *event)
 {
-    if (head == NULL || event->when() < head->when() ||
-        (event->when() == head->when() &&
-         event->priority() <= head->priority())) {
-        event->next = head;
+    // Deal with the head case
+    if (!head || *event <= *head) {
+        insert_before(event, head);
         head = event;
-    } else {
-        Event *prev = head;
-        Event *curr = head->next;
+        return;
+    }
 
-        while (curr) {
-            if (event->when() <= curr->when() &&
-                (event->when() < curr->when() ||
-                 event->priority() <= curr->priority()))
-                break;
+    // Figure out either which two list we are on, or where a new list
+    // needs to be inserted
+    Event *curr = head;
+    Event *next = head->one;
+    while (next && *next < *event) {
+        curr = next;
+        next = next->one;
+    }
 
-            prev = curr;
-            curr = curr->next;
-        }
+    insert_before(event, next);
+    curr->one = event;  // all one pointers on the curr two list are now stale
+}
 
-        event->next = curr;
-        prev->next = event;
+inline Event *
+remove_item(Event *event, Event *last)
+{
+    Event *prev = last;
+    Event *curr = last->two;
+
+    while (event != curr) {
+        if (curr == last)
+            panic("event not found!");
+
+        prev = curr;
+        curr = curr->two;
     }
+
+    // If this was the only item in this list, we're done.
+    if (prev == curr)
+        return NULL;
+
+    // remove curr from the two list since it's what we're looking for
+    prev->two = curr->two;
+
+    // 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->one = last->one;
+    return prev;
 }
 
 void
 EventQueue::remove(Event *event)
 {
     if (head == NULL)
-        return;
+        panic("event not found!");
 
-    if (head == event){
-        head = event->next;
+    // deal with an event on the head's two list (event has the same
+    // time as the head)
+    if (*head == *event) {
+        head = remove_item(event, head);
+        if (!head)
+            head = event->one;
         return;
     }
 
+    // Find the two list that this event belongs on
     Event *prev = head;
-    Event *curr = head->next;
-    while (curr && curr != event) {
+    Event *curr = head->one;
+    while (curr && *curr < *event) {
         prev = curr;
-        curr = curr->next;
+        curr = curr->one;
     }
 
-    if (curr == event)
-        prev->next = curr->next;
+    if (!curr || *curr != *event)
+        panic("event not found!");
+
+    // curr points to the last item of the the correct two list, when
+    // we remove an item, it returns the new last item (which may be
+    // unchanged)
+    Event *last = remove_item(event, curr);
+    if (!last) {
+        // The current item was removed, so we need to fix the one list
+        prev->one = curr->one;
+    } else if (last != curr) {
+        // We have a new last item, so we need to update the one list
+        prev->one = last;
+    }
 }
 
 Event *
 EventQueue::serviceOne()
 {
-    Event *event = head;
+    // grab the first element
+    Event *event = head->two;
     event->clearFlags(Event::Scheduled);
-    head = event->next;
+
+    if (head == event) {
+        // this was the only element on the two list, so get rid of
+        // the two list and point to the next one list
+        head = event->one;
+    } else {
+        // maintain head->two as the first element
+        head->two = event->two;
+    }
 
     // handle action
     if (!event->squashed()) {
@@ -168,18 +245,24 @@ EventQueue::serialize(ostream &os)
     std::list<Event *> eventPtrs;
 
     int numEvents = 0;
-    Event *event = head;
-    while (event) {
-        if (event->getFlags(Event::AutoSerialize)) {
-            eventPtrs.push_back(event);
-            paramOut(os, csprintf("event%d", numEvents++), event->name());
-        }
-        event = event->next;
+    Event *one = head;
+    while (one) {
+        Event *two = one->two;
+
+        do {
+            if (two->getFlags(Event::AutoSerialize)) {
+                eventPtrs.push_back(two);
+                paramOut(os, csprintf("event%d", numEvents++), two->name());
+            }
+            two = two->two;
+        } while (two != one);
+
+        one = one->one;
     }
 
     SERIALIZE_SCALAR(numEvents);
 
-    for (std::list<Event *>::iterator it=eventPtrs.begin();
+    for (std::list<Event *>::iterator it = eventPtrs.begin();
          it != eventPtrs.end(); ++it) {
         (*it)->nameOut(os);
         (*it)->serialize(os);
@@ -209,17 +292,68 @@ EventQueue::dump() const
     cprintf("EventQueue Dump  (cycle %d)\n", curTick);
     cprintf("------------------------------------------------------------\n");
 
+    m5::hash_map<long, bool> map;
+
     if (empty())
         cprintf("<No Events>\n");
     else {
-        Event *event = head;
-        while (event) {
-            event->dump();
-            event = event->next;
+        Event *one = head;
+        while (one) {
+            Event *two = one;
+            if (map[reinterpret_cast<long>(two)])
+                break;
+            map[reinterpret_cast<long>(two)] = true;
+            do {
+                two = two->two;
+                two->dump();
+            } while (two != one);
+
+            one = one->one;
         }
     }
 
     cprintf("============================================================\n");
+}
+
+bool
+EventQueue::debugVerify() const
+{
+    m5::hash_map<long, bool> map;
+
+    Tick time = 0;
+    short priority = 0;
+
+    Event *one = head;
+    while (one) {
+        Event *two = one->two;
+        do {
+            if (two->when() < time) {
+                cprintf("time goes backwards!");
+                two->dump();
+                return false;
+            } else if (two->when() == time && two->priority() < priority) {
+                cprintf("priority inverted!");
+                two->dump();
+                return false;
+            }
+
+            if (map[reinterpret_cast<long>(two)]) {
+                cprintf("Node already seen");
+                two->dump();
+                return false;
+            }
+            map[reinterpret_cast<long>(two)] = true;
+
+            time = two->when();
+            priority = two->priority();
+
+            two = two->two;
+        } while (two != one);
+
+        one = one->one;
+    }
+
+    return true;
 }
 
 void
diff --git a/src/sim/eventq.hh b/src/sim/eventq.hh
--- a/src/sim/eventq.hh
+++ b/src/sim/eventq.hh
@@ -91,7 +91,6 @@ class Event : public Serializable, publi
         Tick created;   //!< time created
         Tick scheduled; //!< time scheduled
 
-
         Debug()
             : instance(++instanceCounter), created(curTick), scheduled(0)
         {
@@ -100,7 +99,20 @@ class Event : public Serializable, publi
 #endif
 
   private:
-    Event *next;
+    // The event queue is now a linked list of linked lists.  The
+    // 'one' pointer is to find the bin, where a bin is defined as
+    // when+priority.  All events in the same bin will be stored in a
+    // second circularly linked list maintained by the 'two' pointer.
+    // The list will be accessed in FIFO order.  The end result is
+    // that the insert/removal in 'one' is linear/constant, and the
+    // lookup/removal in 'two' is constant/constant.  Hopefully this
+    // is a significant improvement over the current fully linear
+    // insertion.
+    Event *one;
+    Event *two;
+
+    friend void insert_before(Event *event, Event *curr);
+    friend Event *remove_item(Event *event, Event *last);
 
 #if TRACING_ON
     Debug *debug;
@@ -113,6 +125,15 @@ class Event : public Serializable, publi
     Tick _when;                //!< timestamp when event should be processed
     short _priority;   //!< event priority
     short _flags;
+
+    void
+    setWhen(Tick when)
+    {
+        _when = when;
+#if TRACING_ON
+        debug->scheduled = curTick;
+#endif
+    }
 
   protected:
     enum Flags {
@@ -196,7 +217,7 @@ class Event : public Serializable, publi
      * @param queue that the event gets scheduled on
      */
     Event(EventQueue *q, Priority p = Default_Pri)
-        : next(NULL), _queue(q), _priority(p), _flags(None)
+        : one(NULL), two(NULL), _queue(q), _priority(p), _flags(None)
     {
 #if TRACING_ON
         debug = new Debug;
@@ -356,9 +377,9 @@ class EventQueue : public Serializable
     virtual const std::string name() const { return objName; }
 
     // schedule the given event on this queue
-    void schedule(Event *ev);
+    void schedule(Event *ev, Tick when);
     void deschedule(Event *ev);
-    void reschedule(Event *ev);
+    void reschedule(Event *ev, Tick when);
 
     Tick nextTick() const { return head->when(); }
     Event *serviceOne();
@@ -392,6 +413,8 @@ class EventQueue : public Serializable
 
     Tick nextEventTime() { return empty() ? curTick : head->when(); }
 
+    bool debugVerify() const;
+
     virtual void serialize(std::ostream &os);
     virtual void unserialize(Checkpoint *cp, const std::string &section);
 };
@@ -409,53 +432,77 @@ class EventQueue : public Serializable
 // schedule at specified time (place on event queue specified via
 // constructor)
 inline void
-Event::schedule(Tick t)
+Event::schedule(Tick when)
 {
-    assert(!scheduled());
-    assert(t >= curTick);
-
-    setFlags(Scheduled);
-#if TRACING_ON
-    debug->scheduled = curTick;
-#endif
-    _when = t;
-    _queue->schedule(this);
+    _queue->schedule(this, when);
 }
 
 inline void
 Event::deschedule()
 {
-    assert(scheduled());
-
-    clearFlags(Squashed);
-    clearFlags(Scheduled);
     _queue->deschedule(this);
 }
 
 inline void
-Event::reschedule(Tick t, bool always)
+Event::reschedule(Tick when, bool always)
 {
-    assert(scheduled() || always);
-    assert(t >= curTick);
-
-#if TRACING_ON
-    debug->scheduled = curTick;
-#endif
-    _when = t;
-
     if (scheduled()) {
-        clearFlags(Squashed);
-        _queue->reschedule(this);
+        _queue->reschedule(this, when);
     } else {
-        setFlags(Scheduled);
-        _queue->schedule(this);
+        assert(always);
+        _queue->schedule(this, when);
     }
 }
 
+inline bool
+operator<(const Event &l, const Event &r)
+{
+    return l.when() < r.when() ||
+        (l.when() == r.when() && l.priority() < r.priority());
+}
+
+inline bool
+operator>(const Event &l, const Event &r)
+{
+    return l.when() > r.when() ||
+        (l.when() == r.when() && l.priority() > r.priority());
+}
+
+inline bool
+operator<=(const Event &l, const Event &r)
+{
+    return l.when() < r.when() ||
+        (l.when() == r.when() && l.priority() <= r.priority());
+}
+inline bool
+operator>=(const Event &l, const Event &r)
+{
+    return l.when() > r.when() ||
+        (l.when() == r.when() && l.priority() >= r.priority());
+}
+
+inline bool
+operator==(const Event &l, const Event &r)
+{
+    return l.when() == r.when() && l.priority() == r.priority();
+}
+
+inline bool
+operator!=(const Event &l, const Event &r)
+{
+    return l.when() != r.when() || l.priority() != r.priority();
+}
+
 inline void
-EventQueue::schedule(Event *event)
+EventQueue::schedule(Event *event, Tick when)
 {
+    assert(when >= curTick);
+    assert(!event->scheduled());
+
+    event->setWhen(when);
     insert(event);
+    event->setFlags(Event::Scheduled);
+
     if (DTRACE(Event))
         event->trace("scheduled");
 }
@@ -463,19 +510,31 @@ inline void
 inline void
 EventQueue::deschedule(Event *event)
 {
+    assert(event->scheduled());
+
     remove(event);
-    if (DTRACE(Event))
-        event->trace("descheduled");
+
+    event->clearFlags(Event::Squashed);
+    event->clearFlags(Event::Scheduled);
 
     if (event->getFlags(Event::AutoDelete))
         delete event;
+
+    if (DTRACE(Event))
+        event->trace("descheduled");
 }
 
 inline void
-EventQueue::reschedule(Event *event)
+EventQueue::reschedule(Event *event, Tick when)
 {
+    assert(when >= curTick);
+    assert(event->scheduled());
+
     remove(event);
+    event->setWhen(when);
     insert(event);
+    event->clearFlags(Event::Squashed);
+
     if (DTRACE(Event))
         event->trace("rescheduled");
 }
_______________________________________________
m5-dev mailing list
m5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/m5-dev

Reply via email to