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