Switch.cc revision 9508
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{ 709508Snilay@cs.wisc.edu m_perfect_switch_ptr->addInPort(in); 719508Snilay@cs.wisc.edu 729508Snilay@cs.wisc.edu for (int i = 0; i < in.size(); i++) { 739508Snilay@cs.wisc.edu in[i]->setReceiver(this); 749508Snilay@cs.wisc.edu } 756145Snate@binkert.org} 766145Snate@binkert.org 777054Snate@binkert.orgvoid 787454Snate@binkert.orgSwitch::addOutPort(const vector<MessageBuffer*>& out, 799499Snilay@cs.wisc.edu const NetDest& routing_table_entry, Cycles link_latency, int bw_multiplier) 806145Snate@binkert.org{ 817054Snate@binkert.org // Create a throttle 829274Snilay@cs.wisc.edu Throttle* throttle_ptr = new Throttle(m_id, m_throttles.size(), 839499Snilay@cs.wisc.edu link_latency, bw_multiplier, m_network_ptr->getEndpointBandwidth(), 849499Snilay@cs.wisc.edu this); 857454Snate@binkert.org m_throttles.push_back(throttle_ptr); 866145Snate@binkert.org 877054Snate@binkert.org // Create one buffer per vnet (these are intermediaryQueues) 887454Snate@binkert.org vector<MessageBuffer*> intermediateBuffers; 897054Snate@binkert.org for (int i = 0; i < out.size(); i++) { 909508Snilay@cs.wisc.edu out[i]->setSender(this); 919508Snilay@cs.wisc.edu 927054Snate@binkert.org MessageBuffer* buffer_ptr = new MessageBuffer; 937054Snate@binkert.org // Make these queues ordered 947054Snate@binkert.org buffer_ptr->setOrdering(true); 959274Snilay@cs.wisc.edu if (m_network_ptr->getBufferSize() > 0) { 969274Snilay@cs.wisc.edu buffer_ptr->resize(m_network_ptr->getBufferSize()); 977054Snate@binkert.org } 989508Snilay@cs.wisc.edu 997454Snate@binkert.org intermediateBuffers.push_back(buffer_ptr); 1007454Snate@binkert.org m_buffers_to_free.push_back(buffer_ptr); 1019508Snilay@cs.wisc.edu 1029508Snilay@cs.wisc.edu buffer_ptr->setSender(this); 1039508Snilay@cs.wisc.edu buffer_ptr->setReceiver(this); 1049274Snilay@cs.wisc.edu } 1056145Snate@binkert.org 1067054Snate@binkert.org // Hook the queues to the PerfectSwitch 1077054Snate@binkert.org m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry); 1086145Snate@binkert.org 1097054Snate@binkert.org // Hook the queues to the Throttle 1109508Snilay@cs.wisc.edu throttle_ptr->addLinks(intermediateBuffers, out); 1116145Snate@binkert.org} 1126145Snate@binkert.org 1137054Snate@binkert.orgvoid 1147054Snate@binkert.orgSwitch::clearRoutingTables() 1156145Snate@binkert.org{ 1167054Snate@binkert.org m_perfect_switch_ptr->clearRoutingTables(); 1176145Snate@binkert.org} 1186145Snate@binkert.org 1197054Snate@binkert.orgvoid 1207054Snate@binkert.orgSwitch::clearBuffers() 1216145Snate@binkert.org{ 1227054Snate@binkert.org m_perfect_switch_ptr->clearBuffers(); 1237054Snate@binkert.org for (int i = 0; i < m_throttles.size(); i++) { 1247054Snate@binkert.org if (m_throttles[i] != NULL) { 1257054Snate@binkert.org m_throttles[i]->clear(); 1267054Snate@binkert.org } 1276145Snate@binkert.org } 1286145Snate@binkert.org} 1296145Snate@binkert.org 1307054Snate@binkert.orgvoid 1317054Snate@binkert.orgSwitch::reconfigureOutPort(const NetDest& routing_table_entry) 1326145Snate@binkert.org{ 1337054Snate@binkert.org m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry); 1346145Snate@binkert.org} 1356145Snate@binkert.org 1367054Snate@binkert.orgconst Throttle* 1377054Snate@binkert.orgSwitch::getThrottle(LinkID link_number) const 1386145Snate@binkert.org{ 1397054Snate@binkert.org assert(m_throttles[link_number] != NULL); 1407054Snate@binkert.org return m_throttles[link_number]; 1416145Snate@binkert.org} 1426145Snate@binkert.org 1437454Snate@binkert.orgconst vector<Throttle*>* 1447054Snate@binkert.orgSwitch::getThrottles() const 1456145Snate@binkert.org{ 1467054Snate@binkert.org return &m_throttles; 1476145Snate@binkert.org} 1486145Snate@binkert.org 1497054Snate@binkert.orgvoid 1507054Snate@binkert.orgSwitch::printStats(std::ostream& out) const 1516145Snate@binkert.org{ 1529274Snilay@cs.wisc.edu ccprintf(out, "switch_%d_inlinks: %d\n", m_id, 1537054Snate@binkert.org m_perfect_switch_ptr->getInLinks()); 1549274Snilay@cs.wisc.edu ccprintf(out, "switch_%d_outlinks: %d\n", m_id, 1557054Snate@binkert.org m_perfect_switch_ptr->getOutLinks()); 1566145Snate@binkert.org 1577054Snate@binkert.org // Average link utilizations 1587054Snate@binkert.org double average_utilization = 0.0; 1597054Snate@binkert.org int throttle_count = 0; 1606145Snate@binkert.org 1617054Snate@binkert.org for (int i = 0; i < m_throttles.size(); i++) { 1627054Snate@binkert.org Throttle* throttle_ptr = m_throttles[i]; 1637054Snate@binkert.org if (throttle_ptr) { 1647054Snate@binkert.org average_utilization += throttle_ptr->getUtilization(); 1657054Snate@binkert.org throttle_count++; 1667054Snate@binkert.org } 1676145Snate@binkert.org } 1687054Snate@binkert.org average_utilization = 1697054Snate@binkert.org throttle_count == 0 ? 0 : average_utilization / throttle_count; 1706145Snate@binkert.org 1717054Snate@binkert.org // Individual link utilizations 1729274Snilay@cs.wisc.edu out << "links_utilized_percent_switch_" << m_id << ": " 1737054Snate@binkert.org << average_utilization << endl; 1747054Snate@binkert.org 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 != NULL) { 1789274Snilay@cs.wisc.edu out << " links_utilized_percent_switch_" << m_id 1797054Snate@binkert.org << "_link_" << link << ": " 1807054Snate@binkert.org << throttle_ptr->getUtilization() << " bw: " 1817054Snate@binkert.org << throttle_ptr->getLinkBandwidth() 1827054Snate@binkert.org << " base_latency: " << throttle_ptr->getLatency() << endl; 1837054Snate@binkert.org } 1846145Snate@binkert.org } 1857054Snate@binkert.org out << endl; 1866145Snate@binkert.org 1877054Snate@binkert.org // Traffic breakdown 1887054Snate@binkert.org for (int link = 0; link < m_throttles.size(); link++) { 1897054Snate@binkert.org Throttle* throttle_ptr = m_throttles[link]; 1907054Snate@binkert.org if (!throttle_ptr) 1917054Snate@binkert.org continue; 1927054Snate@binkert.org 1937454Snate@binkert.org const vector<vector<int> >& message_counts = 1947054Snate@binkert.org throttle_ptr->getCounters(); 1957054Snate@binkert.org for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) { 1967054Snate@binkert.org MessageSizeType type = MessageSizeType(int_type); 1977454Snate@binkert.org const vector<int> &mct = message_counts[type]; 1987454Snate@binkert.org int sum = accumulate(mct.begin(), mct.end(), 0); 1997054Snate@binkert.org if (sum == 0) 2007054Snate@binkert.org continue; 2017054Snate@binkert.org 2029274Snilay@cs.wisc.edu out << " outgoing_messages_switch_" << m_id 2037054Snate@binkert.org << "_link_" << link << "_" << type << ": " << sum << " " 2049274Snilay@cs.wisc.edu << sum * m_network_ptr->MessageSizeType_to_int(type) 2057454Snate@binkert.org << " "; 2067454Snate@binkert.org out << mct; 2077454Snate@binkert.org out << " base_latency: " 2087054Snate@binkert.org << throttle_ptr->getLatency() << endl; 2096145Snate@binkert.org } 2106145Snate@binkert.org } 2117054Snate@binkert.org out << endl; 2126145Snate@binkert.org} 2136145Snate@binkert.org 2147054Snate@binkert.orgvoid 2157054Snate@binkert.orgSwitch::clearStats() 2166145Snate@binkert.org{ 2177054Snate@binkert.org m_perfect_switch_ptr->clearStats(); 2187054Snate@binkert.org for (int i = 0; i < m_throttles.size(); i++) { 2197054Snate@binkert.org if (m_throttles[i] != NULL) 2207054Snate@binkert.org m_throttles[i]->clearStats(); 2216145Snate@binkert.org } 2226145Snate@binkert.org} 2236145Snate@binkert.org 2247054Snate@binkert.orgvoid 2257054Snate@binkert.orgSwitch::print(std::ostream& out) const 2266145Snate@binkert.org{ 2277054Snate@binkert.org // FIXME printing 2287054Snate@binkert.org out << "[Switch]"; 2296145Snate@binkert.org} 2309354Snilay@cs.wisc.edu 2319302Snilay@cs.wisc.edubool 2329302Snilay@cs.wisc.eduSwitch::functionalRead(Packet *pkt) 2339302Snilay@cs.wisc.edu{ 2349302Snilay@cs.wisc.edu // Access the buffers in the switch for performing a functional read 2359302Snilay@cs.wisc.edu for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) { 2369302Snilay@cs.wisc.edu if (m_buffers_to_free[i]->functionalRead(pkt)) { 2379302Snilay@cs.wisc.edu return true; 2389302Snilay@cs.wisc.edu } 2399302Snilay@cs.wisc.edu } 2409302Snilay@cs.wisc.edu return false; 2419302Snilay@cs.wisc.edu} 2429302Snilay@cs.wisc.edu 2439302Snilay@cs.wisc.eduuint32_t 2449302Snilay@cs.wisc.eduSwitch::functionalWrite(Packet *pkt) 2459302Snilay@cs.wisc.edu{ 2469302Snilay@cs.wisc.edu // Access the buffers in the switch for performing a functional write 2479302Snilay@cs.wisc.edu uint32_t num_functional_writes = 0; 2489302Snilay@cs.wisc.edu for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) { 2499302Snilay@cs.wisc.edu num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt); 2509302Snilay@cs.wisc.edu } 2519302Snilay@cs.wisc.edu return num_functional_writes; 2529302Snilay@cs.wisc.edu} 2539274Snilay@cs.wisc.edu 2549274Snilay@cs.wisc.eduSwitch * 2559274Snilay@cs.wisc.eduSwitchParams::create() 2569274Snilay@cs.wisc.edu{ 2579274Snilay@cs.wisc.edu return new Switch(this); 2589274Snilay@cs.wisc.edu} 259