comm_monitor.cc (10064:0267a9b58c8e) comm_monitor.cc (10129:eb34ae5204b8)
1/*
2 * Copyright (c) 2012-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Thomas Grass
38 * Andreas Hansson
39 */
40
41#include "base/callback.hh"
42#include "base/output.hh"
43#include "base/trace.hh"
44#include "debug/CommMonitor.hh"
45#include "mem/comm_monitor.hh"
46#include "proto/packet.pb.h"
47#include "sim/stats.hh"
48
49CommMonitor::CommMonitor(Params* params)
50 : MemObject(params),
51 masterPort(name() + "-master", *this),
52 slavePort(name() + "-slave", *this),
53 samplePeriodicEvent(this),
54 samplePeriodTicks(params->sample_period),
55 readAddrMask(params->read_addr_mask),
56 writeAddrMask(params->write_addr_mask),
57 stats(params),
1/*
2 * Copyright (c) 2012-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Thomas Grass
38 * Andreas Hansson
39 */
40
41#include "base/callback.hh"
42#include "base/output.hh"
43#include "base/trace.hh"
44#include "debug/CommMonitor.hh"
45#include "mem/comm_monitor.hh"
46#include "proto/packet.pb.h"
47#include "sim/stats.hh"
48
49CommMonitor::CommMonitor(Params* params)
50 : MemObject(params),
51 masterPort(name() + "-master", *this),
52 slavePort(name() + "-slave", *this),
53 samplePeriodicEvent(this),
54 samplePeriodTicks(params->sample_period),
55 readAddrMask(params->read_addr_mask),
56 writeAddrMask(params->write_addr_mask),
57 stats(params),
58 traceStream(NULL)
58 traceStream(NULL),
59 system(params->system)
59{
60 // If we are using a trace file, then open the file,
61 if (params->trace_file != "") {
62 // If the trace file is not specified as an absolute path,
63 // append the current simulation output directory
64 std::string filename = simout.resolve(params->trace_file);
65 traceStream = new ProtoOutputStream(filename);
66
67 // Create a protobuf message for the header and write it to
68 // the stream
69 Message::PacketHeader header_msg;
70 header_msg.set_obj_id(name());
71 header_msg.set_tick_freq(SimClock::Frequency);
72 traceStream->write(header_msg);
73
74 // Register a callback to compensate for the destructor not
75 // being called. The callback forces the stream to flush and
76 // closes the output file.
77 Callback* cb = new MakeCallback<CommMonitor,
78 &CommMonitor::closeStreams>(this);
79 registerExitCallback(cb);
80 }
81
82 // keep track of the sample period both in ticks and absolute time
83 samplePeriod.setTick(params->sample_period);
84
85 DPRINTF(CommMonitor,
86 "Created monitor %s with sample period %d ticks (%f ms)\n",
87 name(), samplePeriodTicks, samplePeriod.msec());
88}
89
90void
91CommMonitor::closeStreams()
92{
93 if (traceStream != NULL)
94 delete traceStream;
95}
96
97CommMonitor*
98CommMonitorParams::create()
99{
100 return new CommMonitor(this);
101}
102
103void
104CommMonitor::init()
105{
106 // make sure both sides of the monitor are connected
107 if (!slavePort.isConnected() || !masterPort.isConnected())
108 fatal("Communication monitor is not connected on both sides.\n");
60{
61 // If we are using a trace file, then open the file,
62 if (params->trace_file != "") {
63 // If the trace file is not specified as an absolute path,
64 // append the current simulation output directory
65 std::string filename = simout.resolve(params->trace_file);
66 traceStream = new ProtoOutputStream(filename);
67
68 // Create a protobuf message for the header and write it to
69 // the stream
70 Message::PacketHeader header_msg;
71 header_msg.set_obj_id(name());
72 header_msg.set_tick_freq(SimClock::Frequency);
73 traceStream->write(header_msg);
74
75 // Register a callback to compensate for the destructor not
76 // being called. The callback forces the stream to flush and
77 // closes the output file.
78 Callback* cb = new MakeCallback<CommMonitor,
79 &CommMonitor::closeStreams>(this);
80 registerExitCallback(cb);
81 }
82
83 // keep track of the sample period both in ticks and absolute time
84 samplePeriod.setTick(params->sample_period);
85
86 DPRINTF(CommMonitor,
87 "Created monitor %s with sample period %d ticks (%f ms)\n",
88 name(), samplePeriodTicks, samplePeriod.msec());
89}
90
91void
92CommMonitor::closeStreams()
93{
94 if (traceStream != NULL)
95 delete traceStream;
96}
97
98CommMonitor*
99CommMonitorParams::create()
100{
101 return new CommMonitor(this);
102}
103
104void
105CommMonitor::init()
106{
107 // make sure both sides of the monitor are connected
108 if (!slavePort.isConnected() || !masterPort.isConnected())
109 fatal("Communication monitor is not connected on both sides.\n");
110
111 if (traceStream != NULL) {
112 // Check the memory mode. We only record something when in
113 // timing mode. Warn accordingly.
114 if (!system->isTimingMode())
115 warn("%s: Not in timing mode. No trace will be recorded.", name());
116 }
109}
110
111BaseMasterPort&
112CommMonitor::getMasterPort(const std::string& if_name, PortID idx)
113{
114 if (if_name == "master") {
115 return masterPort;
116 } else {
117 return MemObject::getMasterPort(if_name, idx);
118 }
119}
120
121BaseSlavePort&
122CommMonitor::getSlavePort(const std::string& if_name, PortID idx)
123{
124 if (if_name == "slave") {
125 return slavePort;
126 } else {
127 return MemObject::getSlavePort(if_name, idx);
128 }
129}
130
131void
132CommMonitor::recvFunctional(PacketPtr pkt)
133{
134 masterPort.sendFunctional(pkt);
135}
136
137void
138CommMonitor::recvFunctionalSnoop(PacketPtr pkt)
139{
140 slavePort.sendFunctionalSnoop(pkt);
141}
142
143Tick
144CommMonitor::recvAtomic(PacketPtr pkt)
145{
146 return masterPort.sendAtomic(pkt);
147}
148
149Tick
150CommMonitor::recvAtomicSnoop(PacketPtr pkt)
151{
152 return slavePort.sendAtomicSnoop(pkt);
153}
154
155bool
156CommMonitor::recvTimingReq(PacketPtr pkt)
157{
158 // should always see a request
159 assert(pkt->isRequest());
160
161 // Store relevant fields of packet, because packet may be modified
162 // or even deleted when sendTiming() is called.
163 bool is_read = pkt->isRead();
164 bool is_write = pkt->isWrite();
165 int cmd = pkt->cmdToIndex();
166 Request::FlagsType req_flags = pkt->req->getFlags();
167 unsigned size = pkt->getSize();
168 Addr addr = pkt->getAddr();
169 bool expects_response = pkt->needsResponse() && !pkt->memInhibitAsserted();
170
171 // If a cache miss is served by a cache, a monitor near the memory
172 // would see a request which needs a response, but this response
173 // would be inhibited and not come back from the memory. Therefore
174 // we additionally have to check the inhibit flag.
175 if (expects_response && !stats.disableLatencyHists) {
176 pkt->pushSenderState(new CommMonitorSenderState(curTick()));
177 }
178
179 // Attempt to send the packet (always succeeds for inhibited
180 // packets)
181 bool successful = masterPort.sendTimingReq(pkt);
182
183 // If not successful, restore the sender state
184 if (!successful && expects_response && !stats.disableLatencyHists) {
185 delete pkt->popSenderState();
186 }
187
188 if (successful && traceStream != NULL) {
189 // Create a protobuf message representing the
190 // packet. Currently we do not preserve the flags in the
191 // trace.
192 Message::Packet pkt_msg;
193 pkt_msg.set_tick(curTick());
194 pkt_msg.set_cmd(cmd);
195 pkt_msg.set_flags(req_flags);
196 pkt_msg.set_addr(addr);
197 pkt_msg.set_size(size);
198
199 traceStream->write(pkt_msg);
200 }
201
202 if (successful && is_read) {
203 DPRINTF(CommMonitor, "Forwarded read request\n");
204
205 // Increment number of observed read transactions
206 if (!stats.disableTransactionHists) {
207 ++stats.readTrans;
208 }
209
210 // Get sample of burst length
211 if (!stats.disableBurstLengthHists) {
212 stats.readBurstLengthHist.sample(size);
213 }
214
215 // Sample the masked address
216 if (!stats.disableAddrDists) {
217 stats.readAddrDist.sample(addr & readAddrMask);
218 }
219
220 // If it needs a response increment number of outstanding read
221 // requests
222 if (!stats.disableOutstandingHists && expects_response) {
223 ++stats.outstandingReadReqs;
224 }
225
226 if (!stats.disableITTDists) {
227 // Sample value of read-read inter transaction time
228 if (stats.timeOfLastRead != 0) {
229 stats.ittReadRead.sample(curTick() - stats.timeOfLastRead);
230 }
231 stats.timeOfLastRead = curTick();
232
233 // Sample value of req-req inter transaction time
234 if (stats.timeOfLastReq != 0) {
235 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
236 }
237 stats.timeOfLastReq = curTick();
238 }
239 } else if (successful && is_write) {
240 DPRINTF(CommMonitor, "Forwarded write request\n");
241
242 // Same as for reads
243 if (!stats.disableTransactionHists) {
244 ++stats.writeTrans;
245 }
246
247 if (!stats.disableBurstLengthHists) {
248 stats.writeBurstLengthHist.sample(size);
249 }
250
251 // Update the bandwidth stats on the request
252 if (!stats.disableBandwidthHists) {
253 stats.writtenBytes += size;
254 stats.totalWrittenBytes += size;
255 }
256
257 // Sample the masked write address
258 if (!stats.disableAddrDists) {
259 stats.writeAddrDist.sample(addr & writeAddrMask);
260 }
261
262 if (!stats.disableOutstandingHists && expects_response) {
263 ++stats.outstandingWriteReqs;
264 }
265
266 if (!stats.disableITTDists) {
267 // Sample value of write-to-write inter transaction time
268 if (stats.timeOfLastWrite != 0) {
269 stats.ittWriteWrite.sample(curTick() - stats.timeOfLastWrite);
270 }
271 stats.timeOfLastWrite = curTick();
272
273 // Sample value of req-to-req inter transaction time
274 if (stats.timeOfLastReq != 0) {
275 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
276 }
277 stats.timeOfLastReq = curTick();
278 }
279 } else if (successful) {
280 DPRINTF(CommMonitor, "Forwarded non read/write request\n");
281 }
282
283 return successful;
284}
285
286bool
287CommMonitor::recvTimingResp(PacketPtr pkt)
288{
289 // should always see responses
290 assert(pkt->isResponse());
291
292 // Store relevant fields of packet, because packet may be modified
293 // or even deleted when sendTiming() is called.
294 bool is_read = pkt->isRead();
295 bool is_write = pkt->isWrite();
296 unsigned size = pkt->getSize();
297 Tick latency = 0;
298 CommMonitorSenderState* received_state =
299 dynamic_cast<CommMonitorSenderState*>(pkt->senderState);
300
301 if (!stats.disableLatencyHists) {
302 // Restore initial sender state
303 if (received_state == NULL)
304 panic("Monitor got a response without monitor sender state\n");
305
306 // Restore the sate
307 pkt->senderState = received_state->predecessor;
308 }
309
310 // Attempt to send the packet
311 bool successful = slavePort.sendTimingResp(pkt);
312
313 if (!stats.disableLatencyHists) {
314 // If packet successfully send, sample value of latency,
315 // afterwards delete sender state, otherwise restore state
316 if (successful) {
317 latency = curTick() - received_state->transmitTime;
318 DPRINTF(CommMonitor, "Latency: %d\n", latency);
319 delete received_state;
320 } else {
321 // Don't delete anything and let the packet look like we
322 // did not touch it
323 pkt->senderState = received_state;
324 }
325 }
326
327 if (successful && is_read) {
328 // Decrement number of outstanding read requests
329 DPRINTF(CommMonitor, "Received read response\n");
330 if (!stats.disableOutstandingHists) {
331 assert(stats.outstandingReadReqs != 0);
332 --stats.outstandingReadReqs;
333 }
334
335 if (!stats.disableLatencyHists) {
336 stats.readLatencyHist.sample(latency);
337 }
338
339 // Update the bandwidth stats based on responses for reads
340 if (!stats.disableBandwidthHists) {
341 stats.readBytes += size;
342 stats.totalReadBytes += size;
343 }
344
345 } else if (successful && is_write) {
346 // Decrement number of outstanding write requests
347 DPRINTF(CommMonitor, "Received write response\n");
348 if (!stats.disableOutstandingHists) {
349 assert(stats.outstandingWriteReqs != 0);
350 --stats.outstandingWriteReqs;
351 }
352
353 if (!stats.disableLatencyHists) {
354 stats.writeLatencyHist.sample(latency);
355 }
356 } else if (successful) {
357 DPRINTF(CommMonitor, "Received non read/write response\n");
358 }
359 return successful;
360}
361
362void
363CommMonitor::recvTimingSnoopReq(PacketPtr pkt)
364{
365 slavePort.sendTimingSnoopReq(pkt);
366}
367
368bool
369CommMonitor::recvTimingSnoopResp(PacketPtr pkt)
370{
371 return masterPort.sendTimingSnoopResp(pkt);
372}
373
374bool
375CommMonitor::isSnooping() const
376{
377 // check if the connected master port is snooping
378 return slavePort.isSnooping();
379}
380
381AddrRangeList
382CommMonitor::getAddrRanges() const
383{
384 // get the address ranges of the connected slave port
385 return masterPort.getAddrRanges();
386}
387
388void
389CommMonitor::recvRetryMaster()
390{
391 slavePort.sendRetry();
392}
393
394void
395CommMonitor::recvRetrySlave()
396{
397 masterPort.sendRetry();
398}
399
400void
401CommMonitor::recvRangeChange()
402{
403 slavePort.sendRangeChange();
404}
405
406void
407CommMonitor::regStats()
408{
409 // Initialise all the monitor stats
410 using namespace Stats;
411
412 stats.readBurstLengthHist
413 .init(params()->burst_length_bins)
414 .name(name() + ".readBurstLengthHist")
415 .desc("Histogram of burst lengths of transmitted packets")
416 .flags(stats.disableBurstLengthHists ? nozero : pdf);
417
418 stats.writeBurstLengthHist
419 .init(params()->burst_length_bins)
420 .name(name() + ".writeBurstLengthHist")
421 .desc("Histogram of burst lengths of transmitted packets")
422 .flags(stats.disableBurstLengthHists ? nozero : pdf);
423
424 // Stats based on received responses
425 stats.readBandwidthHist
426 .init(params()->bandwidth_bins)
427 .name(name() + ".readBandwidthHist")
428 .desc("Histogram of read bandwidth per sample period (bytes/s)")
429 .flags(stats.disableBandwidthHists ? nozero : pdf);
430
431 stats.averageReadBW
432 .name(name() + ".averageReadBandwidth")
433 .desc("Average read bandwidth (bytes/s)")
434 .flags(stats.disableBandwidthHists ? nozero : pdf);
435
436 stats.totalReadBytes
437 .name(name() + ".totalReadBytes")
438 .desc("Number of bytes read")
439 .flags(stats.disableBandwidthHists ? nozero : pdf);
440
441 stats.averageReadBW = stats.totalReadBytes / simSeconds;
442
443 // Stats based on successfully sent requests
444 stats.writeBandwidthHist
445 .init(params()->bandwidth_bins)
446 .name(name() + ".writeBandwidthHist")
447 .desc("Histogram of write bandwidth (bytes/s)")
448 .flags(stats.disableBandwidthHists ? (pdf | nozero) : pdf);
449
450 stats.averageWriteBW
451 .name(name() + ".averageWriteBandwidth")
452 .desc("Average write bandwidth (bytes/s)")
453 .flags(stats.disableBandwidthHists ? nozero : pdf);
454
455 stats.totalWrittenBytes
456 .name(name() + ".totalWrittenBytes")
457 .desc("Number of bytes written")
458 .flags(stats.disableBandwidthHists ? nozero : pdf);
459
460 stats.averageWriteBW = stats.totalWrittenBytes / simSeconds;
461
462 stats.readLatencyHist
463 .init(params()->latency_bins)
464 .name(name() + ".readLatencyHist")
465 .desc("Read request-response latency")
466 .flags(stats.disableLatencyHists ? nozero : pdf);
467
468 stats.writeLatencyHist
469 .init(params()->latency_bins)
470 .name(name() + ".writeLatencyHist")
471 .desc("Write request-response latency")
472 .flags(stats.disableLatencyHists ? nozero : pdf);
473
474 stats.ittReadRead
475 .init(1, params()->itt_max_bin, params()->itt_max_bin /
476 params()->itt_bins)
477 .name(name() + ".ittReadRead")
478 .desc("Read-to-read inter transaction time")
479 .flags(stats.disableITTDists ? nozero : pdf);
480
481 stats.ittWriteWrite
482 .init(1, params()->itt_max_bin, params()->itt_max_bin /
483 params()->itt_bins)
484 .name(name() + ".ittWriteWrite")
485 .desc("Write-to-write inter transaction time")
486 .flags(stats.disableITTDists ? nozero : pdf);
487
488 stats.ittReqReq
489 .init(1, params()->itt_max_bin, params()->itt_max_bin /
490 params()->itt_bins)
491 .name(name() + ".ittReqReq")
492 .desc("Request-to-request inter transaction time")
493 .flags(stats.disableITTDists ? nozero : pdf);
494
495 stats.outstandingReadsHist
496 .init(params()->outstanding_bins)
497 .name(name() + ".outstandingReadsHist")
498 .desc("Outstanding read transactions")
499 .flags(stats.disableOutstandingHists ? nozero : pdf);
500
501 stats.outstandingWritesHist
502 .init(params()->outstanding_bins)
503 .name(name() + ".outstandingWritesHist")
504 .desc("Outstanding write transactions")
505 .flags(stats.disableOutstandingHists ? nozero : pdf);
506
507 stats.readTransHist
508 .init(params()->transaction_bins)
509 .name(name() + ".readTransHist")
510 .desc("Histogram of read transactions per sample period")
511 .flags(stats.disableTransactionHists ? nozero : pdf);
512
513 stats.writeTransHist
514 .init(params()->transaction_bins)
515 .name(name() + ".writeTransHist")
516 .desc("Histogram of read transactions per sample period")
517 .flags(stats.disableTransactionHists ? nozero : pdf);
518
519 stats.readAddrDist
520 .init(0)
521 .name(name() + ".readAddrDist")
522 .desc("Read address distribution")
523 .flags(stats.disableAddrDists ? nozero : pdf);
524
525 stats.writeAddrDist
526 .init(0)
527 .name(name() + ".writeAddrDist")
528 .desc("Write address distribution")
529 .flags(stats.disableAddrDists ? nozero : pdf);
530}
531
532void
533CommMonitor::samplePeriodic()
534{
535 // the periodic stats update runs on the granularity of sample
536 // periods, but in combination with this there may also be a
537 // external resets and dumps of the stats (through schedStatEvent)
538 // causing the stats themselves to capture less than a sample
539 // period
540
541 // only capture if we have not reset the stats during the last
542 // sample period
543 if (simTicks.value() >= samplePeriodTicks) {
544 if (!stats.disableTransactionHists) {
545 stats.readTransHist.sample(stats.readTrans);
546 stats.writeTransHist.sample(stats.writeTrans);
547 }
548
549 if (!stats.disableBandwidthHists) {
550 stats.readBandwidthHist.sample(stats.readBytes / samplePeriod);
551 stats.writeBandwidthHist.sample(stats.writtenBytes / samplePeriod);
552 }
553
554 if (!stats.disableOutstandingHists) {
555 stats.outstandingReadsHist.sample(stats.outstandingReadReqs);
556 stats.outstandingWritesHist.sample(stats.outstandingWriteReqs);
557 }
558 }
559
560 // reset the sampled values
561 stats.readTrans = 0;
562 stats.writeTrans = 0;
563
564 stats.readBytes = 0;
565 stats.writtenBytes = 0;
566
567 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
568}
569
570void
571CommMonitor::startup()
572{
573 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
574}
117}
118
119BaseMasterPort&
120CommMonitor::getMasterPort(const std::string& if_name, PortID idx)
121{
122 if (if_name == "master") {
123 return masterPort;
124 } else {
125 return MemObject::getMasterPort(if_name, idx);
126 }
127}
128
129BaseSlavePort&
130CommMonitor::getSlavePort(const std::string& if_name, PortID idx)
131{
132 if (if_name == "slave") {
133 return slavePort;
134 } else {
135 return MemObject::getSlavePort(if_name, idx);
136 }
137}
138
139void
140CommMonitor::recvFunctional(PacketPtr pkt)
141{
142 masterPort.sendFunctional(pkt);
143}
144
145void
146CommMonitor::recvFunctionalSnoop(PacketPtr pkt)
147{
148 slavePort.sendFunctionalSnoop(pkt);
149}
150
151Tick
152CommMonitor::recvAtomic(PacketPtr pkt)
153{
154 return masterPort.sendAtomic(pkt);
155}
156
157Tick
158CommMonitor::recvAtomicSnoop(PacketPtr pkt)
159{
160 return slavePort.sendAtomicSnoop(pkt);
161}
162
163bool
164CommMonitor::recvTimingReq(PacketPtr pkt)
165{
166 // should always see a request
167 assert(pkt->isRequest());
168
169 // Store relevant fields of packet, because packet may be modified
170 // or even deleted when sendTiming() is called.
171 bool is_read = pkt->isRead();
172 bool is_write = pkt->isWrite();
173 int cmd = pkt->cmdToIndex();
174 Request::FlagsType req_flags = pkt->req->getFlags();
175 unsigned size = pkt->getSize();
176 Addr addr = pkt->getAddr();
177 bool expects_response = pkt->needsResponse() && !pkt->memInhibitAsserted();
178
179 // If a cache miss is served by a cache, a monitor near the memory
180 // would see a request which needs a response, but this response
181 // would be inhibited and not come back from the memory. Therefore
182 // we additionally have to check the inhibit flag.
183 if (expects_response && !stats.disableLatencyHists) {
184 pkt->pushSenderState(new CommMonitorSenderState(curTick()));
185 }
186
187 // Attempt to send the packet (always succeeds for inhibited
188 // packets)
189 bool successful = masterPort.sendTimingReq(pkt);
190
191 // If not successful, restore the sender state
192 if (!successful && expects_response && !stats.disableLatencyHists) {
193 delete pkt->popSenderState();
194 }
195
196 if (successful && traceStream != NULL) {
197 // Create a protobuf message representing the
198 // packet. Currently we do not preserve the flags in the
199 // trace.
200 Message::Packet pkt_msg;
201 pkt_msg.set_tick(curTick());
202 pkt_msg.set_cmd(cmd);
203 pkt_msg.set_flags(req_flags);
204 pkt_msg.set_addr(addr);
205 pkt_msg.set_size(size);
206
207 traceStream->write(pkt_msg);
208 }
209
210 if (successful && is_read) {
211 DPRINTF(CommMonitor, "Forwarded read request\n");
212
213 // Increment number of observed read transactions
214 if (!stats.disableTransactionHists) {
215 ++stats.readTrans;
216 }
217
218 // Get sample of burst length
219 if (!stats.disableBurstLengthHists) {
220 stats.readBurstLengthHist.sample(size);
221 }
222
223 // Sample the masked address
224 if (!stats.disableAddrDists) {
225 stats.readAddrDist.sample(addr & readAddrMask);
226 }
227
228 // If it needs a response increment number of outstanding read
229 // requests
230 if (!stats.disableOutstandingHists && expects_response) {
231 ++stats.outstandingReadReqs;
232 }
233
234 if (!stats.disableITTDists) {
235 // Sample value of read-read inter transaction time
236 if (stats.timeOfLastRead != 0) {
237 stats.ittReadRead.sample(curTick() - stats.timeOfLastRead);
238 }
239 stats.timeOfLastRead = curTick();
240
241 // Sample value of req-req inter transaction time
242 if (stats.timeOfLastReq != 0) {
243 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
244 }
245 stats.timeOfLastReq = curTick();
246 }
247 } else if (successful && is_write) {
248 DPRINTF(CommMonitor, "Forwarded write request\n");
249
250 // Same as for reads
251 if (!stats.disableTransactionHists) {
252 ++stats.writeTrans;
253 }
254
255 if (!stats.disableBurstLengthHists) {
256 stats.writeBurstLengthHist.sample(size);
257 }
258
259 // Update the bandwidth stats on the request
260 if (!stats.disableBandwidthHists) {
261 stats.writtenBytes += size;
262 stats.totalWrittenBytes += size;
263 }
264
265 // Sample the masked write address
266 if (!stats.disableAddrDists) {
267 stats.writeAddrDist.sample(addr & writeAddrMask);
268 }
269
270 if (!stats.disableOutstandingHists && expects_response) {
271 ++stats.outstandingWriteReqs;
272 }
273
274 if (!stats.disableITTDists) {
275 // Sample value of write-to-write inter transaction time
276 if (stats.timeOfLastWrite != 0) {
277 stats.ittWriteWrite.sample(curTick() - stats.timeOfLastWrite);
278 }
279 stats.timeOfLastWrite = curTick();
280
281 // Sample value of req-to-req inter transaction time
282 if (stats.timeOfLastReq != 0) {
283 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
284 }
285 stats.timeOfLastReq = curTick();
286 }
287 } else if (successful) {
288 DPRINTF(CommMonitor, "Forwarded non read/write request\n");
289 }
290
291 return successful;
292}
293
294bool
295CommMonitor::recvTimingResp(PacketPtr pkt)
296{
297 // should always see responses
298 assert(pkt->isResponse());
299
300 // Store relevant fields of packet, because packet may be modified
301 // or even deleted when sendTiming() is called.
302 bool is_read = pkt->isRead();
303 bool is_write = pkt->isWrite();
304 unsigned size = pkt->getSize();
305 Tick latency = 0;
306 CommMonitorSenderState* received_state =
307 dynamic_cast<CommMonitorSenderState*>(pkt->senderState);
308
309 if (!stats.disableLatencyHists) {
310 // Restore initial sender state
311 if (received_state == NULL)
312 panic("Monitor got a response without monitor sender state\n");
313
314 // Restore the sate
315 pkt->senderState = received_state->predecessor;
316 }
317
318 // Attempt to send the packet
319 bool successful = slavePort.sendTimingResp(pkt);
320
321 if (!stats.disableLatencyHists) {
322 // If packet successfully send, sample value of latency,
323 // afterwards delete sender state, otherwise restore state
324 if (successful) {
325 latency = curTick() - received_state->transmitTime;
326 DPRINTF(CommMonitor, "Latency: %d\n", latency);
327 delete received_state;
328 } else {
329 // Don't delete anything and let the packet look like we
330 // did not touch it
331 pkt->senderState = received_state;
332 }
333 }
334
335 if (successful && is_read) {
336 // Decrement number of outstanding read requests
337 DPRINTF(CommMonitor, "Received read response\n");
338 if (!stats.disableOutstandingHists) {
339 assert(stats.outstandingReadReqs != 0);
340 --stats.outstandingReadReqs;
341 }
342
343 if (!stats.disableLatencyHists) {
344 stats.readLatencyHist.sample(latency);
345 }
346
347 // Update the bandwidth stats based on responses for reads
348 if (!stats.disableBandwidthHists) {
349 stats.readBytes += size;
350 stats.totalReadBytes += size;
351 }
352
353 } else if (successful && is_write) {
354 // Decrement number of outstanding write requests
355 DPRINTF(CommMonitor, "Received write response\n");
356 if (!stats.disableOutstandingHists) {
357 assert(stats.outstandingWriteReqs != 0);
358 --stats.outstandingWriteReqs;
359 }
360
361 if (!stats.disableLatencyHists) {
362 stats.writeLatencyHist.sample(latency);
363 }
364 } else if (successful) {
365 DPRINTF(CommMonitor, "Received non read/write response\n");
366 }
367 return successful;
368}
369
370void
371CommMonitor::recvTimingSnoopReq(PacketPtr pkt)
372{
373 slavePort.sendTimingSnoopReq(pkt);
374}
375
376bool
377CommMonitor::recvTimingSnoopResp(PacketPtr pkt)
378{
379 return masterPort.sendTimingSnoopResp(pkt);
380}
381
382bool
383CommMonitor::isSnooping() const
384{
385 // check if the connected master port is snooping
386 return slavePort.isSnooping();
387}
388
389AddrRangeList
390CommMonitor::getAddrRanges() const
391{
392 // get the address ranges of the connected slave port
393 return masterPort.getAddrRanges();
394}
395
396void
397CommMonitor::recvRetryMaster()
398{
399 slavePort.sendRetry();
400}
401
402void
403CommMonitor::recvRetrySlave()
404{
405 masterPort.sendRetry();
406}
407
408void
409CommMonitor::recvRangeChange()
410{
411 slavePort.sendRangeChange();
412}
413
414void
415CommMonitor::regStats()
416{
417 // Initialise all the monitor stats
418 using namespace Stats;
419
420 stats.readBurstLengthHist
421 .init(params()->burst_length_bins)
422 .name(name() + ".readBurstLengthHist")
423 .desc("Histogram of burst lengths of transmitted packets")
424 .flags(stats.disableBurstLengthHists ? nozero : pdf);
425
426 stats.writeBurstLengthHist
427 .init(params()->burst_length_bins)
428 .name(name() + ".writeBurstLengthHist")
429 .desc("Histogram of burst lengths of transmitted packets")
430 .flags(stats.disableBurstLengthHists ? nozero : pdf);
431
432 // Stats based on received responses
433 stats.readBandwidthHist
434 .init(params()->bandwidth_bins)
435 .name(name() + ".readBandwidthHist")
436 .desc("Histogram of read bandwidth per sample period (bytes/s)")
437 .flags(stats.disableBandwidthHists ? nozero : pdf);
438
439 stats.averageReadBW
440 .name(name() + ".averageReadBandwidth")
441 .desc("Average read bandwidth (bytes/s)")
442 .flags(stats.disableBandwidthHists ? nozero : pdf);
443
444 stats.totalReadBytes
445 .name(name() + ".totalReadBytes")
446 .desc("Number of bytes read")
447 .flags(stats.disableBandwidthHists ? nozero : pdf);
448
449 stats.averageReadBW = stats.totalReadBytes / simSeconds;
450
451 // Stats based on successfully sent requests
452 stats.writeBandwidthHist
453 .init(params()->bandwidth_bins)
454 .name(name() + ".writeBandwidthHist")
455 .desc("Histogram of write bandwidth (bytes/s)")
456 .flags(stats.disableBandwidthHists ? (pdf | nozero) : pdf);
457
458 stats.averageWriteBW
459 .name(name() + ".averageWriteBandwidth")
460 .desc("Average write bandwidth (bytes/s)")
461 .flags(stats.disableBandwidthHists ? nozero : pdf);
462
463 stats.totalWrittenBytes
464 .name(name() + ".totalWrittenBytes")
465 .desc("Number of bytes written")
466 .flags(stats.disableBandwidthHists ? nozero : pdf);
467
468 stats.averageWriteBW = stats.totalWrittenBytes / simSeconds;
469
470 stats.readLatencyHist
471 .init(params()->latency_bins)
472 .name(name() + ".readLatencyHist")
473 .desc("Read request-response latency")
474 .flags(stats.disableLatencyHists ? nozero : pdf);
475
476 stats.writeLatencyHist
477 .init(params()->latency_bins)
478 .name(name() + ".writeLatencyHist")
479 .desc("Write request-response latency")
480 .flags(stats.disableLatencyHists ? nozero : pdf);
481
482 stats.ittReadRead
483 .init(1, params()->itt_max_bin, params()->itt_max_bin /
484 params()->itt_bins)
485 .name(name() + ".ittReadRead")
486 .desc("Read-to-read inter transaction time")
487 .flags(stats.disableITTDists ? nozero : pdf);
488
489 stats.ittWriteWrite
490 .init(1, params()->itt_max_bin, params()->itt_max_bin /
491 params()->itt_bins)
492 .name(name() + ".ittWriteWrite")
493 .desc("Write-to-write inter transaction time")
494 .flags(stats.disableITTDists ? nozero : pdf);
495
496 stats.ittReqReq
497 .init(1, params()->itt_max_bin, params()->itt_max_bin /
498 params()->itt_bins)
499 .name(name() + ".ittReqReq")
500 .desc("Request-to-request inter transaction time")
501 .flags(stats.disableITTDists ? nozero : pdf);
502
503 stats.outstandingReadsHist
504 .init(params()->outstanding_bins)
505 .name(name() + ".outstandingReadsHist")
506 .desc("Outstanding read transactions")
507 .flags(stats.disableOutstandingHists ? nozero : pdf);
508
509 stats.outstandingWritesHist
510 .init(params()->outstanding_bins)
511 .name(name() + ".outstandingWritesHist")
512 .desc("Outstanding write transactions")
513 .flags(stats.disableOutstandingHists ? nozero : pdf);
514
515 stats.readTransHist
516 .init(params()->transaction_bins)
517 .name(name() + ".readTransHist")
518 .desc("Histogram of read transactions per sample period")
519 .flags(stats.disableTransactionHists ? nozero : pdf);
520
521 stats.writeTransHist
522 .init(params()->transaction_bins)
523 .name(name() + ".writeTransHist")
524 .desc("Histogram of read transactions per sample period")
525 .flags(stats.disableTransactionHists ? nozero : pdf);
526
527 stats.readAddrDist
528 .init(0)
529 .name(name() + ".readAddrDist")
530 .desc("Read address distribution")
531 .flags(stats.disableAddrDists ? nozero : pdf);
532
533 stats.writeAddrDist
534 .init(0)
535 .name(name() + ".writeAddrDist")
536 .desc("Write address distribution")
537 .flags(stats.disableAddrDists ? nozero : pdf);
538}
539
540void
541CommMonitor::samplePeriodic()
542{
543 // the periodic stats update runs on the granularity of sample
544 // periods, but in combination with this there may also be a
545 // external resets and dumps of the stats (through schedStatEvent)
546 // causing the stats themselves to capture less than a sample
547 // period
548
549 // only capture if we have not reset the stats during the last
550 // sample period
551 if (simTicks.value() >= samplePeriodTicks) {
552 if (!stats.disableTransactionHists) {
553 stats.readTransHist.sample(stats.readTrans);
554 stats.writeTransHist.sample(stats.writeTrans);
555 }
556
557 if (!stats.disableBandwidthHists) {
558 stats.readBandwidthHist.sample(stats.readBytes / samplePeriod);
559 stats.writeBandwidthHist.sample(stats.writtenBytes / samplePeriod);
560 }
561
562 if (!stats.disableOutstandingHists) {
563 stats.outstandingReadsHist.sample(stats.outstandingReadReqs);
564 stats.outstandingWritesHist.sample(stats.outstandingWriteReqs);
565 }
566 }
567
568 // reset the sampled values
569 stats.readTrans = 0;
570 stats.writeTrans = 0;
571
572 stats.readBytes = 0;
573 stats.writtenBytes = 0;
574
575 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
576}
577
578void
579CommMonitor::startup()
580{
581 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
582}