Throttle.cc revision 7055
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297054Snate@binkert.org#include "base/cprintf.hh"
307054Snate@binkert.org#include "mem/protocol/Protocol.hh"
316154Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
326154Snate@binkert.org#include "mem/ruby/network/Network.hh"
337054Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
347054Snate@binkert.org#include "mem/ruby/slicc_interface/NetworkMessage.hh"
356154Snate@binkert.org#include "mem/ruby/system/System.hh"
366145Snate@binkert.org
377055Snate@binkert.orgusing namespace std;
387055Snate@binkert.org
396145Snate@binkert.orgconst int HIGH_RANGE = 256;
406145Snate@binkert.orgconst int ADJUST_INTERVAL = 50000;
416145Snate@binkert.orgconst int MESSAGE_SIZE_MULTIPLIER = 1000;
426145Snate@binkert.org//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
436145Snate@binkert.orgconst int BROADCAST_SCALING = 1;
446145Snate@binkert.orgconst int PRIORITY_SWITCH_LIMIT = 128;
456145Snate@binkert.org
466145Snate@binkert.orgstatic int network_message_to_size(NetworkMessage* net_msg_ptr);
476145Snate@binkert.org
487055Snate@binkert.orgextern ostream *debug_cout_ptr;
496145Snate@binkert.org
507054Snate@binkert.orgThrottle::Throttle(int sID, NodeID node, int link_latency,
517054Snate@binkert.org    int link_bandwidth_multiplier)
526145Snate@binkert.org{
537054Snate@binkert.org    init(node, link_latency, link_bandwidth_multiplier);
547054Snate@binkert.org    m_sID = sID;
556145Snate@binkert.org}
566145Snate@binkert.org
577054Snate@binkert.orgThrottle::Throttle(NodeID node, int link_latency,
587054Snate@binkert.org    int link_bandwidth_multiplier)
596145Snate@binkert.org{
607054Snate@binkert.org    init(node, link_latency, link_bandwidth_multiplier);
617054Snate@binkert.org    m_sID = 0;
626145Snate@binkert.org}
636145Snate@binkert.org
647054Snate@binkert.orgvoid
657054Snate@binkert.orgThrottle::init(NodeID node, int link_latency, int link_bandwidth_multiplier)
666145Snate@binkert.org{
677054Snate@binkert.org    m_node = node;
687054Snate@binkert.org    m_vnets = 0;
696145Snate@binkert.org
707054Snate@binkert.org    ASSERT(link_bandwidth_multiplier > 0);
717054Snate@binkert.org    m_link_bandwidth_multiplier = link_bandwidth_multiplier;
727054Snate@binkert.org    m_link_latency = link_latency;
736145Snate@binkert.org
747054Snate@binkert.org    m_wakeups_wo_switch = 0;
757054Snate@binkert.org    clearStats();
766145Snate@binkert.org}
776145Snate@binkert.org
787054Snate@binkert.orgvoid
797054Snate@binkert.orgThrottle::clear()
806145Snate@binkert.org{
817054Snate@binkert.org    for (int counter = 0; counter < m_vnets; counter++) {
827054Snate@binkert.org        m_in[counter]->clear();
837054Snate@binkert.org        m_out[counter]->clear();
847054Snate@binkert.org    }
856145Snate@binkert.org}
866145Snate@binkert.org
877054Snate@binkert.orgvoid
887054Snate@binkert.orgThrottle::addLinks(const Vector<MessageBuffer*>& in_vec,
897054Snate@binkert.org    const Vector<MessageBuffer*>& out_vec)
906145Snate@binkert.org{
917054Snate@binkert.org    assert(in_vec.size() == out_vec.size());
927054Snate@binkert.org    for (int i=0; i<in_vec.size(); i++) {
937054Snate@binkert.org        addVirtualNetwork(in_vec[i], out_vec[i]);
947054Snate@binkert.org    }
956145Snate@binkert.org
967054Snate@binkert.org    m_message_counters.setSize(MessageSizeType_NUM);
977054Snate@binkert.org    for (int i = 0; i < MessageSizeType_NUM; i++) {
987054Snate@binkert.org        m_message_counters[i].setSize(in_vec.size());
997054Snate@binkert.org        for (int j = 0; j<m_message_counters[i].size(); j++) {
1007054Snate@binkert.org            m_message_counters[i][j] = 0;
1017054Snate@binkert.org        }
1026145Snate@binkert.org    }
1036145Snate@binkert.org}
1046145Snate@binkert.org
1057054Snate@binkert.orgvoid
1067054Snate@binkert.orgThrottle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr)
1076145Snate@binkert.org{
1087054Snate@binkert.org    m_units_remaining.insertAtBottom(0);
1097054Snate@binkert.org    m_in.insertAtBottom(in_ptr);
1107054Snate@binkert.org    m_out.insertAtBottom(out_ptr);
1116145Snate@binkert.org
1127054Snate@binkert.org    // Set consumer and description
1137054Snate@binkert.org    m_in[m_vnets]->setConsumer(this);
1147054Snate@binkert.org    string desc = "[Queue to Throttle " + NodeIDToString(m_sID) + " " +
1157054Snate@binkert.org        NodeIDToString(m_node) + "]";
1167054Snate@binkert.org    m_in[m_vnets]->setDescription(desc);
1177054Snate@binkert.org    m_vnets++;
1186145Snate@binkert.org}
1196145Snate@binkert.org
1207054Snate@binkert.orgvoid
1217054Snate@binkert.orgThrottle::wakeup()
1226145Snate@binkert.org{
1237054Snate@binkert.org    // Limits the number of message sent to a limited number of bytes/cycle.
1247054Snate@binkert.org    assert(getLinkBandwidth() > 0);
1257054Snate@binkert.org    int bw_remaining = getLinkBandwidth();
1266145Snate@binkert.org
1277054Snate@binkert.org    // Give the highest numbered link priority most of the time
1287054Snate@binkert.org    m_wakeups_wo_switch++;
1297054Snate@binkert.org    int highest_prio_vnet = m_vnets-1;
1307054Snate@binkert.org    int lowest_prio_vnet = 0;
1317054Snate@binkert.org    int counter = 1;
1327054Snate@binkert.org    bool schedule_wakeup = false;
1336145Snate@binkert.org
1347054Snate@binkert.org    // invert priorities to avoid starvation seen in the component network
1357054Snate@binkert.org    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
1367054Snate@binkert.org        m_wakeups_wo_switch = 0;
1377054Snate@binkert.org        highest_prio_vnet = 0;
1387054Snate@binkert.org        lowest_prio_vnet = m_vnets-1;
1397054Snate@binkert.org        counter = -1;
1406145Snate@binkert.org    }
1416145Snate@binkert.org
1427054Snate@binkert.org    for (int vnet = highest_prio_vnet;
1437054Snate@binkert.org         (vnet * counter) >= (counter * lowest_prio_vnet);
1447054Snate@binkert.org         vnet -= counter) {
1457054Snate@binkert.org
1467054Snate@binkert.org        assert(m_out[vnet] != NULL);
1477054Snate@binkert.org        assert(m_in[vnet] != NULL);
1487054Snate@binkert.org        assert(m_units_remaining[vnet] >= 0);
1497054Snate@binkert.org
1507054Snate@binkert.org        while (bw_remaining > 0 &&
1517054Snate@binkert.org            (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
1527054Snate@binkert.org            m_out[vnet]->areNSlotsAvailable(1)) {
1537054Snate@binkert.org
1547054Snate@binkert.org            // See if we are done transferring the previous message on
1557054Snate@binkert.org            // this virtual network
1567054Snate@binkert.org            if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
1577054Snate@binkert.org                // Find the size of the message we are moving
1587054Snate@binkert.org                MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
1597054Snate@binkert.org                NetworkMessage* net_msg_ptr =
1607054Snate@binkert.org                    safe_cast<NetworkMessage*>(msg_ptr.ref());
1617054Snate@binkert.org                m_units_remaining[vnet] +=
1627054Snate@binkert.org                    network_message_to_size(net_msg_ptr);
1637054Snate@binkert.org
1647054Snate@binkert.org                DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
1657054Snate@binkert.org                DEBUG_MSG(NETWORK_COMP, HighPrio,
1667054Snate@binkert.org                    csprintf("throttle: %d my bw %d bw spent enqueueing "
1677054Snate@binkert.org                        "net msg %d time: %d.",
1687054Snate@binkert.org                        m_node, getLinkBandwidth(), m_units_remaining[vnet],
1697054Snate@binkert.org                        g_eventQueue_ptr->getTime()));
1707054Snate@binkert.org
1717054Snate@binkert.org                // Move the message
1727054Snate@binkert.org                m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
1737054Snate@binkert.org                m_in[vnet]->pop();
1747054Snate@binkert.org
1757054Snate@binkert.org                // Count the message
1767054Snate@binkert.org                m_message_counters[net_msg_ptr->getMessageSize()][vnet]++;
1777054Snate@binkert.org
1787054Snate@binkert.org                DEBUG_MSG(NETWORK_COMP,LowPrio,*m_out[vnet]);
1797054Snate@binkert.org                DEBUG_NEWLINE(NETWORK_COMP,HighPrio);
1807054Snate@binkert.org            }
1817054Snate@binkert.org
1827054Snate@binkert.org            // Calculate the amount of bandwidth we spent on this message
1837054Snate@binkert.org            int diff = m_units_remaining[vnet] - bw_remaining;
1847054Snate@binkert.org            m_units_remaining[vnet] = max(0, diff);
1857054Snate@binkert.org            bw_remaining = max(0, -diff);
1867054Snate@binkert.org        }
1877054Snate@binkert.org
1887054Snate@binkert.org        if (bw_remaining > 0 &&
1897054Snate@binkert.org            (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
1907054Snate@binkert.org            !m_out[vnet]->areNSlotsAvailable(1)) {
1917054Snate@binkert.org            DEBUG_MSG(NETWORK_COMP,LowPrio,vnet);
1927054Snate@binkert.org            // schedule me to wakeup again because I'm waiting for my
1937054Snate@binkert.org            // output queue to become available
1947054Snate@binkert.org            schedule_wakeup = true;
1957054Snate@binkert.org        }
1966145Snate@binkert.org    }
1976145Snate@binkert.org
1987054Snate@binkert.org    // We should only wake up when we use the bandwidth
1997054Snate@binkert.org    // This is only mostly true
2007054Snate@binkert.org    // assert(bw_remaining != getLinkBandwidth());
2016145Snate@binkert.org
2027054Snate@binkert.org    // Record that we used some or all of the link bandwidth this cycle
2037054Snate@binkert.org    double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
2046145Snate@binkert.org
2057054Snate@binkert.org    // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
2067054Snate@binkert.org    linkUtilized(ratio);
2077054Snate@binkert.org
2087054Snate@binkert.org    if (bw_remaining > 0 && !schedule_wakeup) {
2097054Snate@binkert.org        // We have extra bandwidth and our output buffer was
2107054Snate@binkert.org        // available, so we must not have anything else to do until
2117054Snate@binkert.org        // another message arrives.
2127054Snate@binkert.org        DEBUG_MSG(NETWORK_COMP, LowPrio, *this);
2137054Snate@binkert.org        DEBUG_MSG(NETWORK_COMP, LowPrio, "not scheduled again");
2147054Snate@binkert.org    } else {
2157054Snate@binkert.org        DEBUG_MSG(NETWORK_COMP, LowPrio, *this);
2167054Snate@binkert.org        DEBUG_MSG(NETWORK_COMP, LowPrio, "scheduled again");
2177054Snate@binkert.org
2187054Snate@binkert.org        // We are out of bandwidth for this cycle, so wakeup next
2197054Snate@binkert.org        // cycle and continue
2207054Snate@binkert.org        g_eventQueue_ptr->scheduleEvent(this, 1);
2217054Snate@binkert.org    }
2226145Snate@binkert.org}
2236145Snate@binkert.org
2247054Snate@binkert.orgvoid
2257054Snate@binkert.orgThrottle::printStats(ostream& out) const
2266145Snate@binkert.org{
2277054Snate@binkert.org    out << "utilized_percent: " << getUtilization() << endl;
2286145Snate@binkert.org}
2296145Snate@binkert.org
2307054Snate@binkert.orgvoid
2317054Snate@binkert.orgThrottle::clearStats()
2326145Snate@binkert.org{
2337054Snate@binkert.org    m_ruby_start = g_eventQueue_ptr->getTime();
2347054Snate@binkert.org    m_links_utilized = 0.0;
2356145Snate@binkert.org
2367054Snate@binkert.org    for (int i = 0; i < m_message_counters.size(); i++) {
2377054Snate@binkert.org        for (int j = 0; j < m_message_counters[i].size(); j++) {
2387054Snate@binkert.org            m_message_counters[i][j] = 0;
2397054Snate@binkert.org        }
2406145Snate@binkert.org    }
2416145Snate@binkert.org}
2426145Snate@binkert.org
2437054Snate@binkert.orgvoid
2447054Snate@binkert.orgThrottle::printConfig(ostream& out) const
2456145Snate@binkert.org{
2466145Snate@binkert.org}
2476145Snate@binkert.org
2487054Snate@binkert.orgdouble
2497054Snate@binkert.orgThrottle::getUtilization() const
2506145Snate@binkert.org{
2517054Snate@binkert.org    return 100.0 * double(m_links_utilized) /
2527054Snate@binkert.org        double(g_eventQueue_ptr->getTime()-m_ruby_start);
2536145Snate@binkert.org}
2546145Snate@binkert.org
2557054Snate@binkert.orgvoid
2567054Snate@binkert.orgThrottle::print(ostream& out) const
2576145Snate@binkert.org{
2587054Snate@binkert.org    out << "[Throttle: " << m_sID << " " << m_node
2597054Snate@binkert.org        << " bw: " << getLinkBandwidth() << "]";
2606145Snate@binkert.org}
2616145Snate@binkert.org
2627054Snate@binkert.orgint
2637054Snate@binkert.orgnetwork_message_to_size(NetworkMessage* net_msg_ptr)
2647054Snate@binkert.org{
2657054Snate@binkert.org    assert(net_msg_ptr != NULL);
2666145Snate@binkert.org
2677054Snate@binkert.org    int size = RubySystem::getNetwork()->
2687054Snate@binkert.org        MessageSizeType_to_int(net_msg_ptr->getMessageSize());
2697054Snate@binkert.org    size *=  MESSAGE_SIZE_MULTIPLIER;
2706145Snate@binkert.org
2717054Snate@binkert.org    // Artificially increase the size of broadcast messages
2727054Snate@binkert.org    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
2737054Snate@binkert.org        size *= BROADCAST_SCALING;
2747054Snate@binkert.org
2757054Snate@binkert.org    return size;
2766145Snate@binkert.org}
277