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.
31 #include "base/cprintf.hh"
32 #include "base/misc.hh"
33 #include "base/random.hh"
34 #include "base/stl_helpers.hh"
35 #include "debug/RubyQueue.hh"
36 #include "mem/ruby/network/MessageBuffer.hh"
37 #include "mem/ruby/system/RubySystem.hh"
40 using m5::stl_helpers::operator<<;
42 MessageBuffer::MessageBuffer(const Params
*p
)
44 m_max_size(p
->buffer_size
), m_time_last_time_size_checked(0),
45 m_time_last_time_enqueue(0), m_time_last_time_pop(0),
46 m_last_arrival_time(0), m_strict_fifo(p
->ordered
),
47 m_randomization(p
->randomization
)
51 m_size_last_time_size_checked
= 0;
52 m_size_at_cycle_start
= 0;
53 m_msgs_this_cycle
= 0;
54 m_not_avail_count
= 0;
57 m_stall_msg_map
.clear();
63 MessageBuffer::getSize(Tick curTime
)
65 if (m_time_last_time_size_checked
!= curTime
) {
66 m_time_last_time_size_checked
= curTime
;
67 m_size_last_time_size_checked
= m_prio_heap
.size();
70 return m_size_last_time_size_checked
;
74 MessageBuffer::areNSlotsAvailable(unsigned int n
, Tick current_time
)
77 // fast path when message buffers have infinite size
78 if (m_max_size
== 0) {
82 // determine the correct size for the current cycle
83 // pop operations shouldn't effect the network's visible size
84 // until schd cycle, but enqueue operations effect the visible
86 unsigned int current_size
= 0;
88 if (m_time_last_time_pop
< current_time
) {
89 // no pops this cycle - heap size is correct
90 current_size
= m_prio_heap
.size();
92 if (m_time_last_time_enqueue
< current_time
) {
93 // no enqueues this cycle - m_size_at_cycle_start is correct
94 current_size
= m_size_at_cycle_start
;
96 // both pops and enqueues occured this cycle - add new
97 // enqueued msgs to m_size_at_cycle_start
98 current_size
= m_size_at_cycle_start
+ m_msgs_this_cycle
;
102 // now compare the new size with our max size
103 if (current_size
+ n
<= m_max_size
) {
106 DPRINTF(RubyQueue
, "n: %d, current_size: %d, heap size: %d, "
108 n
, current_size
, m_prio_heap
.size(), m_max_size
);
115 MessageBuffer::peek() const
117 DPRINTF(RubyQueue
, "Peeking at head of queue.\n");
118 const Message
* msg_ptr
= m_prio_heap
.front().get();
121 DPRINTF(RubyQueue
, "Message: %s\n", (*msg_ptr
));
125 // FIXME - move me somewhere else
130 time
+= random_mt
.random(0, 3); // [0...3]
131 if (random_mt
.random(0, 7) == 0) { // 1 in 8 chance
132 time
+= 100 + random_mt
.random(1, 15); // 100 + [1...15]
138 MessageBuffer::enqueue(MsgPtr message
, Tick current_time
, Tick delta
)
140 // record current time incase we have a pop that also adjusts my size
141 if (m_time_last_time_enqueue
< current_time
) {
142 m_msgs_this_cycle
= 0; // first msg this cycle
143 m_time_last_time_enqueue
= current_time
;
149 // Calculate the arrival time of the message, that is, the first
150 // cycle the message can be dequeued.
152 Tick arrival_time
= 0;
154 if (!RubySystem::getRandomization() || !m_randomization
) {
156 arrival_time
= current_time
+ delta
;
158 // Randomization - ignore delta
160 if (m_last_arrival_time
< current_time
) {
161 m_last_arrival_time
= current_time
;
163 arrival_time
= m_last_arrival_time
+ random_time();
165 arrival_time
= current_time
+ random_time();
169 // Check the arrival time
170 assert(arrival_time
> current_time
);
172 if (arrival_time
< m_last_arrival_time
) {
173 panic("FIFO ordering violated: %s name: %s current time: %d "
174 "delta: %d arrival_time: %d last arrival_time: %d\n",
175 *this, name(), current_time
, delta
, arrival_time
,
176 m_last_arrival_time
);
180 // If running a cache trace, don't worry about the last arrival checks
181 if (!RubySystem::getWarmupEnabled()) {
182 m_last_arrival_time
= arrival_time
;
185 // compute the delay cycles and set enqueue time
186 Message
* msg_ptr
= message
.get();
187 assert(msg_ptr
!= NULL
);
189 assert(current_time
>= msg_ptr
->getLastEnqueueTime() &&
190 "ensure we aren't dequeued early");
192 msg_ptr
->updateDelayedTicks(current_time
);
193 msg_ptr
->setLastEnqueueTime(arrival_time
);
194 msg_ptr
->setMsgCounter(m_msg_counter
);
196 // Insert the message into the priority heap
197 m_prio_heap
.push_back(message
);
198 push_heap(m_prio_heap
.begin(), m_prio_heap
.end(), greater
<MsgPtr
>());
200 DPRINTF(RubyQueue
, "Enqueue arrival_time: %lld, Message: %s\n",
201 arrival_time
, *(message
.get()));
203 // Schedule the wakeup
204 assert(m_consumer
!= NULL
);
205 m_consumer
->scheduleEventAbsolute(arrival_time
);
206 m_consumer
->storeEventInfo(m_vnet_id
);
210 MessageBuffer::dequeue(Tick current_time
)
212 DPRINTF(RubyQueue
, "Popping\n");
213 assert(isReady(current_time
));
215 // get MsgPtr of the message about to be dequeued
216 MsgPtr message
= m_prio_heap
.front();
218 // get the delay cycles
219 message
->updateDelayedTicks(current_time
);
220 Tick delay
= message
->getDelayedTicks();
222 // record previous size and time so the current buffer size isn't
223 // adjusted until schd cycle
224 if (m_time_last_time_pop
< current_time
) {
225 m_size_at_cycle_start
= m_prio_heap
.size();
226 m_time_last_time_pop
= current_time
;
229 pop_heap(m_prio_heap
.begin(), m_prio_heap
.end(), greater
<MsgPtr
>());
230 m_prio_heap
.pop_back();
236 MessageBuffer::clear()
241 m_time_last_time_enqueue
= 0;
242 m_time_last_time_pop
= 0;
243 m_size_at_cycle_start
= 0;
244 m_msgs_this_cycle
= 0;
248 MessageBuffer::recycle(Tick current_time
, Tick recycle_latency
)
250 DPRINTF(RubyQueue
, "Recycling.\n");
251 assert(isReady(current_time
));
252 MsgPtr node
= m_prio_heap
.front();
253 pop_heap(m_prio_heap
.begin(), m_prio_heap
.end(), greater
<MsgPtr
>());
255 Tick future_time
= current_time
+ recycle_latency
;
256 node
->setLastEnqueueTime(future_time
);
258 m_prio_heap
.back() = node
;
259 push_heap(m_prio_heap
.begin(), m_prio_heap
.end(), greater
<MsgPtr
>());
260 m_consumer
->scheduleEventAbsolute(future_time
);
264 MessageBuffer::reanalyzeList(list
<MsgPtr
> <
, Tick schdTick
)
266 while (!lt
.empty()) {
268 MsgPtr m
= lt
.front();
269 m
->setLastEnqueueTime(schdTick
);
270 m
->setMsgCounter(m_msg_counter
);
272 m_prio_heap
.push_back(m
);
273 push_heap(m_prio_heap
.begin(), m_prio_heap
.end(),
276 m_consumer
->scheduleEventAbsolute(schdTick
);
282 MessageBuffer::reanalyzeMessages(Addr addr
, Tick current_time
)
284 DPRINTF(RubyQueue
, "ReanalyzeMessages %#x\n", addr
);
285 assert(m_stall_msg_map
.count(addr
) > 0);
288 // Put all stalled messages associated with this address back on the
289 // prio heap. The reanalyzeList call will make sure the consumer is
290 // scheduled for the current cycle so that the previously stalled messages
291 // will be observed before any younger messages that may arrive this cycle
293 reanalyzeList(m_stall_msg_map
[addr
], current_time
);
294 m_stall_msg_map
.erase(addr
);
298 MessageBuffer::reanalyzeAllMessages(Tick current_time
)
300 DPRINTF(RubyQueue
, "ReanalyzeAllMessages\n");
303 // Put all stalled messages associated with this address back on the
304 // prio heap. The reanalyzeList call will make sure the consumer is
305 // scheduled for the current cycle so that the previously stalled messages
306 // will be observed before any younger messages that may arrive this cycle.
308 for (StallMsgMapType::iterator map_iter
= m_stall_msg_map
.begin();
309 map_iter
!= m_stall_msg_map
.end(); ++map_iter
) {
310 reanalyzeList(map_iter
->second
, current_time
);
312 m_stall_msg_map
.clear();
316 MessageBuffer::stallMessage(Addr addr
, Tick current_time
)
318 DPRINTF(RubyQueue
, "Stalling due to %#x\n", addr
);
319 assert(isReady(current_time
));
320 assert(getOffset(addr
) == 0);
321 MsgPtr message
= m_prio_heap
.front();
323 dequeue(current_time
);
326 // Note: no event is scheduled to analyze the map at a later time.
327 // Instead the controller is responsible to call reanalyzeMessages when
328 // these addresses change state.
330 (m_stall_msg_map
[addr
]).push_back(message
);
334 MessageBuffer::print(ostream
& out
) const
336 ccprintf(out
, "[MessageBuffer: ");
337 if (m_consumer
!= NULL
) {
338 ccprintf(out
, " consumer-yes ");
341 vector
<MsgPtr
> copy(m_prio_heap
);
342 sort_heap(copy
.begin(), copy
.end(), greater
<MsgPtr
>());
343 ccprintf(out
, "%s] %s", copy
, name());
347 MessageBuffer::isReady(Tick current_time
) const
349 return ((m_prio_heap
.size() > 0) &&
350 (m_prio_heap
.front()->getLastEnqueueTime() <= current_time
));
354 MessageBuffer::functionalWrite(Packet
*pkt
)
356 uint32_t num_functional_writes
= 0;
358 // Check the priority heap and write any messages that may
359 // correspond to the address in the packet.
360 for (unsigned int i
= 0; i
< m_prio_heap
.size(); ++i
) {
361 Message
*msg
= m_prio_heap
[i
].get();
362 if (msg
->functionalWrite(pkt
)) {
363 num_functional_writes
++;
367 // Check the stall queue and write any messages that may
368 // correspond to the address in the packet.
369 for (StallMsgMapType::iterator map_iter
= m_stall_msg_map
.begin();
370 map_iter
!= m_stall_msg_map
.end();
373 for (std::list
<MsgPtr
>::iterator it
= (map_iter
->second
).begin();
374 it
!= (map_iter
->second
).end(); ++it
) {
376 Message
*msg
= (*it
).get();
377 if (msg
->functionalWrite(pkt
)) {
378 num_functional_writes
++;
383 return num_functional_writes
;
387 MessageBufferParams::create()
389 return new MessageBuffer(this);