MessageBuffer.hh revision 13784:1941dc118243
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/* 30 * Unordered buffer of messages that can be inserted such 31 * that they can be dequeued after a given delta time has expired. 32 */ 33 34#ifndef __MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__ 35#define __MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__ 36 37#include <algorithm> 38#include <cassert> 39#include <functional> 40#include <iostream> 41#include <string> 42#include <vector> 43 44#include "base/trace.hh" 45#include "debug/RubyQueue.hh" 46#include "mem/packet.hh" 47#include "mem/port.hh" 48#include "mem/ruby/common/Address.hh" 49#include "mem/ruby/common/Consumer.hh" 50#include "mem/ruby/network/dummy_port.hh" 51#include "mem/ruby/slicc_interface/Message.hh" 52#include "params/MessageBuffer.hh" 53#include "sim/sim_object.hh" 54 55class MessageBuffer : public SimObject 56{ 57 public: 58 typedef MessageBufferParams Params; 59 MessageBuffer(const Params *p); 60 61 void reanalyzeMessages(Addr addr, Tick current_time); 62 void reanalyzeAllMessages(Tick current_time); 63 void stallMessage(Addr addr, Tick current_time); 64 65 // TRUE if head of queue timestamp <= SystemTime 66 bool isReady(Tick current_time) const; 67 68 void 69 delayHead(Tick current_time, Tick delta) 70 { 71 MsgPtr m = m_prio_heap.front(); 72 std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(), 73 std::greater<MsgPtr>()); 74 m_prio_heap.pop_back(); 75 enqueue(m, current_time, delta); 76 } 77 78 bool areNSlotsAvailable(unsigned int n, Tick curTime); 79 int getPriority() { return m_priority_rank; } 80 void setPriority(int rank) { m_priority_rank = rank; } 81 void setConsumer(Consumer* consumer) 82 { 83 DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer); 84 if (m_consumer != NULL) { 85 fatal("Trying to connect %s to MessageBuffer %s. \ 86 \n%s already connected. Check the cntrl_id's.\n", 87 *consumer, *this, *m_consumer); 88 } 89 m_consumer = consumer; 90 } 91 92 Consumer* getConsumer() { return m_consumer; } 93 94 bool getOrdered() { return m_strict_fifo; } 95 96 //! Function for extracting the message at the head of the 97 //! message queue. The function assumes that the queue is nonempty. 98 const Message* peek() const; 99 100 const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); } 101 102 void enqueue(MsgPtr message, Tick curTime, Tick delta); 103 104 //! Updates the delay cycles of the message at the head of the queue, 105 //! removes it from the queue and returns its total delay. 106 Tick dequeue(Tick current_time, bool decrement_messages = true); 107 108 void registerDequeueCallback(std::function<void()> callback); 109 void unregisterDequeueCallback(); 110 111 void recycle(Tick current_time, Tick recycle_latency); 112 bool isEmpty() const { return m_prio_heap.size() == 0; } 113 bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; } 114 unsigned int getStallMapSize() { return m_stall_msg_map.size(); } 115 116 unsigned int getSize(Tick curTime); 117 118 void clear(); 119 void print(std::ostream& out) const; 120 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; } 121 122 void setIncomingLink(int link_id) { m_input_link_id = link_id; } 123 void setVnet(int net) { m_vnet_id = net; } 124 125 Port & 126 getPort(const std::string &, PortID idx=InvalidPortID) override 127 { 128 return RubyDummyPort::instance(); 129 } 130 131 void regStats(); 132 133 // Function for figuring out if any of the messages in the buffer need 134 // to be updated with the data from the packet. 135 // Return value indicates the number of messages that were updated. 136 // This required for debugging the code. 137 uint32_t functionalWrite(Packet *pkt); 138 139 private: 140 void reanalyzeList(std::list<MsgPtr> &, Tick); 141 142 private: 143 // Data Members (m_ prefix) 144 //! Consumer to signal a wakeup(), can be NULL 145 Consumer* m_consumer; 146 std::vector<MsgPtr> m_prio_heap; 147 148 std::function<void()> m_dequeue_callback; 149 150 // use a std::map for the stalled messages as this container is 151 // sorted and ensures a well-defined iteration order 152 typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType; 153 154 /** 155 * A map from line addresses to lists of stalled messages for that line. 156 * If this buffer allows the receiver to stall messages, on a stall 157 * request, the stalled message is removed from the m_prio_heap and placed 158 * in the m_stall_msg_map. Messages are held there until the receiver 159 * requests they be reanalyzed, at which point they are moved back to 160 * m_prio_heap. 161 * 162 * NOTE: The stall map holds messages in the order in which they were 163 * initially received, and when a line is unblocked, the messages are 164 * moved back to the m_prio_heap in the same order. This prevents starving 165 * older requests with younger ones. 166 */ 167 StallMsgMapType m_stall_msg_map; 168 169 /** 170 * Current size of the stall map. 171 * Track the number of messages held in stall map lists. This is used to 172 * ensure that if the buffer is finite-sized, it blocks further requests 173 * when the m_prio_heap and m_stall_msg_map contain m_max_size messages. 174 */ 175 int m_stall_map_size; 176 177 /** 178 * The maximum capacity. For finite-sized buffers, m_max_size stores a 179 * number greater than 0 to indicate the maximum allowed number of messages 180 * in the buffer at any time. To get infinitely-sized buffers, set buffer 181 * size: m_max_size = 0 182 */ 183 const unsigned int m_max_size; 184 185 Tick m_time_last_time_size_checked; 186 unsigned int m_size_last_time_size_checked; 187 188 // variables used so enqueues appear to happen immediately, while 189 // pop happen the next cycle 190 Tick m_time_last_time_enqueue; 191 Tick m_time_last_time_pop; 192 Tick m_last_arrival_time; 193 194 unsigned int m_size_at_cycle_start; 195 unsigned int m_msgs_this_cycle; 196 197 Stats::Scalar m_not_avail_count; // count the # of times I didn't have N 198 // slots available 199 uint64_t m_msg_counter; 200 int m_priority_rank; 201 const bool m_strict_fifo; 202 const bool m_randomization; 203 204 int m_input_link_id; 205 int m_vnet_id; 206 207 Stats::Average m_buf_msgs; 208 Stats::Average m_stall_time; 209 Stats::Scalar m_stall_count; 210 Stats::Formula m_occupancy; 211}; 212 213Tick random_time(); 214 215inline std::ostream& 216operator<<(std::ostream& out, const MessageBuffer& obj) 217{ 218 obj.print(out); 219 out << std::flush; 220 return out; 221} 222 223#endif //__MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__ 224