NetworkInterface.cc revision 11795:588a45268ce4
1/* 2 * Copyright (c) 2008 Princeton University 3 * Copyright (c) 2016 Georgia Institute of Technology 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 * Authors: Niket Agarwal 30 * Tushar Krishna 31 */ 32 33 34#include "mem/ruby/network/garnet2.0/NetworkInterface.hh" 35 36#include <cassert> 37#include <cmath> 38 39#include "base/cast.hh" 40#include "base/stl_helpers.hh" 41#include "debug/RubyNetwork.hh" 42#include "mem/ruby/network/MessageBuffer.hh" 43#include "mem/ruby/network/garnet2.0/Credit.hh" 44#include "mem/ruby/network/garnet2.0/flitBuffer.hh" 45#include "mem/ruby/slicc_interface/Message.hh" 46 47using namespace std; 48using m5::stl_helpers::deletePointers; 49 50NetworkInterface::NetworkInterface(const Params *p) 51 : ClockedObject(p), Consumer(this), m_id(p->id), 52 m_virtual_networks(p->virt_nets), m_vc_per_vnet(p->vcs_per_vnet), 53 m_num_vcs(m_vc_per_vnet * m_virtual_networks), 54 m_deadlock_threshold(p->garnet_deadlock_threshold), 55 vc_busy_counter(m_virtual_networks, 0) 56{ 57 m_router_id = -1; 58 m_vc_round_robin = 0; 59 m_ni_out_vcs.resize(m_num_vcs); 60 m_ni_out_vcs_enqueue_time.resize(m_num_vcs); 61 outCreditQueue = new flitBuffer(); 62 63 // instantiating the NI flit buffers 64 for (int i = 0; i < m_num_vcs; i++) { 65 m_ni_out_vcs[i] = new flitBuffer(); 66 m_ni_out_vcs_enqueue_time[i] = Cycles(INFINITE_); 67 } 68 69 m_vc_allocator.resize(m_virtual_networks); // 1 allocator per vnet 70 for (int i = 0; i < m_virtual_networks; i++) { 71 m_vc_allocator[i] = 0; 72 } 73} 74 75void 76NetworkInterface::init() 77{ 78 for (int i = 0; i < m_num_vcs; i++) { 79 m_out_vc_state.push_back(new OutVcState(i, m_net_ptr)); 80 } 81} 82 83NetworkInterface::~NetworkInterface() 84{ 85 deletePointers(m_out_vc_state); 86 deletePointers(m_ni_out_vcs); 87 delete outCreditQueue; 88 delete outFlitQueue; 89} 90 91void 92NetworkInterface::addInPort(NetworkLink *in_link, 93 CreditLink *credit_link) 94{ 95 inNetLink = in_link; 96 in_link->setLinkConsumer(this); 97 outCreditLink = credit_link; 98 credit_link->setSourceQueue(outCreditQueue); 99} 100 101void 102NetworkInterface::addOutPort(NetworkLink *out_link, 103 CreditLink *credit_link, 104 SwitchID router_id) 105{ 106 inCreditLink = credit_link; 107 credit_link->setLinkConsumer(this); 108 109 outNetLink = out_link; 110 outFlitQueue = new flitBuffer(); 111 out_link->setSourceQueue(outFlitQueue); 112 113 m_router_id = router_id; 114} 115 116void 117NetworkInterface::addNode(vector<MessageBuffer *>& in, 118 vector<MessageBuffer *>& out) 119{ 120 inNode_ptr = in; 121 outNode_ptr = out; 122 123 for (auto& it : in) { 124 if (it != nullptr) { 125 it->setConsumer(this); 126 } 127 } 128} 129 130 131/* 132 * The NI wakeup checks whether there are any ready messages in the protocol 133 * buffer. If yes, it picks that up, flitisizes it into a number of flits and 134 * puts it into an output buffer and schedules the output link. On a wakeup 135 * it also checks whether there are flits in the input link. If yes, it picks 136 * them up and if the flit is a tail, the NI inserts the corresponding message 137 * into the protocol buffer. It also checks for credits being sent by the 138 * downstream router. 139 */ 140 141void 142NetworkInterface::wakeup() 143{ 144 DPRINTF(RubyNetwork, "Network Interface %d connected to router %d " 145 "woke up at time: %lld\n", m_id, m_router_id, curCycle()); 146 147 MsgPtr msg_ptr; 148 Tick curTime = clockEdge(); 149 150 // Checking for messages coming from the protocol 151 // can pick up a message/cycle for each virtual net 152 for (int vnet = 0; vnet < inNode_ptr.size(); ++vnet) { 153 MessageBuffer *b = inNode_ptr[vnet]; 154 if (b == nullptr) { 155 continue; 156 } 157 158 if (b->isReady(curTime)) { // Is there a message waiting 159 msg_ptr = b->peekMsgPtr(); 160 if (flitisizeMessage(msg_ptr, vnet)) { 161 b->dequeue(curTime); 162 } 163 } 164 } 165 166 scheduleOutputLink(); 167 checkReschedule(); 168 169 /*********** Check the incoming flit link **********/ 170 171 if (inNetLink->isReady(curCycle())) { 172 flit *t_flit = inNetLink->consumeLink(); 173 bool free_signal = false; 174 if (t_flit->get_type() == TAIL_ || t_flit->get_type() == HEAD_TAIL_) { 175 free_signal = true; 176 177 // enqueue into the protocol buffers 178 outNode_ptr[t_flit->get_vnet()]->enqueue( 179 t_flit->get_msg_ptr(), curTime, cyclesToTicks(Cycles(1))); 180 } 181 // Simply send a credit back since we are not buffering 182 // this flit in the NI 183 Credit *t_credit = new Credit(t_flit->get_vc(), free_signal, 184 curCycle()); 185 outCreditQueue->insert(t_credit); 186 outCreditLink-> 187 scheduleEventAbsolute(clockEdge(Cycles(1))); 188 189 int vnet = t_flit->get_vnet(); 190 191 // Update Stats 192 193 // Latency 194 m_net_ptr->increment_received_flits(vnet); 195 Cycles network_delay = curCycle() - t_flit->get_enqueue_time(); 196 Cycles queueing_delay = t_flit->get_src_delay(); 197 198 m_net_ptr->increment_flit_network_latency(network_delay, vnet); 199 m_net_ptr->increment_flit_queueing_latency(queueing_delay, vnet); 200 201 if (t_flit->get_type() == TAIL_ || t_flit->get_type() == HEAD_TAIL_) { 202 m_net_ptr->increment_received_packets(vnet); 203 m_net_ptr->increment_packet_network_latency(network_delay, vnet); 204 m_net_ptr->increment_packet_queueing_latency(queueing_delay, vnet); 205 } 206 207 // Hops 208 m_net_ptr->increment_total_hops(t_flit->get_route().hops_traversed); 209 210 delete t_flit; 211 } 212 213 /****************** Check the incoming credit link *******/ 214 215 if (inCreditLink->isReady(curCycle())) { 216 Credit *t_credit = (Credit*) inCreditLink->consumeLink(); 217 m_out_vc_state[t_credit->get_vc()]->increment_credit(); 218 if (t_credit->is_free_signal()) { 219 m_out_vc_state[t_credit->get_vc()]->setState(IDLE_, curCycle()); 220 } 221 delete t_credit; 222 } 223} 224 225 226// Embed the protocol message into flits 227bool 228NetworkInterface::flitisizeMessage(MsgPtr msg_ptr, int vnet) 229{ 230 Message *net_msg_ptr = msg_ptr.get(); 231 NetDest net_msg_dest = net_msg_ptr->getDestination(); 232 233 // gets all the destinations associated with this message. 234 vector<NodeID> dest_nodes = net_msg_dest.getAllDest(); 235 236 // Number of flits is dependent on the link bandwidth available. 237 // This is expressed in terms of bytes/cycle or the flit size 238 int num_flits = (int) ceil((double) m_net_ptr->MessageSizeType_to_int( 239 net_msg_ptr->getMessageSize())/m_net_ptr->getNiFlitSize()); 240 241 // loop to convert all multicast messages into unicast messages 242 for (int ctr = 0; ctr < dest_nodes.size(); ctr++) { 243 244 // this will return a free output virtual channel 245 int vc = calculateVC(vnet); 246 247 if (vc == -1) { 248 return false ; 249 } 250 MsgPtr new_msg_ptr = msg_ptr->clone(); 251 NodeID destID = dest_nodes[ctr]; 252 253 Message *new_net_msg_ptr = new_msg_ptr.get(); 254 if (dest_nodes.size() > 1) { 255 NetDest personal_dest; 256 for (int m = 0; m < (int) MachineType_NUM; m++) { 257 if ((destID >= MachineType_base_number((MachineType) m)) && 258 destID < MachineType_base_number((MachineType) (m+1))) { 259 // calculating the NetDest associated with this destID 260 personal_dest.clear(); 261 personal_dest.add((MachineID) {(MachineType) m, (destID - 262 MachineType_base_number((MachineType) m))}); 263 new_net_msg_ptr->getDestination() = personal_dest; 264 break; 265 } 266 } 267 net_msg_dest.removeNetDest(personal_dest); 268 // removing the destination from the original message to reflect 269 // that a message with this particular destination has been 270 // flitisized and an output vc is acquired 271 net_msg_ptr->getDestination().removeNetDest(personal_dest); 272 } 273 274 // Embed Route into the flits 275 // NetDest format is used by the routing table 276 // Custom routing algorithms just need destID 277 RouteInfo route; 278 route.vnet = vnet; 279 route.net_dest = new_net_msg_ptr->getDestination(); 280 route.src_ni = m_id; 281 route.src_router = m_router_id; 282 route.dest_ni = destID; 283 route.dest_router = m_net_ptr->get_router_id(destID); 284 285 // initialize hops_traversed to -1 286 // so that the first router increments it to 0 287 route.hops_traversed = -1; 288 289 m_net_ptr->increment_injected_packets(vnet); 290 for (int i = 0; i < num_flits; i++) { 291 m_net_ptr->increment_injected_flits(vnet); 292 flit *fl = new flit(i, vc, vnet, route, num_flits, new_msg_ptr, 293 curCycle()); 294 295 fl->set_src_delay(curCycle() - ticksToCycles(msg_ptr->getTime())); 296 m_ni_out_vcs[vc]->insert(fl); 297 } 298 299 m_ni_out_vcs_enqueue_time[vc] = curCycle(); 300 m_out_vc_state[vc]->setState(ACTIVE_, curCycle()); 301 } 302 return true ; 303} 304 305// Looking for a free output vc 306int 307NetworkInterface::calculateVC(int vnet) 308{ 309 for (int i = 0; i < m_vc_per_vnet; i++) { 310 int delta = m_vc_allocator[vnet]; 311 m_vc_allocator[vnet]++; 312 if (m_vc_allocator[vnet] == m_vc_per_vnet) 313 m_vc_allocator[vnet] = 0; 314 315 if (m_out_vc_state[(vnet*m_vc_per_vnet) + delta]->isInState( 316 IDLE_, curCycle())) { 317 vc_busy_counter[vnet] = 0; 318 return ((vnet*m_vc_per_vnet) + delta); 319 } 320 } 321 322 vc_busy_counter[vnet] += 1; 323 panic_if(vc_busy_counter[vnet] > m_deadlock_threshold, 324 "%s: Possible network deadlock in vnet: %d at time: %llu \n", 325 name(), vnet, curTick()); 326 327 return -1; 328} 329 330 331/** This function looks at the NI buffers 332 * if some buffer has flits which are ready to traverse the link in the next 333 * cycle, and the downstream output vc associated with this flit has buffers 334 * left, the link is scheduled for the next cycle 335 */ 336 337void 338NetworkInterface::scheduleOutputLink() 339{ 340 int vc = m_vc_round_robin; 341 m_vc_round_robin++; 342 if (m_vc_round_robin == m_num_vcs) 343 m_vc_round_robin = 0; 344 345 for (int i = 0; i < m_num_vcs; i++) { 346 vc++; 347 if (vc == m_num_vcs) 348 vc = 0; 349 350 // model buffer backpressure 351 if (m_ni_out_vcs[vc]->isReady(curCycle()) && 352 m_out_vc_state[vc]->has_credit()) { 353 354 bool is_candidate_vc = true; 355 int t_vnet = get_vnet(vc); 356 int vc_base = t_vnet * m_vc_per_vnet; 357 358 if (m_net_ptr->isVNetOrdered(t_vnet)) { 359 for (int vc_offset = 0; vc_offset < m_vc_per_vnet; 360 vc_offset++) { 361 int t_vc = vc_base + vc_offset; 362 if (m_ni_out_vcs[t_vc]->isReady(curCycle())) { 363 if (m_ni_out_vcs_enqueue_time[t_vc] < 364 m_ni_out_vcs_enqueue_time[vc]) { 365 is_candidate_vc = false; 366 break; 367 } 368 } 369 } 370 } 371 if (!is_candidate_vc) 372 continue; 373 374 m_out_vc_state[vc]->decrement_credit(); 375 // Just removing the flit 376 flit *t_flit = m_ni_out_vcs[vc]->getTopFlit(); 377 t_flit->set_time(curCycle() + Cycles(1)); 378 outFlitQueue->insert(t_flit); 379 // schedule the out link 380 outNetLink->scheduleEventAbsolute(clockEdge(Cycles(1))); 381 382 if (t_flit->get_type() == TAIL_ || 383 t_flit->get_type() == HEAD_TAIL_) { 384 m_ni_out_vcs_enqueue_time[vc] = Cycles(INFINITE_); 385 } 386 return; 387 } 388 } 389} 390 391int 392NetworkInterface::get_vnet(int vc) 393{ 394 for (int i = 0; i < m_virtual_networks; i++) { 395 if (vc >= (i*m_vc_per_vnet) && vc < ((i+1)*m_vc_per_vnet)) { 396 return i; 397 } 398 } 399 fatal("Could not determine vc"); 400} 401 402 403// Wakeup the NI in the next cycle if there are waiting 404// messages in the protocol buffer, or waiting flits in the 405// output VC buffer 406void 407NetworkInterface::checkReschedule() 408{ 409 for (const auto& it : inNode_ptr) { 410 if (it == nullptr) { 411 continue; 412 } 413 414 while (it->isReady(clockEdge())) { // Is there a message waiting 415 scheduleEvent(Cycles(1)); 416 return; 417 } 418 } 419 420 for (int vc = 0; vc < m_num_vcs; vc++) { 421 if (m_ni_out_vcs[vc]->isReady(curCycle() + Cycles(1))) { 422 scheduleEvent(Cycles(1)); 423 return; 424 } 425 } 426} 427 428void 429NetworkInterface::print(std::ostream& out) const 430{ 431 out << "[Network Interface]"; 432} 433 434uint32_t 435NetworkInterface::functionalWrite(Packet *pkt) 436{ 437 uint32_t num_functional_writes = 0; 438 for (unsigned int i = 0; i < m_num_vcs; ++i) { 439 num_functional_writes += m_ni_out_vcs[i]->functionalWrite(pkt); 440 } 441 442 num_functional_writes += outFlitQueue->functionalWrite(pkt); 443 return num_functional_writes; 444} 445 446NetworkInterface * 447GarnetNetworkInterfaceParams::create() 448{ 449 return new NetworkInterface(this); 450} 451