MessageBuffer.cc revision 11732:e15e445c21a6
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 "base/misc.hh" 33#include "base/random.hh" 34#include "base/stl_helpers.hh" 35#include "debug/RubyQueue.hh" 36#include "mem/ruby/network/MessageBuffer.hh" 37#include "mem/ruby/system/RubySystem.hh" 38 39using namespace std; 40using m5::stl_helpers::operator<<; 41 42MessageBuffer::MessageBuffer(const Params *p) 43 : SimObject(p), 44 m_max_size(p->buffer_size), m_time_last_time_size_checked(0), 45 m_time_last_time_enqueue(0), m_time_last_time_pop(0), 46 m_last_arrival_time(0), m_strict_fifo(p->ordered), 47 m_randomization(p->randomization) 48{ 49 m_msg_counter = 0; 50 m_consumer = NULL; 51 m_size_last_time_size_checked = 0; 52 m_size_at_cycle_start = 0; 53 m_msgs_this_cycle = 0; 54 m_priority_rank = 0; 55 56 m_stall_msg_map.clear(); 57 m_input_link_id = 0; 58 m_vnet_id = 0; 59} 60 61unsigned int 62MessageBuffer::getSize(Tick curTime) 63{ 64 if (m_time_last_time_size_checked != curTime) { 65 m_time_last_time_size_checked = curTime; 66 m_size_last_time_size_checked = m_prio_heap.size(); 67 } 68 69 return m_size_last_time_size_checked; 70} 71 72bool 73MessageBuffer::areNSlotsAvailable(unsigned int n, Tick current_time) 74{ 75 76 // fast path when message buffers have infinite size 77 if (m_max_size == 0) { 78 return true; 79 } 80 81 // determine the correct size for the current cycle 82 // pop operations shouldn't effect the network's visible size 83 // until schd cycle, but enqueue operations effect the visible 84 // size immediately 85 unsigned int current_size = 0; 86 87 if (m_time_last_time_pop < current_time) { 88 // no pops this cycle - heap size is correct 89 current_size = m_prio_heap.size(); 90 } else { 91 if (m_time_last_time_enqueue < current_time) { 92 // no enqueues this cycle - m_size_at_cycle_start is correct 93 current_size = m_size_at_cycle_start; 94 } else { 95 // both pops and enqueues occured this cycle - add new 96 // enqueued msgs to m_size_at_cycle_start 97 current_size = m_size_at_cycle_start + m_msgs_this_cycle; 98 } 99 } 100 101 // now compare the new size with our max size 102 if (current_size + n <= m_max_size) { 103 return true; 104 } else { 105 DPRINTF(RubyQueue, "n: %d, current_size: %d, heap size: %d, " 106 "m_max_size: %d\n", 107 n, current_size, m_prio_heap.size(), m_max_size); 108 m_not_avail_count++; 109 return false; 110 } 111} 112 113const Message* 114MessageBuffer::peek() const 115{ 116 DPRINTF(RubyQueue, "Peeking at head of queue.\n"); 117 const Message* msg_ptr = m_prio_heap.front().get(); 118 assert(msg_ptr); 119 120 DPRINTF(RubyQueue, "Message: %s\n", (*msg_ptr)); 121 return msg_ptr; 122} 123 124// FIXME - move me somewhere else 125Tick 126random_time() 127{ 128 Tick time = 1; 129 time += random_mt.random(0, 3); // [0...3] 130 if (random_mt.random(0, 7) == 0) { // 1 in 8 chance 131 time += 100 + random_mt.random(1, 15); // 100 + [1...15] 132 } 133 return time; 134} 135 136void 137MessageBuffer::enqueue(MsgPtr message, Tick current_time, Tick delta) 138{ 139 // record current time incase we have a pop that also adjusts my size 140 if (m_time_last_time_enqueue < current_time) { 141 m_msgs_this_cycle = 0; // first msg this cycle 142 m_time_last_time_enqueue = current_time; 143 } 144 145 m_msg_counter++; 146 m_msgs_this_cycle++; 147 148 // Calculate the arrival time of the message, that is, the first 149 // cycle the message can be dequeued. 150 assert(delta > 0); 151 Tick arrival_time = 0; 152 153 if (!RubySystem::getRandomization() || !m_randomization) { 154 // No randomization 155 arrival_time = current_time + delta; 156 } else { 157 // Randomization - ignore delta 158 if (m_strict_fifo) { 159 if (m_last_arrival_time < current_time) { 160 m_last_arrival_time = current_time; 161 } 162 arrival_time = m_last_arrival_time + random_time(); 163 } else { 164 arrival_time = current_time + random_time(); 165 } 166 } 167 168 // Check the arrival time 169 assert(arrival_time > current_time); 170 if (m_strict_fifo) { 171 if (arrival_time < m_last_arrival_time) { 172 panic("FIFO ordering violated: %s name: %s current time: %d " 173 "delta: %d arrival_time: %d last arrival_time: %d\n", 174 *this, name(), current_time, delta, arrival_time, 175 m_last_arrival_time); 176 } 177 } 178 179 // If running a cache trace, don't worry about the last arrival checks 180 if (!RubySystem::getWarmupEnabled()) { 181 m_last_arrival_time = arrival_time; 182 } 183 184 // compute the delay cycles and set enqueue time 185 Message* msg_ptr = message.get(); 186 assert(msg_ptr != NULL); 187 188 assert(current_time >= msg_ptr->getLastEnqueueTime() && 189 "ensure we aren't dequeued early"); 190 191 msg_ptr->updateDelayedTicks(current_time); 192 msg_ptr->setLastEnqueueTime(arrival_time); 193 msg_ptr->setMsgCounter(m_msg_counter); 194 195 // Insert the message into the priority heap 196 m_prio_heap.push_back(message); 197 push_heap(m_prio_heap.begin(), m_prio_heap.end(), greater<MsgPtr>()); 198 199 DPRINTF(RubyQueue, "Enqueue arrival_time: %lld, Message: %s\n", 200 arrival_time, *(message.get())); 201 202 // Schedule the wakeup 203 assert(m_consumer != NULL); 204 m_consumer->scheduleEventAbsolute(arrival_time); 205 m_consumer->storeEventInfo(m_vnet_id); 206} 207 208Tick 209MessageBuffer::dequeue(Tick current_time) 210{ 211 DPRINTF(RubyQueue, "Popping\n"); 212 assert(isReady(current_time)); 213 214 // get MsgPtr of the message about to be dequeued 215 MsgPtr message = m_prio_heap.front(); 216 217 // get the delay cycles 218 message->updateDelayedTicks(current_time); 219 Tick delay = message->getDelayedTicks(); 220 221 // record previous size and time so the current buffer size isn't 222 // adjusted until schd cycle 223 if (m_time_last_time_pop < current_time) { 224 m_size_at_cycle_start = m_prio_heap.size(); 225 m_time_last_time_pop = current_time; 226 } 227 228 pop_heap(m_prio_heap.begin(), m_prio_heap.end(), greater<MsgPtr>()); 229 m_prio_heap.pop_back(); 230 231 return delay; 232} 233 234void 235MessageBuffer::clear() 236{ 237 m_prio_heap.clear(); 238 239 m_msg_counter = 0; 240 m_time_last_time_enqueue = 0; 241 m_time_last_time_pop = 0; 242 m_size_at_cycle_start = 0; 243 m_msgs_this_cycle = 0; 244} 245 246void 247MessageBuffer::recycle(Tick current_time, Tick recycle_latency) 248{ 249 DPRINTF(RubyQueue, "Recycling.\n"); 250 assert(isReady(current_time)); 251 MsgPtr node = m_prio_heap.front(); 252 pop_heap(m_prio_heap.begin(), m_prio_heap.end(), greater<MsgPtr>()); 253 254 Tick future_time = current_time + recycle_latency; 255 node->setLastEnqueueTime(future_time); 256 257 m_prio_heap.back() = node; 258 push_heap(m_prio_heap.begin(), m_prio_heap.end(), greater<MsgPtr>()); 259 m_consumer->scheduleEventAbsolute(future_time); 260} 261 262void 263MessageBuffer::reanalyzeList(list<MsgPtr> <, Tick schdTick) 264{ 265 while (!lt.empty()) { 266 m_msg_counter++; 267 MsgPtr m = lt.front(); 268 m->setLastEnqueueTime(schdTick); 269 m->setMsgCounter(m_msg_counter); 270 271 m_prio_heap.push_back(m); 272 push_heap(m_prio_heap.begin(), m_prio_heap.end(), 273 greater<MsgPtr>()); 274 275 m_consumer->scheduleEventAbsolute(schdTick); 276 lt.pop_front(); 277 } 278} 279 280void 281MessageBuffer::reanalyzeMessages(Addr addr, Tick current_time) 282{ 283 DPRINTF(RubyQueue, "ReanalyzeMessages %#x\n", addr); 284 assert(m_stall_msg_map.count(addr) > 0); 285 286 // 287 // Put all stalled messages associated with this address back on the 288 // prio heap. The reanalyzeList call will make sure the consumer is 289 // scheduled for the current cycle so that the previously stalled messages 290 // will be observed before any younger messages that may arrive this cycle 291 // 292 reanalyzeList(m_stall_msg_map[addr], current_time); 293 m_stall_msg_map.erase(addr); 294} 295 296void 297MessageBuffer::reanalyzeAllMessages(Tick current_time) 298{ 299 DPRINTF(RubyQueue, "ReanalyzeAllMessages\n"); 300 301 // 302 // Put all stalled messages associated with this address back on the 303 // prio heap. The reanalyzeList call will make sure the consumer is 304 // scheduled for the current cycle so that the previously stalled messages 305 // will be observed before any younger messages that may arrive this cycle. 306 // 307 for (StallMsgMapType::iterator map_iter = m_stall_msg_map.begin(); 308 map_iter != m_stall_msg_map.end(); ++map_iter) { 309 reanalyzeList(map_iter->second, current_time); 310 } 311 m_stall_msg_map.clear(); 312} 313 314void 315MessageBuffer::stallMessage(Addr addr, Tick current_time) 316{ 317 DPRINTF(RubyQueue, "Stalling due to %#x\n", addr); 318 assert(isReady(current_time)); 319 assert(getOffset(addr) == 0); 320 MsgPtr message = m_prio_heap.front(); 321 322 dequeue(current_time); 323 324 // 325 // Note: no event is scheduled to analyze the map at a later time. 326 // Instead the controller is responsible to call reanalyzeMessages when 327 // these addresses change state. 328 // 329 (m_stall_msg_map[addr]).push_back(message); 330} 331 332void 333MessageBuffer::print(ostream& out) const 334{ 335 ccprintf(out, "[MessageBuffer: "); 336 if (m_consumer != NULL) { 337 ccprintf(out, " consumer-yes "); 338 } 339 340 vector<MsgPtr> copy(m_prio_heap); 341 sort_heap(copy.begin(), copy.end(), greater<MsgPtr>()); 342 ccprintf(out, "%s] %s", copy, name()); 343} 344 345bool 346MessageBuffer::isReady(Tick current_time) const 347{ 348 return ((m_prio_heap.size() > 0) && 349 (m_prio_heap.front()->getLastEnqueueTime() <= current_time)); 350} 351 352void 353MessageBuffer::regStats() 354{ 355 m_not_avail_count 356 .name(name() + ".not_avail_count") 357 .desc("Number of times this buffer did not have N slots available") 358 .flags(Stats::nozero); 359} 360 361uint32_t 362MessageBuffer::functionalWrite(Packet *pkt) 363{ 364 uint32_t num_functional_writes = 0; 365 366 // Check the priority heap and write any messages that may 367 // correspond to the address in the packet. 368 for (unsigned int i = 0; i < m_prio_heap.size(); ++i) { 369 Message *msg = m_prio_heap[i].get(); 370 if (msg->functionalWrite(pkt)) { 371 num_functional_writes++; 372 } 373 } 374 375 // Check the stall queue and write any messages that may 376 // correspond to the address in the packet. 377 for (StallMsgMapType::iterator map_iter = m_stall_msg_map.begin(); 378 map_iter != m_stall_msg_map.end(); 379 ++map_iter) { 380 381 for (std::list<MsgPtr>::iterator it = (map_iter->second).begin(); 382 it != (map_iter->second).end(); ++it) { 383 384 Message *msg = (*it).get(); 385 if (msg->functionalWrite(pkt)) { 386 num_functional_writes++; 387 } 388 } 389 } 390 391 return num_functional_writes; 392} 393 394MessageBuffer * 395MessageBufferParams::create() 396{ 397 return new MessageBuffer(this); 398} 399