a6c08e3b2cc9816909b28ae7877605ae939ba9fb
2 * Copyright (c) 2012-2013 ARM Limited
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.
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.
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.
37 * Authors: Thomas Grass
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"
49 CommMonitor::CommMonitor(Params
* 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
),
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
);
67 // Create a protobuf message for the header and write it to
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
);
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
);
82 // keep track of the sample period both in ticks and absolute time
83 samplePeriod
.setTick(params
->sample_period
);
86 "Created monitor %s with sample period %d ticks (%f s)\n",
87 name(), samplePeriodTicks
, samplePeriod
);
91 CommMonitor::closeStreams()
93 if (traceStream
!= NULL
)
98 CommMonitorParams::create()
100 return new CommMonitor(this);
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");
112 CommMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
114 if (if_name
== "master") {
117 return MemObject::getMasterPort(if_name
, idx
);
122 CommMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
124 if (if_name
== "slave") {
127 return MemObject::getSlavePort(if_name
, idx
);
132 CommMonitor::recvFunctional(PacketPtr pkt
)
134 masterPort
.sendFunctional(pkt
);
138 CommMonitor::recvFunctionalSnoop(PacketPtr pkt
)
140 slavePort
.sendFunctionalSnoop(pkt
);
144 CommMonitor::recvAtomic(PacketPtr pkt
)
146 return masterPort
.sendAtomic(pkt
);
150 CommMonitor::recvAtomicSnoop(PacketPtr pkt
)
152 return slavePort
.sendAtomicSnoop(pkt
);
156 CommMonitor::recvTimingReq(PacketPtr pkt
)
158 // should always see a request
159 assert(pkt
->isRequest());
161 // Store relevant fields of packet, because packet may be modified
162 // or even deleted when sendTiming() is called.
163 bool isRead
= pkt
->isRead();
164 bool isWrite
= pkt
->isWrite();
165 int cmd
= pkt
->cmdToIndex();
166 unsigned size
= pkt
->getSize();
167 Addr addr
= pkt
->getAddr();
168 bool needsResponse
= pkt
->needsResponse();
169 bool memInhibitAsserted
= pkt
->memInhibitAsserted();
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 (needsResponse
&& !memInhibitAsserted
&& !stats
.disableLatencyHists
) {
176 pkt
->pushSenderState(new CommMonitorSenderState(curTick()));
179 // Attempt to send the packet (always succeeds for inhibited
181 bool successful
= masterPort
.sendTimingReq(pkt
);
183 // If not successful, restore the sender state
184 if (!successful
&& needsResponse
&& !stats
.disableLatencyHists
) {
185 delete pkt
->popSenderState();
188 if (successful
&& traceStream
!= NULL
) {
189 // Create a protobuf message representing the
190 // packet. Currently we do not preserve the flags in the
192 Message::Packet pkt_msg
;
193 pkt_msg
.set_tick(curTick());
194 pkt_msg
.set_cmd(cmd
);
195 pkt_msg
.set_addr(addr
);
196 pkt_msg
.set_size(size
);
198 traceStream
->write(pkt_msg
);
201 if (successful
&& isRead
) {
202 DPRINTF(CommMonitor
, "Forwarded read request\n");
204 // Increment number of observed read transactions
205 if (!stats
.disableTransactionHists
) {
209 // Get sample of burst length
210 if (!stats
.disableBurstLengthHists
) {
211 stats
.readBurstLengthHist
.sample(size
);
214 // Sample the masked address
215 if (!stats
.disableAddrDists
) {
216 stats
.readAddrDist
.sample(addr
& readAddrMask
);
219 // If it needs a response increment number of outstanding read
221 if (!stats
.disableOutstandingHists
&& needsResponse
) {
222 ++stats
.outstandingReadReqs
;
225 if (!stats
.disableITTDists
) {
226 // Sample value of read-read inter transaction time
227 if (stats
.timeOfLastRead
!= 0) {
228 stats
.ittReadRead
.sample(curTick() - stats
.timeOfLastRead
);
230 stats
.timeOfLastRead
= curTick();
232 // Sample value of req-req inter transaction time
233 if (stats
.timeOfLastReq
!= 0) {
234 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
236 stats
.timeOfLastReq
= curTick();
238 } else if (successful
&& isWrite
) {
239 DPRINTF(CommMonitor
, "Forwarded write request\n");
242 if (!stats
.disableTransactionHists
) {
246 if (!stats
.disableBurstLengthHists
) {
247 stats
.writeBurstLengthHist
.sample(size
);
250 // Update the bandwidth stats on the request
251 if (!stats
.disableBandwidthHists
) {
252 stats
.writtenBytes
+= size
;
253 stats
.totalWrittenBytes
+= size
;
256 // Sample the masked write address
257 if (!stats
.disableAddrDists
) {
258 stats
.writeAddrDist
.sample(addr
& writeAddrMask
);
261 if (!stats
.disableOutstandingHists
&& needsResponse
) {
262 ++stats
.outstandingWriteReqs
;
265 if (!stats
.disableITTDists
) {
266 // Sample value of write-to-write inter transaction time
267 if (stats
.timeOfLastWrite
!= 0) {
268 stats
.ittWriteWrite
.sample(curTick() - stats
.timeOfLastWrite
);
270 stats
.timeOfLastWrite
= curTick();
272 // Sample value of req-to-req inter transaction time
273 if (stats
.timeOfLastReq
!= 0) {
274 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
276 stats
.timeOfLastReq
= curTick();
278 } else if (successful
) {
279 DPRINTF(CommMonitor
, "Forwarded non read/write request\n");
286 CommMonitor::recvTimingResp(PacketPtr pkt
)
288 // should always see responses
289 assert(pkt
->isResponse());
291 // Store relevant fields of packet, because packet may be modified
292 // or even deleted when sendTiming() is called.
293 bool isRead
= pkt
->isRead();
294 bool isWrite
= pkt
->isWrite();
295 unsigned size
= pkt
->getSize();
297 CommMonitorSenderState
* commReceivedState
=
298 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
300 if (!stats
.disableLatencyHists
) {
301 // Restore initial sender state
302 if (commReceivedState
== NULL
)
303 panic("Monitor got a response without monitor sender state\n");
306 pkt
->senderState
= commReceivedState
->predecessor
;
309 // Attempt to send the packet
310 bool successful
= slavePort
.sendTimingResp(pkt
);
312 if (!stats
.disableLatencyHists
) {
313 // If packet successfully send, sample value of latency,
314 // afterwards delete sender state, otherwise restore state
316 latency
= curTick() - commReceivedState
->transmitTime
;
317 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
318 delete commReceivedState
;
320 // Don't delete anything and let the packet look like we
322 pkt
->senderState
= commReceivedState
;
326 if (successful
&& isRead
) {
327 // Decrement number of outstanding read requests
328 DPRINTF(CommMonitor
, "Received read response\n");
329 if (!stats
.disableOutstandingHists
) {
330 assert(stats
.outstandingReadReqs
!= 0);
331 --stats
.outstandingReadReqs
;
334 if (!stats
.disableLatencyHists
) {
335 stats
.readLatencyHist
.sample(latency
);
338 // Update the bandwidth stats based on responses for reads
339 if (!stats
.disableBandwidthHists
) {
340 stats
.readBytes
+= size
;
341 stats
.totalReadBytes
+= size
;
344 } else if (successful
&& isWrite
) {
345 // Decrement number of outstanding write requests
346 DPRINTF(CommMonitor
, "Received write response\n");
347 if (!stats
.disableOutstandingHists
) {
348 assert(stats
.outstandingWriteReqs
!= 0);
349 --stats
.outstandingWriteReqs
;
352 if (!stats
.disableLatencyHists
) {
353 stats
.writeLatencyHist
.sample(latency
);
355 } else if (successful
) {
356 DPRINTF(CommMonitor
, "Received non read/write response\n");
362 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
364 slavePort
.sendTimingSnoopReq(pkt
);
368 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
370 return masterPort
.sendTimingSnoopResp(pkt
);
374 CommMonitor::isSnooping() const
376 // check if the connected master port is snooping
377 return slavePort
.isSnooping();
381 CommMonitor::deviceBlockSizeMaster()
383 return slavePort
.peerBlockSize();
387 CommMonitor::deviceBlockSizeSlave()
389 return masterPort
.peerBlockSize();
393 CommMonitor::getAddrRanges() const
395 // get the address ranges of the connected slave port
396 return masterPort
.getAddrRanges();
400 CommMonitor::recvRetryMaster()
402 slavePort
.sendRetry();
406 CommMonitor::recvRetrySlave()
408 masterPort
.sendRetry();
412 CommMonitor::recvRangeChange()
414 slavePort
.sendRangeChange();
418 CommMonitor::regStats()
420 // Initialise all the monitor stats
421 using namespace Stats
;
423 stats
.readBurstLengthHist
424 .init(params()->burst_length_bins
)
425 .name(name() + ".readBurstLengthHist")
426 .desc("Histogram of burst lengths of transmitted packets")
427 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
429 stats
.writeBurstLengthHist
430 .init(params()->burst_length_bins
)
431 .name(name() + ".writeBurstLengthHist")
432 .desc("Histogram of burst lengths of transmitted packets")
433 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
435 // Stats based on received responses
436 stats
.readBandwidthHist
437 .init(params()->bandwidth_bins
)
438 .name(name() + ".readBandwidthHist")
439 .desc("Histogram of read bandwidth per sample period (bytes/s)")
440 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
443 .name(name() + ".averageReadBandwidth")
444 .desc("Average read bandwidth (bytes/s)")
445 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
448 .name(name() + ".totalReadBytes")
449 .desc("Number of bytes read")
450 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
452 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
454 // Stats based on successfully sent requests
455 stats
.writeBandwidthHist
456 .init(params()->bandwidth_bins
)
457 .name(name() + ".writeBandwidthHist")
458 .desc("Histogram of write bandwidth (bytes/s)")
459 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
462 .name(name() + ".averageWriteBandwidth")
463 .desc("Average write bandwidth (bytes/s)")
464 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
466 stats
.totalWrittenBytes
467 .name(name() + ".totalWrittenBytes")
468 .desc("Number of bytes written")
469 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
471 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
473 stats
.readLatencyHist
474 .init(params()->latency_bins
)
475 .name(name() + ".readLatencyHist")
476 .desc("Read request-response latency")
477 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
479 stats
.writeLatencyHist
480 .init(params()->latency_bins
)
481 .name(name() + ".writeLatencyHist")
482 .desc("Write request-response latency")
483 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
486 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
488 .name(name() + ".ittReadRead")
489 .desc("Read-to-read inter transaction time")
490 .flags(stats
.disableITTDists
? nozero
: pdf
);
493 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
495 .name(name() + ".ittWriteWrite")
496 .desc("Write-to-write inter transaction time")
497 .flags(stats
.disableITTDists
? nozero
: pdf
);
500 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
502 .name(name() + ".ittReqReq")
503 .desc("Request-to-request inter transaction time")
504 .flags(stats
.disableITTDists
? nozero
: pdf
);
506 stats
.outstandingReadsHist
507 .init(params()->outstanding_bins
)
508 .name(name() + ".outstandingReadsHist")
509 .desc("Outstanding read transactions")
510 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
512 stats
.outstandingWritesHist
513 .init(params()->outstanding_bins
)
514 .name(name() + ".outstandingWritesHist")
515 .desc("Outstanding write transactions")
516 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
519 .init(params()->transaction_bins
)
520 .name(name() + ".readTransHist")
521 .desc("Histogram of read transactions per sample period")
522 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
525 .init(params()->transaction_bins
)
526 .name(name() + ".writeTransHist")
527 .desc("Histogram of read transactions per sample period")
528 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
532 .name(name() + ".readAddrDist")
533 .desc("Read address distribution")
534 .flags(stats
.disableAddrDists
? nozero
: pdf
);
538 .name(name() + ".writeAddrDist")
539 .desc("Write address distribution")
540 .flags(stats
.disableAddrDists
? nozero
: pdf
);
544 CommMonitor::samplePeriodic()
546 // the periodic stats update runs on the granularity of sample
547 // periods, but in combination with this there may also be a
548 // external resets and dumps of the stats (through schedStatEvent)
549 // causing the stats themselves to capture less than a sample
552 // only capture if we have not reset the stats during the last
554 if (simTicks
.value() >= samplePeriodTicks
) {
555 if (!stats
.disableTransactionHists
) {
556 stats
.readTransHist
.sample(stats
.readTrans
);
557 stats
.writeTransHist
.sample(stats
.writeTrans
);
560 if (!stats
.disableBandwidthHists
) {
561 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
562 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
565 if (!stats
.disableOutstandingHists
) {
566 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
567 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
571 // reset the sampled values
573 stats
.writeTrans
= 0;
576 stats
.writtenBytes
= 0;
578 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
582 CommMonitor::startup()
584 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);