Throttle.cc revision 8608
1/* 2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <cassert> 30 31#include "base/cprintf.hh" 32#include "debug/RubyNetwork.hh" 33#include "mem/ruby/buffers/MessageBuffer.hh" 34#include "mem/ruby/network/simple/Throttle.hh" 35#include "mem/ruby/network/Network.hh" 36#include "mem/ruby/slicc_interface/NetworkMessage.hh" 37#include "mem/ruby/system/System.hh" 38 39using namespace std; 40 41const int HIGH_RANGE = 256; 42const int ADJUST_INTERVAL = 50000; 43const int MESSAGE_SIZE_MULTIPLIER = 1000; 44//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems 45const int BROADCAST_SCALING = 1; 46const int PRIORITY_SWITCH_LIMIT = 128; 47 48static int network_message_to_size(NetworkMessage* net_msg_ptr); 49 50Throttle::Throttle(int sID, NodeID node, int link_latency, 51 int link_bandwidth_multiplier, int endpoint_bandwidth) 52{ 53 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth); 54 m_sID = sID; 55} 56 57Throttle::Throttle(NodeID node, int link_latency, 58 int link_bandwidth_multiplier, int endpoint_bandwidth) 59{ 60 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth); 61 m_sID = 0; 62} 63 64void 65Throttle::init(NodeID node, int link_latency, int link_bandwidth_multiplier, 66 int endpoint_bandwidth) 67{ 68 m_node = node; 69 m_vnets = 0; 70 71 assert(link_bandwidth_multiplier > 0); 72 m_link_bandwidth_multiplier = link_bandwidth_multiplier; 73 m_link_latency = link_latency; 74 m_endpoint_bandwidth = endpoint_bandwidth; 75 76 m_wakeups_wo_switch = 0; 77 clearStats(); 78} 79 80void 81Throttle::clear() 82{ 83 for (int counter = 0; counter < m_vnets; counter++) { 84 m_in[counter]->clear(); 85 m_out[counter]->clear(); 86 } 87} 88 89void 90Throttle::addLinks(const std::vector<MessageBuffer*>& in_vec, 91 const std::vector<MessageBuffer*>& out_vec) 92{ 93 assert(in_vec.size() == out_vec.size()); 94 for (int i=0; i<in_vec.size(); i++) { 95 addVirtualNetwork(in_vec[i], out_vec[i]); 96 } 97 98 m_message_counters.resize(MessageSizeType_NUM); 99 for (int i = 0; i < MessageSizeType_NUM; i++) { 100 m_message_counters[i].resize(in_vec.size()); 101 for (int j = 0; j<m_message_counters[i].size(); j++) { 102 m_message_counters[i][j] = 0; 103 } 104 } 105} 106 107void 108Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr) 109{ 110 m_units_remaining.push_back(0); 111 m_in.push_back(in_ptr); 112 m_out.push_back(out_ptr); 113 114 // Set consumer and description 115 m_in[m_vnets]->setConsumer(this); 116 string desc = "[Queue to Throttle " + to_string(m_sID) + " " + 117 to_string(m_node) + "]"; 118 m_in[m_vnets]->setDescription(desc); 119 m_vnets++; 120} 121 122void 123Throttle::wakeup() 124{ 125 // Limits the number of message sent to a limited number of bytes/cycle. 126 assert(getLinkBandwidth() > 0); 127 int bw_remaining = getLinkBandwidth(); 128 129 // Give the highest numbered link priority most of the time 130 m_wakeups_wo_switch++; 131 int highest_prio_vnet = m_vnets-1; 132 int lowest_prio_vnet = 0; 133 int counter = 1; 134 bool schedule_wakeup = false; 135 136 // invert priorities to avoid starvation seen in the component network 137 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 138 m_wakeups_wo_switch = 0; 139 highest_prio_vnet = 0; 140 lowest_prio_vnet = m_vnets-1; 141 counter = -1; 142 } 143 144 for (int vnet = highest_prio_vnet; 145 (vnet * counter) >= (counter * lowest_prio_vnet); 146 vnet -= counter) { 147 148 assert(m_out[vnet] != NULL); 149 assert(m_in[vnet] != NULL); 150 assert(m_units_remaining[vnet] >= 0); 151 152 while (bw_remaining > 0 && 153 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) && 154 m_out[vnet]->areNSlotsAvailable(1)) { 155 156 // See if we are done transferring the previous message on 157 // this virtual network 158 if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) { 159 // Find the size of the message we are moving 160 MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr(); 161 NetworkMessage* net_msg_ptr = 162 safe_cast<NetworkMessage*>(msg_ptr.get()); 163 m_units_remaining[vnet] += 164 network_message_to_size(net_msg_ptr); 165 166 DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent " 167 "enqueueing net msg %d time: %lld.\n", 168 m_node, getLinkBandwidth(), m_units_remaining[vnet], 169 g_eventQueue_ptr->getTime()); 170 171 // Move the message 172 m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency); 173 m_in[vnet]->pop(); 174 175 // Count the message 176 m_message_counters[net_msg_ptr->getMessageSize()][vnet]++; 177 178 DPRINTF(RubyNetwork, "%s\n", *m_out[vnet]); 179 } 180 181 // Calculate the amount of bandwidth we spent on this message 182 int diff = m_units_remaining[vnet] - bw_remaining; 183 m_units_remaining[vnet] = max(0, diff); 184 bw_remaining = max(0, -diff); 185 } 186 187 if (bw_remaining > 0 && 188 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) && 189 !m_out[vnet]->areNSlotsAvailable(1)) { 190 DPRINTF(RubyNetwork, "vnet: %d", vnet); 191 // schedule me to wakeup again because I'm waiting for my 192 // output queue to become available 193 schedule_wakeup = true; 194 } 195 } 196 197 // We should only wake up when we use the bandwidth 198 // This is only mostly true 199 // assert(bw_remaining != getLinkBandwidth()); 200 201 // Record that we used some or all of the link bandwidth this cycle 202 double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth())); 203 204 // If ratio = 0, we used no bandwidth, if ratio = 1, we used all 205 linkUtilized(ratio); 206 207 if (bw_remaining > 0 && !schedule_wakeup) { 208 // We have extra bandwidth and our output buffer was 209 // available, so we must not have anything else to do until 210 // another message arrives. 211 DPRINTF(RubyNetwork, "%s not scheduled again\n", *this); 212 } else { 213 DPRINTF(RubyNetwork, "%s scheduled again\n", *this); 214 215 // We are out of bandwidth for this cycle, so wakeup next 216 // cycle and continue 217 g_eventQueue_ptr->scheduleEvent(this, 1); 218 } 219} 220 221void 222Throttle::printStats(ostream& out) const 223{ 224 out << "utilized_percent: " << getUtilization() << endl; 225} 226 227void 228Throttle::clearStats() 229{ 230 m_ruby_start = g_eventQueue_ptr->getTime(); 231 m_links_utilized = 0.0; 232 233 for (int i = 0; i < m_message_counters.size(); i++) { 234 for (int j = 0; j < m_message_counters[i].size(); j++) { 235 m_message_counters[i][j] = 0; 236 } 237 } 238} 239 240void 241Throttle::printConfig(ostream& out) const 242{ 243} 244 245double 246Throttle::getUtilization() const 247{ 248 return 100.0 * double(m_links_utilized) / 249 double(g_eventQueue_ptr->getTime()-m_ruby_start); 250} 251 252void 253Throttle::print(ostream& out) const 254{ 255 ccprintf(out, "[%i bw: %i]", m_node, getLinkBandwidth()); 256} 257 258int 259network_message_to_size(NetworkMessage* net_msg_ptr) 260{ 261 assert(net_msg_ptr != NULL); 262 263 int size = RubySystem::getNetwork()-> 264 MessageSizeType_to_int(net_msg_ptr->getMessageSize()); 265 size *= MESSAGE_SIZE_MULTIPLIER; 266 267 // Artificially increase the size of broadcast messages 268 if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast()) 269 size *= BROADCAST_SCALING; 270 271 return size; 272} 273