MessageBuffer.hh revision 10917
16145SN/A/* 26145SN/A * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 36145SN/A * All rights reserved. 46145SN/A * 56145SN/A * Redistribution and use in source and binary forms, with or without 66145SN/A * modification, are permitted provided that the following conditions are 76145SN/A * met: redistributions of source code must retain the above copyright 86145SN/A * notice, this list of conditions and the following disclaimer; 96145SN/A * redistributions in binary form must reproduce the above copyright 106145SN/A * notice, this list of conditions and the following disclaimer in the 116145SN/A * documentation and/or other materials provided with the distribution; 126145SN/A * neither the name of the copyright holders nor the names of its 136145SN/A * contributors may be used to endorse or promote products derived from 146145SN/A * this software without specific prior written permission. 156145SN/A * 166145SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176145SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186145SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196145SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206145SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216145SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226145SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236145SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246145SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256145SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266145SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276145SN/A */ 286145SN/A 296145SN/A/* 307039SN/A * Unordered buffer of messages that can be inserted such 316145SN/A * that they can be dequeued after a given delta time has expired. 326145SN/A */ 336145SN/A 347039SN/A#ifndef __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__ 357039SN/A#define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__ 366145SN/A 377456SN/A#include <algorithm> 387832SN/A#include <cassert> 397456SN/A#include <functional> 407002SN/A#include <iostream> 418229SN/A#include <string> 427456SN/A#include <vector> 437002SN/A 448229SN/A#include "mem/ruby/common/Address.hh" 456154SN/A#include "mem/ruby/common/Consumer.hh" 466154SN/A#include "mem/ruby/slicc_interface/Message.hh" 4710301Snilay@cs.wisc.edu#include "mem/packet.hh" 486145SN/A 497039SN/Aclass MessageBuffer 507039SN/A{ 517039SN/A public: 527039SN/A MessageBuffer(const std::string &name = ""); 536145SN/A 548054SN/A std::string name() const { return m_name; } 558054SN/A 569499SN/A void setRecycleLatency(Cycles recycle_latency) 579499SN/A { m_recycle_latency = recycle_latency; } 586145SN/A 597567SN/A void reanalyzeMessages(const Address& addr); 607922SN/A void reanalyzeAllMessages(); 617567SN/A void stallMessage(const Address& addr); 627567SN/A 637039SN/A // TRUE if head of queue timestamp <= SystemTime 649302SN/A bool isReady() const; 656145SN/A 667039SN/A void 677039SN/A delayHead() 687039SN/A { 6910893Snilay@cs.wisc.edu MsgPtr m = m_prio_heap.front(); 707456SN/A std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(), 7110893Snilay@cs.wisc.edu std::greater<MsgPtr>()); 727456SN/A m_prio_heap.pop_back(); 7310893Snilay@cs.wisc.edu enqueue(m, Cycles(1)); 747039SN/A } 756145SN/A 7610074SN/A bool areNSlotsAvailable(unsigned int n); 777039SN/A int getPriority() { return m_priority_rank; } 787039SN/A void setPriority(int rank) { m_priority_rank = rank; } 799602SN/A void setConsumer(Consumer* consumer) 807039SN/A { 819629SN/A if (m_consumer != NULL) { 829629SN/A fatal("Trying to connect %s to MessageBuffer %s. \ 839629SN/A \n%s already connected. Check the cntrl_id's.\n", 849629SN/A *consumer, *this, *m_consumer); 859629SN/A } 869602SN/A m_consumer = consumer; 877039SN/A } 886145SN/A 899508SN/A void setSender(ClockedObject* obj) 909465SN/A { 919602SN/A assert(m_sender == NULL || m_sender == obj); 929602SN/A m_sender = obj; 939508SN/A } 949508SN/A 959508SN/A void setReceiver(ClockedObject* obj) 969508SN/A { 979602SN/A assert(m_receiver == NULL || m_receiver == obj); 989602SN/A m_receiver = obj; 999465SN/A } 1009465SN/A 1017039SN/A void setDescription(const std::string& name) { m_name = name; } 1027039SN/A std::string getDescription() { return m_name;} 1036863SN/A 1049602SN/A Consumer* getConsumer() { return m_consumer; } 1056145SN/A 10610074SN/A //! Function for extracting the message at the head of the 10710074SN/A //! message queue. The function assumes that the queue is nonempty. 10810074SN/A const Message* peek() const; 1096145SN/A 1107039SN/A const MsgPtr& 1117039SN/A peekMsgPtr() const 1127039SN/A { 1137039SN/A assert(isReady()); 11410893Snilay@cs.wisc.edu return m_prio_heap.front(); 1157039SN/A } 1166145SN/A 1179499SN/A void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); } 1189499SN/A void enqueue(MsgPtr message, Cycles delta); 1199465SN/A 12010226SN/A //! Updates the delay cycles of the message at the head of the queue, 12110074SN/A //! removes it from the queue and returns its total delay. 12210226SN/A Cycles dequeue(); 12310074SN/A 1247039SN/A void recycle(); 1257039SN/A bool isEmpty() const { return m_prio_heap.size() == 0; } 1266145SN/A 1277039SN/A void 1287039SN/A setOrdering(bool order) 1297039SN/A { 1307039SN/A m_strict_fifo = order; 1317039SN/A m_ordering_set = true; 1327039SN/A } 13310074SN/A 13410096SN/A void resize(unsigned int size) { m_max_size = size; } 13510096SN/A unsigned int getSize(); 1367039SN/A void setRandomization(bool random_flag) { m_randomization = random_flag; } 1376145SN/A 1387039SN/A void clear(); 1397039SN/A void print(std::ostream& out) const; 1407039SN/A void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; } 1416285SN/A 1427973SN/A void setIncomingLink(int link_id) { m_input_link_id = link_id; } 1437973SN/A void setVnet(int net) { m_vnet_id = net; } 1447973SN/A 1459302SN/A // Function for figuring out if any of the messages in the buffer can 1469302SN/A // satisfy the read request for the address in the packet. 1479302SN/A // Return value, if true, indicates that the request was fulfilled. 1489302SN/A bool functionalRead(Packet *pkt); 1499302SN/A 1509302SN/A // Function for figuring out if any of the messages in the buffer need 1519302SN/A // to be updated with the data from the packet. 1529302SN/A // Return value indicates the number of messages that were updated. 1539302SN/A // This required for debugging the code. 1549302SN/A uint32_t functionalWrite(Packet *pkt); 1559302SN/A 1567039SN/A private: 15710087SN/A void reanalyzeList(std::list<MsgPtr> &, Tick); 15810087SN/A 15910087SN/A private: 1607039SN/A //added by SS 1619499SN/A Cycles m_recycle_latency; 1626145SN/A 1637039SN/A // Data Members (m_ prefix) 1649508SN/A //! The two ends of the buffer. 1659602SN/A ClockedObject* m_sender; 1669602SN/A ClockedObject* m_receiver; 1679508SN/A 1689465SN/A //! Consumer to signal a wakeup(), can be NULL 1699602SN/A Consumer* m_consumer; 17010893Snilay@cs.wisc.edu std::vector<MsgPtr> m_prio_heap; 1719465SN/A 1728943SN/A // use a std::map for the stalled messages as this container is 1738943SN/A // sorted and ensures a well-defined iteration order 1748943SN/A typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType; 1757567SN/A 1767567SN/A StallMsgMapType m_stall_msg_map; 1777039SN/A std::string m_name; 1786145SN/A 1799770SN/A unsigned int m_max_size; 1809503SN/A Cycles m_time_last_time_size_checked; 1819770SN/A unsigned int m_size_last_time_size_checked; 1826145SN/A 18310917Sbrandon.potter@amd.com // variables used so enqueues appear to happen immediately, while 1847039SN/A // pop happen the next cycle 1859503SN/A Cycles m_time_last_time_enqueue; 18610097SN/A Tick m_time_last_time_pop; 18710097SN/A Tick m_last_arrival_time; 18810097SN/A 1899770SN/A unsigned int m_size_at_cycle_start; 1909770SN/A unsigned int m_msgs_this_cycle; 1917039SN/A 1927039SN/A int m_not_avail_count; // count the # of times I didn't have N 1937039SN/A // slots available 1947566SN/A uint64 m_msg_counter; 1957039SN/A int m_priority_rank; 1967039SN/A bool m_strict_fifo; 1977039SN/A bool m_ordering_set; 1987039SN/A bool m_randomization; 1999499SN/A 2007973SN/A int m_input_link_id; 2017973SN/A int m_vnet_id; 2026145SN/A}; 2036145SN/A 2049554SN/ACycles random_time(); 2059554SN/A 2067039SN/Ainline std::ostream& 2077039SN/Aoperator<<(std::ostream& out, const MessageBuffer& obj) 2086145SN/A{ 2097039SN/A obj.print(out); 2107039SN/A out << std::flush; 2117039SN/A return out; 2126145SN/A} 2136145SN/A 2147039SN/A#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__ 215