base.hh revision 14054
111308Santhony.gutierrez@amd.com/*
212697Santhony.gutierrez@amd.com * Copyright (c) 2012-2013, 2016-2019 ARM Limited
311308Santhony.gutierrez@amd.com * All rights reserved
411308Santhony.gutierrez@amd.com *
511308Santhony.gutierrez@amd.com * The license below extends only to copyright in the software and shall
611308Santhony.gutierrez@amd.com * not be construed as granting a license to any other intellectual
711308Santhony.gutierrez@amd.com * property including but not limited to intellectual property relating
811308Santhony.gutierrez@amd.com * to a hardware implementation of the functionality of the software
911308Santhony.gutierrez@amd.com * licensed hereunder.  You may use the software subject to the license
1011308Santhony.gutierrez@amd.com * terms below provided that you ensure that this notice is replicated
1111308Santhony.gutierrez@amd.com * unmodified and in its entirety in all distributions of the software,
1211308Santhony.gutierrez@amd.com * modified or unmodified, in source code or in binary form.
1311308Santhony.gutierrez@amd.com *
1411308Santhony.gutierrez@amd.com * Redistribution and use in source and binary forms, with or without
1511308Santhony.gutierrez@amd.com * modification, are permitted provided that the following conditions are
1611308Santhony.gutierrez@amd.com * met: redistributions of source code must retain the above copyright
1712697Santhony.gutierrez@amd.com * notice, this list of conditions and the following disclaimer;
1812697Santhony.gutierrez@amd.com * redistributions in binary form must reproduce the above copyright
1912697Santhony.gutierrez@amd.com * notice, this list of conditions and the following disclaimer in the
2011308Santhony.gutierrez@amd.com * documentation and/or other materials provided with the distribution;
2111308Santhony.gutierrez@amd.com * neither the name of the copyright holders nor the names of its
2211308Santhony.gutierrez@amd.com * contributors may be used to endorse or promote products derived from
2311308Santhony.gutierrez@amd.com * this software without specific prior written permission.
2411308Santhony.gutierrez@amd.com *
2511308Santhony.gutierrez@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2611308Santhony.gutierrez@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2711308Santhony.gutierrez@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2811308Santhony.gutierrez@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2911308Santhony.gutierrez@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3011308Santhony.gutierrez@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3111308Santhony.gutierrez@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3211308Santhony.gutierrez@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312697Santhony.gutierrez@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3411308Santhony.gutierrez@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3511308Santhony.gutierrez@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3611308Santhony.gutierrez@amd.com *
3711308Santhony.gutierrez@amd.com * Authors: Thomas Grass
3811308Santhony.gutierrez@amd.com *          Andreas Hansson
3911308Santhony.gutierrez@amd.com *          Sascha Bischoff
4011308Santhony.gutierrez@amd.com */
4111308Santhony.gutierrez@amd.com
4211308Santhony.gutierrez@amd.com#ifndef __CPU_TRAFFIC_GEN_BASE_HH__
4311308Santhony.gutierrez@amd.com#define __CPU_TRAFFIC_GEN_BASE_HH__
4411308Santhony.gutierrez@amd.com
4511308Santhony.gutierrez@amd.com#include <memory>
4611308Santhony.gutierrez@amd.com#include <tuple>
4711308Santhony.gutierrez@amd.com#include <unordered_map>
4811308Santhony.gutierrez@amd.com
4911308Santhony.gutierrez@amd.com#include "base/statistics.hh"
5011308Santhony.gutierrez@amd.com#include "mem/qport.hh"
5111308Santhony.gutierrez@amd.com#include "sim/clocked_object.hh"
5211308Santhony.gutierrez@amd.com
5311308Santhony.gutierrez@amd.comclass BaseGen;
5411308Santhony.gutierrez@amd.comclass StreamGen;
5511308Santhony.gutierrez@amd.comclass System;
5611308Santhony.gutierrez@amd.comstruct BaseTrafficGenParams;
5711308Santhony.gutierrez@amd.com
5811308Santhony.gutierrez@amd.com/**
5911308Santhony.gutierrez@amd.com * The traffic generator is a master module that generates stimuli for
6011308Santhony.gutierrez@amd.com * the memory system, based on a collection of simple generator
6111308Santhony.gutierrez@amd.com * behaviours that are either probabilistic or based on traces. It can
6211308Santhony.gutierrez@amd.com * be used stand alone for creating test cases for interconnect and
6311308Santhony.gutierrez@amd.com * memory controllers, or function as a black box replacement for
6411308Santhony.gutierrez@amd.com * system components that are not yet modelled in detail, e.g. a video
6511308Santhony.gutierrez@amd.com * engine or baseband subsystem.
6611308Santhony.gutierrez@amd.com */
67class BaseTrafficGen : public ClockedObject
68{
69    friend class BaseGen;
70
71  protected: // Params
72    /**
73     * The system used to determine which mode we are currently operating
74     * in.
75     */
76    System *const system;
77
78    /**
79     * Determine whether to add elasticity in the request injection,
80     * thus responding to backpressure by slowing things down.
81     */
82    const bool elasticReq;
83
84    /**
85     * Time to tolerate waiting for retries (not making progress),
86     * until we declare things broken.
87     */
88    const Tick progressCheck;
89
90  private:
91    /**
92     * Receive a retry from the neighbouring port and attempt to
93     * resend the waiting packet.
94     */
95    void recvReqRetry();
96
97    void retryReq();
98
99    bool recvTimingResp(PacketPtr pkt);
100
101    /** Transition to the next generator */
102    void transition();
103
104    /**
105     * Schedule the update event based on nextPacketTick and
106     * nextTransitionTick.
107     */
108    void scheduleUpdate();
109
110    /**
111     * Method to inform the user we have made no progress.
112     */
113    void noProgress();
114
115    /**
116     * Event to keep track of our progress, or lack thereof.
117     */
118    EventFunctionWrapper noProgressEvent;
119
120    /** Time of next transition */
121    Tick nextTransitionTick;
122
123    /** Time of the next packet. */
124    Tick nextPacketTick;
125
126    const int maxOutstandingReqs;
127
128
129    /** Master port specialisation for the traffic generator */
130    class TrafficGenPort : public MasterPort
131    {
132      public:
133
134        TrafficGenPort(const std::string& name, BaseTrafficGen& traffic_gen)
135            : MasterPort(name, &traffic_gen), trafficGen(traffic_gen)
136        { }
137
138      protected:
139
140        void recvReqRetry() { trafficGen.recvReqRetry(); }
141
142        bool recvTimingResp(PacketPtr pkt)
143        { return trafficGen.recvTimingResp(pkt); }
144
145        void recvTimingSnoopReq(PacketPtr pkt) { }
146
147        void recvFunctionalSnoop(PacketPtr pkt) { }
148
149        Tick recvAtomicSnoop(PacketPtr pkt) { return 0; }
150
151      private:
152
153        BaseTrafficGen& trafficGen;
154
155    };
156
157    /**
158     * Schedules event for next update and generates a new packet or
159     * requests a new generatoir depending on the current time.
160     */
161    void update();
162
163    /** The instance of master port used by the traffic generator. */
164    TrafficGenPort port;
165
166    /** Packet waiting to be sent. */
167    PacketPtr retryPkt;
168
169    /** Tick when the stalled packet was meant to be sent. */
170    Tick retryPktTick;
171
172    /** Set when we blocked waiting for outstanding reqs */
173    bool blockedWaitingResp;
174
175    /**
176     * Puts this packet in the waitingResp list and returns true if
177     * we are above the maximum number of oustanding requests.
178     */
179    bool allocateWaitingRespSlot(PacketPtr pkt)
180    {
181        assert(waitingResp.find(pkt->req) == waitingResp.end());
182        assert(pkt->needsResponse());
183
184        waitingResp[pkt->req] = curTick();
185
186        return (maxOutstandingReqs > 0) &&
187               (waitingResp.size() > maxOutstandingReqs);
188    }
189
190    /** Event for scheduling updates */
191    EventFunctionWrapper updateEvent;
192
193    /** Count the number of dropped requests. */
194    Stats::Scalar numSuppressed;
195
196  private: // Stats
197    /** Count the number of generated packets. */
198    Stats::Scalar numPackets;
199
200    /** Count the number of retries. */
201    Stats::Scalar numRetries;
202
203    /** Count the time incurred from back-pressure. */
204    Stats::Scalar retryTicks;
205
206    /** Reqs waiting for response **/
207    std::unordered_map<RequestPtr,Tick> waitingResp;
208
209  public:
210    BaseTrafficGen(const BaseTrafficGenParams* p);
211
212    ~BaseTrafficGen();
213
214    Port &getPort(const std::string &if_name,
215                  PortID idx=InvalidPortID) override;
216
217    void init() override;
218
219    DrainState drain() override;
220
221    void serialize(CheckpointOut &cp) const override;
222    void unserialize(CheckpointIn &cp) override;
223
224    /** Register statistics */
225    void regStats() override;
226
227  public: // Generator factory methods
228    std::shared_ptr<BaseGen> createIdle(Tick duration);
229    std::shared_ptr<BaseGen> createExit(Tick duration);
230
231    std::shared_ptr<BaseGen> createLinear(
232        Tick duration,
233        Addr start_addr, Addr end_addr, Addr blocksize,
234        Tick min_period, Tick max_period,
235        uint8_t read_percent, Addr data_limit);
236
237    std::shared_ptr<BaseGen> createRandom(
238        Tick duration,
239        Addr start_addr, Addr end_addr, Addr blocksize,
240        Tick min_period, Tick max_period,
241        uint8_t read_percent, Addr data_limit);
242
243    std::shared_ptr<BaseGen> createDram(
244        Tick duration,
245        Addr start_addr, Addr end_addr, Addr blocksize,
246        Tick min_period, Tick max_period,
247        uint8_t read_percent, Addr data_limit,
248        unsigned int num_seq_pkts, unsigned int page_size,
249        unsigned int nbr_of_banks_DRAM, unsigned int nbr_of_banks_util,
250        unsigned int addr_mapping,
251        unsigned int nbr_of_ranks);
252
253    std::shared_ptr<BaseGen> createDramRot(
254        Tick duration,
255        Addr start_addr, Addr end_addr, Addr blocksize,
256        Tick min_period, Tick max_period,
257        uint8_t read_percent, Addr data_limit,
258        unsigned int num_seq_pkts, unsigned int page_size,
259        unsigned int nbr_of_banks_DRAM, unsigned int nbr_of_banks_util,
260        unsigned int addr_mapping,
261        unsigned int nbr_of_ranks,
262        unsigned int max_seq_count_per_rank);
263
264    std::shared_ptr<BaseGen> createTrace(
265        Tick duration,
266        const std::string& trace_file, Addr addr_offset);
267
268  protected:
269    void start();
270
271    virtual std::shared_ptr<BaseGen> nextGenerator() = 0;
272
273    /**
274     * MasterID used in generated requests.
275     */
276    const MasterID masterID;
277
278    /** Currently active generator */
279    std::shared_ptr<BaseGen> activeGenerator;
280
281    /** Stream/SubStreamID Generator */
282    std::unique_ptr<StreamGen> streamGenerator;
283};
284
285#endif //__CPU_TRAFFIC_GEN_BASE_HH__
286