Throttle.cc revision 6154
16145Snate@binkert.org
26145Snate@binkert.org/*
36145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
46145Snate@binkert.org * All rights reserved.
56145Snate@binkert.org *
66145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org * modification, are permitted provided that the following conditions are
86145Snate@binkert.org * met: redistributions of source code must retain the above copyright
96145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org * documentation and/or other materials provided with the distribution;
136145Snate@binkert.org * neither the name of the copyright holders nor the names of its
146145Snate@binkert.org * contributors may be used to endorse or promote products derived from
156145Snate@binkert.org * this software without specific prior written permission.
166145Snate@binkert.org *
176145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org */
296145Snate@binkert.org
306145Snate@binkert.org/*
316145Snate@binkert.org * $Id$
326145Snate@binkert.org *
336145Snate@binkert.org * Description: see Throttle.h
346145Snate@binkert.org *
356145Snate@binkert.org */
366145Snate@binkert.org
376154Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
386154Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
396154Snate@binkert.org#include "mem/ruby/network/Network.hh"
406154Snate@binkert.org#include "mem/ruby/system/System.hh"
416154Snate@binkert.org#include "mem/ruby/slicc_interface/NetworkMessage.hh"
426154Snate@binkert.org#include "mem/protocol/Protocol.hh"
436145Snate@binkert.org
446145Snate@binkert.orgconst int HIGH_RANGE = 256;
456145Snate@binkert.orgconst int ADJUST_INTERVAL = 50000;
466145Snate@binkert.orgconst int MESSAGE_SIZE_MULTIPLIER = 1000;
476145Snate@binkert.org//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
486145Snate@binkert.orgconst int BROADCAST_SCALING = 1;
496145Snate@binkert.orgconst int PRIORITY_SWITCH_LIMIT = 128;
506145Snate@binkert.org
516145Snate@binkert.orgstatic int network_message_to_size(NetworkMessage* net_msg_ptr);
526145Snate@binkert.org
536145Snate@binkert.orgextern std::ostream * debug_cout_ptr;
546145Snate@binkert.org
556145Snate@binkert.orgThrottle::Throttle(int sID, NodeID node, int link_latency, int link_bandwidth_multiplier)
566145Snate@binkert.org{
576145Snate@binkert.org  init(node, link_latency, link_bandwidth_multiplier);
586145Snate@binkert.org  m_sID = sID;
596145Snate@binkert.org}
606145Snate@binkert.org
616145Snate@binkert.orgThrottle::Throttle(NodeID node, int link_latency, int link_bandwidth_multiplier)
626145Snate@binkert.org{
636145Snate@binkert.org  init(node, link_latency, link_bandwidth_multiplier);
646145Snate@binkert.org  m_sID = 0;
656145Snate@binkert.org}
666145Snate@binkert.org
676145Snate@binkert.orgvoid Throttle::init(NodeID node, int link_latency, int link_bandwidth_multiplier)
686145Snate@binkert.org{
696145Snate@binkert.org  m_node = node;
706145Snate@binkert.org  m_vnets = 0;
716145Snate@binkert.org
726145Snate@binkert.org  ASSERT(link_bandwidth_multiplier > 0);
736145Snate@binkert.org  m_link_bandwidth_multiplier = link_bandwidth_multiplier;
746145Snate@binkert.org  m_link_latency = link_latency;
756145Snate@binkert.org
766145Snate@binkert.org  m_bash_counter = HIGH_RANGE;
776145Snate@binkert.org  m_bandwidth_since_sample = 0;
786145Snate@binkert.org  m_last_bandwidth_sample = 0;
796145Snate@binkert.org  m_wakeups_wo_switch = 0;
806145Snate@binkert.org  clearStats();
816145Snate@binkert.org}
826145Snate@binkert.org
836145Snate@binkert.orgvoid Throttle::clear()
846145Snate@binkert.org{
856145Snate@binkert.org  for (int counter = 0; counter < m_vnets; counter++) {
866145Snate@binkert.org    m_in[counter]->clear();
876145Snate@binkert.org    m_out[counter]->clear();
886145Snate@binkert.org  }
896145Snate@binkert.org}
906145Snate@binkert.org
916145Snate@binkert.orgvoid Throttle::addLinks(const Vector<MessageBuffer*>& in_vec, const Vector<MessageBuffer*>& out_vec)
926145Snate@binkert.org{
936145Snate@binkert.org  assert(in_vec.size() == out_vec.size());
946145Snate@binkert.org  for (int i=0; i<in_vec.size(); i++) {
956145Snate@binkert.org    addVirtualNetwork(in_vec[i], out_vec[i]);
966145Snate@binkert.org  }
976145Snate@binkert.org
986145Snate@binkert.org  m_message_counters.setSize(MessageSizeType_NUM);
996145Snate@binkert.org  for (int i=0; i<MessageSizeType_NUM; i++) {
1006145Snate@binkert.org    m_message_counters[i].setSize(in_vec.size());
1016145Snate@binkert.org    for (int j=0; j<m_message_counters[i].size(); j++) {
1026145Snate@binkert.org      m_message_counters[i][j] = 0;
1036145Snate@binkert.org    }
1046145Snate@binkert.org  }
1056145Snate@binkert.org
1066145Snate@binkert.org  if (g_PRINT_TOPOLOGY) {
1076145Snate@binkert.org    m_out_link_vec.insertAtBottom(out_vec);
1086145Snate@binkert.org  }
1096145Snate@binkert.org}
1106145Snate@binkert.org
1116145Snate@binkert.orgvoid Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr)
1126145Snate@binkert.org{
1136145Snate@binkert.org  m_units_remaining.insertAtBottom(0);
1146145Snate@binkert.org  m_in.insertAtBottom(in_ptr);
1156145Snate@binkert.org  m_out.insertAtBottom(out_ptr);
1166145Snate@binkert.org
1176145Snate@binkert.org  // Set consumer and description
1186145Snate@binkert.org  m_in[m_vnets]->setConsumer(this);
1196145Snate@binkert.org  string desc = "[Queue to Throttle " + NodeIDToString(m_sID) + " " + NodeIDToString(m_node) + "]";
1206145Snate@binkert.org  m_in[m_vnets]->setDescription(desc);
1216145Snate@binkert.org  m_vnets++;
1226145Snate@binkert.org}
1236145Snate@binkert.org
1246145Snate@binkert.orgvoid Throttle::wakeup()
1256145Snate@binkert.org{
1266145Snate@binkert.org  // Limits the number of message sent to a limited number of bytes/cycle.
1276145Snate@binkert.org  assert(getLinkBandwidth() > 0);
1286145Snate@binkert.org  int bw_remaining = getLinkBandwidth();
1296145Snate@binkert.org
1306145Snate@binkert.org  // Give the highest numbered link priority most of the time
1316145Snate@binkert.org  m_wakeups_wo_switch++;
1326145Snate@binkert.org  int highest_prio_vnet = m_vnets-1;
1336145Snate@binkert.org  int lowest_prio_vnet = 0;
1346145Snate@binkert.org  int counter = 1;
1356145Snate@binkert.org  bool schedule_wakeup = false;
1366145Snate@binkert.org
1376145Snate@binkert.org  // invert priorities to avoid starvation seen in the component network
1386145Snate@binkert.org  if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
1396145Snate@binkert.org    m_wakeups_wo_switch = 0;
1406145Snate@binkert.org    highest_prio_vnet = 0;
1416145Snate@binkert.org    lowest_prio_vnet = m_vnets-1;
1426145Snate@binkert.org    counter = -1;
1436145Snate@binkert.org  }
1446145Snate@binkert.org
1456145Snate@binkert.org  for (int vnet = highest_prio_vnet; (vnet*counter) >= (counter*lowest_prio_vnet); vnet -= counter) {
1466145Snate@binkert.org
1476145Snate@binkert.org    assert(m_out[vnet] != NULL);
1486145Snate@binkert.org    assert(m_in[vnet] != NULL);
1496145Snate@binkert.org    assert(m_units_remaining[vnet] >= 0);
1506145Snate@binkert.org
1516145Snate@binkert.org    while ((bw_remaining > 0) && ((m_in[vnet]->isReady()) || (m_units_remaining[vnet] > 0)) && m_out[vnet]->areNSlotsAvailable(1)) {
1526145Snate@binkert.org
1536145Snate@binkert.org      // See if we are done transferring the previous message on this virtual network
1546145Snate@binkert.org      if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
1556145Snate@binkert.org
1566145Snate@binkert.org        // Find the size of the message we are moving
1576145Snate@binkert.org        MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
1586145Snate@binkert.org        NetworkMessage* net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref());
1596145Snate@binkert.org        m_units_remaining[vnet] += network_message_to_size(net_msg_ptr);
1606145Snate@binkert.org
1616145Snate@binkert.org        DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
1626145Snate@binkert.org        DEBUG_MSG(NETWORK_COMP,HighPrio,"throttle: " + int_to_string(m_node)
1636145Snate@binkert.org                  + " my bw " + int_to_string(getLinkBandwidth())
1646145Snate@binkert.org                  + " bw spent enqueueing net msg " + int_to_string(m_units_remaining[vnet])
1656145Snate@binkert.org                  + " time: " + int_to_string(g_eventQueue_ptr->getTime()) + ".");
1666145Snate@binkert.org
1676145Snate@binkert.org        // Move the message
1686145Snate@binkert.org        m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
1696145Snate@binkert.org        m_in[vnet]->pop();
1706145Snate@binkert.org
1716145Snate@binkert.org        // Count the message
1726145Snate@binkert.org        m_message_counters[net_msg_ptr->getMessageSize()][vnet]++;
1736145Snate@binkert.org
1746145Snate@binkert.org        DEBUG_MSG(NETWORK_COMP,LowPrio,*m_out[vnet]);
1756145Snate@binkert.org        DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
1766145Snate@binkert.org      }
1776145Snate@binkert.org
1786145Snate@binkert.org      // Calculate the amount of bandwidth we spent on this message
1796145Snate@binkert.org      int diff = m_units_remaining[vnet] - bw_remaining;
1806145Snate@binkert.org      m_units_remaining[vnet] = max(0, diff);
1816145Snate@binkert.org      bw_remaining = max(0, -diff);
1826145Snate@binkert.org    }
1836145Snate@binkert.org
1846145Snate@binkert.org    if ((bw_remaining > 0) && ((m_in[vnet]->isReady()) || (m_units_remaining[vnet] > 0)) && !m_out[vnet]->areNSlotsAvailable(1)) {
1856145Snate@binkert.org      DEBUG_MSG(NETWORK_COMP,LowPrio,vnet);
1866145Snate@binkert.org      schedule_wakeup = true; // schedule me to wakeup again because I'm waiting for my output queue to become available
1876145Snate@binkert.org    }
1886145Snate@binkert.org  }
1896145Snate@binkert.org
1906145Snate@binkert.org  // We should only wake up when we use the bandwidth
1916145Snate@binkert.org  //  assert(bw_remaining != getLinkBandwidth());  // This is only mostly true
1926145Snate@binkert.org
1936145Snate@binkert.org  // Record that we used some or all of the link bandwidth this cycle
1946145Snate@binkert.org  double ratio = 1.0-(double(bw_remaining)/double(getLinkBandwidth()));
1956145Snate@binkert.org  // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
1966145Snate@binkert.org  linkUtilized(ratio);
1976145Snate@binkert.org
1986145Snate@binkert.org  // Sample the link bandwidth utilization over a number of cycles
1996145Snate@binkert.org  int bw_used = getLinkBandwidth()-bw_remaining;
2006145Snate@binkert.org  m_bandwidth_since_sample += bw_used;
2016145Snate@binkert.org
2026145Snate@binkert.org  // FIXME - comment out the bash specific code for faster performance
2036145Snate@binkert.org  // Start Bash code
2046145Snate@binkert.org  // Update the predictor
2056145Snate@binkert.org  Time current_time = g_eventQueue_ptr->getTime();
2066145Snate@binkert.org  while ((current_time - m_last_bandwidth_sample) > ADJUST_INTERVAL) {
2076145Snate@binkert.org    double utilization = m_bandwidth_since_sample/double(ADJUST_INTERVAL * getLinkBandwidth());
2086145Snate@binkert.org
2096145Snate@binkert.org    if (utilization > g_bash_bandwidth_adaptive_threshold) {
2106145Snate@binkert.org      // Used more bandwidth
2116145Snate@binkert.org      m_bash_counter++;
2126145Snate@binkert.org    } else {
2136145Snate@binkert.org      // Used less bandwidth
2146145Snate@binkert.org      m_bash_counter--;
2156145Snate@binkert.org    }
2166145Snate@binkert.org
2176145Snate@binkert.org    // Make sure we don't overflow
2186145Snate@binkert.org    m_bash_counter = min(HIGH_RANGE, m_bash_counter);
2196145Snate@binkert.org    m_bash_counter = max(0, m_bash_counter);
2206145Snate@binkert.org
2216145Snate@binkert.org    // Reset samples
2226145Snate@binkert.org    m_last_bandwidth_sample += ADJUST_INTERVAL;
2236145Snate@binkert.org    m_bandwidth_since_sample = 0;
2246145Snate@binkert.org  }
2256145Snate@binkert.org  // End Bash code
2266145Snate@binkert.org
2276145Snate@binkert.org  if ((bw_remaining > 0) && !schedule_wakeup) {
2286145Snate@binkert.org    // We have extra bandwidth and our output buffer was available, so we must not have anything else to do until another message arrives.
2296145Snate@binkert.org    DEBUG_MSG(NETWORK_COMP,LowPrio,*this);
2306145Snate@binkert.org    DEBUG_MSG(NETWORK_COMP,LowPrio,"not scheduled again");
2316145Snate@binkert.org  } else {
2326145Snate@binkert.org    DEBUG_MSG(NETWORK_COMP,LowPrio,*this);
2336145Snate@binkert.org    DEBUG_MSG(NETWORK_COMP,LowPrio,"scheduled again");
2346145Snate@binkert.org    // We are out of bandwidth for this cycle, so wakeup next cycle and continue
2356145Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, 1);
2366145Snate@binkert.org  }
2376145Snate@binkert.org}
2386145Snate@binkert.org
2396145Snate@binkert.orgbool Throttle::broadcastBandwidthAvailable(int rand) const
2406145Snate@binkert.org{
2416145Snate@binkert.org  bool result =  !(m_bash_counter > ((HIGH_RANGE/4) + (rand % (HIGH_RANGE/2))));
2426145Snate@binkert.org  return result;
2436145Snate@binkert.org}
2446145Snate@binkert.org
2456145Snate@binkert.orgvoid Throttle::printStats(ostream& out) const
2466145Snate@binkert.org{
2476145Snate@binkert.org  out << "utilized_percent: " << getUtilization() << endl;
2486145Snate@binkert.org}
2496145Snate@binkert.org
2506145Snate@binkert.orgvoid Throttle::clearStats()
2516145Snate@binkert.org{
2526145Snate@binkert.org  m_ruby_start = g_eventQueue_ptr->getTime();
2536145Snate@binkert.org  m_links_utilized = 0.0;
2546145Snate@binkert.org
2556145Snate@binkert.org  for (int i=0; i<m_message_counters.size(); i++) {
2566145Snate@binkert.org    for (int j=0; j<m_message_counters[i].size(); j++) {
2576145Snate@binkert.org      m_message_counters[i][j] = 0;
2586145Snate@binkert.org    }
2596145Snate@binkert.org  }
2606145Snate@binkert.org}
2616145Snate@binkert.org
2626145Snate@binkert.orgvoid Throttle::printConfig(ostream& out) const
2636145Snate@binkert.org{
2646145Snate@binkert.org
2656145Snate@binkert.org}
2666145Snate@binkert.org
2676145Snate@binkert.orgdouble Throttle::getUtilization() const
2686145Snate@binkert.org{
2696145Snate@binkert.org  return (100.0 * double(m_links_utilized)) / (double(g_eventQueue_ptr->getTime()-m_ruby_start));
2706145Snate@binkert.org}
2716145Snate@binkert.org
2726145Snate@binkert.orgvoid Throttle::print(ostream& out) const
2736145Snate@binkert.org{
2746145Snate@binkert.org  out << "[Throttle: " << m_sID << " " << m_node << " bw: " << getLinkBandwidth() << "]";
2756145Snate@binkert.org}
2766145Snate@binkert.org
2776145Snate@binkert.org// Helper function
2786145Snate@binkert.org
2796145Snate@binkert.orgstatic
2806145Snate@binkert.orgint network_message_to_size(NetworkMessage* net_msg_ptr)
2816145Snate@binkert.org{
2826145Snate@binkert.org  assert(net_msg_ptr != NULL);
2836145Snate@binkert.org
2846145Snate@binkert.org  // Artificially increase the size of broadcast messages
2856145Snate@binkert.org  if (BROADCAST_SCALING > 1) {
2866145Snate@binkert.org    if (net_msg_ptr->getDestination().isBroadcast()) {
2876145Snate@binkert.org      return (MessageSizeType_to_int(net_msg_ptr->getMessageSize()) * MESSAGE_SIZE_MULTIPLIER * BROADCAST_SCALING);
2886145Snate@binkert.org    }
2896145Snate@binkert.org  }
2906145Snate@binkert.org  return (MessageSizeType_to_int(net_msg_ptr->getMessageSize()) * MESSAGE_SIZE_MULTIPLIER);
2916145Snate@binkert.org}
292