mem: Redesign the stack distance calculator as a probe
[gem5.git] / src / mem / comm_monitor.cc
1 /*
2 * Copyright (c) 2012-2013, 2015 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 #include "base/callback.hh"
42 #include "base/output.hh"
43 #include "base/trace.hh"
44 #include "debug/CommMonitor.hh"
45 #include "mem/comm_monitor.hh"
46 #include "proto/packet.pb.h"
47 #include "sim/stats.hh"
48
49 CommMonitor::CommMonitor(Params* params)
50 : MemObject(params),
51 masterPort(name() + "-master", *this),
52 slavePort(name() + "-slave", *this),
53 samplePeriodicEvent(this),
54 samplePeriodTicks(params->sample_period),
55 samplePeriod(params->sample_period / SimClock::Float::s),
56 readAddrMask(params->read_addr_mask),
57 writeAddrMask(params->write_addr_mask),
58 system(params->system),
59 traceStream(nullptr),
60 stats(params)
61 {
62 // If we are using a trace file, then open the file
63 if (params->trace_enable) {
64 std::string filename;
65 if (params->trace_file != "") {
66 // If the trace file is not specified as an absolute path,
67 // append the current simulation output directory
68 filename = simout.resolve(params->trace_file);
69
70 std::string suffix = ".gz";
71 // If trace_compress has been set, check the suffix. Append
72 // accordingly.
73 if (params->trace_compress &&
74 filename.compare(filename.size() - suffix.size(), suffix.size(),
75 suffix) != 0)
76 filename = filename + suffix;
77 } else {
78 // Generate a filename from the name of the SimObject. Append .trc
79 // and .gz if we want compression enabled.
80 filename = simout.resolve(name() + ".trc" +
81 (params->trace_compress ? ".gz" : ""));
82 }
83
84 traceStream = new ProtoOutputStream(filename);
85
86 // Create a protobuf message for the header and write it to
87 // the stream
88 ProtoMessage::PacketHeader header_msg;
89 header_msg.set_obj_id(name());
90 header_msg.set_tick_freq(SimClock::Frequency);
91 traceStream->write(header_msg);
92
93 // Register a callback to compensate for the destructor not
94 // being called. The callback forces the stream to flush and
95 // closes the output file.
96 Callback* cb = new MakeCallback<CommMonitor,
97 &CommMonitor::closeStreams>(this);
98 registerExitCallback(cb);
99 }
100
101 DPRINTF(CommMonitor,
102 "Created monitor %s with sample period %d ticks (%f ms)\n",
103 name(), samplePeriodTicks, samplePeriod * 1E3);
104 }
105
106 CommMonitor::~CommMonitor()
107 {
108 // if not already done, close the stream
109 closeStreams();
110 }
111
112 void
113 CommMonitor::closeStreams()
114 {
115 if (traceStream != NULL)
116 delete traceStream;
117 }
118
119 CommMonitor*
120 CommMonitorParams::create()
121 {
122 return new CommMonitor(this);
123 }
124
125 void
126 CommMonitor::init()
127 {
128 // make sure both sides of the monitor are connected
129 if (!slavePort.isConnected() || !masterPort.isConnected())
130 fatal("Communication monitor is not connected on both sides.\n");
131
132 if (traceStream != NULL) {
133 // Check the memory mode. We only record something when in
134 // timing mode. Warn accordingly.
135 if (!system->isTimingMode())
136 warn("%s: Not in timing mode. No trace will be recorded.", name());
137 }
138
139 }
140
141 void
142 CommMonitor::regProbePoints()
143 {
144 ppPktReq.reset(new ProbePoints::Packet(getProbeManager(), "PktRequest"));
145 ppPktResp.reset(new ProbePoints::Packet(getProbeManager(), "PktResponse"));
146 }
147
148 BaseMasterPort&
149 CommMonitor::getMasterPort(const std::string& if_name, PortID idx)
150 {
151 if (if_name == "master") {
152 return masterPort;
153 } else {
154 return MemObject::getMasterPort(if_name, idx);
155 }
156 }
157
158 BaseSlavePort&
159 CommMonitor::getSlavePort(const std::string& if_name, PortID idx)
160 {
161 if (if_name == "slave") {
162 return slavePort;
163 } else {
164 return MemObject::getSlavePort(if_name, idx);
165 }
166 }
167
168 void
169 CommMonitor::recvFunctional(PacketPtr pkt)
170 {
171 masterPort.sendFunctional(pkt);
172 }
173
174 void
175 CommMonitor::recvFunctionalSnoop(PacketPtr pkt)
176 {
177 slavePort.sendFunctionalSnoop(pkt);
178 }
179
180 Tick
181 CommMonitor::recvAtomic(PacketPtr pkt)
182 {
183 ppPktReq->notify(pkt);
184
185 // if tracing enabled, store the packet information
186 // to the trace stream
187 if (traceStream != NULL) {
188 ProtoMessage::Packet pkt_msg;
189 pkt_msg.set_tick(curTick());
190 pkt_msg.set_cmd(pkt->cmdToIndex());
191 pkt_msg.set_flags(pkt->req->getFlags());
192 pkt_msg.set_addr(pkt->getAddr());
193 pkt_msg.set_size(pkt->getSize());
194
195 traceStream->write(pkt_msg);
196 }
197
198 const Tick delay(masterPort.sendAtomic(pkt));
199 assert(pkt->isResponse());
200 ppPktResp->notify(pkt);
201 return delay;
202 }
203
204 Tick
205 CommMonitor::recvAtomicSnoop(PacketPtr pkt)
206 {
207 return slavePort.sendAtomicSnoop(pkt);
208 }
209
210 bool
211 CommMonitor::recvTimingReq(PacketPtr pkt)
212 {
213 // should always see a request
214 assert(pkt->isRequest());
215
216 // Store relevant fields of packet, because packet may be modified
217 // or even deleted when sendTiming() is called.
218 const bool is_read = pkt->isRead();
219 const bool is_write = pkt->isWrite();
220 const MemCmd cmd = pkt->cmd;
221 const int cmd_idx = pkt->cmdToIndex();
222 const Request::FlagsType req_flags = pkt->req->getFlags();
223 const unsigned size = pkt->getSize();
224 const Addr addr = pkt->getAddr();
225 const bool expects_response(
226 pkt->needsResponse() && !pkt->memInhibitAsserted());
227
228 // If a cache miss is served by a cache, a monitor near the memory
229 // would see a request which needs a response, but this response
230 // would be inhibited and not come back from the memory. Therefore
231 // we additionally have to check the inhibit flag.
232 if (expects_response && !stats.disableLatencyHists) {
233 pkt->pushSenderState(new CommMonitorSenderState(curTick()));
234 }
235
236 // Attempt to send the packet (always succeeds for inhibited
237 // packets)
238 bool successful = masterPort.sendTimingReq(pkt);
239
240 // If not successful, restore the sender state
241 if (!successful && expects_response && !stats.disableLatencyHists) {
242 delete pkt->popSenderState();
243 }
244
245 if (successful) {
246 // The receiver might already have modified the packet. We
247 // want to give the probe access to the original packet, which
248 // means we need to fake the original packet by temporarily
249 // restoring the command.
250 const MemCmd response_cmd(pkt->cmd);
251 pkt->cmd = cmd;
252 ppPktReq->notify(pkt);
253 pkt->cmd = response_cmd;
254 }
255
256 if (successful && traceStream != NULL) {
257 // Create a protobuf message representing the
258 // packet. Currently we do not preserve the flags in the
259 // trace.
260 ProtoMessage::Packet pkt_msg;
261 pkt_msg.set_tick(curTick());
262 pkt_msg.set_cmd(cmd_idx);
263 pkt_msg.set_flags(req_flags);
264 pkt_msg.set_addr(addr);
265 pkt_msg.set_size(size);
266
267 traceStream->write(pkt_msg);
268 }
269
270 if (successful && is_read) {
271 DPRINTF(CommMonitor, "Forwarded read request\n");
272
273 // Increment number of observed read transactions
274 if (!stats.disableTransactionHists) {
275 ++stats.readTrans;
276 }
277
278 // Get sample of burst length
279 if (!stats.disableBurstLengthHists) {
280 stats.readBurstLengthHist.sample(size);
281 }
282
283 // Sample the masked address
284 if (!stats.disableAddrDists) {
285 stats.readAddrDist.sample(addr & readAddrMask);
286 }
287
288 // If it needs a response increment number of outstanding read
289 // requests
290 if (!stats.disableOutstandingHists && expects_response) {
291 ++stats.outstandingReadReqs;
292 }
293
294 if (!stats.disableITTDists) {
295 // Sample value of read-read inter transaction time
296 if (stats.timeOfLastRead != 0) {
297 stats.ittReadRead.sample(curTick() - stats.timeOfLastRead);
298 }
299 stats.timeOfLastRead = curTick();
300
301 // Sample value of req-req inter transaction time
302 if (stats.timeOfLastReq != 0) {
303 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
304 }
305 stats.timeOfLastReq = curTick();
306 }
307 } else if (successful && is_write) {
308 DPRINTF(CommMonitor, "Forwarded write request\n");
309
310 // Same as for reads
311 if (!stats.disableTransactionHists) {
312 ++stats.writeTrans;
313 }
314
315 if (!stats.disableBurstLengthHists) {
316 stats.writeBurstLengthHist.sample(size);
317 }
318
319 // Update the bandwidth stats on the request
320 if (!stats.disableBandwidthHists) {
321 stats.writtenBytes += size;
322 stats.totalWrittenBytes += size;
323 }
324
325 // Sample the masked write address
326 if (!stats.disableAddrDists) {
327 stats.writeAddrDist.sample(addr & writeAddrMask);
328 }
329
330 if (!stats.disableOutstandingHists && expects_response) {
331 ++stats.outstandingWriteReqs;
332 }
333
334 if (!stats.disableITTDists) {
335 // Sample value of write-to-write inter transaction time
336 if (stats.timeOfLastWrite != 0) {
337 stats.ittWriteWrite.sample(curTick() - stats.timeOfLastWrite);
338 }
339 stats.timeOfLastWrite = curTick();
340
341 // Sample value of req-to-req inter transaction time
342 if (stats.timeOfLastReq != 0) {
343 stats.ittReqReq.sample(curTick() - stats.timeOfLastReq);
344 }
345 stats.timeOfLastReq = curTick();
346 }
347 } else if (successful) {
348 DPRINTF(CommMonitor, "Forwarded non read/write request\n");
349 }
350
351 return successful;
352 }
353
354 bool
355 CommMonitor::recvTimingResp(PacketPtr pkt)
356 {
357 // should always see responses
358 assert(pkt->isResponse());
359
360 // Store relevant fields of packet, because packet may be modified
361 // or even deleted when sendTiming() is called.
362 bool is_read = pkt->isRead();
363 bool is_write = pkt->isWrite();
364 unsigned size = pkt->getSize();
365 Tick latency = 0;
366 CommMonitorSenderState* received_state =
367 dynamic_cast<CommMonitorSenderState*>(pkt->senderState);
368
369 if (!stats.disableLatencyHists) {
370 // Restore initial sender state
371 if (received_state == NULL)
372 panic("Monitor got a response without monitor sender state\n");
373
374 // Restore the sate
375 pkt->senderState = received_state->predecessor;
376 }
377
378 // Attempt to send the packet
379 bool successful = slavePort.sendTimingResp(pkt);
380
381 if (!stats.disableLatencyHists) {
382 // If packet successfully send, sample value of latency,
383 // afterwards delete sender state, otherwise restore state
384 if (successful) {
385 latency = curTick() - received_state->transmitTime;
386 DPRINTF(CommMonitor, "Latency: %d\n", latency);
387 delete received_state;
388 } else {
389 // Don't delete anything and let the packet look like we
390 // did not touch it
391 pkt->senderState = received_state;
392 }
393 }
394
395 if (successful) {
396 assert(pkt->isResponse());
397 ppPktResp->notify(pkt);
398 }
399
400 if (successful && is_read) {
401 // Decrement number of outstanding read requests
402 DPRINTF(CommMonitor, "Received read response\n");
403 if (!stats.disableOutstandingHists) {
404 assert(stats.outstandingReadReqs != 0);
405 --stats.outstandingReadReqs;
406 }
407
408 if (!stats.disableLatencyHists) {
409 stats.readLatencyHist.sample(latency);
410 }
411
412 // Update the bandwidth stats based on responses for reads
413 if (!stats.disableBandwidthHists) {
414 stats.readBytes += size;
415 stats.totalReadBytes += size;
416 }
417
418 } else if (successful && is_write) {
419 // Decrement number of outstanding write requests
420 DPRINTF(CommMonitor, "Received write response\n");
421 if (!stats.disableOutstandingHists) {
422 assert(stats.outstandingWriteReqs != 0);
423 --stats.outstandingWriteReqs;
424 }
425
426 if (!stats.disableLatencyHists) {
427 stats.writeLatencyHist.sample(latency);
428 }
429 } else if (successful) {
430 DPRINTF(CommMonitor, "Received non read/write response\n");
431 }
432 return successful;
433 }
434
435 void
436 CommMonitor::recvTimingSnoopReq(PacketPtr pkt)
437 {
438 slavePort.sendTimingSnoopReq(pkt);
439 }
440
441 bool
442 CommMonitor::recvTimingSnoopResp(PacketPtr pkt)
443 {
444 return masterPort.sendTimingSnoopResp(pkt);
445 }
446
447 bool
448 CommMonitor::isSnooping() const
449 {
450 // check if the connected master port is snooping
451 return slavePort.isSnooping();
452 }
453
454 AddrRangeList
455 CommMonitor::getAddrRanges() const
456 {
457 // get the address ranges of the connected slave port
458 return masterPort.getAddrRanges();
459 }
460
461 void
462 CommMonitor::recvReqRetry()
463 {
464 slavePort.sendRetryReq();
465 }
466
467 void
468 CommMonitor::recvRespRetry()
469 {
470 masterPort.sendRetryResp();
471 }
472
473 void
474 CommMonitor::recvRangeChange()
475 {
476 slavePort.sendRangeChange();
477 }
478
479 void
480 CommMonitor::regStats()
481 {
482 // Initialise all the monitor stats
483 using namespace Stats;
484
485 stats.readBurstLengthHist
486 .init(params()->burst_length_bins)
487 .name(name() + ".readBurstLengthHist")
488 .desc("Histogram of burst lengths of transmitted packets")
489 .flags(stats.disableBurstLengthHists ? nozero : pdf);
490
491 stats.writeBurstLengthHist
492 .init(params()->burst_length_bins)
493 .name(name() + ".writeBurstLengthHist")
494 .desc("Histogram of burst lengths of transmitted packets")
495 .flags(stats.disableBurstLengthHists ? nozero : pdf);
496
497 // Stats based on received responses
498 stats.readBandwidthHist
499 .init(params()->bandwidth_bins)
500 .name(name() + ".readBandwidthHist")
501 .desc("Histogram of read bandwidth per sample period (bytes/s)")
502 .flags(stats.disableBandwidthHists ? nozero : pdf);
503
504 stats.averageReadBW
505 .name(name() + ".averageReadBandwidth")
506 .desc("Average read bandwidth (bytes/s)")
507 .flags(stats.disableBandwidthHists ? nozero : pdf);
508
509 stats.totalReadBytes
510 .name(name() + ".totalReadBytes")
511 .desc("Number of bytes read")
512 .flags(stats.disableBandwidthHists ? nozero : pdf);
513
514 stats.averageReadBW = stats.totalReadBytes / simSeconds;
515
516 // Stats based on successfully sent requests
517 stats.writeBandwidthHist
518 .init(params()->bandwidth_bins)
519 .name(name() + ".writeBandwidthHist")
520 .desc("Histogram of write bandwidth (bytes/s)")
521 .flags(stats.disableBandwidthHists ? (pdf | nozero) : pdf);
522
523 stats.averageWriteBW
524 .name(name() + ".averageWriteBandwidth")
525 .desc("Average write bandwidth (bytes/s)")
526 .flags(stats.disableBandwidthHists ? nozero : pdf);
527
528 stats.totalWrittenBytes
529 .name(name() + ".totalWrittenBytes")
530 .desc("Number of bytes written")
531 .flags(stats.disableBandwidthHists ? nozero : pdf);
532
533 stats.averageWriteBW = stats.totalWrittenBytes / simSeconds;
534
535 stats.readLatencyHist
536 .init(params()->latency_bins)
537 .name(name() + ".readLatencyHist")
538 .desc("Read request-response latency")
539 .flags(stats.disableLatencyHists ? nozero : pdf);
540
541 stats.writeLatencyHist
542 .init(params()->latency_bins)
543 .name(name() + ".writeLatencyHist")
544 .desc("Write request-response latency")
545 .flags(stats.disableLatencyHists ? nozero : pdf);
546
547 stats.ittReadRead
548 .init(1, params()->itt_max_bin, params()->itt_max_bin /
549 params()->itt_bins)
550 .name(name() + ".ittReadRead")
551 .desc("Read-to-read inter transaction time")
552 .flags(stats.disableITTDists ? nozero : pdf);
553
554 stats.ittWriteWrite
555 .init(1, params()->itt_max_bin, params()->itt_max_bin /
556 params()->itt_bins)
557 .name(name() + ".ittWriteWrite")
558 .desc("Write-to-write inter transaction time")
559 .flags(stats.disableITTDists ? nozero : pdf);
560
561 stats.ittReqReq
562 .init(1, params()->itt_max_bin, params()->itt_max_bin /
563 params()->itt_bins)
564 .name(name() + ".ittReqReq")
565 .desc("Request-to-request inter transaction time")
566 .flags(stats.disableITTDists ? nozero : pdf);
567
568 stats.outstandingReadsHist
569 .init(params()->outstanding_bins)
570 .name(name() + ".outstandingReadsHist")
571 .desc("Outstanding read transactions")
572 .flags(stats.disableOutstandingHists ? nozero : pdf);
573
574 stats.outstandingWritesHist
575 .init(params()->outstanding_bins)
576 .name(name() + ".outstandingWritesHist")
577 .desc("Outstanding write transactions")
578 .flags(stats.disableOutstandingHists ? nozero : pdf);
579
580 stats.readTransHist
581 .init(params()->transaction_bins)
582 .name(name() + ".readTransHist")
583 .desc("Histogram of read transactions per sample period")
584 .flags(stats.disableTransactionHists ? nozero : pdf);
585
586 stats.writeTransHist
587 .init(params()->transaction_bins)
588 .name(name() + ".writeTransHist")
589 .desc("Histogram of read transactions per sample period")
590 .flags(stats.disableTransactionHists ? nozero : pdf);
591
592 stats.readAddrDist
593 .init(0)
594 .name(name() + ".readAddrDist")
595 .desc("Read address distribution")
596 .flags(stats.disableAddrDists ? nozero : pdf);
597
598 stats.writeAddrDist
599 .init(0)
600 .name(name() + ".writeAddrDist")
601 .desc("Write address distribution")
602 .flags(stats.disableAddrDists ? nozero : pdf);
603 }
604
605 void
606 CommMonitor::samplePeriodic()
607 {
608 // the periodic stats update runs on the granularity of sample
609 // periods, but in combination with this there may also be a
610 // external resets and dumps of the stats (through schedStatEvent)
611 // causing the stats themselves to capture less than a sample
612 // period
613
614 // only capture if we have not reset the stats during the last
615 // sample period
616 if (simTicks.value() >= samplePeriodTicks) {
617 if (!stats.disableTransactionHists) {
618 stats.readTransHist.sample(stats.readTrans);
619 stats.writeTransHist.sample(stats.writeTrans);
620 }
621
622 if (!stats.disableBandwidthHists) {
623 stats.readBandwidthHist.sample(stats.readBytes / samplePeriod);
624 stats.writeBandwidthHist.sample(stats.writtenBytes / samplePeriod);
625 }
626
627 if (!stats.disableOutstandingHists) {
628 stats.outstandingReadsHist.sample(stats.outstandingReadReqs);
629 stats.outstandingWritesHist.sample(stats.outstandingWriteReqs);
630 }
631 }
632
633 // reset the sampled values
634 stats.readTrans = 0;
635 stats.writeTrans = 0;
636
637 stats.readBytes = 0;
638 stats.writtenBytes = 0;
639
640 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
641 }
642
643 void
644 CommMonitor::startup()
645 {
646 schedule(samplePeriodicEvent, curTick() + samplePeriodTicks);
647 }