2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
30 * Unordered buffer of messages that can be inserted such
31 * that they can be dequeued after a given delta time has expired.
34 #ifndef __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
35 #define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
44 #include "debug/RubyQueue.hh"
45 #include "mem/ruby/common/Address.hh"
46 #include "mem/ruby/common/Consumer.hh"
47 #include "mem/ruby/slicc_interface/Message.hh"
48 #include "mem/packet.hh"
49 #include "params/MessageBuffer.hh"
50 #include "sim/sim_object.hh"
52 class MessageBuffer : public SimObject
55 typedef MessageBufferParams Params;
56 MessageBuffer(const Params *p);
58 void reanalyzeMessages(Addr addr);
59 void reanalyzeAllMessages();
60 void stallMessage(Addr addr);
62 // TRUE if head of queue timestamp <= SystemTime
68 MsgPtr m = m_prio_heap.front();
69 std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
70 std::greater<MsgPtr>());
71 m_prio_heap.pop_back();
72 enqueue(m, Cycles(1));
75 bool areNSlotsAvailable(unsigned int n);
76 int getPriority() { return m_priority_rank; }
77 void setPriority(int rank) { m_priority_rank = rank; }
78 void setConsumer(Consumer* consumer)
80 DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer);
81 if (m_consumer != NULL) {
82 fatal("Trying to connect %s to MessageBuffer %s. \
83 \n%s already connected. Check the cntrl_id's.\n",
84 *consumer, *this, *m_consumer);
86 m_consumer = consumer;
89 void setSender(ClockedObject* obj)
91 DPRINTF(RubyQueue, "Setting sender: %s\n", obj->name());
92 assert(m_sender == NULL || m_sender == obj);
96 void setReceiver(ClockedObject* obj)
98 DPRINTF(RubyQueue, "Setting receiver: %s\n", obj->name());
99 assert(m_receiver == NULL || m_receiver == obj);
103 Consumer* getConsumer() { return m_consumer; }
105 bool getOrdered() { return m_strict_fifo; }
107 //! Function for extracting the message at the head of the
108 //! message queue. The function assumes that the queue is nonempty.
109 const Message* peek() const;
115 return m_prio_heap.front();
118 void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
119 void enqueue(MsgPtr message, Cycles delta);
121 //! Updates the delay cycles of the message at the head of the queue,
122 //! removes it from the queue and returns its total delay.
126 bool isEmpty() const { return m_prio_heap.size() == 0; }
127 bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
128 unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
130 unsigned int getSize();
133 void print(std::ostream& out) const;
134 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
136 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
137 void setVnet(int net) { m_vnet_id = net; }
139 // Function for figuring out if any of the messages in the buffer need
140 // to be updated with the data from the packet.
141 // Return value indicates the number of messages that were updated.
142 // This required for debugging the code.
143 uint32_t functionalWrite(Packet *pkt);
147 const Cycles m_recycle_latency;
149 void reanalyzeList(std::list<MsgPtr> &, Tick);
152 // Data Members (m_ prefix)
153 //! The two ends of the buffer.
154 ClockedObject* m_sender;
155 ClockedObject* m_receiver;
157 //! Consumer to signal a wakeup(), can be NULL
158 Consumer* m_consumer;
159 std::vector<MsgPtr> m_prio_heap;
161 // use a std::map for the stalled messages as this container is
162 // sorted and ensures a well-defined iteration order
163 typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
165 StallMsgMapType m_stall_msg_map;
167 const unsigned int m_max_size;
168 Cycles m_time_last_time_size_checked;
169 unsigned int m_size_last_time_size_checked;
171 // variables used so enqueues appear to happen immediately, while
172 // pop happen the next cycle
173 Cycles m_time_last_time_enqueue;
174 Tick m_time_last_time_pop;
175 Tick m_last_arrival_time;
177 unsigned int m_size_at_cycle_start;
178 unsigned int m_msgs_this_cycle;
180 int m_not_avail_count; // count the # of times I didn't have N
182 uint64_t m_msg_counter;
184 const bool m_strict_fifo;
185 const bool m_randomization;
191 Cycles random_time();
194 operator<<(std::ostream& out, const MessageBuffer& obj)
201 #endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__