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