Switch.cc revision 7055
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
377055Snate@binkert.orgusing namespace std;
387055Snate@binkert.org
396145Snate@binkert.orgSwitch::Switch(SwitchID sid, SimpleNetwork* network_ptr)
406145Snate@binkert.org{
417054Snate@binkert.org    m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr);
427054Snate@binkert.org    m_switch_id = sid;
437054Snate@binkert.org    m_throttles.setSize(0);
446145Snate@binkert.org}
456145Snate@binkert.org
466145Snate@binkert.orgSwitch::~Switch()
476145Snate@binkert.org{
487054Snate@binkert.org    delete m_perfect_switch_ptr;
496145Snate@binkert.org
507054Snate@binkert.org    // Delete throttles (one per output port)
517054Snate@binkert.org    m_throttles.deletePointers();
526145Snate@binkert.org
537054Snate@binkert.org    // Delete MessageBuffers
547054Snate@binkert.org    m_buffers_to_free.deletePointers();
556145Snate@binkert.org}
566145Snate@binkert.org
577054Snate@binkert.orgvoid
587054Snate@binkert.orgSwitch::addInPort(const Vector<MessageBuffer*>& in)
596145Snate@binkert.org{
607054Snate@binkert.org    m_perfect_switch_ptr->addInPort(in);
616145Snate@binkert.org}
626145Snate@binkert.org
637054Snate@binkert.orgvoid
647054Snate@binkert.orgSwitch::addOutPort(const Vector<MessageBuffer*>& out,
657054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
666145Snate@binkert.org{
677054Snate@binkert.org    Throttle* throttle_ptr = NULL;
686145Snate@binkert.org
697054Snate@binkert.org    // Create a throttle
707054Snate@binkert.org    throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency,
717054Snate@binkert.org        bw_multiplier);
727054Snate@binkert.org    m_throttles.insertAtBottom(throttle_ptr);
736145Snate@binkert.org
747054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
757054Snate@binkert.org    Vector<MessageBuffer*> intermediateBuffers;
767054Snate@binkert.org    for (int i = 0; i < out.size(); i++) {
777054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
787054Snate@binkert.org        // Make these queues ordered
797054Snate@binkert.org        buffer_ptr->setOrdering(true);
807054Snate@binkert.org        Network* net_ptr = RubySystem::getNetwork();
817054Snate@binkert.org        if (net_ptr->getBufferSize() > 0) {
827054Snate@binkert.org            buffer_ptr->setSize(net_ptr->getBufferSize());
837054Snate@binkert.org        }
847054Snate@binkert.org        intermediateBuffers.insertAtBottom(buffer_ptr);
857054Snate@binkert.org        m_buffers_to_free.insertAtBottom(buffer_ptr);
866145Snate@binkert.org  }
876145Snate@binkert.org
887054Snate@binkert.org    // Hook the queues to the PerfectSwitch
897054Snate@binkert.org    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
906145Snate@binkert.org
917054Snate@binkert.org    // Hook the queues to the Throttle
927054Snate@binkert.org    throttle_ptr->addLinks(intermediateBuffers, out);
936145Snate@binkert.org}
946145Snate@binkert.org
957054Snate@binkert.orgvoid
967054Snate@binkert.orgSwitch::clearRoutingTables()
976145Snate@binkert.org{
987054Snate@binkert.org    m_perfect_switch_ptr->clearRoutingTables();
996145Snate@binkert.org}
1006145Snate@binkert.org
1017054Snate@binkert.orgvoid
1027054Snate@binkert.orgSwitch::clearBuffers()
1036145Snate@binkert.org{
1047054Snate@binkert.org    m_perfect_switch_ptr->clearBuffers();
1057054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1067054Snate@binkert.org        if (m_throttles[i] != NULL) {
1077054Snate@binkert.org            m_throttles[i]->clear();
1087054Snate@binkert.org        }
1096145Snate@binkert.org    }
1106145Snate@binkert.org}
1116145Snate@binkert.org
1127054Snate@binkert.orgvoid
1137054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
1146145Snate@binkert.org{
1157054Snate@binkert.org    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
1166145Snate@binkert.org}
1176145Snate@binkert.org
1187054Snate@binkert.orgconst Throttle*
1197054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1206145Snate@binkert.org{
1217054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1227054Snate@binkert.org    return m_throttles[link_number];
1236145Snate@binkert.org}
1246145Snate@binkert.org
1257054Snate@binkert.orgconst Vector<Throttle*>*
1267054Snate@binkert.orgSwitch::getThrottles() const
1276145Snate@binkert.org{
1287054Snate@binkert.org    return &m_throttles;
1296145Snate@binkert.org}
1306145Snate@binkert.org
1317054Snate@binkert.orgvoid
1327054Snate@binkert.orgSwitch::printStats(std::ostream& out) const
1336145Snate@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