MessageBuffer.hh revision 7832
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 stallMessage(const Address& addr);
65
66    // TRUE if head of queue timestamp <= SystemTime
67    bool
68    isReady() const
69    {
70        return ((m_prio_heap.size() > 0) &&
71                (m_prio_heap.front().m_time <= g_eventQueue_ptr->getTime()));
72    }
73
74    void
75    delayHead()
76    {
77        MessageBufferNode node = m_prio_heap.front();
78        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
79                      std::greater<MessageBufferNode>());
80        m_prio_heap.pop_back();
81        enqueue(node.m_msgptr, 1);
82    }
83
84    bool areNSlotsAvailable(int n);
85    int getPriority() { return m_priority_rank; }
86    void setPriority(int rank) { m_priority_rank = rank; }
87    void setConsumer(Consumer* consumer_ptr)
88    {
89        assert(m_consumer_ptr == NULL);
90        m_consumer_ptr = consumer_ptr;
91    }
92
93    void setDescription(const std::string& name) { m_name = name; }
94    std::string getDescription() { return m_name;}
95
96    Consumer* getConsumer() { return m_consumer_ptr; }
97
98    const Message* peekAtHeadOfQueue() const;
99    const Message* peek() const { return peekAtHeadOfQueue(); }
100    const MsgPtr getMsgPtrCopy() const;
101
102    const MsgPtr&
103    peekMsgPtr() const
104    {
105        assert(isReady());
106        return m_prio_heap.front().m_msgptr;
107    }
108
109    const MsgPtr&
110    peekMsgPtrEvenIfNotReady() const
111    {
112        return m_prio_heap.front().m_msgptr;
113    }
114
115    void enqueue(MsgPtr message) { enqueue(message, 1); }
116    void enqueue(MsgPtr message, Time delta);
117    //  void enqueueAbsolute(const MsgPtr& message, Time absolute_time);
118    int dequeue_getDelayCycles(MsgPtr& message);  // returns delay
119                                                  // cycles of the
120                                                  // message
121    void dequeue(MsgPtr& message);
122    int dequeue_getDelayCycles();  // returns delay cycles of the message
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  private:
145    //added by SS
146    int m_recycle_latency;
147
148    // Private Methods
149    int setAndReturnDelayCycles(MsgPtr message);
150
151    // Private copy constructor and assignment operator
152    MessageBuffer(const MessageBuffer& obj);
153    MessageBuffer& operator=(const MessageBuffer& obj);
154
155    // Data Members (m_ prefix)
156    Consumer* m_consumer_ptr;  // Consumer to signal a wakeup(), can be NULL
157    std::vector<MessageBufferNode> m_prio_heap;
158
159    typedef m5::hash_map< Address, std::list<MsgPtr> > StallMsgMapType;
160    typedef std::vector<MsgPtr>::iterator MsgListIter;
161
162    StallMsgMapType m_stall_msg_map;
163    std::string m_name;
164
165    int m_max_size;
166    int m_size;
167
168    Time m_time_last_time_size_checked;
169    int m_size_last_time_size_checked;
170
171    // variables used so enqueues appear to happen imediately, while
172    // pop happen the next cycle
173    Time m_time_last_time_enqueue;
174    Time m_time_last_time_pop;
175    int m_size_at_cycle_start;
176    int m_msgs_this_cycle;
177
178    int m_not_avail_count;  // count the # of times I didn't have N
179                            // slots available
180    uint64 m_msg_counter;
181    int m_priority_rank;
182    bool m_strict_fifo;
183    bool m_ordering_set;
184    bool m_randomization;
185    Time m_last_arrival_time;
186};
187
188inline std::ostream&
189operator<<(std::ostream& out, const MessageBuffer& obj)
190{
191    obj.print(out);
192    out << std::flush;
193    return out;
194}
195
196#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
197