Switch.cc revision 8645
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
446145Snate@binkert.orgSwitch::Switch(SwitchID sid, SimpleNetwork* network_ptr)
456145Snate@binkert.org{
467054Snate@binkert.org    m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr);
477054Snate@binkert.org    m_switch_id = sid;
486145Snate@binkert.org}
496145Snate@binkert.org
506145Snate@binkert.orgSwitch::~Switch()
516145Snate@binkert.org{
527054Snate@binkert.org    delete m_perfect_switch_ptr;
536145Snate@binkert.org
547054Snate@binkert.org    // Delete throttles (one per output port)
557454Snate@binkert.org    deletePointers(m_throttles);
566145Snate@binkert.org
577054Snate@binkert.org    // Delete MessageBuffers
587454Snate@binkert.org    deletePointers(m_buffers_to_free);
596145Snate@binkert.org}
606145Snate@binkert.org
617054Snate@binkert.orgvoid
627454Snate@binkert.orgSwitch::addInPort(const vector<MessageBuffer*>& in)
636145Snate@binkert.org{
647054Snate@binkert.org    m_perfect_switch_ptr->addInPort(in);
656145Snate@binkert.org}
666145Snate@binkert.org
677054Snate@binkert.orgvoid
687454Snate@binkert.orgSwitch::addOutPort(const vector<MessageBuffer*>& out,
697054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
706145Snate@binkert.org{
717054Snate@binkert.org    Throttle* throttle_ptr = NULL;
728259SBrad.Beckmann@amd.com    SimpleNetwork* net_ptr =
738259SBrad.Beckmann@amd.com        safe_cast<SimpleNetwork*>(RubySystem::getNetwork());
746145Snate@binkert.org
757054Snate@binkert.org    // Create a throttle
767054Snate@binkert.org    throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency,
778259SBrad.Beckmann@amd.com                                bw_multiplier, net_ptr->getEndpointBandwidth());
787454Snate@binkert.org    m_throttles.push_back(throttle_ptr);
796145Snate@binkert.org
807054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
817454Snate@binkert.org    vector<MessageBuffer*> intermediateBuffers;
827054Snate@binkert.org    for (int i = 0; i < out.size(); i++) {
837054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
847054Snate@binkert.org        // Make these queues ordered
857054Snate@binkert.org        buffer_ptr->setOrdering(true);
867054Snate@binkert.org        if (net_ptr->getBufferSize() > 0) {
877454Snate@binkert.org            buffer_ptr->resize(net_ptr->getBufferSize());
887054Snate@binkert.org        }
897454Snate@binkert.org        intermediateBuffers.push_back(buffer_ptr);
907454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
916145Snate@binkert.org  }
926145Snate@binkert.org
937054Snate@binkert.org    // Hook the queues to the PerfectSwitch
947054Snate@binkert.org    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
956145Snate@binkert.org
967054Snate@binkert.org    // Hook the queues to the Throttle
977054Snate@binkert.org    throttle_ptr->addLinks(intermediateBuffers, out);
986145Snate@binkert.org}
996145Snate@binkert.org
1007054Snate@binkert.orgvoid
1017054Snate@binkert.orgSwitch::clearRoutingTables()
1026145Snate@binkert.org{
1037054Snate@binkert.org    m_perfect_switch_ptr->clearRoutingTables();
1046145Snate@binkert.org}
1056145Snate@binkert.org
1067054Snate@binkert.orgvoid
1077054Snate@binkert.orgSwitch::clearBuffers()
1086145Snate@binkert.org{
1097054Snate@binkert.org    m_perfect_switch_ptr->clearBuffers();
1107054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1117054Snate@binkert.org        if (m_throttles[i] != NULL) {
1127054Snate@binkert.org            m_throttles[i]->clear();
1137054Snate@binkert.org        }
1146145Snate@binkert.org    }
1156145Snate@binkert.org}
1166145Snate@binkert.org
1177054Snate@binkert.orgvoid
1187054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
1196145Snate@binkert.org{
1207054Snate@binkert.org    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
1216145Snate@binkert.org}
1226145Snate@binkert.org
1237054Snate@binkert.orgconst Throttle*
1247054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1256145Snate@binkert.org{
1267054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1277054Snate@binkert.org    return m_throttles[link_number];
1286145Snate@binkert.org}
1296145Snate@binkert.org
1307454Snate@binkert.orgconst vector<Throttle*>*
1317054Snate@binkert.orgSwitch::getThrottles() const
1326145Snate@binkert.org{
1337054Snate@binkert.org    return &m_throttles;
1346145Snate@binkert.org}
1356145Snate@binkert.org
1367054Snate@binkert.orgvoid
1377054Snate@binkert.orgSwitch::printStats(std::ostream& out) const
1386145Snate@binkert.org{
1397054Snate@binkert.org    ccprintf(out, "switch_%d_inlinks: %d\n", m_switch_id,
1407054Snate@binkert.org        m_perfect_switch_ptr->getInLinks());
1417054Snate@binkert.org    ccprintf(out, "switch_%d_outlinks: %d\n", m_switch_id,
1427054Snate@binkert.org        m_perfect_switch_ptr->getOutLinks());
1436145Snate@binkert.org
1447054Snate@binkert.org    // Average link utilizations
1457054Snate@binkert.org    double average_utilization = 0.0;
1467054Snate@binkert.org    int throttle_count = 0;
1476145Snate@binkert.org
1487054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1497054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[i];
1507054Snate@binkert.org        if (throttle_ptr) {
1517054Snate@binkert.org            average_utilization += throttle_ptr->getUtilization();
1527054Snate@binkert.org            throttle_count++;
1537054Snate@binkert.org        }
1546145Snate@binkert.org    }
1557054Snate@binkert.org    average_utilization =
1567054Snate@binkert.org        throttle_count == 0 ? 0 : average_utilization / throttle_count;
1576145Snate@binkert.org
1587054Snate@binkert.org    // Individual link utilizations
1597054Snate@binkert.org    out << "links_utilized_percent_switch_" << m_switch_id << ": "
1607054Snate@binkert.org        << average_utilization << endl;
1617054Snate@binkert.org
1627054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1637054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1647054Snate@binkert.org        if (throttle_ptr != NULL) {
1657054Snate@binkert.org            out << "  links_utilized_percent_switch_" << m_switch_id
1667054Snate@binkert.org                << "_link_" << link << ": "
1677054Snate@binkert.org                << throttle_ptr->getUtilization() << " bw: "
1687054Snate@binkert.org                << throttle_ptr->getLinkBandwidth()
1697054Snate@binkert.org                << " base_latency: " << throttle_ptr->getLatency() << endl;
1707054Snate@binkert.org        }
1716145Snate@binkert.org    }
1727054Snate@binkert.org    out << endl;
1736145Snate@binkert.org
1747054Snate@binkert.org    // Traffic breakdown
1757054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1767054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1777054Snate@binkert.org        if (!throttle_ptr)
1787054Snate@binkert.org            continue;
1797054Snate@binkert.org
1807454Snate@binkert.org        const vector<vector<int> >& message_counts =
1817054Snate@binkert.org            throttle_ptr->getCounters();
1827054Snate@binkert.org        for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) {
1837054Snate@binkert.org            MessageSizeType type = MessageSizeType(int_type);
1847454Snate@binkert.org            const vector<int> &mct = message_counts[type];
1857454Snate@binkert.org            int sum = accumulate(mct.begin(), mct.end(), 0);
1867054Snate@binkert.org            if (sum == 0)
1877054Snate@binkert.org                continue;
1887054Snate@binkert.org
1897054Snate@binkert.org            out << "  outgoing_messages_switch_" << m_switch_id
1907054Snate@binkert.org                << "_link_" << link << "_" << type << ": " << sum << " "
1917054Snate@binkert.org                << sum * RubySystem::getNetwork()->MessageSizeType_to_int(type)
1927454Snate@binkert.org                << " ";
1937454Snate@binkert.org            out << mct;
1947454Snate@binkert.org            out << " base_latency: "
1957054Snate@binkert.org                << throttle_ptr->getLatency() << endl;
1966145Snate@binkert.org        }
1976145Snate@binkert.org    }
1987054Snate@binkert.org    out << endl;
1996145Snate@binkert.org}
2006145Snate@binkert.org
2017054Snate@binkert.orgvoid
2027054Snate@binkert.orgSwitch::clearStats()
2036145Snate@binkert.org{
2047054Snate@binkert.org    m_perfect_switch_ptr->clearStats();
2057054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2067054Snate@binkert.org        if (m_throttles[i] != NULL)
2077054Snate@binkert.org            m_throttles[i]->clearStats();
2086145Snate@binkert.org    }
2096145Snate@binkert.org}
2106145Snate@binkert.org
2117054Snate@binkert.orgvoid
2127054Snate@binkert.orgSwitch::printConfig(std::ostream& out) const
2136145Snate@binkert.org{
2147054Snate@binkert.org    m_perfect_switch_ptr->printConfig(out);
2157054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2167054Snate@binkert.org        if (m_throttles[i] != NULL)
2177054Snate@binkert.org            m_throttles[i]->printConfig(out);
2186145Snate@binkert.org    }
2196145Snate@binkert.org}
2206145Snate@binkert.org
2217054Snate@binkert.orgvoid
2227054Snate@binkert.orgSwitch::print(std::ostream& out) const
2236145Snate@binkert.org{
2247054Snate@binkert.org    // FIXME printing
2257054Snate@binkert.org    out << "[Switch]";
2266145Snate@binkert.org}
2276145Snate@binkert.org
228