arch, cpu, dev, gpu, mem, sim, python: start using getPort.
[gem5.git] / src / mem / comm_monitor.hh
1 /*
2 * Copyright (c) 2012-2013, 2015, 2018 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/mem_object.hh"
50 #include "params/CommMonitor.hh"
51 #include "sim/probe/mem.hh"
52
53 /**
54 * The communication monitor is a MemObject which can monitor statistics of
55 * the communication happening between two ports in the memory system.
56 *
57 * Currently the following stats are implemented: Histograms of read/write
58 * transactions, read/write burst lengths, read/write bandwidth,
59 * outstanding read/write requests, read latency and inter transaction time
60 * (read-read, write-write, read/write-read/write). Furthermore it allows
61 * to capture the number of accesses to an address over time ("heat map").
62 * All stats can be disabled from Python.
63 */
64 class CommMonitor : public MemObject
65 {
66
67 public: // Construction & SimObject interfaces
68
69 /** Parameters of communication monitor */
70 typedef CommMonitorParams Params;
71 const Params* params() const
72 { return reinterpret_cast<const Params*>(_params); }
73
74 /**
75 * Constructor based on the Python params
76 *
77 * @param params Python parameters
78 */
79 CommMonitor(Params* params);
80
81 void init() override;
82 void regStats() override;
83 void startup() override;
84 void regProbePoints() override;
85
86 public: // MemObject 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
278 {
279
280 /** Disable flag for burst length histograms **/
281 bool disableBurstLengthHists;
282
283 /** Histogram of read burst lengths */
284 Stats::Histogram readBurstLengthHist;
285
286 /** Histogram of write burst lengths */
287 Stats::Histogram writeBurstLengthHist;
288
289 /** Disable flag for the bandwidth histograms */
290 bool disableBandwidthHists;
291
292 /**
293 * Histogram for read bandwidth per sample window. The
294 * internal counter is an unsigned int rather than a stat.
295 */
296 unsigned int readBytes;
297 Stats::Histogram readBandwidthHist;
298 Stats::Formula averageReadBW;
299 Stats::Scalar totalReadBytes;
300
301 /**
302 * Histogram for write bandwidth per sample window. The
303 * internal counter is an unsigned int rather than a stat.
304 */
305 unsigned int writtenBytes;
306 Stats::Histogram writeBandwidthHist;
307 Stats::Formula averageWriteBW;
308 Stats::Scalar totalWrittenBytes;
309
310 /** Disable flag for latency histograms. */
311 bool disableLatencyHists;
312
313 /** Histogram of read request-to-response latencies */
314 Stats::Histogram readLatencyHist;
315
316 /** Histogram of write request-to-response latencies */
317 Stats::Histogram writeLatencyHist;
318
319 /** Disable flag for ITT distributions. */
320 bool disableITTDists;
321
322 /**
323 * Inter transaction time (ITT) distributions. There are
324 * histograms of the time between two read, write or arbitrary
325 * accesses. The time of a request is the tick at which the
326 * request is forwarded by the monitor.
327 */
328 Stats::Distribution ittReadRead;
329 Stats::Distribution ittWriteWrite;
330 Stats::Distribution ittReqReq;
331 Tick timeOfLastRead;
332 Tick timeOfLastWrite;
333 Tick timeOfLastReq;
334
335 /** Disable flag for outstanding histograms. */
336 bool disableOutstandingHists;
337
338 /**
339 * Histogram of outstanding read requests. Counter for
340 * outstanding read requests is an unsigned integer because
341 * it should not be reset when stats are reset.
342 */
343 Stats::Histogram outstandingReadsHist;
344 unsigned int outstandingReadReqs;
345
346 /**
347 * Histogram of outstanding write requests. Counter for
348 * outstanding write requests is an unsigned integer because
349 * it should not be reset when stats are reset.
350 */
351 Stats::Histogram outstandingWritesHist;
352 unsigned int outstandingWriteReqs;
353
354 /** Disable flag for transaction histograms. */
355 bool disableTransactionHists;
356
357 /** Histogram of number of read transactions per time bin */
358 Stats::Histogram readTransHist;
359 unsigned int readTrans;
360
361 /** Histogram of number of timing write transactions per time bin */
362 Stats::Histogram writeTransHist;
363 unsigned int writeTrans;
364
365 /** Disable flag for address distributions. */
366 bool disableAddrDists;
367
368 /** Address mask for sources of read accesses to be captured */
369 const Addr readAddrMask;
370
371 /** Address mask for sources of write accesses to be captured */
372 const Addr writeAddrMask;
373
374 /**
375 * Histogram of number of read accesses to addresses over
376 * time.
377 */
378 Stats::SparseHistogram readAddrDist;
379
380 /**
381 * Histogram of number of write accesses to addresses over
382 * time.
383 */
384 Stats::SparseHistogram writeAddrDist;
385
386 /**
387 * Create the monitor stats and initialise all the members
388 * that are not statistics themselves, but used to control the
389 * stats or track values during a sample period.
390 */
391 MonitorStats(const CommMonitorParams* params) :
392 disableBurstLengthHists(params->disable_burst_length_hists),
393 disableBandwidthHists(params->disable_bandwidth_hists),
394 readBytes(0), writtenBytes(0),
395 disableLatencyHists(params->disable_latency_hists),
396 disableITTDists(params->disable_itt_dists),
397 timeOfLastRead(0), timeOfLastWrite(0), timeOfLastReq(0),
398 disableOutstandingHists(params->disable_outstanding_hists),
399 outstandingReadReqs(0), outstandingWriteReqs(0),
400 disableTransactionHists(params->disable_transaction_hists),
401 readTrans(0), writeTrans(0),
402 disableAddrDists(params->disable_addr_dists),
403 readAddrMask(params->read_addr_mask),
404 writeAddrMask(params->write_addr_mask)
405 { }
406
407 void updateReqStats(const ProbePoints::PacketInfo& pkt, bool is_atomic,
408 bool expects_response);
409 void updateRespStats(const ProbePoints::PacketInfo& pkt, Tick latency,
410 bool is_atomic);
411 };
412
413 /** This function is called periodically at the end of each time bin */
414 void samplePeriodic();
415
416 /** Periodic event called at the end of each simulation time bin */
417 EventFunctionWrapper samplePeriodicEvent;
418
419 /**
420 *@{
421 * @name Configuration
422 */
423
424 /** Length of simulation time bin*/
425 const Tick samplePeriodTicks;
426 /** Sample period in seconds */
427 const double samplePeriod;
428
429 /** @} */
430
431 /** Instantiate stats */
432 MonitorStats stats;
433
434 protected: // Probe points
435 /**
436 * @{
437 * @name Memory system probe points
438 */
439
440 /** Successfully forwarded request packet */
441 ProbePoints::PacketUPtr ppPktReq;
442
443 /** Successfully forwarded response packet */
444 ProbePoints::PacketUPtr ppPktResp;
445
446 /** @} */
447 };
448
449 #endif //__MEM_COMM_MONITOR_HH__