MessageBuffer.hh (10301:44839e8febbd) MessageBuffer.hh (10893:f567e80c0714)
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_BUFFERS_MESSAGEBUFFER_HH__
35#define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
36
37#include <algorithm>
38#include <cassert>
39#include <functional>
40#include <iostream>
41#include <string>
42#include <vector>
43
44#include "mem/ruby/common/Address.hh"
45#include "mem/ruby/common/Consumer.hh"
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_BUFFERS_MESSAGEBUFFER_HH__
35#define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
36
37#include <algorithm>
38#include <cassert>
39#include <functional>
40#include <iostream>
41#include <string>
42#include <vector>
43
44#include "mem/ruby/common/Address.hh"
45#include "mem/ruby/common/Consumer.hh"
46#include "mem/ruby/network/MessageBufferNode.hh"
47#include "mem/ruby/slicc_interface/Message.hh"
48#include "mem/packet.hh"
49
50class MessageBuffer
51{
52 public:
53 MessageBuffer(const std::string &name = "");
54
55 std::string name() const { return m_name; }
56
57 void setRecycleLatency(Cycles recycle_latency)
58 { m_recycle_latency = recycle_latency; }
59
60 void reanalyzeMessages(const Address& addr);
61 void reanalyzeAllMessages();
62 void stallMessage(const Address& addr);
63
64 // TRUE if head of queue timestamp <= SystemTime
65 bool isReady() const;
66
67 void
68 delayHead()
69 {
46#include "mem/ruby/slicc_interface/Message.hh"
47#include "mem/packet.hh"
48
49class MessageBuffer
50{
51 public:
52 MessageBuffer(const std::string &name = "");
53
54 std::string name() const { return m_name; }
55
56 void setRecycleLatency(Cycles recycle_latency)
57 { m_recycle_latency = recycle_latency; }
58
59 void reanalyzeMessages(const Address& addr);
60 void reanalyzeAllMessages();
61 void stallMessage(const Address& addr);
62
63 // TRUE if head of queue timestamp <= SystemTime
64 bool isReady() const;
65
66 void
67 delayHead()
68 {
70 MessageBufferNode node = m_prio_heap.front();
69 MsgPtr m = m_prio_heap.front();
71 std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
70 std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
72 std::greater<MessageBufferNode>());
71 std::greater<MsgPtr>());
73 m_prio_heap.pop_back();
72 m_prio_heap.pop_back();
74 enqueue(node.m_msgptr, Cycles(1));
73 enqueue(m, Cycles(1));
75 }
76
77 bool areNSlotsAvailable(unsigned int n);
78 int getPriority() { return m_priority_rank; }
79 void setPriority(int rank) { m_priority_rank = rank; }
80 void setConsumer(Consumer* consumer)
81 {
82 if (m_consumer != NULL) {
83 fatal("Trying to connect %s to MessageBuffer %s. \
84 \n%s already connected. Check the cntrl_id's.\n",
85 *consumer, *this, *m_consumer);
86 }
87 m_consumer = consumer;
88 }
89
90 void setSender(ClockedObject* obj)
91 {
92 assert(m_sender == NULL || m_sender == obj);
93 m_sender = obj;
94 }
95
96 void setReceiver(ClockedObject* obj)
97 {
98 assert(m_receiver == NULL || m_receiver == obj);
99 m_receiver = obj;
100 }
101
102 void setDescription(const std::string& name) { m_name = name; }
103 std::string getDescription() { return m_name;}
104
105 Consumer* getConsumer() { return m_consumer; }
106
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;
110
111 const MsgPtr&
112 peekMsgPtr() const
113 {
114 assert(isReady());
74 }
75
76 bool areNSlotsAvailable(unsigned int n);
77 int getPriority() { return m_priority_rank; }
78 void setPriority(int rank) { m_priority_rank = rank; }
79 void setConsumer(Consumer* consumer)
80 {
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);
85 }
86 m_consumer = consumer;
87 }
88
89 void setSender(ClockedObject* obj)
90 {
91 assert(m_sender == NULL || m_sender == obj);
92 m_sender = obj;
93 }
94
95 void setReceiver(ClockedObject* obj)
96 {
97 assert(m_receiver == NULL || m_receiver == obj);
98 m_receiver = obj;
99 }
100
101 void setDescription(const std::string& name) { m_name = name; }
102 std::string getDescription() { return m_name;}
103
104 Consumer* getConsumer() { return m_consumer; }
105
106 //! Function for extracting the message at the head of the
107 //! message queue. The function assumes that the queue is nonempty.
108 const Message* peek() const;
109
110 const MsgPtr&
111 peekMsgPtr() const
112 {
113 assert(isReady());
115 return m_prio_heap.front().m_msgptr;
114 return m_prio_heap.front();
116 }
117
118 void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
119 void enqueue(MsgPtr message, Cycles delta);
120
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.
123 Cycles dequeue();
124
125 void recycle();
126 bool isEmpty() const { return m_prio_heap.size() == 0; }
127
128 void
129 setOrdering(bool order)
130 {
131 m_strict_fifo = order;
132 m_ordering_set = true;
133 }
134
135 void resize(unsigned int size) { m_max_size = size; }
136 unsigned int getSize();
137 void setRandomization(bool random_flag) { m_randomization = random_flag; }
138
139 void clear();
140 void print(std::ostream& out) const;
141 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
142
143 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
144 void setVnet(int net) { m_vnet_id = net; }
145
146 // Function for figuring out if any of the messages in the buffer can
147 // satisfy the read request for the address in the packet.
148 // Return value, if true, indicates that the request was fulfilled.
149 bool functionalRead(Packet *pkt);
150
151 // Function for figuring out if any of the messages in the buffer need
152 // to be updated with the data from the packet.
153 // Return value indicates the number of messages that were updated.
154 // This required for debugging the code.
155 uint32_t functionalWrite(Packet *pkt);
156
157 private:
158 void reanalyzeList(std::list<MsgPtr> &, Tick);
159
160 private:
161 //added by SS
162 Cycles m_recycle_latency;
163
164 // Data Members (m_ prefix)
165 //! The two ends of the buffer.
166 ClockedObject* m_sender;
167 ClockedObject* m_receiver;
168
169 //! Consumer to signal a wakeup(), can be NULL
170 Consumer* m_consumer;
115 }
116
117 void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
118 void enqueue(MsgPtr message, Cycles delta);
119
120 //! Updates the delay cycles of the message at the head of the queue,
121 //! removes it from the queue and returns its total delay.
122 Cycles dequeue();
123
124 void recycle();
125 bool isEmpty() const { return m_prio_heap.size() == 0; }
126
127 void
128 setOrdering(bool order)
129 {
130 m_strict_fifo = order;
131 m_ordering_set = true;
132 }
133
134 void resize(unsigned int size) { m_max_size = size; }
135 unsigned int getSize();
136 void setRandomization(bool random_flag) { m_randomization = random_flag; }
137
138 void clear();
139 void print(std::ostream& out) const;
140 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
141
142 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
143 void setVnet(int net) { m_vnet_id = net; }
144
145 // Function for figuring out if any of the messages in the buffer can
146 // satisfy the read request for the address in the packet.
147 // Return value, if true, indicates that the request was fulfilled.
148 bool functionalRead(Packet *pkt);
149
150 // Function for figuring out if any of the messages in the buffer need
151 // to be updated with the data from the packet.
152 // Return value indicates the number of messages that were updated.
153 // This required for debugging the code.
154 uint32_t functionalWrite(Packet *pkt);
155
156 private:
157 void reanalyzeList(std::list<MsgPtr> &, Tick);
158
159 private:
160 //added by SS
161 Cycles m_recycle_latency;
162
163 // Data Members (m_ prefix)
164 //! The two ends of the buffer.
165 ClockedObject* m_sender;
166 ClockedObject* m_receiver;
167
168 //! Consumer to signal a wakeup(), can be NULL
169 Consumer* m_consumer;
171 std::vector<MessageBufferNode> m_prio_heap;
170 std::vector<MsgPtr> m_prio_heap;
172
173 // use a std::map for the stalled messages as this container is
174 // sorted and ensures a well-defined iteration order
175 typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
176
177 StallMsgMapType m_stall_msg_map;
178 std::string m_name;
179
180 unsigned int m_max_size;
181 Cycles m_time_last_time_size_checked;
182 unsigned int m_size_last_time_size_checked;
183
184 // variables used so enqueues appear to happen imediately, while
185 // pop happen the next cycle
186 Cycles m_time_last_time_enqueue;
187 Tick m_time_last_time_pop;
188 Tick m_last_arrival_time;
189
190 unsigned int m_size_at_cycle_start;
191 unsigned int m_msgs_this_cycle;
192
193 int m_not_avail_count; // count the # of times I didn't have N
194 // slots available
195 uint64 m_msg_counter;
196 int m_priority_rank;
197 bool m_strict_fifo;
198 bool m_ordering_set;
199 bool m_randomization;
200
201 int m_input_link_id;
202 int m_vnet_id;
203};
204
205Cycles random_time();
206
207inline std::ostream&
208operator<<(std::ostream& out, const MessageBuffer& obj)
209{
210 obj.print(out);
211 out << std::flush;
212 return out;
213}
214
215#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
171
172 // use a std::map for the stalled messages as this container is
173 // sorted and ensures a well-defined iteration order
174 typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
175
176 StallMsgMapType m_stall_msg_map;
177 std::string m_name;
178
179 unsigned int m_max_size;
180 Cycles m_time_last_time_size_checked;
181 unsigned int m_size_last_time_size_checked;
182
183 // variables used so enqueues appear to happen imediately, while
184 // pop happen the next cycle
185 Cycles m_time_last_time_enqueue;
186 Tick m_time_last_time_pop;
187 Tick m_last_arrival_time;
188
189 unsigned int m_size_at_cycle_start;
190 unsigned int m_msgs_this_cycle;
191
192 int m_not_avail_count; // count the # of times I didn't have N
193 // slots available
194 uint64 m_msg_counter;
195 int m_priority_rank;
196 bool m_strict_fifo;
197 bool m_ordering_set;
198 bool m_randomization;
199
200 int m_input_link_id;
201 int m_vnet_id;
202};
203
204Cycles random_time();
205
206inline std::ostream&
207operator<<(std::ostream& out, const MessageBuffer& obj)
208{
209 obj.print(out);
210 out << std::flush;
211 return out;
212}
213
214#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__