Switch.cc revision 9354
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"
337054Snate@binkert.org#include "mem/protocol/MessageSizeType.hh"
347054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
357054Snate@binkert.org#include "mem/ruby/network/simple/PerfectSwitch.hh"
368259SBrad.Beckmann@amd.com#include "mem/ruby/network/simple/SimpleNetwork.hh"
376154Snate@binkert.org#include "mem/ruby/network/simple/Switch.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{
469274Snilay@cs.wisc.edu    m_perfect_switch_ptr = new PerfectSwitch(m_id, this, p->virt_nets);
476145Snate@binkert.org}
486145Snate@binkert.org
496145Snate@binkert.orgSwitch::~Switch()
506145Snate@binkert.org{
517054Snate@binkert.org    delete m_perfect_switch_ptr;
526145Snate@binkert.org
537054Snate@binkert.org    // Delete throttles (one per output port)
547454Snate@binkert.org    deletePointers(m_throttles);
556145Snate@binkert.org
567054Snate@binkert.org    // Delete MessageBuffers
577454Snate@binkert.org    deletePointers(m_buffers_to_free);
586145Snate@binkert.org}
596145Snate@binkert.org
607054Snate@binkert.orgvoid
619274Snilay@cs.wisc.eduSwitch::init()
629274Snilay@cs.wisc.edu{
639274Snilay@cs.wisc.edu    BasicRouter::init();
649274Snilay@cs.wisc.edu    m_perfect_switch_ptr->init(m_network_ptr);
659274Snilay@cs.wisc.edu}
669274Snilay@cs.wisc.edu
679274Snilay@cs.wisc.eduvoid
687454Snate@binkert.orgSwitch::addInPort(const vector<MessageBuffer*>& in)
696145Snate@binkert.org{
707054Snate@binkert.org    m_perfect_switch_ptr->addInPort(in);
716145Snate@binkert.org}
726145Snate@binkert.org
737054Snate@binkert.orgvoid
747454Snate@binkert.orgSwitch::addOutPort(const vector<MessageBuffer*>& out,
757054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
766145Snate@binkert.org{
777054Snate@binkert.org    // Create a throttle
789274Snilay@cs.wisc.edu    Throttle* throttle_ptr = new Throttle(m_id, m_throttles.size(),
799274Snilay@cs.wisc.edu        link_latency, bw_multiplier, m_network_ptr->getEndpointBandwidth(),
809274Snilay@cs.wisc.edu        this);
817454Snate@binkert.org    m_throttles.push_back(throttle_ptr);
826145Snate@binkert.org
837054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
847454Snate@binkert.org    vector<MessageBuffer*> intermediateBuffers;
857054Snate@binkert.org    for (int i = 0; i < out.size(); i++) {
867054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
877054Snate@binkert.org        // Make these queues ordered
887054Snate@binkert.org        buffer_ptr->setOrdering(true);
899274Snilay@cs.wisc.edu        if (m_network_ptr->getBufferSize() > 0) {
909274Snilay@cs.wisc.edu            buffer_ptr->resize(m_network_ptr->getBufferSize());
917054Snate@binkert.org        }
927454Snate@binkert.org        intermediateBuffers.push_back(buffer_ptr);
937454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
949274Snilay@cs.wisc.edu    }
956145Snate@binkert.org
967054Snate@binkert.org    // Hook the queues to the PerfectSwitch
977054Snate@binkert.org    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
986145Snate@binkert.org
997054Snate@binkert.org    // Hook the queues to the Throttle
1007054Snate@binkert.org    throttle_ptr->addLinks(intermediateBuffers, out);
1016145Snate@binkert.org}
1026145Snate@binkert.org
1037054Snate@binkert.orgvoid
1047054Snate@binkert.orgSwitch::clearRoutingTables()
1056145Snate@binkert.org{
1067054Snate@binkert.org    m_perfect_switch_ptr->clearRoutingTables();
1076145Snate@binkert.org}
1086145Snate@binkert.org
1097054Snate@binkert.orgvoid
1107054Snate@binkert.orgSwitch::clearBuffers()
1116145Snate@binkert.org{
1127054Snate@binkert.org    m_perfect_switch_ptr->clearBuffers();
1137054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1147054Snate@binkert.org        if (m_throttles[i] != NULL) {
1157054Snate@binkert.org            m_throttles[i]->clear();
1167054Snate@binkert.org        }
1176145Snate@binkert.org    }
1186145Snate@binkert.org}
1196145Snate@binkert.org
1207054Snate@binkert.orgvoid
1217054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
1226145Snate@binkert.org{
1237054Snate@binkert.org    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
1246145Snate@binkert.org}
1256145Snate@binkert.org
1267054Snate@binkert.orgconst Throttle*
1277054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1286145Snate@binkert.org{
1297054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1307054Snate@binkert.org    return m_throttles[link_number];
1316145Snate@binkert.org}
1326145Snate@binkert.org
1337454Snate@binkert.orgconst vector<Throttle*>*
1347054Snate@binkert.orgSwitch::getThrottles() const
1356145Snate@binkert.org{
1367054Snate@binkert.org    return &m_throttles;
1376145Snate@binkert.org}
1386145Snate@binkert.org
1397054Snate@binkert.orgvoid
1407054Snate@binkert.orgSwitch::printStats(std::ostream& out) const
1416145Snate@binkert.org{
1429274Snilay@cs.wisc.edu    ccprintf(out, "switch_%d_inlinks: %d\n", m_id,
1437054Snate@binkert.org        m_perfect_switch_ptr->getInLinks());
1449274Snilay@cs.wisc.edu    ccprintf(out, "switch_%d_outlinks: %d\n", m_id,
1457054Snate@binkert.org        m_perfect_switch_ptr->getOutLinks());
1466145Snate@binkert.org
1477054Snate@binkert.org    // Average link utilizations
1487054Snate@binkert.org    double average_utilization = 0.0;
1497054Snate@binkert.org    int throttle_count = 0;
1506145Snate@binkert.org
1517054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1527054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[i];
1537054Snate@binkert.org        if (throttle_ptr) {
1547054Snate@binkert.org            average_utilization += throttle_ptr->getUtilization();
1557054Snate@binkert.org            throttle_count++;
1567054Snate@binkert.org        }
1576145Snate@binkert.org    }
1587054Snate@binkert.org    average_utilization =
1597054Snate@binkert.org        throttle_count == 0 ? 0 : average_utilization / throttle_count;
1606145Snate@binkert.org
1617054Snate@binkert.org    // Individual link utilizations
1629274Snilay@cs.wisc.edu    out << "links_utilized_percent_switch_" << m_id << ": "
1637054Snate@binkert.org        << average_utilization << endl;
1647054Snate@binkert.org
1657054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1667054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1677054Snate@binkert.org        if (throttle_ptr != NULL) {
1689274Snilay@cs.wisc.edu            out << "  links_utilized_percent_switch_" << m_id
1697054Snate@binkert.org                << "_link_" << link << ": "
1707054Snate@binkert.org                << throttle_ptr->getUtilization() << " bw: "
1717054Snate@binkert.org                << throttle_ptr->getLinkBandwidth()
1727054Snate@binkert.org                << " base_latency: " << throttle_ptr->getLatency() << endl;
1737054Snate@binkert.org        }
1746145Snate@binkert.org    }
1757054Snate@binkert.org    out << endl;
1766145Snate@binkert.org
1777054Snate@binkert.org    // Traffic breakdown
1787054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1797054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1807054Snate@binkert.org        if (!throttle_ptr)
1817054Snate@binkert.org            continue;
1827054Snate@binkert.org
1837454Snate@binkert.org        const vector<vector<int> >& message_counts =
1847054Snate@binkert.org            throttle_ptr->getCounters();
1857054Snate@binkert.org        for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) {
1867054Snate@binkert.org            MessageSizeType type = MessageSizeType(int_type);
1877454Snate@binkert.org            const vector<int> &mct = message_counts[type];
1887454Snate@binkert.org            int sum = accumulate(mct.begin(), mct.end(), 0);
1897054Snate@binkert.org            if (sum == 0)
1907054Snate@binkert.org                continue;
1917054Snate@binkert.org
1929274Snilay@cs.wisc.edu            out << "  outgoing_messages_switch_" << m_id
1937054Snate@binkert.org                << "_link_" << link << "_" << type << ": " << sum << " "
1949274Snilay@cs.wisc.edu                << sum * m_network_ptr->MessageSizeType_to_int(type)
1957454Snate@binkert.org                << " ";
1967454Snate@binkert.org            out << mct;
1977454Snate@binkert.org            out << " base_latency: "
1987054Snate@binkert.org                << throttle_ptr->getLatency() << endl;
1996145Snate@binkert.org        }
2006145Snate@binkert.org    }
2017054Snate@binkert.org    out << endl;
2026145Snate@binkert.org}
2036145Snate@binkert.org
2047054Snate@binkert.orgvoid
2057054Snate@binkert.orgSwitch::clearStats()
2066145Snate@binkert.org{
2077054Snate@binkert.org    m_perfect_switch_ptr->clearStats();
2087054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2097054Snate@binkert.org        if (m_throttles[i] != NULL)
2107054Snate@binkert.org            m_throttles[i]->clearStats();
2116145Snate@binkert.org    }
2126145Snate@binkert.org}
2136145Snate@binkert.org
2147054Snate@binkert.orgvoid
2157054Snate@binkert.orgSwitch::print(std::ostream& out) const
2166145Snate@binkert.org{
2177054Snate@binkert.org    // FIXME printing
2187054Snate@binkert.org    out << "[Switch]";
2196145Snate@binkert.org}
2209354Snilay@cs.wisc.edu
2219302Snilay@cs.wisc.edubool
2229302Snilay@cs.wisc.eduSwitch::functionalRead(Packet *pkt)
2239302Snilay@cs.wisc.edu{
2249302Snilay@cs.wisc.edu    // Access the buffers in the switch for performing a functional read
2259302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2269302Snilay@cs.wisc.edu        if (m_buffers_to_free[i]->functionalRead(pkt)) {
2279302Snilay@cs.wisc.edu            return true;
2289302Snilay@cs.wisc.edu        }
2299302Snilay@cs.wisc.edu    }
2309302Snilay@cs.wisc.edu    return false;
2319302Snilay@cs.wisc.edu}
2329302Snilay@cs.wisc.edu
2339302Snilay@cs.wisc.eduuint32_t
2349302Snilay@cs.wisc.eduSwitch::functionalWrite(Packet *pkt)
2359302Snilay@cs.wisc.edu{
2369302Snilay@cs.wisc.edu    // Access the buffers in the switch for performing a functional write
2379302Snilay@cs.wisc.edu    uint32_t num_functional_writes = 0;
2389302Snilay@cs.wisc.edu    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
2399302Snilay@cs.wisc.edu        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
2409302Snilay@cs.wisc.edu    }
2419302Snilay@cs.wisc.edu    return num_functional_writes;
2429302Snilay@cs.wisc.edu}
2439274Snilay@cs.wisc.edu
2449274Snilay@cs.wisc.eduSwitch *
2459274Snilay@cs.wisc.eduSwitchParams::create()
2469274Snilay@cs.wisc.edu{
2479274Snilay@cs.wisc.edu    return new Switch(this);
2489274Snilay@cs.wisc.edu}
249