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"
4613784Sgabeblack@google.com#include "mem/packet.hh"
4713784Sgabeblack@google.com#include "mem/port.hh"
488229SN/A#include "mem/ruby/common/Address.hh"
496154SN/A#include "mem/ruby/common/Consumer.hh"
5013784Sgabeblack@google.com#include "mem/ruby/network/dummy_port.hh"
516154SN/A#include "mem/ruby/slicc_interface/Message.hh"
5211021Sjthestness@gmail.com#include "params/MessageBuffer.hh"
5311021Sjthestness@gmail.com#include "sim/sim_object.hh"
546145SN/A
5511021Sjthestness@gmail.comclass MessageBuffer : public SimObject
567039SN/A{
577039SN/A  public:
5811021Sjthestness@gmail.com    typedef MessageBufferParams Params;
5911021Sjthestness@gmail.com    MessageBuffer(const Params *p);
606145SN/A
6111111Snilay@cs.wisc.edu    void reanalyzeMessages(Addr addr, Tick current_time);
6211111Snilay@cs.wisc.edu    void reanalyzeAllMessages(Tick current_time);
6311111Snilay@cs.wisc.edu    void stallMessage(Addr addr, Tick current_time);
647567SN/A
657039SN/A    // TRUE if head of queue timestamp <= SystemTime
6611111Snilay@cs.wisc.edu    bool isReady(Tick current_time) const;
676145SN/A
687039SN/A    void
6911111Snilay@cs.wisc.edu    delayHead(Tick current_time, Tick delta)
707039SN/A    {
7110893Snilay@cs.wisc.edu        MsgPtr m = m_prio_heap.front();
727456SN/A        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
7310893Snilay@cs.wisc.edu                      std::greater<MsgPtr>());
747456SN/A        m_prio_heap.pop_back();
7511111Snilay@cs.wisc.edu        enqueue(m, current_time, delta);
767039SN/A    }
776145SN/A
7811111Snilay@cs.wisc.edu    bool areNSlotsAvailable(unsigned int n, Tick curTime);
797039SN/A    int getPriority() { return m_priority_rank; }
807039SN/A    void setPriority(int rank) { m_priority_rank = rank; }
819602SN/A    void setConsumer(Consumer* consumer)
827039SN/A    {
8311021Sjthestness@gmail.com        DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer);
849629SN/A        if (m_consumer != NULL) {
859629SN/A            fatal("Trying to connect %s to MessageBuffer %s. \
869629SN/A                  \n%s already connected. Check the cntrl_id's.\n",
879629SN/A                  *consumer, *this, *m_consumer);
889629SN/A        }
899602SN/A        m_consumer = consumer;
907039SN/A    }
916145SN/A
9211021Sjthestness@gmail.com    Consumer* getConsumer() { return m_consumer; }
936863SN/A
9411021Sjthestness@gmail.com    bool getOrdered() { return m_strict_fifo; }
956145SN/A
9610074SN/A    //! Function for extracting the message at the head of the
9710074SN/A    //! message queue.  The function assumes that the queue is nonempty.
9810074SN/A    const Message* peek() const;
996145SN/A
10011111Snilay@cs.wisc.edu    const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
1016145SN/A
10211111Snilay@cs.wisc.edu    void enqueue(MsgPtr message, Tick curTime, Tick delta);
1039465SN/A
10410226SN/A    //! Updates the delay cycles of the message at the head of the queue,
10510074SN/A    //! removes it from the queue and returns its total delay.
10611796Smatthew.poremba@amd.com    Tick dequeue(Tick current_time, bool decrement_messages = true);
10710074SN/A
10811797Smatthew.poremba@amd.com    void registerDequeueCallback(std::function<void()> callback);
10911797Smatthew.poremba@amd.com    void unregisterDequeueCallback();
11011797Smatthew.poremba@amd.com
11111111Snilay@cs.wisc.edu    void recycle(Tick current_time, Tick recycle_latency);
1127039SN/A    bool isEmpty() const { return m_prio_heap.size() == 0; }
11310979Sdavid.hashe@amd.com    bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
11410979Sdavid.hashe@amd.com    unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
1156145SN/A
11611111Snilay@cs.wisc.edu    unsigned int getSize(Tick curTime);
1176145SN/A
1187039SN/A    void clear();
1197039SN/A    void print(std::ostream& out) const;
1207039SN/A    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
1216285SN/A
1227973SN/A    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
1237973SN/A    void setVnet(int net) { m_vnet_id = net; }
1247973SN/A
12513784Sgabeblack@google.com    Port &
12613784Sgabeblack@google.com    getPort(const std::string &, PortID idx=InvalidPortID) override
12713784Sgabeblack@google.com    {
12813784Sgabeblack@google.com        return RubyDummyPort::instance();
12913784Sgabeblack@google.com    }
13013784Sgabeblack@google.com
13113799SAndrea.Mondelli@ucf.edu    void regStats() override;
13211732Smatthew.poremba@amd.com
1339302SN/A    // Function for figuring out if any of the messages in the buffer need
1349302SN/A    // to be updated with the data from the packet.
1359302SN/A    // Return value indicates the number of messages that were updated.
1369302SN/A    // This required for debugging the code.
1379302SN/A    uint32_t functionalWrite(Packet *pkt);
1389302SN/A
1397039SN/A  private:
14010087SN/A    void reanalyzeList(std::list<MsgPtr> &, Tick);
14110087SN/A
14210087SN/A  private:
1437039SN/A    // Data Members (m_ prefix)
1449465SN/A    //! Consumer to signal a wakeup(), can be NULL
1459602SN/A    Consumer* m_consumer;
14610893Snilay@cs.wisc.edu    std::vector<MsgPtr> m_prio_heap;
1479465SN/A
14811797Smatthew.poremba@amd.com    std::function<void()> m_dequeue_callback;
14911797Smatthew.poremba@amd.com
1508943SN/A    // use a std::map for the stalled messages as this container is
1518943SN/A    // sorted and ensures a well-defined iteration order
15211025Snilay@cs.wisc.edu    typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
1537567SN/A
15411779Sjthestness@gmail.com    /**
15511779Sjthestness@gmail.com     * A map from line addresses to lists of stalled messages for that line.
15611779Sjthestness@gmail.com     * If this buffer allows the receiver to stall messages, on a stall
15711779Sjthestness@gmail.com     * request, the stalled message is removed from the m_prio_heap and placed
15811779Sjthestness@gmail.com     * in the m_stall_msg_map. Messages are held there until the receiver
15911779Sjthestness@gmail.com     * requests they be reanalyzed, at which point they are moved back to
16011779Sjthestness@gmail.com     * m_prio_heap.
16111779Sjthestness@gmail.com     *
16211779Sjthestness@gmail.com     * NOTE: The stall map holds messages in the order in which they were
16311779Sjthestness@gmail.com     * initially received, and when a line is unblocked, the messages are
16411779Sjthestness@gmail.com     * moved back to the m_prio_heap in the same order. This prevents starving
16511779Sjthestness@gmail.com     * older requests with younger ones.
16611779Sjthestness@gmail.com     */
1677567SN/A    StallMsgMapType m_stall_msg_map;
1686145SN/A
16911779Sjthestness@gmail.com    /**
17011779Sjthestness@gmail.com     * Current size of the stall map.
17111779Sjthestness@gmail.com     * Track the number of messages held in stall map lists. This is used to
17211779Sjthestness@gmail.com     * ensure that if the buffer is finite-sized, it blocks further requests
17311779Sjthestness@gmail.com     * when the m_prio_heap and m_stall_msg_map contain m_max_size messages.
17411779Sjthestness@gmail.com     */
17511779Sjthestness@gmail.com    int m_stall_map_size;
17611779Sjthestness@gmail.com
17711779Sjthestness@gmail.com    /**
17811779Sjthestness@gmail.com     * The maximum capacity. For finite-sized buffers, m_max_size stores a
17911779Sjthestness@gmail.com     * number greater than 0 to indicate the maximum allowed number of messages
18011779Sjthestness@gmail.com     * in the buffer at any time. To get infinitely-sized buffers, set buffer
18111779Sjthestness@gmail.com     * size: m_max_size = 0
18211779Sjthestness@gmail.com     */
18311021Sjthestness@gmail.com    const unsigned int m_max_size;
18411779Sjthestness@gmail.com
18511111Snilay@cs.wisc.edu    Tick m_time_last_time_size_checked;
1869770SN/A    unsigned int m_size_last_time_size_checked;
1876145SN/A
18810917Sbrandon.potter@amd.com    // variables used so enqueues appear to happen immediately, while
1897039SN/A    // pop happen the next cycle
19011111Snilay@cs.wisc.edu    Tick m_time_last_time_enqueue;
19110097SN/A    Tick m_time_last_time_pop;
19210097SN/A    Tick m_last_arrival_time;
19310097SN/A
1949770SN/A    unsigned int m_size_at_cycle_start;
19514217Ssrikant.bharadwaj@amd.com    unsigned int m_stalled_at_cycle_start;
1969770SN/A    unsigned int m_msgs_this_cycle;
1977039SN/A
19811732Smatthew.poremba@amd.com    Stats::Scalar m_not_avail_count;  // count the # of times I didn't have N
19911732Smatthew.poremba@amd.com                                      // slots available
20011061Snilay@cs.wisc.edu    uint64_t m_msg_counter;
2017039SN/A    int m_priority_rank;
20211021Sjthestness@gmail.com    const bool m_strict_fifo;
20311021Sjthestness@gmail.com    const bool m_randomization;
2049499SN/A
2057973SN/A    int m_input_link_id;
2067973SN/A    int m_vnet_id;
20711796Smatthew.poremba@amd.com
20811796Smatthew.poremba@amd.com    Stats::Average m_buf_msgs;
20911796Smatthew.poremba@amd.com    Stats::Average m_stall_time;
21011796Smatthew.poremba@amd.com    Stats::Scalar m_stall_count;
21111796Smatthew.poremba@amd.com    Stats::Formula m_occupancy;
2126145SN/A};
2136145SN/A
21411111Snilay@cs.wisc.eduTick random_time();
2159554SN/A
2167039SN/Ainline std::ostream&
2177039SN/Aoperator<<(std::ostream& out, const MessageBuffer& obj)
2186145SN/A{
2197039SN/A    obj.print(out);
2207039SN/A    out << std::flush;
2217039SN/A    return out;
2226145SN/A}
2236145SN/A
22412492Sodanrc@yahoo.com.br#endif //__MEM_RUBY_NETWORK_MESSAGEBUFFER_HH__
225