112396SRiken.Gohil@arm.com/*
212396SRiken.Gohil@arm.com * Copyright (c) 2012-2013, 2016-2017 ARM Limited
312396SRiken.Gohil@arm.com * All rights reserved
412396SRiken.Gohil@arm.com *
512396SRiken.Gohil@arm.com * The license below extends only to copyright in the software and shall
612396SRiken.Gohil@arm.com * not be construed as granting a license to any other intellectual
712396SRiken.Gohil@arm.com * property including but not limited to intellectual property relating
812396SRiken.Gohil@arm.com * to a hardware implementation of the functionality of the software
912396SRiken.Gohil@arm.com * licensed here under.  You may use the software subject to the license
1012396SRiken.Gohil@arm.com * terms below provided that you ensure that this notice is replicated
1112396SRiken.Gohil@arm.com * unmodified and in its entirety in all distributions of the software,
1212396SRiken.Gohil@arm.com * modified or unmodified, in source code or in binary form.
1312396SRiken.Gohil@arm.com *
1412396SRiken.Gohil@arm.com * Redistribution and use in source and binary forms, with or without
1512396SRiken.Gohil@arm.com * modification, are permitted provided that the following conditions are
1612396SRiken.Gohil@arm.com * met: redistributions of source code must retain the above copyright
1712396SRiken.Gohil@arm.com * notice, this list of conditions and the following disclaimer;
1812396SRiken.Gohil@arm.com * redistributions in binary form must reproduce the above copyright
1912396SRiken.Gohil@arm.com * notice, this list of conditions and the following disclaimer in the
2012396SRiken.Gohil@arm.com * documentation and/or other materials provided with the distribution;
2112396SRiken.Gohil@arm.com * neither the name of the copyright holders nor the names of its
2212396SRiken.Gohil@arm.com * contributors may be used to endorse or promote products derived from
2312396SRiken.Gohil@arm.com * this software without specific prior written permission.
2412396SRiken.Gohil@arm.com *
2512396SRiken.Gohil@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612396SRiken.Gohil@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712396SRiken.Gohil@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812396SRiken.Gohil@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912396SRiken.Gohil@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012396SRiken.Gohil@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112396SRiken.Gohil@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212396SRiken.Gohil@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312396SRiken.Gohil@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3412396SRiken.Gohil@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3512396SRiken.Gohil@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3612396SRiken.Gohil@arm.com *
3712396SRiken.Gohil@arm.com * Authors: Thomas Grass
3812396SRiken.Gohil@arm.com *          Andreas Hansson
3912396SRiken.Gohil@arm.com *          Sascha Bischoff
4012396SRiken.Gohil@arm.com *          Neha Agarwal
4112396SRiken.Gohil@arm.com */
4212396SRiken.Gohil@arm.com
4312396SRiken.Gohil@arm.com#include "cpu/testers/traffic_gen/trace_gen.hh"
4412396SRiken.Gohil@arm.com
4512396SRiken.Gohil@arm.com#include <algorithm>
4612396SRiken.Gohil@arm.com
4712396SRiken.Gohil@arm.com#include "base/random.hh"
4812396SRiken.Gohil@arm.com#include "base/trace.hh"
4912396SRiken.Gohil@arm.com#include "debug/TrafficGen.hh"
5012396SRiken.Gohil@arm.com#include "proto/packet.pb.h"
5112396SRiken.Gohil@arm.com
5212396SRiken.Gohil@arm.comTraceGen::InputStream::InputStream(const std::string& filename)
5312396SRiken.Gohil@arm.com    : trace(filename)
5412396SRiken.Gohil@arm.com{
5512396SRiken.Gohil@arm.com    init();
5612396SRiken.Gohil@arm.com}
5712396SRiken.Gohil@arm.com
5812396SRiken.Gohil@arm.comvoid
5912396SRiken.Gohil@arm.comTraceGen::InputStream::init()
6012396SRiken.Gohil@arm.com{
6112396SRiken.Gohil@arm.com    // Create a protobuf message for the header and read it from the stream
6212396SRiken.Gohil@arm.com    ProtoMessage::PacketHeader header_msg;
6312396SRiken.Gohil@arm.com    if (!trace.read(header_msg)) {
6412396SRiken.Gohil@arm.com        panic("Failed to read packet header from trace\n");
6512396SRiken.Gohil@arm.com    } else if (header_msg.tick_freq() != SimClock::Frequency) {
6612396SRiken.Gohil@arm.com        panic("Trace was recorded with a different tick frequency %d\n",
6712396SRiken.Gohil@arm.com              header_msg.tick_freq());
6812396SRiken.Gohil@arm.com    }
6912396SRiken.Gohil@arm.com}
7012396SRiken.Gohil@arm.com
7112396SRiken.Gohil@arm.comvoid
7212396SRiken.Gohil@arm.comTraceGen::InputStream::reset()
7312396SRiken.Gohil@arm.com{
7412396SRiken.Gohil@arm.com    trace.reset();
7512396SRiken.Gohil@arm.com    init();
7612396SRiken.Gohil@arm.com}
7712396SRiken.Gohil@arm.com
7812396SRiken.Gohil@arm.combool
7912396SRiken.Gohil@arm.comTraceGen::InputStream::read(TraceElement& element)
8012396SRiken.Gohil@arm.com{
8112396SRiken.Gohil@arm.com    ProtoMessage::Packet pkt_msg;
8212396SRiken.Gohil@arm.com    if (trace.read(pkt_msg)) {
8312396SRiken.Gohil@arm.com        element.cmd = pkt_msg.cmd();
8412396SRiken.Gohil@arm.com        element.addr = pkt_msg.addr();
8512396SRiken.Gohil@arm.com        element.blocksize = pkt_msg.size();
8612396SRiken.Gohil@arm.com        element.tick = pkt_msg.tick();
8712396SRiken.Gohil@arm.com        element.flags = pkt_msg.has_flags() ? pkt_msg.flags() : 0;
8812396SRiken.Gohil@arm.com        return true;
8912396SRiken.Gohil@arm.com    }
9012396SRiken.Gohil@arm.com
9112396SRiken.Gohil@arm.com    // We have reached the end of the file
9212396SRiken.Gohil@arm.com    return false;
9312396SRiken.Gohil@arm.com}
9412396SRiken.Gohil@arm.com
9512396SRiken.Gohil@arm.comTick
9612396SRiken.Gohil@arm.comTraceGen::nextPacketTick(bool elastic, Tick delay) const
9712396SRiken.Gohil@arm.com{
9812396SRiken.Gohil@arm.com    if (traceComplete) {
9912396SRiken.Gohil@arm.com        DPRINTF(TrafficGen, "No next tick as trace is finished\n");
10012396SRiken.Gohil@arm.com        // We are at the end of the file, thus we have no more data in
10112396SRiken.Gohil@arm.com        // the trace Return MaxTick to signal that there will be no
10212396SRiken.Gohil@arm.com        // more transactions in this active period for the state.
10312396SRiken.Gohil@arm.com        return MaxTick;
10412396SRiken.Gohil@arm.com    }
10512396SRiken.Gohil@arm.com
10612396SRiken.Gohil@arm.com    assert(nextElement.isValid());
10712396SRiken.Gohil@arm.com
10812396SRiken.Gohil@arm.com    DPRINTF(TrafficGen, "Next packet tick is %d\n", tickOffset +
10912396SRiken.Gohil@arm.com            nextElement.tick);
11012396SRiken.Gohil@arm.com
11112396SRiken.Gohil@arm.com    // if the playback is supposed to be elastic, add the delay
11212396SRiken.Gohil@arm.com    if (elastic)
11312396SRiken.Gohil@arm.com        tickOffset += delay;
11412396SRiken.Gohil@arm.com
11512396SRiken.Gohil@arm.com    return std::max(tickOffset + nextElement.tick, curTick());
11612396SRiken.Gohil@arm.com}
11712396SRiken.Gohil@arm.com
11812396SRiken.Gohil@arm.comvoid
11912396SRiken.Gohil@arm.comTraceGen::enter()
12012396SRiken.Gohil@arm.com{
12112396SRiken.Gohil@arm.com    // update the trace offset to the time where the state was entered.
12212396SRiken.Gohil@arm.com    tickOffset = curTick();
12312396SRiken.Gohil@arm.com
12412396SRiken.Gohil@arm.com    // clear everything
12512396SRiken.Gohil@arm.com    currElement.clear();
12612396SRiken.Gohil@arm.com
12712396SRiken.Gohil@arm.com    // read the first element in the file and set the complete flag
12812396SRiken.Gohil@arm.com    traceComplete = !trace.read(nextElement);
12912396SRiken.Gohil@arm.com}
13012396SRiken.Gohil@arm.com
13112396SRiken.Gohil@arm.comPacketPtr
13212396SRiken.Gohil@arm.comTraceGen::getNextPacket()
13312396SRiken.Gohil@arm.com{
13412396SRiken.Gohil@arm.com    // shift things one step forward
13512396SRiken.Gohil@arm.com    currElement = nextElement;
13612396SRiken.Gohil@arm.com    nextElement.clear();
13712396SRiken.Gohil@arm.com
13812396SRiken.Gohil@arm.com    // read the next element and set the complete flag
13912396SRiken.Gohil@arm.com    traceComplete = !trace.read(nextElement);
14012396SRiken.Gohil@arm.com
14112396SRiken.Gohil@arm.com    // it is the responsibility of the traceComplete flag to ensure we
14212396SRiken.Gohil@arm.com    // always have a valid element here
14312396SRiken.Gohil@arm.com    assert(currElement.isValid());
14412396SRiken.Gohil@arm.com
14512396SRiken.Gohil@arm.com    DPRINTF(TrafficGen, "TraceGen::getNextPacket: %c %d %d %d 0x%x\n",
14612396SRiken.Gohil@arm.com            currElement.cmd.isRead() ? 'r' : 'w',
14712396SRiken.Gohil@arm.com            currElement.addr,
14812396SRiken.Gohil@arm.com            currElement.blocksize,
14912396SRiken.Gohil@arm.com            currElement.tick,
15012396SRiken.Gohil@arm.com            currElement.flags);
15112396SRiken.Gohil@arm.com
15212396SRiken.Gohil@arm.com    PacketPtr pkt = getPacket(currElement.addr + addrOffset,
15312396SRiken.Gohil@arm.com                              currElement.blocksize,
15412396SRiken.Gohil@arm.com                              currElement.cmd, currElement.flags);
15512396SRiken.Gohil@arm.com
15612396SRiken.Gohil@arm.com    if (!traceComplete)
15712396SRiken.Gohil@arm.com        DPRINTF(TrafficGen, "nextElement: %c addr %d size %d tick %d (%d)\n",
15812396SRiken.Gohil@arm.com                nextElement.cmd.isRead() ? 'r' : 'w',
15912396SRiken.Gohil@arm.com                nextElement.addr,
16012396SRiken.Gohil@arm.com                nextElement.blocksize,
16112396SRiken.Gohil@arm.com                nextElement.tick + tickOffset,
16212396SRiken.Gohil@arm.com                nextElement.tick);
16312396SRiken.Gohil@arm.com
16412396SRiken.Gohil@arm.com    return pkt;
16512396SRiken.Gohil@arm.com}
16612396SRiken.Gohil@arm.com
16712396SRiken.Gohil@arm.comvoid
16812396SRiken.Gohil@arm.comTraceGen::exit()
16912396SRiken.Gohil@arm.com{
17012396SRiken.Gohil@arm.com    // Check if we reached the end of the trace file. If we did not
17112396SRiken.Gohil@arm.com    // then we want to generate a warning stating that not the entire
17212396SRiken.Gohil@arm.com    // trace was played.
17312396SRiken.Gohil@arm.com    if (!traceComplete) {
17412396SRiken.Gohil@arm.com        warn("Trace player %s was unable to replay the entire trace!\n",
17512396SRiken.Gohil@arm.com             name());
17612396SRiken.Gohil@arm.com    }
17712396SRiken.Gohil@arm.com
17812396SRiken.Gohil@arm.com    // Clear any flags and start over again from the beginning of the
17912396SRiken.Gohil@arm.com    // file
18012396SRiken.Gohil@arm.com    trace.reset();
18112396SRiken.Gohil@arm.com}
182