2 * Copyright (c) 2019,2020 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Unordered buffer of messages that can be inserted such
43 * that they can be dequeued after a given delta time has expired.
46 #ifndef __MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__
47 #define __MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__
54 #include <unordered_map>
57 #include "base/trace.hh"
58 #include "debug/RubyQueue.hh"
59 #include "mem/packet.hh"
60 #include "mem/port.hh"
61 #include "mem/ruby/common/Address.hh"
62 #include "mem/ruby/common/Consumer.hh"
63 #include "mem/ruby/network/dummy_port.hh"
64 #include "mem/ruby/slicc_interface/Message.hh"
65 #include "params/MessageBuffer.hh"
66 #include "sim/sim_object.hh"
68 class MessageBuffer : public SimObject
71 typedef MessageBufferParams Params;
72 MessageBuffer(const Params &p);
74 void reanalyzeMessages(Addr addr, Tick current_time);
75 void reanalyzeAllMessages(Tick current_time);
76 void stallMessage(Addr addr, Tick current_time);
77 // return true if the stall map has a message of this address
78 bool hasStalledMsg(Addr addr) const;
80 // TRUE if head of queue timestamp <= SystemTime
81 bool isReady(Tick current_time) const;
84 delayHead(Tick current_time, Tick delta)
86 MsgPtr m = m_prio_heap.front();
87 std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
88 std::greater<MsgPtr>());
89 m_prio_heap.pop_back();
90 enqueue(m, current_time, delta);
93 bool areNSlotsAvailable(unsigned int n, Tick curTime);
94 int getPriority() { return m_priority_rank; }
95 void setPriority(int rank) { m_priority_rank = rank; }
96 void setConsumer(Consumer* consumer)
98 DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer);
99 if (m_consumer != NULL) {
100 fatal("Trying to connect %s to MessageBuffer %s. \
101 \n%s already connected. Check the cntrl_id's.\n",
102 *consumer, *this, *m_consumer);
104 m_consumer = consumer;
107 Consumer* getConsumer() { return m_consumer; }
109 bool getOrdered() { return m_strict_fifo; }
111 //! Function for extracting the message at the head of the
112 //! message queue. The function assumes that the queue is nonempty.
113 const Message* peek() const;
115 const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
117 void enqueue(MsgPtr message, Tick curTime, Tick delta);
119 // Defer enqueueing a message to a later cycle by putting it aside and not
120 // enqueueing it in this cycle
121 // The corresponding controller will need to explicitly enqueue the
122 // deferred message into the message buffer. Otherwise, the message will
124 void deferEnqueueingMessage(Addr addr, MsgPtr message);
126 // enqueue all previously deferred messages that are associated with the
128 void enqueueDeferredMessages(Addr addr, Tick curTime, Tick delay);
129 bool isDeferredMsgMapEmpty(Addr addr) const;
131 //! Updates the delay cycles of the message at the head of the queue,
132 //! removes it from the queue and returns its total delay.
133 Tick dequeue(Tick current_time, bool decrement_messages = true);
135 void registerDequeueCallback(std::function<void()> callback);
136 void unregisterDequeueCallback();
138 void recycle(Tick current_time, Tick recycle_latency);
139 bool isEmpty() const { return m_prio_heap.size() == 0; }
140 bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
141 unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
143 unsigned int getSize(Tick curTime);
146 void print(std::ostream& out) const;
147 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
149 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
150 void setVnet(int net) { m_vnet_id = net; }
153 getPort(const std::string &, PortID idx=InvalidPortID) override
155 return RubyDummyPort::instance();
158 // Function for figuring out if any of the messages in the buffer need
159 // to be updated with the data from the packet.
160 // Return value indicates the number of messages that were updated.
161 uint32_t functionalWrite(Packet *pkt)
163 return functionalAccess(pkt, false);
166 // Function for figuring if message in the buffer has valid data for
168 // Returns true only if a message was found with valid data and the
169 // read was performed.
170 bool functionalRead(Packet *pkt)
172 return functionalAccess(pkt, true) == 1;
176 void reanalyzeList(std::list<MsgPtr> &, Tick);
178 uint32_t functionalAccess(Packet *pkt, bool is_read);
181 // Data Members (m_ prefix)
182 //! Consumer to signal a wakeup(), can be NULL
183 Consumer* m_consumer;
184 std::vector<MsgPtr> m_prio_heap;
186 std::function<void()> m_dequeue_callback;
188 // use a std::map for the stalled messages as this container is
189 // sorted and ensures a well-defined iteration order
190 typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
193 * A map from line addresses to lists of stalled messages for that line.
194 * If this buffer allows the receiver to stall messages, on a stall
195 * request, the stalled message is removed from the m_prio_heap and placed
196 * in the m_stall_msg_map. Messages are held there until the receiver
197 * requests they be reanalyzed, at which point they are moved back to
200 * NOTE: The stall map holds messages in the order in which they were
201 * initially received, and when a line is unblocked, the messages are
202 * moved back to the m_prio_heap in the same order. This prevents starving
203 * older requests with younger ones.
205 StallMsgMapType m_stall_msg_map;
208 * A map from line addresses to corresponding vectors of messages that
209 * are deferred for enqueueing. Messages in this map are waiting to be
210 * enqueued into the message buffer.
212 typedef std::unordered_map<Addr, std::vector<MsgPtr>> DeferredMsgMapType;
213 DeferredMsgMapType m_deferred_msg_map;
216 * Current size of the stall map.
217 * Track the number of messages held in stall map lists. This is used to
218 * ensure that if the buffer is finite-sized, it blocks further requests
219 * when the m_prio_heap and m_stall_msg_map contain m_max_size messages.
221 int m_stall_map_size;
224 * The maximum capacity. For finite-sized buffers, m_max_size stores a
225 * number greater than 0 to indicate the maximum allowed number of messages
226 * in the buffer at any time. To get infinitely-sized buffers, set buffer
227 * size: m_max_size = 0
229 const unsigned int m_max_size;
231 Tick m_time_last_time_size_checked;
232 unsigned int m_size_last_time_size_checked;
234 // variables used so enqueues appear to happen immediately, while
235 // pop happen the next cycle
236 Tick m_time_last_time_enqueue;
237 Tick m_time_last_time_pop;
238 Tick m_last_arrival_time;
240 unsigned int m_size_at_cycle_start;
241 unsigned int m_stalled_at_cycle_start;
242 unsigned int m_msgs_this_cycle;
244 uint64_t m_msg_counter;
246 const bool m_strict_fifo;
247 const MessageRandomization m_randomization;
248 const bool m_allow_zero_latency;
253 Stats::Scalar m_not_avail_count; // count the # of times I didn't have N
255 Stats::Average m_buf_msgs;
256 Stats::Average m_stall_time;
257 Stats::Scalar m_stall_count;
258 Stats::Formula m_occupancy;
264 operator<<(std::ostream& out, const MessageBuffer& obj)
271 #endif //__MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__