1/*
2 * Copyright (c) 2016 Georgia Institute of Technology
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Tushar Krishna
29 */
30
31#include "cpu/testers/garnet_synthetic_traffic/GarnetSyntheticTraffic.hh"
32
33#include <cmath>
34#include <iomanip>
35#include <set>
36#include <string>
37#include <vector>
38
39#include "base/logging.hh"
40#include "base/random.hh"
41#include "base/statistics.hh"
42#include "debug/GarnetSyntheticTraffic.hh"
43#include "mem/packet.hh"
44#include "mem/port.hh"
45#include "mem/request.hh"
46#include "sim/sim_events.hh"
47#include "sim/stats.hh"
48#include "sim/system.hh"
49
50using namespace std;
51
52int TESTER_NETWORK=0;
53
54bool
55GarnetSyntheticTraffic::CpuPort::recvTimingResp(PacketPtr pkt)
56{
57    tester->completeRequest(pkt);
58    return true;
59}
60
61void
62GarnetSyntheticTraffic::CpuPort::recvReqRetry()
63{
64    tester->doRetry();
65}
66
67void
68GarnetSyntheticTraffic::sendPkt(PacketPtr pkt)
69{
70    if (!cachePort.sendTimingReq(pkt)) {
71        retryPkt = pkt; // RubyPort will retry sending
72    }
73    numPacketsSent++;
74}
75
76GarnetSyntheticTraffic::GarnetSyntheticTraffic(const Params *p)
77    : ClockedObject(p),
78      tickEvent([this]{ tick(); }, "GarnetSyntheticTraffic tick",
79                false, Event::CPU_Tick_Pri),
80      cachePort("GarnetSyntheticTraffic", this),
81      retryPkt(NULL),
82      size(p->memory_size),
83      blockSizeBits(p->block_offset),
84      numDestinations(p->num_dest),
85      simCycles(p->sim_cycles),
86      numPacketsMax(p->num_packets_max),
87      numPacketsSent(0),
88      singleSender(p->single_sender),
89      singleDest(p->single_dest),
90      trafficType(p->traffic_type),
91      injRate(p->inj_rate),
92      injVnet(p->inj_vnet),
93      precision(p->precision),
94      responseLimit(p->response_limit),
95      masterId(p->system->getMasterId(this))
96{
97    // set up counters
98    noResponseCycles = 0;
99    schedule(tickEvent, 0);
100
101    initTrafficType();
102    if (trafficStringToEnum.count(trafficType) == 0) {
103        fatal("Unknown Traffic Type: %s!\n", traffic);
104    }
105    traffic = trafficStringToEnum[trafficType];
106
107    id = TESTER_NETWORK++;
108    DPRINTF(GarnetSyntheticTraffic,"Config Created: Name = %s , and id = %d\n",
109            name(), id);
110}
111
112Port &
113GarnetSyntheticTraffic::getPort(const std::string &if_name, PortID idx)
114{
115    if (if_name == "test")
116        return cachePort;
117    else
118        return ClockedObject::getPort(if_name, idx);
119}
120
121void
122GarnetSyntheticTraffic::init()
123{
124    numPacketsSent = 0;
125}
126
127
128void
129GarnetSyntheticTraffic::completeRequest(PacketPtr pkt)
130{
131    DPRINTF(GarnetSyntheticTraffic,
132            "Completed injection of %s packet for address %x\n",
133            pkt->isWrite() ? "write" : "read\n",
134            pkt->req->getPaddr());
135
136    assert(pkt->isResponse());
137    noResponseCycles = 0;
138    delete pkt;
139}
140
141
142void
143GarnetSyntheticTraffic::tick()
144{
145    if (++noResponseCycles >= responseLimit) {
146        fatal("%s deadlocked at cycle %d\n", name(), curTick());
147    }
148
149    // make new request based on injection rate
150    // (injection rate's range depends on precision)
151    // - generate a random number between 0 and 10^precision
152    // - send pkt if this number is < injRate*(10^precision)
153    bool sendAllowedThisCycle;
154    double injRange = pow((double) 10, (double) precision);
155    unsigned trySending = random_mt.random<unsigned>(0, (int) injRange);
156    if (trySending < injRate*injRange)
157        sendAllowedThisCycle = true;
158    else
159        sendAllowedThisCycle = false;
160
161    // always generatePkt unless fixedPkts or singleSender is enabled
162    if (sendAllowedThisCycle) {
163        bool senderEnable = true;
164
165        if (numPacketsMax >= 0 && numPacketsSent >= numPacketsMax)
166            senderEnable = false;
167
168        if (singleSender >= 0 && id != singleSender)
169            senderEnable = false;
170
171        if (senderEnable)
172            generatePkt();
173    }
174
175    // Schedule wakeup
176    if (curTick() >= simCycles)
177        exitSimLoop("Network Tester completed simCycles");
178    else {
179        if (!tickEvent.scheduled())
180            schedule(tickEvent, clockEdge(Cycles(1)));
181    }
182}
183
184void
185GarnetSyntheticTraffic::generatePkt()
186{
187    int num_destinations = numDestinations;
188    int radix = (int) sqrt(num_destinations);
189    unsigned destination = id;
190    int dest_x = -1;
191    int dest_y = -1;
192    int source = id;
193    int src_x = id%radix;
194    int src_y = id/radix;
195
196    if (singleDest >= 0)
197    {
198        destination = singleDest;
199    } else if (traffic == UNIFORM_RANDOM_) {
200        destination = random_mt.random<unsigned>(0, num_destinations - 1);
201    } else if (traffic == BIT_COMPLEMENT_) {
202        dest_x = radix - src_x - 1;
203        dest_y = radix - src_y - 1;
204        destination = dest_y*radix + dest_x;
205    } else if (traffic == BIT_REVERSE_) {
206        unsigned int straight = source;
207        unsigned int reverse = source & 1; // LSB
208
209        int num_bits = (int) log2(num_destinations);
210
211        for (int i = 1; i < num_bits; i++)
212        {
213            reverse <<= 1;
214            straight >>= 1;
215            reverse |= (straight & 1); // LSB
216        }
217        destination = reverse;
218    } else if (traffic == BIT_ROTATION_) {
219        if (source%2 == 0)
220            destination = source/2;
221        else // (source%2 == 1)
222            destination = ((source/2) + (num_destinations/2));
223    } else if (traffic == NEIGHBOR_) {
224            dest_x = (src_x + 1) % radix;
225            dest_y = src_y;
226            destination = dest_y*radix + dest_x;
227    } else if (traffic == SHUFFLE_) {
228        if (source < num_destinations/2)
229            destination = source*2;
230        else
231            destination = (source*2 - num_destinations + 1);
232    } else if (traffic == TRANSPOSE_) {
233            dest_x = src_y;
234            dest_y = src_x;
235            destination = dest_y*radix + dest_x;
236    } else if (traffic == TORNADO_) {
237        dest_x = (src_x + (int) ceil(radix/2) - 1) % radix;
238        dest_y = src_y;
239        destination = dest_y*radix + dest_x;
240    }
241    else {
242        fatal("Unknown Traffic Type: %s!\n", traffic);
243    }
244
245    // The source of the packets is a cache.
246    // The destination of the packets is a directory.
247    // The destination bits are embedded in the address after byte-offset.
248    Addr paddr =  destination;
249    paddr <<= blockSizeBits;
250    unsigned access_size = 1; // Does not affect Ruby simulation
251
252    // Modeling different coherence msg types over different msg classes.
253    //
254    // GarnetSyntheticTraffic assumes the Garnet_standalone coherence protocol
255    // which models three message classes/virtual networks.
256    // These are: request, forward, response.
257    // requests and forwards are "control" packets (typically 8 bytes),
258    // while responses are "data" packets (typically 72 bytes).
259    //
260    // Life of a packet from the tester into the network:
261    // (1) This function generatePkt() generates packets of one of the
262    //     following 3 types (randomly) : ReadReq, INST_FETCH, WriteReq
263    // (2) mem/ruby/system/RubyPort.cc converts these to RubyRequestType_LD,
264    //     RubyRequestType_IFETCH, RubyRequestType_ST respectively
265    // (3) mem/ruby/system/Sequencer.cc sends these to the cache controllers
266    //     in the coherence protocol.
267    // (4) Network_test-cache.sm tags RubyRequestType:LD,
268    //     RubyRequestType:IFETCH and RubyRequestType:ST as
269    //     Request, Forward, and Response events respectively;
270    //     and injects them into virtual networks 0, 1 and 2 respectively.
271    //     It immediately calls back the sequencer.
272    // (5) The packet traverses the network (simple/garnet) and reaches its
273    //     destination (Directory), and network stats are updated.
274    // (6) Network_test-dir.sm simply drops the packet.
275    //
276    MemCmd::Command requestType;
277
278    RequestPtr req = nullptr;
279    Request::Flags flags;
280
281    // Inject in specific Vnet
282    // Vnet 0 and 1 are for control packets (1-flit)
283    // Vnet 2 is for data packets (5-flit)
284    int injReqType = injVnet;
285
286    if (injReqType < 0 || injReqType > 2)
287    {
288        // randomly inject in any vnet
289        injReqType = random_mt.random(0, 2);
290    }
291
292    if (injReqType == 0) {
293        // generate packet for virtual network 0
294        requestType = MemCmd::ReadReq;
295        req = std::make_shared<Request>(paddr, access_size, flags, masterId);
296    } else if (injReqType == 1) {
297        // generate packet for virtual network 1
298        requestType = MemCmd::ReadReq;
299        flags.set(Request::INST_FETCH);
300        req = std::make_shared<Request>(
301            0, 0x0, access_size, flags, masterId, 0x0, 0);
302        req->setPaddr(paddr);
303    } else {  // if (injReqType == 2)
304        // generate packet for virtual network 2
305        requestType = MemCmd::WriteReq;
306        req = std::make_shared<Request>(paddr, access_size, flags, masterId);
307    }
308
309    req->setContext(id);
310
311    //No need to do functional simulation
312    //We just do timing simulation of the network
313
314    DPRINTF(GarnetSyntheticTraffic,
315            "Generated packet with destination %d, embedded in address %x\n",
316            destination, req->getPaddr());
317
318    PacketPtr pkt = new Packet(req, requestType);
319    pkt->dataDynamic(new uint8_t[req->getSize()]);
320    pkt->senderState = NULL;
321
322    sendPkt(pkt);
323}
324
325void
326GarnetSyntheticTraffic::initTrafficType()
327{
328    trafficStringToEnum["bit_complement"] = BIT_COMPLEMENT_;
329    trafficStringToEnum["bit_reverse"] = BIT_REVERSE_;
330    trafficStringToEnum["bit_rotation"] = BIT_ROTATION_;
331    trafficStringToEnum["neighbor"] = NEIGHBOR_;
332    trafficStringToEnum["shuffle"] = SHUFFLE_;
333    trafficStringToEnum["tornado"] = TORNADO_;
334    trafficStringToEnum["transpose"] = TRANSPOSE_;
335    trafficStringToEnum["uniform_random"] = UNIFORM_RANDOM_;
336}
337
338void
339GarnetSyntheticTraffic::doRetry()
340{
341    if (cachePort.sendTimingReq(retryPkt)) {
342        retryPkt = NULL;
343    }
344}
345
346void
347GarnetSyntheticTraffic::printAddr(Addr a)
348{
349    cachePort.printAddr(a);
350}
351
352
353GarnetSyntheticTraffic *
354GarnetSyntheticTrafficParams::create()
355{
356    return new GarnetSyntheticTraffic(this);
357}
358