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 "mem/ruby/network/simple/Throttle.hh" 30 31#include <cassert> 32 33#include "base/cast.hh" 34#include "base/cprintf.hh" 35#include "debug/RubyNetwork.hh" 36#include "mem/ruby/network/MessageBuffer.hh" 37#include "mem/ruby/network/Network.hh" 38#include "mem/ruby/network/simple/Switch.hh" 39#include "mem/ruby/slicc_interface/Message.hh" 40#include "mem/ruby/system/RubySystem.hh" 41 42using namespace std; 43 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(Message* net_msg_ptr); 50 51Throttle::Throttle(int sID, RubySystem *rs, NodeID node, Cycles link_latency, 52 int link_bandwidth_multiplier, int endpoint_bandwidth, 53 Switch *em) 54 : Consumer(em), m_switch_id(sID), m_switch(em), m_node(node), 55 m_ruby_system(rs) 56{ 57 m_vnets = 0; 58 59 assert(link_bandwidth_multiplier > 0); 60 m_link_bandwidth_multiplier = link_bandwidth_multiplier; 61 62 m_link_latency = link_latency; 63 m_endpoint_bandwidth = endpoint_bandwidth; 64 65 m_wakeups_wo_switch = 0; 66 m_link_utilization_proxy = 0; 67} 68 69void 70Throttle::addLinks(const vector<MessageBuffer*>& in_vec, 71 const vector<MessageBuffer*>& out_vec) 72{ 73 assert(in_vec.size() == out_vec.size()); 74 75 for (int vnet = 0; vnet < in_vec.size(); ++vnet) { 76 MessageBuffer *in_ptr = in_vec[vnet]; 77 MessageBuffer *out_ptr = out_vec[vnet]; 78 79 m_vnets++; 80 m_units_remaining.push_back(0); 81 m_in.push_back(in_ptr); 82 m_out.push_back(out_ptr); 83 84 // Set consumer and description 85 in_ptr->setConsumer(this); 86 string desc = "[Queue to Throttle " + to_string(m_switch_id) + " " + 87 to_string(m_node) + "]"; 88 } 89} 90 91void 92Throttle::operateVnet(int vnet, int &bw_remaining, bool &schedule_wakeup, 93 MessageBuffer *in, MessageBuffer *out) 94{ 95 if (out == nullptr || in == nullptr) { 96 return; 97 } 98 99 assert(m_units_remaining[vnet] >= 0); 100 Tick current_time = m_switch->clockEdge(); 101 102 while (bw_remaining > 0 && (in->isReady(current_time) || 103 m_units_remaining[vnet] > 0) && 104 out->areNSlotsAvailable(1, current_time)) { 105 // See if we are done transferring the previous message on 106 // this virtual network 107 if (m_units_remaining[vnet] == 0 && in->isReady(current_time)) { 108 // Find the size of the message we are moving 109 MsgPtr msg_ptr = in->peekMsgPtr(); 110 Message *net_msg_ptr = msg_ptr.get(); 111 m_units_remaining[vnet] += 112 network_message_to_size(net_msg_ptr); 113 114 DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent " 115 "enqueueing net msg %d time: %lld.\n", 116 m_node, getLinkBandwidth(), m_units_remaining[vnet], 117 m_ruby_system->curCycle()); 118 119 // Move the message 120 in->dequeue(current_time); 121 out->enqueue(msg_ptr, current_time, 122 m_switch->cyclesToTicks(m_link_latency)); 123 124 // Count the message 125 m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++; 126 DPRINTF(RubyNetwork, "%s\n", *out); 127 } 128 129 // Calculate the amount of bandwidth we spent on this message 130 int diff = m_units_remaining[vnet] - bw_remaining; 131 m_units_remaining[vnet] = max(0, diff); 132 bw_remaining = max(0, -diff); 133 } 134 135 if (bw_remaining > 0 && (in->isReady(current_time) || 136 m_units_remaining[vnet] > 0) && 137 !out->areNSlotsAvailable(1, current_time)) { 138 DPRINTF(RubyNetwork, "vnet: %d", vnet); 139 140 // schedule me to wakeup again because I'm waiting for my 141 // output queue to become available 142 schedule_wakeup = true; 143 } 144} 145 146void 147Throttle::wakeup() 148{ 149 // Limits the number of message sent to a limited number of bytes/cycle. 150 assert(getLinkBandwidth() > 0); 151 int bw_remaining = getLinkBandwidth(); 152 153 m_wakeups_wo_switch++; 154 bool schedule_wakeup = false; 155 156 // variable for deciding the direction in which to iterate 157 bool iteration_direction = false; 158 159 160 // invert priorities to avoid starvation seen in the component network 161 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 162 m_wakeups_wo_switch = 0; 163 iteration_direction = true; 164 } 165 166 if (iteration_direction) { 167 for (int vnet = 0; vnet < m_vnets; ++vnet) { 168 operateVnet(vnet, bw_remaining, schedule_wakeup, 169 m_in[vnet], m_out[vnet]); 170 } 171 } else { 172 for (int vnet = m_vnets-1; vnet >= 0; --vnet) { 173 operateVnet(vnet, bw_remaining, schedule_wakeup, 174 m_in[vnet], m_out[vnet]); 175 } 176 } 177 178 // We should only wake up when we use the bandwidth 179 // This is only mostly true 180 // assert(bw_remaining != getLinkBandwidth()); 181 182 // Record that we used some or all of the link bandwidth this cycle 183 double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth())); 184 185 // If ratio = 0, we used no bandwidth, if ratio = 1, we used all 186 m_link_utilization_proxy += ratio; 187 188 if (bw_remaining > 0 && !schedule_wakeup) { 189 // We have extra bandwidth and our output buffer was 190 // available, so we must not have anything else to do until 191 // another message arrives. 192 DPRINTF(RubyNetwork, "%s not scheduled again\n", *this); 193 } else { 194 DPRINTF(RubyNetwork, "%s scheduled again\n", *this); 195 196 // We are out of bandwidth for this cycle, so wakeup next 197 // cycle and continue 198 scheduleEvent(Cycles(1)); 199 } 200} 201 202void 203Throttle::regStats(string parent) 204{ 205 m_link_utilization 206 .name(parent + csprintf(".throttle%i", m_node) + ".link_utilization"); 207 208 for (MessageSizeType type = MessageSizeType_FIRST; 209 type < MessageSizeType_NUM; ++type) { 210 m_msg_counts[(unsigned int)type] 211 .init(Network::getNumberOfVirtualNetworks()) 212 .name(parent + csprintf(".throttle%i", m_node) + ".msg_count." + 213 MessageSizeType_to_string(type)) 214 .flags(Stats::nozero) 215 ; 216 m_msg_bytes[(unsigned int) type] 217 .name(parent + csprintf(".throttle%i", m_node) + ".msg_bytes." + 218 MessageSizeType_to_string(type)) 219 .flags(Stats::nozero) 220 ; 221 222 m_msg_bytes[(unsigned int) type] = m_msg_counts[type] * Stats::constant( 223 Network::MessageSizeType_to_int(type)); 224 } 225} 226 227void 228Throttle::clearStats() 229{ 230 m_link_utilization_proxy = 0; 231} 232 233void 234Throttle::collateStats() 235{ 236 double time_delta = double(m_ruby_system->curCycle() - 237 m_ruby_system->getStartCycle()); 238 239 m_link_utilization = 100.0 * m_link_utilization_proxy / time_delta; 240} 241 242void 243Throttle::print(ostream& out) const 244{ 245 ccprintf(out, "[%i bw: %i]", m_node, getLinkBandwidth()); 246} 247 248int 249network_message_to_size(Message *net_msg_ptr) 250{ 251 assert(net_msg_ptr != NULL); 252 253 int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize()); 254 size *= MESSAGE_SIZE_MULTIPLIER; 255 256 // Artificially increase the size of broadcast messages 257 if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast()) 258 size *= BROADCAST_SCALING; 259 260 return size; 261} 262