2 * Copyright (c) 2012-2013, 2015 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 samplePeriod(params
->sample_period
/ SimClock::Float::s
),
56 readAddrMask(params
->read_addr_mask
),
57 writeAddrMask(params
->write_addr_mask
),
58 system(params
->system
),
62 // If we are using a trace file, then open the file
63 if (params
->trace_enable
) {
65 if (params
->trace_file
!= "") {
66 // If the trace file is not specified as an absolute path,
67 // append the current simulation output directory
68 filename
= simout
.resolve(params
->trace_file
);
70 std::string suffix
= ".gz";
71 // If trace_compress has been set, check the suffix. Append
73 if (params
->trace_compress
&&
74 filename
.compare(filename
.size() - suffix
.size(), suffix
.size(),
76 filename
= filename
+ suffix
;
78 // Generate a filename from the name of the SimObject. Append .trc
79 // and .gz if we want compression enabled.
80 filename
= simout
.resolve(name() + ".trc" +
81 (params
->trace_compress
? ".gz" : ""));
84 traceStream
= new ProtoOutputStream(filename
);
86 // Create a protobuf message for the header and write it to
88 ProtoMessage::PacketHeader header_msg
;
89 header_msg
.set_obj_id(name());
90 header_msg
.set_tick_freq(SimClock::Frequency
);
91 traceStream
->write(header_msg
);
93 // Register a callback to compensate for the destructor not
94 // being called. The callback forces the stream to flush and
95 // closes the output file.
96 Callback
* cb
= new MakeCallback
<CommMonitor
,
97 &CommMonitor::closeStreams
>(this);
98 registerExitCallback(cb
);
102 "Created monitor %s with sample period %d ticks (%f ms)\n",
103 name(), samplePeriodTicks
, samplePeriod
* 1E3
);
106 CommMonitor::~CommMonitor()
108 // if not already done, close the stream
113 CommMonitor::closeStreams()
115 if (traceStream
!= NULL
)
120 CommMonitorParams::create()
122 return new CommMonitor(this);
128 // make sure both sides of the monitor are connected
129 if (!slavePort
.isConnected() || !masterPort
.isConnected())
130 fatal("Communication monitor is not connected on both sides.\n");
132 if (traceStream
!= NULL
) {
133 // Check the memory mode. We only record something when in
134 // timing mode. Warn accordingly.
135 if (!system
->isTimingMode())
136 warn("%s: Not in timing mode. No trace will be recorded.", name());
142 CommMonitor::regProbePoints()
144 ppPktReq
.reset(new ProbePoints::Packet(getProbeManager(), "PktRequest"));
145 ppPktResp
.reset(new ProbePoints::Packet(getProbeManager(), "PktResponse"));
149 CommMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
151 if (if_name
== "master") {
154 return MemObject::getMasterPort(if_name
, idx
);
159 CommMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
161 if (if_name
== "slave") {
164 return MemObject::getSlavePort(if_name
, idx
);
169 CommMonitor::recvFunctional(PacketPtr pkt
)
171 masterPort
.sendFunctional(pkt
);
175 CommMonitor::recvFunctionalSnoop(PacketPtr pkt
)
177 slavePort
.sendFunctionalSnoop(pkt
);
181 CommMonitor::recvAtomic(PacketPtr pkt
)
183 ppPktReq
->notify(pkt
);
185 // if tracing enabled, store the packet information
186 // to the trace stream
187 if (traceStream
!= NULL
) {
188 ProtoMessage::Packet pkt_msg
;
189 pkt_msg
.set_tick(curTick());
190 pkt_msg
.set_cmd(pkt
->cmdToIndex());
191 pkt_msg
.set_flags(pkt
->req
->getFlags());
192 pkt_msg
.set_addr(pkt
->getAddr());
193 pkt_msg
.set_size(pkt
->getSize());
195 traceStream
->write(pkt_msg
);
198 const Tick
delay(masterPort
.sendAtomic(pkt
));
199 assert(pkt
->isResponse());
200 ppPktResp
->notify(pkt
);
205 CommMonitor::recvAtomicSnoop(PacketPtr pkt
)
207 return slavePort
.sendAtomicSnoop(pkt
);
211 CommMonitor::recvTimingReq(PacketPtr pkt
)
213 // should always see a request
214 assert(pkt
->isRequest());
216 // Store relevant fields of packet, because packet may be modified
217 // or even deleted when sendTiming() is called.
218 const bool is_read
= pkt
->isRead();
219 const bool is_write
= pkt
->isWrite();
220 const MemCmd cmd
= pkt
->cmd
;
221 const int cmd_idx
= pkt
->cmdToIndex();
222 const Request::FlagsType req_flags
= pkt
->req
->getFlags();
223 const unsigned size
= pkt
->getSize();
224 const Addr addr
= pkt
->getAddr();
225 const bool expects_response(
226 pkt
->needsResponse() && !pkt
->memInhibitAsserted());
228 // If a cache miss is served by a cache, a monitor near the memory
229 // would see a request which needs a response, but this response
230 // would be inhibited and not come back from the memory. Therefore
231 // we additionally have to check the inhibit flag.
232 if (expects_response
&& !stats
.disableLatencyHists
) {
233 pkt
->pushSenderState(new CommMonitorSenderState(curTick()));
236 // Attempt to send the packet (always succeeds for inhibited
238 bool successful
= masterPort
.sendTimingReq(pkt
);
240 // If not successful, restore the sender state
241 if (!successful
&& expects_response
&& !stats
.disableLatencyHists
) {
242 delete pkt
->popSenderState();
246 // The receiver might already have modified the packet. We
247 // want to give the probe access to the original packet, which
248 // means we need to fake the original packet by temporarily
249 // restoring the command.
250 const MemCmd
response_cmd(pkt
->cmd
);
252 ppPktReq
->notify(pkt
);
253 pkt
->cmd
= response_cmd
;
256 if (successful
&& traceStream
!= NULL
) {
257 // Create a protobuf message representing the
258 // packet. Currently we do not preserve the flags in the
260 ProtoMessage::Packet pkt_msg
;
261 pkt_msg
.set_tick(curTick());
262 pkt_msg
.set_cmd(cmd_idx
);
263 pkt_msg
.set_flags(req_flags
);
264 pkt_msg
.set_addr(addr
);
265 pkt_msg
.set_size(size
);
267 traceStream
->write(pkt_msg
);
270 if (successful
&& is_read
) {
271 DPRINTF(CommMonitor
, "Forwarded read request\n");
273 // Increment number of observed read transactions
274 if (!stats
.disableTransactionHists
) {
278 // Get sample of burst length
279 if (!stats
.disableBurstLengthHists
) {
280 stats
.readBurstLengthHist
.sample(size
);
283 // Sample the masked address
284 if (!stats
.disableAddrDists
) {
285 stats
.readAddrDist
.sample(addr
& readAddrMask
);
288 // If it needs a response increment number of outstanding read
290 if (!stats
.disableOutstandingHists
&& expects_response
) {
291 ++stats
.outstandingReadReqs
;
294 if (!stats
.disableITTDists
) {
295 // Sample value of read-read inter transaction time
296 if (stats
.timeOfLastRead
!= 0) {
297 stats
.ittReadRead
.sample(curTick() - stats
.timeOfLastRead
);
299 stats
.timeOfLastRead
= curTick();
301 // Sample value of req-req inter transaction time
302 if (stats
.timeOfLastReq
!= 0) {
303 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
305 stats
.timeOfLastReq
= curTick();
307 } else if (successful
&& is_write
) {
308 DPRINTF(CommMonitor
, "Forwarded write request\n");
311 if (!stats
.disableTransactionHists
) {
315 if (!stats
.disableBurstLengthHists
) {
316 stats
.writeBurstLengthHist
.sample(size
);
319 // Update the bandwidth stats on the request
320 if (!stats
.disableBandwidthHists
) {
321 stats
.writtenBytes
+= size
;
322 stats
.totalWrittenBytes
+= size
;
325 // Sample the masked write address
326 if (!stats
.disableAddrDists
) {
327 stats
.writeAddrDist
.sample(addr
& writeAddrMask
);
330 if (!stats
.disableOutstandingHists
&& expects_response
) {
331 ++stats
.outstandingWriteReqs
;
334 if (!stats
.disableITTDists
) {
335 // Sample value of write-to-write inter transaction time
336 if (stats
.timeOfLastWrite
!= 0) {
337 stats
.ittWriteWrite
.sample(curTick() - stats
.timeOfLastWrite
);
339 stats
.timeOfLastWrite
= curTick();
341 // Sample value of req-to-req inter transaction time
342 if (stats
.timeOfLastReq
!= 0) {
343 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
345 stats
.timeOfLastReq
= curTick();
347 } else if (successful
) {
348 DPRINTF(CommMonitor
, "Forwarded non read/write request\n");
355 CommMonitor::recvTimingResp(PacketPtr pkt
)
357 // should always see responses
358 assert(pkt
->isResponse());
360 // Store relevant fields of packet, because packet may be modified
361 // or even deleted when sendTiming() is called.
362 bool is_read
= pkt
->isRead();
363 bool is_write
= pkt
->isWrite();
364 unsigned size
= pkt
->getSize();
366 CommMonitorSenderState
* received_state
=
367 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
369 if (!stats
.disableLatencyHists
) {
370 // Restore initial sender state
371 if (received_state
== NULL
)
372 panic("Monitor got a response without monitor sender state\n");
375 pkt
->senderState
= received_state
->predecessor
;
378 // Attempt to send the packet
379 bool successful
= slavePort
.sendTimingResp(pkt
);
381 if (!stats
.disableLatencyHists
) {
382 // If packet successfully send, sample value of latency,
383 // afterwards delete sender state, otherwise restore state
385 latency
= curTick() - received_state
->transmitTime
;
386 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
387 delete received_state
;
389 // Don't delete anything and let the packet look like we
391 pkt
->senderState
= received_state
;
396 assert(pkt
->isResponse());
397 ppPktResp
->notify(pkt
);
400 if (successful
&& is_read
) {
401 // Decrement number of outstanding read requests
402 DPRINTF(CommMonitor
, "Received read response\n");
403 if (!stats
.disableOutstandingHists
) {
404 assert(stats
.outstandingReadReqs
!= 0);
405 --stats
.outstandingReadReqs
;
408 if (!stats
.disableLatencyHists
) {
409 stats
.readLatencyHist
.sample(latency
);
412 // Update the bandwidth stats based on responses for reads
413 if (!stats
.disableBandwidthHists
) {
414 stats
.readBytes
+= size
;
415 stats
.totalReadBytes
+= size
;
418 } else if (successful
&& is_write
) {
419 // Decrement number of outstanding write requests
420 DPRINTF(CommMonitor
, "Received write response\n");
421 if (!stats
.disableOutstandingHists
) {
422 assert(stats
.outstandingWriteReqs
!= 0);
423 --stats
.outstandingWriteReqs
;
426 if (!stats
.disableLatencyHists
) {
427 stats
.writeLatencyHist
.sample(latency
);
429 } else if (successful
) {
430 DPRINTF(CommMonitor
, "Received non read/write response\n");
436 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
438 slavePort
.sendTimingSnoopReq(pkt
);
442 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
444 return masterPort
.sendTimingSnoopResp(pkt
);
448 CommMonitor::isSnooping() const
450 // check if the connected master port is snooping
451 return slavePort
.isSnooping();
455 CommMonitor::getAddrRanges() const
457 // get the address ranges of the connected slave port
458 return masterPort
.getAddrRanges();
462 CommMonitor::recvReqRetry()
464 slavePort
.sendRetryReq();
468 CommMonitor::recvRespRetry()
470 masterPort
.sendRetryResp();
474 CommMonitor::recvRangeChange()
476 slavePort
.sendRangeChange();
480 CommMonitor::regStats()
482 // Initialise all the monitor stats
483 using namespace Stats
;
485 stats
.readBurstLengthHist
486 .init(params()->burst_length_bins
)
487 .name(name() + ".readBurstLengthHist")
488 .desc("Histogram of burst lengths of transmitted packets")
489 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
491 stats
.writeBurstLengthHist
492 .init(params()->burst_length_bins
)
493 .name(name() + ".writeBurstLengthHist")
494 .desc("Histogram of burst lengths of transmitted packets")
495 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
497 // Stats based on received responses
498 stats
.readBandwidthHist
499 .init(params()->bandwidth_bins
)
500 .name(name() + ".readBandwidthHist")
501 .desc("Histogram of read bandwidth per sample period (bytes/s)")
502 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
505 .name(name() + ".averageReadBandwidth")
506 .desc("Average read bandwidth (bytes/s)")
507 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
510 .name(name() + ".totalReadBytes")
511 .desc("Number of bytes read")
512 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
514 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
516 // Stats based on successfully sent requests
517 stats
.writeBandwidthHist
518 .init(params()->bandwidth_bins
)
519 .name(name() + ".writeBandwidthHist")
520 .desc("Histogram of write bandwidth (bytes/s)")
521 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
524 .name(name() + ".averageWriteBandwidth")
525 .desc("Average write bandwidth (bytes/s)")
526 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
528 stats
.totalWrittenBytes
529 .name(name() + ".totalWrittenBytes")
530 .desc("Number of bytes written")
531 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
533 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
535 stats
.readLatencyHist
536 .init(params()->latency_bins
)
537 .name(name() + ".readLatencyHist")
538 .desc("Read request-response latency")
539 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
541 stats
.writeLatencyHist
542 .init(params()->latency_bins
)
543 .name(name() + ".writeLatencyHist")
544 .desc("Write request-response latency")
545 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
548 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
550 .name(name() + ".ittReadRead")
551 .desc("Read-to-read inter transaction time")
552 .flags(stats
.disableITTDists
? nozero
: pdf
);
555 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
557 .name(name() + ".ittWriteWrite")
558 .desc("Write-to-write inter transaction time")
559 .flags(stats
.disableITTDists
? nozero
: pdf
);
562 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
564 .name(name() + ".ittReqReq")
565 .desc("Request-to-request inter transaction time")
566 .flags(stats
.disableITTDists
? nozero
: pdf
);
568 stats
.outstandingReadsHist
569 .init(params()->outstanding_bins
)
570 .name(name() + ".outstandingReadsHist")
571 .desc("Outstanding read transactions")
572 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
574 stats
.outstandingWritesHist
575 .init(params()->outstanding_bins
)
576 .name(name() + ".outstandingWritesHist")
577 .desc("Outstanding write transactions")
578 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
581 .init(params()->transaction_bins
)
582 .name(name() + ".readTransHist")
583 .desc("Histogram of read transactions per sample period")
584 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
587 .init(params()->transaction_bins
)
588 .name(name() + ".writeTransHist")
589 .desc("Histogram of read transactions per sample period")
590 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
594 .name(name() + ".readAddrDist")
595 .desc("Read address distribution")
596 .flags(stats
.disableAddrDists
? nozero
: pdf
);
600 .name(name() + ".writeAddrDist")
601 .desc("Write address distribution")
602 .flags(stats
.disableAddrDists
? nozero
: pdf
);
606 CommMonitor::samplePeriodic()
608 // the periodic stats update runs on the granularity of sample
609 // periods, but in combination with this there may also be a
610 // external resets and dumps of the stats (through schedStatEvent)
611 // causing the stats themselves to capture less than a sample
614 // only capture if we have not reset the stats during the last
616 if (simTicks
.value() >= samplePeriodTicks
) {
617 if (!stats
.disableTransactionHists
) {
618 stats
.readTransHist
.sample(stats
.readTrans
);
619 stats
.writeTransHist
.sample(stats
.writeTrans
);
622 if (!stats
.disableBandwidthHists
) {
623 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
624 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
627 if (!stats
.disableOutstandingHists
) {
628 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
629 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
633 // reset the sampled values
635 stats
.writeTrans
= 0;
638 stats
.writtenBytes
= 0;
640 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
644 CommMonitor::startup()
646 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);