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