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