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