Throttle.cc revision 9275:ef43e69c837a
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/cast.hh" 32#include "base/cprintf.hh" 33#include "debug/RubyNetwork.hh" 34#include "mem/ruby/buffers/MessageBuffer.hh" 35#include "mem/ruby/network/simple/Throttle.hh" 36#include "mem/ruby/network/Network.hh" 37#include "mem/ruby/slicc_interface/NetworkMessage.hh" 38#include "mem/ruby/system/System.hh" 39 40using namespace std; 41 42const int HIGH_RANGE = 256; 43const int ADJUST_INTERVAL = 50000; 44const int MESSAGE_SIZE_MULTIPLIER = 1000; 45//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems 46const int BROADCAST_SCALING = 1; 47const int PRIORITY_SWITCH_LIMIT = 128; 48 49static int network_message_to_size(NetworkMessage* net_msg_ptr); 50 51Throttle::Throttle(int sID, NodeID node, int link_latency, 52 int link_bandwidth_multiplier, int endpoint_bandwidth, 53 EventManager *em) 54 : Consumer(em) 55{ 56 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth); 57 m_sID = sID; 58} 59 60Throttle::Throttle(NodeID node, int link_latency, 61 int link_bandwidth_multiplier, int endpoint_bandwidth, 62 EventManager *em) 63 : Consumer(em) 64{ 65 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth); 66 m_sID = 0; 67} 68 69void 70Throttle::init(NodeID node, int link_latency, int link_bandwidth_multiplier, 71 int endpoint_bandwidth) 72{ 73 m_node = node; 74 m_vnets = 0; 75 76 assert(link_bandwidth_multiplier > 0); 77 m_link_bandwidth_multiplier = link_bandwidth_multiplier; 78 m_link_latency = link_latency; 79 m_endpoint_bandwidth = endpoint_bandwidth; 80 81 m_wakeups_wo_switch = 0; 82 clearStats(); 83} 84 85void 86Throttle::clear() 87{ 88 for (int counter = 0; counter < m_vnets; counter++) { 89 m_in[counter]->clear(); 90 m_out[counter]->clear(); 91 } 92} 93 94void 95Throttle::addLinks(const std::vector<MessageBuffer*>& in_vec, 96 const std::vector<MessageBuffer*>& out_vec) 97{ 98 assert(in_vec.size() == out_vec.size()); 99 for (int i=0; i<in_vec.size(); i++) { 100 addVirtualNetwork(in_vec[i], out_vec[i]); 101 } 102 103 m_message_counters.resize(MessageSizeType_NUM); 104 for (int i = 0; i < MessageSizeType_NUM; i++) { 105 m_message_counters[i].resize(in_vec.size()); 106 for (int j = 0; j<m_message_counters[i].size(); j++) { 107 m_message_counters[i][j] = 0; 108 } 109 } 110} 111 112void 113Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr) 114{ 115 m_units_remaining.push_back(0); 116 m_in.push_back(in_ptr); 117 m_out.push_back(out_ptr); 118 119 // Set consumer and description 120 m_in[m_vnets]->setConsumer(this); 121 string desc = "[Queue to Throttle " + to_string(m_sID) + " " + 122 to_string(m_node) + "]"; 123 m_in[m_vnets]->setDescription(desc); 124 m_vnets++; 125} 126 127void 128Throttle::wakeup() 129{ 130 // Limits the number of message sent to a limited number of bytes/cycle. 131 assert(getLinkBandwidth() > 0); 132 int bw_remaining = getLinkBandwidth(); 133 134 // Give the highest numbered link priority most of the time 135 m_wakeups_wo_switch++; 136 int highest_prio_vnet = m_vnets-1; 137 int lowest_prio_vnet = 0; 138 int counter = 1; 139 bool schedule_wakeup = false; 140 141 // invert priorities to avoid starvation seen in the component network 142 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 143 m_wakeups_wo_switch = 0; 144 highest_prio_vnet = 0; 145 lowest_prio_vnet = m_vnets-1; 146 counter = -1; 147 } 148 149 for (int vnet = highest_prio_vnet; 150 (vnet * counter) >= (counter * lowest_prio_vnet); 151 vnet -= counter) { 152 153 assert(m_out[vnet] != NULL); 154 assert(m_in[vnet] != NULL); 155 assert(m_units_remaining[vnet] >= 0); 156 157 while (bw_remaining > 0 && 158 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) && 159 m_out[vnet]->areNSlotsAvailable(1)) { 160 161 // See if we are done transferring the previous message on 162 // this virtual network 163 if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) { 164 // Find the size of the message we are moving 165 MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr(); 166 NetworkMessage* net_msg_ptr = 167 safe_cast<NetworkMessage*>(msg_ptr.get()); 168 m_units_remaining[vnet] += 169 network_message_to_size(net_msg_ptr); 170 171 DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent " 172 "enqueueing net msg %d time: %lld.\n", 173 m_node, getLinkBandwidth(), m_units_remaining[vnet], 174 g_system_ptr->getTime()); 175 176 // Move the message 177 m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency); 178 m_in[vnet]->pop(); 179 180 // Count the message 181 m_message_counters[net_msg_ptr->getMessageSize()][vnet]++; 182 183 DPRINTF(RubyNetwork, "%s\n", *m_out[vnet]); 184 } 185 186 // Calculate the amount of bandwidth we spent on this message 187 int diff = m_units_remaining[vnet] - bw_remaining; 188 m_units_remaining[vnet] = max(0, diff); 189 bw_remaining = max(0, -diff); 190 } 191 192 if (bw_remaining > 0 && 193 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) && 194 !m_out[vnet]->areNSlotsAvailable(1)) { 195 DPRINTF(RubyNetwork, "vnet: %d", vnet); 196 // schedule me to wakeup again because I'm waiting for my 197 // output queue to become available 198 schedule_wakeup = true; 199 } 200 } 201 202 // We should only wake up when we use the bandwidth 203 // This is only mostly true 204 // assert(bw_remaining != getLinkBandwidth()); 205 206 // Record that we used some or all of the link bandwidth this cycle 207 double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth())); 208 209 // If ratio = 0, we used no bandwidth, if ratio = 1, we used all 210 linkUtilized(ratio); 211 212 if (bw_remaining > 0 && !schedule_wakeup) { 213 // We have extra bandwidth and our output buffer was 214 // available, so we must not have anything else to do until 215 // another message arrives. 216 DPRINTF(RubyNetwork, "%s not scheduled again\n", *this); 217 } else { 218 DPRINTF(RubyNetwork, "%s scheduled again\n", *this); 219 220 // We are out of bandwidth for this cycle, so wakeup next 221 // cycle and continue 222 scheduleEvent(1); 223 } 224} 225 226void 227Throttle::printStats(ostream& out) const 228{ 229 out << "utilized_percent: " << getUtilization() << endl; 230} 231 232void 233Throttle::clearStats() 234{ 235 m_ruby_start = g_system_ptr->getTime(); 236 m_links_utilized = 0.0; 237 238 for (int i = 0; i < m_message_counters.size(); i++) { 239 for (int j = 0; j < m_message_counters[i].size(); j++) { 240 m_message_counters[i][j] = 0; 241 } 242 } 243} 244 245double 246Throttle::getUtilization() const 247{ 248 return 100.0 * double(m_links_utilized) / 249 double(g_system_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 = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize()); 264 size *= MESSAGE_SIZE_MULTIPLIER; 265 266 // Artificially increase the size of broadcast messages 267 if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast()) 268 size *= BROADCAST_SCALING; 269 270 return size; 271} 272