ruby: message buffers: significant changes
[gem5.git] / src / mem / ruby / network / simple / Throttle.cc
index 1d9ec0dc6279b3e2e6f2e7fc8644b7963aef54ed..91bad217bbfb32662f8bd70e5330bfe8a987145d 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
  * All rights reserved.
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * $Id$
- *
- * Description: see Throttle.h
- *
- */
+#include <cassert>
 
+#include "base/cast.hh"
+#include "base/cprintf.hh"
+#include "debug/RubyNetwork.hh"
 #include "mem/ruby/network/simple/Throttle.hh"
-#include "mem/ruby/buffers/MessageBuffer.hh"
+#include "mem/ruby/network/MessageBuffer.hh"
 #include "mem/ruby/network/Network.hh"
-#include "mem/ruby/system/System.hh"
 #include "mem/ruby/slicc_interface/NetworkMessage.hh"
-#include "mem/protocol/Protocol.hh"
+#include "mem/ruby/system/System.hh"
+
+using namespace std;
 
-const int HIGH_RANGE = 256;
-const int ADJUST_INTERVAL = 50000;
 const int MESSAGE_SIZE_MULTIPLIER = 1000;
 //const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
 const int BROADCAST_SCALING = 1;
@@ -50,242 +46,230 @@ const int PRIORITY_SWITCH_LIMIT = 128;
 
 static int network_message_to_size(NetworkMessage* net_msg_ptr);
 
-extern std::ostream * debug_cout_ptr;
-
-Throttle::Throttle(int sID, NodeID node, int link_latency, int link_bandwidth_multiplier)
+Throttle::Throttle(int sID, NodeID node, Cycles link_latency,
+                   int link_bandwidth_multiplier, int endpoint_bandwidth,
+                   ClockedObject *em)
+    : Consumer(em)
 {
-  init(node, link_latency, link_bandwidth_multiplier);
-  m_sID = sID;
+    init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
+    m_sID = sID;
 }
 
-Throttle::Throttle(NodeID node, int link_latency, int link_bandwidth_multiplier)
+Throttle::Throttle(NodeID node, Cycles link_latency,
+                   int link_bandwidth_multiplier, int endpoint_bandwidth,
+                   ClockedObject *em)
+    : Consumer(em)
 {
-  init(node, link_latency, link_bandwidth_multiplier);
-  m_sID = 0;
+    init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
+    m_sID = 0;
 }
 
-void Throttle::init(NodeID node, int link_latency, int link_bandwidth_multiplier)
+void
+Throttle::init(NodeID node, Cycles link_latency,
+               int link_bandwidth_multiplier, int endpoint_bandwidth)
 {
-  m_node = node;
-  m_vnets = 0;
-
-  ASSERT(link_bandwidth_multiplier > 0);
-  m_link_bandwidth_multiplier = link_bandwidth_multiplier;
-  m_link_latency = link_latency;
-
-  m_bash_counter = HIGH_RANGE;
-  m_bandwidth_since_sample = 0;
-  m_last_bandwidth_sample = 0;
-  m_wakeups_wo_switch = 0;
-  clearStats();
-}
+    m_node = node;
+    assert(link_bandwidth_multiplier > 0);
+    m_link_bandwidth_multiplier = link_bandwidth_multiplier;
 
-void Throttle::clear()
-{
-  for (int counter = 0; counter < m_vnets; counter++) {
-    m_in[counter]->clear();
-    m_out[counter]->clear();
-  }
+    m_link_latency = link_latency;
+    m_endpoint_bandwidth = endpoint_bandwidth;
+
+    m_wakeups_wo_switch = 0;
+    m_link_utilization_proxy = 0;
 }
 
