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 #ifndef __MEM_COMM_MONITOR_HH__
42 #define __MEM_COMM_MONITOR_HH__
44 #include "base/statistics.hh"
45 #include "base/time.hh"
46 #include "mem/mem_object.hh"
47 #include "mem/stack_dist_calc.hh"
48 #include "params/CommMonitor.hh"
49 #include "proto/protoio.hh"
50 #include "sim/system.hh"
53 * The communication monitor is a MemObject which can monitor statistics of
54 * the communication happening between two ports in the memory system.
56 * Currently the following stats are implemented: Histograms of read/write
57 * transactions, read/write burst lengths, read/write bandwidth,
58 * outstanding read/write requests, read latency and inter transaction time
59 * (read-read, write-write, read/write-read/write). Furthermore it allows
60 * to capture the number of accesses to an address over time ("heat map").
61 * All stats can be disabled from Python.
63 class CommMonitor : public MemObject
68 /** Parameters of communication monitor */
69 typedef CommMonitorParams Params;
70 const Params* params() const
71 { return reinterpret_cast<const Params*>(_params); }
74 * Constructor based on the Python params
76 * @param params Python parameters
78 CommMonitor(Params* params);
84 * Callback to flush and close all open output streams on exit. If
85 * we were calling the destructor it could be done there.
89 virtual BaseMasterPort& getMasterPort(const std::string& if_name,
90 PortID idx = InvalidPortID);
92 virtual BaseSlavePort& getSlavePort(const std::string& if_name,
93 PortID idx = InvalidPortID);
97 /** Register statistics */
103 * Sender state class for the monitor so that we can annotate
104 * packets with a transmit time and receive time.
106 class CommMonitorSenderState : public Packet::SenderState
112 * Construct a new sender state and store the time so we can
113 * calculate round-trip latency.
115 * @param _transmitTime Time of packet transmission
117 CommMonitorSenderState(Tick _transmitTime)
118 : transmitTime(_transmitTime)
122 ~CommMonitorSenderState() { }
124 /** Tick when request is transmitted */
130 * This is the master port of the communication monitor. All recv
131 * functions call a function in CommMonitor, where the
132 * send function of the slave port is called. Besides this, these
133 * functions can also perform actions for capturing statistics.
135 class MonitorMasterPort : public MasterPort
140 MonitorMasterPort(const std::string& _name, CommMonitor& _mon)
141 : MasterPort(_name, &_mon), mon(_mon)
146 void recvFunctionalSnoop(PacketPtr pkt)
148 mon.recvFunctionalSnoop(pkt);
151 Tick recvAtomicSnoop(PacketPtr pkt)
153 return mon.recvAtomicSnoop(pkt);
156 bool recvTimingResp(PacketPtr pkt)
158 return mon.recvTimingResp(pkt);
161 void recvTimingSnoopReq(PacketPtr pkt)
163 mon.recvTimingSnoopReq(pkt);
166 void recvRangeChange()
168 mon.recvRangeChange();
171 bool isSnooping() const
173 return mon.isSnooping();
178 mon.recvRetryMaster();
187 /** Instance of master port, facing the memory side */
188 MonitorMasterPort masterPort;
191 * This is the slave port of the communication monitor. All recv
192 * functions call a function in CommMonitor, where the
193 * send function of the master port is called. Besides this, these
194 * functions can also perform actions for capturing statistics.
196 class MonitorSlavePort : public SlavePort
201 MonitorSlavePort(const std::string& _name, CommMonitor& _mon)
202 : SlavePort(_name, &_mon), mon(_mon)
207 void recvFunctional(PacketPtr pkt)
209 mon.recvFunctional(pkt);
212 Tick recvAtomic(PacketPtr pkt)
214 return mon.recvAtomic(pkt);
217 bool recvTimingReq(PacketPtr pkt)
219 return mon.recvTimingReq(pkt);
222 bool recvTimingSnoopResp(PacketPtr pkt)
224 return mon.recvTimingSnoopResp(pkt);
227 AddrRangeList getAddrRanges() const
229 return mon.getAddrRanges();
234 mon.recvRetrySlave();
243 /** Instance of slave port, i.e. on the CPU side */
244 MonitorSlavePort slavePort;
246 void recvFunctional(PacketPtr pkt);
248 void recvFunctionalSnoop(PacketPtr pkt);
250 Tick recvAtomic(PacketPtr pkt);
252 Tick recvAtomicSnoop(PacketPtr pkt);
254 bool recvTimingReq(PacketPtr pkt);
256 bool recvTimingResp(PacketPtr pkt);
258 void recvTimingSnoopReq(PacketPtr pkt);
260 bool recvTimingSnoopResp(PacketPtr pkt);
262 AddrRangeList getAddrRanges() const;
264 bool isSnooping() const;
266 void recvRetryMaster();
268 void recvRetrySlave();
270 void recvRangeChange();
272 /** Stats declarations, all in a struct for convenience. */
276 /** Disable flag for burst length historgrams **/
277 bool disableBurstLengthHists;
279 /** Histogram of read burst lengths */
280 Stats::Histogram readBurstLengthHist;
282 /** Histogram of write burst lengths */
283 Stats::Histogram writeBurstLengthHist;
285 /** Disable flag for the bandwidth histograms */
286 bool disableBandwidthHists;
289 * Histogram for read bandwidth per sample window. The
290 * internal counter is an unsigned int rather than a stat.
292 unsigned int readBytes;
293 Stats::Histogram readBandwidthHist;
294 Stats::Formula averageReadBW;
295 Stats::Scalar totalReadBytes;
298 * Histogram for write bandwidth per sample window. The
299 * internal counter is an unsigned int rather than a stat.
301 unsigned int writtenBytes;
302 Stats::Histogram writeBandwidthHist;
303 Stats::Formula averageWriteBW;
304 Stats::Scalar totalWrittenBytes;
306 /** Disable flag for latency histograms. */
307 bool disableLatencyHists;
309 /** Histogram of read request-to-response latencies */
310 Stats::Histogram readLatencyHist;
312 /** Histogram of write request-to-response latencies */
313 Stats::Histogram writeLatencyHist;
315 /** Disable flag for ITT distributions. */
316 bool disableITTDists;
319 * Inter transaction time (ITT) distributions. There are
320 * histograms of the time between two read, write or arbitrary
321 * accesses. The time of a request is the tick at which the
322 * request is forwarded by the monitor.
324 Stats::Distribution ittReadRead;
325 Stats::Distribution ittWriteWrite;
326 Stats::Distribution ittReqReq;
328 Tick timeOfLastWrite;
331 /** Disable flag for outstanding histograms. */
332 bool disableOutstandingHists;
335 * Histogram of outstanding read requests. Counter for
336 * outstanding read requests is an unsigned integer because
337 * it should not be reset when stats are reset.
339 Stats::Histogram outstandingReadsHist;
340 unsigned int outstandingReadReqs;
343 * Histogram of outstanding write requests. Counter for
344 * outstanding write requests is an unsigned integer because
345 * it should not be reset when stats are reset.
347 Stats::Histogram outstandingWritesHist;
348 unsigned int outstandingWriteReqs;
350 /** Disable flag for transaction histograms. */
351 bool disableTransactionHists;
353 /** Histogram of number of read transactions per time bin */
354 Stats::Histogram readTransHist;
355 unsigned int readTrans;
357 /** Histogram of number of timing write transactions per time bin */
358 Stats::Histogram writeTransHist;
359 unsigned int writeTrans;
361 /** Disable flag for address distributions. */
362 bool disableAddrDists;
365 * Histogram of number of read accesses to addresses over
368 Stats::SparseHistogram readAddrDist;
371 * Histogram of number of write accesses to addresses over
374 Stats::SparseHistogram writeAddrDist;
377 * Create the monitor stats and initialise all the members
378 * that are not statistics themselves, but used to control the
379 * stats or track values during a sample period.
381 MonitorStats(const CommMonitorParams* params) :
382 disableBurstLengthHists(params->disable_burst_length_hists),
383 disableBandwidthHists(params->disable_bandwidth_hists),
384 readBytes(0), writtenBytes(0),
385 disableLatencyHists(params->disable_latency_hists),
386 disableITTDists(params->disable_itt_dists),
387 timeOfLastRead(0), timeOfLastWrite(0), timeOfLastReq(0),
388 disableOutstandingHists(params->disable_outstanding_hists),
389 outstandingReadReqs(0), outstandingWriteReqs(0),
390 disableTransactionHists(params->disable_transaction_hists),
391 readTrans(0), writeTrans(0),
392 disableAddrDists(params->disable_addr_dists)
397 /** This function is called periodically at the end of each time bin */
398 void samplePeriodic();
400 /** Schedule the first periodic event */
403 /** Periodic event called at the end of each simulation time bin */
404 EventWrapper<CommMonitor, &CommMonitor::samplePeriodic> samplePeriodicEvent;
406 /** Length of simulation time bin*/
407 Tick samplePeriodTicks;
410 /** Address mask for sources of read accesses to be captured */
413 /** Address mask for sources of write accesses to be captured */
416 /** Instantiate stats */
419 /** Optional stack distance calculator */
420 StackDistCalc* stackDistCalc;
422 /** Output stream for a potential trace. */
423 ProtoOutputStream* traceStream;
425 /** The system in which the monitor lives */
429 #endif //__MEM_COMM_MONITOR_HH__