SimpleNetwork.cc revision 10301
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/common/NetDest.hh"
3510301Snilay@cs.wisc.edu#include "mem/ruby/network/MessageBuffer.hh"
368258SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleLink.hh"
376154Snate@binkert.org#include "mem/ruby/network/simple/SimpleNetwork.hh"
387054Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
397547SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/Throttle.hh"
406154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
416154Snate@binkert.org#include "mem/ruby/system/System.hh"
426145Snate@binkert.org
437055Snate@binkert.orgusing namespace std;
447454Snate@binkert.orgusing m5::stl_helpers::deletePointers;
457055Snate@binkert.org
466876Ssteve.reinhardt@amd.comSimpleNetwork::SimpleNetwork(const Params *p)
476876Ssteve.reinhardt@amd.com    : Network(p)
486285Snate@binkert.org{
498259SBrad.Beckmann@amd.com    m_buffer_size = p->buffer_size;
508259SBrad.Beckmann@amd.com    m_endpoint_bandwidth = p->endpoint_bandwidth;
518259SBrad.Beckmann@amd.com    m_adaptive_routing = p->adaptive_routing;
528259SBrad.Beckmann@amd.com
537054Snate@binkert.org    // Note: the parent Network Object constructor is called before the
547054Snate@binkert.org    // SimpleNetwork child constructor.  Therefore, the member variables
557054Snate@binkert.org    // used below should already be initialized.
566285Snate@binkert.org
577454Snate@binkert.org    m_endpoint_switches.resize(m_nodes);
586285Snate@binkert.org
597454Snate@binkert.org    m_in_use.resize(m_virtual_networks);
607454Snate@binkert.org    m_ordered.resize(m_virtual_networks);
617054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
627054Snate@binkert.org        m_in_use[i] = false;
637054Snate@binkert.org        m_ordered[i] = false;
646285Snate@binkert.org    }
657054Snate@binkert.org
667054Snate@binkert.org    // Allocate to and from queues
677454Snate@binkert.org    m_toNetQueues.resize(m_nodes);
687454Snate@binkert.org    m_fromNetQueues.resize(m_nodes);
697054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
707454Snate@binkert.org        m_toNetQueues[node].resize(m_virtual_networks);
717454Snate@binkert.org        m_fromNetQueues[node].resize(m_virtual_networks);
727054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
737056Snate@binkert.org            m_toNetQueues[node][j] =
747056Snate@binkert.org                new MessageBuffer(csprintf("toNet node %d j %d", node, j));
757056Snate@binkert.org            m_fromNetQueues[node][j] =
767056Snate@binkert.org                new MessageBuffer(csprintf("fromNet node %d j %d", node, j));
777054Snate@binkert.org        }
787054Snate@binkert.org    }
799274Snilay@cs.wisc.edu
809274Snilay@cs.wisc.edu    // record the routers
819593Snilay@cs.wisc.edu    for (vector<BasicRouter*>::const_iterator i = p->routers.begin();
829593Snilay@cs.wisc.edu         i != p->routers.end(); ++i) {
839274Snilay@cs.wisc.edu        Switch* s = safe_cast<Switch*>(*i);
849858Snilay@cs.wisc.edu        m_switches.push_back(s);
859274Snilay@cs.wisc.edu        s->init_net_ptr(this);
869274Snilay@cs.wisc.edu    }
876881SBrad.Beckmann@amd.com}
886285Snate@binkert.org
897054Snate@binkert.orgvoid
907054Snate@binkert.orgSimpleNetwork::init()
916881SBrad.Beckmann@amd.com{
927054Snate@binkert.org    Network::init();
936881SBrad.Beckmann@amd.com
947054Snate@binkert.org    // The topology pointer should have already been initialized in
957054Snate@binkert.org    // the parent class network constructor.
967054Snate@binkert.org    assert(m_topology_ptr != NULL);
979799Snilay@cs.wisc.edu    m_topology_ptr->createLinks(this);
986285Snate@binkert.org}
996145Snate@binkert.org
1006145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
1016145Snate@binkert.org{
1027054Snate@binkert.org    for (int i = 0; i < m_nodes; i++) {
1037454Snate@binkert.org        deletePointers(m_toNetQueues[i]);
1047454Snate@binkert.org        deletePointers(m_fromNetQueues[i]);
1057054Snate@binkert.org    }
1069858Snilay@cs.wisc.edu    deletePointers(m_switches);
1077454Snate@binkert.org    deletePointers(m_buffers_to_free);
1087054Snate@binkert.org    // delete m_topology_ptr;
1096145Snate@binkert.org}
1106145Snate@binkert.org
1116145Snate@binkert.org// From a switch to an endpoint node
1127054Snate@binkert.orgvoid
1138257SBrad.Beckmann@amd.comSimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
1148257SBrad.Beckmann@amd.com                           LinkDirection direction,
1159799Snilay@cs.wisc.edu                           const NetDest& routing_table_entry)
1166145Snate@binkert.org{
1177054Snate@binkert.org    assert(dest < m_nodes);
1189858Snilay@cs.wisc.edu    assert(src < m_switches.size());
1199858Snilay@cs.wisc.edu    assert(m_switches[src] != NULL);
1207054Snate@binkert.org
1218258SBrad.Beckmann@amd.com    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
1228258SBrad.Beckmann@amd.com
1239858Snilay@cs.wisc.edu    m_switches[src]->addOutPort(m_fromNetQueues[dest],
1248257SBrad.Beckmann@amd.com                                         routing_table_entry,
1258258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1268258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1278257SBrad.Beckmann@amd.com
1289858Snilay@cs.wisc.edu    m_endpoint_switches[dest] = m_switches[src];
1296145Snate@binkert.org}
1306145Snate@binkert.org
1316145Snate@binkert.org// From an endpoint node to a switch
1327054Snate@binkert.orgvoid
1338257SBrad.Beckmann@amd.comSimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
1348257SBrad.Beckmann@amd.com                          LinkDirection direction,
1359799Snilay@cs.wisc.edu                          const NetDest& routing_table_entry)
1366145Snate@binkert.org{
1377054Snate@binkert.org    assert(src < m_nodes);
1389858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(m_toNetQueues[src]);
1396145Snate@binkert.org}
1406145Snate@binkert.org
1416145Snate@binkert.org// From a switch to a switch
1427054Snate@binkert.orgvoid
1438257SBrad.Beckmann@amd.comSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
1448257SBrad.Beckmann@amd.com                                LinkDirection direction,
1459799Snilay@cs.wisc.edu                                const NetDest& routing_table_entry)
1466145Snate@binkert.org{
1476145Snate@binkert.org    // Create a set of new MessageBuffers
1487454Snate@binkert.org    std::vector<MessageBuffer*> queues;
1496145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1507054Snate@binkert.org        // allocate a buffer
1517054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1527054Snate@binkert.org        buffer_ptr->setOrdering(true);
1537054Snate@binkert.org        if (m_buffer_size > 0) {
1547454Snate@binkert.org            buffer_ptr->resize(m_buffer_size);
1557054Snate@binkert.org        }
1567454Snate@binkert.org        queues.push_back(buffer_ptr);
1577054Snate@binkert.org        // remember to deallocate it
1587454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
1597054Snate@binkert.org    }
1607054Snate@binkert.org    // Connect it to the two switches
1618258SBrad.Beckmann@amd.com    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
1628258SBrad.Beckmann@amd.com
1639858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(queues);
1649858Snilay@cs.wisc.edu    m_switches[src]->addOutPort(queues, routing_table_entry,
1658258SBrad.Beckmann@amd.com                                         simple_link->m_latency,
1668258SBrad.Beckmann@amd.com                                         simple_link->m_bw_multiplier);
1677054Snate@binkert.org}
1687054Snate@binkert.org
1697054Snate@binkert.orgvoid
1707054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
1717054Snate@binkert.org{
1727832Snate@binkert.org    assert(id < m_nodes);
1737832Snate@binkert.org    assert(network_num < m_virtual_networks);
1747054Snate@binkert.org
1757054Snate@binkert.org    if (ordered) {
1767054Snate@binkert.org        m_ordered[network_num] = true;
1777054Snate@binkert.org    }
1787054Snate@binkert.org    m_in_use[network_num] = true;
1797054Snate@binkert.org}
1807054Snate@binkert.org
1817054Snate@binkert.orgMessageBuffer*
1828308Stushar@csail.mit.eduSimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num,
1838308Stushar@csail.mit.edu                             std::string vnet_type)
1847054Snate@binkert.org{
1857054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
1867054Snate@binkert.org    return m_toNetQueues[id][network_num];
1877054Snate@binkert.org}
1887054Snate@binkert.org
1897054Snate@binkert.orgMessageBuffer*
1908308Stushar@csail.mit.eduSimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num,
1918308Stushar@csail.mit.edu                               std::string vnet_type)
1927054Snate@binkert.org{
1937054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
1947054Snate@binkert.org    return m_fromNetQueues[id][network_num];
1957054Snate@binkert.org}
1967054Snate@binkert.org
1977054Snate@binkert.orgvoid
1989863Snilay@cs.wisc.eduSimpleNetwork::regStats()
1997054Snate@binkert.org{
2009863Snilay@cs.wisc.edu    for (MessageSizeType type = MessageSizeType_FIRST;
2019863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
2029863Snilay@cs.wisc.edu        m_msg_counts[(unsigned int) type]
2039863Snilay@cs.wisc.edu            .name(name() + ".msg_count." + MessageSizeType_to_string(type))
2049863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2059863Snilay@cs.wisc.edu            ;
2069863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type]
2079863Snilay@cs.wisc.edu            .name(name() + ".msg_byte." + MessageSizeType_to_string(type))
2089863Snilay@cs.wisc.edu            .flags(Stats::nozero)
2099863Snilay@cs.wisc.edu            ;
2107547SBrad.Beckmann@amd.com
2119863Snilay@cs.wisc.edu        // Now state what the formula is.
2129863Snilay@cs.wisc.edu        for (int i = 0; i < m_switches.size(); i++) {
2139863Snilay@cs.wisc.edu            m_msg_counts[(unsigned int) type] +=
2149863Snilay@cs.wisc.edu                sum(m_switches[i]->getMsgCount(type));
2157547SBrad.Beckmann@amd.com        }
2169863Snilay@cs.wisc.edu
2179863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type] =
2189863Snilay@cs.wisc.edu            m_msg_counts[(unsigned int) type] * Stats::constant(
2199863Snilay@cs.wisc.edu                    Network::MessageSizeType_to_int(type));
2207054Snate@binkert.org    }
2217054Snate@binkert.org}
2227054Snate@binkert.org
2237054Snate@binkert.orgvoid
2249863Snilay@cs.wisc.eduSimpleNetwork::collateStats()
2257054Snate@binkert.org{
2269858Snilay@cs.wisc.edu    for (int i = 0; i < m_switches.size(); i++) {
2279863Snilay@cs.wisc.edu        m_switches[i]->collateStats();
2287054Snate@binkert.org    }
2297054Snate@binkert.org}
2307054Snate@binkert.org
2317054Snate@binkert.orgvoid
2327054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
2336145Snate@binkert.org{
2347054Snate@binkert.org    out << "[SimpleNetwork]";
2356145Snate@binkert.org}
2366876Ssteve.reinhardt@amd.com
2376876Ssteve.reinhardt@amd.comSimpleNetwork *
2386876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
2396876Ssteve.reinhardt@amd.com{
2406876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
2416876Ssteve.reinhardt@amd.com}
2429302Snilay@cs.wisc.edu
2439302Snilay@cs.wisc.edu/*
2449302Snilay@cs.wisc.edu * The simple network has an array of switches. These switches have buffers
2459302Snilay@cs.wisc.edu * that need to be accessed for functional reads and writes. Also the links
2469302Snilay@cs.wisc.edu * between different switches have buffers that need to be accessed.
2479302Snilay@cs.wisc.edu */
2489302Snilay@cs.wisc.edubool
2499302Snilay@cs.wisc.eduSimpleNetwork::functionalRead(Packet *pkt)
2509302Snilay@cs.wisc.edu{
2519858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
2529858Snilay@cs.wisc.edu        if (m_switches[i]->functionalRead(pkt)) {
2539302Snilay@cs.wisc.edu            return true;
2549302Snilay@cs.wisc.edu        }
2559302Snilay@cs.wisc.edu    }
2569302Snilay@cs.wisc.edu
2579302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2589302Snilay@cs.wisc.edu        if (m_buffers_to_free[i]->functionalRead(pkt)) {
2599302Snilay@cs.wisc.edu            return true;
2609302Snilay@cs.wisc.edu        }
2619302Snilay@cs.wisc.edu    }
2629302Snilay@cs.wisc.edu
2639302Snilay@cs.wisc.edu    return false;
2649302Snilay@cs.wisc.edu}
2659302Snilay@cs.wisc.edu
2669302Snilay@cs.wisc.eduuint32_t
2679302Snilay@cs.wisc.eduSimpleNetwork::functionalWrite(Packet *pkt)
2689302Snilay@cs.wisc.edu{
2699302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
2709302Snilay@cs.wisc.edu
2719858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
2729858Snilay@cs.wisc.edu        num_functional_writes += m_switches[i]->functionalWrite(pkt);
2739302Snilay@cs.wisc.edu    }
2749302Snilay@cs.wisc.edu
2759302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2769302Snilay@cs.wisc.edu        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
2779302Snilay@cs.wisc.edu    }
2789302Snilay@cs.wisc.edu    return num_functional_writes;
2799302Snilay@cs.wisc.edu}
280