comm_monitor.hh revision 10902:36b9241fa027
111821SChristian.Menard@tu-dresden.de/*
211821SChristian.Menard@tu-dresden.de * Copyright (c) 2012-2013, 2015 ARM Limited
311821SChristian.Menard@tu-dresden.de * All rights reserved
411821SChristian.Menard@tu-dresden.de *
511821SChristian.Menard@tu-dresden.de * The license below extends only to copyright in the software and shall
611821SChristian.Menard@tu-dresden.de * not be construed as granting a license to any other intellectual
711821SChristian.Menard@tu-dresden.de * property including but not limited to intellectual property relating
811821SChristian.Menard@tu-dresden.de * to a hardware implementation of the functionality of the software
911821SChristian.Menard@tu-dresden.de * licensed hereunder.  You may use the software subject to the license
1011821SChristian.Menard@tu-dresden.de * terms below provided that you ensure that this notice is replicated
1111821SChristian.Menard@tu-dresden.de * unmodified and in its entirety in all distributions of the software,
1211821SChristian.Menard@tu-dresden.de * modified or unmodified, in source code or in binary form.
1311821SChristian.Menard@tu-dresden.de *
1411821SChristian.Menard@tu-dresden.de * Redistribution and use in source and binary forms, with or without
1511821SChristian.Menard@tu-dresden.de * modification, are permitted provided that the following conditions are
1611821SChristian.Menard@tu-dresden.de * met: redistributions of source code must retain the above copyright
1711821SChristian.Menard@tu-dresden.de * notice, this list of conditions and the following disclaimer;
1811821SChristian.Menard@tu-dresden.de * redistributions in binary form must reproduce the above copyright
1911821SChristian.Menard@tu-dresden.de * notice, this list of conditions and the following disclaimer in the
2011821SChristian.Menard@tu-dresden.de * documentation and/or other materials provided with the distribution;
2111821SChristian.Menard@tu-dresden.de * neither the name of the copyright holders nor the names of its
2211821SChristian.Menard@tu-dresden.de * contributors may be used to endorse or promote products derived from
2311821SChristian.Menard@tu-dresden.de * this software without specific prior written permission.
2411821SChristian.Menard@tu-dresden.de *
2511821SChristian.Menard@tu-dresden.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2611821SChristian.Menard@tu-dresden.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2711821SChristian.Menard@tu-dresden.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2811821SChristian.Menard@tu-dresden.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2911821SChristian.Menard@tu-dresden.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3011821SChristian.Menard@tu-dresden.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3111821SChristian.Menard@tu-dresden.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3211821SChristian.Menard@tu-dresden.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3311821SChristian.Menard@tu-dresden.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3411821SChristian.Menard@tu-dresden.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3511821SChristian.Menard@tu-dresden.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3611821SChristian.Menard@tu-dresden.de *
3711821SChristian.Menard@tu-dresden.de * Authors: Thomas Grass
3811821SChristian.Menard@tu-dresden.de *          Andreas Hansson
3911821SChristian.Menard@tu-dresden.de */
4011821SChristian.Menard@tu-dresden.de
4111821SChristian.Menard@tu-dresden.de#ifndef __MEM_COMM_MONITOR_HH__
4211821SChristian.Menard@tu-dresden.de#define __MEM_COMM_MONITOR_HH__
4311821SChristian.Menard@tu-dresden.de
4411821SChristian.Menard@tu-dresden.de#include "base/statistics.hh"
4511821SChristian.Menard@tu-dresden.de#include "mem/mem_object.hh"
4611821SChristian.Menard@tu-dresden.de#include "mem/stack_dist_calc.hh"
4711821SChristian.Menard@tu-dresden.de#include "params/CommMonitor.hh"
4811821SChristian.Menard@tu-dresden.de#include "proto/protoio.hh"
4911821SChristian.Menard@tu-dresden.de#include "sim/system.hh"
5011821SChristian.Menard@tu-dresden.de
5111821SChristian.Menard@tu-dresden.de/**
5211821SChristian.Menard@tu-dresden.de * The communication monitor is a MemObject which can monitor statistics of
5311821SChristian.Menard@tu-dresden.de * the communication happening between two ports in the memory system.
5411821SChristian.Menard@tu-dresden.de *
5511821SChristian.Menard@tu-dresden.de * Currently the following stats are implemented: Histograms of read/write
5611821SChristian.Menard@tu-dresden.de * transactions, read/write burst lengths, read/write bandwidth,
5711821SChristian.Menard@tu-dresden.de * outstanding read/write requests, read latency and inter transaction time
5811821SChristian.Menard@tu-dresden.de * (read-read, write-write, read/write-read/write). Furthermore it allows
5911821SChristian.Menard@tu-dresden.de * to capture the number of accesses to an address over time ("heat map").
6011821SChristian.Menard@tu-dresden.de * All stats can be disabled from Python.
6111821SChristian.Menard@tu-dresden.de */
6211821SChristian.Menard@tu-dresden.declass CommMonitor : public MemObject
6311821SChristian.Menard@tu-dresden.de{
6411821SChristian.Menard@tu-dresden.de
6511821SChristian.Menard@tu-dresden.de  public: // Construction & SimObject interfaces
6611821SChristian.Menard@tu-dresden.de
6711821SChristian.Menard@tu-dresden.de    /** Parameters of communication monitor */
6811821SChristian.Menard@tu-dresden.de    typedef CommMonitorParams Params;
6911821SChristian.Menard@tu-dresden.de    const Params* params() const
7011821SChristian.Menard@tu-dresden.de    { return reinterpret_cast<const Params*>(_params); }
7111821SChristian.Menard@tu-dresden.de
7211821SChristian.Menard@tu-dresden.de    /**
7311821SChristian.Menard@tu-dresden.de     * Constructor based on the Python params
7411821SChristian.Menard@tu-dresden.de     *
7511821SChristian.Menard@tu-dresden.de     * @param params Python parameters
7611821SChristian.Menard@tu-dresden.de     */
7711821SChristian.Menard@tu-dresden.de    CommMonitor(Params* params);
7811821SChristian.Menard@tu-dresden.de
7911821SChristian.Menard@tu-dresden.de    /** Destructor */
8011821SChristian.Menard@tu-dresden.de    ~CommMonitor();
8111821SChristian.Menard@tu-dresden.de
8211821SChristian.Menard@tu-dresden.de    void init() M5_ATTR_OVERRIDE;
8311821SChristian.Menard@tu-dresden.de    void regStats() M5_ATTR_OVERRIDE;
8411821SChristian.Menard@tu-dresden.de    void startup() M5_ATTR_OVERRIDE;
8511821SChristian.Menard@tu-dresden.de
8611821SChristian.Menard@tu-dresden.de  public: // MemObject interfaces
8711821SChristian.Menard@tu-dresden.de    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