112396SRiken.Gohil@arm.com/*
212811Sandreas.sandberg@arm.com * Copyright (c) 2012-2013, 2017-2018 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/**
4412396SRiken.Gohil@arm.com * @file
4512396SRiken.Gohil@arm.com * Declaration of the trace generator that reads a trace file
4612396SRiken.Gohil@arm.com * and plays the transactions.
4712396SRiken.Gohil@arm.com */
4812396SRiken.Gohil@arm.com
4912396SRiken.Gohil@arm.com#ifndef __CPU_TRAFFIC_GEN_TRACE_GEN_HH__
5012396SRiken.Gohil@arm.com#define __CPU_TRAFFIC_GEN_TRACE_GEN_HH__
5112396SRiken.Gohil@arm.com
5212396SRiken.Gohil@arm.com#include "base/bitfield.hh"
5312396SRiken.Gohil@arm.com#include "base/intmath.hh"
5412396SRiken.Gohil@arm.com#include "base_gen.hh"
5512396SRiken.Gohil@arm.com#include "mem/packet.hh"
5612396SRiken.Gohil@arm.com#include "proto/protoio.hh"
5712396SRiken.Gohil@arm.com
5812396SRiken.Gohil@arm.com/**
5912396SRiken.Gohil@arm.com * The trace replay generator reads a trace file and plays
6012396SRiken.Gohil@arm.com * back the transactions. The trace is offset with respect to
6112396SRiken.Gohil@arm.com * the time when the state was entered.
6212396SRiken.Gohil@arm.com */
6312396SRiken.Gohil@arm.comclass TraceGen : public BaseGen
6412396SRiken.Gohil@arm.com{
6512396SRiken.Gohil@arm.com
6612396SRiken.Gohil@arm.com  private:
6712396SRiken.Gohil@arm.com
6812396SRiken.Gohil@arm.com    /**
6912396SRiken.Gohil@arm.com     * This struct stores a line in the trace file.
7012396SRiken.Gohil@arm.com     */
7112396SRiken.Gohil@arm.com    struct TraceElement {
7212396SRiken.Gohil@arm.com
7312396SRiken.Gohil@arm.com        /** Specifies if the request is to be a read or a write */
7412396SRiken.Gohil@arm.com        MemCmd cmd;
7512396SRiken.Gohil@arm.com
7612396SRiken.Gohil@arm.com        /** The address for the request */
7712396SRiken.Gohil@arm.com        Addr addr;
7812396SRiken.Gohil@arm.com
7912396SRiken.Gohil@arm.com        /** The size of the access for the request */
8012396SRiken.Gohil@arm.com        Addr blocksize;
8112396SRiken.Gohil@arm.com
8212396SRiken.Gohil@arm.com        /** The time at which the request should be sent */
8312396SRiken.Gohil@arm.com        Tick tick;
8412396SRiken.Gohil@arm.com
8512396SRiken.Gohil@arm.com        /** Potential request flags to use */
8612396SRiken.Gohil@arm.com        Request::FlagsType flags;
8712396SRiken.Gohil@arm.com
8812396SRiken.Gohil@arm.com        /**
8912396SRiken.Gohil@arm.com         * Check validity of this element.
9012396SRiken.Gohil@arm.com         *
9112396SRiken.Gohil@arm.com         * @return if this element is valid
9212396SRiken.Gohil@arm.com         */
9312396SRiken.Gohil@arm.com        bool isValid() const {
9412396SRiken.Gohil@arm.com            return cmd != MemCmd::InvalidCmd;
9512396SRiken.Gohil@arm.com        }
9612396SRiken.Gohil@arm.com
9712396SRiken.Gohil@arm.com        /**
9812396SRiken.Gohil@arm.com         * Make this element invalid.
9912396SRiken.Gohil@arm.com         */
10012396SRiken.Gohil@arm.com        void clear() {
10112396SRiken.Gohil@arm.com            cmd = MemCmd::InvalidCmd;
10212396SRiken.Gohil@arm.com        }
10312396SRiken.Gohil@arm.com    };
10412396SRiken.Gohil@arm.com
10512396SRiken.Gohil@arm.com    /**
10612396SRiken.Gohil@arm.com     * The InputStream encapsulates a trace file and the
10712396SRiken.Gohil@arm.com     * internal buffers and populates TraceElements based on
10812396SRiken.Gohil@arm.com     * the input.
10912396SRiken.Gohil@arm.com     */
11012396SRiken.Gohil@arm.com    class InputStream
11112396SRiken.Gohil@arm.com    {
11212396SRiken.Gohil@arm.com
11312396SRiken.Gohil@arm.com      private:
11412396SRiken.Gohil@arm.com
11512396SRiken.Gohil@arm.com        /// Input file stream for the protobuf trace
11612396SRiken.Gohil@arm.com        ProtoInputStream trace;
11712396SRiken.Gohil@arm.com
11812396SRiken.Gohil@arm.com      public:
11912396SRiken.Gohil@arm.com
12012396SRiken.Gohil@arm.com        /**
12112396SRiken.Gohil@arm.com         * Create a trace input stream for a given file name.
12212396SRiken.Gohil@arm.com         *
12312396SRiken.Gohil@arm.com         * @param filename Path to the file to read from
12412396SRiken.Gohil@arm.com         */
12512396SRiken.Gohil@arm.com        InputStream(const std::string& filename);
12612396SRiken.Gohil@arm.com
12712396SRiken.Gohil@arm.com        /**
12812396SRiken.Gohil@arm.com         * Reset the stream such that it can be played once
12912396SRiken.Gohil@arm.com         * again.
13012396SRiken.Gohil@arm.com         */
13112396SRiken.Gohil@arm.com        void reset();
13212396SRiken.Gohil@arm.com
13312396SRiken.Gohil@arm.com        /**
13412396SRiken.Gohil@arm.com         * Check the trace header to make sure that it is of the right
13512396SRiken.Gohil@arm.com         * format.
13612396SRiken.Gohil@arm.com         */
13712396SRiken.Gohil@arm.com        void init();
13812396SRiken.Gohil@arm.com
13912396SRiken.Gohil@arm.com        /**
14012396SRiken.Gohil@arm.com         * Attempt to read a trace element from the stream,
14112396SRiken.Gohil@arm.com         * and also notify the caller if the end of the file
14212396SRiken.Gohil@arm.com         * was reached.
14312396SRiken.Gohil@arm.com         *
14412396SRiken.Gohil@arm.com         * @param element Trace element to populate
14512396SRiken.Gohil@arm.com         * @return True if an element could be read successfully
14612396SRiken.Gohil@arm.com         */
14712396SRiken.Gohil@arm.com        bool read(TraceElement& element);
14812396SRiken.Gohil@arm.com    };
14912396SRiken.Gohil@arm.com
15012396SRiken.Gohil@arm.com  public:
15112396SRiken.Gohil@arm.com
15212396SRiken.Gohil@arm.com    /**
15312396SRiken.Gohil@arm.com     * Create a trace generator.
15412396SRiken.Gohil@arm.com     *
15512844Sgiacomo.travaglini@arm.com     * @param obj SimObject owning this sequence generator
15612844Sgiacomo.travaglini@arm.com     * @param master_id MasterID related to the memory requests
15712396SRiken.Gohil@arm.com     * @param _duration duration of this state before transitioning
15812396SRiken.Gohil@arm.com     * @param trace_file File to read the transactions from
15912396SRiken.Gohil@arm.com     * @param addr_offset Positive offset to add to trace address
16012396SRiken.Gohil@arm.com     */
16112844Sgiacomo.travaglini@arm.com    TraceGen(SimObject &obj, MasterID master_id, Tick _duration,
16212396SRiken.Gohil@arm.com             const std::string& trace_file, Addr addr_offset)
16312844Sgiacomo.travaglini@arm.com        : BaseGen(obj, master_id, _duration),
16412396SRiken.Gohil@arm.com          trace(trace_file),
16512396SRiken.Gohil@arm.com          tickOffset(0),
16612396SRiken.Gohil@arm.com          addrOffset(addr_offset),
16712396SRiken.Gohil@arm.com          traceComplete(false)
16812396SRiken.Gohil@arm.com    {
16912396SRiken.Gohil@arm.com    }
17012396SRiken.Gohil@arm.com
17112396SRiken.Gohil@arm.com    void enter();
17212396SRiken.Gohil@arm.com
17312396SRiken.Gohil@arm.com    PacketPtr getNextPacket();
17412396SRiken.Gohil@arm.com
17512396SRiken.Gohil@arm.com    void exit();
17612396SRiken.Gohil@arm.com
17712396SRiken.Gohil@arm.com    /**
17812396SRiken.Gohil@arm.com     * Returns the tick when the next request should be generated. If
17912396SRiken.Gohil@arm.com     * the end of the file has been reached, it returns MaxTick to
18012396SRiken.Gohil@arm.com     * indicate that there will be no more requests.
18112396SRiken.Gohil@arm.com     */
18212396SRiken.Gohil@arm.com    Tick nextPacketTick(bool elastic, Tick delay) const;
18312396SRiken.Gohil@arm.com
18412396SRiken.Gohil@arm.com  private:
18512396SRiken.Gohil@arm.com
18612396SRiken.Gohil@arm.com    /** Input stream used for reading the input trace file */
18712396SRiken.Gohil@arm.com    InputStream trace;
18812396SRiken.Gohil@arm.com
18912396SRiken.Gohil@arm.com    /** Store the current and next element in the trace */
19012396SRiken.Gohil@arm.com    TraceElement currElement;
19112396SRiken.Gohil@arm.com    TraceElement nextElement;
19212396SRiken.Gohil@arm.com
19312396SRiken.Gohil@arm.com    /**
19412396SRiken.Gohil@arm.com     * Stores the time when the state was entered. This is to add an
19512396SRiken.Gohil@arm.com     * offset to the times stored in the trace file. This is mutable
19612396SRiken.Gohil@arm.com     * to allow us to change it as part of nextPacketTick.
19712396SRiken.Gohil@arm.com     */
19812396SRiken.Gohil@arm.com    mutable Tick tickOffset;
19912396SRiken.Gohil@arm.com
20012396SRiken.Gohil@arm.com    /**
20112396SRiken.Gohil@arm.com     * Offset for memory requests. Used to shift the trace
20212396SRiken.Gohil@arm.com     * away from the CPU address space.
20312396SRiken.Gohil@arm.com     */
20412396SRiken.Gohil@arm.com    Addr addrOffset;
20512396SRiken.Gohil@arm.com
20612396SRiken.Gohil@arm.com    /**
20712396SRiken.Gohil@arm.com     * Set to true when the trace replay for one instance of
20812396SRiken.Gohil@arm.com     * state is complete.
20912396SRiken.Gohil@arm.com     */
21012396SRiken.Gohil@arm.com    bool traceComplete;
21112396SRiken.Gohil@arm.com};
21212396SRiken.Gohil@arm.com
21312396SRiken.Gohil@arm.com#endif
214