MessageBuffer.hh revision 9602
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/packet.hh"
45#include "mem/ruby/buffers/MessageBufferNode.hh"
46#include "mem/ruby/common/Address.hh"
47#include "mem/ruby/common/Consumer.hh"
48#include "mem/ruby/slicc_interface/Message.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    {
70        MessageBufferNode node = m_prio_heap.front();
71        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
72                      std::greater<MessageBufferNode>());
73        m_prio_heap.pop_back();
74        enqueue(node.m_msgptr, Cycles(1));
75    }
76
77    bool areNSlotsAvailable(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        assert(m_consumer == NULL);
83        m_consumer = consumer;
84    }
85
86    void setSender(ClockedObject* obj)
87    {
88        assert(m_sender == NULL || m_sender == obj);
89        m_sender = obj;
90    }
91
92    void setReceiver(ClockedObject* obj)
93    {
94        assert(m_receiver == NULL || m_receiver == obj);
95        m_receiver = obj;
96    }
97
98    void setDescription(const std::string& name) { m_name = name; }
99    std::string getDescription() { return m_name;}
100
101    Consumer* getConsumer() { return m_consumer; }
102
103    const Message* peekAtHeadOfQueue() const;
104    const Message* peek() const { return peekAtHeadOfQueue(); }
105    const MsgPtr getMsgPtrCopy() const;
106
107    const MsgPtr&
108    peekMsgPtr() const
109    {
110        assert(isReady());
111        return m_prio_heap.front().m_msgptr;
112    }
113
114    const MsgPtr&
115    peekMsgPtrEvenIfNotReady() const
116    {
117        return m_prio_heap.front().m_msgptr;
118    }
119
120    void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
121    void enqueue(MsgPtr message, Cycles delta);
122
123    //!  returns delay ticks of the message.
124    Cycles dequeue_getDelayCycles(MsgPtr& message);
125    void dequeue(MsgPtr& message);
126
127    //! returns delay cycles of the message
128    Cycles dequeue_getDelayCycles();
129    void dequeue() { pop(); }
130    void pop();
131    void recycle();
132    bool isEmpty() const { return m_prio_heap.size() == 0; }
133
134    void
135    setOrdering(bool order)
136    {
137        m_strict_fifo = order;
138        m_ordering_set = true;
139    }
140    void resize(int size) { m_max_size = size; }
141    int getSize();
142    void setRandomization(bool random_flag) { m_randomization = random_flag; }
143
144    void clear();
145
146    void print(std::ostream& out) const;
147    void printStats(std::ostream& out);
148    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
149
150    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
151    void setVnet(int net) { m_vnet_id = net; }
152
153    // Function for figuring out if any of the messages in the buffer can
154    // satisfy the read request for the address in the packet.
155    // Return value, if true, indicates that the request was fulfilled.
156    bool functionalRead(Packet *pkt);
157
158    // Function for figuring out if any of the messages in the buffer need
159    // to be updated with the data from the packet.
160    // Return value indicates the number of messages that were updated.
161    // This required for debugging the code.
162    uint32_t functionalWrite(Packet *pkt);
163
164  private:
165    //added by SS
166    Cycles m_recycle_latency;
167
168    // Private Methods
169    Cycles setAndReturnDelayCycles(MsgPtr message);
170
171    // Private copy constructor and assignment operator
172    MessageBuffer(const MessageBuffer& obj);
173    MessageBuffer& operator=(const MessageBuffer& obj);
174
175    // Data Members (m_ prefix)
176    //! The two ends of the buffer.
177    ClockedObject* m_sender;
178    ClockedObject* m_receiver;
179
180    //! Consumer to signal a wakeup(), can be NULL
181    Consumer* m_consumer;
182    std::vector<MessageBufferNode> m_prio_heap;
183
184    // use a std::map for the stalled messages as this container is
185    // sorted and ensures a well-defined iteration order
186    typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
187    typedef std::vector<MsgPtr>::iterator MsgListIter;
188
189    StallMsgMapType m_stall_msg_map;
190    std::string m_name;
191
192    int m_max_size;
193    int m_size;
194
195    Cycles m_time_last_time_size_checked;
196    int m_size_last_time_size_checked;
197
198    // variables used so enqueues appear to happen imediately, while
199    // pop happen the next cycle
200    Cycles m_time_last_time_enqueue;
201    Cycles m_time_last_time_pop;
202    int m_size_at_cycle_start;
203    int m_msgs_this_cycle;
204
205    int m_not_avail_count;  // count the # of times I didn't have N
206                            // slots available
207    uint64 m_msg_counter;
208    int m_priority_rank;
209    bool m_strict_fifo;
210    bool m_ordering_set;
211    bool m_randomization;
212
213    Cycles m_last_arrival_time;
214
215    int m_input_link_id;
216    int m_vnet_id;
217};
218
219Cycles random_time();
220
221inline std::ostream&
222operator<<(std::ostream& out, const MessageBuffer& obj)
223{
224    obj.print(out);
225    out << std::flush;
226    return out;
227}
228
229#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
230