base.cc revision 12845:19729e2e70d8
1/*
2 * Copyright (c) 2012-2013, 2016-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 * Authors: Thomas Grass
38 *          Andreas Hansson
39 *          Sascha Bischoff
40 */
41#include "cpu/testers/traffic_gen/base.hh"
42
43#include <sstream>
44
45#include "base/intmath.hh"
46#include "base/random.hh"
47#include "config/have_protobuf.hh"
48#include "cpu/testers/traffic_gen/base_gen.hh"
49#include "cpu/testers/traffic_gen/dram_gen.hh"
50#include "cpu/testers/traffic_gen/dram_rot_gen.hh"
51#include "cpu/testers/traffic_gen/exit_gen.hh"
52#include "cpu/testers/traffic_gen/idle_gen.hh"
53#include "cpu/testers/traffic_gen/linear_gen.hh"
54#include "cpu/testers/traffic_gen/random_gen.hh"
55#include "debug/Checkpoint.hh"
56#include "debug/TrafficGen.hh"
57#include "params/BaseTrafficGen.hh"
58#include "sim/sim_exit.hh"
59#include "sim/stats.hh"
60#include "sim/system.hh"
61
62#if HAVE_PROTOBUF
63#include "cpu/testers/traffic_gen/trace_gen.hh"
64#endif
65
66
67using namespace std;
68
69BaseTrafficGen::BaseTrafficGen(const BaseTrafficGenParams* p)
70    : MemObject(p),
71      system(p->system),
72      elasticReq(p->elastic_req),
73      progressCheck(p->progress_check),
74      noProgressEvent([this]{ noProgress(); }, name()),
75      nextTransitionTick(0),
76      nextPacketTick(0),
77      port(name() + ".port", *this),
78      retryPkt(NULL),
79      retryPktTick(0),
80      updateEvent([this]{ update(); }, name()),
81      numSuppressed(0),
82      masterID(system->getMasterId(this))
83{
84}
85
86BaseMasterPort&
87BaseTrafficGen::getMasterPort(const string& if_name, PortID idx)
88{
89    if (if_name == "port") {
90        return port;
91    } else {
92        return MemObject::getMasterPort(if_name, idx);
93    }
94}
95
96void
97BaseTrafficGen::init()
98{
99    MemObject::init();
100
101    if (!port.isConnected())
102        fatal("The port of %s is not connected!\n", name());
103}
104
105DrainState
106BaseTrafficGen::drain()
107{
108    if (!updateEvent.scheduled()) {
109        // no event has been scheduled yet (e.g. switched from atomic mode)
110        return DrainState::Drained;
111    }
112
113    if (retryPkt == NULL) {
114        // shut things down
115        nextPacketTick = MaxTick;
116        nextTransitionTick = MaxTick;
117        deschedule(updateEvent);
118        return DrainState::Drained;
119    } else {
120        return DrainState::Draining;
121    }
122}
123
124void
125BaseTrafficGen::serialize(CheckpointOut &cp) const
126{
127    warn("%s serialization does not keep all traffic generator"
128         " internal state\n", name());
129
130    DPRINTF(Checkpoint, "Serializing BaseTrafficGen\n");
131
132    // save ticks of the graph event if it is scheduled
133    Tick nextEvent = updateEvent.scheduled() ? updateEvent.when() : 0;
134
135    DPRINTF(TrafficGen, "Saving nextEvent=%llu\n", nextEvent);
136
137    SERIALIZE_SCALAR(nextEvent);
138
139    SERIALIZE_SCALAR(nextTransitionTick);
140
141    SERIALIZE_SCALAR(nextPacketTick);
142}
143
144void
145BaseTrafficGen::unserialize(CheckpointIn &cp)
146{
147    warn("%s serialization does not restore all traffic generator"
148         " internal state\n", name());
149
150    // restore scheduled events
151    Tick nextEvent;
152    UNSERIALIZE_SCALAR(nextEvent);
153    if (nextEvent != 0)
154        schedule(updateEvent, nextEvent);
155
156    UNSERIALIZE_SCALAR(nextTransitionTick);
157
158    UNSERIALIZE_SCALAR(nextPacketTick);
159}
160
161void
162BaseTrafficGen::update()
163{
164    // shift our progress-tracking event forward
165    reschedule(noProgressEvent, curTick() + progressCheck, true);
166
167    // if we have reached the time for the next state transition, then
168    // perform the transition
169    if (curTick() >= nextTransitionTick) {
170        transition();
171    } else {
172        assert(curTick() >= nextPacketTick);
173        // get the next packet and try to send it
174        PacketPtr pkt = activeGenerator->getNextPacket();
175
176        // suppress packets that are not destined for a memory, such as
177        // device accesses that could be part of a trace
178        if (pkt && system->isMemAddr(pkt->getAddr())) {
179            numPackets++;
180            if (!port.sendTimingReq(pkt)) {
181                retryPkt = pkt;
182                retryPktTick = curTick();
183            }
184        } else if (pkt) {
185            DPRINTF(TrafficGen, "Suppressed packet %s 0x%x\n",
186                    pkt->cmdString(), pkt->getAddr());
187
188            ++numSuppressed;
189            if (numSuppressed % 10000)
190                warn("%s suppressed %d packets with non-memory addresses\n",
191                     name(), numSuppressed);
192
193            delete pkt;
194            pkt = nullptr;
195        }
196    }
197
198    // if we are waiting for a retry, do not schedule any further
199    // events, in the case of a transition or a successful send, go
200    // ahead and determine when the next update should take place
201    if (retryPkt == NULL) {
202        nextPacketTick = activeGenerator->nextPacketTick(elasticReq, 0);
203        scheduleUpdate();
204    }
205}
206
207void
208BaseTrafficGen::transition()
209{
210    if (activeGenerator)
211        activeGenerator->exit();
212
213    activeGenerator = nextGenerator();
214
215    if (activeGenerator) {
216        const Tick duration = activeGenerator->duration;
217        if (duration != MaxTick && duration != 0) {
218            // we could have been delayed and not transitioned on the
219            // exact tick when we were supposed to (due to back
220            // pressure when sending a packet)
221            nextTransitionTick = curTick() + duration;
222        } else {
223            nextTransitionTick = MaxTick;
224        }
225
226        activeGenerator->enter();
227        nextPacketTick = activeGenerator->nextPacketTick(elasticReq, 0);
228    } else {
229        nextPacketTick = MaxTick;
230        nextTransitionTick = MaxTick;
231        assert(!updateEvent.scheduled());
232    }
233}
234
235void
236BaseTrafficGen::scheduleUpdate()
237{
238    // Has the generator run out of work? In that case, force a
239    // transition if a transition period hasn't been configured.
240    while (activeGenerator &&
241           nextPacketTick == MaxTick && nextTransitionTick == MaxTick) {
242        transition();
243    }
244
245    if (!activeGenerator)
246        return;
247
248    // schedule next update event based on either the next execute
249    // tick or the next transition, which ever comes first
250    const Tick nextEventTick = std::min(nextPacketTick, nextTransitionTick);
251
252    DPRINTF(TrafficGen, "Next event scheduled at %lld\n", nextEventTick);
253
254    // The next transition tick may be in the past if there was a
255    // retry, so ensure that we don't schedule anything in the past.
256    schedule(updateEvent, std::max(curTick(), nextEventTick));
257}
258
259void
260BaseTrafficGen::start()
261{
262    transition();
263    scheduleUpdate();
264}
265
266void
267BaseTrafficGen::recvReqRetry()
268{
269    assert(retryPkt != NULL);
270
271    DPRINTF(TrafficGen, "Received retry\n");
272    numRetries++;
273    // attempt to send the packet, and if we are successful start up
274    // the machinery again
275    if (port.sendTimingReq(retryPkt)) {
276        retryPkt = NULL;
277        // remember how much delay was incurred due to back-pressure
278        // when sending the request, we also use this to derive
279        // the tick for the next packet
280        Tick delay = curTick() - retryPktTick;
281        retryPktTick = 0;
282        retryTicks += delay;
283
284        if (drainState() != DrainState::Draining) {
285            // packet is sent, so find out when the next one is due
286            nextPacketTick = activeGenerator->nextPacketTick(elasticReq,
287                                                             delay);
288            scheduleUpdate();
289        } else {
290            // shut things down
291            nextPacketTick = MaxTick;
292            nextTransitionTick = MaxTick;
293            signalDrainDone();
294        }
295    }
296}
297
298void
299BaseTrafficGen::noProgress()
300{
301    fatal("BaseTrafficGen %s spent %llu ticks without making progress",
302          name(), progressCheck);
303}
304
305void
306BaseTrafficGen::regStats()
307{
308    ClockedObject::regStats();
309
310    // Initialise all the stats
311    using namespace Stats;
312
313    numPackets
314        .name(name() + ".numPackets")
315        .desc("Number of packets generated");
316
317    numRetries
318        .name(name() + ".numRetries")
319        .desc("Number of retries");
320
321    retryTicks
322        .name(name() + ".retryTicks")
323        .desc("Time spent waiting due to back-pressure (ticks)");
324}
325
326std::shared_ptr<BaseGen>
327BaseTrafficGen::createIdle(Tick duration)
328{
329    return std::shared_ptr<BaseGen>(new IdleGen(*this, masterID, duration));
330}
331
332std::shared_ptr<BaseGen>
333BaseTrafficGen::createExit(Tick duration)
334{
335    return std::shared_ptr<BaseGen>(new ExitGen(*this, masterID, duration));
336}
337
338std::shared_ptr<BaseGen>
339BaseTrafficGen::createLinear(Tick duration,
340                             Addr start_addr, Addr end_addr, Addr blocksize,
341                             Tick min_period, Tick max_period,
342                             uint8_t read_percent, Addr data_limit)
343{
344    return std::shared_ptr<BaseGen>(new LinearGen(*this, masterID,
345                                                  duration, start_addr,
346                                                  end_addr, blocksize,
347                                                  system->cacheLineSize(),
348                                                  min_period, max_period,
349                                                  read_percent, data_limit));
350}
351
352std::shared_ptr<BaseGen>
353BaseTrafficGen::createRandom(Tick duration,
354                             Addr start_addr, Addr end_addr, Addr blocksize,
355                             Tick min_period, Tick max_period,
356                             uint8_t read_percent, Addr data_limit)
357{
358    return std::shared_ptr<BaseGen>(new RandomGen(*this, masterID,
359                                                  duration, start_addr,
360                                                  end_addr, blocksize,
361                                                  system->cacheLineSize(),
362                                                  min_period, max_period,
363                                                  read_percent, data_limit));
364}
365
366std::shared_ptr<BaseGen>
367BaseTrafficGen::createDram(Tick duration,
368                           Addr start_addr, Addr end_addr, Addr blocksize,
369                           Tick min_period, Tick max_period,
370                           uint8_t read_percent, Addr data_limit,
371                           unsigned int num_seq_pkts, unsigned int page_size,
372                           unsigned int nbr_of_banks_DRAM,
373                           unsigned int nbr_of_banks_util,
374                           unsigned int addr_mapping,
375                           unsigned int nbr_of_ranks)
376{
377    return std::shared_ptr<BaseGen>(new DramGen(*this, masterID,
378                                                duration, start_addr,
379                                                end_addr, blocksize,
380                                                system->cacheLineSize(),
381                                                min_period, max_period,
382                                                read_percent, data_limit,
383                                                num_seq_pkts, page_size,
384                                                nbr_of_banks_DRAM,
385                                                nbr_of_banks_util,
386                                                addr_mapping,
387                                                nbr_of_ranks));
388}
389
390std::shared_ptr<BaseGen>
391BaseTrafficGen::createDramRot(Tick duration,
392                              Addr start_addr, Addr end_addr, Addr blocksize,
393                              Tick min_period, Tick max_period,
394                              uint8_t read_percent, Addr data_limit,
395                              unsigned int num_seq_pkts,
396                              unsigned int page_size,
397                              unsigned int nbr_of_banks_DRAM,
398                              unsigned int nbr_of_banks_util,
399                              unsigned int addr_mapping,
400                              unsigned int nbr_of_ranks,
401                              unsigned int max_seq_count_per_rank)
402{
403    return std::shared_ptr<BaseGen>(new DramRotGen(*this, masterID,
404                                                   duration, start_addr,
405                                                   end_addr, blocksize,
406                                                   system->cacheLineSize(),
407                                                   min_period, max_period,
408                                                   read_percent, data_limit,
409                                                   num_seq_pkts, page_size,
410                                                   nbr_of_banks_DRAM,
411                                                   nbr_of_banks_util,
412                                                   addr_mapping,
413                                                   nbr_of_ranks,
414                                                   max_seq_count_per_rank));
415}
416
417std::shared_ptr<BaseGen>
418BaseTrafficGen::createTrace(Tick duration,
419                            const std::string& trace_file, Addr addr_offset)
420{
421#if HAVE_PROTOBUF
422    return std::shared_ptr<BaseGen>(
423        new TraceGen(*this, masterID, duration, trace_file, addr_offset));
424#else
425    panic("Can't instantiate trace generation without Protobuf support!\n");
426#endif
427}
428
429bool
430BaseTrafficGen::TrafficGenPort::recvTimingResp(PacketPtr pkt)
431{
432    delete pkt;
433
434    return true;
435}
436