trace: reimplement the DTRACE function so it doesn't use a vector
[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 numPacketsSent++;
108 accessRetry = false;
109 } else {
110 accessRetry = true;
111 retryPkt = pkt;
112 }
113 }
114
115 NetworkTest::NetworkTest(const Params *p)
116 : MemObject(p),
117 tickEvent(this),
118 cachePort("network-test", this),
119 retryPkt(NULL),
120 size(p->memory_size),
121 blockSizeBits(p->block_offset),
122 numMemories(p->num_memories),
123 fixedPkts(p->fixed_pkts),
124 maxPackets(p->max_packets),
125 trafficType(p->traffic_type),
126 injRate(p->inj_rate),
127 precision(p->precision)
128 {
129 cachePort.snoopRangeSent = false;
130
131 // set up counters
132 noResponseCycles = 0;
133 schedule(tickEvent, 0);
134
135 id = TESTER_NETWORK++;
136 DPRINTF(NetworkTest,"Config Created: Name = %s , and id = %d\n",
137 name(), id);
138
139 accessRetry = false;
140 }
141
142 Port *
143 NetworkTest::getPort(const std::string &if_name, int idx)
144 {
145 if (if_name == "test")
146 return &cachePort;
147 else
148 panic("No Such Port\n");
149 }
150
151 void
152 NetworkTest::init()
153 {
154 numPacketsSent = 0;
155 }
156
157
158 void
159 NetworkTest::completeRequest(PacketPtr pkt)
160 {
161 Request *req = pkt->req;
162
163 DPRINTF(NetworkTest, "Completed injection of %s packet for address %x\n",
164 pkt->isWrite() ? "write" : "read\n",
165 req->getPaddr());
166
167 assert(pkt->isResponse());
168 noResponseCycles = 0;
169 delete req;
170 delete pkt;
171 }
172
173
174 void
175 NetworkTest::tick()
176 {
177 if (!tickEvent.scheduled())
178 schedule(tickEvent, curTick() + ticks(1));
179
180 if (++noResponseCycles >= 500000) {
181 cerr << name() << ": deadlocked at cycle " << curTick() << endl;
182 fatal("");
183 }
184
185 if (accessRetry) {
186 sendPkt(retryPkt);
187 return;
188 }
189
190 // make new request based on injection rate
191 // (injection rate's range depends on precision)
192 // - generate a random number between 0 and 10^precision
193 // - send pkt if this number is < injRate*(10^precision)
194 bool send_this_cycle;
195 double injRange = pow((double) 10, (double) precision);
196 unsigned trySending = random() % (int) injRange;
197 if (trySending < injRate*injRange)
198 send_this_cycle = true;
199 else
200 send_this_cycle = false;
201
202 // always generatePkt unless fixedPkts is enabled
203 if (send_this_cycle) {
204 if (fixedPkts) {
205 if (numPacketsSent < maxPackets) {
206 generatePkt();
207 }
208 } else {
209 generatePkt();
210 }
211 }
212 }
213
214 void
215 NetworkTest::generatePkt()
216 {
217 unsigned destination = id;
218 if (trafficType == 0) { // Uniform Random
219 while (destination == id)
220 destination = random() % numMemories;
221 } else if (trafficType == 1) { // Tornado
222 int networkDimension = (int) sqrt(numMemories);
223 int my_x = id%networkDimension;
224 int my_y = id/networkDimension;
225
226 int dest_x = my_x + (int) ceil(networkDimension/2) - 1;
227 dest_x = dest_x%networkDimension;
228 int dest_y = my_y;
229
230 destination = dest_y*networkDimension + dest_x;
231 } else if (trafficType == 2) { // Bit Complement
232 int networkDimension = (int) sqrt(numMemories);
233 int my_x = id%networkDimension;
234 int my_y = id/networkDimension;
235
236 int dest_x = networkDimension - my_x - 1;
237 int dest_y = networkDimension - my_y - 1;
238
239 destination = dest_y*networkDimension + dest_x;
240 }
241
242 Request *req = new Request();
243 Request::Flags flags;
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 // networktest assumes the Network_test 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 unsigned randomReqType = random() % 3;
279 if (randomReqType == 0) {
280 // generate packet for virtual network 0
281 requestType = MemCmd::ReadReq;
282 req->setPhys(paddr, access_size, flags);
283 } else if (randomReqType == 1) {
284 // generate packet for virtual network 1
285 requestType = MemCmd::ReadReq;
286 flags.set(Request::INST_FETCH);
287 req->setVirt(0, 0x0, access_size, flags, 0x0);
288 req->setPaddr(paddr);
289 } else { // if (randomReqType == 2)
290 // generate packet for virtual network 2
291 requestType = MemCmd::WriteReq;
292 req->setPhys(paddr, access_size, flags);
293 }
294
295 req->setThreadContext(id,0);
296 uint8_t *result = new uint8_t[8];
297
298 //No need to do functional simulation
299 //We just do timing simulation of the network
300
301 DPRINTF(NetworkTest,
302 "Generated packet with destination %d, embedded in address %x\n",
303 destination, req->getPaddr());
304
305 PacketPtr pkt = new Packet(req, requestType, 0);
306 pkt->setSrc(0); //Not used
307 pkt->dataDynamicArray(new uint8_t[req->getSize()]);
308 NetworkTestSenderState *state = new NetworkTestSenderState(result);
309 pkt->senderState = state;
310
311 sendPkt(pkt);
312 }
313
314 void
315 NetworkTest::doRetry()
316 {
317 if (cachePort.sendTiming(retryPkt)) {
318 accessRetry = false;
319 retryPkt = NULL;
320 }
321 }
322
323 void
324 NetworkTest::printAddr(Addr a)
325 {
326 cachePort.printAddr(a);
327 }
328
329
330 NetworkTest *
331 NetworkTestParams::create()
332 {
333 return new NetworkTest(this);
334 }