Switch.cc revision 7054
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
297054Snate@binkert.org#include "mem/protocol/MessageSizeType.hh"
307054Snate@binkert.org#include "mem/protocol/Protocol.hh"
317054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
327054Snate@binkert.org#include "mem/ruby/network/Network.hh"
337054Snate@binkert.org#include "mem/ruby/network/simple/PerfectSwitch.hh"
346154Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
356154Snate@binkert.org#include "mem/ruby/network/simple/Throttle.hh"
366145Snate@binkert.org
376145Snate@binkert.orgSwitch::Switch(SwitchID sid, SimpleNetwork* network_ptr)
386145Snate@binkert.org{
397054Snate@binkert.org    m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr);
407054Snate@binkert.org    m_switch_id = sid;
417054Snate@binkert.org    m_throttles.setSize(0);
426145Snate@binkert.org}
436145Snate@binkert.org
446145Snate@binkert.orgSwitch::~Switch()
456145Snate@binkert.org{
467054Snate@binkert.org    delete m_perfect_switch_ptr;
476145Snate@binkert.org
487054Snate@binkert.org    // Delete throttles (one per output port)
497054Snate@binkert.org    m_throttles.deletePointers();
506145Snate@binkert.org
517054Snate@binkert.org    // Delete MessageBuffers
527054Snate@binkert.org    m_buffers_to_free.deletePointers();
536145Snate@binkert.org}
546145Snate@binkert.org
557054Snate@binkert.orgvoid
567054Snate@binkert.orgSwitch::addInPort(const Vector<MessageBuffer*>& in)
576145Snate@binkert.org{
587054Snate@binkert.org    m_perfect_switch_ptr->addInPort(in);
596145Snate@binkert.org}
606145Snate@binkert.org
617054Snate@binkert.orgvoid
627054Snate@binkert.orgSwitch::addOutPort(const Vector<MessageBuffer*>& out,
637054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
646145Snate@binkert.org{
657054Snate@binkert.org    Throttle* throttle_ptr = NULL;
666145Snate@binkert.org
677054Snate@binkert.org    // Create a throttle
687054Snate@binkert.org    throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency,
697054Snate@binkert.org        bw_multiplier);
707054Snate@binkert.org    m_throttles.insertAtBottom(throttle_ptr);
716145Snate@binkert.org
727054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
737054Snate@binkert.org    Vector<MessageBuffer*> intermediateBuffers;
747054Snate@binkert.org    for (int i = 0; i < out.size(); i++) {
757054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
767054Snate@binkert.org        // Make these queues ordered
777054Snate@binkert.org        buffer_ptr->setOrdering(true);
787054Snate@binkert.org        Network* net_ptr = RubySystem::getNetwork();
797054Snate@binkert.org        if (net_ptr->getBufferSize() > 0) {
807054Snate@binkert.org            buffer_ptr->setSize(net_ptr->getBufferSize());
817054Snate@binkert.org        }
827054Snate@binkert.org        intermediateBuffers.insertAtBottom(buffer_ptr);
837054Snate@binkert.org        m_buffers_to_free.insertAtBottom(buffer_ptr);
846145Snate@binkert.org  }
856145Snate@binkert.org
867054Snate@binkert.org    // Hook the queues to the PerfectSwitch
877054Snate@binkert.org    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
886145Snate@binkert.org
897054Snate@binkert.org    // Hook the queues to the Throttle
907054Snate@binkert.org    throttle_ptr->addLinks(intermediateBuffers, out);
916145Snate@binkert.org}
926145Snate@binkert.org
937054Snate@binkert.orgvoid
947054Snate@binkert.orgSwitch::clearRoutingTables()
956145Snate@binkert.org{
967054Snate@binkert.org    m_perfect_switch_ptr->clearRoutingTables();
976145Snate@binkert.org}
986145Snate@binkert.org
997054Snate@binkert.orgvoid
1007054Snate@binkert.orgSwitch::clearBuffers()
1016145Snate@binkert.org{
1027054Snate@binkert.org    m_perfect_switch_ptr->clearBuffers();
1037054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1047054Snate@binkert.org        if (m_throttles[i] != NULL) {
1057054Snate@binkert.org            m_throttles[i]->clear();
1067054Snate@binkert.org        }
1076145Snate@binkert.org    }
1086145Snate@binkert.org}
1096145Snate@binkert.org
1107054Snate@binkert.orgvoid
1117054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
1126145Snate@binkert.org{
1137054Snate@binkert.org    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
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.orgconst Vector<Throttle*>*
1247054Snate@binkert.orgSwitch::getThrottles() const
1256145Snate@binkert.org{
1267054Snate@binkert.org    return &m_throttles;
1276145Snate@binkert.org}
1286145Snate@binkert.org
1297054Snate@binkert.orgvoid
1307054Snate@binkert.orgSwitch::printStats(std::ostream& out) const
1316145Snate@binkert.org{
1327054Snate@binkert.org    using namespace std;
1337002Snate@binkert.org
1347054Snate@binkert.org    ccprintf(out, "switch_%d_inlinks: %d\n", m_switch_id,
1357054Snate@binkert.org        m_perfect_switch_ptr->getInLinks());
1367054Snate@binkert.org    ccprintf(out, "switch_%d_outlinks: %d\n", m_switch_id,
1377054Snate@binkert.org        m_perfect_switch_ptr->getOutLinks());
1386145Snate@binkert.org
1397054Snate@binkert.org    // Average link utilizations
1407054Snate@binkert.org    double average_utilization = 0.0;
1417054Snate@binkert.org    int throttle_count = 0;
1426145Snate@binkert.org
1437054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1447054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[i];
1457054Snate@binkert.org        if (throttle_ptr) {
1467054Snate@binkert.org            average_utilization += throttle_ptr->getUtilization();
1477054Snate@binkert.org            throttle_count++;
1487054Snate@binkert.org        }
1496145Snate@binkert.org    }
1507054Snate@binkert.org    average_utilization =
1517054Snate@binkert.org        throttle_count == 0 ? 0 : average_utilization / throttle_count;
1526145Snate@binkert.org
1537054Snate@binkert.org    // Individual link utilizations
1547054Snate@binkert.org    out << "links_utilized_percent_switch_" << m_switch_id << ": "
1557054Snate@binkert.org        << average_utilization << endl;
1567054Snate@binkert.org
1577054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1587054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1597054Snate@binkert.org        if (throttle_ptr != NULL) {
1607054Snate@binkert.org            out << "  links_utilized_percent_switch_" << m_switch_id
1617054Snate@binkert.org                << "_link_" << link << ": "
1627054Snate@binkert.org                << throttle_ptr->getUtilization() << " bw: "
1637054Snate@binkert.org                << throttle_ptr->getLinkBandwidth()
1647054Snate@binkert.org                << " base_latency: " << throttle_ptr->getLatency() << endl;
1657054Snate@binkert.org        }
1666145Snate@binkert.org    }
1677054Snate@binkert.org    out << endl;
1686145Snate@binkert.org
1697054Snate@binkert.org    // Traffic breakdown
1707054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1717054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1727054Snate@binkert.org        if (!throttle_ptr)
1737054Snate@binkert.org            continue;
1747054Snate@binkert.org
1757054Snate@binkert.org        const Vector<Vector<int> >& message_counts =
1767054Snate@binkert.org            throttle_ptr->getCounters();
1777054Snate@binkert.org        for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) {
1787054Snate@binkert.org            MessageSizeType type = MessageSizeType(int_type);
1797054Snate@binkert.org            int sum = message_counts[type].sum();
1807054Snate@binkert.org            if (sum == 0)
1817054Snate@binkert.org                continue;
1827054Snate@binkert.org
1837054Snate@binkert.org            out << "  outgoing_messages_switch_" << m_switch_id
1847054Snate@binkert.org                << "_link_" << link << "_" << type << ": " << sum << " "
1857054Snate@binkert.org                << sum * RubySystem::getNetwork()->MessageSizeType_to_int(type)
1867054Snate@binkert.org                << " " << message_counts[type] << " base_latency: "
1877054Snate@binkert.org                << throttle_ptr->getLatency() << endl;
1886145Snate@binkert.org        }
1896145Snate@binkert.org    }
1907054Snate@binkert.org    out << endl;
1916145Snate@binkert.org}
1926145Snate@binkert.org
1937054Snate@binkert.orgvoid
1947054Snate@binkert.orgSwitch::clearStats()
1956145Snate@binkert.org{
1967054Snate@binkert.org    m_perfect_switch_ptr->clearStats();
1977054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1987054Snate@binkert.org        if (m_throttles[i] != NULL)
1997054Snate@binkert.org            m_throttles[i]->clearStats();
2006145Snate@binkert.org    }
2016145Snate@binkert.org}
2026145Snate@binkert.org
2037054Snate@binkert.orgvoid
2047054Snate@binkert.orgSwitch::printConfig(std::ostream& out) const
2056145Snate@binkert.org{
2067054Snate@binkert.org    m_perfect_switch_ptr->printConfig(out);
2077054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2087054Snate@binkert.org        if (m_throttles[i] != NULL)
2097054Snate@binkert.org            m_throttles[i]->printConfig(out);
2106145Snate@binkert.org    }
2116145Snate@binkert.org}
2126145Snate@binkert.org
2137054Snate@binkert.orgvoid
2147054Snate@binkert.orgSwitch::print(std::ostream& out) const
2156145Snate@binkert.org{
2167054Snate@binkert.org    // FIXME printing
2177054Snate@binkert.org    out << "[Switch]";
2186145Snate@binkert.org}
2196145Snate@binkert.org
220