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