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
2911793Sbrandon.potter@amd.com#include "mem/ruby/network/simple/Switch.hh"
3011793Sbrandon.potter@amd.com
317454Snate@binkert.org#include <numeric>
327454Snate@binkert.org
338645Snilay@cs.wisc.edu#include "base/cast.hh"
347454Snate@binkert.org#include "base/stl_helpers.hh"
3510301Snilay@cs.wisc.edu#include "mem/ruby/network/MessageBuffer.hh"
367054Snate@binkert.org#include "mem/ruby/network/simple/PerfectSwitch.hh"
378259SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleNetwork.hh"
386154Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
396145Snate@binkert.org
407055Snate@binkert.orgusing namespace std;
417454Snate@binkert.orgusing m5::stl_helpers::deletePointers;
427454Snate@binkert.orgusing m5::stl_helpers::operator<<;
437055Snate@binkert.org
449274Snilay@cs.wisc.eduSwitch::Switch(const Params *p) : BasicRouter(p)
456145Snate@binkert.org{
469858Snilay@cs.wisc.edu    m_perfect_switch = new PerfectSwitch(m_id, this, p->virt_nets);
4711021Sjthestness@gmail.com    m_port_buffers = p->port_buffers;
4811021Sjthestness@gmail.com    m_num_connected_buffers = 0;
496145Snate@binkert.org}
506145Snate@binkert.org
516145Snate@binkert.orgSwitch::~Switch()
526145Snate@binkert.org{
539858Snilay@cs.wisc.edu    delete m_perfect_switch;
546145Snate@binkert.org
557054Snate@binkert.org    // Delete throttles (one per output port)
567454Snate@binkert.org    deletePointers(m_throttles);
576145Snate@binkert.org
587054Snate@binkert.org    // Delete MessageBuffers
5911021Sjthestness@gmail.com    deletePointers(m_port_buffers);
606145Snate@binkert.org}
616145Snate@binkert.org
627054Snate@binkert.orgvoid
639274Snilay@cs.wisc.eduSwitch::init()
649274Snilay@cs.wisc.edu{
659274Snilay@cs.wisc.edu    BasicRouter::init();
669858Snilay@cs.wisc.edu    m_perfect_switch->init(m_network_ptr);
679274Snilay@cs.wisc.edu}
689274Snilay@cs.wisc.edu
699274Snilay@cs.wisc.eduvoid
7010370Snilay@cs.wisc.eduSwitch::addInPort(const vector<MessageBuffer*>& in)
716145Snate@binkert.org{
729858Snilay@cs.wisc.edu    m_perfect_switch->addInPort(in);
736145Snate@binkert.org}
746145Snate@binkert.org
757054Snate@binkert.orgvoid
7610370Snilay@cs.wisc.eduSwitch::addOutPort(const vector<MessageBuffer*>& out,
7710311Snilay@cs.wisc.edu                   const NetDest& routing_table_entry,
7810311Snilay@cs.wisc.edu                   Cycles link_latency, int bw_multiplier)
796145Snate@binkert.org{
807054Snate@binkert.org    // Create a throttle
8110918Sbrandon.potter@amd.com    RubySystem *rs = m_network_ptr->params()->ruby_system;
8210918Sbrandon.potter@amd.com    Throttle* throttle_ptr = new Throttle(m_id, rs, m_throttles.size(),
8310311Snilay@cs.wisc.edu                                          link_latency, bw_multiplier,
8410311Snilay@cs.wisc.edu                                          m_network_ptr->getEndpointBandwidth(),
8510311Snilay@cs.wisc.edu                                          this);
8610311Snilay@cs.wisc.edu
877454Snate@binkert.org    m_throttles.push_back(throttle_ptr);
886145Snate@binkert.org
897054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
9010370Snilay@cs.wisc.edu    vector<MessageBuffer*> intermediateBuffers;
9110311Snilay@cs.wisc.edu
9210370Snilay@cs.wisc.edu    for (int i = 0; i < out.size(); ++i) {
9311021Sjthestness@gmail.com        assert(m_num_connected_buffers < m_port_buffers.size());
9411021Sjthestness@gmail.com        MessageBuffer* buffer_ptr = m_port_buffers[m_num_connected_buffers];
9511021Sjthestness@gmail.com        m_num_connected_buffers++;
9610370Snilay@cs.wisc.edu        intermediateBuffers.push_back(buffer_ptr);
979274Snilay@cs.wisc.edu    }
986145Snate@binkert.org
997054Snate@binkert.org    // Hook the queues to the PerfectSwitch
1009858Snilay@cs.wisc.edu    m_perfect_switch->addOutPort(intermediateBuffers, routing_table_entry);
1016145Snate@binkert.org
1027054Snate@binkert.org    // Hook the queues to the Throttle
1039508Snilay@cs.wisc.edu    throttle_ptr->addLinks(intermediateBuffers, out);
1046145Snate@binkert.org}
1056145Snate@binkert.org
1067054Snate@binkert.orgconst Throttle*
1077054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1086145Snate@binkert.org{
1097054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1107054Snate@binkert.org    return m_throttles[link_number];
1116145Snate@binkert.org}
1126145Snate@binkert.org
1137054Snate@binkert.orgvoid
1149863Snilay@cs.wisc.eduSwitch::regStats()
1156145Snate@binkert.org{
11611523Sdavid.guillen@arm.com    BasicRouter::regStats();
11711523Sdavid.guillen@arm.com
1189863Snilay@cs.wisc.edu    for (int link = 0; link < m_throttles.size(); link++) {
1199863Snilay@cs.wisc.edu        m_throttles[link]->regStats(name());
1209863Snilay@cs.wisc.edu    }
1216145Snate@binkert.org
1229863Snilay@cs.wisc.edu    m_avg_utilization.name(name() + ".percent_links_utilized");
1239863Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_throttles.size(); i++) {
1249863Snilay@cs.wisc.edu        m_avg_utilization += m_throttles[i]->getUtilization();
1259863Snilay@cs.wisc.edu    }
1269863Snilay@cs.wisc.edu    m_avg_utilization /= Stats::constant(m_throttles.size());
1276145Snate@binkert.org
1289863Snilay@cs.wisc.edu    for (unsigned int type = MessageSizeType_FIRST;
1299863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
1309863Snilay@cs.wisc.edu        m_msg_counts[type]
1319863Snilay@cs.wisc.edu            .name(name() + ".msg_count." +
1329863Snilay@cs.wisc.edu                MessageSizeType_to_string(MessageSizeType(type)))
1339863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1349863Snilay@cs.wisc.edu            ;
1359863Snilay@cs.wisc.edu        m_msg_bytes[type]
1369863Snilay@cs.wisc.edu            .name(name() + ".msg_bytes." +
1379863Snilay@cs.wisc.edu                MessageSizeType_to_string(MessageSizeType(type)))
1389863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1399863Snilay@cs.wisc.edu            ;
1409863Snilay@cs.wisc.edu
1419863Snilay@cs.wisc.edu        for (unsigned int i = 0; i < m_throttles.size(); i++) {
1429863Snilay@cs.wisc.edu            m_msg_counts[type] += m_throttles[i]->getMsgCount(type);
1437054Snate@binkert.org        }
1449863Snilay@cs.wisc.edu        m_msg_bytes[type] = m_msg_counts[type] * Stats::constant(
1459863Snilay@cs.wisc.edu                Network::MessageSizeType_to_int(MessageSizeType(type)));
1466145Snate@binkert.org    }
1476145Snate@binkert.org}
1486145Snate@binkert.org
1497054Snate@binkert.orgvoid
1509863Snilay@cs.wisc.eduSwitch::resetStats()
1516145Snate@binkert.org{
1529858Snilay@cs.wisc.edu    m_perfect_switch->clearStats();
1537054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1549863Snilay@cs.wisc.edu        m_throttles[i]->clearStats();
1559863Snilay@cs.wisc.edu    }
1569863Snilay@cs.wisc.edu}
1579863Snilay@cs.wisc.edu
1589863Snilay@cs.wisc.eduvoid
1599863Snilay@cs.wisc.eduSwitch::collateStats()
1609863Snilay@cs.wisc.edu{
1619863Snilay@cs.wisc.edu    m_perfect_switch->collateStats();
1629863Snilay@cs.wisc.edu    for (int i = 0; i < m_throttles.size(); i++) {
1639863Snilay@cs.wisc.edu        m_throttles[i]->collateStats();
1646145Snate@binkert.org    }
1656145Snate@binkert.org}
1666145Snate@binkert.org
1677054Snate@binkert.orgvoid
1687054Snate@binkert.orgSwitch::print(std::ostream& out) const
1696145Snate@binkert.org{
1707054Snate@binkert.org    // FIXME printing
1717054Snate@binkert.org    out << "[Switch]";
1726145Snate@binkert.org}
1739354Snilay@cs.wisc.edu
1749302Snilay@cs.wisc.edubool
1759302Snilay@cs.wisc.eduSwitch::functionalRead(Packet *pkt)
1769302Snilay@cs.wisc.edu{
1779302Snilay@cs.wisc.edu    return false;
1789302Snilay@cs.wisc.edu}
1799302Snilay@cs.wisc.edu
1809302Snilay@cs.wisc.eduuint32_t
1819302Snilay@cs.wisc.eduSwitch::functionalWrite(Packet *pkt)
1829302Snilay@cs.wisc.edu{
1839302Snilay@cs.wisc.edu    // Access the buffers in the switch for performing a functional write
1849302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
18511021Sjthestness@gmail.com    for (unsigned int i = 0; i < m_port_buffers.size(); ++i) {
18611021Sjthestness@gmail.com        num_functional_writes += m_port_buffers[i]->functionalWrite(pkt);
1879302Snilay@cs.wisc.edu    }
1889302Snilay@cs.wisc.edu    return num_functional_writes;
1899302Snilay@cs.wisc.edu}
1909274Snilay@cs.wisc.edu
1919274Snilay@cs.wisc.eduSwitch *
1929274Snilay@cs.wisc.eduSwitchParams::create()
1939274Snilay@cs.wisc.edu{
1949274Snilay@cs.wisc.edu    return new Switch(this);
1959274Snilay@cs.wisc.edu}
196