2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * Copyright (c) 2016 Google Inc.
4 * Copyright (c) 2017, Centre National de la Recherche Scientifique
7 * The license below extends only to copyright in the software and shall
8 * not be construed as granting a license to any other intellectual
9 * property including but not limited to intellectual property relating
10 * to a hardware implementation of the functionality of the software
11 * licensed hereunder. You may use the software subject to the license
12 * terms below provided that you ensure that this notice is replicated
13 * unmodified and in its entirety in all distributions of the software,
14 * modified or unmodified, in source code or in binary form.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
18 * met: redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer;
20 * redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution;
23 * neither the name of the copyright holders nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Authors: Thomas Grass
45 #include "mem/comm_monitor.hh"
47 #include "base/trace.hh"
48 #include "debug/CommMonitor.hh"
49 #include "sim/stats.hh"
51 CommMonitor::CommMonitor(Params
* params
)
53 masterPort(name() + "-master", *this),
54 slavePort(name() + "-slave", *this),
55 samplePeriodicEvent([this]{ samplePeriodic(); }, name()),
56 samplePeriodTicks(params
->sample_period
),
57 samplePeriod(params
->sample_period
/ SimClock::Float::s
),
61 "Created monitor %s with sample period %d ticks (%f ms)\n",
62 name(), samplePeriodTicks
, samplePeriod
* 1E3
);
66 CommMonitorParams::create()
68 return new CommMonitor(this);
74 // make sure both sides of the monitor are connected
75 if (!slavePort
.isConnected() || !masterPort
.isConnected())
76 fatal("Communication monitor is not connected on both sides.\n");
80 CommMonitor::regProbePoints()
82 ppPktReq
.reset(new ProbePoints::Packet(getProbeManager(), "PktRequest"));
83 ppPktResp
.reset(new ProbePoints::Packet(getProbeManager(), "PktResponse"));
87 CommMonitor::getMasterPort(const std::string
& if_name
, PortID idx
)
89 if (if_name
== "master") {
92 return MemObject::getMasterPort(if_name
, idx
);
97 CommMonitor::getSlavePort(const std::string
& if_name
, PortID idx
)
99 if (if_name
== "slave") {
102 return MemObject::getSlavePort(if_name
, idx
);
107 CommMonitor::recvFunctional(PacketPtr pkt
)
109 masterPort
.sendFunctional(pkt
);
113 CommMonitor::recvFunctionalSnoop(PacketPtr pkt
)
115 slavePort
.sendFunctionalSnoop(pkt
);
119 CommMonitor::MonitorStats::updateReqStats(
120 const ProbePoints::PacketInfo
& pkt_info
, bool is_atomic
,
121 bool expects_response
)
123 if (pkt_info
.cmd
.isRead()) {
124 // Increment number of observed read transactions
125 if (!disableTransactionHists
)
128 // Get sample of burst length
129 if (!disableBurstLengthHists
)
130 readBurstLengthHist
.sample(pkt_info
.size
);
132 // Sample the masked address
133 if (!disableAddrDists
)
134 readAddrDist
.sample(pkt_info
.addr
& readAddrMask
);
136 if (!disableITTDists
) {
137 // Sample value of read-read inter transaction time
138 if (timeOfLastRead
!= 0)
139 ittReadRead
.sample(curTick() - timeOfLastRead
);
140 timeOfLastRead
= curTick();
142 // Sample value of req-req inter transaction time
143 if (timeOfLastReq
!= 0)
144 ittReqReq
.sample(curTick() - timeOfLastReq
);
145 timeOfLastReq
= curTick();
147 if (!is_atomic
&& !disableOutstandingHists
&& expects_response
)
148 ++outstandingReadReqs
;
150 } else if (pkt_info
.cmd
.isWrite()) {
152 if (!disableTransactionHists
)
155 if (!disableBurstLengthHists
)
156 writeBurstLengthHist
.sample(pkt_info
.size
);
158 // Update the bandwidth stats on the request
159 if (!disableBandwidthHists
) {
160 writtenBytes
+= pkt_info
.size
;
161 totalWrittenBytes
+= pkt_info
.size
;
164 // Sample the masked write address
165 if (!disableAddrDists
)
166 writeAddrDist
.sample(pkt_info
.addr
& writeAddrMask
);
168 if (!disableITTDists
) {
169 // Sample value of write-to-write inter transaction time
170 if (timeOfLastWrite
!= 0)
171 ittWriteWrite
.sample(curTick() - timeOfLastWrite
);
172 timeOfLastWrite
= curTick();
174 // Sample value of req-to-req inter transaction time
175 if (timeOfLastReq
!= 0)
176 ittReqReq
.sample(curTick() - timeOfLastReq
);
177 timeOfLastReq
= curTick();
180 if (!is_atomic
&& !disableOutstandingHists
&& expects_response
)
181 ++outstandingWriteReqs
;
186 CommMonitor::MonitorStats::updateRespStats(
187 const ProbePoints::PacketInfo
& pkt_info
, Tick latency
, bool is_atomic
)
189 if (pkt_info
.cmd
.isRead()) {
190 // Decrement number of outstanding read requests
191 if (!is_atomic
&& !disableOutstandingHists
) {
192 assert(outstandingReadReqs
!= 0);
193 --outstandingReadReqs
;
196 if (!disableLatencyHists
)
197 readLatencyHist
.sample(latency
);
199 // Update the bandwidth stats based on responses for reads
200 if (!disableBandwidthHists
) {
201 readBytes
+= pkt_info
.size
;
202 totalReadBytes
+= pkt_info
.size
;
205 } else if (pkt_info
.cmd
.isWrite()) {
206 // Decrement number of outstanding write requests
207 if (!is_atomic
&& !disableOutstandingHists
) {
208 assert(outstandingWriteReqs
!= 0);
209 --outstandingWriteReqs
;
212 if (!disableLatencyHists
)
213 writeLatencyHist
.sample(latency
);
218 CommMonitor::recvAtomic(PacketPtr pkt
)
220 const bool expects_response(pkt
->needsResponse() &&
221 !pkt
->cacheResponding());
222 ProbePoints::PacketInfo
req_pkt_info(pkt
);
223 ppPktReq
->notify(req_pkt_info
);
225 const Tick
delay(masterPort
.sendAtomic(pkt
));
227 stats
.updateReqStats(req_pkt_info
, true, expects_response
);
228 if (expects_response
)
229 stats
.updateRespStats(req_pkt_info
, delay
, true);
231 assert(pkt
->isResponse());
232 ProbePoints::PacketInfo
resp_pkt_info(pkt
);
233 ppPktResp
->notify(resp_pkt_info
);
238 CommMonitor::recvAtomicSnoop(PacketPtr pkt
)
240 return slavePort
.sendAtomicSnoop(pkt
);
244 CommMonitor::recvTimingReq(PacketPtr pkt
)
246 // should always see a request
247 assert(pkt
->isRequest());
249 // Store relevant fields of packet, because packet may be modified
250 // or even deleted when sendTiming() is called.
251 const ProbePoints::PacketInfo
pkt_info(pkt
);
253 const bool expects_response(pkt
->needsResponse() &&
254 !pkt
->cacheResponding());
256 // If a cache miss is served by a cache, a monitor near the memory
257 // would see a request which needs a response, but this response
258 // would not come back from the memory. Therefore we additionally
259 // have to check the cacheResponding flag
260 if (expects_response
&& !stats
.disableLatencyHists
) {
261 pkt
->pushSenderState(new CommMonitorSenderState(curTick()));
264 // Attempt to send the packet
265 bool successful
= masterPort
.sendTimingReq(pkt
);
267 // If not successful, restore the sender state
268 if (!successful
&& expects_response
&& !stats
.disableLatencyHists
) {
269 delete pkt
->popSenderState();
273 ppPktReq
->notify(pkt_info
);
277 DPRINTF(CommMonitor
, "Forwarded %s request\n", pkt
->isRead() ? "read" :
278 pkt
->isWrite() ? "write" : "non read/write");
279 stats
.updateReqStats(pkt_info
, false, expects_response
);
285 CommMonitor::recvTimingResp(PacketPtr pkt
)
287 // should always see responses
288 assert(pkt
->isResponse());
290 // Store relevant fields of packet, because packet may be modified
291 // or even deleted when sendTiming() is called.
292 const ProbePoints::PacketInfo
pkt_info(pkt
);
295 CommMonitorSenderState
* received_state
=
296 dynamic_cast<CommMonitorSenderState
*>(pkt
->senderState
);
298 if (!stats
.disableLatencyHists
) {
299 // Restore initial sender state
300 if (received_state
== NULL
)
301 panic("Monitor got a response without monitor sender state\n");
304 pkt
->senderState
= received_state
->predecessor
;
307 // Attempt to send the packet
308 bool successful
= slavePort
.sendTimingResp(pkt
);
310 if (!stats
.disableLatencyHists
) {
311 // If packet successfully send, sample value of latency,
312 // afterwards delete sender state, otherwise restore state
314 latency
= curTick() - received_state
->transmitTime
;
315 DPRINTF(CommMonitor
, "Latency: %d\n", latency
);
316 delete received_state
;
318 // Don't delete anything and let the packet look like we
320 pkt
->senderState
= received_state
;
325 ppPktResp
->notify(pkt_info
);
326 DPRINTF(CommMonitor
, "Received %s response\n", pkt
->isRead() ? "read" :
327 pkt
->isWrite() ? "write" : "non read/write");
328 stats
.updateRespStats(pkt_info
, latency
, false);
334 CommMonitor::recvTimingSnoopReq(PacketPtr pkt
)
336 slavePort
.sendTimingSnoopReq(pkt
);
340 CommMonitor::recvTimingSnoopResp(PacketPtr pkt
)
342 return masterPort
.sendTimingSnoopResp(pkt
);
346 CommMonitor::recvRetrySnoopResp()
348 slavePort
.sendRetrySnoopResp();
352 CommMonitor::isSnooping() const
354 // check if the connected master port is snooping
355 return slavePort
.isSnooping();
359 CommMonitor::getAddrRanges() const
361 // get the address ranges of the connected slave port
362 return masterPort
.getAddrRanges();
366 CommMonitor::recvReqRetry()
368 slavePort
.sendRetryReq();
372 CommMonitor::recvRespRetry()
374 masterPort
.sendRetryResp();
378 CommMonitor::recvRangeChange()
380 slavePort
.sendRangeChange();
384 CommMonitor::regStats()
386 MemObject::regStats();
388 // Initialise all the monitor stats
389 using namespace Stats
;
391 stats
.readBurstLengthHist
392 .init(params()->burst_length_bins
)
393 .name(name() + ".readBurstLengthHist")
394 .desc("Histogram of burst lengths of transmitted packets")
395 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
397 stats
.writeBurstLengthHist
398 .init(params()->burst_length_bins
)
399 .name(name() + ".writeBurstLengthHist")
400 .desc("Histogram of burst lengths of transmitted packets")
401 .flags(stats
.disableBurstLengthHists
? nozero
: pdf
);
403 // Stats based on received responses
404 stats
.readBandwidthHist
405 .init(params()->bandwidth_bins
)
406 .name(name() + ".readBandwidthHist")
407 .desc("Histogram of read bandwidth per sample period (bytes/s)")
408 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
411 .name(name() + ".averageReadBandwidth")
412 .desc("Average read bandwidth (bytes/s)")
413 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
416 .name(name() + ".totalReadBytes")
417 .desc("Number of bytes read")
418 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
420 stats
.averageReadBW
= stats
.totalReadBytes
/ simSeconds
;
422 // Stats based on successfully sent requests
423 stats
.writeBandwidthHist
424 .init(params()->bandwidth_bins
)
425 .name(name() + ".writeBandwidthHist")
426 .desc("Histogram of write bandwidth (bytes/s)")
427 .flags(stats
.disableBandwidthHists
? (pdf
| nozero
) : pdf
);
430 .name(name() + ".averageWriteBandwidth")
431 .desc("Average write bandwidth (bytes/s)")
432 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
434 stats
.totalWrittenBytes
435 .name(name() + ".totalWrittenBytes")
436 .desc("Number of bytes written")
437 .flags(stats
.disableBandwidthHists
? nozero
: pdf
);
439 stats
.averageWriteBW
= stats
.totalWrittenBytes
/ simSeconds
;
441 stats
.readLatencyHist
442 .init(params()->latency_bins
)
443 .name(name() + ".readLatencyHist")
444 .desc("Read request-response latency")
445 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
447 stats
.writeLatencyHist
448 .init(params()->latency_bins
)
449 .name(name() + ".writeLatencyHist")
450 .desc("Write request-response latency")
451 .flags(stats
.disableLatencyHists
? nozero
: pdf
);
454 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
456 .name(name() + ".ittReadRead")
457 .desc("Read-to-read inter transaction time")
458 .flags(stats
.disableITTDists
? nozero
: pdf
);
461 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
463 .name(name() + ".ittWriteWrite")
464 .desc("Write-to-write inter transaction time")
465 .flags(stats
.disableITTDists
? nozero
: pdf
);
468 .init(1, params()->itt_max_bin
, params()->itt_max_bin
/
470 .name(name() + ".ittReqReq")
471 .desc("Request-to-request inter transaction time")
472 .flags(stats
.disableITTDists
? nozero
: pdf
);
474 stats
.outstandingReadsHist
475 .init(params()->outstanding_bins
)
476 .name(name() + ".outstandingReadsHist")
477 .desc("Outstanding read transactions")
478 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
480 stats
.outstandingWritesHist
481 .init(params()->outstanding_bins
)
482 .name(name() + ".outstandingWritesHist")
483 .desc("Outstanding write transactions")
484 .flags(stats
.disableOutstandingHists
? nozero
: pdf
);
487 .init(params()->transaction_bins
)
488 .name(name() + ".readTransHist")
489 .desc("Histogram of read transactions per sample period")
490 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
493 .init(params()->transaction_bins
)
494 .name(name() + ".writeTransHist")
495 .desc("Histogram of write transactions per sample period")
496 .flags(stats
.disableTransactionHists
? nozero
: pdf
);
500 .name(name() + ".readAddrDist")
501 .desc("Read address distribution")
502 .flags(stats
.disableAddrDists
? nozero
: pdf
);
506 .name(name() + ".writeAddrDist")
507 .desc("Write address distribution")
508 .flags(stats
.disableAddrDists
? nozero
: pdf
);
512 CommMonitor::samplePeriodic()
514 // the periodic stats update runs on the granularity of sample
515 // periods, but in combination with this there may also be a
516 // external resets and dumps of the stats (through schedStatEvent)
517 // causing the stats themselves to capture less than a sample
520 // only capture if we have not reset the stats during the last
522 if (simTicks
.value() >= samplePeriodTicks
) {
523 if (!stats
.disableTransactionHists
) {
524 stats
.readTransHist
.sample(stats
.readTrans
);
525 stats
.writeTransHist
.sample(stats
.writeTrans
);
528 if (!stats
.disableBandwidthHists
) {
529 stats
.readBandwidthHist
.sample(stats
.readBytes
/ samplePeriod
);
530 stats
.writeBandwidthHist
.sample(stats
.writtenBytes
/ samplePeriod
);
533 if (!stats
.disableOutstandingHists
) {
534 stats
.outstandingReadsHist
.sample(stats
.outstandingReadReqs
);
535 stats
.outstandingWritesHist
.sample(stats
.outstandingWriteReqs
);
539 // reset the sampled values
541 stats
.writeTrans
= 0;
544 stats
.writtenBytes
= 0;
546 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);
550 CommMonitor::startup()
552 schedule(samplePeriodicEvent
, curTick() + samplePeriodTicks
);