MessageBuffer.hh revision 8229
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/ruby/buffers/MessageBufferNode.hh"
45#include "mem/ruby/common/Address.hh"
46#include "mem/ruby/common/Consumer.hh"
47#include "mem/ruby/common/Global.hh"
48#include "mem/ruby/eventqueue/RubyEventQueue.hh"
49#include "mem/ruby/slicc_interface/Message.hh"
50
51class MessageBuffer
52{
53  public:
54    MessageBuffer(const std::string &name = "");
55
56    std::string name() const { return m_name; }
57
58    static void printConfig(std::ostream& out) {}
59    void
60    setRecycleLatency(int recycle_latency)
61    {
62        m_recycle_latency = recycle_latency;
63    }
64
65    void reanalyzeMessages(const Address& addr);
66    void reanalyzeAllMessages();
67    void stallMessage(const Address& addr);
68
69    // TRUE if head of queue timestamp <= SystemTime
70    bool
71    isReady() const
72    {
73        return ((m_prio_heap.size() > 0) &&
74                (m_prio_heap.front().m_time <= g_eventQueue_ptr->getTime()));
75    }
76
77    void
78    delayHead()
79    {
80        MessageBufferNode node = m_prio_heap.front();
81        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
82                      std::greater<MessageBufferNode>());
83        m_prio_heap.pop_back();
84        enqueue(node.m_msgptr, 1);
85    }
86
87    bool areNSlotsAvailable(int n);
88    int getPriority() { return m_priority_rank; }
89    void setPriority(int rank) { m_priority_rank = rank; }
90    void setConsumer(Consumer* consumer_ptr)
91    {
92        assert(m_consumer_ptr == NULL);
93        m_consumer_ptr = consumer_ptr;
94    }
95
96    void setDescription(const std::string& name) { m_name = name; }
97    std::string getDescription() { return m_name;}
98
99    Consumer* getConsumer() { return m_consumer_ptr; }
100
101    const Message* peekAtHeadOfQueue() const;
102    const Message* peek() const { return peekAtHeadOfQueue(); }
103    const MsgPtr getMsgPtrCopy() const;
104
105    const MsgPtr&
106    peekMsgPtr() const
107    {
108        assert(isReady());
109        return m_prio_heap.front().m_msgptr;
110    }
111
112    const MsgPtr&
113    peekMsgPtrEvenIfNotReady() const
114    {
115        return m_prio_heap.front().m_msgptr;
116    }
117
118    void enqueue(MsgPtr message) { enqueue(message, 1); }
119    void enqueue(MsgPtr message, Time delta);
120    //  void enqueueAbsolute(const MsgPtr& message, Time absolute_time);
121    int dequeue_getDelayCycles(MsgPtr& message);  // returns delay
122                                                  // cycles of the
123                                                  // message
124    void dequeue(MsgPtr& message);
125    int dequeue_getDelayCycles();  // returns delay cycles of the message
126    void dequeue() { pop(); }
127    void pop();
128    void recycle();
129    bool isEmpty() const { return m_prio_heap.size() == 0; }
130
131    void
132    setOrdering(bool order)
133    {
134        m_strict_fifo = order;
135        m_ordering_set = true;
136    }
137    void resize(int size) { m_max_size = size; }
138    int getSize();
139    void setRandomization(bool random_flag) { m_randomization = random_flag; }
140
141    void clear();
142
143    void print(std::ostream& out) const;
144    void printStats(std::ostream& out);
145    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
146
147    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
148    void setVnet(int net) { m_vnet_id = net; }
149
150  private:
151    //added by SS
152    int m_recycle_latency;
153
154    // Private Methods
155    int setAndReturnDelayCycles(MsgPtr message);
156
157    // Private copy constructor and assignment operator
158    MessageBuffer(const MessageBuffer& obj);
159    MessageBuffer& operator=(const MessageBuffer& obj);
160
161    // Data Members (m_ prefix)
162    Consumer* m_consumer_ptr;  // Consumer to signal a wakeup(), can be NULL
163    std::vector<MessageBufferNode> m_prio_heap;
164
165    typedef m5::hash_map< Address, std::list<MsgPtr> > StallMsgMapType;
166    typedef std::vector<MsgPtr>::iterator MsgListIter;
167
168    StallMsgMapType m_stall_msg_map;
169    std::string m_name;
170
171    int m_max_size;
172    int m_size;
173
174    Time m_time_last_time_size_checked;
175    int m_size_last_time_size_checked;
176
177    // variables used so enqueues appear to happen imediately, while
178    // pop happen the next cycle
179    Time m_time_last_time_enqueue;
180    Time m_time_last_time_pop;
181    int m_size_at_cycle_start;
182    int m_msgs_this_cycle;
183
184    int m_not_avail_count;  // count the # of times I didn't have N
185                            // slots available
186    uint64 m_msg_counter;
187    int m_priority_rank;
188    bool m_strict_fifo;
189    bool m_ordering_set;
190    bool m_randomization;
191    Time m_last_arrival_time;
192
193    int m_input_link_id;
194    int m_vnet_id;
195};
196
197inline std::ostream&
198operator<<(std::ostream& out, const MessageBuffer& obj)
199{
200    obj.print(out);
201    out << std::flush;
202    return out;
203}
204
205#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
206