Switch.cc revision 6285
1 2/* 3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/* 31 * Switch.cc 32 * 33 * Description: See Switch.hh 34 * 35 * $Id$ 36 * 37 */ 38 39 40#include "mem/ruby/network/simple/Switch.hh" 41#include "mem/ruby/network/simple/PerfectSwitch.hh" 42#include "mem/ruby/buffers/MessageBuffer.hh" 43#include "mem/ruby/network/simple/Throttle.hh" 44#include "mem/protocol/MessageSizeType.hh" 45#include "mem/ruby/network/Network.hh" 46#include "mem/protocol/Protocol.hh" 47 48Switch::Switch(SwitchID sid, SimpleNetwork* network_ptr) 49{ 50 m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr); 51 m_switch_id = sid; 52 m_throttles.setSize(0); 53} 54 55Switch::~Switch() 56{ 57 delete m_perfect_switch_ptr; 58 59 // Delete throttles (one per output port) 60 m_throttles.deletePointers(); 61 62 // Delete MessageBuffers 63 m_buffers_to_free.deletePointers(); 64} 65 66void Switch::addInPort(const Vector<MessageBuffer*>& in) 67{ 68 m_perfect_switch_ptr->addInPort(in); 69} 70 71void Switch::addOutPort(const Vector<MessageBuffer*>& out, const NetDest& routing_table_entry, int link_latency, int bw_multiplier) 72{ 73 Throttle* throttle_ptr = NULL; 74 75 // Create a throttle 76 throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency, bw_multiplier); 77 m_throttles.insertAtBottom(throttle_ptr); 78 79 // Create one buffer per vnet (these are intermediaryQueues) 80 Vector<MessageBuffer*> intermediateBuffers; 81 for (int i=0; i<out.size(); i++) { 82 MessageBuffer* buffer_ptr = new MessageBuffer; 83 // Make these queues ordered 84 buffer_ptr->setOrdering(true); 85 Network* net_ptr = RubySystem::getNetwork(); 86 if(net_ptr->getBufferSize() > 0) { 87 buffer_ptr->setSize(net_ptr->getBufferSize()); 88 } 89 intermediateBuffers.insertAtBottom(buffer_ptr); 90 m_buffers_to_free.insertAtBottom(buffer_ptr); 91 } 92 93 // Hook the queues to the PerfectSwitch 94 m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry); 95 96 // Hook the queues to the Throttle 97 throttle_ptr->addLinks(intermediateBuffers, out); 98 99} 100 101void Switch::clearRoutingTables() 102{ 103 m_perfect_switch_ptr->clearRoutingTables(); 104} 105 106void Switch::clearBuffers() 107{ 108 m_perfect_switch_ptr->clearBuffers(); 109 for (int i=0; i<m_throttles.size(); i++) { 110 if (m_throttles[i] != NULL) { 111 m_throttles[i]->clear(); 112 } 113 } 114} 115 116void Switch::reconfigureOutPort(const NetDest& routing_table_entry) 117{ 118 m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry); 119} 120 121const Throttle* Switch::getThrottle(LinkID link_number) const 122{ 123 assert(m_throttles[link_number] != NULL); 124 return m_throttles[link_number]; 125} 126 127const Vector<Throttle*>* Switch::getThrottles() const 128{ 129 return &m_throttles; 130} 131 132void Switch::printStats(ostream& out) const 133{ 134 out << "switch_" << m_switch_id << "_inlinks: " << m_perfect_switch_ptr->getInLinks() << endl; 135 out << "switch_" << m_switch_id << "_outlinks: " << m_perfect_switch_ptr->getOutLinks() << endl; 136 137 // Average link utilizations 138 double average_utilization = 0.0; 139 int throttle_count = 0; 140 141 for (int i=0; i<m_throttles.size(); i++) { 142 Throttle* throttle_ptr = m_throttles[i]; 143 if (throttle_ptr != NULL) { 144 average_utilization += throttle_ptr->getUtilization(); 145 throttle_count++; 146 } 147 } 148 average_utilization = (throttle_count == 0) ? 0 : average_utilization / float(throttle_count); 149 150 // Individual link utilizations 151 out << "links_utilized_percent_switch_" << m_switch_id << ": " << average_utilization << endl; 152 for (int link=0; link<m_throttles.size(); link++) { 153 Throttle* throttle_ptr = m_throttles[link]; 154 if (throttle_ptr != NULL) { 155 out << " links_utilized_percent_switch_" << m_switch_id << "_link_" << link << ": " 156 << throttle_ptr->getUtilization() << " bw: " << throttle_ptr->getLinkBandwidth() 157 << " base_latency: " << throttle_ptr->getLatency() << endl; 158 } 159 } 160 out << endl; 161 162 // Traffic breakdown 163 for (int link=0; link<m_throttles.size(); link++) { 164 Throttle* throttle_ptr = m_throttles[link]; 165 if (throttle_ptr != NULL) { 166 const Vector<Vector<int> >& message_counts = throttle_ptr->getCounters(); 167 for (int int_type=0; int_type<MessageSizeType_NUM; int_type++) { 168 MessageSizeType type = MessageSizeType(int_type); 169 int sum = message_counts[type].sum(); 170 if (sum != 0) { 171 out << " outgoing_messages_switch_" << m_switch_id << "_link_" << link << "_" << type 172 << ": " << sum << " " << sum * MessageSizeType_to_int(type) 173 << " " << message_counts[type] << " base_latency: " << throttle_ptr->getLatency() << endl; 174 } 175 } 176 } 177 } 178 out << endl; 179} 180 181void Switch::clearStats() 182{ 183 m_perfect_switch_ptr->clearStats(); 184 for (int i=0; i<m_throttles.size(); i++) { 185 if (m_throttles[i] != NULL) { 186 m_throttles[i]->clearStats(); 187 } 188 } 189} 190 191void Switch::printConfig(ostream& out) const 192{ 193 m_perfect_switch_ptr->printConfig(out); 194 for (int i=0; i<m_throttles.size(); i++) { 195 if (m_throttles[i] != NULL) { 196 m_throttles[i]->printConfig(out); 197 } 198 } 199} 200 201void Switch::print(ostream& out) const 202{ 203 // FIXME printing 204 out << "[Switch]"; 205} 206 207