comm_monitor.hh revision 10902
1/*
2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Thomas Grass
38 *          Andreas Hansson
39 */
40
41#ifndef __MEM_COMM_MONITOR_HH__
42#define __MEM_COMM_MONITOR_HH__
43
44#include "base/statistics.hh"
45#include "mem/mem_object.hh"
46#include "mem/stack_dist_calc.hh"
47#include "params/CommMonitor.hh"
48#include "proto/protoio.hh"
49#include "sim/system.hh"
50
51/**
52 * The communication monitor is a MemObject which can monitor statistics of
53 * the communication happening between two ports in the memory system.
54 *
55 * Currently the following stats are implemented: Histograms of read/write
56 * transactions, read/write burst lengths, read/write bandwidth,
57 * outstanding read/write requests, read latency and inter transaction time
58 * (read-read, write-write, read/write-read/write). Furthermore it allows
59 * to capture the number of accesses to an address over time ("heat map").
60 * All stats can be disabled from Python.
61 */
62class CommMonitor : public MemObject
63{
64
65  public: // Construction & SimObject interfaces
66
67    /** Parameters of communication monitor */
68    typedef CommMonitorParams Params;
69    const Params* params() const
70    { return reinterpret_cast<const Params*>(_params); }
71
72    /**
73     * Constructor based on the Python params
74     *
75     * @param params Python parameters
76     */
77    CommMonitor(Params* params);
78
79    /** Destructor */
80    ~CommMonitor();
81
82    void init() M5_ATTR_OVERRIDE;
83    void regStats() M5_ATTR_OVERRIDE;
84    void startup() M5_ATTR_OVERRIDE;
85
86  public: // MemObject interfaces
87    BaseMasterPort& getMasterPort(const std::string& if_name,
88                                  PortID idx = InvalidPortID) M5_ATTR_OVERRIDE;
89
90    BaseSlavePort& getSlavePort(const std::string& if_name,
91                                PortID idx = InvalidPortID) M5_ATTR_OVERRIDE;
92
93  private:
94
95    /**
96     * Sender state class for the monitor so that we can annotate
97     * packets with a transmit time and receive time.
98     */
99    class CommMonitorSenderState : public Packet::SenderState
100    {
101
102      public:
103
104        /**
105         * Construct a new sender state and store the time so we can
106         * calculate round-trip latency.
107         *
108         * @param _transmitTime Time of packet transmission
109         */
110        CommMonitorSenderState(Tick _transmitTime)
111            : transmitTime(_transmitTime)
112        { }
113
114        /** Destructor */
115        ~CommMonitorSenderState() { }
116
117        /** Tick when request is transmitted */
118        Tick transmitTime;
119
120    };
121
122    /**
123     * This is the master port of the communication monitor. All recv
124     * functions call a function in CommMonitor, where the
125     * send function of the slave port is called. Besides this, these
126     * functions can also perform actions for capturing statistics.
127     */
128    class MonitorMasterPort : public MasterPort
129    {
130
131      public:
132
133        MonitorMasterPort(const std::string& _name, CommMonitor& _mon)
134            : MasterPort(_name, &_mon), mon(_mon)
135        { }
136
137      protected:
138
139        void recvFunctionalSnoop(PacketPtr pkt)
140        {
141            mon.recvFunctionalSnoop(pkt);
142        }
143
144        Tick recvAtomicSnoop(PacketPtr pkt)
145        {
146            return mon.recvAtomicSnoop(pkt);
147        }
148
149        bool recvTimingResp(PacketPtr pkt)
150        {
151            return mon.recvTimingResp(pkt);
152        }
153
154        void recvTimingSnoopReq(PacketPtr pkt)
155        {
156            mon.recvTimingSnoopReq(pkt);
157        }
158
159        void recvRangeChange()
160        {
161            mon.recvRangeChange();
162        }
163
164        bool isSnooping() const
165        {
166            return mon.isSnooping();
167        }
168
169        void recvReqRetry()
170        {
171            mon.recvReqRetry();
172        }
173
174      private:
175
176        CommMonitor& mon;
177
178    };
179
180    /** Instance of master port, facing the memory side */
181    MonitorMasterPort masterPort;
182
183    /**
184     * This is the slave port of the communication monitor. All recv
185     * functions call a function in CommMonitor, where the
186     * send function of the master port is called. Besides this, these
187     * functions can also perform actions for capturing statistics.
188     */
189    class MonitorSlavePort : public SlavePort
190    {
191
192      public:
193
194        MonitorSlavePort(const std::string& _name, CommMonitor& _mon)
195            : SlavePort(_name, &_mon), mon(_mon)
196        { }
197
198      protected:
199
200        void recvFunctional(PacketPtr pkt)
201        {
202            mon.recvFunctional(pkt);
203        }
204
205        Tick recvAtomic(PacketPtr pkt)
206        {
207            return mon.recvAtomic(pkt);
208        }
209
210        bool recvTimingReq(PacketPtr pkt)
211        {
212            return mon.recvTimingReq(pkt);
213        }
214
215        bool recvTimingSnoopResp(PacketPtr pkt)
216        {
217            return mon.recvTimingSnoopResp(pkt);
218        }
219
220        AddrRangeList getAddrRanges() const
221        {
222            return mon.getAddrRanges();
223        }
224
225        void recvRespRetry()
226        {
227            mon.recvRespRetry();
228        }
229
230      private:
231
232        CommMonitor& mon;
233
234    };
235
236    /** Instance of slave port, i.e. on the CPU side */
237    MonitorSlavePort slavePort;
238
239    void recvFunctional(PacketPtr pkt);
240
241    void recvFunctionalSnoop(PacketPtr pkt);
242
243    Tick recvAtomic(PacketPtr pkt);
244
245    Tick recvAtomicSnoop(PacketPtr pkt);
246
247    bool recvTimingReq(PacketPtr pkt);
248
249    bool recvTimingResp(PacketPtr pkt);
250
251    void recvTimingSnoopReq(PacketPtr pkt);
252
253    bool recvTimingSnoopResp(PacketPtr pkt);
254
255    AddrRangeList getAddrRanges() const;
256
257    bool isSnooping() const;
258
259    void recvReqRetry();
260
261    void recvRespRetry();
262
263    void recvRangeChange();
264
265    /** Stats declarations, all in a struct for convenience. */
266    struct MonitorStats
267    {
268
269        /** Disable flag for burst length historgrams **/
270        bool disableBurstLengthHists;
271
272        /** Histogram of read burst lengths */
273        Stats::Histogram readBurstLengthHist;
274
275        /** Histogram of write burst lengths */
276        Stats::Histogram writeBurstLengthHist;
277
278        /** Disable flag for the bandwidth histograms */
279        bool disableBandwidthHists;
280
281        /**
282         * Histogram for read bandwidth per sample window. The
283         * internal counter is an unsigned int rather than a stat.
284         */
285        unsigned int readBytes;
286        Stats::Histogram readBandwidthHist;
287        Stats::Formula averageReadBW;
288        Stats::Scalar totalReadBytes;
289
290        /**
291         * Histogram for write bandwidth per sample window. The
292         * internal counter is an unsigned int rather than a stat.
293         */
294        unsigned int writtenBytes;
295        Stats::Histogram writeBandwidthHist;
296        Stats::Formula averageWriteBW;
297        Stats::Scalar totalWrittenBytes;
298
299        /** Disable flag for latency histograms. */
300        bool disableLatencyHists;
301
302        /** Histogram of read request-to-response latencies */
303        Stats::Histogram readLatencyHist;
304
305        /** Histogram of write request-to-response latencies */
306        Stats::Histogram writeLatencyHist;
307
308        /** Disable flag for ITT distributions. */
309        bool disableITTDists;
310
311        /**
312         * Inter transaction time (ITT) distributions. There are
313         * histograms of the time between two read, write or arbitrary
314         * accesses. The time of a request is the tick at which the
315         * request is forwarded by the monitor.
316         */
317        Stats::Distribution ittReadRead;
318        Stats::Distribution ittWriteWrite;
319        Stats::Distribution ittReqReq;
320        Tick timeOfLastRead;
321        Tick timeOfLastWrite;
322        Tick timeOfLastReq;
323
324        /** Disable flag for outstanding histograms. */
325        bool disableOutstandingHists;
326
327        /**
328         * Histogram of outstanding read requests. Counter for
329         * outstanding read requests is an unsigned integer because
330         * it should not be reset when stats are reset.
331         */
332        Stats::Histogram outstandingReadsHist;
333        unsigned int outstandingReadReqs;
334
335        /**
336         * Histogram of outstanding write requests. Counter for
337         * outstanding write requests is an unsigned integer because
338         * it should not be reset when stats are reset.
339         */
340        Stats::Histogram outstandingWritesHist;
341        unsigned int outstandingWriteReqs;
342
343        /** Disable flag for transaction histograms. */
344        bool disableTransactionHists;
345
346        /** Histogram of number of read transactions per time bin */
347        Stats::Histogram readTransHist;
348        unsigned int readTrans;
349
350        /** Histogram of number of timing write transactions per time bin */
351        Stats::Histogram writeTransHist;
352        unsigned int writeTrans;
353
354        /** Disable flag for address distributions. */
355        bool disableAddrDists;
356
357        /**
358         * Histogram of number of read accesses to addresses over
359         * time.
360         */
361        Stats::SparseHistogram readAddrDist;
362
363        /**
364         * Histogram of number of write accesses to addresses over
365         * time.
366         */
367        Stats::SparseHistogram writeAddrDist;
368
369        /**
370         * Create the monitor stats and initialise all the members
371         * that are not statistics themselves, but used to control the
372         * stats or track values during a sample period.
373         */
374        MonitorStats(const CommMonitorParams* params) :
375            disableBurstLengthHists(params->disable_burst_length_hists),
376            disableBandwidthHists(params->disable_bandwidth_hists),
377            readBytes(0), writtenBytes(0),
378            disableLatencyHists(params->disable_latency_hists),
379            disableITTDists(params->disable_itt_dists),
380            timeOfLastRead(0), timeOfLastWrite(0), timeOfLastReq(0),
381            disableOutstandingHists(params->disable_outstanding_hists),
382            outstandingReadReqs(0), outstandingWriteReqs(0),
383            disableTransactionHists(params->disable_transaction_hists),
384            readTrans(0), writeTrans(0),
385            disableAddrDists(params->disable_addr_dists)
386        { }
387
388    };
389
390    /** This function is called periodically at the end of each time bin */
391    void samplePeriodic();
392
393    /**
394     * Callback to flush and close all open output streams on exit. If
395     * we were calling the destructor it could be done there.
396     */
397    void closeStreams();
398
399    /** Periodic event called at the end of each simulation time bin */
400    EventWrapper<CommMonitor, &CommMonitor::samplePeriodic> samplePeriodicEvent;
401
402    /**
403     *@{
404     * @name Configuration
405     */
406
407    /** Length of simulation time bin*/
408    const Tick samplePeriodTicks;
409    /** Sample period in seconds */
410    const double samplePeriod;
411
412    /** Address mask for sources of read accesses to be captured */
413    const Addr readAddrMask;
414
415    /** Address mask for sources of write accesses to be captured */
416    const Addr writeAddrMask;
417
418    /** Optional stack distance calculator */
419    StackDistCalc *const stackDistCalc;
420
421    /** The system in which the monitor lives */
422    System *const system;
423
424    /** @} */
425
426    /** Output stream for a potential trace. */
427    ProtoOutputStream *traceStream;
428
429    /** Instantiate stats */
430    MonitorStats stats;
431};
432
433#endif //__MEM_COMM_MONITOR_HH__
434