GarnetSyntheticTraffic.cc revision 12749
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/mem_object.hh"
44#include "mem/packet.hh"
45#include "mem/port.hh"
46#include "mem/request.hh"
47#include "sim/sim_events.hh"
48#include "sim/stats.hh"
49#include "sim/system.hh"
50
51using namespace std;
52
53int TESTER_NETWORK=0;
54
55bool
56GarnetSyntheticTraffic::CpuPort::recvTimingResp(PacketPtr pkt)
57{
58    tester->completeRequest(pkt);
59    return true;
60}
61
62void
63GarnetSyntheticTraffic::CpuPort::recvReqRetry()
64{
65    tester->doRetry();
66}
67
68void
69GarnetSyntheticTraffic::sendPkt(PacketPtr pkt)
70{
71    if (!cachePort.sendTimingReq(pkt)) {
72        retryPkt = pkt; // RubyPort will retry sending
73    }
74    numPacketsSent++;
75}
76
77GarnetSyntheticTraffic::GarnetSyntheticTraffic(const Params *p)
78    : MemObject(p),
79      tickEvent([this]{ tick(); }, "GarnetSyntheticTraffic tick",
80                false, Event::CPU_Tick_Pri),
81      cachePort("GarnetSyntheticTraffic", this),
82      retryPkt(NULL),
83      size(p->memory_size),
84      blockSizeBits(p->block_offset),
85      numDestinations(p->num_dest),
86      simCycles(p->sim_cycles),
87      numPacketsMax(p->num_packets_max),
88      numPacketsSent(0),
89      singleSender(p->single_sender),
90      singleDest(p->single_dest),
91      trafficType(p->traffic_type),
92      injRate(p->inj_rate),
93      injVnet(p->inj_vnet),
94      precision(p->precision),
95      responseLimit(p->response_limit),
96      masterId(p->system->getMasterId(this))
97{
98    // set up counters
99    noResponseCycles = 0;
100    schedule(tickEvent, 0);
101
102    initTrafficType();
103    if (trafficStringToEnum.count(trafficType) == 0) {
104        fatal("Unknown Traffic Type: %s!\n", traffic);
105    }
106    traffic = trafficStringToEnum[trafficType];
107
108    id = TESTER_NETWORK++;
109    DPRINTF(GarnetSyntheticTraffic,"Config Created: Name = %s , and id = %d\n",
110            name(), id);
111}
112
113BaseMasterPort &
114GarnetSyntheticTraffic::getMasterPort(const std::string &if_name, PortID idx)
115{
116    if (if_name == "test")
117        return cachePort;
118    else
119        return MemObject::getMasterPort(if_name, idx);
120}
121
122void
123GarnetSyntheticTraffic::init()
124{
125    numPacketsSent = 0;
126}
127
128
129void
130GarnetSyntheticTraffic::completeRequest(PacketPtr pkt)
131{
132    DPRINTF(GarnetSyntheticTraffic,
133            "Completed injection of %s packet for address %x\n",
134            pkt->isWrite() ? "write" : "read\n",
135            pkt->req->getPaddr());
136
137    assert(pkt->isResponse());
138    noResponseCycles = 0;
139    delete pkt;
140}
141
142
143void
144GarnetSyntheticTraffic::tick()
145{
146    if (++noResponseCycles >= responseLimit) {
147        fatal("%s deadlocked at cycle %d\n", name(), curTick());
148    }
149
150    // make new request based on injection rate
151    // (injection rate's range depends on precision)
152    // - generate a random number between 0 and 10^precision
153    // - send pkt if this number is < injRate*(10^precision)
154    bool sendAllowedThisCycle;
155    double injRange = pow((double) 10, (double) precision);
156    unsigned trySending = random_mt.random<unsigned>(0, (int) injRange);
157    if (trySending < injRate*injRange)
158        sendAllowedThisCycle = true;
159    else
160        sendAllowedThisCycle = false;
161
162    // always generatePkt unless fixedPkts or singleSender is enabled
163    if (sendAllowedThisCycle) {
164        bool senderEnable = true;
165
166        if (numPacketsMax >= 0 && numPacketsSent >= numPacketsMax)
167            senderEnable = false;
168
169        if (singleSender >= 0 && id != singleSender)
170            senderEnable = false;
171
172        if (senderEnable)
173            generatePkt();
174    }
175
176    // Schedule wakeup
177    if (curTick() >= simCycles)
178        exitSimLoop("Network Tester completed simCycles");
179    else {
180        if (!tickEvent.scheduled())
181            schedule(tickEvent, clockEdge(Cycles(1)));
182    }
183}
184
185void
186GarnetSyntheticTraffic::generatePkt()
187{
188    int num_destinations = numDestinations;
189    int radix = (int) sqrt(num_destinations);
190    unsigned destination = id;
191    int dest_x = -1;
192    int dest_y = -1;
193    int source = id;
194    int src_x = id%radix;
195    int src_y = id/radix;
196
197    if (singleDest >= 0)
198    {
199        destination = singleDest;
200    } else if (traffic == UNIFORM_RANDOM_) {
201        destination = random_mt.random<unsigned>(0, num_destinations - 1);
202    } else if (traffic == BIT_COMPLEMENT_) {
203        dest_x = radix - src_x - 1;
204        dest_y = radix - src_y - 1;
205        destination = dest_y*radix + dest_x;
206    } else if (traffic == BIT_REVERSE_) {
207        unsigned int straight = source;
208        unsigned int reverse = source & 1; // LSB
209
210        int num_bits = (int) log2(num_destinations);
211
212        for (int i = 1; i < num_bits; i++)
213        {
214            reverse <<= 1;
215            straight >>= 1;
216            reverse |= (straight & 1); // LSB
217        }
218        destination = reverse;
219    } else if (traffic == BIT_ROTATION_) {
220        if (source%2 == 0)
221            destination = source/2;
222        else // (source%2 == 1)
223            destination = ((source/2) + (num_destinations/2));
224    } else if (traffic == NEIGHBOR_) {
225            dest_x = (src_x + 1) % radix;
226            dest_y = src_y;
227            destination = dest_y*radix + dest_x;
228    } else if (traffic == SHUFFLE_) {
229        if (source < num_destinations/2)
230            destination = source*2;
231        else
232            destination = (source*2 - num_destinations + 1);
233    } else if (traffic == TRANSPOSE_) {
234            dest_x = src_y;
235            dest_y = src_x;
236            destination = dest_y*radix + dest_x;
237    } else if (traffic == TORNADO_) {
238        dest_x = (src_x + (int) ceil(radix/2) - 1) % radix;
239        dest_y = src_y;
240        destination = dest_y*radix + dest_x;
241    }
242    else {
243        fatal("Unknown Traffic Type: %s!\n", traffic);
244    }
245
246    // The source of the packets is a cache.
247    // The destination of the packets is a directory.
248    // The destination bits are embedded in the address after byte-offset.
249    Addr paddr =  destination;
250    paddr <<= blockSizeBits;
251    unsigned access_size = 1; // Does not affect Ruby simulation
252
253    // Modeling different coherence msg types over different msg classes.
254    //
255    // GarnetSyntheticTraffic assumes the Garnet_standalone coherence protocol
256    // which models three message classes/virtual networks.
257    // These are: request, forward, response.
258    // requests and forwards are "control" packets (typically 8 bytes),
259    // while responses are "data" packets (typically 72 bytes).
260    //
261    // Life of a packet from the tester into the network:
262    // (1) This function generatePkt() generates packets of one of the
263    //     following 3 types (randomly) : ReadReq, INST_FETCH, WriteReq
264    // (2) mem/ruby/system/RubyPort.cc converts these to RubyRequestType_LD,
265    //     RubyRequestType_IFETCH, RubyRequestType_ST respectively
266    // (3) mem/ruby/system/Sequencer.cc sends these to the cache controllers
267    //     in the coherence protocol.
268    // (4) Network_test-cache.sm tags RubyRequestType:LD,
269    //     RubyRequestType:IFETCH and RubyRequestType:ST as
270    //     Request, Forward, and Response events respectively;
271    //     and injects them into virtual networks 0, 1 and 2 respectively.
272    //     It immediately calls back the sequencer.
273    // (5) The packet traverses the network (simple/garnet) and reaches its
274    //     destination (Directory), and network stats are updated.
275    // (6) Network_test-dir.sm simply drops the packet.
276    //
277    MemCmd::Command requestType;
278
279    RequestPtr req = nullptr;
280    Request::Flags flags;
281
282    // Inject in specific Vnet
283    // Vnet 0 and 1 are for control packets (1-flit)
284    // Vnet 2 is for data packets (5-flit)
285    int injReqType = injVnet;
286
287    if (injReqType < 0 || injReqType > 2)
288    {
289        // randomly inject in any vnet
290        injReqType = random_mt.random(0, 2);
291    }
292
293    if (injReqType == 0) {
294        // generate packet for virtual network 0
295        requestType = MemCmd::ReadReq;
296        req = std::make_shared<Request>(paddr, access_size, flags, masterId);
297    } else if (injReqType == 1) {
298        // generate packet for virtual network 1
299        requestType = MemCmd::ReadReq;
300        flags.set(Request::INST_FETCH);
301        req = std::make_shared<Request>(
302            0, 0x0, access_size, flags, masterId, 0x0, 0);
303        req->setPaddr(paddr);
304    } else {  // if (injReqType == 2)
305        // generate packet for virtual network 2
306        requestType = MemCmd::WriteReq;
307        req = std::make_shared<Request>(paddr, access_size, flags, masterId);
308    }
309
310    req->setContext(id);
311
312    //No need to do functional simulation
313    //We just do timing simulation of the network
314
315    DPRINTF(GarnetSyntheticTraffic,
316            "Generated packet with destination %d, embedded in address %x\n",
317            destination, req->getPaddr());
318
319    PacketPtr pkt = new Packet(req, requestType);
320    pkt->dataDynamic(new uint8_t[req->getSize()]);
321    pkt->senderState = NULL;
322
323    sendPkt(pkt);
324}
325
326void
327GarnetSyntheticTraffic::initTrafficType()
328{
329    trafficStringToEnum["bit_complement"] = BIT_COMPLEMENT_;
330    trafficStringToEnum["bit_reverse"] = BIT_REVERSE_;
331    trafficStringToEnum["bit_rotation"] = BIT_ROTATION_;
332    trafficStringToEnum["neighbor"] = NEIGHBOR_;
333    trafficStringToEnum["shuffle"] = SHUFFLE_;
334    trafficStringToEnum["tornado"] = TORNADO_;
335    trafficStringToEnum["transpose"] = TRANSPOSE_;
336    trafficStringToEnum["uniform_random"] = UNIFORM_RANDOM_;
337}
338
339void
340GarnetSyntheticTraffic::doRetry()
341{
342    if (cachePort.sendTimingReq(retryPkt)) {
343        retryPkt = NULL;
344    }
345}
346
347void
348GarnetSyntheticTraffic::printAddr(Addr a)
349{
350    cachePort.printAddr(a);
351}
352
353
354GarnetSyntheticTraffic *
355GarnetSyntheticTrafficParams::create()
356{
357    return new GarnetSyntheticTraffic(this);
358}
359