MessageBuffer.hh revision 6285
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 Chip; 50 51class MessageBuffer { 52public: 53 // Constructors 54 MessageBuffer(); 55 MessageBuffer(const Chip* chip_ptr); // The chip_ptr is ignored, but could be used for extra debugging 56 57 // Use Default Destructor 58 // ~MessageBuffer() 59 60 // Public Methods 61 62 static void printConfig(ostream& out) {} 63 void setRecycleLatency(int recycle_latency) { m_recycle_latency = recycle_latency; } 64 65 // TRUE if head of queue timestamp <= SystemTime 66 bool isReady() const { 67 return ((m_prio_heap.size() > 0) && 68 (m_prio_heap.peekMin().m_time <= g_eventQueue_ptr->getTime())); 69 } 70 71 bool areNSlotsAvailable(int n); 72 int getPriority() { return m_priority_rank; } 73 void setPriority(int rank) { m_priority_rank = rank; } 74 void setConsumer(Consumer* consumer_ptr) { ASSERT(m_consumer_ptr==NULL); m_consumer_ptr = consumer_ptr; } 75 void setDescription(const string& name) { m_name = name; } 76 string getDescription() { return m_name;} 77 78 Consumer* getConsumer() { return m_consumer_ptr; } 79 80 const Message* peekAtHeadOfQueue() const; 81 const Message* peek() const { return peekAtHeadOfQueue(); } 82 const MsgPtr getMsgPtrCopy() const; 83 const MsgPtr& peekMsgPtr() const { assert(isReady()); return m_prio_heap.peekMin().m_msgptr; } 84 const MsgPtr& peekMsgPtrEvenIfNotReady() const {return m_prio_heap.peekMin().m_msgptr; } 85 86 void enqueue(const MsgPtr& message) { enqueue(message, 1); } 87 void enqueue(const MsgPtr& message, Time delta); 88 // void enqueueAbsolute(const MsgPtr& message, Time absolute_time); 89 int dequeue_getDelayCycles(MsgPtr& message); // returns delay cycles of the message 90 void dequeue(MsgPtr& message); 91 int dequeue_getDelayCycles(); // returns delay cycles of the message 92 void dequeue() { pop(); } 93 void pop(); 94 void recycle(); 95 bool isEmpty() const { return m_prio_heap.size() == 0; } 96 97 void setOrdering(bool order) { m_strict_fifo = order; m_ordering_set = true; } 98 void setSize(int size) {m_max_size = size;} 99 int getSize(); 100 void setRandomization(bool random_flag) { m_randomization = random_flag; } 101 102 void clear(); 103 104 void print(ostream& out) const; 105 void printStats(ostream& out); 106 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; } 107 108private: 109 //added by SS 110 int m_recycle_latency; 111 112 // Private Methods 113 int setAndReturnDelayCycles(MsgPtr& message); 114 115 // Private copy constructor and assignment operator 116 MessageBuffer(const MessageBuffer& obj); 117 MessageBuffer& operator=(const MessageBuffer& obj); 118 119 // Data Members (m_ prefix) 120 Consumer* m_consumer_ptr; // Consumer to signal a wakeup(), can be NULL 121 PrioHeap<MessageBufferNode> m_prio_heap; 122 string m_name; 123 124 int m_max_size; 125 int m_size; 126 127 Time m_time_last_time_size_checked; 128 int m_size_last_time_size_checked; 129 130 // variables used so enqueues appear to happen imediately, while pop happen the next cycle 131 Time m_time_last_time_enqueue; 132 Time m_time_last_time_pop; 133 int m_size_at_cycle_start; 134 int m_msgs_this_cycle; 135 136 int m_not_avail_count; // count the # of times I didn't have N slots available 137 int m_msg_counter; 138 int m_priority_rank; 139 bool m_strict_fifo; 140 bool m_ordering_set; 141 bool m_randomization; 142 Time m_last_arrival_time; 143}; 144 145// Output operator declaration 146//template <class TYPE> 147ostream& operator<<(ostream& out, const MessageBuffer& obj); 148 149// ******************* Definitions ******************* 150 151// Output operator definition 152extern inline 153ostream& operator<<(ostream& out, const MessageBuffer& obj) 154{ 155 obj.print(out); 156 out << flush; 157 return out; 158} 159 160#endif //MESSAGEBUFFER_H 161