q_policy.hh revision 12966:3b20a7f755d5
1/*
2 * Copyright (c) 2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Author: Matteo Andreozzi
38 */
39
40#ifndef __MEM_QOS_Q_POLICY_HH__
41#define __MEM_QOS_Q_POLICY_HH__
42
43#include <deque>
44#include <unordered_set>
45
46#include "mem/packet.hh"
47#include "params/QoSMemCtrl.hh"
48
49namespace QoS {
50
51class MemCtrl;
52
53/**
54 * QoS Queue Policy
55 *
56 * The QoS Queue Policy class implements algorithms to schedule packets
57 * within the same QoS priority queue
58 */
59class QueuePolicy
60{
61  public:
62    typedef std::deque<PacketPtr> PacketQueue;
63
64    /**
65     * This factory method is used for generating the queue policy. It takes
66     * the memory controller params as an argument and returns the related
67     * QueuePolicy object.  If no particular QueuePolicy has been specified in
68     * the QoSMemCtrlParams, the method will default to a LIFO queue policy.
69     *
70     * @param p QoS::MemCtrl parameter variable
71     * @return Pointer to the QueuePolicy
72     */
73    static QueuePolicy* create(const QoSMemCtrlParams* p);
74
75    /**
76     * This method is called by the memory controller after it enqueues a
77     * packet. It is a way for the queue policy to hook into the packet
78     * enqueuing and update relevant metadata. This will then be used once
79     * the QueuePolicy::selectPacket will be called.
80     *
81     * @param pkt Enqueued packet
82     */
83    virtual void enqueuePacket(PacketPtr pkt) {};
84
85    /**
86     * Policy selector.
87     * The implementation of this virtual method selects the packet
88     * to be serviced from the packet queue passed as an argument.
89     *
90     * @param queue Packet queue
91     * @return Iterator pointing to the packet in the queue to be
92     *         serviced
93     */
94    virtual PacketQueue::iterator selectPacket(PacketQueue* queue) = 0;
95
96    /**
97     * Setting a pointer to the Memory Controller implementing
98     * the policy.
99     */
100    void setMemCtrl(MemCtrl* mem) { memCtrl = mem; };
101
102  protected:
103    QueuePolicy(const QoSMemCtrlParams* p)
104      : memCtrl(nullptr)
105    {}
106
107    /** Pointer to parent memory controller implementing the policy */
108    MemCtrl* memCtrl;
109};
110
111/** Last In First Out Queue Policy */
112class LifoQueuePolicy : public QueuePolicy
113{
114  public:
115    LifoQueuePolicy(const QoSMemCtrlParams* p)
116      : QueuePolicy(p)
117    {}
118
119    /**
120     * Implements LIFO packet select policy
121     *
122     * @param queue The queue in which to select a packet
123     * @return Iterator to the selected packet
124     */
125    PacketQueue::iterator
126    selectPacket(PacketQueue* queue) override
127    {
128        return queue->end();
129    }
130};
131
132/** First In First Out Queue Policy */
133class FifoQueuePolicy : public QueuePolicy
134{
135  public:
136    FifoQueuePolicy(const QoSMemCtrlParams* p)
137      : QueuePolicy(p)
138    {}
139
140    /**
141     * Implements FCFS packet select policy
142     *
143     * @param queue The queue in which to select a packet
144     * @return Iterator to the selected packet
145     */
146    PacketQueue::iterator
147    selectPacket(PacketQueue* queue) override
148    {
149        return queue->begin();
150    }
151};
152
153/**
154 * Least Recently Granted Queue Policy
155 * It selects packets from the queue with a round
156 * robin-like policy: using the master id as a switching
157 * parameter rather than switching over a time quantum.
158 */
159class LrgQueuePolicy : public QueuePolicy
160{
161  public:
162    LrgQueuePolicy(const QoSMemCtrlParams* p)
163      : QueuePolicy(p)
164    {}
165
166    void enqueuePacket(PacketPtr pkt) override;
167
168    /**
169     * Implements LRG packet select policy
170     *
171     * @param queue The queue in which to select a packet
172     * @return Iterator to the selected packet
173     */
174    PacketQueue::iterator
175    selectPacket(PacketQueue* queue) override;
176
177  protected:
178    /**
179     * Support structure for lrg algorithms:
180     * keeps track of serviced masters,
181     * always serve the front element.
182     */
183    std::list<MasterID> toServe;
184};
185
186} // namespace QoS
187
188#endif /* __MEM_QOS_Q_POLICY_HH__ */
189