SimpleNetwork.cc revision 9274
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
328645Snilay@cs.wisc.edu#include "base/cast.hh"
337454Snate@binkert.org#include "base/stl_helpers.hh"
347054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
357054Snate@binkert.org#include "mem/ruby/common/NetDest.hh"
368257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh"
378258SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleLink.hh"
386154Snate@binkert.org#include "mem/ruby/network/simple/SimpleNetwork.hh"
397054Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
407547SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/Throttle.hh"
418255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh"
426154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
436154Snate@binkert.org#include "mem/ruby/system/System.hh"
446145Snate@binkert.org
457055Snate@binkert.orgusing namespace std;
467454Snate@binkert.orgusing m5::stl_helpers::deletePointers;
477055Snate@binkert.org
486876Ssteve.reinhardt@amd.comSimpleNetwork::SimpleNetwork(const Params *p)
496876Ssteve.reinhardt@amd.com    : Network(p)
506285Snate@binkert.org{
518259SBrad.Beckmann@amd.com    m_buffer_size = p->buffer_size;
528259SBrad.Beckmann@amd.com    m_endpoint_bandwidth = p->endpoint_bandwidth;
538259SBrad.Beckmann@amd.com    m_adaptive_routing = p->adaptive_routing;
548259SBrad.Beckmann@amd.com
557054Snate@binkert.org    // Note: the parent Network Object constructor is called before the
567054Snate@binkert.org    // SimpleNetwork child constructor.  Therefore, the member variables
577054Snate@binkert.org    // used below should already be initialized.
586285Snate@binkert.org
597454Snate@binkert.org    m_endpoint_switches.resize(m_nodes);
606285Snate@binkert.org
617454Snate@binkert.org    m_in_use.resize(m_virtual_networks);
627454Snate@binkert.org    m_ordered.resize(m_virtual_networks);
637054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
647054Snate@binkert.org        m_in_use[i] = false;
657054Snate@binkert.org        m_ordered[i] = false;
666285Snate@binkert.org    }
677054Snate@binkert.org
687054Snate@binkert.org    // Allocate to and from queues
697454Snate@binkert.org    m_toNetQueues.resize(m_nodes);
707454Snate@binkert.org    m_fromNetQueues.resize(m_nodes);
717054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
727454Snate@binkert.org        m_toNetQueues[node].resize(m_virtual_networks);
737454Snate@binkert.org        m_fromNetQueues[node].resize(m_virtual_networks);
747054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
757056Snate@binkert.org            m_toNetQueues[node][j] =
767056Snate@binkert.org                new MessageBuffer(csprintf("toNet node %d j %d", node, j));
777056Snate@binkert.org            m_fromNetQueues[node][j] =
787056Snate@binkert.org                new MessageBuffer(csprintf("fromNet node %d j %d", node, j));
797054Snate@binkert.org        }
807054Snate@binkert.org    }
819274Snilay@cs.wisc.edu
829274Snilay@cs.wisc.edu    // record the routers
839274Snilay@cs.wisc.edu    for (vector<BasicRouter*>::const_iterator i =
849274Snilay@cs.wisc.edu             m_topology_ptr->params()->routers.begin();
859274Snilay@cs.wisc.edu         i != m_topology_ptr->params()->routers.end(); ++i) {
869274Snilay@cs.wisc.edu        Switch* s = safe_cast<Switch*>(*i);
879274Snilay@cs.wisc.edu        m_switch_ptr_vector.push_back(s);
889274Snilay@cs.wisc.edu        s->init_net_ptr(this);
899274Snilay@cs.wisc.edu    }
906881SBrad.Beckmann@amd.com}
916285Snate@binkert.org
927054Snate@binkert.orgvoid
937054Snate@binkert.orgSimpleNetwork::init()
946881SBrad.Beckmann@amd.com{
957054Snate@binkert.org    Network::init();
966881SBrad.Beckmann@amd.com
977054Snate@binkert.org    // The topology pointer should have already been initialized in
987054Snate@binkert.org    // the parent class network constructor.
997054Snate@binkert.org    assert(m_topology_ptr != NULL);
1007054Snate@binkert.org    // false because this isn't a reconfiguration
1017054Snate@binkert.org    m_topology_ptr->createLinks(this, false);
1026285Snate@binkert.org}
1036145Snate@binkert.org
1047054Snate@binkert.orgvoid
1057054Snate@binkert.orgSimpleNetwork::reset()
1066145Snate@binkert.org{
1077054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
1087054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
1097054Snate@binkert.org            m_toNetQueues[node][j]->clear();
1107054Snate@binkert.org            m_fromNetQueues[node][j]->clear();
1117054Snate@binkert.org        }
1126145Snate@binkert.org    }
1136145Snate@binkert.org
1147054Snate@binkert.org    for(int i = 0; i < m_switch_ptr_vector.size(); i++){
1157054Snate@binkert.org        m_switch_ptr_vector[i]->clearBuffers();
1167054Snate@binkert.org    }
1176145Snate@binkert.org}
1186145Snate@binkert.org
1196145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
1206145Snate@binkert.org{
1217054Snate@binkert.org    for (int i = 0; i < m_nodes; i++) {
1227454Snate@binkert.org        deletePointers(m_toNetQueues[i]);
1237454Snate@binkert.org        deletePointers(m_fromNetQueues[i]);
1247054Snate@binkert.org    }
1257454Snate@binkert.org    deletePointers(m_switch_ptr_vector);
1267454Snate@binkert.org    deletePointers(m_buffers_to_free);
1277054Snate@binkert.org    // delete m_topology_ptr;
1286145Snate@binkert.org}
1296145Snate@binkert.org
1306145Snate@binkert.org// From a switch to an endpoint node
1317054Snate@binkert.orgvoid
1328257SBrad.Beckmann@amd.comSimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
1338257SBrad.Beckmann@amd.com                           LinkDirection direction,
1348257SBrad.Beckmann@amd.com                           const NetDest& routing_table_entry,
1358257SBrad.Beckmann@amd.com                           bool isReconfiguration)
1366145Snate@binkert.org{
1377054Snate@binkert.org    assert(dest < m_nodes);
1387054Snate@binkert.org    assert(src < m_switch_ptr_vector.size());
1397054Snate@binkert.org    assert(m_switch_ptr_vector[src] != NULL);
1407054Snate@binkert.org
1417054Snate@binkert.org    if (isReconfiguration) {
1427054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1437054Snate@binkert.org        return;
1447054Snate@binkert.org    }
1457054Snate@binkert.org
1468258SBrad.Beckmann@amd.com    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
1478258SBrad.Beckmann@amd.com
1487054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(m_fromNetQueues[dest],
1498257SBrad.Beckmann@amd.com                                         routing_table_entry,
1508258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1518258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1528257SBrad.Beckmann@amd.com
1536145Snate@binkert.org    m_endpoint_switches[dest] = m_switch_ptr_vector[src];
1546145Snate@binkert.org}
1556145Snate@binkert.org
1566145Snate@binkert.org// From an endpoint node to a switch
1577054Snate@binkert.orgvoid
1588257SBrad.Beckmann@amd.comSimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
1598257SBrad.Beckmann@amd.com                          LinkDirection direction,
1608257SBrad.Beckmann@amd.com                          const NetDest& routing_table_entry,
1618257SBrad.Beckmann@amd.com                          bool isReconfiguration)
1626145Snate@binkert.org{
1637054Snate@binkert.org    assert(src < m_nodes);
1647054Snate@binkert.org    if (isReconfiguration) {
1657054Snate@binkert.org        // do nothing
1667054Snate@binkert.org        return;
1677054Snate@binkert.org    }
1687054Snate@binkert.org
1696145Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(m_toNetQueues[src]);
1706145Snate@binkert.org}
1716145Snate@binkert.org
1726145Snate@binkert.org// From a switch to a switch
1737054Snate@binkert.orgvoid
1748257SBrad.Beckmann@amd.comSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
1758257SBrad.Beckmann@amd.com                                LinkDirection direction,
1768257SBrad.Beckmann@amd.com                                const NetDest& routing_table_entry,
1778257SBrad.Beckmann@amd.com                                bool isReconfiguration)
1786145Snate@binkert.org{
1797054Snate@binkert.org    if (isReconfiguration) {
1807054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1817054Snate@binkert.org        return;
1827054Snate@binkert.org    }
1837054Snate@binkert.org
1846145Snate@binkert.org    // Create a set of new MessageBuffers
1857454Snate@binkert.org    std::vector<MessageBuffer*> queues;
1866145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1877054Snate@binkert.org        // allocate a buffer
1887054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1897054Snate@binkert.org        buffer_ptr->setOrdering(true);
1907054Snate@binkert.org        if (m_buffer_size > 0) {
1917454Snate@binkert.org            buffer_ptr->resize(m_buffer_size);
1927054Snate@binkert.org        }
1937454Snate@binkert.org        queues.push_back(buffer_ptr);
1947054Snate@binkert.org        // remember to deallocate it
1957454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
1967054Snate@binkert.org    }
1977054Snate@binkert.org    // Connect it to the two switches
1988258SBrad.Beckmann@amd.com    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
1998258SBrad.Beckmann@amd.com
2007054Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(queues);
2017054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(queues, routing_table_entry,
2028258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
2038258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
2047054Snate@binkert.org}
2057054Snate@binkert.org
2067054Snate@binkert.orgvoid
2077054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
2087054Snate@binkert.org{
2097832Snate@binkert.org    assert(id < m_nodes);
2107832Snate@binkert.org    assert(network_num < m_virtual_networks);
2117054Snate@binkert.org
2127054Snate@binkert.org    if (ordered) {
2137054Snate@binkert.org        m_ordered[network_num] = true;
2147054Snate@binkert.org    }
2157054Snate@binkert.org    m_in_use[network_num] = true;
2167054Snate@binkert.org}
2177054Snate@binkert.org
2187054Snate@binkert.orgMessageBuffer*
2198308Stushar@csail.mit.eduSimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num,
2208308Stushar@csail.mit.edu                             std::string vnet_type)
2217054Snate@binkert.org{
2227054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2237054Snate@binkert.org    return m_toNetQueues[id][network_num];
2247054Snate@binkert.org}
2257054Snate@binkert.org
2267054Snate@binkert.orgMessageBuffer*
2278308Stushar@csail.mit.eduSimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num,
2288308Stushar@csail.mit.edu                               std::string vnet_type)
2297054Snate@binkert.org{
2307054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2317054Snate@binkert.org    return m_fromNetQueues[id][network_num];
2327054Snate@binkert.org}
2337054Snate@binkert.org
2347454Snate@binkert.orgconst std::vector<Throttle*>*
2357054Snate@binkert.orgSimpleNetwork::getThrottles(NodeID id) const
2367054Snate@binkert.org{
2377054Snate@binkert.org    assert(id >= 0);
2387054Snate@binkert.org    assert(id < m_nodes);
2397054Snate@binkert.org    assert(m_endpoint_switches[id] != NULL);
2407054Snate@binkert.org    return m_endpoint_switches[id]->getThrottles();
2417054Snate@binkert.org}
2427054Snate@binkert.org
2437054Snate@binkert.orgvoid
2447054Snate@binkert.orgSimpleNetwork::printStats(ostream& out) const
2457054Snate@binkert.org{
2467054Snate@binkert.org    out << endl;
2477054Snate@binkert.org    out << "Network Stats" << endl;
2487054Snate@binkert.org    out << "-------------" << endl;
2497054Snate@binkert.org    out << endl;
2507547SBrad.Beckmann@amd.com
2517547SBrad.Beckmann@amd.com    //
2527547SBrad.Beckmann@amd.com    // Determine total counts before printing out each switch's stats
2537547SBrad.Beckmann@amd.com    //
2547547SBrad.Beckmann@amd.com    std::vector<uint64> total_msg_counts;
2557547SBrad.Beckmann@amd.com    total_msg_counts.resize(MessageSizeType_NUM);
2567547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2577547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2587547SBrad.Beckmann@amd.com         ++type) {
2597547SBrad.Beckmann@amd.com        total_msg_counts[type] = 0;
2607547SBrad.Beckmann@amd.com    }
2617547SBrad.Beckmann@amd.com
2627547SBrad.Beckmann@amd.com    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
2637547SBrad.Beckmann@amd.com        const std::vector<Throttle*>* throttles =
2647547SBrad.Beckmann@amd.com            m_switch_ptr_vector[i]->getThrottles();
2657547SBrad.Beckmann@amd.com
2667547SBrad.Beckmann@amd.com        for (int p = 0; p < throttles->size(); p++) {
2677547SBrad.Beckmann@amd.com
2687547SBrad.Beckmann@amd.com            const std::vector<std::vector<int> >& message_counts =
2697547SBrad.Beckmann@amd.com                ((*throttles)[p])->getCounters();
2707547SBrad.Beckmann@amd.com
2717547SBrad.Beckmann@amd.com            for (MessageSizeType type = MessageSizeType_FIRST;
2727547SBrad.Beckmann@amd.com                 type < MessageSizeType_NUM;
2737547SBrad.Beckmann@amd.com                 ++type) {
2747547SBrad.Beckmann@amd.com
2757547SBrad.Beckmann@amd.com                const std::vector<int> &mct = message_counts[type];
2767547SBrad.Beckmann@amd.com                int sum = accumulate(mct.begin(), mct.end(), 0);
2777547SBrad.Beckmann@amd.com                total_msg_counts[type] += uint64(sum);
2787547SBrad.Beckmann@amd.com            }
2797547SBrad.Beckmann@amd.com        }
2807547SBrad.Beckmann@amd.com    }
2817547SBrad.Beckmann@amd.com    uint64 total_msgs = 0;
2827547SBrad.Beckmann@amd.com    uint64 total_bytes = 0;
2837547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2847547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2857547SBrad.Beckmann@amd.com         ++type) {
2867547SBrad.Beckmann@amd.com
2877547SBrad.Beckmann@amd.com        if (total_msg_counts[type] > 0) {
2887547SBrad.Beckmann@amd.com            out << "total_msg_count_" << type << ": " << total_msg_counts[type]
2899274Snilay@cs.wisc.edu                << " " << total_msg_counts[type] *
2909274Snilay@cs.wisc.edu                uint64(MessageSizeType_to_int(type))
2917547SBrad.Beckmann@amd.com                << endl;
2927547SBrad.Beckmann@amd.com
2937547SBrad.Beckmann@amd.com            total_msgs += total_msg_counts[type];
2947547SBrad.Beckmann@amd.com
2957547SBrad.Beckmann@amd.com            total_bytes += total_msg_counts[type] *
2969274Snilay@cs.wisc.edu                uint64(MessageSizeType_to_int(type));
2977547SBrad.Beckmann@amd.com        }
2987547SBrad.Beckmann@amd.com    }
2997547SBrad.Beckmann@amd.com
3007547SBrad.Beckmann@amd.com    out << "total_msgs: " << total_msgs
3017547SBrad.Beckmann@amd.com        << " total_bytes: " << total_bytes << endl;
3027547SBrad.Beckmann@amd.com
3037547SBrad.Beckmann@amd.com    out << endl;
3047054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
3057054Snate@binkert.org        m_switch_ptr_vector[i]->printStats(out);
3067054Snate@binkert.org    }
3077054Snate@binkert.org    m_topology_ptr->printStats(out);
3087054Snate@binkert.org}
3097054Snate@binkert.org
3107054Snate@binkert.orgvoid
3117054Snate@binkert.orgSimpleNetwork::clearStats()
3127054Snate@binkert.org{
3137054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
3147054Snate@binkert.org        m_switch_ptr_vector[i]->clearStats();
3157054Snate@binkert.org    }
3167054Snate@binkert.org    m_topology_ptr->clearStats();
3177054Snate@binkert.org}
3187054Snate@binkert.org
3197054Snate@binkert.orgvoid
3207054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
3216145Snate@binkert.org{
3227054Snate@binkert.org    out << "[SimpleNetwork]";
3236145Snate@binkert.org}
3246876Ssteve.reinhardt@amd.com
3256876Ssteve.reinhardt@amd.comSimpleNetwork *
3266876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
3276876Ssteve.reinhardt@amd.com{
3286876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
3296876Ssteve.reinhardt@amd.com}
330