-void Throttle::addLinks(const Vector<MessageBuffer*>& in_vec, const Vector<MessageBuffer*>& out_vec)
+void
+Throttle::addLinks(const map<int, MessageBuffer*>& in_vec,
+                   const map<int, MessageBuffer*>& out_vec)
 {
-  assert(in_vec.size() == out_vec.size());
-  for (int i=0; i<in_vec.size(); i++) {
-    addVirtualNetwork(in_vec[i], out_vec[i]);
-  }
-
-  m_message_counters.setSize(MessageSizeType_NUM);
-  for (int i=0; i<MessageSizeType_NUM; i++) {
-    m_message_counters[i].setSize(in_vec.size());
-    for (int j=0; j<m_message_counters[i].size(); j++) {
-      m_message_counters[i][j] = 0;
-    }
-  }
+    assert(in_vec.size() == out_vec.size());
 
-  if (g_PRINT_TOPOLOGY) {
-    m_out_link_vec.insertAtBottom(out_vec);
-  }
-}
+    for (auto& it : in_vec) {
+        int vnet = it.first;
 
-void Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr)
-{
-  m_units_remaining.insertAtBottom(0);
-  m_in.insertAtBottom(in_ptr);
-  m_out.insertAtBottom(out_ptr);
-
-  // Set consumer and description
-  m_in[m_vnets]->setConsumer(this);
-  string desc = "[Queue to Throttle " + NodeIDToString(m_sID) + " " + NodeIDToString(m_node) + "]";
-  m_in[m_vnets]->setDescription(desc);
-  m_vnets++;
-}
+        auto jt = out_vec.find(vnet);
+        assert(jt != out_vec.end());
 
-void Throttle::wakeup()
-{
-  // Limits the number of message sent to a limited number of bytes/cycle.
-  assert(getLinkBandwidth() > 0);
-  int bw_remaining = getLinkBandwidth();
-
-  // Give the highest numbered link priority most of the time
-  m_wakeups_wo_switch++;
-  int highest_prio_vnet = m_vnets-1;
-  int lowest_prio_vnet = 0;
-  int counter = 1;
-  bool schedule_wakeup = false;
-
-  // invert priorities to avoid starvation seen in the component network
-  if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
-    m_wakeups_wo_switch = 0;
-    highest_prio_vnet = 0;
-    lowest_prio_vnet = m_vnets-1;
-    counter = -1;
-  }
+        MessageBuffer *in_ptr = it.second;
+        MessageBuffer *out_ptr = (*jt).second;
 
-  for (int vnet = highest_prio_vnet; (vnet*counter) >= (counter*lowest_prio_vnet); vnet -= counter) {
+        m_in[vnet] = in_ptr;
+        m_out[vnet] = out_ptr;
+        m_units_remaining[vnet] = 0;
+
+        // Set consumer and description
+        in_ptr->setConsumer(this);
+        string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
+            to_string(m_node) + "]";
+        in_ptr->setDescription(desc);
+    }
+}
 
-    assert(m_out[vnet] != NULL);
-    assert(m_in[vnet] != NULL);
+void
+Throttle::operateVnet(int vnet, int &bw_remaining, bool &schedule_wakeup,
+                      MessageBuffer *in, MessageBuffer *out)
+{
+    assert(out != NULL);
+    assert(in != NULL);
     assert(m_units_remaining[vnet] >= 0);
 
-    while ((bw_remaining > 0) && ((m_in[vnet]->isReady()) || (m_units_remaining[vnet] > 0)) && m_out[vnet]->areNSlotsAvailable(1)) {
+    while (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
+                                out->areNSlotsAvailable(1)) {
+
+        // See if we are done transferring the previous message on
+        // this virtual network
+        if (m_units_remaining[vnet] == 0 && in->isReady()) {
+            // Find the size of the message we are moving
+            MsgPtr msg_ptr = in->peekMsgPtr();
+            NetworkMessage* net_msg_ptr =
+                safe_cast<NetworkMessage*>(msg_ptr.get());
+            m_units_remaining[vnet] +=
+                network_message_to_size(net_msg_ptr);
+
+            DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
+                    "enqueueing net msg %d time: %lld.\n",
+                    m_node, getLinkBandwidth(), m_units_remaining[vnet],
+                    g_system_ptr->curCycle());
+
+            // Move the message
+            in->dequeue();
+            out->enqueue(msg_ptr, m_link_latency);
+
+            // Count the message
+            m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++;
+            DPRINTF(RubyNetwork, "%s\n", *out);
+        }
+
+        // Calculate the amount of bandwidth we spent on this message
+        int diff = m_units_remaining[vnet] - bw_remaining;
+        m_units_remaining[vnet] = max(0, diff);
+        bw_remaining = max(0, -diff);
+    }
 
-      // See if we are done transferring the previous message on this virtual network
-      if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
+    if (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
+                             !out->areNSlotsAvailable(1)) {
+        DPRINTF(RubyNetwork, "vnet: %d", vnet);
 
-        // Find the size of the message we are moving
-        MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
-        NetworkMessage* net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref());
-        m_units_remaining[vnet] += network_message_to_size(net_msg_ptr);
+        // schedule me to wakeup again because I'm waiting for my
+        // output queue to become available
+        schedule_wakeup = true;
+    }
+}
 
