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