2 * Copyright (c) 2012 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 unsigned size
= pkt
->getSize();
166 Addr addr
= pkt
->getAddr();
167 bool needsResponse
= pkt
->needsResponse();
168 bool memInhibitAsserted
= pkt
->memInhibitAsserted();
169 Packet::SenderState
* senderState
= pkt
->senderState
;
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
->senderState
= new CommMonitorSenderState(senderState
,
180 // Attempt to send the packet (always succeeds for inhibited
182 bool successful
= masterPort
.sendTimingReq(pkt
);
184 // If not successful, restore the sender state
185 if (!successful
&& needsResponse
&& !stats
.disableLatencyHists
) {
186 delete pkt
->senderState
;
187 pkt
->senderState
= senderState
;
190 if (successful
&& traceStream
!= NULL
) {
191 // Create a protobuf message representing the
192 // packet. Currently we do not preserve the flags in the
194 Message::Packet pkt_msg
;
195 pkt_msg
.set_tick(curTick());
196 pkt_msg
.set_cmd(pkt
->cmdToIndex());
197 pkt_msg
.set_addr(pkt
->getAddr());
198 pkt_msg
.set_size(pkt
->getSize());
200 traceStream
->write(pkt_msg
);
203 if (successful
&& isRead
) {
204 DPRINTF(CommMonitor
, "Forwarded read request\n");
206 // Increment number of observed read transactions
207 if (!stats
.disableTransactionHists
) {
211 // Get sample of burst length
212 if (!stats
.disableBurstLengthHists
) {
213 stats
.readBurstLengthHist
.sample(size
);
216 // Sample the masked address
217 if (!stats
.disableAddrDists
) {
218 stats
.readAddrDist
.sample(addr
& readAddrMask
);
221 // If it needs a response increment number of outstanding read
223 if (!stats
.disableOutstandingHists
&& needsResponse
) {
224 ++stats
.outstandingReadReqs
;
227 if (!stats
.disableITTDists
) {
228 // Sample value of read-read inter transaction time
229 if (stats
.timeOfLastRead
!= 0) {
230 stats
.ittReadRead
.sample(curTick() - stats
.timeOfLastRead
);
232 stats
.timeOfLastRead
= curTick();
234 // Sample value of req-req inter transaction time
235 if (stats
.timeOfLastReq
!= 0) {
236 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
238 stats
.timeOfLastReq
= curTick();
240 } else if (successful
&& isWrite
) {
241 DPRINTF(CommMonitor
, "Forwarded write request\n");
244 if (!stats
.disableTransactionHists
) {
248 if (!stats
.disableBurstLengthHists
) {
249 stats
.writeBurstLengthHist
.sample(size
);
252 // Update the bandwidth stats on the request
253 if (!stats
.disableBandwidthHists
) {
254 stats
.writtenBytes
+= size
;
255 stats
.totalWrittenBytes
+= size
;
258 // Sample the masked write address
259 if (!stats
.disableAddrDists
) {
260 stats
.writeAddrDist
.sample(addr
& writeAddrMask
);
263 if (!stats
.disableOutstandingHists
&& needsResponse
) {
264 ++stats
.outstandingWriteReqs
;
267 if (!stats
.disableITTDists
) {
268 // Sample value of write-to-write inter transaction time
269 if (stats
.timeOfLastWrite
!= 0) {
270 stats
.ittWriteWrite
.sample(curTick() - stats
.timeOfLastWrite
);
272 stats
.timeOfLastWrite
= curTick();
274 // Sample value of req-to-req inter transaction time
275 if (stats
.timeOfLastReq
!= 0) {
276 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
278 stats
.timeOfLastReq
= curTick();
280 } else if (successful
) {
281 DPRINTF(CommMonitor
, "Forwarded non read/write request\n");
288 CommMonitor::recvTimingResp(PacketPtr pkt
)
290 // should always see responses
291 assert(pkt
->isResponse());
293 // Store relevant fields of packet, because packet may be modified
294 // or even deleted when sendTiming() is called.
295 bool isRead
= pkt
->isRead();
296 bool isWrite
= pkt
->isWrite();
297 unsigned size
= pkt
->getSize();
299 CommMonitorSenderState
* commReceivedState
=
300 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
302 if (!stats
.disableLatencyHists
) {
303 // Restore initial sender state
304 if (commReceivedState
== NULL
)
305 panic("Monitor got a response without monitor sender state\n");
308 pkt
->senderState
= commReceivedState
->origSenderState
;
311 // Attempt to send the packet
312 bool successful
= slavePort
.sendTimingResp(pkt
);
314 if (!stats
.disableLatencyHists
) {
315 // If packet successfully send, sample value of latency,
316 // afterwards delete sender state, otherwise restore state
318 latency
= curTick() - commReceivedState
->transmitTime
;
319 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
320 delete commReceivedState
;
322 // Don't delete anything and let the packet look like we
324 pkt
->senderState
= commReceivedState
;
328 if (successful
&& isRead
) {
329 // Decrement number of outstanding read requests
330 DPRINTF(CommMonitor
, "Received read response\n");
331 if (!stats
.disableOutstandingHists
) {
332 assert(stats
.outstandingReadReqs
!= 0);
333 --stats
.outstandingReadReqs
;
336 if (!stats
.disableLatencyHists
) {
337 stats
.readLatencyHist
.sample(latency
);
340 // Update the bandwidth stats based on responses for reads
341 if (!stats
.disableBandwidthHists
) {
342 stats
.readBytes
+= size
;
343 stats
.totalReadBytes
+= size
;
346 } else if (successful
&& isWrite
) {
347 // Decrement number of outstanding write requests
348 DPRINTF(CommMonitor
, "Received write response\n");
349 if (!stats
.disableOutstandingHists
) {
350 assert(stats
.outstandingWriteReqs
!= 0);
351 --stats
.outstandingWriteReqs
;
354 if (!stats
.disableLatencyHists
) {
355 stats
.writeLatencyHist
.sample(latency
);
357 } else if (successful
) {
358 DPRINTF(CommMonitor
, "Received non read/write response\n");
364 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
366 slavePort
.sendTimingSnoopReq(pkt
);
370 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
372 return masterPort
.sendTimingSnoopResp(pkt
);
376 CommMonitor::isSnooping() const
378 // check if the connected master port is snooping
379 return slavePort
.isSnooping();
383 CommMonitor::deviceBlockSizeMaster()
385 return slavePort
.peerBlockSize();
389 CommMonitor::deviceBlockSizeSlave()
391 return masterPort
.peerBlockSize();
395 CommMonitor::getAddrRanges() const
397 // get the address ranges of the connected slave port
398 return masterPort
.getAddrRanges();
402 CommMonitor::recvRetryMaster()
404 slavePort
.sendRetry();
408 CommMonitor::recvRetrySlave()
410 masterPort
.sendRetry();
414 CommMonitor::recvRangeChange()
416 slavePort
.sendRangeChange();
420 CommMonitor::regStats()
422 // Initialise all the monitor stats
423 using namespace Stats
;
425 stats
.readBurstLengthHist
426 .init(params()->burst_length_bins
)
427 .name(name() + ".readBurstLengthHist")
428 .desc("Histogram of burst lengths of transmitted packets")
429 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
431 stats
.writeBurstLengthHist
432 .init(params()->burst_length_bins
)
433 .name(name() + ".writeBurstLengthHist")
434 .desc("Histogram of burst lengths of transmitted packets")
435 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
437 // Stats based on received responses
438 stats
.readBandwidthHist
439 .init(params()->bandwidth_bins
)
440 .name(name() + ".readBandwidthHist")
441 .desc("Histogram of read bandwidth per sample period (bytes/s)")
442 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
445 .name(name() + ".averageReadBandwidth")
446 .desc("Average read bandwidth (bytes/s)")
447 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
450 .name(name() + ".totalReadBytes")
451 .desc("Number of bytes read")
452 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
454 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
456 // Stats based on successfully sent requests
457 stats
.writeBandwidthHist
458 .init(params()->bandwidth_bins
)
459 .name(name() + ".writeBandwidthHist")
460 .desc("Histogram of write bandwidth (bytes/s)")
461 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
464 .name(name() + ".averageWriteBandwidth")
465 .desc("Average write bandwidth (bytes/s)")
466 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
468 stats
.totalWrittenBytes
469 .name(name() + ".totalWrittenBytes")
470 .desc("Number of bytes written")
471 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
473 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
475 stats
.readLatencyHist
476 .init(params()->latency_bins
)
477 .name(name() + ".readLatencyHist")
478 .desc("Read request-response latency")
479 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
481 stats
.writeLatencyHist
482 .init(params()->latency_bins
)
483 .name(name() + ".writeLatencyHist")
484 .desc("Write request-response latency")
485 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
488 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
490 .name(name() + ".ittReadRead")
491 .desc("Read-to-read inter transaction time")
492 .flags(stats
.disableITTDists
? nozero
: pdf
);
495 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
497 .name(name() + ".ittWriteWrite")
498 .desc("Write-to-write inter transaction time")
499 .flags(stats
.disableITTDists
? nozero
: pdf
);
502 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
504 .name(name() + ".ittReqReq")
505 .desc("Request-to-request inter transaction time")
506 .flags(stats
.disableITTDists
? nozero
: pdf
);
508 stats
.outstandingReadsHist
509 .init(params()->outstanding_bins
)
510 .name(name() + ".outstandingReadsHist")
511 .desc("Outstanding read transactions")
512 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
514 stats
.outstandingWritesHist
515 .init(params()->outstanding_bins
)
516 .name(name() + ".outstandingWritesHist")
517 .desc("Outstanding write transactions")
518 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
521 .init(params()->transaction_bins
)
522 .name(name() + ".readTransHist")
523 .desc("Histogram of read transactions per sample period")
524 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
527 .init(params()->transaction_bins
)
528 .name(name() + ".writeTransHist")
529 .desc("Histogram of read transactions per sample period")
530 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
534 .name(name() + ".readAddrDist")
535 .desc("Read address distribution")
536 .flags(stats
.disableAddrDists
? nozero
: pdf
);
540 .name(name() + ".writeAddrDist")
541 .desc("Write address distribution")
542 .flags(stats
.disableAddrDists
? nozero
: pdf
);
546 CommMonitor::samplePeriodic()
548 // the periodic stats update runs on the granularity of sample
549 // periods, but in combination with this there may also be a
550 // external resets and dumps of the stats (through schedStatEvent)
551 // causing the stats themselves to capture less than a sample
554 // only capture if we have not reset the stats during the last
556 if (simTicks
.value() >= samplePeriodTicks
) {
557 if (!stats
.disableTransactionHists
) {
558 stats
.readTransHist
.sample(stats
.readTrans
);
559 stats
.writeTransHist
.sample(stats
.writeTrans
);
562 if (!stats
.disableBandwidthHists
) {
563 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
564 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
567 if (!stats
.disableOutstandingHists
) {
568 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
569 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
573 // reset the sampled values
575 stats
.writeTrans
= 0;
578 stats
.writtenBytes
= 0;
580 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
584 CommMonitor::startup()
586 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);