-        DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
-        DEBUG_MSG(NETWORK_COMP,HighPrio,"throttle: " + int_to_string(m_node)
-                  + " my bw " + int_to_string(getLinkBandwidth())
-                  + " bw spent enqueueing net msg " + int_to_string(m_units_remaining[vnet])
-                  + " time: " + int_to_string(g_eventQueue_ptr->getTime()) + ".");
+void
+Throttle::wakeup()
+{
+    // Limits the number of message sent to a limited number of bytes/cycle.
+    assert(getLinkBandwidth() > 0);
+    int bw_remaining = getLinkBandwidth();
 
-        // Move the message
-        m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
-        m_in[vnet]->pop();
+    m_wakeups_wo_switch++;
+    bool schedule_wakeup = false;
 
-        // Count the message
-        m_message_counters[net_msg_ptr->getMessageSize()][vnet]++;
+    // variable for deciding the direction in which to iterate
+    bool iteration_direction = false;
 
-        DEBUG_MSG(NETWORK_COMP,LowPrio,*m_out[vnet]);
-        DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
-      }
 
-      // Calculate the amount of bandwidth we spent on this message
-      int diff = m_units_remaining[vnet] - bw_remaining;
-      m_units_remaining[vnet] = max(0, diff);
-      bw_remaining = max(0, -diff);
+    // invert priorities to avoid starvation seen in the component network
+    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
+        m_wakeups_wo_switch = 0;
+        iteration_direction = true;
     }
 
-    if ((bw_remaining > 0) && ((m_in[vnet]->isReady()) || (m_units_remaining[vnet] > 0)) && !m_out[vnet]->areNSlotsAvailable(1)) {
-      DEBUG_MSG(NETWORK_COMP,LowPrio,vnet);
-      schedule_wakeup = true; // schedule me to wakeup again because I'm waiting for my output queue to become available
-    }
-  }
-
-  // We should only wake up when we use the bandwidth
-  //  assert(bw_remaining != getLinkBandwidth());  // This is only mostly true
-
-  // Record that we used some or all of the link bandwidth this cycle
-  double ratio = 1.0-(double(bw_remaining)/double(getLinkBandwidth()));
-  // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
-  linkUtilized(ratio);
-
-  // Sample the link bandwidth utilization over a number of cycles
-  int bw_used = getLinkBandwidth()-bw_remaining;
-  m_bandwidth_since_sample += bw_used;
-
-  // FIXME - comment out the bash specific code for faster performance
-  // Start Bash code
-  // Update the predictor
-  Time current_time = g_eventQueue_ptr->getTime();
-  while ((current_time - m_last_bandwidth_sample) > ADJUST_INTERVAL) {
-    double utilization = m_bandwidth_since_sample/double(ADJUST_INTERVAL * getLinkBandwidth());
-
-    if (utilization > g_bash_bandwidth_adaptive_threshold) {
-      // Used more bandwidth
-      m_bash_counter++;
+    if (iteration_direction) {
+        for (auto& it : m_in) {
+            int vnet = it.first;
+            operateVnet(vnet, bw_remaining, schedule_wakeup,
+                        it.second, m_out[vnet]);
+        }
     } else {
-      // Used less bandwidth
-      m_bash_counter--;
+        for (auto it = m_in.rbegin(); it != m_in.rend(); ++it) {
+            int vnet = (*it).first;
+            operateVnet(vnet, bw_remaining, schedule_wakeup,
+                        (*it).second, m_out[vnet]);
+        }
     }
 
-    // Make sure we don't overflow
-    m_bash_counter = min(HIGH_RANGE, m_bash_counter);
-    m_bash_counter = max(0, m_bash_counter);
-
-    // Reset samples
-    m_last_bandwidth_sample += ADJUST_INTERVAL;
-    m_bandwidth_since_sample = 0;
-  }
-  // End Bash code
-
-  if ((bw_remaining > 0) && !schedule_wakeup) {
-    // We have extra bandwidth and our output buffer was available, so we must not have anything else to do until another message arrives.
-    DEBUG_MSG(NETWORK_COMP,LowPrio,*this);
-    DEBUG_MSG(NETWORK_COMP,LowPrio,"not scheduled again");
-  } else {
-    DEBUG_MSG(NETWORK_COMP,LowPrio,*this);
-    DEBUG_MSG(NETWORK_COMP,LowPrio,"scheduled again");
-    // We are out of bandwidth for this cycle, so wakeup next cycle and continue
-    g_eventQueue_ptr->scheduleEvent(this, 1);
-  }
-}
+    // We should only wake up when we use the bandwidth
+    // This is only mostly true
+    // assert(bw_remaining != getLinkBandwidth());
 
