SimpleNetwork.cc revision 9858
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
839593Snilay@cs.wisc.edu    for (vector<BasicRouter*>::const_iterator i = p->routers.begin();
849593Snilay@cs.wisc.edu         i != p->routers.end(); ++i) {
859274Snilay@cs.wisc.edu        Switch* s = safe_cast<Switch*>(*i);
869858Snilay@cs.wisc.edu        m_switches.push_back(s);
879274Snilay@cs.wisc.edu        s->init_net_ptr(this);
889274Snilay@cs.wisc.edu    }
896881SBrad.Beckmann@amd.com}
906285Snate@binkert.org
917054Snate@binkert.orgvoid
927054Snate@binkert.orgSimpleNetwork::init()
936881SBrad.Beckmann@amd.com{
947054Snate@binkert.org    Network::init();
956881SBrad.Beckmann@amd.com
967054Snate@binkert.org    // The topology pointer should have already been initialized in
977054Snate@binkert.org    // the parent class network constructor.
987054Snate@binkert.org    assert(m_topology_ptr != NULL);
999799Snilay@cs.wisc.edu    m_topology_ptr->createLinks(this);
1006285Snate@binkert.org}
1016145Snate@binkert.org
1027054Snate@binkert.orgvoid
1037054Snate@binkert.orgSimpleNetwork::reset()
1046145Snate@binkert.org{
1057054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
1067054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
1077054Snate@binkert.org            m_toNetQueues[node][j]->clear();
1087054Snate@binkert.org            m_fromNetQueues[node][j]->clear();
1097054Snate@binkert.org        }
1106145Snate@binkert.org    }
1116145Snate@binkert.org
1129858Snilay@cs.wisc.edu    for(int i = 0; i < m_switches.size(); i++){
1139858Snilay@cs.wisc.edu        m_switches[i]->clearBuffers();
1147054Snate@binkert.org    }
1156145Snate@binkert.org}
1166145Snate@binkert.org
1176145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
1186145Snate@binkert.org{
1197054Snate@binkert.org    for (int i = 0; i < m_nodes; i++) {
1207454Snate@binkert.org        deletePointers(m_toNetQueues[i]);
1217454Snate@binkert.org        deletePointers(m_fromNetQueues[i]);
1227054Snate@binkert.org    }
1239858Snilay@cs.wisc.edu    deletePointers(m_switches);
1247454Snate@binkert.org    deletePointers(m_buffers_to_free);
1257054Snate@binkert.org    // delete m_topology_ptr;
1266145Snate@binkert.org}
1276145Snate@binkert.org
1286145Snate@binkert.org// From a switch to an endpoint node
1297054Snate@binkert.orgvoid
1308257SBrad.Beckmann@amd.comSimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
1318257SBrad.Beckmann@amd.com                           LinkDirection direction,
1329799Snilay@cs.wisc.edu                           const NetDest& routing_table_entry)
1336145Snate@binkert.org{
1347054Snate@binkert.org    assert(dest < m_nodes);
1359858Snilay@cs.wisc.edu    assert(src < m_switches.size());
1369858Snilay@cs.wisc.edu    assert(m_switches[src] != NULL);
1377054Snate@binkert.org
1388258SBrad.Beckmann@amd.com    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
1398258SBrad.Beckmann@amd.com
1409858Snilay@cs.wisc.edu    m_switches[src]->addOutPort(m_fromNetQueues[dest],
1418257SBrad.Beckmann@amd.com                                         routing_table_entry,
1428258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1438258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1448257SBrad.Beckmann@amd.com
1459858Snilay@cs.wisc.edu    m_endpoint_switches[dest] = m_switches[src];
1466145Snate@binkert.org}
1476145Snate@binkert.org
1486145Snate@binkert.org// From an endpoint node to a switch
1497054Snate@binkert.orgvoid
1508257SBrad.Beckmann@amd.comSimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
1518257SBrad.Beckmann@amd.com                          LinkDirection direction,
1529799Snilay@cs.wisc.edu                          const NetDest& routing_table_entry)
1536145Snate@binkert.org{
1547054Snate@binkert.org    assert(src < m_nodes);
1559858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(m_toNetQueues[src]);
1566145Snate@binkert.org}
1576145Snate@binkert.org
1586145Snate@binkert.org// From a switch to a switch
1597054Snate@binkert.orgvoid
1608257SBrad.Beckmann@amd.comSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
1618257SBrad.Beckmann@amd.com                                LinkDirection direction,
1629799Snilay@cs.wisc.edu                                const NetDest& routing_table_entry)
1636145Snate@binkert.org{
1646145Snate@binkert.org    // Create a set of new MessageBuffers
1657454Snate@binkert.org    std::vector<MessageBuffer*> queues;
1666145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1677054Snate@binkert.org        // allocate a buffer
1687054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1697054Snate@binkert.org        buffer_ptr->setOrdering(true);
1707054Snate@binkert.org        if (m_buffer_size > 0) {
1717454Snate@binkert.org            buffer_ptr->resize(m_buffer_size);
1727054Snate@binkert.org        }
1737454Snate@binkert.org        queues.push_back(buffer_ptr);
1747054Snate@binkert.org        // remember to deallocate it
1757454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
1767054Snate@binkert.org    }
1777054Snate@binkert.org    // Connect it to the two switches
1788258SBrad.Beckmann@amd.com    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
1798258SBrad.Beckmann@amd.com
1809858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(queues);
1819858Snilay@cs.wisc.edu    m_switches[src]->addOutPort(queues, routing_table_entry,
1828258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1838258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1847054Snate@binkert.org}
1857054Snate@binkert.org
1867054Snate@binkert.orgvoid
1877054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
1887054Snate@binkert.org{
1897832Snate@binkert.org    assert(id < m_nodes);
1907832Snate@binkert.org    assert(network_num < m_virtual_networks);
1917054Snate@binkert.org
1927054Snate@binkert.org    if (ordered) {
1937054Snate@binkert.org        m_ordered[network_num] = true;
1947054Snate@binkert.org    }
1957054Snate@binkert.org    m_in_use[network_num] = true;
1967054Snate@binkert.org}
1977054Snate@binkert.org
1987054Snate@binkert.orgMessageBuffer*
1998308Stushar@csail.mit.eduSimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num,
2008308Stushar@csail.mit.edu                             std::string vnet_type)
2017054Snate@binkert.org{
2027054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2037054Snate@binkert.org    return m_toNetQueues[id][network_num];
2047054Snate@binkert.org}
2057054Snate@binkert.org
2067054Snate@binkert.orgMessageBuffer*
2078308Stushar@csail.mit.eduSimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num,
2088308Stushar@csail.mit.edu                               std::string vnet_type)
2097054Snate@binkert.org{
2107054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2117054Snate@binkert.org    return m_fromNetQueues[id][network_num];
2127054Snate@binkert.org}
2137054Snate@binkert.org
2147454Snate@binkert.orgconst std::vector<Throttle*>*
2157054Snate@binkert.orgSimpleNetwork::getThrottles(NodeID id) const
2167054Snate@binkert.org{
2177054Snate@binkert.org    assert(id >= 0);
2187054Snate@binkert.org    assert(id < m_nodes);
2197054Snate@binkert.org    assert(m_endpoint_switches[id] != NULL);
2207054Snate@binkert.org    return m_endpoint_switches[id]->getThrottles();
2217054Snate@binkert.org}
2227054Snate@binkert.org
2237054Snate@binkert.orgvoid
2247054Snate@binkert.orgSimpleNetwork::printStats(ostream& out) const
2257054Snate@binkert.org{
2267054Snate@binkert.org    out << endl;
2277054Snate@binkert.org    out << "Network Stats" << endl;
2287054Snate@binkert.org    out << "-------------" << endl;
2297054Snate@binkert.org    out << endl;
2307547SBrad.Beckmann@amd.com
2317547SBrad.Beckmann@amd.com    //
2327547SBrad.Beckmann@amd.com    // Determine total counts before printing out each switch's stats
2337547SBrad.Beckmann@amd.com    //
2347547SBrad.Beckmann@amd.com    std::vector<uint64> total_msg_counts;
2357547SBrad.Beckmann@amd.com    total_msg_counts.resize(MessageSizeType_NUM);
2367547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2377547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2387547SBrad.Beckmann@amd.com         ++type) {
2397547SBrad.Beckmann@amd.com        total_msg_counts[type] = 0;
2407547SBrad.Beckmann@amd.com    }
2417547SBrad.Beckmann@amd.com
2429858Snilay@cs.wisc.edu    for (int i = 0; i < m_switches.size(); i++) {
2437547SBrad.Beckmann@amd.com        const std::vector<Throttle*>* throttles =
2449858Snilay@cs.wisc.edu            m_switches[i]->getThrottles();
2457547SBrad.Beckmann@amd.com
2467547SBrad.Beckmann@amd.com        for (int p = 0; p < throttles->size(); p++) {
2477547SBrad.Beckmann@amd.com
2487547SBrad.Beckmann@amd.com            const std::vector<std::vector<int> >& message_counts =
2497547SBrad.Beckmann@amd.com                ((*throttles)[p])->getCounters();
2507547SBrad.Beckmann@amd.com
2517547SBrad.Beckmann@amd.com            for (MessageSizeType type = MessageSizeType_FIRST;
2527547SBrad.Beckmann@amd.com                 type < MessageSizeType_NUM;
2537547SBrad.Beckmann@amd.com                 ++type) {
2547547SBrad.Beckmann@amd.com
2557547SBrad.Beckmann@amd.com                const std::vector<int> &mct = message_counts[type];
2567547SBrad.Beckmann@amd.com                int sum = accumulate(mct.begin(), mct.end(), 0);
2577547SBrad.Beckmann@amd.com                total_msg_counts[type] += uint64(sum);
2587547SBrad.Beckmann@amd.com            }
2597547SBrad.Beckmann@amd.com        }
2607547SBrad.Beckmann@amd.com    }
2617547SBrad.Beckmann@amd.com    uint64 total_msgs = 0;
2627547SBrad.Beckmann@amd.com    uint64 total_bytes = 0;
2637547SBrad.Beckmann@amd.com    for (MessageSizeType type = MessageSizeType_FIRST;
2647547SBrad.Beckmann@amd.com         type < MessageSizeType_NUM;
2657547SBrad.Beckmann@amd.com         ++type) {
2667547SBrad.Beckmann@amd.com
2677547SBrad.Beckmann@amd.com        if (total_msg_counts[type] > 0) {
2687547SBrad.Beckmann@amd.com            out << "total_msg_count_" << type << ": " << total_msg_counts[type]
2699274Snilay@cs.wisc.edu                << " " << total_msg_counts[type] *
2709274Snilay@cs.wisc.edu                uint64(MessageSizeType_to_int(type))
2717547SBrad.Beckmann@amd.com                << endl;
2727547SBrad.Beckmann@amd.com
2737547SBrad.Beckmann@amd.com            total_msgs += total_msg_counts[type];
2747547SBrad.Beckmann@amd.com
2757547SBrad.Beckmann@amd.com            total_bytes += total_msg_counts[type] *
2769274Snilay@cs.wisc.edu                uint64(MessageSizeType_to_int(type));
2777547SBrad.Beckmann@amd.com        }
2787547SBrad.Beckmann@amd.com    }
2797547SBrad.Beckmann@amd.com
2807547SBrad.Beckmann@amd.com    out << "total_msgs: " << total_msgs
2817547SBrad.Beckmann@amd.com        << " total_bytes: " << total_bytes << endl;
2827547SBrad.Beckmann@amd.com
2837547SBrad.Beckmann@amd.com    out << endl;
2849858Snilay@cs.wisc.edu    for (int i = 0; i < m_switches.size(); i++) {
2859858Snilay@cs.wisc.edu        m_switches[i]->printStats(out);
2867054Snate@binkert.org    }
2877054Snate@binkert.org}
2887054Snate@binkert.org
2897054Snate@binkert.orgvoid
2907054Snate@binkert.orgSimpleNetwork::clearStats()
2917054Snate@binkert.org{
2929858Snilay@cs.wisc.edu    for (int i = 0; i < m_switches.size(); i++) {
2939858Snilay@cs.wisc.edu        m_switches[i]->clearStats();
2947054Snate@binkert.org    }
2957054Snate@binkert.org}
2967054Snate@binkert.org
2977054Snate@binkert.orgvoid
2987054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
2996145Snate@binkert.org{
3007054Snate@binkert.org    out << "[SimpleNetwork]";
3016145Snate@binkert.org}
3026876Ssteve.reinhardt@amd.com
3036876Ssteve.reinhardt@amd.comSimpleNetwork *
3046876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
3056876Ssteve.reinhardt@amd.com{
3066876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
3076876Ssteve.reinhardt@amd.com}
3089302Snilay@cs.wisc.edu
3099302Snilay@cs.wisc.edu/*
3109302Snilay@cs.wisc.edu * The simple network has an array of switches. These switches have buffers
3119302Snilay@cs.wisc.edu * that need to be accessed for functional reads and writes. Also the links
3129302Snilay@cs.wisc.edu * between different switches have buffers that need to be accessed.
3139302Snilay@cs.wisc.edu */
3149302Snilay@cs.wisc.edubool
3159302Snilay@cs.wisc.eduSimpleNetwork::functionalRead(Packet *pkt)
3169302Snilay@cs.wisc.edu{
3179858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
3189858Snilay@cs.wisc.edu        if (m_switches[i]->functionalRead(pkt)) {
3199302Snilay@cs.wisc.edu            return true;
3209302Snilay@cs.wisc.edu        }
3219302Snilay@cs.wisc.edu    }
3229302Snilay@cs.wisc.edu
3239302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
3249302Snilay@cs.wisc.edu        if (m_buffers_to_free[i]->functionalRead(pkt)) {
3259302Snilay@cs.wisc.edu            return true;
3269302Snilay@cs.wisc.edu        }
3279302Snilay@cs.wisc.edu    }
3289302Snilay@cs.wisc.edu
3299302Snilay@cs.wisc.edu    return false;
3309302Snilay@cs.wisc.edu}
3319302Snilay@cs.wisc.edu
3329302Snilay@cs.wisc.eduuint32_t
3339302Snilay@cs.wisc.eduSimpleNetwork::functionalWrite(Packet *pkt)
3349302Snilay@cs.wisc.edu{
3359302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
3369302Snilay@cs.wisc.edu
3379858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
3389858Snilay@cs.wisc.edu        num_functional_writes += m_switches[i]->functionalWrite(pkt);
3399302Snilay@cs.wisc.edu    }
3409302Snilay@cs.wisc.edu
3419302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
3429302Snilay@cs.wisc.edu        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
3439302Snilay@cs.wisc.edu    }
3449302Snilay@cs.wisc.edu    return num_functional_writes;
3459302Snilay@cs.wisc.edu}
346