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