-bool Throttle::broadcastBandwidthAvailable(int rand) const
-{
-  bool result =  !(m_bash_counter > ((HIGH_RANGE/4) + (rand % (HIGH_RANGE/2))));
-  return result;
-}
+    // Record that we used some or all of the link bandwidth this cycle
+    double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
 
-void Throttle::printStats(ostream& out) const
-{
-  out << "utilized_percent: " << getUtilization() << endl;
-}
+    // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
+    m_link_utilization_proxy += ratio;
 
-void Throttle::clearStats()
-{
-  m_ruby_start = g_eventQueue_ptr->getTime();
-  m_links_utilized = 0.0;
+    if (bw_remaining > 0 && !schedule_wakeup) {
+        // We have extra bandwidth and our output buffer was
+        // available, so we must not have anything else to do until
+        // another message arrives.
+        DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
+    } else {
+        DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
 
-  for (int i=0; i<m_message_counters.size(); i++) {
-    for (int j=0; j<m_message_counters[i].size(); j++) {
-      m_message_counters[i][j] = 0;
+        // We are out of bandwidth for this cycle, so wakeup next
+        // cycle and continue
+        scheduleEvent(Cycles(1));
     }
-  }
 }
 
-void Throttle::printConfig(ostream& out) const
+void
+Throttle::regStats(string parent)
 {
-
+    m_link_utilization
+        .name(parent + csprintf(".throttle%i", m_node) + ".link_utilization");
+
+    for (MessageSizeType type = MessageSizeType_FIRST;
+         type < MessageSizeType_NUM; ++type) {
+        m_msg_counts[(unsigned int)type]
+            .init(Network::getNumberOfVirtualNetworks())
+            .name(parent + csprintf(".throttle%i", m_node) + ".msg_count." +
+                    MessageSizeType_to_string(type))
+            .flags(Stats::nozero)
+            ;
+        m_msg_bytes[(unsigned int) type]
+            .name(parent + csprintf(".throttle%i", m_node) + ".msg_bytes." +
+                    MessageSizeType_to_string(type))
+            .flags(Stats::nozero)
+            ;
+
+        m_msg_bytes[(unsigned int) type] = m_msg_counts[type] * Stats::constant(
+                Network::MessageSizeType_to_int(type));
+    }
 }
 
-double Throttle::getUtilization() const
+void
+Throttle::clearStats()
 {
-  return (100.0 * double(m_links_utilized)) / (double(g_eventQueue_ptr->getTime()-m_ruby_start));
+    m_link_utilization_proxy = 0;
 }
 
-void Throttle::print(ostream& out) const
+void
+Throttle::collateStats()
 {
-  out << "[Throttle: " << m_sID << " " << m_node << " bw: " << getLinkBandwidth() << "]";
+    m_link_utilization = 100.0 * m_link_utilization_proxy
+        / (double(g_system_ptr->curCycle() - g_ruby_start));
 }
 
-// Helper function
+void
+Throttle::print(ostream& out) const
+{
+    ccprintf(out,  "[%i bw: %i]", m_node, getLinkBandwidth());
+}
 
-static
-int network_message_to_size(NetworkMessage* net_msg_ptr)
+int
+network_message_to_size(NetworkMessage* net_msg_ptr)
 {
-  assert(net_msg_ptr != NULL);
+    assert(net_msg_ptr != NULL);
 
-  // Artificially increase the size of broadcast messages
-  if (BROADCAST_SCALING > 1) {
-    if (net_msg_ptr->getDestination().isBroadcast()) {
-      return (MessageSizeType_to_int(net_msg_ptr->getMessageSize()) * MESSAGE_SIZE_MULTIPLIER * BROADCAST_SCALING);
-    }
-  }
-  return (MessageSizeType_to_int(net_msg_ptr->getMessageSize()) * MESSAGE_SIZE_MULTIPLIER);
+    int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
+    size *=  MESSAGE_SIZE_MULTIPLIER;
+
+    // Artificially increase the size of broadcast messages
+    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
+        size *= BROADCAST_SCALING;
+
+    return size;
 }