MessageBuffer.hh (11732:e15e445c21a6) MessageBuffer.hh (11779:25dd0fd23474)
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 "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"
51
52class MessageBuffer : public SimObject
53{
54 public:
55 typedef MessageBufferParams Params;
56 MessageBuffer(const Params *p);
57
58 void reanalyzeMessages(Addr addr, Tick current_time);
59 void reanalyzeAllMessages(Tick current_time);
60 void stallMessage(Addr addr, Tick current_time);
61
62 // TRUE if head of queue timestamp <= SystemTime
63 bool isReady(Tick current_time) const;
64
65 void
66 delayHead(Tick current_time, Tick delta)
67 {
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, current_time, delta);
73 }
74
75 bool areNSlotsAvailable(unsigned int n, Tick curTime);
76 int getPriority() { return m_priority_rank; }
77 void setPriority(int rank) { m_priority_rank = rank; }
78 void setConsumer(Consumer* consumer)
79 {
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);
85 }
86 m_consumer = consumer;
87 }
88
89 Consumer* getConsumer() { return m_consumer; }
90
91 bool getOrdered() { return m_strict_fifo; }
92
93 //! Function for extracting the message at the head of the
94 //! message queue. The function assumes that the queue is nonempty.
95 const Message* peek() const;
96
97 const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
98
99 void enqueue(MsgPtr message, Tick curTime, Tick delta);
100
101 //! Updates the delay cycles of the message at the head of the queue,
102 //! removes it from the queue and returns its total delay.
103 Tick dequeue(Tick current_time);
104
105 void recycle(Tick current_time, Tick recycle_latency);
106 bool isEmpty() const { return m_prio_heap.size() == 0; }
107 bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
108 unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
109
110 unsigned int getSize(Tick curTime);
111
112 void clear();
113 void print(std::ostream& out) const;
114 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
115
116 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
117 void setVnet(int net) { m_vnet_id = net; }
118
119 void regStats();
120
121 // Function for figuring out if any of the messages in the buffer need
122 // to be updated with the data from the packet.
123 // Return value indicates the number of messages that were updated.
124 // This required for debugging the code.
125 uint32_t functionalWrite(Packet *pkt);
126
127 private:
128 void reanalyzeList(std::list<MsgPtr> &, Tick);
129
130 private:
131 // Data Members (m_ prefix)
132 //! Consumer to signal a wakeup(), can be NULL
133 Consumer* m_consumer;
134 std::vector<MsgPtr> m_prio_heap;
135
136 // use a std::map for the stalled messages as this container is
137 // sorted and ensures a well-defined iteration order
138 typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
139
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 "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"
51
52class MessageBuffer : public SimObject
53{
54 public:
55 typedef MessageBufferParams Params;
56 MessageBuffer(const Params *p);
57
58 void reanalyzeMessages(Addr addr, Tick current_time);
59 void reanalyzeAllMessages(Tick current_time);
60 void stallMessage(Addr addr, Tick current_time);
61
62 // TRUE if head of queue timestamp <= SystemTime
63 bool isReady(Tick current_time) const;
64
65 void
66 delayHead(Tick current_time, Tick delta)
67 {
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, current_time, delta);
73 }
74
75 bool areNSlotsAvailable(unsigned int n, Tick curTime);
76 int getPriority() { return m_priority_rank; }
77 void setPriority(int rank) { m_priority_rank = rank; }
78 void setConsumer(Consumer* consumer)
79 {
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);
85 }
86 m_consumer = consumer;
87 }
88
89 Consumer* getConsumer() { return m_consumer; }
90
91 bool getOrdered() { return m_strict_fifo; }
92
93 //! Function for extracting the message at the head of the
94 //! message queue. The function assumes that the queue is nonempty.
95 const Message* peek() const;
96
97 const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
98
99 void enqueue(MsgPtr message, Tick curTime, Tick delta);
100
101 //! Updates the delay cycles of the message at the head of the queue,
102 //! removes it from the queue and returns its total delay.
103 Tick dequeue(Tick current_time);
104
105 void recycle(Tick current_time, Tick recycle_latency);
106 bool isEmpty() const { return m_prio_heap.size() == 0; }
107 bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
108 unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
109
110 unsigned int getSize(Tick curTime);
111
112 void clear();
113 void print(std::ostream& out) const;
114 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
115
116 void setIncomingLink(int link_id) { m_input_link_id = link_id; }
117 void setVnet(int net) { m_vnet_id = net; }
118
119 void regStats();
120
121 // Function for figuring out if any of the messages in the buffer need
122 // to be updated with the data from the packet.
123 // Return value indicates the number of messages that were updated.
124 // This required for debugging the code.
125 uint32_t functionalWrite(Packet *pkt);
126
127 private:
128 void reanalyzeList(std::list<MsgPtr> &, Tick);
129
130 private:
131 // Data Members (m_ prefix)
132 //! Consumer to signal a wakeup(), can be NULL
133 Consumer* m_consumer;
134 std::vector<MsgPtr> m_prio_heap;
135
136 // use a std::map for the stalled messages as this container is
137 // sorted and ensures a well-defined iteration order
138 typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
139
140 /**
141 * A map from line addresses to lists of stalled messages for that line.
142 * If this buffer allows the receiver to stall messages, on a stall
143 * request, the stalled message is removed from the m_prio_heap and placed
144 * in the m_stall_msg_map. Messages are held there until the receiver
145 * requests they be reanalyzed, at which point they are moved back to
146 * m_prio_heap.
147 *
148 * NOTE: The stall map holds messages in the order in which they were
149 * initially received, and when a line is unblocked, the messages are
150 * moved back to the m_prio_heap in the same order. This prevents starving
151 * older requests with younger ones.
152 */
140 StallMsgMapType m_stall_msg_map;
141
153 StallMsgMapType m_stall_msg_map;
154
155 /**
156 * Current size of the stall map.
157 * Track the number of messages held in stall map lists. This is used to
158 * ensure that if the buffer is finite-sized, it blocks further requests
159 * when the m_prio_heap and m_stall_msg_map contain m_max_size messages.
160 */
161 int m_stall_map_size;
162
163 /**
164 * The maximum capacity. For finite-sized buffers, m_max_size stores a
165 * number greater than 0 to indicate the maximum allowed number of messages
166 * in the buffer at any time. To get infinitely-sized buffers, set buffer
167 * size: m_max_size = 0
168 */
142 const unsigned int m_max_size;
169 const unsigned int m_max_size;
170
143 Tick m_time_last_time_size_checked;
144 unsigned int m_size_last_time_size_checked;
145
146 // variables used so enqueues appear to happen immediately, while
147 // pop happen the next cycle
148 Tick m_time_last_time_enqueue;
149 Tick m_time_last_time_pop;
150 Tick m_last_arrival_time;
151
152 unsigned int m_size_at_cycle_start;
153 unsigned int m_msgs_this_cycle;
154
155 Stats::Scalar m_not_avail_count; // count the # of times I didn't have N
156 // slots available
157 uint64_t m_msg_counter;
158 int m_priority_rank;
159 const bool m_strict_fifo;
160 const bool m_randomization;
161
162 int m_input_link_id;
163 int m_vnet_id;
164};
165
166Tick random_time();
167
168inline std::ostream&
169operator<<(std::ostream& out, const MessageBuffer& obj)
170{
171 obj.print(out);
172 out << std::flush;
173 return out;
174}
175
176#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
171 Tick m_time_last_time_size_checked;
172 unsigned int m_size_last_time_size_checked;
173
174 // variables used so enqueues appear to happen immediately, while
175 // pop happen the next cycle
176 Tick m_time_last_time_enqueue;
177 Tick m_time_last_time_pop;
178 Tick m_last_arrival_time;
179
180 unsigned int m_size_at_cycle_start;
181 unsigned int m_msgs_this_cycle;
182
183 Stats::Scalar m_not_avail_count; // count the # of times I didn't have N
184 // slots available
185 uint64_t m_msg_counter;
186 int m_priority_rank;
187 const bool m_strict_fifo;
188 const bool m_randomization;
189
190 int m_input_link_id;
191 int m_vnet_id;
192};
193
194Tick random_time();
195
196inline std::ostream&
197operator<<(std::ostream& out, const MessageBuffer& obj)
198{
199 obj.print(out);
200 out << std::flush;
201 return out;
202}
203
204#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__