MessageBuffer.hh revision 6863
1 2/* 3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/* 31 * $Id$ 32 * 33 * Description: Unordered buffer of messages that can be inserted such 34 * that they can be dequeued after a given delta time has expired. 35 * 36 */ 37 38#ifndef MESSAGEBUFFER_H 39#define MESSAGEBUFFER_H 40 41#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