MessageBuffer.hh revision 10074
110235Syasuko.eckert@amd.com/*
210235Syasuko.eckert@amd.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
310235Syasuko.eckert@amd.com * All rights reserved.
410235Syasuko.eckert@amd.com *
510235Syasuko.eckert@amd.com * Redistribution and use in source and binary forms, with or without
610235Syasuko.eckert@amd.com * modification, are permitted provided that the following conditions are
710235Syasuko.eckert@amd.com * met: redistributions of source code must retain the above copyright
810235Syasuko.eckert@amd.com * notice, this list of conditions and the following disclaimer;
910235Syasuko.eckert@amd.com * redistributions in binary form must reproduce the above copyright
1010235Syasuko.eckert@amd.com * notice, this list of conditions and the following disclaimer in the
1110235Syasuko.eckert@amd.com * documentation and/or other materials provided with the distribution;
1210235Syasuko.eckert@amd.com * neither the name of the copyright holders nor the names of its
1310235Syasuko.eckert@amd.com * contributors may be used to endorse or promote products derived from
1410235Syasuko.eckert@amd.com * this software without specific prior written permission.
1510235Syasuko.eckert@amd.com *
1610235Syasuko.eckert@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710235Syasuko.eckert@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810235Syasuko.eckert@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910235Syasuko.eckert@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010235Syasuko.eckert@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110235Syasuko.eckert@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210235Syasuko.eckert@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310235Syasuko.eckert@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410235Syasuko.eckert@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510235Syasuko.eckert@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610235Syasuko.eckert@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710235Syasuko.eckert@amd.com */
2810235Syasuko.eckert@amd.com
2910235Syasuko.eckert@amd.com/*
3010235Syasuko.eckert@amd.com * Unordered buffer of messages that can be inserted such
3110235Syasuko.eckert@amd.com * that they can be dequeued after a given delta time has expired.
3210235Syasuko.eckert@amd.com */
3310235Syasuko.eckert@amd.com
3410235Syasuko.eckert@amd.com#ifndef __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
3510235Syasuko.eckert@amd.com#define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
3610235Syasuko.eckert@amd.com
3710235Syasuko.eckert@amd.com#include <algorithm>
3810235Syasuko.eckert@amd.com#include <cassert>
3910235Syasuko.eckert@amd.com#include <functional>
4010235Syasuko.eckert@amd.com#include <iostream>
4110235Syasuko.eckert@amd.com#include <string>
4210235Syasuko.eckert@amd.com#include <vector>
4310235Syasuko.eckert@amd.com
4410235Syasuko.eckert@amd.com#include "mem/packet.hh"
4510235Syasuko.eckert@amd.com#include "mem/ruby/buffers/MessageBufferNode.hh"
4610235Syasuko.eckert@amd.com#include "mem/ruby/common/Address.hh"
4710235Syasuko.eckert@amd.com#include "mem/ruby/common/Consumer.hh"
4810235Syasuko.eckert@amd.com#include "mem/ruby/slicc_interface/Message.hh"
4910235Syasuko.eckert@amd.com
5010235Syasuko.eckert@amd.comclass MessageBuffer
5110235Syasuko.eckert@amd.com{
5210235Syasuko.eckert@amd.com  public:
5310235Syasuko.eckert@amd.com    MessageBuffer(const std::string &name = "");
5410235Syasuko.eckert@amd.com
5510235Syasuko.eckert@amd.com    std::string name() const { return m_name; }
5610235Syasuko.eckert@amd.com
5710235Syasuko.eckert@amd.com    void setRecycleLatency(Cycles recycle_latency)
5810235Syasuko.eckert@amd.com    { m_recycle_latency = recycle_latency; }
5910235Syasuko.eckert@amd.com
6010235Syasuko.eckert@amd.com    void reanalyzeMessages(const Address& addr);
6110235Syasuko.eckert@amd.com    void reanalyzeAllMessages();
6210235Syasuko.eckert@amd.com    void stallMessage(const Address& addr);
6310235Syasuko.eckert@amd.com
6410235Syasuko.eckert@amd.com    // TRUE if head of queue timestamp <= SystemTime
6510235Syasuko.eckert@amd.com    bool isReady() const;
6610235Syasuko.eckert@amd.com
6710235Syasuko.eckert@amd.com    void
6810235Syasuko.eckert@amd.com    delayHead()
6910235Syasuko.eckert@amd.com    {
7010235Syasuko.eckert@amd.com        MessageBufferNode node = m_prio_heap.front();
7110235Syasuko.eckert@amd.com        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
7210235Syasuko.eckert@amd.com                      std::greater<MessageBufferNode>());
7310235Syasuko.eckert@amd.com        m_prio_heap.pop_back();
7410235Syasuko.eckert@amd.com        enqueue(node.m_msgptr, Cycles(1));
7510235Syasuko.eckert@amd.com    }
7610235Syasuko.eckert@amd.com
7710235Syasuko.eckert@amd.com    bool areNSlotsAvailable(unsigned int n);
7810235Syasuko.eckert@amd.com    int getPriority() { return m_priority_rank; }
7910235Syasuko.eckert@amd.com    void setPriority(int rank) { m_priority_rank = rank; }
8010235Syasuko.eckert@amd.com    void setConsumer(Consumer* consumer)
8110235Syasuko.eckert@amd.com    {
8210235Syasuko.eckert@amd.com        if (m_consumer != NULL) {
8310235Syasuko.eckert@amd.com            fatal("Trying to connect %s to MessageBuffer %s. \
8410235Syasuko.eckert@amd.com                  \n%s already connected. Check the cntrl_id's.\n",
8510235Syasuko.eckert@amd.com                  *consumer, *this, *m_consumer);
8610235Syasuko.eckert@amd.com        }
8710235Syasuko.eckert@amd.com        m_consumer = consumer;
8810235Syasuko.eckert@amd.com    }
8910235Syasuko.eckert@amd.com
9010235Syasuko.eckert@amd.com    void setSender(ClockedObject* obj)
9110235Syasuko.eckert@amd.com    {
9210235Syasuko.eckert@amd.com        assert(m_sender == NULL || m_sender == obj);
9310235Syasuko.eckert@amd.com        m_sender = obj;
9410235Syasuko.eckert@amd.com    }
9510235Syasuko.eckert@amd.com
9610235Syasuko.eckert@amd.com    void setReceiver(ClockedObject* obj)
9710235Syasuko.eckert@amd.com    {
9810235Syasuko.eckert@amd.com        assert(m_receiver == NULL || m_receiver == obj);
9910235Syasuko.eckert@amd.com        m_receiver = obj;
10010235Syasuko.eckert@amd.com    }
10110235Syasuko.eckert@amd.com
10210235Syasuko.eckert@amd.com    void setDescription(const std::string& name) { m_name = name; }
10310235Syasuko.eckert@amd.com    std::string getDescription() { return m_name;}
10410235Syasuko.eckert@amd.com
10510235Syasuko.eckert@amd.com    Consumer* getConsumer() { return m_consumer; }
10610235Syasuko.eckert@amd.com
10710235Syasuko.eckert@amd.com    //! Function for extracting the message at the head of the
10810235Syasuko.eckert@amd.com    //! message queue.  The function assumes that the queue is nonempty.
10910235Syasuko.eckert@amd.com    const Message* peek() const;
11010235Syasuko.eckert@amd.com    const MsgPtr getMsgPtrCopy() const;
11110235Syasuko.eckert@amd.com
11210235Syasuko.eckert@amd.com    const MsgPtr&
11310235Syasuko.eckert@amd.com    peekMsgPtr() const
11410235Syasuko.eckert@amd.com    {
11510235Syasuko.eckert@amd.com        assert(isReady());
11610235Syasuko.eckert@amd.com        return m_prio_heap.front().m_msgptr;
11710235Syasuko.eckert@amd.com    }
11810235Syasuko.eckert@amd.com
11910235Syasuko.eckert@amd.com    void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
12010235Syasuko.eckert@amd.com    void enqueue(MsgPtr message, Cycles delta);
12110235Syasuko.eckert@amd.com
12210235Syasuko.eckert@amd.com    //! Updates the delay cycles of the message at the of the queue,
12310235Syasuko.eckert@amd.com    //! removes it from the queue and returns its total delay.
12410235Syasuko.eckert@amd.com    Cycles dequeue_getDelayCycles();
12510235Syasuko.eckert@amd.com
12610235Syasuko.eckert@amd.com    void dequeue();
12710235Syasuko.eckert@amd.com
12810235Syasuko.eckert@amd.com    void recycle();
12910235Syasuko.eckert@amd.com    bool isEmpty() const { return m_prio_heap.size() == 0; }
13010235Syasuko.eckert@amd.com
13110235Syasuko.eckert@amd.com    void
13210235Syasuko.eckert@amd.com    setOrdering(bool order)
13310235Syasuko.eckert@amd.com    {
13410235Syasuko.eckert@amd.com        m_strict_fifo = order;
13510235Syasuko.eckert@amd.com        m_ordering_set = true;
13610235Syasuko.eckert@amd.com    }
13710235Syasuko.eckert@amd.com
13810235Syasuko.eckert@amd.com    void resize(int size) { m_max_size = size; }
13910235Syasuko.eckert@amd.com    int getSize();
14010235Syasuko.eckert@amd.com    void setRandomization(bool random_flag) { m_randomization = random_flag; }
14110235Syasuko.eckert@amd.com
14210235Syasuko.eckert@amd.com    void clear();
14310235Syasuko.eckert@amd.com    void print(std::ostream& out) const;
14410235Syasuko.eckert@amd.com    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
14510235Syasuko.eckert@amd.com
14610235Syasuko.eckert@amd.com    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
14710235Syasuko.eckert@amd.com    void setVnet(int net) { m_vnet_id = net; }
14810235Syasuko.eckert@amd.com
14910235Syasuko.eckert@amd.com    // Function for figuring out if any of the messages in the buffer can
15010235Syasuko.eckert@amd.com    // satisfy the read request for the address in the packet.
15110235Syasuko.eckert@amd.com    // Return value, if true, indicates that the request was fulfilled.
15210235Syasuko.eckert@amd.com    bool functionalRead(Packet *pkt);
15310235Syasuko.eckert@amd.com
15410235Syasuko.eckert@amd.com    // Function for figuring out if any of the messages in the buffer need
15510235Syasuko.eckert@amd.com    // to be updated with the data from the packet.
15610235Syasuko.eckert@amd.com    // Return value indicates the number of messages that were updated.
15710235Syasuko.eckert@amd.com    // This required for debugging the code.
15810235Syasuko.eckert@amd.com    uint32_t functionalWrite(Packet *pkt);
15910235Syasuko.eckert@amd.com
16010235Syasuko.eckert@amd.com  private:
16110235Syasuko.eckert@amd.com    //added by SS
16210235Syasuko.eckert@amd.com    Cycles m_recycle_latency;
16310235Syasuko.eckert@amd.com
16410235Syasuko.eckert@amd.com    // Data Members (m_ prefix)
16510235Syasuko.eckert@amd.com    //! The two ends of the buffer.
16610235Syasuko.eckert@amd.com    ClockedObject* m_sender;
16710235Syasuko.eckert@amd.com    ClockedObject* m_receiver;
16810235Syasuko.eckert@amd.com
16910235Syasuko.eckert@amd.com    //! Consumer to signal a wakeup(), can be NULL
17010235Syasuko.eckert@amd.com    Consumer* m_consumer;
17110235Syasuko.eckert@amd.com    std::vector<MessageBufferNode> m_prio_heap;
17210235Syasuko.eckert@amd.com
17310235Syasuko.eckert@amd.com    // use a std::map for the stalled messages as this container is
17410235Syasuko.eckert@amd.com    // sorted and ensures a well-defined iteration order
17510235Syasuko.eckert@amd.com    typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
17610235Syasuko.eckert@amd.com    typedef std::vector<MsgPtr>::iterator MsgListIter;
17710235Syasuko.eckert@amd.com
17810235Syasuko.eckert@amd.com    StallMsgMapType m_stall_msg_map;
17910235Syasuko.eckert@amd.com    std::string m_name;
18010235Syasuko.eckert@amd.com
18110235Syasuko.eckert@amd.com    unsigned int m_max_size;
18210235Syasuko.eckert@amd.com    Cycles m_time_last_time_size_checked;
18310235Syasuko.eckert@amd.com    unsigned int m_size_last_time_size_checked;
18410235Syasuko.eckert@amd.com
18510235Syasuko.eckert@amd.com    // variables used so enqueues appear to happen imediately, while
18610235Syasuko.eckert@amd.com    // pop happen the next cycle
18710235Syasuko.eckert@amd.com    Cycles m_time_last_time_enqueue;
18810235Syasuko.eckert@amd.com    Cycles m_time_last_time_pop;
18910235Syasuko.eckert@amd.com    unsigned int m_size_at_cycle_start;
19010235Syasuko.eckert@amd.com    unsigned int m_msgs_this_cycle;
19110235Syasuko.eckert@amd.com
19210235Syasuko.eckert@amd.com    int m_not_avail_count;  // count the # of times I didn't have N
19310235Syasuko.eckert@amd.com                            // slots available
19410235Syasuko.eckert@amd.com    uint64 m_msg_counter;
19510235Syasuko.eckert@amd.com    int m_priority_rank;
19610235Syasuko.eckert@amd.com    bool m_strict_fifo;
19710235Syasuko.eckert@amd.com    bool m_ordering_set;
19810235Syasuko.eckert@amd.com    bool m_randomization;
19910235Syasuko.eckert@amd.com
20010235Syasuko.eckert@amd.com    Tick m_last_arrival_time;
20110235Syasuko.eckert@amd.com
20210235Syasuko.eckert@amd.com    int m_input_link_id;
20310235Syasuko.eckert@amd.com    int m_vnet_id;
20410235Syasuko.eckert@amd.com};
20510235Syasuko.eckert@amd.com
20610235Syasuko.eckert@amd.comCycles random_time();
20710235Syasuko.eckert@amd.com
20810235Syasuko.eckert@amd.cominline std::ostream&
209operator<<(std::ostream& out, const MessageBuffer& obj)
210{
211    obj.print(out);
212    out << std::flush;
213    return out;
214}
215
216#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
217