Switch.cc revision 10311
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
297454Snate@binkert.org#include <numeric>
307454Snate@binkert.org
318645Snilay@cs.wisc.edu#include "base/cast.hh"
327454Snate@binkert.org#include "base/stl_helpers.hh"
3310301Snilay@cs.wisc.edu#include "mem/ruby/network/MessageBuffer.hh"
347054Snate@binkert.org#include "mem/ruby/network/simple/PerfectSwitch.hh"
358259SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleNetwork.hh"
366154Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
376154Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
386145Snate@binkert.org
397055Snate@binkert.orgusing namespace std;
407454Snate@binkert.orgusing m5::stl_helpers::deletePointers;
417454Snate@binkert.orgusing m5::stl_helpers::operator<<;
427055Snate@binkert.org
439274Snilay@cs.wisc.eduSwitch::Switch(const Params *p) : BasicRouter(p)
446145Snate@binkert.org{
459858Snilay@cs.wisc.edu    m_perfect_switch = new PerfectSwitch(m_id, this, p->virt_nets);
466145Snate@binkert.org}
476145Snate@binkert.org
486145Snate@binkert.orgSwitch::~Switch()
496145Snate@binkert.org{
509858Snilay@cs.wisc.edu    delete m_perfect_switch;
516145Snate@binkert.org
527054Snate@binkert.org    // Delete throttles (one per output port)
537454Snate@binkert.org    deletePointers(m_throttles);
546145Snate@binkert.org
557054Snate@binkert.org    // Delete MessageBuffers
567454Snate@binkert.org    deletePointers(m_buffers_to_free);
576145Snate@binkert.org}
586145Snate@binkert.org
597054Snate@binkert.orgvoid
609274Snilay@cs.wisc.eduSwitch::init()
619274Snilay@cs.wisc.edu{
629274Snilay@cs.wisc.edu    BasicRouter::init();
639858Snilay@cs.wisc.edu    m_perfect_switch->init(m_network_ptr);
649274Snilay@cs.wisc.edu}
659274Snilay@cs.wisc.edu
669274Snilay@cs.wisc.eduvoid
6710311Snilay@cs.wisc.eduSwitch::addInPort(const map<int, MessageBuffer*>& in)
686145Snate@binkert.org{
699858Snilay@cs.wisc.edu    m_perfect_switch->addInPort(in);
709508Snilay@cs.wisc.edu
7110311Snilay@cs.wisc.edu    for (auto& it : in) {
7210311Snilay@cs.wisc.edu        it.second->setReceiver(this);
739508Snilay@cs.wisc.edu    }
746145Snate@binkert.org}
756145Snate@binkert.org
767054Snate@binkert.orgvoid
7710311Snilay@cs.wisc.eduSwitch::addOutPort(const map<int, MessageBuffer*>& out,
7810311Snilay@cs.wisc.edu                   const NetDest& routing_table_entry,
7910311Snilay@cs.wisc.edu                   Cycles link_latency, int bw_multiplier)
806145Snate@binkert.org{
817054Snate@binkert.org    // Create a throttle
829274Snilay@cs.wisc.edu    Throttle* throttle_ptr = new Throttle(m_id, 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)
9010311Snilay@cs.wisc.edu    map<int, MessageBuffer*> intermediateBuffers;
9110311Snilay@cs.wisc.edu
9210311Snilay@cs.wisc.edu    for (auto& it : out) {
9310311Snilay@cs.wisc.edu        it.second->setSender(this);
949508Snilay@cs.wisc.edu
957054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
967054Snate@binkert.org        // Make these queues ordered
977054Snate@binkert.org        buffer_ptr->setOrdering(true);
989274Snilay@cs.wisc.edu        if (m_network_ptr->getBufferSize() > 0) {
999274Snilay@cs.wisc.edu            buffer_ptr->resize(m_network_ptr->getBufferSize());
1007054Snate@binkert.org        }
1019508Snilay@cs.wisc.edu
10210311Snilay@cs.wisc.edu        intermediateBuffers[it.first] = buffer_ptr;
1037454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
1049508Snilay@cs.wisc.edu
1059508Snilay@cs.wisc.edu        buffer_ptr->setSender(this);
1069508Snilay@cs.wisc.edu        buffer_ptr->setReceiver(this);
1079274Snilay@cs.wisc.edu    }
1086145Snate@binkert.org
1097054Snate@binkert.org    // Hook the queues to the PerfectSwitch
1109858Snilay@cs.wisc.edu    m_perfect_switch->addOutPort(intermediateBuffers, routing_table_entry);
1116145Snate@binkert.org
1127054Snate@binkert.org    // Hook the queues to the Throttle
1139508Snilay@cs.wisc.edu    throttle_ptr->addLinks(intermediateBuffers, out);
1146145Snate@binkert.org}
1156145Snate@binkert.org
1167054Snate@binkert.orgconst Throttle*
1177054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1186145Snate@binkert.org{
1197054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1207054Snate@binkert.org    return m_throttles[link_number];
1216145Snate@binkert.org}
1226145Snate@binkert.org
1237054Snate@binkert.orgvoid
1249863Snilay@cs.wisc.eduSwitch::regStats()
1256145Snate@binkert.org{
1269863Snilay@cs.wisc.edu    for (int link = 0; link < m_throttles.size(); link++) {
1279863Snilay@cs.wisc.edu        m_throttles[link]->regStats(name());
1289863Snilay@cs.wisc.edu    }
1296145Snate@binkert.org
1309863Snilay@cs.wisc.edu    m_avg_utilization.name(name() + ".percent_links_utilized");
1319863Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_throttles.size(); i++) {
1329863Snilay@cs.wisc.edu        m_avg_utilization += m_throttles[i]->getUtilization();
1339863Snilay@cs.wisc.edu    }
1349863Snilay@cs.wisc.edu    m_avg_utilization /= Stats::constant(m_throttles.size());
1356145Snate@binkert.org
1369863Snilay@cs.wisc.edu    for (unsigned int type = MessageSizeType_FIRST;
1379863Snilay@cs.wisc.edu         type < MessageSizeType_NUM; ++type) {
1389863Snilay@cs.wisc.edu        m_msg_counts[type]
1399863Snilay@cs.wisc.edu            .name(name() + ".msg_count." +
1409863Snilay@cs.wisc.edu                MessageSizeType_to_string(MessageSizeType(type)))
1419863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1429863Snilay@cs.wisc.edu            ;
1439863Snilay@cs.wisc.edu        m_msg_bytes[type]
1449863Snilay@cs.wisc.edu            .name(name() + ".msg_bytes." +
1459863Snilay@cs.wisc.edu                MessageSizeType_to_string(MessageSizeType(type)))
1469863Snilay@cs.wisc.edu            .flags(Stats::nozero)
1479863Snilay@cs.wisc.edu            ;
1489863Snilay@cs.wisc.edu
1499863Snilay@cs.wisc.edu        for (unsigned int i = 0; i < m_throttles.size(); i++) {
1509863Snilay@cs.wisc.edu            m_msg_counts[type] += m_throttles[i]->getMsgCount(type);
1517054Snate@binkert.org        }
1529863Snilay@cs.wisc.edu        m_msg_bytes[type] = m_msg_counts[type] * Stats::constant(
1539863Snilay@cs.wisc.edu                Network::MessageSizeType_to_int(MessageSizeType(type)));
1546145Snate@binkert.org    }
1556145Snate@binkert.org}
1566145Snate@binkert.org
1577054Snate@binkert.orgvoid
1589863Snilay@cs.wisc.eduSwitch::resetStats()
1596145Snate@binkert.org{
1609858Snilay@cs.wisc.edu    m_perfect_switch->clearStats();
1617054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1629863Snilay@cs.wisc.edu        m_throttles[i]->clearStats();
1639863Snilay@cs.wisc.edu    }
1649863Snilay@cs.wisc.edu}
1659863Snilay@cs.wisc.edu
1669863Snilay@cs.wisc.eduvoid
1679863Snilay@cs.wisc.eduSwitch::collateStats()
1689863Snilay@cs.wisc.edu{
1699863Snilay@cs.wisc.edu    m_perfect_switch->collateStats();
1709863Snilay@cs.wisc.edu    for (int i = 0; i < m_throttles.size(); i++) {
1719863Snilay@cs.wisc.edu        m_throttles[i]->collateStats();
1726145Snate@binkert.org    }
1736145Snate@binkert.org}
1746145Snate@binkert.org
1757054Snate@binkert.orgvoid
1767054Snate@binkert.orgSwitch::print(std::ostream& out) const
1776145Snate@binkert.org{
1787054Snate@binkert.org    // FIXME printing
1797054Snate@binkert.org    out << "[Switch]";
1806145Snate@binkert.org}
1819354Snilay@cs.wisc.edu
1829302Snilay@cs.wisc.edubool
1839302Snilay@cs.wisc.eduSwitch::functionalRead(Packet *pkt)
1849302Snilay@cs.wisc.edu{
1859302Snilay@cs.wisc.edu    // Access the buffers in the switch for performing a functional read
1869302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
1879302Snilay@cs.wisc.edu        if (m_buffers_to_free[i]->functionalRead(pkt)) {
1889302Snilay@cs.wisc.edu            return true;
1899302Snilay@cs.wisc.edu        }
1909302Snilay@cs.wisc.edu    }
1919302Snilay@cs.wisc.edu    return false;
1929302Snilay@cs.wisc.edu}
1939302Snilay@cs.wisc.edu
1949302Snilay@cs.wisc.eduuint32_t
1959302Snilay@cs.wisc.eduSwitch::functionalWrite(Packet *pkt)
1969302Snilay@cs.wisc.edu{
1979302Snilay@cs.wisc.edu    // Access the buffers in the switch for performing a functional write
1989302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
1999302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2009302Snilay@cs.wisc.edu        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
2019302Snilay@cs.wisc.edu    }
2029302Snilay@cs.wisc.edu    return num_functional_writes;
2039302Snilay@cs.wisc.edu}
2049274Snilay@cs.wisc.edu
2059274Snilay@cs.wisc.eduSwitch *
2069274Snilay@cs.wisc.eduSwitchParams::create()
2079274Snilay@cs.wisc.edu{
2089274Snilay@cs.wisc.edu    return new Switch(this);
2099274Snilay@cs.wisc.edu}
210