SimpleNetwork.cc revision 8308
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>
307547SBrad.Beckmann@amd.com#include <numeric>
317547SBrad.Beckmann@amd.com
327454Snate@binkert.org#include "base/stl_helpers.hh"
337054Snate@binkert.org#include "mem/protocol/MachineType.hh"
347054Snate@binkert.org#include "mem/protocol/Protocol.hh"
357054Snate@binkert.org#include "mem/protocol/TopologyType.hh"
367054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
377054Snate@binkert.org#include "mem/ruby/common/NetDest.hh"
388257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh"
398258SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleLink.hh"
406154Snate@binkert.org#include "mem/ruby/network/simple/SimpleNetwork.hh"
417054Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
427547SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/Throttle.hh"
438255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh"
446154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
456154Snate@binkert.org#include "mem/ruby/system/System.hh"
466145Snate@binkert.org
477055Snate@binkert.orgusing namespace std;
487454Snate@binkert.orgusing m5::stl_helpers::deletePointers;
497055Snate@binkert.org
507054Snate@binkert.org#if 0
516284Snate@binkert.org// ***BIG HACK*** - This is actually code that _should_ be in Network.cc
526145Snate@binkert.org
536145Snate@binkert.org// Note: Moved to Princeton Network
546145Snate@binkert.org// calls new to abstract away from the network
557054Snate@binkert.orgNetwork*
567054Snate@binkert.orgNetwork::createNetwork(int nodes)
576145Snate@binkert.org{
587054Snate@binkert.org    return new SimpleNetwork(nodes);
596145Snate@binkert.org}
607054Snate@binkert.org#endif
616145Snate@binkert.org
626876Ssteve.reinhardt@amd.comSimpleNetwork::SimpleNetwork(const Params *p)
636876Ssteve.reinhardt@amd.com    : Network(p)
646285Snate@binkert.org{
658259SBrad.Beckmann@amd.com    m_buffer_size = p->buffer_size;
668259SBrad.Beckmann@amd.com    m_endpoint_bandwidth = p->endpoint_bandwidth;
678259SBrad.Beckmann@amd.com    m_adaptive_routing = p->adaptive_routing;
688259SBrad.Beckmann@amd.com
697054Snate@binkert.org    // Note: the parent Network Object constructor is called before the
707054Snate@binkert.org    // SimpleNetwork child constructor.  Therefore, the member variables
717054Snate@binkert.org    // used below should already be initialized.
726285Snate@binkert.org
737454Snate@binkert.org    m_endpoint_switches.resize(m_nodes);
746285Snate@binkert.org
757454Snate@binkert.org    m_in_use.resize(m_virtual_networks);
767454Snate@binkert.org    m_ordered.resize(m_virtual_networks);
777054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
787054Snate@binkert.org        m_in_use[i] = false;
797054Snate@binkert.org        m_ordered[i] = false;
806285Snate@binkert.org    }
817054Snate@binkert.org
827054Snate@binkert.org    // Allocate to and from queues
837454Snate@binkert.org    m_toNetQueues.resize(m_nodes);
847454Snate@binkert.org    m_fromNetQueues.resize(m_nodes);
857054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
867454Snate@binkert.org        m_toNetQueues[node].resize(m_virtual_networks);
877454Snate@binkert.org        m_fromNetQueues[node].resize(m_virtual_networks);
887054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
897056Snate@binkert.org            m_toNetQueues[node][j] =
907056Snate@binkert.org                new MessageBuffer(csprintf("toNet node %d j %d", node, j));
917056Snate@binkert.org            m_fromNetQueues[node][j] =
927056Snate@binkert.org                new MessageBuffer(csprintf("fromNet node %d j %d", node, j));
937054Snate@binkert.org        }
947054Snate@binkert.org    }
956881SBrad.Beckmann@amd.com}
966285Snate@binkert.org
977054Snate@binkert.orgvoid
987054Snate@binkert.orgSimpleNetwork::init()
996881SBrad.Beckmann@amd.com{
1007054Snate@binkert.org    Network::init();
1016881SBrad.Beckmann@amd.com
1027054Snate@binkert.org    // The topology pointer should have already been initialized in
1037054Snate@binkert.org    // the parent class network constructor.
1047054Snate@binkert.org    assert(m_topology_ptr != NULL);
1057054Snate@binkert.org    int number_of_switches = m_topology_ptr->numSwitches();
1067054Snate@binkert.org    for (int i = 0; i < number_of_switches; i++) {
1077454Snate@binkert.org        m_switch_ptr_vector.push_back(new Switch(i, this));
1087054Snate@binkert.org    }
1096881SBrad.Beckmann@amd.com
1107054Snate@binkert.org    // false because this isn't a reconfiguration
1117054Snate@binkert.org    m_topology_ptr->createLinks(this, false);
1126285Snate@binkert.org}
1136145Snate@binkert.org
1147054Snate@binkert.orgvoid
1157054Snate@binkert.orgSimpleNetwork::reset()
1166145Snate@binkert.org{
1177054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
1187054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
1197054Snate@binkert.org            m_toNetQueues[node][j]->clear();
1207054Snate@binkert.org            m_fromNetQueues[node][j]->clear();
1217054Snate@binkert.org        }
1226145Snate@binkert.org    }
1236145Snate@binkert.org
1247054Snate@binkert.org    for(int i = 0; i < m_switch_ptr_vector.size(); i++){
1257054Snate@binkert.org        m_switch_ptr_vector[i]->clearBuffers();
1267054Snate@binkert.org    }
1276145Snate@binkert.org}
1286145Snate@binkert.org
1296145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
1306145Snate@binkert.org{
1317054Snate@binkert.org    for (int i = 0; i < m_nodes; i++) {
1327454Snate@binkert.org        deletePointers(m_toNetQueues[i]);
1337454Snate@binkert.org        deletePointers(m_fromNetQueues[i]);
1347054Snate@binkert.org    }
1357454Snate@binkert.org    deletePointers(m_switch_ptr_vector);
1367454Snate@binkert.org    deletePointers(m_buffers_to_free);
1377054Snate@binkert.org    // delete m_topology_ptr;
1386145Snate@binkert.org}
1396145Snate@binkert.org
1406145Snate@binkert.org// From a switch to an endpoint node
1417054Snate@binkert.orgvoid
1428257SBrad.Beckmann@amd.comSimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
1438257SBrad.Beckmann@amd.com                           LinkDirection direction,
1448257SBrad.Beckmann@amd.com                           const NetDest& routing_table_entry,
1458257SBrad.Beckmann@amd.com                           bool isReconfiguration)
1466145Snate@binkert.org{
1477054Snate@binkert.org    assert(dest < m_nodes);
1487054Snate@binkert.org    assert(src < m_switch_ptr_vector.size());
1497054Snate@binkert.org    assert(m_switch_ptr_vector[src] != NULL);
1507054Snate@binkert.org
1517054Snate@binkert.org    if (isReconfiguration) {
1527054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1537054Snate@binkert.org        return;
1547054Snate@binkert.org    }
1557054Snate@binkert.org
1568258SBrad.Beckmann@amd.com    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
1578258SBrad.Beckmann@amd.com
1587054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(m_fromNetQueues[dest],
1598257SBrad.Beckmann@amd.com                                         routing_table_entry,
1608258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1618258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1628257SBrad.Beckmann@amd.com
1636145Snate@binkert.org    m_endpoint_switches[dest] = m_switch_ptr_vector[src];
1646145Snate@binkert.org}
1656145Snate@binkert.org
1666145Snate@binkert.org// From an endpoint node to a switch
1677054Snate@binkert.orgvoid
1688257SBrad.Beckmann@amd.comSimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
1698257SBrad.Beckmann@amd.com                          LinkDirection direction,
1708257SBrad.Beckmann@amd.com                          const NetDest& routing_table_entry,
1718257SBrad.Beckmann@amd.com                          bool isReconfiguration)
1726145Snate@binkert.org{
1737054Snate@binkert.org    assert(src < m_nodes);
1747054Snate@binkert.org    if (isReconfiguration) {
1757054Snate@binkert.org        // do nothing
1767054Snate@binkert.org        return;
1777054Snate@binkert.org    }
1787054Snate@binkert.org
1796145Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(m_toNetQueues[src]);
1806145Snate@binkert.org}
1816145Snate@binkert.org
1826145Snate@binkert.org// From a switch to a switch
1837054Snate@binkert.orgvoid
1848257SBrad.Beckmann@amd.comSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
1858257SBrad.Beckmann@amd.com                                LinkDirection direction,
1868257SBrad.Beckmann@amd.com                                const NetDest& routing_table_entry,
1878257SBrad.Beckmann@amd.com                                bool isReconfiguration)
1886145Snate@binkert.org{
1897054Snate@binkert.org    if (isReconfiguration) {
1907054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1917054Snate@binkert.org        return;
1927054Snate@binkert.org    }
1937054Snate@binkert.org
1946145Snate@binkert.org    // Create a set of new MessageBuffers
1957454Snate@binkert.org    std::vector<MessageBuffer*> queues;
1966145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1977054Snate@binkert.org        // allocate a buffer
1987054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1997054Snate@binkert.org        buffer_ptr->setOrdering(true);
2007054Snate@binkert.org        if (m_buffer_size > 0) {
2017454Snate@binkert.org            buffer_ptr->resize(m_buffer_size);
2027054Snate@binkert.org        }
2037454Snate@binkert.org        queues.push_back(buffer_ptr);
2047054Snate@binkert.org        // remember to deallocate it
2057454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
2067054Snate@binkert.org    }
2077054Snate@binkert.org    // Connect it to the two switches
2088258SBrad.Beckmann@amd.com    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
2098258SBrad.Beckmann@amd.com
2107054Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(queues);
2117054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(queues, routing_table_entry,
2128258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
2138258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
2147054Snate@binkert.org}
2157054Snate@binkert.org
2167054Snate@binkert.orgvoid
2177054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
2187054Snate@binkert.org{
2197832Snate@binkert.org    assert(id < m_nodes);
2207832Snate@binkert.org    assert(network_num < m_virtual_networks);
2217054Snate@binkert.org
2227054Snate@binkert.org    if (ordered) {
2237054Snate@binkert.org        m_ordered[network_num] = true;
2247054Snate@binkert.org    }
2257054Snate@binkert.org    m_in_use[network_num] = true;
2267054Snate@binkert.org}
2277054Snate@binkert.org
2287054Snate@binkert.orgMessageBuffer*
2298308Stushar@csail.mit.eduSimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num,
2308308Stushar@csail.mit.edu                             std::string vnet_type)
2317054Snate@binkert.org{
2327054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2337054Snate@binkert.org    return m_toNetQueues[id][network_num];
2347054Snate@binkert.org}
2357054Snate@binkert.org
2367054Snate@binkert.orgMessageBuffer*
2378308Stushar@csail.mit.eduSimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num,
2388308Stushar@csail.mit.edu                               std::string vnet_type)
2397054Snate@binkert.org{
2407054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2417054Snate@binkert.org    return m_fromNetQueues[id][network_num];
2427054Snate@binkert.org}
2437054Snate@binkert.org
2447454Snate@binkert.orgconst std::vector<Throttle*>*
2457054Snate@binkert.orgSimpleNetwork::getThrottles(NodeID id) const
2467054Snate@binkert.org{
2477054Snate@binkert.org    assert(id >= 0);
2487054Snate@binkert.org    assert(id < m_nodes);
2497054Snate@binkert.org    assert(m_endpoint_switches[id] != NULL);
2507054Snate@binkert.org    return m_endpoint_switches[id]->getThrottles();
2517054Snate@binkert.org}
2527054Snate@binkert.org
2537054Snate@binkert.orgvoid
2547054Snate@binkert.orgSimpleNetwork::printStats(ostream& out) const
2557054Snate@binkert.org{
2567054Snate@binkert.org    out << endl;
2577054Snate@binkert.org    out << "Network Stats" << endl;
2587054Snate@binkert.org    out << "-------------" << endl;
2597054Snate@binkert.org    out << endl;
2607547SBrad.Beckmann@amd.com
2617547SBrad.Beckmann@amd.com    //
2627547SBrad.Beckmann@amd.com    // Determine total counts before printing out each switch's stats
2637547SBrad.Beckmann@amd.com    //
2647547SBrad.Beckmann@amd.com    std::vector<uint64> total_msg_counts;
2657547SBrad.Beckmann@amd.com    total_msg_counts.resize(MessageSizeType_NUM);
2667547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2677547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2687547SBrad.Beckmann@amd.com         ++type) {
2697547SBrad.Beckmann@amd.com        total_msg_counts[type] = 0;
2707547SBrad.Beckmann@amd.com    }
2717547SBrad.Beckmann@amd.com
2727547SBrad.Beckmann@amd.com    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
2737547SBrad.Beckmann@amd.com        const std::vector<Throttle*>* throttles =
2747547SBrad.Beckmann@amd.com            m_switch_ptr_vector[i]->getThrottles();
2757547SBrad.Beckmann@amd.com
2767547SBrad.Beckmann@amd.com        for (int p = 0; p < throttles->size(); p++) {
2777547SBrad.Beckmann@amd.com
2787547SBrad.Beckmann@amd.com            const std::vector<std::vector<int> >& message_counts =
2797547SBrad.Beckmann@amd.com                ((*throttles)[p])->getCounters();
2807547SBrad.Beckmann@amd.com
2817547SBrad.Beckmann@amd.com            for (MessageSizeType type = MessageSizeType_FIRST;
2827547SBrad.Beckmann@amd.com                 type < MessageSizeType_NUM;
2837547SBrad.Beckmann@amd.com                 ++type) {
2847547SBrad.Beckmann@amd.com
2857547SBrad.Beckmann@amd.com                const std::vector<int> &mct = message_counts[type];
2867547SBrad.Beckmann@amd.com                int sum = accumulate(mct.begin(), mct.end(), 0);
2877547SBrad.Beckmann@amd.com                total_msg_counts[type] += uint64(sum);
2887547SBrad.Beckmann@amd.com            }
2897547SBrad.Beckmann@amd.com        }
2907547SBrad.Beckmann@amd.com    }
2917547SBrad.Beckmann@amd.com    uint64 total_msgs = 0;
2927547SBrad.Beckmann@amd.com    uint64 total_bytes = 0;
2937547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2947547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2957547SBrad.Beckmann@amd.com         ++type) {
2967547SBrad.Beckmann@amd.com
2977547SBrad.Beckmann@amd.com        if (total_msg_counts[type] > 0) {
2987547SBrad.Beckmann@amd.com            out << "total_msg_count_" << type << ": " << total_msg_counts[type]
2997547SBrad.Beckmann@amd.com                << " " << total_msg_counts[type] *
3007547SBrad.Beckmann@amd.com                uint64(RubySystem::getNetwork()->MessageSizeType_to_int(type))
3017547SBrad.Beckmann@amd.com                << endl;
3027547SBrad.Beckmann@amd.com
3037547SBrad.Beckmann@amd.com            total_msgs += total_msg_counts[type];
3047547SBrad.Beckmann@amd.com
3057547SBrad.Beckmann@amd.com            total_bytes += total_msg_counts[type] *
3067547SBrad.Beckmann@amd.com                uint64(RubySystem::getNetwork()->MessageSizeType_to_int(type));
3077547SBrad.Beckmann@amd.com
3087547SBrad.Beckmann@amd.com        }
3097547SBrad.Beckmann@amd.com    }
3107547SBrad.Beckmann@amd.com
3117547SBrad.Beckmann@amd.com    out << "total_msgs: " << total_msgs
3127547SBrad.Beckmann@amd.com        << " total_bytes: " << total_bytes << endl;
3137547SBrad.Beckmann@amd.com
3147547SBrad.Beckmann@amd.com    out << endl;
3157054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
3167054Snate@binkert.org        m_switch_ptr_vector[i]->printStats(out);
3177054Snate@binkert.org    }
3187054Snate@binkert.org    m_topology_ptr->printStats(out);
3197054Snate@binkert.org}
3207054Snate@binkert.org
3217054Snate@binkert.orgvoid
3227054Snate@binkert.orgSimpleNetwork::clearStats()
3237054Snate@binkert.org{
3247054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
3257054Snate@binkert.org        m_switch_ptr_vector[i]->clearStats();
3267054Snate@binkert.org    }
3277054Snate@binkert.org    m_topology_ptr->clearStats();
3287054Snate@binkert.org}
3297054Snate@binkert.org
3307054Snate@binkert.orgvoid
3317054Snate@binkert.orgSimpleNetwork::printConfig(ostream& out) const
3327054Snate@binkert.org{
3337054Snate@binkert.org    out << endl;
3347054Snate@binkert.org    out << "Network Configuration" << endl;
3357054Snate@binkert.org    out << "---------------------" << endl;
3367054Snate@binkert.org    out << "network: SIMPLE_NETWORK" << endl;
3377054Snate@binkert.org    out << "topology: " << m_topology_ptr->getName() << endl;
3387054Snate@binkert.org    out << endl;
3397054Snate@binkert.org
3407054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
3417054Snate@binkert.org        out << "virtual_net_" << i << ": ";
3427054Snate@binkert.org        if (m_in_use[i]) {
3437054Snate@binkert.org            out << "active, ";
3447054Snate@binkert.org            if (m_ordered[i]) {
3457054Snate@binkert.org                out << "ordered" << endl;
3467054Snate@binkert.org            } else {
3477054Snate@binkert.org                out << "unordered" << endl;
3487054Snate@binkert.org            }
3497054Snate@binkert.org        } else {
3507054Snate@binkert.org            out << "inactive" << endl;
3517054Snate@binkert.org        }
3527054Snate@binkert.org    }
3537054Snate@binkert.org    out << endl;
3547054Snate@binkert.org
3557054Snate@binkert.org    for(int i = 0; i < m_switch_ptr_vector.size(); i++) {
3567054Snate@binkert.org        m_switch_ptr_vector[i]->printConfig(out);
3576145Snate@binkert.org    }
3586145Snate@binkert.org
3597054Snate@binkert.org    m_topology_ptr->printConfig(out);
3606145Snate@binkert.org}
3616145Snate@binkert.org
3627054Snate@binkert.orgvoid
3637054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
3646145Snate@binkert.org{
3657054Snate@binkert.org    out << "[SimpleNetwork]";
3666145Snate@binkert.org}
3676876Ssteve.reinhardt@amd.com
3686876Ssteve.reinhardt@amd.com
3696876Ssteve.reinhardt@amd.comSimpleNetwork *
3706876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
3716876Ssteve.reinhardt@amd.com{
3726876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
3736876Ssteve.reinhardt@amd.com}
374