Deleted Added
sdiff udiff text old ( 10301:44839e8febbd ) new ( 10348:c91b23c72d5e )
full compact
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#include <cassert>
30
31#include "base/cprintf.hh"
32#include "base/misc.hh"
33#include "base/stl_helpers.hh"
34#include "debug/RubyQueue.hh"
35#include "mem/ruby/network/MessageBuffer.hh"
36#include "mem/ruby/system/System.hh"
37
38using namespace std;
39using m5::stl_helpers::operator<<;
40
41MessageBuffer::MessageBuffer(const string &name)
42 : m_time_last_time_size_checked(0), m_time_last_time_enqueue(0),
43 m_time_last_time_pop(0), m_last_arrival_time(0)
44{
45 m_msg_counter = 0;
46 m_consumer = NULL;
47 m_sender = NULL;
48 m_receiver = NULL;
49
50 m_ordering_set = false;
51 m_strict_fifo = true;
52 m_max_size = 0;
53 m_randomization = true;
54 m_size_last_time_size_checked = 0;
55 m_size_at_cycle_start = 0;
56 m_msgs_this_cycle = 0;
57 m_not_avail_count = 0;
58 m_priority_rank = 0;
59 m_name = name;
60
61 m_stall_msg_map.clear();
62 m_input_link_id = 0;
63 m_vnet_id = 0;
64}
65
66unsigned int
67MessageBuffer::getSize()
68{
69 if (m_time_last_time_size_checked != m_receiver->curCycle()) {
70 m_time_last_time_size_checked = m_receiver->curCycle();
71 m_size_last_time_size_checked = m_prio_heap.size();
72 }
73
74 return m_size_last_time_size_checked;
75}
76
77bool
78MessageBuffer::areNSlotsAvailable(unsigned int n)
79{
80
81 // fast path when message buffers have infinite size
82 if (m_max_size == 0) {
83 return true;
84 }
85
86 // determine the correct size for the current cycle
87 // pop operations shouldn't effect the network's visible size
88 // until next cycle, but enqueue operations effect the visible
89 // size immediately
90 unsigned int current_size = 0;
91
92 if (m_time_last_time_pop < m_sender->clockEdge()) {
93 // no pops this cycle - heap size is correct
94 current_size = m_prio_heap.size();
95 } else {
96 if (m_time_last_time_enqueue < m_sender->curCycle()) {
97 // no enqueues this cycle - m_size_at_cycle_start is correct
98 current_size = m_size_at_cycle_start;
99 } else {
100 // both pops and enqueues occured this cycle - add new
101 // enqueued msgs to m_size_at_cycle_start
102 current_size = m_size_at_cycle_start + m_msgs_this_cycle;
103 }
104 }
105
106 // now compare the new size with our max size
107 if (current_size + n <= m_max_size) {
108 return true;
109 } else {
110 DPRINTF(RubyQueue, "n: %d, current_size: %d, heap size: %d, "
111 "m_max_size: %d\n",
112 n, current_size, m_prio_heap.size(), m_max_size);
113 m_not_avail_count++;
114 return false;
115 }
116}
117
118const Message*
119MessageBuffer::peek() const
120{
121 DPRINTF(RubyQueue, "Peeking at head of queue.\n");
122 assert(isReady());
123
124 const Message* msg_ptr = m_prio_heap.front().m_msgptr.get();
125 assert(msg_ptr);
126
127 DPRINTF(RubyQueue, "Message: %s\n", (*msg_ptr));
128 return msg_ptr;
129}
130
131// FIXME - move me somewhere else
132Cycles
133random_time()
134{
135 Cycles time(1);
136 time += Cycles(random() & 0x3); // [0...3]
137 if ((random() & 0x7) == 0) { // 1 in 8 chance
138 time += Cycles(100 + (random() % 0xf)); // 100 + [1...15]
139 }
140 return time;
141}
142
143void
144MessageBuffer::enqueue(MsgPtr message, Cycles delta)
145{
146 m_msg_counter++;
147
148 // record current time incase we have a pop that also adjusts my size
149 if (m_time_last_time_enqueue < m_sender->curCycle()) {
150 m_msgs_this_cycle = 0; // first msg this cycle
151 m_time_last_time_enqueue = m_sender->curCycle();
152 }
153 m_msgs_this_cycle++;
154
155 assert(m_ordering_set);
156
157 // Calculate the arrival time of the message, that is, the first
158 // cycle the message can be dequeued.
159 assert(delta > 0);
160 Tick current_time = m_sender->clockEdge();
161 Tick arrival_time = 0;
162
163 if (!RubySystem::getRandomization() || !m_randomization) {
164 // No randomization
165 arrival_time = current_time + delta * m_sender->clockPeriod();
166 } else {
167 // Randomization - ignore delta
168 if (m_strict_fifo) {
169 if (m_last_arrival_time < current_time) {
170 m_last_arrival_time = current_time;
171 }
172 arrival_time = m_last_arrival_time +
173 random_time() * m_sender->clockPeriod();
174 } else {
175 arrival_time = current_time +
176 random_time() * m_sender->clockPeriod();
177 }
178 }
179
180 // Check the arrival time
181 assert(arrival_time > current_time);
182 if (m_strict_fifo) {
183 if (arrival_time < m_last_arrival_time) {
184 panic("FIFO ordering violated: %s name: %s current time: %d "
185 "delta: %d arrival_time: %d last arrival_time: %d\n",
186 *this, m_name, current_time,
187 delta * m_sender->clockPeriod(),
188 arrival_time, m_last_arrival_time);
189 }
190 }
191
192 // If running a cache trace, don't worry about the last arrival checks
193 if (!g_system_ptr->m_warmup_enabled) {
194 m_last_arrival_time = arrival_time;
195 }
196
197 // compute the delay cycles and set enqueue time
198 Message* msg_ptr = message.get();
199 assert(msg_ptr != NULL);
200
201 assert(m_sender->clockEdge() >= msg_ptr->getLastEnqueueTime() &&
202 "ensure we aren't dequeued early");
203
204 msg_ptr->updateDelayedTicks(m_sender->clockEdge());
205 msg_ptr->setLastEnqueueTime(arrival_time);
206
207 // Insert the message into the priority heap
208 MessageBufferNode thisNode(arrival_time, m_msg_counter, message);
209 m_prio_heap.push_back(thisNode);
210 push_heap(m_prio_heap.begin(), m_prio_heap.end(),
211 greater<MessageBufferNode>());
212
213 DPRINTF(RubyQueue, "Enqueue arrival_time: %lld, Message: %s\n",
214 arrival_time, *(message.get()));
215
216 // Schedule the wakeup
217 assert(m_consumer != NULL);
218 m_consumer->scheduleEventAbsolute(arrival_time);
219 m_consumer->storeEventInfo(m_vnet_id);
220}
221
222Cycles
223MessageBuffer::dequeue()
224{
225 DPRINTF(RubyQueue, "Popping\n");
226 assert(isReady());
227
228 // get MsgPtr of the message about to be dequeued
229 MsgPtr message = m_prio_heap.front().m_msgptr;
230
231 // get the delay cycles
232 message->updateDelayedTicks(m_receiver->clockEdge());
233 Cycles delayCycles =
234 m_receiver->ticksToCycles(message->getDelayedTicks());
235
236 // record previous size and time so the current buffer size isn't
237 // adjusted until next cycle
238 if (m_time_last_time_pop < m_receiver->clockEdge()) {
239 m_size_at_cycle_start = m_prio_heap.size();
240 m_time_last_time_pop = m_receiver->clockEdge();
241 }
242
243 pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
244 greater<MessageBufferNode>());
245 m_prio_heap.pop_back();
246
247 return delayCycles;
248}
249
250void
251MessageBuffer::clear()
252{
253 m_prio_heap.clear();
254
255 m_msg_counter = 0;
256 m_time_last_time_enqueue = Cycles(0);
257 m_time_last_time_pop = 0;
258 m_size_at_cycle_start = 0;
259 m_msgs_this_cycle = 0;
260}
261
262void
263MessageBuffer::recycle()
264{
265 DPRINTF(RubyQueue, "Recycling.\n");
266 assert(isReady());
267 MessageBufferNode node = m_prio_heap.front();
268 pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
269 greater<MessageBufferNode>());
270
271 node.m_time = m_receiver->clockEdge(m_recycle_latency);
272 m_prio_heap.back() = node;
273 push_heap(m_prio_heap.begin(), m_prio_heap.end(),
274 greater<MessageBufferNode>());
275 m_consumer->
276 scheduleEventAbsolute(m_receiver->clockEdge(m_recycle_latency));
277}
278
279void
280MessageBuffer::reanalyzeList(list<MsgPtr> &lt, Tick nextTick)
281{
282 while(!lt.empty()) {
283 m_msg_counter++;
284 MessageBufferNode msgNode(nextTick, m_msg_counter, lt.front());
285
286 m_prio_heap.push_back(msgNode);
287 push_heap(m_prio_heap.begin(), m_prio_heap.end(),
288 greater<MessageBufferNode>());
289
290 m_consumer->scheduleEventAbsolute(nextTick);
291 lt.pop_front();
292 }
293}
294
295void
296MessageBuffer::reanalyzeMessages(const Address& addr)
297{
298 DPRINTF(RubyQueue, "ReanalyzeMessages\n");
299 assert(m_stall_msg_map.count(addr) > 0);
300 Tick nextTick = m_receiver->clockEdge(Cycles(1));
301
302 //
303 // Put all stalled messages associated with this address back on the
304 // prio heap
305 //
306 reanalyzeList(m_stall_msg_map[addr], nextTick);
307 m_stall_msg_map.erase(addr);
308}
309
310void
311MessageBuffer::reanalyzeAllMessages()
312{
313 DPRINTF(RubyQueue, "ReanalyzeAllMessages\n");
314 Tick nextTick = m_receiver->clockEdge(Cycles(1));
315
316 //
317 // Put all stalled messages associated with this address back on the
318 // prio heap
319 //
320 for (StallMsgMapType::iterator map_iter = m_stall_msg_map.begin();
321 map_iter != m_stall_msg_map.end(); ++map_iter) {
322 reanalyzeList(map_iter->second, nextTick);
323 }
324 m_stall_msg_map.clear();
325}
326
327void
328MessageBuffer::stallMessage(const Address& addr)
329{
330 DPRINTF(RubyQueue, "Stalling due to %s\n", addr);
331 assert(isReady());
332 assert(addr.getOffset() == 0);
333 MsgPtr message = m_prio_heap.front().m_msgptr;
334
335 dequeue();
336
337 //
338 // Note: no event is scheduled to analyze the map at a later time.
339 // Instead the controller is responsible to call reanalyzeMessages when
340 // these addresses change state.
341 //
342 (m_stall_msg_map[addr]).push_back(message);
343}
344
345void
346MessageBuffer::print(ostream& out) const
347{
348 ccprintf(out, "[MessageBuffer: ");
349 if (m_consumer != NULL) {
350 ccprintf(out, " consumer-yes ");
351 }
352
353 vector<MessageBufferNode> copy(m_prio_heap);
354 sort_heap(copy.begin(), copy.end(), greater<MessageBufferNode>());
355 ccprintf(out, "%s] %s", copy, m_name);
356}
357
358bool
359MessageBuffer::isReady() const
360{
361 return ((m_prio_heap.size() > 0) &&
362 (m_prio_heap.front().m_time <= m_receiver->clockEdge()));
363}
364
365bool
366MessageBuffer::functionalRead(Packet *pkt)
367{
368 // Check the priority heap and read any messages that may
369 // correspond to the address in the packet.
370 for (unsigned int i = 0; i < m_prio_heap.size(); ++i) {
371 Message *msg = m_prio_heap[i].m_msgptr.get();
372 if (msg->functionalRead(pkt)) return true;
373 }
374
375 // Read the messages in the stall queue that correspond
376 // to the address in the packet.
377 for (StallMsgMapType::iterator map_iter = m_stall_msg_map.begin();
378 map_iter != m_stall_msg_map.end();
379 ++map_iter) {
380
381 for (std::list<MsgPtr>::iterator it = (map_iter->second).begin();
382 it != (map_iter->second).end(); ++it) {
383
384 Message *msg = (*it).get();
385 if (msg->functionalRead(pkt)) return true;
386 }
387 }
388 return false;
389}
390
391uint32_t
392MessageBuffer::functionalWrite(Packet *pkt)
393{
394 uint32_t num_functional_writes = 0;
395
396 // Check the priority heap and write any messages that may
397 // correspond to the address in the packet.
398 for (unsigned int i = 0; i < m_prio_heap.size(); ++i) {
399 Message *msg = m_prio_heap[i].m_msgptr.get();
400 if (msg->functionalWrite(pkt)) {
401 num_functional_writes++;
402 }
403 }
404
405 // Check the stall queue and write any messages that may
406 // correspond to the address in the packet.
407 for (StallMsgMapType::iterator map_iter = m_stall_msg_map.begin();
408 map_iter != m_stall_msg_map.end();
409 ++map_iter) {
410
411 for (std::list<MsgPtr>::iterator it = (map_iter->second).begin();
412 it != (map_iter->second).end(); ++it) {
413
414 Message *msg = (*it).get();
415 if (msg->functionalWrite(pkt)) {
416 num_functional_writes++;
417 }
418 }
419 }
420
421 return num_functional_writes;
422}