RubyTester.cc revision 11800
113271Sgabeblack@google.com/*
213271Sgabeblack@google.com * Copyright (c) 2012-2013 ARM Limited
313271Sgabeblack@google.com * All rights reserved
413271Sgabeblack@google.com *
513271Sgabeblack@google.com * The license below extends only to copyright in the software and shall
613271Sgabeblack@google.com * not be construed as granting a license to any other intellectual
713271Sgabeblack@google.com * property including but not limited to intellectual property relating
813271Sgabeblack@google.com * to a hardware implementation of the functionality of the software
913271Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1013271Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1113271Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1213271Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1313271Sgabeblack@google.com *
1413271Sgabeblack@google.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
1513271Sgabeblack@google.com * Copyright (c) 2009 Advanced Micro Devices, Inc.
1613271Sgabeblack@google.com * All rights reserved.
1713271Sgabeblack@google.com *
1813271Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1913271Sgabeblack@google.com * modification, are permitted provided that the following conditions are
2012852Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2112852Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2212852Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2312852Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2412852Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2512852Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2612852Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2712852Sgabeblack@google.com * this software without specific prior written permission.
2812852Sgabeblack@google.com *
2912852Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012852Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112852Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212852Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312852Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412852Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3512852Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612852Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3712852Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812852Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3912852Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012852Sgabeblack@google.com */
4112852Sgabeblack@google.com
4212852Sgabeblack@google.com#include "cpu/testers/rubytest/RubyTester.hh"
4312852Sgabeblack@google.com
4412852Sgabeblack@google.com#include "base/misc.hh"
4512852Sgabeblack@google.com#include "base/trace.hh"
4612852Sgabeblack@google.com#include "cpu/testers/rubytest/Check.hh"
4712852Sgabeblack@google.com#include "debug/RubyTest.hh"
4812852Sgabeblack@google.com#include "mem/ruby/common/SubBlock.hh"
4913271Sgabeblack@google.com#include "sim/sim_exit.hh"
5013271Sgabeblack@google.com#include "sim/system.hh"
5113271Sgabeblack@google.com
5213271Sgabeblack@google.comRubyTester::RubyTester(const Params *p)
5313271Sgabeblack@google.com  : MemObject(p), checkStartEvent(this),
5412852Sgabeblack@google.com    _masterId(p->system->getMasterId(name())),
5512852Sgabeblack@google.com    m_checkTable_ptr(nullptr),
5612852Sgabeblack@google.com    m_num_cpus(p->num_cpus),
5712852Sgabeblack@google.com    m_checks_to_complete(p->checks_to_complete),
5812852Sgabeblack@google.com    m_deadlock_threshold(p->deadlock_threshold),
5913271Sgabeblack@google.com    m_num_writers(0),
6012852Sgabeblack@google.com    m_num_readers(0),
6112852Sgabeblack@google.com    m_wakeup_frequency(p->wakeup_frequency),
6212852Sgabeblack@google.com    m_check_flush(p->check_flush),
6312852Sgabeblack@google.com    m_num_inst_only_ports(p->port_cpuInstPort_connection_count),
6413271Sgabeblack@google.com    m_num_inst_data_ports(p->port_cpuInstDataPort_connection_count)
6513271Sgabeblack@google.com{
6613271Sgabeblack@google.com    m_checks_completed = 0;
6713271Sgabeblack@google.com
6813271Sgabeblack@google.com    //
6913271Sgabeblack@google.com    // Create the requested inst and data ports and place them on the
7013271Sgabeblack@google.com    // appropriate read and write port lists.  The reason for the subtle
7113271Sgabeblack@google.com    // difference between inst and data ports vs. read and write ports is
7213271Sgabeblack@google.com    // from the tester's perspective, it only needs to know whether a port
7313271Sgabeblack@google.com    // supports reads (checks) or writes (actions).  Meanwhile, the protocol
7413271Sgabeblack@google.com    // controllers have data ports (support read and writes) or inst ports
7513271Sgabeblack@google.com    // (support only reads).
7613271Sgabeblack@google.com    // Note: the inst ports are the lowest elements of the readPort vector,
7713271Sgabeblack@google.com    // then the data ports are added to the readPort vector
7813271Sgabeblack@google.com    //
7913271Sgabeblack@google.com    int idx = 0;
8013271Sgabeblack@google.com    for (int i = 0; i < p->port_cpuInstPort_connection_count; ++i) {
8113271Sgabeblack@google.com        readPorts.push_back(new CpuPort(csprintf("%s-instPort%d", name(), i),
8213271Sgabeblack@google.com                                        this, i, idx));
8313271Sgabeblack@google.com        idx++;
8413271Sgabeblack@google.com    }
8513271Sgabeblack@google.com    for (int i = 0; i < p->port_cpuInstDataPort_connection_count; ++i) {
8613271Sgabeblack@google.com        CpuPort *port = new CpuPort(csprintf("%s-instDataPort%d", name(), i),
8713271Sgabeblack@google.com                                    this, i, idx);
8813271Sgabeblack@google.com        readPorts.push_back(port);
8913271Sgabeblack@google.com        writePorts.push_back(port);
9013271Sgabeblack@google.com        idx++;
9113271Sgabeblack@google.com    }
9213271Sgabeblack@google.com    for (int i = 0; i < p->port_cpuDataPort_connection_count; ++i) {
9313271Sgabeblack@google.com        CpuPort *port = new CpuPort(csprintf("%s-dataPort%d", name(), i),
9413271Sgabeblack@google.com                                    this, i, idx);
9513271Sgabeblack@google.com        readPorts.push_back(port);
9613271Sgabeblack@google.com        writePorts.push_back(port);
9713271Sgabeblack@google.com        idx++;
9813271Sgabeblack@google.com    }
9913271Sgabeblack@google.com
10013271Sgabeblack@google.com    // add the check start event to the event queue
10113271Sgabeblack@google.com    schedule(checkStartEvent, 1);
10213271Sgabeblack@google.com}
10313271Sgabeblack@google.com
10413271Sgabeblack@google.comRubyTester::~RubyTester()
10513271Sgabeblack@google.com{
10613271Sgabeblack@google.com    delete m_checkTable_ptr;
10713271Sgabeblack@google.com    // Only delete the readPorts since the writePorts are just a subset
10813271Sgabeblack@google.com    for (int i = 0; i < readPorts.size(); i++)
10913271Sgabeblack@google.com        delete readPorts[i];
11013271Sgabeblack@google.com}
11113271Sgabeblack@google.com
11212852Sgabeblack@google.comvoid
11312852Sgabeblack@google.comRubyTester::init()
11412852Sgabeblack@google.com{
115    assert(writePorts.size() > 0 && readPorts.size() > 0);
116
117    m_last_progress_vector.resize(m_num_cpus);
118    for (int i = 0; i < m_last_progress_vector.size(); i++) {
119        m_last_progress_vector[i] = Cycles(0);
120    }
121
122    m_num_writers = writePorts.size();
123    m_num_readers = readPorts.size();
124    assert(m_num_readers == m_num_cpus);
125
126    m_checkTable_ptr = new CheckTable(m_num_writers, m_num_readers, this);
127}
128
129BaseMasterPort &
130RubyTester::getMasterPort(const std::string &if_name, PortID idx)
131{
132    if (if_name != "cpuInstPort" && if_name != "cpuInstDataPort" &&
133        if_name != "cpuDataPort") {
134        // pass it along to our super class
135        return MemObject::getMasterPort(if_name, idx);
136    } else {
137        if (if_name == "cpuInstPort") {
138            if (idx > m_num_inst_only_ports) {
139                panic("RubyTester::getMasterPort: unknown inst port %d\n",
140                      idx);
141            }
142            //
143            // inst ports map to the lowest readPort elements
144            //
145            return *readPorts[idx];
146        } else if (if_name == "cpuInstDataPort") {
147            if (idx > m_num_inst_data_ports) {
148                panic("RubyTester::getMasterPort: unknown inst+data port %d\n",
149                      idx);
150            }
151            int read_idx = idx + m_num_inst_only_ports;
152            //
153            // inst+data ports map to the next readPort elements
154            //
155            return *readPorts[read_idx];
156        } else {
157            assert(if_name == "cpuDataPort");
158            //
159            // data only ports map to the final readPort elements
160            //
161            if (idx > (static_cast<int>(readPorts.size()) -
162                       (m_num_inst_only_ports + m_num_inst_data_ports))) {
163                panic("RubyTester::getMasterPort: unknown data port %d\n",
164                      idx);
165            }
166            int read_idx = idx + m_num_inst_only_ports + m_num_inst_data_ports;
167            return *readPorts[read_idx];
168        }
169        // Note: currently the Ruby Tester does not support write only ports
170        // but that could easily be added here
171    }
172}
173
174bool
175RubyTester::CpuPort::recvTimingResp(PacketPtr pkt)
176{
177    // retrieve the subblock and call hitCallback
178    RubyTester::SenderState* senderState =
179        safe_cast<RubyTester::SenderState*>(pkt->senderState);
180    SubBlock& subblock = senderState->subBlock;
181
182    tester->hitCallback(globalIdx, &subblock);
183
184    // Now that the tester has completed, delete the senderState
185    // (includes sublock) and the packet, then return
186    delete pkt->senderState;
187    delete pkt->req;
188    delete pkt;
189    return true;
190}
191
192bool
193RubyTester::isInstOnlyCpuPort(int idx)
194{
195    return idx < m_num_inst_only_ports;
196}
197
198bool
199RubyTester::isInstDataCpuPort(int idx)
200{
201    return ((idx >= m_num_inst_only_ports) &&
202            (idx < (m_num_inst_only_ports + m_num_inst_data_ports)));
203}
204
205MasterPort*
206RubyTester::getReadableCpuPort(int idx)
207{
208    assert(idx >= 0 && idx < readPorts.size());
209
210    return readPorts[idx];
211}
212
213MasterPort*
214RubyTester::getWritableCpuPort(int idx)
215{
216    assert(idx >= 0 && idx < writePorts.size());
217
218    return writePorts[idx];
219}
220
221void
222RubyTester::hitCallback(NodeID proc, SubBlock* data)
223{
224    // Mark that we made progress
225    m_last_progress_vector[proc] = curCycle();
226
227    DPRINTF(RubyTest, "completed request for proc: %d", proc);
228    DPRINTFR(RubyTest, " addr: 0x%x, size: %d, data: ",
229            data->getAddress(), data->getSize());
230    for (int byte = 0; byte < data->getSize(); byte++) {
231        DPRINTFR(RubyTest, "%d ", data->getByte(byte));
232    }
233    DPRINTFR(RubyTest, "\n");
234
235    // This tells us our store has 'completed' or for a load gives us
236    // back the data to make the check
237    Check* check_ptr = m_checkTable_ptr->getCheck(data->getAddress());
238    assert(check_ptr != NULL);
239    check_ptr->performCallback(proc, data, curCycle());
240}
241
242void
243RubyTester::wakeup()
244{
245    if (m_checks_completed < m_checks_to_complete) {
246        // Try to perform an action or check
247        Check* check_ptr = m_checkTable_ptr->getRandomCheck();
248        assert(check_ptr != NULL);
249        check_ptr->initiate();
250
251        checkForDeadlock();
252
253        schedule(checkStartEvent, curTick() + m_wakeup_frequency);
254    } else {
255        exitSimLoop("Ruby Tester completed");
256    }
257}
258
259void
260RubyTester::checkForDeadlock()
261{
262    int size = m_last_progress_vector.size();
263    Cycles current_time = curCycle();
264    for (int processor = 0; processor < size; processor++) {
265        if ((current_time - m_last_progress_vector[processor]) >
266                m_deadlock_threshold) {
267            panic("Deadlock detected: current_time: %d last_progress_time: %d "
268                  "difference:  %d processor: %d\n",
269                  current_time, m_last_progress_vector[processor],
270                  current_time - m_last_progress_vector[processor], processor);
271        }
272    }
273}
274
275void
276RubyTester::print(std::ostream& out) const
277{
278    out << "[RubyTester]" << std::endl;
279}
280
281RubyTester *
282RubyTesterParams::create()
283{
284    return new RubyTester(this);
285}
286