MessageBuffer.hh revision 10979
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; }
12610979Sdavid.hashe@amd.com    bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
12710979Sdavid.hashe@amd.com    unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
1286145SN/A
1297039SN/A    void
1307039SN/A    setOrdering(bool order)
1317039SN/A    {
1327039SN/A        m_strict_fifo = order;
1337039SN/A        m_ordering_set = true;
1347039SN/A    }
13510074SN/A
13610096SN/A    void resize(unsigned int size) { m_max_size = size; }
13710096SN/A    unsigned int getSize();
1387039SN/A    void setRandomization(bool random_flag) { m_randomization = random_flag; }
1396145SN/A
1407039SN/A    void clear();
1417039SN/A    void print(std::ostream& out) const;
1427039SN/A    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
1436285SN/A
1447973SN/A    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
1457973SN/A    void setVnet(int net) { m_vnet_id = net; }
1467973SN/A
1479302SN/A    // Function for figuring out if any of the messages in the buffer can
1489302SN/A    // satisfy the read request for the address in the packet.
1499302SN/A    // Return value, if true, indicates that the request was fulfilled.
1509302SN/A    bool functionalRead(Packet *pkt);
1519302SN/A
1529302SN/A    // Function for figuring out if any of the messages in the buffer need
1539302SN/A    // to be updated with the data from the packet.
1549302SN/A    // Return value indicates the number of messages that were updated.
1559302SN/A    // This required for debugging the code.
1569302SN/A    uint32_t functionalWrite(Packet *pkt);
1579302SN/A
1587039SN/A  private:
15910087SN/A    void reanalyzeList(std::list<MsgPtr> &, Tick);
16010087SN/A
16110087SN/A  private:
1627039SN/A    //added by SS
1639499SN/A    Cycles m_recycle_latency;
1646145SN/A
1657039SN/A    // Data Members (m_ prefix)
1669508SN/A    //! The two ends of the buffer.
1679602SN/A    ClockedObject* m_sender;
1689602SN/A    ClockedObject* m_receiver;
1699508SN/A
1709465SN/A    //! Consumer to signal a wakeup(), can be NULL
1719602SN/A    Consumer* m_consumer;
17210893Snilay@cs.wisc.edu    std::vector<MsgPtr> m_prio_heap;
1739465SN/A
1748943SN/A    // use a std::map for the stalled messages as this container is
1758943SN/A    // sorted and ensures a well-defined iteration order
1768943SN/A    typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
1777567SN/A
1787567SN/A    StallMsgMapType m_stall_msg_map;
1797039SN/A    std::string m_name;
1806145SN/A
1819770SN/A    unsigned int m_max_size;
1829503SN/A    Cycles m_time_last_time_size_checked;
1839770SN/A    unsigned int m_size_last_time_size_checked;
1846145SN/A
18510917Sbrandon.potter@amd.com    // variables used so enqueues appear to happen immediately, while
1867039SN/A    // pop happen the next cycle
1879503SN/A    Cycles m_time_last_time_enqueue;
18810097SN/A    Tick m_time_last_time_pop;
18910097SN/A    Tick m_last_arrival_time;
19010097SN/A
1919770SN/A    unsigned int m_size_at_cycle_start;
1929770SN/A    unsigned int m_msgs_this_cycle;
1937039SN/A
1947039SN/A    int m_not_avail_count;  // count the # of times I didn't have N
1957039SN/A                            // slots available
1967566SN/A    uint64 m_msg_counter;
1977039SN/A    int m_priority_rank;
1987039SN/A    bool m_strict_fifo;
1997039SN/A    bool m_ordering_set;
2007039SN/A    bool m_randomization;
2019499SN/A
2027973SN/A    int m_input_link_id;
2037973SN/A    int m_vnet_id;
2046145SN/A};
2056145SN/A
2069554SN/ACycles random_time();
2079554SN/A
2087039SN/Ainline std::ostream&
2097039SN/Aoperator<<(std::ostream& out, const MessageBuffer& obj)
2106145SN/A{
2117039SN/A    obj.print(out);
2127039SN/A    out << std::flush;
2137039SN/A    return out;
2146145SN/A}
2156145SN/A
2167039SN/A#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
217