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
2911793Sbrandon.potter@amd.com#include "mem/ruby/network/simple/Throttle.hh"
3011793Sbrandon.potter@amd.com
317832Snate@binkert.org#include <cassert>
327832Snate@binkert.org
338645Snilay@cs.wisc.edu#include "base/cast.hh"
347054Snate@binkert.org#include "base/cprintf.hh"
358232Snate@binkert.org#include "debug/RubyNetwork.hh"
3610301Snilay@cs.wisc.edu#include "mem/ruby/network/MessageBuffer.hh"
376154Snate@binkert.org#include "mem/ruby/network/Network.hh"
3811793Sbrandon.potter@amd.com#include "mem/ruby/network/simple/Switch.hh"
3910895Snilay@cs.wisc.edu#include "mem/ruby/slicc_interface/Message.hh"
4011108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
416145Snate@binkert.org
427055Snate@binkert.orgusing namespace std;
437055Snate@binkert.org
446145Snate@binkert.orgconst int MESSAGE_SIZE_MULTIPLIER = 1000;
456145Snate@binkert.org//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
466145Snate@binkert.orgconst int BROADCAST_SCALING = 1;
476145Snate@binkert.orgconst int PRIORITY_SWITCH_LIMIT = 128;
486145Snate@binkert.org
4910895Snilay@cs.wisc.edustatic int network_message_to_size(Message* net_msg_ptr);
506145Snate@binkert.org
5110918Sbrandon.potter@amd.comThrottle::Throttle(int sID, RubySystem *rs, NodeID node, Cycles link_latency,
529230Snilay@cs.wisc.edu                   int link_bandwidth_multiplier, int endpoint_bandwidth,
5311092Snilay@cs.wisc.edu                   Switch *em)
5411092Snilay@cs.wisc.edu    : Consumer(em), m_switch_id(sID), m_switch(em), m_node(node),
5511092Snilay@cs.wisc.edu      m_ruby_system(rs)
566145Snate@binkert.org{
5710370Snilay@cs.wisc.edu    m_vnets = 0;
5810370Snilay@cs.wisc.edu
597832Snate@binkert.org    assert(link_bandwidth_multiplier > 0);
607054Snate@binkert.org    m_link_bandwidth_multiplier = link_bandwidth_multiplier;
6110311Snilay@cs.wisc.edu
627054Snate@binkert.org    m_link_latency = link_latency;
638259SBrad.Beckmann@amd.com    m_endpoint_bandwidth = endpoint_bandwidth;
646145Snate@binkert.org
657054Snate@binkert.org    m_wakeups_wo_switch = 0;
669863Snilay@cs.wisc.edu    m_link_utilization_proxy = 0;
676145Snate@binkert.org}
686145Snate@binkert.org
697054Snate@binkert.orgvoid
7010370Snilay@cs.wisc.eduThrottle::addLinks(const vector<MessageBuffer*>& in_vec,
7110370Snilay@cs.wisc.edu                   const vector<MessageBuffer*>& out_vec)
726145Snate@binkert.org{
737054Snate@binkert.org    assert(in_vec.size() == out_vec.size());
7410311Snilay@cs.wisc.edu
7510370Snilay@cs.wisc.edu    for (int vnet = 0; vnet < in_vec.size(); ++vnet) {
7610370Snilay@cs.wisc.edu        MessageBuffer *in_ptr = in_vec[vnet];
7710370Snilay@cs.wisc.edu        MessageBuffer *out_ptr = out_vec[vnet];
7810311Snilay@cs.wisc.edu
7910370Snilay@cs.wisc.edu        m_vnets++;
8010370Snilay@cs.wisc.edu        m_units_remaining.push_back(0);
8110370Snilay@cs.wisc.edu        m_in.push_back(in_ptr);
8210370Snilay@cs.wisc.edu        m_out.push_back(out_ptr);
8310311Snilay@cs.wisc.edu
8410311Snilay@cs.wisc.edu        // Set consumer and description
8510311Snilay@cs.wisc.edu        in_ptr->setConsumer(this);
8611092Snilay@cs.wisc.edu        string desc = "[Queue to Throttle " + to_string(m_switch_id) + " " +
8710311Snilay@cs.wisc.edu            to_string(m_node) + "]";
887054Snate@binkert.org    }
896145Snate@binkert.org}
906145Snate@binkert.org
917054Snate@binkert.orgvoid
9210311Snilay@cs.wisc.eduThrottle::operateVnet(int vnet, int &bw_remaining, bool &schedule_wakeup,
9310311Snilay@cs.wisc.edu                      MessageBuffer *in, MessageBuffer *out)
946145Snate@binkert.org{
9510370Snilay@cs.wisc.edu    if (out == nullptr || in == nullptr) {
9610370Snilay@cs.wisc.edu        return;
9710370Snilay@cs.wisc.edu    }
9811111Snilay@cs.wisc.edu
9910311Snilay@cs.wisc.edu    assert(m_units_remaining[vnet] >= 0);
10011111Snilay@cs.wisc.edu    Tick current_time = m_switch->clockEdge();
1016145Snate@binkert.org
10211111Snilay@cs.wisc.edu    while (bw_remaining > 0 && (in->isReady(current_time) ||
10311111Snilay@cs.wisc.edu                                m_units_remaining[vnet] > 0) &&
10411111Snilay@cs.wisc.edu           out->areNSlotsAvailable(1, current_time)) {
10510311Snilay@cs.wisc.edu        // See if we are done transferring the previous message on
10610311Snilay@cs.wisc.edu        // this virtual network
10711111Snilay@cs.wisc.edu        if (m_units_remaining[vnet] == 0 && in->isReady(current_time)) {
10810311Snilay@cs.wisc.edu            // Find the size of the message we are moving
10910311Snilay@cs.wisc.edu            MsgPtr msg_ptr = in->peekMsgPtr();
11010895Snilay@cs.wisc.edu            Message *net_msg_ptr = msg_ptr.get();
11110311Snilay@cs.wisc.edu            m_units_remaining[vnet] +=
11210311Snilay@cs.wisc.edu                network_message_to_size(net_msg_ptr);
11310311Snilay@cs.wisc.edu
11410311Snilay@cs.wisc.edu            DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
11510311Snilay@cs.wisc.edu                    "enqueueing net msg %d time: %lld.\n",
11610311Snilay@cs.wisc.edu                    m_node, getLinkBandwidth(), m_units_remaining[vnet],
11710919Sbrandon.potter@amd.com                    m_ruby_system->curCycle());
11810311Snilay@cs.wisc.edu
11910311Snilay@cs.wisc.edu            // Move the message
12011111Snilay@cs.wisc.edu            in->dequeue(current_time);
12111111Snilay@cs.wisc.edu            out->enqueue(msg_ptr, current_time,
12211111Snilay@cs.wisc.edu                         m_switch->cyclesToTicks(m_link_latency));
12310311Snilay@cs.wisc.edu
12410311Snilay@cs.wisc.edu            // Count the message
12510311Snilay@cs.wisc.edu            m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++;
12610311Snilay@cs.wisc.edu            DPRINTF(RubyNetwork, "%s\n", *out);
12710311Snilay@cs.wisc.edu        }
12810311Snilay@cs.wisc.edu
12910311Snilay@cs.wisc.edu        // Calculate the amount of bandwidth we spent on this message
13010311Snilay@cs.wisc.edu        int diff = m_units_remaining[vnet] - bw_remaining;
13110311Snilay@cs.wisc.edu        m_units_remaining[vnet] = max(0, diff);
13210311Snilay@cs.wisc.edu        bw_remaining = max(0, -diff);
13310311Snilay@cs.wisc.edu    }
13410311Snilay@cs.wisc.edu
13511111Snilay@cs.wisc.edu    if (bw_remaining > 0 && (in->isReady(current_time) ||
13611111Snilay@cs.wisc.edu                             m_units_remaining[vnet] > 0) &&
13711111Snilay@cs.wisc.edu        !out->areNSlotsAvailable(1, current_time)) {
13810311Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "vnet: %d", vnet);
13910311Snilay@cs.wisc.edu
14010311Snilay@cs.wisc.edu        // schedule me to wakeup again because I'm waiting for my
14110311Snilay@cs.wisc.edu        // output queue to become available
14210311Snilay@cs.wisc.edu        schedule_wakeup = true;
14310311Snilay@cs.wisc.edu    }
1446145Snate@binkert.org}
1456145Snate@binkert.org
1467054Snate@binkert.orgvoid
1477054Snate@binkert.orgThrottle::wakeup()
1486145Snate@binkert.org{
1497054Snate@binkert.org    // Limits the number of message sent to a limited number of bytes/cycle.
1507054Snate@binkert.org    assert(getLinkBandwidth() > 0);
1517054Snate@binkert.org    int bw_remaining = getLinkBandwidth();
1526145Snate@binkert.org
1537054Snate@binkert.org    m_wakeups_wo_switch++;
1547054Snate@binkert.org    bool schedule_wakeup = false;
1556145Snate@binkert.org
15610311Snilay@cs.wisc.edu    // variable for deciding the direction in which to iterate
15710311Snilay@cs.wisc.edu    bool iteration_direction = false;
15810311Snilay@cs.wisc.edu
15910311Snilay@cs.wisc.edu
1607054Snate@binkert.org    // invert priorities to avoid starvation seen in the component network
1617054Snate@binkert.org    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
1627054Snate@binkert.org        m_wakeups_wo_switch = 0;
16310311Snilay@cs.wisc.edu        iteration_direction = true;
1646145Snate@binkert.org    }
1656145Snate@binkert.org
16610311Snilay@cs.wisc.edu    if (iteration_direction) {
16710370Snilay@cs.wisc.edu        for (int vnet = 0; vnet < m_vnets; ++vnet) {
16810311Snilay@cs.wisc.edu            operateVnet(vnet, bw_remaining, schedule_wakeup,
16910370Snilay@cs.wisc.edu                        m_in[vnet], m_out[vnet]);
1707054Snate@binkert.org        }
17110311Snilay@cs.wisc.edu    } else {
17210370Snilay@cs.wisc.edu        for (int vnet = m_vnets-1; vnet >= 0; --vnet) {
17310311Snilay@cs.wisc.edu            operateVnet(vnet, bw_remaining, schedule_wakeup,
17410370Snilay@cs.wisc.edu                        m_in[vnet], m_out[vnet]);
1757054Snate@binkert.org        }
1766145Snate@binkert.org    }
1776145Snate@binkert.org
1787054Snate@binkert.org    // We should only wake up when we use the bandwidth
1797054Snate@binkert.org    // This is only mostly true
1807054Snate@binkert.org    // assert(bw_remaining != getLinkBandwidth());
1816145Snate@binkert.org
1827054Snate@binkert.org    // Record that we used some or all of the link bandwidth this cycle
1837054Snate@binkert.org    double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
1846145Snate@binkert.org
1857054Snate@binkert.org    // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
1869863Snilay@cs.wisc.edu    m_link_utilization_proxy += ratio;
1877054Snate@binkert.org
1887054Snate@binkert.org    if (bw_remaining > 0 && !schedule_wakeup) {
1897054Snate@binkert.org        // We have extra bandwidth and our output buffer was
1907054Snate@binkert.org        // available, so we must not have anything else to do until
1917054Snate@binkert.org        // another message arrives.
1927780Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
1937054Snate@binkert.org    } else {
1947780Snilay@cs.wisc.edu        DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
1957054Snate@binkert.org
1967054Snate@binkert.org        // We are out of bandwidth for this cycle, so wakeup next
1977054Snate@binkert.org        // cycle and continue
1989499Snilay@cs.wisc.edu        scheduleEvent(Cycles(1));
1997054Snate@binkert.org    }
2006145Snate@binkert.org}
2016145Snate@binkert.org
2027054Snate@binkert.orgvoid
2039863Snilay@cs.wisc.eduThrottle::regStats(string parent)
2046145Snate@binkert.org{
2059863Snilay@cs.wisc.edu    m_link_utilization
2069863Snilay@cs.wisc.edu        .name(parent + csprintf(".throttle%i", m_node) + ".link_utilization");
2079863Snilay@cs.wisc.edu
2089863Snilay@cs.wisc.edu    for (MessageSizeType type = MessageSizeType_FIRST;
2099863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
2109863Snilay@cs.wisc.edu        m_msg_counts[(unsigned int)type]
21110311Snilay@cs.wisc.edu            .init(Network::getNumberOfVirtualNetworks())
2129863Snilay@cs.wisc.edu            .name(parent + csprintf(".throttle%i", m_node) + ".msg_count." +
2139863Snilay@cs.wisc.edu                    MessageSizeType_to_string(type))
2149863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2159863Snilay@cs.wisc.edu            ;
2169863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type]
2179863Snilay@cs.wisc.edu            .name(parent + csprintf(".throttle%i", m_node) + ".msg_bytes." +
2189863Snilay@cs.wisc.edu                    MessageSizeType_to_string(type))
2199863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2209863Snilay@cs.wisc.edu            ;
2219863Snilay@cs.wisc.edu
2229863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type] = m_msg_counts[type] * Stats::constant(
2239863Snilay@cs.wisc.edu                Network::MessageSizeType_to_int(type));
2249863Snilay@cs.wisc.edu    }
2256145Snate@binkert.org}
2266145Snate@binkert.org
2277054Snate@binkert.orgvoid
2287054Snate@binkert.orgThrottle::clearStats()
2296145Snate@binkert.org{
2309863Snilay@cs.wisc.edu    m_link_utilization_proxy = 0;
2316145Snate@binkert.org}
2326145Snate@binkert.org
2339863Snilay@cs.wisc.eduvoid
2349863Snilay@cs.wisc.eduThrottle::collateStats()
2356145Snate@binkert.org{
23610918Sbrandon.potter@amd.com    double time_delta = double(m_ruby_system->curCycle() -
23710918Sbrandon.potter@amd.com                               m_ruby_system->getStartCycle());
23810918Sbrandon.potter@amd.com
23910918Sbrandon.potter@amd.com    m_link_utilization = 100.0 * m_link_utilization_proxy / time_delta;
2406145Snate@binkert.org}
2416145Snate@binkert.org
2427054Snate@binkert.orgvoid
2437054Snate@binkert.orgThrottle::print(ostream& out) const
2446145Snate@binkert.org{
2458054Sksewell@umich.edu    ccprintf(out,  "[%i bw: %i]", m_node, getLinkBandwidth());
2466145Snate@binkert.org}
2476145Snate@binkert.org
2487054Snate@binkert.orgint
24910895Snilay@cs.wisc.edunetwork_message_to_size(Message *net_msg_ptr)
2507054Snate@binkert.org{
2517054Snate@binkert.org    assert(net_msg_ptr != NULL);
2526145Snate@binkert.org
2539275Snilay@cs.wisc.edu    int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
2547054Snate@binkert.org    size *=  MESSAGE_SIZE_MULTIPLIER;
2556145Snate@binkert.org
2567054Snate@binkert.org    // Artificially increase the size of broadcast messages
2577054Snate@binkert.org    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
2587054Snate@binkert.org        size *= BROADCAST_SCALING;
2597054Snate@binkert.org
2607054Snate@binkert.org    return size;
2616145Snate@binkert.org}
262