MessageBuffer.hh revision 11796
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
4411021Sjthestness@gmail.com#include "debug/RubyQueue.hh"
458229SN/A#include "mem/ruby/common/Address.hh"
466154SN/A#include "mem/ruby/common/Consumer.hh"
476154SN/A#include "mem/ruby/slicc_interface/Message.hh"
4810301Snilay@cs.wisc.edu#include "mem/packet.hh"
4911021Sjthestness@gmail.com#include "params/MessageBuffer.hh"
5011021Sjthestness@gmail.com#include "sim/sim_object.hh"
516145SN/A
5211021Sjthestness@gmail.comclass MessageBuffer : public SimObject
537039SN/A{
547039SN/A  public:
5511021Sjthestness@gmail.com    typedef MessageBufferParams Params;
5611021Sjthestness@gmail.com    MessageBuffer(const Params *p);
576145SN/A
5811111Snilay@cs.wisc.edu    void reanalyzeMessages(Addr addr, Tick current_time);
5911111Snilay@cs.wisc.edu    void reanalyzeAllMessages(Tick current_time);
6011111Snilay@cs.wisc.edu    void stallMessage(Addr addr, Tick current_time);
617567SN/A
627039SN/A    // TRUE if head of queue timestamp <= SystemTime
6311111Snilay@cs.wisc.edu    bool isReady(Tick current_time) const;
646145SN/A
657039SN/A    void
6611111Snilay@cs.wisc.edu    delayHead(Tick current_time, Tick delta)
677039SN/A    {
6810893Snilay@cs.wisc.edu        MsgPtr m = m_prio_heap.front();
697456SN/A        std::pop_heap(m_prio_heap.begin(), m_prio_heap.end(),
7010893Snilay@cs.wisc.edu                      std::greater<MsgPtr>());
717456SN/A        m_prio_heap.pop_back();
7211111Snilay@cs.wisc.edu        enqueue(m, current_time, delta);
737039SN/A    }
746145SN/A
7511111Snilay@cs.wisc.edu    bool areNSlotsAvailable(unsigned int n, Tick curTime);
767039SN/A    int getPriority() { return m_priority_rank; }
777039SN/A    void setPriority(int rank) { m_priority_rank = rank; }
789602SN/A    void setConsumer(Consumer* consumer)
797039SN/A    {
8011021Sjthestness@gmail.com        DPRINTF(RubyQueue, "Setting consumer: %s\n", *consumer);
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
8911021Sjthestness@gmail.com    Consumer* getConsumer() { return m_consumer; }
906863SN/A
9111021Sjthestness@gmail.com    bool getOrdered() { return m_strict_fifo; }
926145SN/A
9310074SN/A    //! Function for extracting the message at the head of the
9410074SN/A    //! message queue.  The function assumes that the queue is nonempty.
9510074SN/A    const Message* peek() const;
966145SN/A
9711111Snilay@cs.wisc.edu    const MsgPtr &peekMsgPtr() const { return m_prio_heap.front(); }
986145SN/A
9911111Snilay@cs.wisc.edu    void enqueue(MsgPtr message, Tick curTime, Tick delta);
1009465SN/A
10110226SN/A    //! Updates the delay cycles of the message at the head of the queue,
10210074SN/A    //! removes it from the queue and returns its total delay.
10311796Smatthew.poremba@amd.com    Tick dequeue(Tick current_time, bool decrement_messages = true);
10410074SN/A
10511111Snilay@cs.wisc.edu    void recycle(Tick current_time, Tick recycle_latency);
1067039SN/A    bool isEmpty() const { return m_prio_heap.size() == 0; }
10710979Sdavid.hashe@amd.com    bool isStallMapEmpty() { return m_stall_msg_map.size() == 0; }
10810979Sdavid.hashe@amd.com    unsigned int getStallMapSize() { return m_stall_msg_map.size(); }
1096145SN/A
11011111Snilay@cs.wisc.edu    unsigned int getSize(Tick curTime);
1116145SN/A
1127039SN/A    void clear();
1137039SN/A    void print(std::ostream& out) const;
1147039SN/A    void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; }
1156285SN/A
1167973SN/A    void setIncomingLink(int link_id) { m_input_link_id = link_id; }
1177973SN/A    void setVnet(int net) { m_vnet_id = net; }
1187973SN/A
11911732Smatthew.poremba@amd.com    void regStats();
12011732Smatthew.poremba@amd.com
1219302SN/A    // Function for figuring out if any of the messages in the buffer need
1229302SN/A    // to be updated with the data from the packet.
1239302SN/A    // Return value indicates the number of messages that were updated.
1249302SN/A    // This required for debugging the code.
1259302SN/A    uint32_t functionalWrite(Packet *pkt);
1269302SN/A
1277039SN/A  private:
12810087SN/A    void reanalyzeList(std::list<MsgPtr> &, Tick);
12910087SN/A
13010087SN/A  private:
1317039SN/A    // Data Members (m_ prefix)
1329465SN/A    //! Consumer to signal a wakeup(), can be NULL
1339602SN/A    Consumer* m_consumer;
13410893Snilay@cs.wisc.edu    std::vector<MsgPtr> m_prio_heap;
1359465SN/A
1368943SN/A    // use a std::map for the stalled messages as this container is
1378943SN/A    // sorted and ensures a well-defined iteration order
13811025Snilay@cs.wisc.edu    typedef std::map<Addr, std::list<MsgPtr> > StallMsgMapType;
1397567SN/A
14011779Sjthestness@gmail.com    /**
14111779Sjthestness@gmail.com     * A map from line addresses to lists of stalled messages for that line.
14211779Sjthestness@gmail.com     * If this buffer allows the receiver to stall messages, on a stall
14311779Sjthestness@gmail.com     * request, the stalled message is removed from the m_prio_heap and placed
14411779Sjthestness@gmail.com     * in the m_stall_msg_map. Messages are held there until the receiver
14511779Sjthestness@gmail.com     * requests they be reanalyzed, at which point they are moved back to
14611779Sjthestness@gmail.com     * m_prio_heap.
14711779Sjthestness@gmail.com     *
14811779Sjthestness@gmail.com     * NOTE: The stall map holds messages in the order in which they were
14911779Sjthestness@gmail.com     * initially received, and when a line is unblocked, the messages are
15011779Sjthestness@gmail.com     * moved back to the m_prio_heap in the same order. This prevents starving
15111779Sjthestness@gmail.com     * older requests with younger ones.
15211779Sjthestness@gmail.com     */
1537567SN/A    StallMsgMapType m_stall_msg_map;
1546145SN/A
15511779Sjthestness@gmail.com    /**
15611779Sjthestness@gmail.com     * Current size of the stall map.
15711779Sjthestness@gmail.com     * Track the number of messages held in stall map lists. This is used to
15811779Sjthestness@gmail.com     * ensure that if the buffer is finite-sized, it blocks further requests
15911779Sjthestness@gmail.com     * when the m_prio_heap and m_stall_msg_map contain m_max_size messages.
16011779Sjthestness@gmail.com     */
16111779Sjthestness@gmail.com    int m_stall_map_size;
16211779Sjthestness@gmail.com
16311779Sjthestness@gmail.com    /**
16411779Sjthestness@gmail.com     * The maximum capacity. For finite-sized buffers, m_max_size stores a
16511779Sjthestness@gmail.com     * number greater than 0 to indicate the maximum allowed number of messages
16611779Sjthestness@gmail.com     * in the buffer at any time. To get infinitely-sized buffers, set buffer
16711779Sjthestness@gmail.com     * size: m_max_size = 0
16811779Sjthestness@gmail.com     */
16911021Sjthestness@gmail.com    const unsigned int m_max_size;
17011779Sjthestness@gmail.com
17111111Snilay@cs.wisc.edu    Tick m_time_last_time_size_checked;
1729770SN/A    unsigned int m_size_last_time_size_checked;
1736145SN/A
17410917Sbrandon.potter@amd.com    // variables used so enqueues appear to happen immediately, while
1757039SN/A    // pop happen the next cycle
17611111Snilay@cs.wisc.edu    Tick m_time_last_time_enqueue;
17710097SN/A    Tick m_time_last_time_pop;
17810097SN/A    Tick m_last_arrival_time;
17910097SN/A
1809770SN/A    unsigned int m_size_at_cycle_start;
1819770SN/A    unsigned int m_msgs_this_cycle;
1827039SN/A
18311732Smatthew.poremba@amd.com    Stats::Scalar m_not_avail_count;  // count the # of times I didn't have N
18411732Smatthew.poremba@amd.com                                      // slots available
18511061Snilay@cs.wisc.edu    uint64_t m_msg_counter;
1867039SN/A    int m_priority_rank;
18711021Sjthestness@gmail.com    const bool m_strict_fifo;
18811021Sjthestness@gmail.com    const bool m_randomization;
1899499SN/A
1907973SN/A    int m_input_link_id;
1917973SN/A    int m_vnet_id;
19211796Smatthew.poremba@amd.com
19311796Smatthew.poremba@amd.com    Stats::Average m_buf_msgs;
19411796Smatthew.poremba@amd.com    Stats::Average m_stall_time;
19511796Smatthew.poremba@amd.com    Stats::Scalar m_stall_count;
19611796Smatthew.poremba@amd.com    Stats::Formula m_occupancy;
1976145SN/A};
1986145SN/A
19911111Snilay@cs.wisc.eduTick random_time();
2009554SN/A
2017039SN/Ainline std::ostream&
2027039SN/Aoperator<<(std::ostream& out, const MessageBuffer& obj)
2036145SN/A{
2047039SN/A    obj.print(out);
2057039SN/A    out << std::flush;
2067039SN/A    return out;
2076145SN/A}
2086145SN/A
2097039SN/A#endif // __MEM_RUBY_BUFFERS_MESSAGEBUFFER_HH__
210