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 ProtoMessage::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());
108 CommMonitor::~CommMonitor()
110 // if not already done, close the stream
115 CommMonitor::closeStreams()
117 if (traceStream
!= NULL
)
122 CommMonitorParams::create()
124 return new CommMonitor(this);
130 // make sure both sides of the monitor are connected
131 if (!slavePort
.isConnected() || !masterPort
.isConnected())
132 fatal("Communication monitor is not connected on both sides.\n");
134 if (traceStream
!= NULL
) {
135 // Check the memory mode. We only record something when in
136 // timing mode. Warn accordingly.
137 if (!system
->isTimingMode())
138 warn("%s: Not in timing mode. No trace will be recorded.", name());
143 CommMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
145 if (if_name
== "master") {
148 return MemObject::getMasterPort(if_name
, idx
);
153 CommMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
155 if (if_name
== "slave") {
158 return MemObject::getSlavePort(if_name
, idx
);
163 CommMonitor::recvFunctional(PacketPtr pkt
)
165 masterPort
.sendFunctional(pkt
);
169 CommMonitor::recvFunctionalSnoop(PacketPtr pkt
)
171 slavePort
.sendFunctionalSnoop(pkt
);
175 CommMonitor::recvAtomic(PacketPtr pkt
)
177 return masterPort
.sendAtomic(pkt
);
181 CommMonitor::recvAtomicSnoop(PacketPtr pkt
)
183 return slavePort
.sendAtomicSnoop(pkt
);
187 CommMonitor::recvTimingReq(PacketPtr pkt
)
189 // should always see a request
190 assert(pkt
->isRequest());
192 // Store relevant fields of packet, because packet may be modified
193 // or even deleted when sendTiming() is called.
194 bool is_read
= pkt
->isRead();
195 bool is_write
= pkt
->isWrite();
196 int cmd
= pkt
->cmdToIndex();
197 Request::FlagsType req_flags
= pkt
->req
->getFlags();
198 unsigned size
= pkt
->getSize();
199 Addr addr
= pkt
->getAddr();
200 bool expects_response
= pkt
->needsResponse() && !pkt
->memInhibitAsserted();
202 // If a cache miss is served by a cache, a monitor near the memory
203 // would see a request which needs a response, but this response
204 // would be inhibited and not come back from the memory. Therefore
205 // we additionally have to check the inhibit flag.
206 if (expects_response
&& !stats
.disableLatencyHists
) {
207 pkt
->pushSenderState(new CommMonitorSenderState(curTick()));
210 // Attempt to send the packet (always succeeds for inhibited
212 bool successful
= masterPort
.sendTimingReq(pkt
);
214 // If not successful, restore the sender state
215 if (!successful
&& expects_response
&& !stats
.disableLatencyHists
) {
216 delete pkt
->popSenderState();
219 if (successful
&& traceStream
!= NULL
) {
220 // Create a protobuf message representing the
221 // packet. Currently we do not preserve the flags in the
223 ProtoMessage::Packet pkt_msg
;
224 pkt_msg
.set_tick(curTick());
225 pkt_msg
.set_cmd(cmd
);
226 pkt_msg
.set_flags(req_flags
);
227 pkt_msg
.set_addr(addr
);
228 pkt_msg
.set_size(size
);
230 traceStream
->write(pkt_msg
);
233 if (successful
&& is_read
) {
234 DPRINTF(CommMonitor
, "Forwarded read request\n");
236 // Increment number of observed read transactions
237 if (!stats
.disableTransactionHists
) {
241 // Get sample of burst length
242 if (!stats
.disableBurstLengthHists
) {
243 stats
.readBurstLengthHist
.sample(size
);
246 // Sample the masked address
247 if (!stats
.disableAddrDists
) {
248 stats
.readAddrDist
.sample(addr
& readAddrMask
);
251 // If it needs a response increment number of outstanding read
253 if (!stats
.disableOutstandingHists
&& expects_response
) {
254 ++stats
.outstandingReadReqs
;
257 if (!stats
.disableITTDists
) {
258 // Sample value of read-read inter transaction time
259 if (stats
.timeOfLastRead
!= 0) {
260 stats
.ittReadRead
.sample(curTick() - stats
.timeOfLastRead
);
262 stats
.timeOfLastRead
= curTick();
264 // Sample value of req-req inter transaction time
265 if (stats
.timeOfLastReq
!= 0) {
266 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
268 stats
.timeOfLastReq
= curTick();
270 } else if (successful
&& is_write
) {
271 DPRINTF(CommMonitor
, "Forwarded write request\n");
274 if (!stats
.disableTransactionHists
) {
278 if (!stats
.disableBurstLengthHists
) {
279 stats
.writeBurstLengthHist
.sample(size
);
282 // Update the bandwidth stats on the request
283 if (!stats
.disableBandwidthHists
) {
284 stats
.writtenBytes
+= size
;
285 stats
.totalWrittenBytes
+= size
;
288 // Sample the masked write address
289 if (!stats
.disableAddrDists
) {
290 stats
.writeAddrDist
.sample(addr
& writeAddrMask
);
293 if (!stats
.disableOutstandingHists
&& expects_response
) {
294 ++stats
.outstandingWriteReqs
;
297 if (!stats
.disableITTDists
) {
298 // Sample value of write-to-write inter transaction time
299 if (stats
.timeOfLastWrite
!= 0) {
300 stats
.ittWriteWrite
.sample(curTick() - stats
.timeOfLastWrite
);
302 stats
.timeOfLastWrite
= curTick();
304 // Sample value of req-to-req inter transaction time
305 if (stats
.timeOfLastReq
!= 0) {
306 stats
.ittReqReq
.sample(curTick() - stats
.timeOfLastReq
);
308 stats
.timeOfLastReq
= curTick();
310 } else if (successful
) {
311 DPRINTF(CommMonitor
, "Forwarded non read/write request\n");
318 CommMonitor::recvTimingResp(PacketPtr pkt
)
320 // should always see responses
321 assert(pkt
->isResponse());
323 // Store relevant fields of packet, because packet may be modified
324 // or even deleted when sendTiming() is called.
325 bool is_read
= pkt
->isRead();
326 bool is_write
= pkt
->isWrite();
327 unsigned size
= pkt
->getSize();
329 CommMonitorSenderState
* received_state
=
330 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
332 if (!stats
.disableLatencyHists
) {
333 // Restore initial sender state
334 if (received_state
== NULL
)
335 panic("Monitor got a response without monitor sender state\n");
338 pkt
->senderState
= received_state
->predecessor
;
341 // Attempt to send the packet
342 bool successful
= slavePort
.sendTimingResp(pkt
);
344 if (!stats
.disableLatencyHists
) {
345 // If packet successfully send, sample value of latency,
346 // afterwards delete sender state, otherwise restore state
348 latency
= curTick() - received_state
->transmitTime
;
349 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
350 delete received_state
;
352 // Don't delete anything and let the packet look like we
354 pkt
->senderState
= received_state
;
358 if (successful
&& is_read
) {
359 // Decrement number of outstanding read requests
360 DPRINTF(CommMonitor
, "Received read response\n");
361 if (!stats
.disableOutstandingHists
) {
362 assert(stats
.outstandingReadReqs
!= 0);
363 --stats
.outstandingReadReqs
;
366 if (!stats
.disableLatencyHists
) {
367 stats
.readLatencyHist
.sample(latency
);
370 // Update the bandwidth stats based on responses for reads
371 if (!stats
.disableBandwidthHists
) {
372 stats
.readBytes
+= size
;
373 stats
.totalReadBytes
+= size
;
376 } else if (successful
&& is_write
) {
377 // Decrement number of outstanding write requests
378 DPRINTF(CommMonitor
, "Received write response\n");
379 if (!stats
.disableOutstandingHists
) {
380 assert(stats
.outstandingWriteReqs
!= 0);
381 --stats
.outstandingWriteReqs
;
384 if (!stats
.disableLatencyHists
) {
385 stats
.writeLatencyHist
.sample(latency
);
387 } else if (successful
) {
388 DPRINTF(CommMonitor
, "Received non read/write response\n");
394 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
396 slavePort
.sendTimingSnoopReq(pkt
);
400 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
402 return masterPort
.sendTimingSnoopResp(pkt
);
406 CommMonitor::isSnooping() const
408 // check if the connected master port is snooping
409 return slavePort
.isSnooping();
413 CommMonitor::getAddrRanges() const
415 // get the address ranges of the connected slave port
416 return masterPort
.getAddrRanges();
420 CommMonitor::recvRetryMaster()
422 slavePort
.sendRetry();
426 CommMonitor::recvRetrySlave()
428 masterPort
.sendRetry();
432 CommMonitor::recvRangeChange()
434 slavePort
.sendRangeChange();
438 CommMonitor::regStats()
440 // Initialise all the monitor stats
441 using namespace Stats
;
443 stats
.readBurstLengthHist
444 .init(params()->burst_length_bins
)
445 .name(name() + ".readBurstLengthHist")
446 .desc("Histogram of burst lengths of transmitted packets")
447 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
449 stats
.writeBurstLengthHist
450 .init(params()->burst_length_bins
)
451 .name(name() + ".writeBurstLengthHist")
452 .desc("Histogram of burst lengths of transmitted packets")
453 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
455 // Stats based on received responses
456 stats
.readBandwidthHist
457 .init(params()->bandwidth_bins
)
458 .name(name() + ".readBandwidthHist")
459 .desc("Histogram of read bandwidth per sample period (bytes/s)")
460 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
463 .name(name() + ".averageReadBandwidth")
464 .desc("Average read bandwidth (bytes/s)")
465 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
468 .name(name() + ".totalReadBytes")
469 .desc("Number of bytes read")
470 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
472 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
474 // Stats based on successfully sent requests
475 stats
.writeBandwidthHist
476 .init(params()->bandwidth_bins
)
477 .name(name() + ".writeBandwidthHist")
478 .desc("Histogram of write bandwidth (bytes/s)")
479 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
482 .name(name() + ".averageWriteBandwidth")
483 .desc("Average write bandwidth (bytes/s)")
484 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
486 stats
.totalWrittenBytes
487 .name(name() + ".totalWrittenBytes")
488 .desc("Number of bytes written")
489 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
491 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
493 stats
.readLatencyHist
494 .init(params()->latency_bins
)
495 .name(name() + ".readLatencyHist")
496 .desc("Read request-response latency")
497 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
499 stats
.writeLatencyHist
500 .init(params()->latency_bins
)
501 .name(name() + ".writeLatencyHist")
502 .desc("Write request-response latency")
503 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
506 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
508 .name(name() + ".ittReadRead")
509 .desc("Read-to-read inter transaction time")
510 .flags(stats
.disableITTDists
? nozero
: pdf
);
513 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
515 .name(name() + ".ittWriteWrite")
516 .desc("Write-to-write inter transaction time")
517 .flags(stats
.disableITTDists
? nozero
: pdf
);
520 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
522 .name(name() + ".ittReqReq")
523 .desc("Request-to-request inter transaction time")
524 .flags(stats
.disableITTDists
? nozero
: pdf
);
526 stats
.outstandingReadsHist
527 .init(params()->outstanding_bins
)
528 .name(name() + ".outstandingReadsHist")
529 .desc("Outstanding read transactions")
530 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
532 stats
.outstandingWritesHist
533 .init(params()->outstanding_bins
)
534 .name(name() + ".outstandingWritesHist")
535 .desc("Outstanding write transactions")
536 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
539 .init(params()->transaction_bins
)
540 .name(name() + ".readTransHist")
541 .desc("Histogram of read transactions per sample period")
542 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
545 .init(params()->transaction_bins
)
546 .name(name() + ".writeTransHist")
547 .desc("Histogram of read transactions per sample period")
548 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
552 .name(name() + ".readAddrDist")
553 .desc("Read address distribution")
554 .flags(stats
.disableAddrDists
? nozero
: pdf
);
558 .name(name() + ".writeAddrDist")
559 .desc("Write address distribution")
560 .flags(stats
.disableAddrDists
? nozero
: pdf
);
564 CommMonitor::samplePeriodic()
566 // the periodic stats update runs on the granularity of sample
567 // periods, but in combination with this there may also be a
568 // external resets and dumps of the stats (through schedStatEvent)
569 // causing the stats themselves to capture less than a sample
572 // only capture if we have not reset the stats during the last
574 if (simTicks
.value() >= samplePeriodTicks
) {
575 if (!stats
.disableTransactionHists
) {
576 stats
.readTransHist
.sample(stats
.readTrans
);
577 stats
.writeTransHist
.sample(stats
.writeTrans
);
580 if (!stats
.disableBandwidthHists
) {
581 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
582 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
585 if (!stats
.disableOutstandingHists
) {
586 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
587 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
591 // reset the sampled values
593 stats
.writeTrans
= 0;
596 stats
.writtenBytes
= 0;
598 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
602 CommMonitor::startup()
604 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);