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