MessageBuffer.hh revision 10074
110066Sandreas.hansson@arm.com/*
210066Sandreas.hansson@arm.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
310066Sandreas.hansson@arm.com * All rights reserved.
410066Sandreas.hansson@arm.com *
510066Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
610066Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
710066Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
810066Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
910066Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
1010066Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
1110066Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
1210066Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
1310066Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
1410066Sandreas.hansson@arm.com * this software without specific prior written permission.
1510066Sandreas.hansson@arm.com *
1610066Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710066Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810066Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910066Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010066Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110066Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210066Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310066Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410066Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510066Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610066Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710066Sandreas.hansson@arm.com */
2810066Sandreas.hansson@arm.com
2910066Sandreas.hansson@arm.com/*
3010066Sandreas.hansson@arm.com * Unordered buffer of messages that can be inserted such
3110066Sandreas.hansson@arm.com * that they can be dequeued after a given delta time has expired.
3210066Sandreas.hansson@arm.com */
3310066Sandreas.hansson@arm.com
3410066Sandreas.hansson@arm.com#ifndef __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
3510066Sandreas.hansson@arm.com#define __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
3610066Sandreas.hansson@arm.com
3710066Sandreas.hansson@arm.com#include <algorithm>
3810066Sandreas.hansson@arm.com#include <cassert>
3910066Sandreas.hansson@arm.com#include <functional>
4010066Sandreas.hansson@arm.com#include <iostream>
4110066Sandreas.hansson@arm.com#include <string>
4210066Sandreas.hansson@arm.com#include <vector>
4310066Sandreas.hansson@arm.com
4410066Sandreas.hansson@arm.com#include "mem/packet.hh"
4510066Sandreas.hansson@arm.com#include "mem/ruby/buffers/MessageBufferNode.hh"
4610066Sandreas.hansson@arm.com#include "mem/ruby/common/Address.hh"
4710066Sandreas.hansson@arm.com#include "mem/ruby/common/Consumer.hh"
4810066Sandreas.hansson@arm.com#include "mem/ruby/slicc_interface/Message.hh"
4910066Sandreas.hansson@arm.com
5010066Sandreas.hansson@arm.comclass MessageBuffer
5110066Sandreas.hansson@arm.com{
5210066Sandreas.hansson@arm.com  public:
5310066Sandreas.hansson@arm.com    MessageBuffer(const std::string &name = "");
5410066Sandreas.hansson@arm.com
5510066Sandreas.hansson@arm.com    std::string name() const { return m_name; }
5610066Sandreas.hansson@arm.com
5710066Sandreas.hansson@arm.com    void setRecycleLatency(Cycles recycle_latency)
5810066Sandreas.hansson@arm.com    { m_recycle_latency = recycle_latency; }
5910066Sandreas.hansson@arm.com
6010066Sandreas.hansson@arm.com    void reanalyzeMessages(const Address& addr);
6110066Sandreas.hansson@arm.com    void reanalyzeAllMessages();
6210066Sandreas.hansson@arm.com    void stallMessage(const Address& addr);
6310066Sandreas.hansson@arm.com
6410066Sandreas.hansson@arm.com    // TRUE if head of queue timestamp <= SystemTime
6510066Sandreas.hansson@arm.com    bool isReady() const;
6610066Sandreas.hansson@arm.com
6710066Sandreas.hansson@arm.com    void
6810066Sandreas.hansson@arm.com    delayHead()
6910066Sandreas.hansson@arm.com    {
7010066Sandreas.hansson@arm.com        MessageBufferNode node = m_prio_heap.front();
7110066Sandreas.hansson@arm.com        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
7210066Sandreas.hansson@arm.com                      std::greater<MessageBufferNode>());
7310066Sandreas.hansson@arm.com        m_prio_heap.pop_back();
7410066Sandreas.hansson@arm.com        enqueue(node.m_msgptr, Cycles(1));
7510066Sandreas.hansson@arm.com    }
7610066Sandreas.hansson@arm.com
7710066Sandreas.hansson@arm.com    bool areNSlotsAvailable(unsigned int n);
7810130Sandreas.hansson@arm.com    int getPriority() { return m_priority_rank; }
7910130Sandreas.hansson@arm.com    void setPriority(int rank) { m_priority_rank = rank; }
8011294Sandreas.hansson@arm.com    void setConsumer(Consumer* consumer)
8111294Sandreas.hansson@arm.com    {
8210130Sandreas.hansson@arm.com        if (m_consumer != NULL) {
8310066Sandreas.hansson@arm.com            fatal("Trying to connect %s to MessageBuffer %s. \
8410066Sandreas.hansson@arm.com                  \n%s already connected. Check the cntrl_id's.\n",
8510066Sandreas.hansson@arm.com                  *consumer, *this, *m_consumer);
8610066Sandreas.hansson@arm.com        }
8710130Sandreas.hansson@arm.com        m_consumer = consumer;
8810066Sandreas.hansson@arm.com    }
8910066Sandreas.hansson@arm.com
9010066Sandreas.hansson@arm.com    void setSender(ClockedObject* obj)
9110066Sandreas.hansson@arm.com    {
92        assert(m_sender == NULL || m_sender == obj);
93        m_sender = obj;
94    }
95
96    void setReceiver(ClockedObject* obj)
97    {
98        assert(m_receiver == NULL || m_receiver == obj);
99        m_receiver = obj;
100    }
101
102    void setDescription(const std::string& name) { m_name = name; }
103    std::string getDescription() { return m_name;}
104
105    Consumer* getConsumer() { return m_consumer; }
106
107    //! Function for extracting the message at the head of the
108    //! message queue.  The function assumes that the queue is nonempty.
109    const Message* peek() const;
110    const MsgPtr getMsgPtrCopy() const;
111
112    const MsgPtr&
113    peekMsgPtr() const
114    {
115        assert(isReady());
116        return m_prio_heap.front().m_msgptr;
117    }
118
119    void enqueue(MsgPtr message) { enqueue(message, Cycles(1)); }
120    void enqueue(MsgPtr message, Cycles delta);
121
122    //! Updates the delay cycles of the message at the of the queue,
123    //! removes it from the queue and returns its total delay.
124    Cycles dequeue_getDelayCycles();
125
126    void dequeue();
127
128    void recycle();
129    bool isEmpty() const { return m_prio_heap.size() == 0; }
130
131    void
132    setOrdering(bool order)
133    {
134        m_strict_fifo = order;
135        m_ordering_set = true;
136    }
137
138    void resize(int size) { m_max_size = size; }
139    int getSize();
140    void setRandomization(bool random_flag) { m_randomization = random_flag; }
141
142    void clear();
143    void print(std::ostream& out) const;
144    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
145
146    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
147    void setVnet(int net) { m_vnet_id = net; }
148
149    // Function for figuring out if any of the messages in the buffer can
150    // satisfy the read request for the address in the packet.
151    // Return value, if true, indicates that the request was fulfilled.
152    bool functionalRead(Packet *pkt);
153
154    // Function for figuring out if any of the messages in the buffer need
155    // to be updated with the data from the packet.
156    // Return value indicates the number of messages that were updated.
157    // This required for debugging the code.
158    uint32_t functionalWrite(Packet *pkt);
159
160  private:
161    //added by SS
162    Cycles m_recycle_latency;
163
164    // Data Members (m_ prefix)
165    //! The two ends of the buffer.
166    ClockedObject* m_sender;
167    ClockedObject* m_receiver;
168
169    //! Consumer to signal a wakeup(), can be NULL
170    Consumer* m_consumer;
171    std::vector<MessageBufferNode> m_prio_heap;
172
173    // use a std::map for the stalled messages as this container is
174    // sorted and ensures a well-defined iteration order
175    typedef std::map< Address, std::list<MsgPtr> > StallMsgMapType;
176    typedef std::vector<MsgPtr>::iterator MsgListIter;
177
178    StallMsgMapType m_stall_msg_map;
179    std::string m_name;
180
181    unsigned int m_max_size;
182    Cycles m_time_last_time_size_checked;
183    unsigned int m_size_last_time_size_checked;
184
185    // variables used so enqueues appear to happen imediately, while
186    // pop happen the next cycle
187    Cycles m_time_last_time_enqueue;
188    Cycles m_time_last_time_pop;
189    unsigned int m_size_at_cycle_start;
190    unsigned int m_msgs_this_cycle;
191
192    int m_not_avail_count;  // count the # of times I didn't have N
193                            // slots available
194    uint64 m_msg_counter;
195    int m_priority_rank;
196    bool m_strict_fifo;
197    bool m_ordering_set;
198    bool m_randomization;
199
200    Tick m_last_arrival_time;
201
202    int m_input_link_id;
203    int m_vnet_id;
204};
205
206Cycles random_time();
207
208inline 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