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