SimpleNetwork.cc revision 10917
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.
567454Snate@binkert.org    m_endpoint_switches.resize(m_nodes);
576285Snate@binkert.org
589274Snilay@cs.wisc.edu    // record the routers
599593Snilay@cs.wisc.edu    for (vector<BasicRouter*>::const_iterator i = p->routers.begin();
609593Snilay@cs.wisc.edu         i != p->routers.end(); ++i) {
619274Snilay@cs.wisc.edu        Switch* s = safe_cast<Switch*>(*i);
629858Snilay@cs.wisc.edu        m_switches.push_back(s);
639274Snilay@cs.wisc.edu        s->init_net_ptr(this);
649274Snilay@cs.wisc.edu    }
656881SBrad.Beckmann@amd.com}
666285Snate@binkert.org
677054Snate@binkert.orgvoid
687054Snate@binkert.orgSimpleNetwork::init()
696881SBrad.Beckmann@amd.com{
707054Snate@binkert.org    Network::init();
716881SBrad.Beckmann@amd.com
727054Snate@binkert.org    // The topology pointer should have already been initialized in
737054Snate@binkert.org    // the parent class network constructor.
747054Snate@binkert.org    assert(m_topology_ptr != NULL);
759799Snilay@cs.wisc.edu    m_topology_ptr->createLinks(this);
766285Snate@binkert.org}
776145Snate@binkert.org
786145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
796145Snate@binkert.org{
809858Snilay@cs.wisc.edu    deletePointers(m_switches);
817454Snate@binkert.org    deletePointers(m_buffers_to_free);
826145Snate@binkert.org}
836145Snate@binkert.org
846145Snate@binkert.org// From a switch to an endpoint node
857054Snate@binkert.orgvoid
8610917Sbrandon.potter@amd.comSimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
8710917Sbrandon.potter@amd.com                           LinkDirection direction,
889799Snilay@cs.wisc.edu                           const NetDest& routing_table_entry)
896145Snate@binkert.org{
907054Snate@binkert.org    assert(dest < m_nodes);
919858Snilay@cs.wisc.edu    assert(src < m_switches.size());
929858Snilay@cs.wisc.edu    assert(m_switches[src] != NULL);
937054Snate@binkert.org
948258SBrad.Beckmann@amd.com    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
958258SBrad.Beckmann@amd.com
9610311Snilay@cs.wisc.edu    m_switches[src]->addOutPort(m_fromNetQueues[dest], routing_table_entry,
9710311Snilay@cs.wisc.edu                                simple_link->m_latency,
9810311Snilay@cs.wisc.edu                                simple_link->m_bw_multiplier);
998257SBrad.Beckmann@amd.com
1009858Snilay@cs.wisc.edu    m_endpoint_switches[dest] = m_switches[src];
1016145Snate@binkert.org}
1026145Snate@binkert.org
1036145Snate@binkert.org// From an endpoint node to a switch
1047054Snate@binkert.orgvoid
10510917Sbrandon.potter@amd.comSimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
10610917Sbrandon.potter@amd.com                          LinkDirection direction,
1079799Snilay@cs.wisc.edu                          const NetDest& routing_table_entry)
1086145Snate@binkert.org{
1097054Snate@binkert.org    assert(src < m_nodes);
1109858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(m_toNetQueues[src]);
1116145Snate@binkert.org}
1126145Snate@binkert.org
1136145Snate@binkert.org// From a switch to a switch
1147054Snate@binkert.orgvoid
11510917Sbrandon.potter@amd.comSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
11610917Sbrandon.potter@amd.com                                LinkDirection direction,
1179799Snilay@cs.wisc.edu                                const NetDest& routing_table_entry)
1186145Snate@binkert.org{
1196145Snate@binkert.org    // Create a set of new MessageBuffers
12010370Snilay@cs.wisc.edu    std::vector<MessageBuffer*> queues(m_virtual_networks);
12110370Snilay@cs.wisc.edu
1226145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1237054Snate@binkert.org        // allocate a buffer
1247054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1257054Snate@binkert.org        buffer_ptr->setOrdering(true);
12610311Snilay@cs.wisc.edu
1277054Snate@binkert.org        if (m_buffer_size > 0) {
1287454Snate@binkert.org            buffer_ptr->resize(m_buffer_size);
1297054Snate@binkert.org        }
13010311Snilay@cs.wisc.edu
13110311Snilay@cs.wisc.edu        queues[i] = buffer_ptr;
1327054Snate@binkert.org        // remember to deallocate it
1337454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
1347054Snate@binkert.org    }
13510311Snilay@cs.wisc.edu
1367054Snate@binkert.org    // Connect it to the two switches
1378258SBrad.Beckmann@amd.com    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
1388258SBrad.Beckmann@amd.com
1399858Snilay@cs.wisc.edu    m_switches[dest]->addInPort(queues);
1409858Snilay@cs.wisc.edu    m_switches[src]->addOutPort(queues, routing_table_entry,
14110311Snilay@cs.wisc.edu                                simple_link->m_latency,
14210311Snilay@cs.wisc.edu                                simple_link->m_bw_multiplier);
1437054Snate@binkert.org}
1447054Snate@binkert.org
1457054Snate@binkert.orgvoid
1467054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
1477054Snate@binkert.org{
1487832Snate@binkert.org    assert(id < m_nodes);
1497832Snate@binkert.org    assert(network_num < m_virtual_networks);
1507054Snate@binkert.org
1517054Snate@binkert.org    if (ordered) {
1527054Snate@binkert.org        m_ordered[network_num] = true;
1537054Snate@binkert.org    }
1547054Snate@binkert.org    m_in_use[network_num] = true;
1557054Snate@binkert.org}
1567054Snate@binkert.org
15710311Snilay@cs.wisc.eduvoid
15810311Snilay@cs.wisc.eduSimpleNetwork::setToNetQueue(NodeID id, bool ordered, int network_num,
15910311Snilay@cs.wisc.edu                             std::string vnet_type, MessageBuffer *b)
1607054Snate@binkert.org{
1617054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
16210370Snilay@cs.wisc.edu    while (m_toNetQueues[id].size() <= network_num) {
16310370Snilay@cs.wisc.edu        m_toNetQueues[id].push_back(nullptr);
16410370Snilay@cs.wisc.edu    }
16510311Snilay@cs.wisc.edu    m_toNetQueues[id][network_num] = b;
1667054Snate@binkert.org}
1677054Snate@binkert.org
16810311Snilay@cs.wisc.eduvoid
16910311Snilay@cs.wisc.eduSimpleNetwork::setFromNetQueue(NodeID id, bool ordered, int network_num,
17010311Snilay@cs.wisc.edu                               std::string vnet_type, MessageBuffer *b)
1717054Snate@binkert.org{
1727054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
17310370Snilay@cs.wisc.edu    while (m_fromNetQueues[id].size() <= network_num) {
17410370Snilay@cs.wisc.edu        m_fromNetQueues[id].push_back(nullptr);
17510370Snilay@cs.wisc.edu    }
17610311Snilay@cs.wisc.edu    m_fromNetQueues[id][network_num] = b;
1777054Snate@binkert.org}
1787054Snate@binkert.org
1797054Snate@binkert.orgvoid
1809863Snilay@cs.wisc.eduSimpleNetwork::regStats()
1817054Snate@binkert.org{
1829863Snilay@cs.wisc.edu    for (MessageSizeType type = MessageSizeType_FIRST;
1839863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
1849863Snilay@cs.wisc.edu        m_msg_counts[(unsigned int) type]
1859863Snilay@cs.wisc.edu            .name(name() + ".msg_count." + MessageSizeType_to_string(type))
1869863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1879863Snilay@cs.wisc.edu            ;
1889863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type]
1899863Snilay@cs.wisc.edu            .name(name() + ".msg_byte." + MessageSizeType_to_string(type))
1909863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1919863Snilay@cs.wisc.edu            ;
1927547SBrad.Beckmann@amd.com
1939863Snilay@cs.wisc.edu        // Now state what the formula is.
1949863Snilay@cs.wisc.edu        for (int i = 0; i < m_switches.size(); i++) {
1959863Snilay@cs.wisc.edu            m_msg_counts[(unsigned int) type] +=
1969863Snilay@cs.wisc.edu                sum(m_switches[i]->getMsgCount(type));
1977547SBrad.Beckmann@amd.com        }
1989863Snilay@cs.wisc.edu
1999863Snilay@cs.wisc.edu        m_msg_bytes[(unsigned int) type] =
2009863Snilay@cs.wisc.edu            m_msg_counts[(unsigned int) type] * Stats::constant(
2019863Snilay@cs.wisc.edu                    Network::MessageSizeType_to_int(type));
2027054Snate@binkert.org    }
2037054Snate@binkert.org}
2047054Snate@binkert.org
2057054Snate@binkert.orgvoid
2069863Snilay@cs.wisc.eduSimpleNetwork::collateStats()
2077054Snate@binkert.org{
2089858Snilay@cs.wisc.edu    for (int i = 0; i < m_switches.size(); i++) {
2099863Snilay@cs.wisc.edu        m_switches[i]->collateStats();
2107054Snate@binkert.org    }
2117054Snate@binkert.org}
2127054Snate@binkert.org
2137054Snate@binkert.orgvoid
2147054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
2156145Snate@binkert.org{
2167054Snate@binkert.org    out << "[SimpleNetwork]";
2176145Snate@binkert.org}
2186876Ssteve.reinhardt@amd.com
2196876Ssteve.reinhardt@amd.comSimpleNetwork *
2206876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
2216876Ssteve.reinhardt@amd.com{
2226876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
2236876Ssteve.reinhardt@amd.com}
2249302Snilay@cs.wisc.edu
2259302Snilay@cs.wisc.edu/*
2269302Snilay@cs.wisc.edu * The simple network has an array of switches. These switches have buffers
2279302Snilay@cs.wisc.edu * that need to be accessed for functional reads and writes. Also the links
2289302Snilay@cs.wisc.edu * between different switches have buffers that need to be accessed.
2299302Snilay@cs.wisc.edu */
2309302Snilay@cs.wisc.edubool
2319302Snilay@cs.wisc.eduSimpleNetwork::functionalRead(Packet *pkt)
2329302Snilay@cs.wisc.edu{
2339858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
2349858Snilay@cs.wisc.edu        if (m_switches[i]->functionalRead(pkt)) {
2359302Snilay@cs.wisc.edu            return true;
2369302Snilay@cs.wisc.edu        }
2379302Snilay@cs.wisc.edu    }
2389302Snilay@cs.wisc.edu
2399302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2409302Snilay@cs.wisc.edu        if (m_buffers_to_free[i]->functionalRead(pkt)) {
2419302Snilay@cs.wisc.edu            return true;
2429302Snilay@cs.wisc.edu        }
2439302Snilay@cs.wisc.edu    }
2449302Snilay@cs.wisc.edu
2459302Snilay@cs.wisc.edu    return false;
2469302Snilay@cs.wisc.edu}
2479302Snilay@cs.wisc.edu
2489302Snilay@cs.wisc.eduuint32_t
2499302Snilay@cs.wisc.eduSimpleNetwork::functionalWrite(Packet *pkt)
2509302Snilay@cs.wisc.edu{
2519302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
2529302Snilay@cs.wisc.edu
2539858Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_switches.size(); i++) {
2549858Snilay@cs.wisc.edu        num_functional_writes += m_switches[i]->functionalWrite(pkt);
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        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
2599302Snilay@cs.wisc.edu    }
2609302Snilay@cs.wisc.edu    return num_functional_writes;
2619302Snilay@cs.wisc.edu}
262