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