Throttle.cc revision 10311
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
297832Snate@binkert.org#include <cassert>
307832Snate@binkert.org
318645Snilay@cs.wisc.edu#include "base/cast.hh"
327054Snate@binkert.org#include "base/cprintf.hh"
338232Snate@binkert.org#include "debug/RubyNetwork.hh"
348229Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
3510301Snilay@cs.wisc.edu#include "mem/ruby/network/MessageBuffer.hh"
366154Snate@binkert.org#include "mem/ruby/network/Network.hh"
377054Snate@binkert.org#include "mem/ruby/slicc_interface/NetworkMessage.hh"
386154Snate@binkert.org#include "mem/ruby/system/System.hh"
396145Snate@binkert.org
407055Snate@binkert.orgusing namespace std;
417055Snate@binkert.org
426145Snate@binkert.orgconst int MESSAGE_SIZE_MULTIPLIER = 1000;
436145Snate@binkert.org//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
446145Snate@binkert.orgconst int BROADCAST_SCALING = 1;
456145Snate@binkert.orgconst int PRIORITY_SWITCH_LIMIT = 128;
466145Snate@binkert.org
476145Snate@binkert.orgstatic int network_message_to_size(NetworkMessage* net_msg_ptr);
486145Snate@binkert.org
499499Snilay@cs.wisc.eduThrottle::Throttle(int sID, NodeID node, Cycles link_latency,
509230Snilay@cs.wisc.edu                   int link_bandwidth_multiplier, int endpoint_bandwidth,
519465Snilay@cs.wisc.edu                   ClockedObject *em)
529230Snilay@cs.wisc.edu    : Consumer(em)
536145Snate@binkert.org{
548259SBrad.Beckmann@amd.com    init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
557054Snate@binkert.org    m_sID = sID;
566145Snate@binkert.org}
576145Snate@binkert.org
589499Snilay@cs.wisc.eduThrottle::Throttle(NodeID node, Cycles link_latency,
599230Snilay@cs.wisc.edu                   int link_bandwidth_multiplier, int endpoint_bandwidth,
609465Snilay@cs.wisc.edu                   ClockedObject *em)
619230Snilay@cs.wisc.edu    : Consumer(em)
626145Snate@binkert.org{
638259SBrad.Beckmann@amd.com    init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
647054Snate@binkert.org    m_sID = 0;
656145Snate@binkert.org}
666145Snate@binkert.org
677054Snate@binkert.orgvoid
689499Snilay@cs.wisc.eduThrottle::init(NodeID node, Cycles link_latency,
699499Snilay@cs.wisc.edu               int link_bandwidth_multiplier, int endpoint_bandwidth)
706145Snate@binkert.org{
717054Snate@binkert.org    m_node = node;
727832Snate@binkert.org    assert(link_bandwidth_multiplier > 0);
737054Snate@binkert.org    m_link_bandwidth_multiplier = link_bandwidth_multiplier;
7410311Snilay@cs.wisc.edu
757054Snate@binkert.org    m_link_latency = link_latency;
768259SBrad.Beckmann@amd.com    m_endpoint_bandwidth = endpoint_bandwidth;
776145Snate@binkert.org
787054Snate@binkert.org    m_wakeups_wo_switch = 0;
799863Snilay@cs.wisc.edu    m_link_utilization_proxy = 0;
806145Snate@binkert.org}
816145Snate@binkert.org
827054Snate@binkert.orgvoid
8310311Snilay@cs.wisc.eduThrottle::addLinks(const map<int, MessageBuffer*>& in_vec,
8410311Snilay@cs.wisc.edu                   const map<int, MessageBuffer*>& out_vec)
856145Snate@binkert.org{
867054Snate@binkert.org    assert(in_vec.size() == out_vec.size());
8710311Snilay@cs.wisc.edu
8810311Snilay@cs.wisc.edu    for (auto& it : in_vec) {
8910311Snilay@cs.wisc.edu        int vnet = it.first;
9010311Snilay@cs.wisc.edu
9110311Snilay@cs.wisc.edu        auto jt = out_vec.find(vnet);
9210311Snilay@cs.wisc.edu        assert(jt != out_vec.end());
9310311Snilay@cs.wisc.edu
9410311Snilay@cs.wisc.edu        MessageBuffer *in_ptr = it.second;
9510311Snilay@cs.wisc.edu        MessageBuffer *out_ptr = (*jt).second;
9610311Snilay@cs.wisc.edu
9710311Snilay@cs.wisc.edu        m_in[vnet] = in_ptr;
9810311Snilay@cs.wisc.edu        m_out[vnet] = out_ptr;
9910311Snilay@cs.wisc.edu        m_units_remaining[vnet] = 0;
10010311Snilay@cs.wisc.edu
10110311Snilay@cs.wisc.edu        // Set consumer and description
10210311Snilay@cs.wisc.edu        in_ptr->setConsumer(this);
10310311Snilay@cs.wisc.edu        string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
10410311Snilay@cs.wisc.edu            to_string(m_node) + "]";
10510311Snilay@cs.wisc.edu        in_ptr->setDescription(desc);
1067054Snate@binkert.org    }
1076145Snate@binkert.org}
1086145Snate@binkert.org
1097054Snate@binkert.orgvoid
11010311Snilay@cs.wisc.eduThrottle::operateVnet(int vnet, int &bw_remaining, bool &schedule_wakeup,
11110311Snilay@cs.wisc.edu                      MessageBuffer *in, MessageBuffer *out)
1126145Snate@binkert.org{
11310311Snilay@cs.wisc.edu    assert(out != NULL);
11410311Snilay@cs.wisc.edu    assert(in != NULL);
11510311Snilay@cs.wisc.edu    assert(m_units_remaining[vnet] >= 0);
1166145Snate@binkert.org
11710311Snilay@cs.wisc.edu    while (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
11810311Snilay@cs.wisc.edu                                out->areNSlotsAvailable(1)) {
1199465Snilay@cs.wisc.edu
12010311Snilay@cs.wisc.edu        // See if we are done transferring the previous message on
12110311Snilay@cs.wisc.edu        // this virtual network
12210311Snilay@cs.wisc.edu        if (m_units_remaining[vnet] == 0 && in->isReady()) {
12310311Snilay@cs.wisc.edu            // Find the size of the message we are moving
12410311Snilay@cs.wisc.edu            MsgPtr msg_ptr = in->peekMsgPtr();
12510311Snilay@cs.wisc.edu            NetworkMessage* net_msg_ptr =
12610311Snilay@cs.wisc.edu                safe_cast<NetworkMessage*>(msg_ptr.get());
12710311Snilay@cs.wisc.edu            m_units_remaining[vnet] +=
12810311Snilay@cs.wisc.edu                network_message_to_size(net_msg_ptr);
12910311Snilay@cs.wisc.edu
13010311Snilay@cs.wisc.edu            DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
13110311Snilay@cs.wisc.edu                    "enqueueing net msg %d time: %lld.\n",
13210311Snilay@cs.wisc.edu                    m_node, getLinkBandwidth(), m_units_remaining[vnet],
13310311Snilay@cs.wisc.edu                    g_system_ptr->curCycle());
13410311Snilay@cs.wisc.edu
13510311Snilay@cs.wisc.edu            // Move the message
13610311Snilay@cs.wisc.edu            in->dequeue();
13710311Snilay@cs.wisc.edu            out->enqueue(msg_ptr, m_link_latency);
13810311Snilay@cs.wisc.edu
13910311Snilay@cs.wisc.edu            // Count the message
14010311Snilay@cs.wisc.edu            m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++;
14110311Snilay@cs.wisc.edu            DPRINTF(RubyNetwork, "%s\n", *out);
14210311Snilay@cs.wisc.edu        }
14310311Snilay@cs.wisc.edu
14410311Snilay@cs.wisc.edu        // Calculate the amount of bandwidth we spent on this message
14510311Snilay@cs.wisc.edu        int diff = m_units_remaining[vnet] - bw_remaining;
14610311Snilay@cs.wisc.edu        m_units_remaining[vnet] = max(0, diff);
14710311Snilay@cs.wisc.edu        bw_remaining = max(0, -diff);
14810311Snilay@cs.wisc.edu    }
14910311Snilay@cs.wisc.edu
15010311Snilay@cs.wisc.edu    if (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
15110311Snilay@cs.wisc.edu                             !out->areNSlotsAvailable(1)) {
15210311Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "vnet: %d", vnet);
15310311Snilay@cs.wisc.edu
15410311Snilay@cs.wisc.edu        // schedule me to wakeup again because I'm waiting for my
15510311Snilay@cs.wisc.edu        // output queue to become available
15610311Snilay@cs.wisc.edu        schedule_wakeup = true;
15710311Snilay@cs.wisc.edu    }
1586145Snate@binkert.org}
1596145Snate@binkert.org
1607054Snate@binkert.orgvoid
1617054Snate@binkert.orgThrottle::wakeup()
1626145Snate@binkert.org{
1637054Snate@binkert.org    // Limits the number of message sent to a limited number of bytes/cycle.
1647054Snate@binkert.org    assert(getLinkBandwidth() > 0);
1657054Snate@binkert.org    int bw_remaining = getLinkBandwidth();
1666145Snate@binkert.org
1677054Snate@binkert.org    m_wakeups_wo_switch++;
1687054Snate@binkert.org    bool schedule_wakeup = false;
1696145Snate@binkert.org
17010311Snilay@cs.wisc.edu    // variable for deciding the direction in which to iterate
17110311Snilay@cs.wisc.edu    bool iteration_direction = false;
17210311Snilay@cs.wisc.edu
17310311Snilay@cs.wisc.edu
1747054Snate@binkert.org    // invert priorities to avoid starvation seen in the component network
1757054Snate@binkert.org    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
1767054Snate@binkert.org        m_wakeups_wo_switch = 0;
17710311Snilay@cs.wisc.edu        iteration_direction = true;
1786145Snate@binkert.org    }
1796145Snate@binkert.org
18010311Snilay@cs.wisc.edu    if (iteration_direction) {
18110311Snilay@cs.wisc.edu        for (auto& it : m_in) {
18210311Snilay@cs.wisc.edu            int vnet = it.first;
18310311Snilay@cs.wisc.edu            operateVnet(vnet, bw_remaining, schedule_wakeup,
18410311Snilay@cs.wisc.edu                        it.second, m_out[vnet]);
1857054Snate@binkert.org        }
18610311Snilay@cs.wisc.edu    } else {
18710311Snilay@cs.wisc.edu        for (auto it = m_in.rbegin(); it != m_in.rend(); ++it) {
18810311Snilay@cs.wisc.edu            int vnet = (*it).first;
18910311Snilay@cs.wisc.edu            operateVnet(vnet, bw_remaining, schedule_wakeup,
19010311Snilay@cs.wisc.edu                        (*it).second, m_out[vnet]);
1917054Snate@binkert.org        }
1926145Snate@binkert.org    }
1936145Snate@binkert.org
1947054Snate@binkert.org    // We should only wake up when we use the bandwidth
1957054Snate@binkert.org    // This is only mostly true
1967054Snate@binkert.org    // assert(bw_remaining != getLinkBandwidth());
1976145Snate@binkert.org
1987054Snate@binkert.org    // Record that we used some or all of the link bandwidth this cycle
1997054Snate@binkert.org    double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
2006145Snate@binkert.org
2017054Snate@binkert.org    // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
2029863Snilay@cs.wisc.edu    m_link_utilization_proxy += ratio;
2037054Snate@binkert.org
2047054Snate@binkert.org    if (bw_remaining > 0 && !schedule_wakeup) {
2057054Snate@binkert.org        // We have extra bandwidth and our output buffer was
2067054Snate@binkert.org        // available, so we must not have anything else to do until
2077054Snate@binkert.org        // another message arrives.
2087780Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
2097054Snate@binkert.org    } else {
2107780Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
2117054Snate@binkert.org
2127054Snate@binkert.org        // We are out of bandwidth for this cycle, so wakeup next
2137054Snate@binkert.org        // cycle and continue
2149499Snilay@cs.wisc.edu        scheduleEvent(Cycles(1));
2157054Snate@binkert.org    }
2166145Snate@binkert.org}
2176145Snate@binkert.org
2187054Snate@binkert.orgvoid
2199863Snilay@cs.wisc.eduThrottle::regStats(string parent)
2206145Snate@binkert.org{
2219863Snilay@cs.wisc.edu    m_link_utilization
2229863Snilay@cs.wisc.edu        .name(parent + csprintf(".throttle%i", m_node) + ".link_utilization");
2239863Snilay@cs.wisc.edu
2249863Snilay@cs.wisc.edu    for (MessageSizeType type = MessageSizeType_FIRST;
2259863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
2269863Snilay@cs.wisc.edu        m_msg_counts[(unsigned int)type]
22710311Snilay@cs.wisc.edu            .init(Network::getNumberOfVirtualNetworks())
2289863Snilay@cs.wisc.edu            .name(parent + csprintf(".throttle%i", m_node) + ".msg_count." +
2299863Snilay@cs.wisc.edu                    MessageSizeType_to_string(type))
2309863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2319863Snilay@cs.wisc.edu            ;
2329863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type]
2339863Snilay@cs.wisc.edu            .name(parent + csprintf(".throttle%i", m_node) + ".msg_bytes." +
2349863Snilay@cs.wisc.edu                    MessageSizeType_to_string(type))
2359863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2369863Snilay@cs.wisc.edu            ;
2379863Snilay@cs.wisc.edu
2389863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type] = m_msg_counts[type] * Stats::constant(
2399863Snilay@cs.wisc.edu                Network::MessageSizeType_to_int(type));
2409863Snilay@cs.wisc.edu    }
2416145Snate@binkert.org}
2426145Snate@binkert.org
2437054Snate@binkert.orgvoid
2447054Snate@binkert.orgThrottle::clearStats()
2456145Snate@binkert.org{
2469863Snilay@cs.wisc.edu    m_link_utilization_proxy = 0;
2476145Snate@binkert.org}
2486145Snate@binkert.org
2499863Snilay@cs.wisc.eduvoid
2509863Snilay@cs.wisc.eduThrottle::collateStats()
2516145Snate@binkert.org{
2529863Snilay@cs.wisc.edu    m_link_utilization = 100.0 * m_link_utilization_proxy
2539863Snilay@cs.wisc.edu        / (double(g_system_ptr->curCycle() - g_ruby_start));
2546145Snate@binkert.org}
2556145Snate@binkert.org
2567054Snate@binkert.orgvoid
2577054Snate@binkert.orgThrottle::print(ostream& out) const
2586145Snate@binkert.org{
2598054Sksewell@umich.edu    ccprintf(out,  "[%i bw: %i]", m_node, 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
2679275Snilay@cs.wisc.edu    int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
2687054Snate@binkert.org    size *=  MESSAGE_SIZE_MULTIPLIER;
2696145Snate@binkert.org
2707054Snate@binkert.org    // Artificially increase the size of broadcast messages
2717054Snate@binkert.org    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
2727054Snate@binkert.org        size *= BROADCAST_SCALING;
2737054Snate@binkert.org
2747054Snate@binkert.org    return size;
2756145Snate@binkert.org}
276