scons: rename TraceFlags to DebugFlags
[gem5.git] / src / cpu / testers / networktest / networktest.cc
1 /*
2 * Copyright (c) 2009 Advanced Micro Devices, Inc.
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 <cmath>
32 #include <iomanip>
33 #include <set>
34 #include <string>
35 #include <vector>
36
37 #include "base/misc.hh"
38 #include "base/statistics.hh"
39 #include "cpu/testers/networktest/networktest.hh"
40 #include "debug/NetworkTest.hh"
41 #include "mem/mem_object.hh"
42 #include "mem/packet.hh"
43 #include "mem/port.hh"
44 #include "mem/request.hh"
45 #include "sim/sim_events.hh"
46 #include "sim/stats.hh"
47
48 using namespace std;
49
50 int TESTER_NETWORK=0;
51
52 bool
53 NetworkTest::CpuPort::recvTiming(PacketPtr pkt)
54 {
55 if (pkt->isResponse()) {
56 networktest->completeRequest(pkt);
57 } else {
58 // must be snoop upcall
59 assert(pkt->isRequest());
60 assert(pkt->getDest() == Packet::Broadcast);
61 }
62 return true;
63 }
64
65 Tick
66 NetworkTest::CpuPort::recvAtomic(PacketPtr pkt)
67 {
68 panic("NetworkTest doesn't expect recvAtomic call!");
69 // Will not be used
70 assert(pkt->isRequest());
71 assert(pkt->getDest() == Packet::Broadcast);
72 return curTick();
73 }
74
75 void
76 NetworkTest::CpuPort::recvFunctional(PacketPtr pkt)
77 {
78 panic("NetworkTest doesn't expect recvFunctional call!");
79 // Will not be used
80 return;
81 }
82
83 void
84 NetworkTest::CpuPort::recvStatusChange(Status status)
85 {
86 if (status == RangeChange) {
87 if (!snoopRangeSent) {
88 snoopRangeSent = true;
89 sendStatusChange(Port::RangeChange);
90 }
91 return;
92 }
93
94 panic("NetworkTest doesn't expect recvStatusChange callback!");
95 }
96
97 void
98 NetworkTest::CpuPort::recvRetry()
99 {
100 networktest->doRetry();
101 }
102
103 void
104 NetworkTest::sendPkt(PacketPtr pkt)
105 {
106 if (!cachePort.sendTiming(pkt)) {
107 retryPkt = pkt; // RubyPort will retry sending
108 }
109 numPacketsSent++;
110 }
111
112 NetworkTest::NetworkTest(const Params *p)
113 : MemObject(p),
114 tickEvent(this),
115 cachePort("network-test", this),
116 retryPkt(NULL),
117 size(p->memory_size),
118 blockSizeBits(p->block_offset),
119 numMemories(p->num_memories),
120 simCycles(p->sim_cycles),
121 fixedPkts(p->fixed_pkts),
122 maxPackets(p->max_packets),
123 trafficType(p->traffic_type),
124 injRate(p->inj_rate),
125 precision(p->precision)
126 {
127 cachePort.snoopRangeSent = false;
128
129 // set up counters
130 noResponseCycles = 0;
131 schedule(tickEvent, 0);
132
133 id = TESTER_NETWORK++;
134 DPRINTF(NetworkTest,"Config Created: Name = %s , and id = %d\n",
135 name(), id);
136 }
137
138 Port *
139 NetworkTest::getPort(const std::string &if_name, int idx)
140 {
141 if (if_name == "test")
142 return &cachePort;
143 else
144 panic("No Such Port\n");
145 }
146
147 void
148 NetworkTest::init()
149 {
150 numPacketsSent = 0;
151 }
152
153
154 void
155 NetworkTest::completeRequest(PacketPtr pkt)
156 {
157 Request *req = pkt->req;
158
159 DPRINTF(NetworkTest, "Completed injection of %s packet for address %x\n",
160 pkt->isWrite() ? "write" : "read\n",
161 req->getPaddr());
162
163 assert(pkt->isResponse());
164 noResponseCycles = 0;
165 delete req;
166 delete pkt;
167 }
168
169
170 void
171 NetworkTest::tick()
172 {
173 if (++noResponseCycles >= 500000) {
174 cerr << name() << ": deadlocked at cycle " << curTick() << endl;
175 fatal("");
176 }
177
178 // make new request based on injection rate
179 // (injection rate's range depends on precision)
180 // - generate a random number between 0 and 10^precision
181 // - send pkt if this number is < injRate*(10^precision)
182 bool send_this_cycle;
183 double injRange = pow((double) 10, (double) precision);
184 unsigned trySending = random() % (int) injRange;
185 if (trySending < injRate*injRange)
186 send_this_cycle = true;
187 else
188 send_this_cycle = false;
189
190 // always generatePkt unless fixedPkts is enabled
191 if (send_this_cycle) {
192 if (fixedPkts) {
193 if (numPacketsSent < maxPackets) {
194 generatePkt();
195 }
196 } else {
197 generatePkt();
198 }
199 }
200
201 // Schedule wakeup
202 if (curTick() >= simCycles)
203 exitSimLoop("Network Tester completed simCycles");
204 else {
205 if (!tickEvent.scheduled())
206 schedule(tickEvent, curTick() + ticks(1));
207 }
208 }
209
210 void
211 NetworkTest::generatePkt()
212 {
213 unsigned destination = id;
214 if (trafficType == 0) { // Uniform Random
215 destination = random() % numMemories;
216 } else if (trafficType == 1) { // Tornado
217 int networkDimension = (int) sqrt(numMemories);
218 int my_x = id%networkDimension;
219 int my_y = id/networkDimension;
220
221 int dest_x = my_x + (int) ceil(networkDimension/2) - 1;
222 dest_x = dest_x%networkDimension;
223 int dest_y = my_y;
224
225 destination = dest_y*networkDimension + dest_x;
226 } else if (trafficType == 2) { // Bit Complement
227 int networkDimension = (int) sqrt(numMemories);
228 int my_x = id%networkDimension;
229 int my_y = id/networkDimension;
230
231 int dest_x = networkDimension - my_x - 1;
232 int dest_y = networkDimension - my_y - 1;
233
234 destination = dest_y*networkDimension + dest_x;
235 }
236
237 Request *req = new Request();
238 Request::Flags flags;
239
240 // The source of the packets is a cache.
241 // The destination of the packets is a directory.
242 // The destination bits are embedded in the address after byte-offset.
243 Addr paddr = destination;
244 paddr <<= blockSizeBits;
245 unsigned access_size = 1; // Does not affect Ruby simulation
246
247 // Modeling different coherence msg types over different msg classes.
248 //
249 // networktest assumes the Network_test coherence protocol
250 // which models three message classes/virtual networks.
251 // These are: request, forward, response.
252 // requests and forwards are "control" packets (typically 8 bytes),
253 // while responses are "data" packets (typically 72 bytes).
254 //
255 // Life of a packet from the tester into the network:
256 // (1) This function generatePkt() generates packets of one of the
257 // following 3 types (randomly) : ReadReq, INST_FETCH, WriteReq
258 // (2) mem/ruby/system/RubyPort.cc converts these to RubyRequestType_LD,
259 // RubyRequestType_IFETCH, RubyRequestType_ST respectively
260 // (3) mem/ruby/system/Sequencer.cc sends these to the cache controllers
261 // in the coherence protocol.
262 // (4) Network_test-cache.sm tags RubyRequestType:LD,
263 // RubyRequestType:IFETCH and RubyRequestType:ST as
264 // Request, Forward, and Response events respectively;
265 // and injects them into virtual networks 0, 1 and 2 respectively.
266 // It immediately calls back the sequencer.
267 // (5) The packet traverses the network (simple/garnet) and reaches its
268 // destination (Directory), and network stats are updated.
269 // (6) Network_test-dir.sm simply drops the packet.
270 //
271 MemCmd::Command requestType;
272
273 unsigned randomReqType = random() % 3;
274 if (randomReqType == 0) {
275 // generate packet for virtual network 0
276 requestType = MemCmd::ReadReq;
277 req->setPhys(paddr, access_size, flags);
278 } else if (randomReqType == 1) {
279 // generate packet for virtual network 1
280 requestType = MemCmd::ReadReq;
281 flags.set(Request::INST_FETCH);
282 req->setVirt(0, 0x0, access_size, flags, 0x0);
283 req->setPaddr(paddr);
284 } else { // if (randomReqType == 2)
285 // generate packet for virtual network 2
286 requestType = MemCmd::WriteReq;
287 req->setPhys(paddr, access_size, flags);
288 }
289
290 req->setThreadContext(id,0);
291 uint8_t *result = new uint8_t[8];
292
293 //No need to do functional simulation
294 //We just do timing simulation of the network
295
296 DPRINTF(NetworkTest,
297 "Generated packet with destination %d, embedded in address %x\n",
298 destination, req->getPaddr());
299
300 PacketPtr pkt = new Packet(req, requestType, 0);
301 pkt->setSrc(0); //Not used
302 pkt->dataDynamicArray(new uint8_t[req->getSize()]);
303 NetworkTestSenderState *state = new NetworkTestSenderState(result);
304 pkt->senderState = state;
305
306 sendPkt(pkt);
307 }
308
309 void
310 NetworkTest::doRetry()
311 {
312 if (cachePort.sendTiming(retryPkt)) {
313 retryPkt = NULL;
314 }
315 }
316
317 void
318 NetworkTest::printAddr(Addr a)
319 {
320 cachePort.printAddr(a);
321 }
322
323
324 NetworkTest *
325 NetworkTestParams::create()
326 {
327 return new NetworkTest(this);
328 }