comm_monitor.hh revision 13573:3223a8c1c3dd
1/*
2 * Copyright (c) 2012-2013, 2015, 2018 ARM Limited
3 * Copyright (c) 2016 Google Inc.
4 * Copyright (c) 2017, Centre National de la Recherche Scientifique
5 * All rights reserved.
6 *
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.
15 *
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.
26 *
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.
38 *
39 * Authors: Thomas Grass
40 *          Andreas Hansson
41 *          Rahul Thakur
42 *          Pierre-Yves Peneau
43 */
44
45#ifndef __MEM_COMM_MONITOR_HH__
46#define __MEM_COMM_MONITOR_HH__
47
48#include "base/statistics.hh"
49#include "mem/mem_object.hh"
50#include "params/CommMonitor.hh"
51#include "sim/probe/mem.hh"
52
53/**
54 * The communication monitor is a MemObject which can monitor statistics of
55 * the communication happening between two ports in the memory system.
56 *
57 * Currently the following stats are implemented: Histograms of read/write
58 * transactions, read/write burst lengths, read/write bandwidth,
59 * outstanding read/write requests, read latency and inter transaction time
60 * (read-read, write-write, read/write-read/write). Furthermore it allows
61 * to capture the number of accesses to an address over time ("heat map").
62 * All stats can be disabled from Python.
63 */
64class CommMonitor : public MemObject
65{
66
67  public: // Construction & SimObject interfaces
68
69    /** Parameters of communication monitor */
70    typedef CommMonitorParams Params;
71    const Params* params() const
72    { return reinterpret_cast<const Params*>(_params); }
73
74    /**
75     * Constructor based on the Python params
76     *
77     * @param params Python parameters
78     */
79    CommMonitor(Params* params);
80
81    void init() override;
82    void regStats() override;
83    void startup() override;
84    void regProbePoints() override;
85
86  public: // MemObject interfaces
87    BaseMasterPort& getMasterPort(const std::string& if_name,
88                                  PortID idx = InvalidPortID) override;
89
90    BaseSlavePort& getSlavePort(const std::string& if_name,
91                                PortID idx = InvalidPortID) 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        void recvRetrySnoopResp()
175        {
176            mon.recvRetrySnoopResp();
177        }
178
179      private:
180
181        CommMonitor& mon;
182
183    };
184
185    /** Instance of master port, facing the memory side */
186    MonitorMasterPort masterPort;
187
188    /**
189     * This is the slave port of the communication monitor. All recv
190     * functions call a function in CommMonitor, where the
191     * send function of the master port is called. Besides this, these
192     * functions can also perform actions for capturing statistics.
193     */
194    class MonitorSlavePort : public SlavePort
195    {
196
197      public:
198
199        MonitorSlavePort(const std::string& _name, CommMonitor& _mon)
200            : SlavePort(_name, &_mon), mon(_mon)
201        { }
202
203      protected:
204
205        void recvFunctional(PacketPtr pkt)
206        {
207            mon.recvFunctional(pkt);
208        }
209
210        Tick recvAtomic(PacketPtr pkt)
211        {
212            return mon.recvAtomic(pkt);
213        }
214
215        bool recvTimingReq(PacketPtr pkt)
216        {
217            return mon.recvTimingReq(pkt);
218        }
219
220        bool recvTimingSnoopResp(PacketPtr pkt)
221        {
222            return mon.recvTimingSnoopResp(pkt);
223        }
224
225        AddrRangeList getAddrRanges() const
226        {
227            return mon.getAddrRanges();
228        }
229
230        void recvRespRetry()
231        {
232            mon.recvRespRetry();
233        }
234
235        bool tryTiming(PacketPtr pkt)
236        {
237            return mon.tryTiming(pkt);
238        }
239
240      private:
241
242        CommMonitor& mon;
243
244    };
245
246    /** Instance of slave port, i.e. on the CPU side */
247    MonitorSlavePort slavePort;
248
249    void recvFunctional(PacketPtr pkt);
250
251    void recvFunctionalSnoop(PacketPtr pkt);
252
253    Tick recvAtomic(PacketPtr pkt);
254
255    Tick recvAtomicSnoop(PacketPtr pkt);
256
257    bool recvTimingReq(PacketPtr pkt);
258
259    bool recvTimingResp(PacketPtr pkt);
260
261    void recvTimingSnoopReq(PacketPtr pkt);
262
263    bool recvTimingSnoopResp(PacketPtr pkt);
264
265    void recvRetrySnoopResp();
266
267    AddrRangeList getAddrRanges() const;
268
269    bool isSnooping() const;
270
271    void recvReqRetry();
272
273    void recvRespRetry();
274
275    void recvRangeChange();
276
277    bool tryTiming(PacketPtr pkt);
278
279    /** Stats declarations, all in a struct for convenience. */
280    struct MonitorStats
281    {
282
283        /** Disable flag for burst length histograms **/
284        bool disableBurstLengthHists;
285
286        /** Histogram of read burst lengths */
287        Stats::Histogram readBurstLengthHist;
288
289        /** Histogram of write burst lengths */
290        Stats::Histogram writeBurstLengthHist;
291
292        /** Disable flag for the bandwidth histograms */
293        bool disableBandwidthHists;
294
295        /**
296         * Histogram for read bandwidth per sample window. The
297         * internal counter is an unsigned int rather than a stat.
298         */
299        unsigned int readBytes;
300        Stats::Histogram readBandwidthHist;
301        Stats::Formula averageReadBW;
302        Stats::Scalar totalReadBytes;
303
304        /**
305         * Histogram for write bandwidth per sample window. The
306         * internal counter is an unsigned int rather than a stat.
307         */
308        unsigned int writtenBytes;
309        Stats::Histogram writeBandwidthHist;
310        Stats::Formula averageWriteBW;
311        Stats::Scalar totalWrittenBytes;
312
313        /** Disable flag for latency histograms. */
314        bool disableLatencyHists;
315
316        /** Histogram of read request-to-response latencies */
317        Stats::Histogram readLatencyHist;
318
319        /** Histogram of write request-to-response latencies */
320        Stats::Histogram writeLatencyHist;
321
322        /** Disable flag for ITT distributions. */
323        bool disableITTDists;
324
325        /**
326         * Inter transaction time (ITT) distributions. There are
327         * histograms of the time between two read, write or arbitrary
328         * accesses. The time of a request is the tick at which the
329         * request is forwarded by the monitor.
330         */
331        Stats::Distribution ittReadRead;
332        Stats::Distribution ittWriteWrite;
333        Stats::Distribution ittReqReq;
334        Tick timeOfLastRead;
335        Tick timeOfLastWrite;
336        Tick timeOfLastReq;
337
338        /** Disable flag for outstanding histograms. */
339        bool disableOutstandingHists;
340
341        /**
342         * Histogram of outstanding read requests. Counter for
343         * outstanding read requests is an unsigned integer because
344         * it should not be reset when stats are reset.
345         */
346        Stats::Histogram outstandingReadsHist;
347        unsigned int outstandingReadReqs;
348
349        /**
350         * Histogram of outstanding write requests. Counter for
351         * outstanding write requests is an unsigned integer because
352         * it should not be reset when stats are reset.
353         */
354        Stats::Histogram outstandingWritesHist;
355        unsigned int outstandingWriteReqs;
356
357        /** Disable flag for transaction histograms. */
358        bool disableTransactionHists;
359
360        /** Histogram of number of read transactions per time bin */
361        Stats::Histogram readTransHist;
362        unsigned int readTrans;
363
364        /** Histogram of number of timing write transactions per time bin */
365        Stats::Histogram writeTransHist;
366        unsigned int writeTrans;
367
368        /** Disable flag for address distributions. */
369        bool disableAddrDists;
370
371        /** Address mask for sources of read accesses to be captured */
372        const Addr readAddrMask;
373
374        /** Address mask for sources of write accesses to be captured */
375        const Addr writeAddrMask;
376
377        /**
378         * Histogram of number of read accesses to addresses over
379         * time.
380         */
381        Stats::SparseHistogram readAddrDist;
382
383        /**
384         * Histogram of number of write accesses to addresses over
385         * time.
386         */
387        Stats::SparseHistogram writeAddrDist;
388
389        /**
390         * Create the monitor stats and initialise all the members
391         * that are not statistics themselves, but used to control the
392         * stats or track values during a sample period.
393         */
394        MonitorStats(const CommMonitorParams* params) :
395            disableBurstLengthHists(params->disable_burst_length_hists),
396            disableBandwidthHists(params->disable_bandwidth_hists),
397            readBytes(0), writtenBytes(0),
398            disableLatencyHists(params->disable_latency_hists),
399            disableITTDists(params->disable_itt_dists),
400            timeOfLastRead(0), timeOfLastWrite(0), timeOfLastReq(0),
401            disableOutstandingHists(params->disable_outstanding_hists),
402            outstandingReadReqs(0), outstandingWriteReqs(0),
403            disableTransactionHists(params->disable_transaction_hists),
404            readTrans(0), writeTrans(0),
405            disableAddrDists(params->disable_addr_dists),
406            readAddrMask(params->read_addr_mask),
407            writeAddrMask(params->write_addr_mask)
408        { }
409
410        void updateReqStats(const ProbePoints::PacketInfo& pkt, bool is_atomic,
411                            bool expects_response);
412        void updateRespStats(const ProbePoints::PacketInfo& pkt, Tick latency,
413                             bool is_atomic);
414    };
415
416    /** This function is called periodically at the end of each time bin */
417    void samplePeriodic();
418
419    /** Periodic event called at the end of each simulation time bin */
420    EventFunctionWrapper samplePeriodicEvent;
421
422    /**
423     *@{
424     * @name Configuration
425     */
426
427    /** Length of simulation time bin*/
428    const Tick samplePeriodTicks;
429    /** Sample period in seconds */
430    const double samplePeriod;
431
432    /** @} */
433
434    /** Instantiate stats */
435    MonitorStats stats;
436
437  protected: // Probe points
438    /**
439     * @{
440     * @name Memory system probe points
441     */
442
443    /** Successfully forwarded request packet */
444    ProbePoints::PacketUPtr ppPktReq;
445
446    /** Successfully forwarded response packet */
447    ProbePoints::PacketUPtr ppPktResp;
448
449    /** @} */
450};
451
452#endif //__MEM_COMM_MONITOR_HH__
453