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