MessageBuffer.hh revision 6863
110448Snilay@cs.wisc.edu
210448Snilay@cs.wisc.edu/*
310448Snilay@cs.wisc.edu * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
410448Snilay@cs.wisc.edu * All rights reserved.
510448Snilay@cs.wisc.edu *
610448Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
710448Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions are
810448Snilay@cs.wisc.edu * met: redistributions of source code must retain the above copyright
910448Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer;
1010448Snilay@cs.wisc.edu * redistributions in binary form must reproduce the above copyright
1110448Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer in the
1210448Snilay@cs.wisc.edu * documentation and/or other materials provided with the distribution;
1310448Snilay@cs.wisc.edu * neither the name of the copyright holders nor the names of its
1410448Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
1510448Snilay@cs.wisc.edu * this software without specific prior written permission.
1610448Snilay@cs.wisc.edu *
1710448Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1810448Snilay@cs.wisc.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1910448Snilay@cs.wisc.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2010448Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2110448Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2210447Snilay@cs.wisc.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2310447Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2410447Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2510447Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2610447Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2710447Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2810447Snilay@cs.wisc.edu */
2910447Snilay@cs.wisc.edu
3010447Snilay@cs.wisc.edu/*
3110447Snilay@cs.wisc.edu * $Id$
3210447Snilay@cs.wisc.edu *
3310447Snilay@cs.wisc.edu * Description: Unordered buffer of messages that can be inserted such
3410447Snilay@cs.wisc.edu * that they can be dequeued after a given delta time has expired.
3510447Snilay@cs.wisc.edu *
3610447Snilay@cs.wisc.edu */
3710447Snilay@cs.wisc.edu
3810447Snilay@cs.wisc.edu#ifndef MESSAGEBUFFER_H
3910447Snilay@cs.wisc.edu#define MESSAGEBUFFER_H
4010447Snilay@cs.wisc.edu
4110447Snilay@cs.wisc.edu#include "mem/ruby/common/Global.hh"
42#include "mem/ruby/buffers/MessageBufferNode.hh"
43#include "mem/ruby/common/Consumer.hh"
44#include "mem/ruby/eventqueue/RubyEventQueue.hh"
45#include "mem/ruby/slicc_interface/Message.hh"
46#include "mem/gems_common/PrioHeap.hh"
47#include "mem/gems_common/util.hh"
48
49class MessageBuffer {
50public:
51  // Constructors
52  MessageBuffer(const string &name = "");
53
54  // ~MessageBuffer()
55
56  // Public Methods
57
58  static void printConfig(ostream& out) {}
59  void setRecycleLatency(int recycle_latency) { m_recycle_latency = recycle_latency; }
60
61  // TRUE if head of queue timestamp <= SystemTime
62  bool isReady() const {
63    return ((m_prio_heap.size() > 0) &&
64            (m_prio_heap.peekMin().m_time <= g_eventQueue_ptr->getTime()));
65  }
66
67  void delayHead() {
68    MessageBufferNode node = m_prio_heap.extractMin();
69    enqueue(node.m_msgptr, 1);
70  }
71
72  bool areNSlotsAvailable(int n);
73  int getPriority() { return m_priority_rank; }
74  void setPriority(int rank) { m_priority_rank = rank; }
75  void setConsumer(Consumer* consumer_ptr) { ASSERT(m_consumer_ptr==NULL); m_consumer_ptr = consumer_ptr; }
76  void setDescription(const string& name) { m_name = name; }
77  string getDescription() { return m_name;}
78
79  Consumer* getConsumer() { return m_consumer_ptr; }
80
81  const Message* peekAtHeadOfQueue() const;
82  const Message* peek() const { return peekAtHeadOfQueue(); }
83  const MsgPtr getMsgPtrCopy() const;
84  const MsgPtr& peekMsgPtr() const { assert(isReady()); return m_prio_heap.peekMin().m_msgptr; }
85  const MsgPtr& peekMsgPtrEvenIfNotReady() const {return m_prio_heap.peekMin().m_msgptr; }
86
87  void enqueue(const MsgPtr& message) { enqueue(message, 1); }
88  void enqueue(const MsgPtr& message, Time delta);
89  //  void enqueueAbsolute(const MsgPtr& message, Time absolute_time);
90  int dequeue_getDelayCycles(MsgPtr& message);  // returns delay cycles of the message
91  void dequeue(MsgPtr& message);
92  int dequeue_getDelayCycles();  // returns delay cycles of the message
93  void dequeue() { pop(); }
94  void pop();
95  void recycle();
96  bool isEmpty() const { return m_prio_heap.size() == 0; }
97
98  void setOrdering(bool order) { m_strict_fifo = order; m_ordering_set = true; }
99  void setSize(int size) {m_max_size = size;}
100  int getSize();
101  void setRandomization(bool random_flag) { m_randomization = random_flag; }
102
103  void clear();
104
105  void print(ostream& out) const;
106  void printStats(ostream& out);
107  void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
108
109private:
110  //added by SS
111  int m_recycle_latency;
112
113  // Private Methods
114  int setAndReturnDelayCycles(MsgPtr& message);
115
116  // Private copy constructor and assignment operator
117  MessageBuffer(const MessageBuffer& obj);
118  MessageBuffer& operator=(const MessageBuffer& obj);
119
120  // Data Members (m_ prefix)
121  Consumer* m_consumer_ptr;  // Consumer to signal a wakeup(), can be NULL
122  PrioHeap<MessageBufferNode> m_prio_heap;
123  string m_name;
124
125  int m_max_size;
126  int m_size;
127
128  Time m_time_last_time_size_checked;
129  int m_size_last_time_size_checked;
130
131  // variables used so enqueues appear to happen imediately, while pop happen the next cycle
132  Time m_time_last_time_enqueue;
133  Time m_time_last_time_pop;
134  int m_size_at_cycle_start;
135  int m_msgs_this_cycle;
136
137  int m_not_avail_count;  // count the # of times I didn't have N slots available
138  int m_msg_counter;
139  int m_priority_rank;
140  bool m_strict_fifo;
141  bool m_ordering_set;
142  bool m_randomization;
143  Time m_last_arrival_time;
144};
145
146// Output operator declaration
147//template <class TYPE>
148ostream& operator<<(ostream& out, const MessageBuffer& obj);
149
150// ******************* Definitions *******************
151
152// Output operator definition
153extern inline
154ostream& operator<<(ostream& out, const MessageBuffer& obj)
155{
156  obj.print(out);
157  out << flush;
158  return out;
159}
160
161#endif //MESSAGEBUFFER_H
162