Switch.cc revision 7454
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
317454Snate@binkert.org#include "base/stl_helpers.hh"
327054Snate@binkert.org#include "mem/protocol/MessageSizeType.hh"
337054Snate@binkert.org#include "mem/protocol/Protocol.hh"
347054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
357054Snate@binkert.org#include "mem/ruby/network/Network.hh"
367054Snate@binkert.org#include "mem/ruby/network/simple/PerfectSwitch.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;
726145Snate@binkert.org
737054Snate@binkert.org    // Create a throttle
747054Snate@binkert.org    throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency,
757054Snate@binkert.org        bw_multiplier);
767454Snate@binkert.org    m_throttles.push_back(throttle_ptr);
776145Snate@binkert.org
787054Snate@binkert.org    // Create one buffer per vnet (these are intermediaryQueues)
797454Snate@binkert.org    vector<MessageBuffer*> intermediateBuffers;
807054Snate@binkert.org    for (int i = 0; i < out.size(); i++) {
817054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
827054Snate@binkert.org        // Make these queues ordered
837054Snate@binkert.org        buffer_ptr->setOrdering(true);
847054Snate@binkert.org        Network* net_ptr = RubySystem::getNetwork();
857054Snate@binkert.org        if (net_ptr->getBufferSize() > 0) {
867454Snate@binkert.org            buffer_ptr->resize(net_ptr->getBufferSize());
877054Snate@binkert.org        }
887454Snate@binkert.org        intermediateBuffers.push_back(buffer_ptr);
897454Snate@binkert.org        m_buffers_to_free.push_back(buffer_ptr);
906145Snate@binkert.org  }
916145Snate@binkert.org
927054Snate@binkert.org    // Hook the queues to the PerfectSwitch
937054Snate@binkert.org    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
946145Snate@binkert.org
957054Snate@binkert.org    // Hook the queues to the Throttle
967054Snate@binkert.org    throttle_ptr->addLinks(intermediateBuffers, out);
976145Snate@binkert.org}
986145Snate@binkert.org
997054Snate@binkert.orgvoid
1007054Snate@binkert.orgSwitch::clearRoutingTables()
1016145Snate@binkert.org{
1027054Snate@binkert.org    m_perfect_switch_ptr->clearRoutingTables();
1036145Snate@binkert.org}
1046145Snate@binkert.org
1057054Snate@binkert.orgvoid
1067054Snate@binkert.orgSwitch::clearBuffers()
1076145Snate@binkert.org{
1087054Snate@binkert.org    m_perfect_switch_ptr->clearBuffers();
1097054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1107054Snate@binkert.org        if (m_throttles[i] != NULL) {
1117054Snate@binkert.org            m_throttles[i]->clear();
1127054Snate@binkert.org        }
1136145Snate@binkert.org    }
1146145Snate@binkert.org}
1156145Snate@binkert.org
1167054Snate@binkert.orgvoid
1177054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
1186145Snate@binkert.org{
1197054Snate@binkert.org    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
1206145Snate@binkert.org}
1216145Snate@binkert.org
1227054Snate@binkert.orgconst Throttle*
1237054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const
1246145Snate@binkert.org{
1257054Snate@binkert.org    assert(m_throttles[link_number] != NULL);
1267054Snate@binkert.org    return m_throttles[link_number];
1276145Snate@binkert.org}
1286145Snate@binkert.org
1297454Snate@binkert.orgconst vector<Throttle*>*
1307054Snate@binkert.orgSwitch::getThrottles() const
1316145Snate@binkert.org{
1327054Snate@binkert.org    return &m_throttles;
1336145Snate@binkert.org}
1346145Snate@binkert.org
1357054Snate@binkert.orgvoid
1367054Snate@binkert.orgSwitch::printStats(std::ostream& out) const
1376145Snate@binkert.org{
1387054Snate@binkert.org    ccprintf(out, "switch_%d_inlinks: %d\n", m_switch_id,
1397054Snate@binkert.org        m_perfect_switch_ptr->getInLinks());
1407054Snate@binkert.org    ccprintf(out, "switch_%d_outlinks: %d\n", m_switch_id,
1417054Snate@binkert.org        m_perfect_switch_ptr->getOutLinks());
1426145Snate@binkert.org
1437054Snate@binkert.org    // Average link utilizations
1447054Snate@binkert.org    double average_utilization = 0.0;
1457054Snate@binkert.org    int throttle_count = 0;
1466145Snate@binkert.org
1477054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
1487054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[i];
1497054Snate@binkert.org        if (throttle_ptr) {
1507054Snate@binkert.org            average_utilization += throttle_ptr->getUtilization();
1517054Snate@binkert.org            throttle_count++;
1527054Snate@binkert.org        }
1536145Snate@binkert.org    }
1547054Snate@binkert.org    average_utilization =
1557054Snate@binkert.org        throttle_count == 0 ? 0 : average_utilization / throttle_count;
1566145Snate@binkert.org
1577054Snate@binkert.org    // Individual link utilizations
1587054Snate@binkert.org    out << "links_utilized_percent_switch_" << m_switch_id << ": "
1597054Snate@binkert.org        << average_utilization << endl;
1607054Snate@binkert.org
1617054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1627054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1637054Snate@binkert.org        if (throttle_ptr != NULL) {
1647054Snate@binkert.org            out << "  links_utilized_percent_switch_" << m_switch_id
1657054Snate@binkert.org                << "_link_" << link << ": "
1667054Snate@binkert.org                << throttle_ptr->getUtilization() << " bw: "
1677054Snate@binkert.org                << throttle_ptr->getLinkBandwidth()
1687054Snate@binkert.org                << " base_latency: " << throttle_ptr->getLatency() << endl;
1697054Snate@binkert.org        }
1706145Snate@binkert.org    }
1717054Snate@binkert.org    out << endl;
1726145Snate@binkert.org
1737054Snate@binkert.org    // Traffic breakdown
1747054Snate@binkert.org    for (int link = 0; link < m_throttles.size(); link++) {
1757054Snate@binkert.org        Throttle* throttle_ptr = m_throttles[link];
1767054Snate@binkert.org        if (!throttle_ptr)
1777054Snate@binkert.org            continue;
1787054Snate@binkert.org
1797454Snate@binkert.org        const vector<vector<int> >& message_counts =
1807054Snate@binkert.org            throttle_ptr->getCounters();
1817054Snate@binkert.org        for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) {
1827054Snate@binkert.org            MessageSizeType type = MessageSizeType(int_type);
1837454Snate@binkert.org            const vector<int> &mct = message_counts[type];
1847454Snate@binkert.org            int sum = accumulate(mct.begin(), mct.end(), 0);
1857054Snate@binkert.org            if (sum == 0)
1867054Snate@binkert.org                continue;
1877054Snate@binkert.org
1887054Snate@binkert.org            out << "  outgoing_messages_switch_" << m_switch_id
1897054Snate@binkert.org                << "_link_" << link << "_" << type << ": " << sum << " "
1907054Snate@binkert.org                << sum * RubySystem::getNetwork()->MessageSizeType_to_int(type)
1917454Snate@binkert.org                << " ";
1927454Snate@binkert.org            out << mct;
1937454Snate@binkert.org            out << " base_latency: "
1947054Snate@binkert.org                << throttle_ptr->getLatency() << endl;
1956145Snate@binkert.org        }
1966145Snate@binkert.org    }
1977054Snate@binkert.org    out << endl;
1986145Snate@binkert.org}
1996145Snate@binkert.org
2007054Snate@binkert.orgvoid
2017054Snate@binkert.orgSwitch::clearStats()
2026145Snate@binkert.org{
2037054Snate@binkert.org    m_perfect_switch_ptr->clearStats();
2047054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2057054Snate@binkert.org        if (m_throttles[i] != NULL)
2067054Snate@binkert.org            m_throttles[i]->clearStats();
2076145Snate@binkert.org    }
2086145Snate@binkert.org}
2096145Snate@binkert.org
2107054Snate@binkert.orgvoid
2117054Snate@binkert.orgSwitch::printConfig(std::ostream& out) const
2126145Snate@binkert.org{
2137054Snate@binkert.org    m_perfect_switch_ptr->printConfig(out);
2147054Snate@binkert.org    for (int i = 0; i < m_throttles.size(); i++) {
2157054Snate@binkert.org        if (m_throttles[i] != NULL)
2167054Snate@binkert.org            m_throttles[i]->printConfig(out);
2176145Snate@binkert.org    }
2186145Snate@binkert.org}
2196145Snate@binkert.org
2207054Snate@binkert.orgvoid
2217054Snate@binkert.orgSwitch::print(std::ostream& out) const
2226145Snate@binkert.org{
2237054Snate@binkert.org    // FIXME printing
2247054Snate@binkert.org    out << "[Switch]";
2256145Snate@binkert.org}
2266145Snate@binkert.org
227