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
),
59 system(params
->system
)
61 // If we are using a trace file, then open the file
62 if (params
->trace_enable
) {
64 if (params
->trace_file
!= "") {
65 // If the trace file is not specified as an absolute path,
66 // append the current simulation output directory
67 filename
= simout
.resolve(params
->trace_file
);
69 std::string suffix
= ".gz";
70 // If trace_compress has been set, check the suffix. Append
72 if (params
->trace_compress
&&
73 filename
.compare(filename
.size() - suffix
.size(), suffix
.size(),
75 filename
= filename
+ suffix
;
77 // Generate a filename from the name of the SimObject. Append .trc
78 // and .gz if we want compression enabled.
79 filename
= simout
.resolve(name() + ".trc" +
80 (params
->trace_compress
? ".gz" : ""));
83 traceStream
= new ProtoOutputStream(filename
);
85 // Create a protobuf message for the header and write it to
87 Message::PacketHeader header_msg
;
88 header_msg
.set_obj_id(name());
89 header_msg
.set_tick_freq(SimClock::Frequency
);
90 traceStream
->write(header_msg
);
92 // Register a callback to compensate for the destructor not
93 // being called. The callback forces the stream to flush and
94 // closes the output file.
95 Callback
* cb
= new MakeCallback
<CommMonitor
,
96 &CommMonitor::closeStreams
>(this);
97 registerExitCallback(cb
);
100 // keep track of the sample period both in ticks and absolute time
101 samplePeriod
.setTick(params
->sample_period
);
104 "Created monitor %s with sample period %d ticks (%f ms)\n",
105 name(), samplePeriodTicks
, samplePeriod
.msec());
109 CommMonitor::closeStreams()
111 if (traceStream
!= NULL
)
116 CommMonitorParams::create()
118 return new CommMonitor(this);
124 // make sure both sides of the monitor are connected
125 if (!slavePort
.isConnected() || !masterPort
.isConnected())
126 fatal("Communication monitor is not connected on both sides.\n");
128 if (traceStream
!= NULL
) {
129 // Check the memory mode. We only record something when in
130 // timing mode. Warn accordingly.
131 if (!system
->isTimingMode())
132 warn("%s: Not in timing mode. No trace will be recorded.", name());
137 CommMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
139 if (if_name
== "master") {
142 return MemObject::getMasterPort(if_name
, idx
);
147 CommMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
149 if (if_name
== "slave") {
152 return MemObject::getSlavePort(if_name
, idx
);
157 CommMonitor::recvFunctional(PacketPtr pkt
)
159 masterPort
.sendFunctional(pkt
);
163 CommMonitor::recvFunctionalSnoop(PacketPtr pkt
)
165 slavePort
.sendFunctionalSnoop(pkt
);
169 CommMonitor::recvAtomic(PacketPtr pkt
)
171 return masterPort
.sendAtomic(pkt
);
175 CommMonitor::recvAtomicSnoop(PacketPtr pkt
)
177 return slavePort
.sendAtomicSnoop(pkt
);
181 CommMonitor::recvTimingReq(PacketPtr pkt
)
183 // should always see a request
184 assert(pkt
->isRequest());
186 // Store relevant fields of packet, because packet may be modified
187 // or even deleted when sendTiming() is called.
188 bool is_read
= pkt
->isRead();
189 bool is_write
= pkt
->isWrite();
190 int cmd
= pkt
->cmdToIndex();
191 Request::FlagsType req_flags
= pkt
->req
->getFlags();
192 unsigned size
= pkt
->getSize();
193 Addr addr
= pkt
->getAddr();
194 bool expects_response
= pkt
->needsResponse() && !pkt
->memInhibitAsserted();
196 // If a cache miss is served by a cache, a monitor near the memory
197 // would see a request which needs a response, but this response
198 // would be inhibited and not come back from the memory. Therefore
199 // we additionally have to check the inhibit flag.
200 if (expects_response
&& !stats
.disableLatencyHists
) {
201 pkt
->pushSenderState(new CommMonitorSenderState(curTick()));
204 // Attempt to send the packet (always succeeds for inhibited
206 bool successful
= masterPort
.sendTimingReq(pkt
);
208 // If not successful, restore the sender state
209 if (!successful
&& expects_response
&& !stats
.disableLatencyHists
) {
210 delete pkt
->popSenderState();
213 if (successful
&& traceStream
!= NULL
) {
214 // Create a protobuf message representing the
215 // packet. Currently we do not preserve the flags in the
217 Message::Packet pkt_msg
;
218 pkt_msg
.set_tick(curTick());
219 pkt_msg
.set_cmd(cmd
);
220 pkt_msg
.set_flags(req_flags
);
221 pkt_msg
.set_addr(addr
);
222 pkt_msg
.set_size(size
);
224 traceStream
->write(pkt_msg
);
227 if (successful
&& is_read
) {
228 DPRINTF(CommMonitor
, "Forwarded read request\n");
230 // Increment number of observed read transactions
231 if (!stats
.disableTransactionHists
) {
235 // Get sample of burst length
236 if (!stats
.disableBurstLengthHists
) {
237 stats
.readBurstLengthHist
.sample(size
);
240 // Sample the masked address
241 if (!stats
.disableAddrDists
) {
242 stats
.readAddrDist
.sample(addr
& readAddrMask
);
245 // If it needs a response increment number of outstanding read
247 if (!stats
.disableOutstandingHists
&& expects_response
) {
248 ++stats
.outstandingReadReqs
;
251 if (!stats
.disableITTDists
) {
252 // Sample value of read-read inter transaction time
253 if (stats
.timeOfLastRead
!= 0) {
254 stats
.ittReadRead
.sample(curTick() - stats
.timeOfLastRead
);
256 stats
.timeOfLastRead
= curTick();
258 // Sample value of req-req inter transaction time
259 if (stats
.timeOfLastReq
!= 0) {
260 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
262 stats
.timeOfLastReq
= curTick();
264 } else if (successful
&& is_write
) {
265 DPRINTF(CommMonitor
, "Forwarded write request\n");
268 if (!stats
.disableTransactionHists
) {
272 if (!stats
.disableBurstLengthHists
) {
273 stats
.writeBurstLengthHist
.sample(size
);
276 // Update the bandwidth stats on the request
277 if (!stats
.disableBandwidthHists
) {
278 stats
.writtenBytes
+= size
;
279 stats
.totalWrittenBytes
+= size
;
282 // Sample the masked write address
283 if (!stats
.disableAddrDists
) {
284 stats
.writeAddrDist
.sample(addr
& writeAddrMask
);
287 if (!stats
.disableOutstandingHists
&& expects_response
) {
288 ++stats
.outstandingWriteReqs
;
291 if (!stats
.disableITTDists
) {
292 // Sample value of write-to-write inter transaction time
293 if (stats
.timeOfLastWrite
!= 0) {
294 stats
.ittWriteWrite
.sample(curTick() - stats
.timeOfLastWrite
);
296 stats
.timeOfLastWrite
= curTick();
298 // Sample value of req-to-req inter transaction time
299 if (stats
.timeOfLastReq
!= 0) {
300 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
302 stats
.timeOfLastReq
= curTick();
304 } else if (successful
) {
305 DPRINTF(CommMonitor
, "Forwarded non read/write request\n");
312 CommMonitor::recvTimingResp(PacketPtr pkt
)
314 // should always see responses
315 assert(pkt
->isResponse());
317 // Store relevant fields of packet, because packet may be modified
318 // or even deleted when sendTiming() is called.
319 bool is_read
= pkt
->isRead();
320 bool is_write
= pkt
->isWrite();
321 unsigned size
= pkt
->getSize();
323 CommMonitorSenderState
* received_state
=
324 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
326 if (!stats
.disableLatencyHists
) {
327 // Restore initial sender state
328 if (received_state
== NULL
)
329 panic("Monitor got a response without monitor sender state\n");
332 pkt
->senderState
= received_state
->predecessor
;
335 // Attempt to send the packet
336 bool successful
= slavePort
.sendTimingResp(pkt
);
338 if (!stats
.disableLatencyHists
) {
339 // If packet successfully send, sample value of latency,
340 // afterwards delete sender state, otherwise restore state
342 latency
= curTick() - received_state
->transmitTime
;
343 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
344 delete received_state
;
346 // Don't delete anything and let the packet look like we
348 pkt
->senderState
= received_state
;
352 if (successful
&& is_read
) {
353 // Decrement number of outstanding read requests
354 DPRINTF(CommMonitor
, "Received read response\n");
355 if (!stats
.disableOutstandingHists
) {
356 assert(stats
.outstandingReadReqs
!= 0);
357 --stats
.outstandingReadReqs
;
360 if (!stats
.disableLatencyHists
) {
361 stats
.readLatencyHist
.sample(latency
);
364 // Update the bandwidth stats based on responses for reads
365 if (!stats
.disableBandwidthHists
) {
366 stats
.readBytes
+= size
;
367 stats
.totalReadBytes
+= size
;
370 } else if (successful
&& is_write
) {
371 // Decrement number of outstanding write requests
372 DPRINTF(CommMonitor
, "Received write response\n");
373 if (!stats
.disableOutstandingHists
) {
374 assert(stats
.outstandingWriteReqs
!= 0);
375 --stats
.outstandingWriteReqs
;
378 if (!stats
.disableLatencyHists
) {
379 stats
.writeLatencyHist
.sample(latency
);
381 } else if (successful
) {
382 DPRINTF(CommMonitor
, "Received non read/write response\n");
388 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
390 slavePort
.sendTimingSnoopReq(pkt
);
394 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
396 return masterPort
.sendTimingSnoopResp(pkt
);
400 CommMonitor::isSnooping() const
402 // check if the connected master port is snooping
403 return slavePort
.isSnooping();
407 CommMonitor::getAddrRanges() const
409 // get the address ranges of the connected slave port
410 return masterPort
.getAddrRanges();
414 CommMonitor::recvRetryMaster()
416 slavePort
.sendRetry();
420 CommMonitor::recvRetrySlave()
422 masterPort
.sendRetry();
426 CommMonitor::recvRangeChange()
428 slavePort
.sendRangeChange();
432 CommMonitor::regStats()
434 // Initialise all the monitor stats
435 using namespace Stats
;
437 stats
.readBurstLengthHist
438 .init(params()->burst_length_bins
)
439 .name(name() + ".readBurstLengthHist")
440 .desc("Histogram of burst lengths of transmitted packets")
441 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
443 stats
.writeBurstLengthHist
444 .init(params()->burst_length_bins
)
445 .name(name() + ".writeBurstLengthHist")
446 .desc("Histogram of burst lengths of transmitted packets")
447 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
449 // Stats based on received responses
450 stats
.readBandwidthHist
451 .init(params()->bandwidth_bins
)
452 .name(name() + ".readBandwidthHist")
453 .desc("Histogram of read bandwidth per sample period (bytes/s)")
454 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
457 .name(name() + ".averageReadBandwidth")
458 .desc("Average read bandwidth (bytes/s)")
459 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
462 .name(name() + ".totalReadBytes")
463 .desc("Number of bytes read")
464 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
466 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
468 // Stats based on successfully sent requests
469 stats
.writeBandwidthHist
470 .init(params()->bandwidth_bins
)
471 .name(name() + ".writeBandwidthHist")
472 .desc("Histogram of write bandwidth (bytes/s)")
473 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
476 .name(name() + ".averageWriteBandwidth")
477 .desc("Average write bandwidth (bytes/s)")
478 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
480 stats
.totalWrittenBytes
481 .name(name() + ".totalWrittenBytes")
482 .desc("Number of bytes written")
483 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
485 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
487 stats
.readLatencyHist
488 .init(params()->latency_bins
)
489 .name(name() + ".readLatencyHist")
490 .desc("Read request-response latency")
491 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
493 stats
.writeLatencyHist
494 .init(params()->latency_bins
)
495 .name(name() + ".writeLatencyHist")
496 .desc("Write request-response latency")
497 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
500 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
502 .name(name() + ".ittReadRead")
503 .desc("Read-to-read inter transaction time")
504 .flags(stats
.disableITTDists
? nozero
: pdf
);
507 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
509 .name(name() + ".ittWriteWrite")
510 .desc("Write-to-write inter transaction time")
511 .flags(stats
.disableITTDists
? nozero
: pdf
);
514 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
516 .name(name() + ".ittReqReq")
517 .desc("Request-to-request inter transaction time")
518 .flags(stats
.disableITTDists
? nozero
: pdf
);
520 stats
.outstandingReadsHist
521 .init(params()->outstanding_bins
)
522 .name(name() + ".outstandingReadsHist")
523 .desc("Outstanding read transactions")
524 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
526 stats
.outstandingWritesHist
527 .init(params()->outstanding_bins
)
528 .name(name() + ".outstandingWritesHist")
529 .desc("Outstanding write transactions")
530 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
533 .init(params()->transaction_bins
)
534 .name(name() + ".readTransHist")
535 .desc("Histogram of read transactions per sample period")
536 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
539 .init(params()->transaction_bins
)
540 .name(name() + ".writeTransHist")
541 .desc("Histogram of read transactions per sample period")
542 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
546 .name(name() + ".readAddrDist")
547 .desc("Read address distribution")
548 .flags(stats
.disableAddrDists
? nozero
: pdf
);
552 .name(name() + ".writeAddrDist")
553 .desc("Write address distribution")
554 .flags(stats
.disableAddrDists
? nozero
: pdf
);
558 CommMonitor::samplePeriodic()
560 // the periodic stats update runs on the granularity of sample
561 // periods, but in combination with this there may also be a
562 // external resets and dumps of the stats (through schedStatEvent)
563 // causing the stats themselves to capture less than a sample
566 // only capture if we have not reset the stats during the last
568 if (simTicks
.value() >= samplePeriodTicks
) {
569 if (!stats
.disableTransactionHists
) {
570 stats
.readTransHist
.sample(stats
.readTrans
);
571 stats
.writeTransHist
.sample(stats
.writeTrans
);
574 if (!stats
.disableBandwidthHists
) {
575 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
576 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
579 if (!stats
.disableOutstandingHists
) {
580 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
581 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
585 // reset the sampled values
587 stats
.writeTrans
= 0;
590 stats
.writtenBytes
= 0;
592 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
596 CommMonitor::startup()
598 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);