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