1/*
2 * Copyright (c) 2012-2013, 2016-2017 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 here under.  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 *          Sascha Bischoff
40 *          Neha Agarwal
41 */
42
43#include "cpu/testers/traffic_gen/trace_gen.hh"
44
45#include <algorithm>
46
47#include "base/random.hh"
48#include "base/trace.hh"
49#include "debug/TrafficGen.hh"
50#include "proto/packet.pb.h"
51
52TraceGen::InputStream::InputStream(const std::string& filename)
53    : trace(filename)
54{
55    init();
56}
57
58void
59TraceGen::InputStream::init()
60{
61    // Create a protobuf message for the header and read it from the stream
62    ProtoMessage::PacketHeader header_msg;
63    if (!trace.read(header_msg)) {
64        panic("Failed to read packet header from trace\n");
65    } else if (header_msg.tick_freq() != SimClock::Frequency) {
66        panic("Trace was recorded with a different tick frequency %d\n",
67              header_msg.tick_freq());
68    }
69}
70
71void
72TraceGen::InputStream::reset()
73{
74    trace.reset();
75    init();
76}
77
78bool
79TraceGen::InputStream::read(TraceElement& element)
80{
81    ProtoMessage::Packet pkt_msg;
82    if (trace.read(pkt_msg)) {
83        element.cmd = pkt_msg.cmd();
84        element.addr = pkt_msg.addr();
85        element.blocksize = pkt_msg.size();
86        element.tick = pkt_msg.tick();
87        element.flags = pkt_msg.has_flags() ? pkt_msg.flags() : 0;
88        return true;
89    }
90
91    // We have reached the end of the file
92    return false;
93}
94
95Tick
96TraceGen::nextPacketTick(bool elastic, Tick delay) const
97{
98    if (traceComplete) {
99        DPRINTF(TrafficGen, "No next tick as trace is finished\n");
100        // We are at the end of the file, thus we have no more data in
101        // the trace Return MaxTick to signal that there will be no
102        // more transactions in this active period for the state.
103        return MaxTick;
104    }
105
106    assert(nextElement.isValid());
107
108    DPRINTF(TrafficGen, "Next packet tick is %d\n", tickOffset +
109            nextElement.tick);
110
111    // if the playback is supposed to be elastic, add the delay
112    if (elastic)
113        tickOffset += delay;
114
115    return std::max(tickOffset + nextElement.tick, curTick());
116}
117
118void
119TraceGen::enter()
120{
121    // update the trace offset to the time where the state was entered.
122    tickOffset = curTick();
123
124    // clear everything
125    currElement.clear();
126
127    // read the first element in the file and set the complete flag
128    traceComplete = !trace.read(nextElement);
129}
130
131PacketPtr
132TraceGen::getNextPacket()
133{
134    // shift things one step forward
135    currElement = nextElement;
136    nextElement.clear();
137
138    // read the next element and set the complete flag
139    traceComplete = !trace.read(nextElement);
140
141    // it is the responsibility of the traceComplete flag to ensure we
142    // always have a valid element here
143    assert(currElement.isValid());
144
145    DPRINTF(TrafficGen, "TraceGen::getNextPacket: %c %d %d %d 0x%x\n",
146            currElement.cmd.isRead() ? 'r' : 'w',
147            currElement.addr,
148            currElement.blocksize,
149            currElement.tick,
150            currElement.flags);
151
152    PacketPtr pkt = getPacket(currElement.addr + addrOffset,
153                              currElement.blocksize,
154                              currElement.cmd, currElement.flags);
155
156    if (!traceComplete)
157        DPRINTF(TrafficGen, "nextElement: %c addr %d size %d tick %d (%d)\n",
158                nextElement.cmd.isRead() ? 'r' : 'w',
159                nextElement.addr,
160                nextElement.blocksize,
161                nextElement.tick + tickOffset,
162                nextElement.tick);
163
164    return pkt;
165}
166
167void
168TraceGen::exit()
169{
170    // Check if we reached the end of the trace file. If we did not
171    // then we want to generate a warning stating that not the entire
172    // trace was played.
173    if (!traceComplete) {
174        warn("Trace player %s was unable to replay the entire trace!\n",
175             name());
176    }
177
178    // Clear any flags and start over again from the beginning of the
179    // file
180    trace.reset();
181}
182