MessageBuffer.hh revision 12492
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
3412492Sodanrc@yahoo.com.br#ifndef __MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__
3512492Sodanrc@yahoo.com.br#define __MEM_RUBY_NETWORK_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
4411800Sbrandon.potter@amd.com#include "base/trace.hh"
4511021Sjthestness@gmail.com#include "debug/RubyQueue.hh"
468229SN/A#include "mem/ruby/common/Address.hh"
476154SN/A#include "mem/ruby/common/Consumer.hh"
486154SN/A#include "mem/ruby/slicc_interface/Message.hh"
4910301Snilay@cs.wisc.edu#include "mem/packet.hh"
5011021Sjthestness@gmail.com#include "params/MessageBuffer.hh"
5111021Sjthestness@gmail.com#include "sim/sim_object.hh"
526145SN/A
5311021Sjthestness@gmail.comclass MessageBuffer : public SimObject
547039SN/A{
557039SN/A  public:
5611021Sjthestness@gmail.com    typedef MessageBufferParams Params;
5711021Sjthestness@gmail.com    MessageBuffer(const Params *p);
586145SN/A
5911111Snilay@cs.wisc.edu    void reanalyzeMessages(Addr addr, Tick current_time);
6011111Snilay@cs.wisc.edu    void reanalyzeAllMessages(Tick current_time);
6111111Snilay@cs.wisc.edu    void stallMessage(Addr addr, Tick current_time);
627567SN/A
637039SN/A    // TRUE if head of queue timestamp <= SystemTime
6411111Snilay@cs.wisc.edu    bool isReady(Tick current_time) const;
656145SN/A
667039SN/A    void
6711111Snilay@cs.wisc.edu    delayHead(Tick current_time, Tick delta)
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();
7311111Snilay@cs.wisc.edu        enqueue(m, current_time, delta);
747039SN/A    }
756145SN/A
7611111Snilay@cs.wisc.edu    bool areNSlotsAvailable(unsigned int n, Tick curTime);
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    {
8111021Sjthestness@gmail.com        DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer);
829629SN/A        if (m_consumer != NULL) {
839629SN/A            fatal("Trying to connect %s to MessageBuffer %s. \
849629SN/A                  \n%s already connected. Check the cntrl_id's.\n",
859629SN/A                  *consumer, *this, *m_consumer);
869629SN/A        }
879602SN/A        m_consumer = consumer;
887039SN/A    }
896145SN/A
9011021Sjthestness@gmail.com    Consumer* getConsumer() { return m_consumer; }
916863SN/A
9211021Sjthestness@gmail.com    bool getOrdered() { return m_strict_fifo; }
936145SN/A
9410074SN/A    //! Function for extracting the message at the head of the
9510074SN/A    //! message queue.  The function assumes that the queue is nonempty.
9610074SN/A    const Message* peek() const;
976145SN/A
9811111Snilay@cs.wisc.edu    const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
996145SN/A
10011111Snilay@cs.wisc.edu    void enqueue(MsgPtr message, Tick curTime, Tick delta);
1019465SN/A
10210226SN/A    //! Updates the delay cycles of the message at the head of the queue,
10310074SN/A    //! removes it from the queue and returns its total delay.
10411796Smatthew.poremba@amd.com    Tick dequeue(Tick current_time, bool decrement_messages = true);
10510074SN/A
10611797Smatthew.poremba@amd.com    void registerDequeueCallback(std::function<void()> callback);
10711797Smatthew.poremba@amd.com    void unregisterDequeueCallback();
10811797Smatthew.poremba@amd.com
10911111Snilay@cs.wisc.edu    void recycle(Tick current_time, Tick recycle_latency);
1107039SN/A    bool isEmpty() const { return m_prio_heap.size() == 0; }
11110979Sdavid.hashe@amd.com    bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
11210979Sdavid.hashe@amd.com    unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
1136145SN/A
11411111Snilay@cs.wisc.edu    unsigned int getSize(Tick curTime);
1156145SN/A
1167039SN/A    void clear();
1177039SN/A    void print(std::ostream& out) const;
1187039SN/A    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
1196285SN/A
1207973SN/A    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
1217973SN/A    void setVnet(int net) { m_vnet_id = net; }
1227973SN/A
12311732Smatthew.poremba@amd.com    void regStats();
12411732Smatthew.poremba@amd.com
1259302SN/A    // Function for figuring out if any of the messages in the buffer need
1269302SN/A    // to be updated with the data from the packet.
1279302SN/A    // Return value indicates the number of messages that were updated.
1289302SN/A    // This required for debugging the code.
1299302SN/A    uint32_t functionalWrite(Packet *pkt);
1309302SN/A
1317039SN/A  private:
13210087SN/A    void reanalyzeList(std::list<MsgPtr> &, Tick);
13310087SN/A
13410087SN/A  private:
1357039SN/A    // Data Members (m_ prefix)
1369465SN/A    //! Consumer to signal a wakeup(), can be NULL
1379602SN/A    Consumer* m_consumer;
13810893Snilay@cs.wisc.edu    std::vector<MsgPtr> m_prio_heap;
1399465SN/A
14011797Smatthew.poremba@amd.com    std::function<void()> m_dequeue_callback;
14111797Smatthew.poremba@amd.com
1428943SN/A    // use a std::map for the stalled messages as this container is
1438943SN/A    // sorted and ensures a well-defined iteration order
14411025Snilay@cs.wisc.edu    typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
1457567SN/A
14611779Sjthestness@gmail.com    /**
14711779Sjthestness@gmail.com     * A map from line addresses to lists of stalled messages for that line.
14811779Sjthestness@gmail.com     * If this buffer allows the receiver to stall messages, on a stall
14911779Sjthestness@gmail.com     * request, the stalled message is removed from the m_prio_heap and placed
15011779Sjthestness@gmail.com     * in the m_stall_msg_map. Messages are held there until the receiver
15111779Sjthestness@gmail.com     * requests they be reanalyzed, at which point they are moved back to
15211779Sjthestness@gmail.com     * m_prio_heap.
15311779Sjthestness@gmail.com     *
15411779Sjthestness@gmail.com     * NOTE: The stall map holds messages in the order in which they were
15511779Sjthestness@gmail.com     * initially received, and when a line is unblocked, the messages are
15611779Sjthestness@gmail.com     * moved back to the m_prio_heap in the same order. This prevents starving
15711779Sjthestness@gmail.com     * older requests with younger ones.
15811779Sjthestness@gmail.com     */
1597567SN/A    StallMsgMapType m_stall_msg_map;
1606145SN/A
16111779Sjthestness@gmail.com    /**
16211779Sjthestness@gmail.com     * Current size of the stall map.
16311779Sjthestness@gmail.com     * Track the number of messages held in stall map lists. This is used to
16411779Sjthestness@gmail.com     * ensure that if the buffer is finite-sized, it blocks further requests
16511779Sjthestness@gmail.com     * when the m_prio_heap and m_stall_msg_map contain m_max_size messages.
16611779Sjthestness@gmail.com     */
16711779Sjthestness@gmail.com    int m_stall_map_size;
16811779Sjthestness@gmail.com
16911779Sjthestness@gmail.com    /**
17011779Sjthestness@gmail.com     * The maximum capacity. For finite-sized buffers, m_max_size stores a
17111779Sjthestness@gmail.com     * number greater than 0 to indicate the maximum allowed number of messages
17211779Sjthestness@gmail.com     * in the buffer at any time. To get infinitely-sized buffers, set buffer
17311779Sjthestness@gmail.com     * size: m_max_size = 0
17411779Sjthestness@gmail.com     */
17511021Sjthestness@gmail.com    const unsigned int m_max_size;
17611779Sjthestness@gmail.com
17711111Snilay@cs.wisc.edu    Tick m_time_last_time_size_checked;
1789770SN/A    unsigned int m_size_last_time_size_checked;
1796145SN/A
18010917Sbrandon.potter@amd.com    // variables used so enqueues appear to happen immediately, while
1817039SN/A    // pop happen the next cycle
18211111Snilay@cs.wisc.edu    Tick m_time_last_time_enqueue;
18310097SN/A    Tick m_time_last_time_pop;
18410097SN/A    Tick m_last_arrival_time;
18510097SN/A
1869770SN/A    unsigned int m_size_at_cycle_start;
1879770SN/A    unsigned int m_msgs_this_cycle;
1887039SN/A
18911732Smatthew.poremba@amd.com    Stats::Scalar m_not_avail_count;  // count the # of times I didn't have N
19011732Smatthew.poremba@amd.com                                      // slots available
19111061Snilay@cs.wisc.edu    uint64_t m_msg_counter;
1927039SN/A    int m_priority_rank;
19311021Sjthestness@gmail.com    const bool m_strict_fifo;
19411021Sjthestness@gmail.com    const bool m_randomization;
1959499SN/A
1967973SN/A    int m_input_link_id;
1977973SN/A    int m_vnet_id;
19811796Smatthew.poremba@amd.com
19911796Smatthew.poremba@amd.com    Stats::Average m_buf_msgs;
20011796Smatthew.poremba@amd.com    Stats::Average m_stall_time;
20111796Smatthew.poremba@amd.com    Stats::Scalar m_stall_count;
20211796Smatthew.poremba@amd.com    Stats::Formula m_occupancy;
2036145SN/A};
2046145SN/A
20511111Snilay@cs.wisc.eduTick random_time();
2069554SN/A
2077039SN/Ainline std::ostream&
2087039SN/Aoperator<<(std::ostream& out, const MessageBuffer& obj)
2096145SN/A{
2107039SN/A    obj.print(out);
2117039SN/A    out << std::flush;
2127039SN/A    return out;
2136145SN/A}
2146145SN/A
21512492Sodanrc@yahoo.com.br#endif //__MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__
216