mem-cache: Fix setting prefetch bit
[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 &
68 params() const
69 {
70 return reinterpret_cast<const Params &>(_params);
71 }
72
73 /**
74 * Constructor based on the Python params
75 *
76 * @param params Python parameters
77 */
78 CommMonitor(const Params &params);
79
80 void init() override;
81 void startup() override;
82 void regProbePoints() override;
83
84 public: // SimObject interfaces
85 Port &getPort(const std::string &if_name,
86 PortID idx=InvalidPortID) override;
87
88 private:
89
90 /**
91 * Sender state class for the monitor so that we can annotate
92 * packets with a transmit time and receive time.
93 */
94 class CommMonitorSenderState : public Packet::SenderState
95 {
96
97 public:
98
99 /**
100 * Construct a new sender state and store the time so we can
101 * calculate round-trip latency.
102 *
103 * @param _transmitTime Time of packet transmission
104 */
105 CommMonitorSenderState(Tick _transmitTime)
106 : transmitTime(_transmitTime)
107 { }
108
109 /** Destructor */
110 ~CommMonitorSenderState() { }
111
112 /** Tick when request is transmitted */
113 Tick transmitTime;
114
115 };
116
117 /**
118 * This is the request port of the communication monitor. All recv
119 * functions call a function in CommMonitor, where the
120 * send function of the CPU-side port is called. Besides this, these
121 * functions can also perform actions for capturing statistics.
122 */
123 class MonitorRequestPort : public RequestPort
124 {
125
126 public:
127
128 MonitorRequestPort(const std::string& _name, CommMonitor& _mon)
129 : RequestPort(_name, &_mon), mon(_mon)
130 { }
131
132 protected:
133
134 void recvFunctionalSnoop(PacketPtr pkt)
135 {
136 mon.recvFunctionalSnoop(pkt);
137 }
138
139 Tick recvAtomicSnoop(PacketPtr pkt)
140 {
141 return mon.recvAtomicSnoop(pkt);
142 }
143
144 bool recvTimingResp(PacketPtr pkt)
145 {
146 return mon.recvTimingResp(pkt);
147 }
148
149 void recvTimingSnoopReq(PacketPtr pkt)
150 {
151 mon.recvTimingSnoopReq(pkt);
152 }
153
154 void recvRangeChange()
155 {
156 mon.recvRangeChange();
157 }
158
159 bool isSnooping() const
160 {
161 return mon.isSnooping();
162 }
163
164 void recvReqRetry()
165 {
166 mon.recvReqRetry();
167 }
168
169 void recvRetrySnoopResp()
170 {
171 mon.recvRetrySnoopResp();
172 }
173
174 private:
175
176 CommMonitor& mon;
177
178 };
179
180 /** Instance of request port, facing the memory side */
181 MonitorRequestPort memSidePort;
182
183 /**
184 * This is the CPU-side port of the communication monitor. All recv
185 * functions call a function in CommMonitor, where the
186 * send function of the request port is called. Besides this, these
187 * functions can also perform actions for capturing statistics.
188 */
189 class MonitorResponsePort : public ResponsePort
190 {
191
192 public:
193
194 MonitorResponsePort(const std::string& _name, CommMonitor& _mon)
195 : ResponsePort(_name, &_mon), mon(_mon)
196 { }
197
198 protected:
199
200 void recvFunctional(PacketPtr pkt)
201 {
202 mon.recvFunctional(pkt);
203 }
204
205 Tick recvAtomic(PacketPtr pkt)
206 {
207 return mon.recvAtomic(pkt);
208 }
209
210 bool recvTimingReq(PacketPtr pkt)
211 {
212 return mon.recvTimingReq(pkt);
213 }
214
215 bool recvTimingSnoopResp(PacketPtr pkt)
216 {
217 return mon.recvTimingSnoopResp(pkt);
218 }
219
220 AddrRangeList getAddrRanges() const
221 {
222 return mon.getAddrRanges();
223 }
224
225 void recvRespRetry()
226 {
227 mon.recvRespRetry();
228 }
229
230 bool tryTiming(PacketPtr pkt)
231 {
232 return mon.tryTiming(pkt);
233 }
234
235 private:
236
237 CommMonitor& mon;
238
239 };
240
241 /** Instance of response port, i.e. on the CPU side */
242 MonitorResponsePort cpuSidePort;
243
244 void recvFunctional(PacketPtr pkt);
245
246 void recvFunctionalSnoop(PacketPtr pkt);
247
248 Tick recvAtomic(PacketPtr pkt);
249
250 Tick recvAtomicSnoop(PacketPtr pkt);
251
252 bool recvTimingReq(PacketPtr pkt);
253
254 bool recvTimingResp(PacketPtr pkt);
255
256 void recvTimingSnoopReq(PacketPtr pkt);
257
258 bool recvTimingSnoopResp(PacketPtr pkt);
259
260 void recvRetrySnoopResp();
261
262 AddrRangeList getAddrRanges() const;
263
264 bool isSnooping() const;
265
266 void recvReqRetry();
267
268 void recvRespRetry();
269
270 void recvRangeChange();
271
272 bool tryTiming(PacketPtr pkt);
273
274 /** Stats declarations, all in a struct for convenience. */
275 struct MonitorStats : public Stats::Group
276 {
277 /** Disable flag for burst length histograms **/
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::Scalar totalReadBytes;
296 Stats::Formula averageReadBandwidth;
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::Scalar totalWrittenBytes;
305 Stats::Formula averageWriteBandwidth;
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 /** Address mask for sources of read accesses to be captured */
366 const Addr readAddrMask;
367
368 /** Address mask for sources of write accesses to be captured */
369 const Addr writeAddrMask;
370
371 /**
372 * Histogram of number of read accesses to addresses over
373 * time.
374 */
375 Stats::SparseHistogram readAddrDist;
376
377 /**
378 * Histogram of number of write accesses to addresses over
379 * time.
380 */
381 Stats::SparseHistogram writeAddrDist;
382
383 /**
384 * Create the monitor stats and initialise all the members
385 * that are not statistics themselves, but used to control the
386 * stats or track values during a sample period.
387 */
388 MonitorStats(Stats::Group *parent, const CommMonitorParams &params);
389
390 void updateReqStats(const ProbePoints::PacketInfo& pkt, bool is_atomic,
391 bool expects_response);
392 void updateRespStats(const ProbePoints::PacketInfo& pkt, Tick latency,
393 bool is_atomic);
394 };
395
396 /** This function is called periodically at the end of each time bin */
397 void samplePeriodic();
398
399 /** Periodic event called at the end of each simulation time bin */
400 EventFunctionWrapper samplePeriodicEvent;
401
402 /**
403 *@{
404 * @name Configuration
405 */
406
407 /** Length of simulation time bin*/
408 const Tick samplePeriodTicks;
409 /** Sample period in seconds */
410 const double samplePeriod;
411
412 /** @} */
413
414 /** Instantiate stats */
415 MonitorStats stats;
416
417 protected: // Probe points
418 /**
419 * @{
420 * @name Memory system probe points
421 */
422
423 /** Successfully forwarded request packet */
424 ProbePoints::PacketUPtr ppPktReq;
425
426 /** Successfully forwarded response packet */
427 ProbePoints::PacketUPtr ppPktResp;
428
429 /** @} */
430 };
431
432 #endif //__MEM_COMM_MONITOR_HH__