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