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