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