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