RubyTester.cc revision 7823
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * Copyright (c) 2009 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "base/misc.hh"
31#include "cpu/testers/rubytest/Check.hh"
32#include "cpu/testers/rubytest/RubyTester.hh"
33#include "mem/ruby/common/Global.hh"
34#include "mem/ruby/common/SubBlock.hh"
35#include "mem/ruby/eventqueue/RubyEventQueue.hh"
36#include "mem/ruby/system/System.hh"
37#include "sim/sim_exit.hh"
38
39RubyTester::RubyTester(const Params *p)
40  : MemObject(p), checkStartEvent(this),
41    m_checks_to_complete(p->checks_to_complete),
42    m_deadlock_threshold(p->deadlock_threshold),
43    m_wakeup_frequency(p->wakeup_frequency)
44{
45    m_checks_completed = 0;
46
47    // add the check start event to the event queue
48    schedule(checkStartEvent, 1);
49}
50
51RubyTester::~RubyTester()
52{
53    delete m_checkTable_ptr;
54    for (int i = 0; i < ports.size(); i++)
55        delete ports[i];
56}
57
58void
59RubyTester::init()
60{
61    assert(ports.size() > 0);
62
63    m_last_progress_vector.resize(ports.size());
64    for (int i = 0; i < m_last_progress_vector.size(); i++) {
65        m_last_progress_vector[i] = 0;
66    }
67
68    m_num_cpu_sequencers = ports.size();
69
70    m_checkTable_ptr = new CheckTable(m_num_cpu_sequencers, this);
71}
72
73Port *
74RubyTester::getPort(const std::string &if_name, int idx)
75{
76    if (if_name != "cpuPort") {
77        panic("RubyTester::getPort: unknown port %s requested", if_name);
78    }
79
80    if (idx >= (int)ports.size()) {
81        ports.resize(idx + 1);
82    }
83
84    if (ports[idx] != NULL) {
85        panic("RubyTester::getPort: port %d already assigned", idx);
86    }
87
88    CpuPort *port = new CpuPort(csprintf("%s-port%d", name(), idx), this, idx);
89
90    ports[idx] = port;
91    return port;
92}
93
94Tick
95RubyTester::CpuPort::recvAtomic(PacketPtr pkt)
96{
97    panic("RubyTester::CpuPort::recvAtomic() not implemented!\n");
98    return 0;
99}
100
101bool
102RubyTester::CpuPort::recvTiming(PacketPtr pkt)
103{
104    // retrieve the subblock and call hitCallback
105    RubyTester::SenderState* senderState =
106        safe_cast<RubyTester::SenderState*>(pkt->senderState);
107    SubBlock* subblock = senderState->subBlock;
108    assert(subblock != NULL);
109
110    // pop the sender state from the packet
111    pkt->senderState = senderState->saved;
112
113    tester->hitCallback(idx, subblock);
114
115    // Now that the tester has completed, delete the senderState
116    // (includes sublock) and the packet, then return
117    delete senderState;
118    delete pkt->req;
119    delete pkt;
120    return true;
121}
122
123Port*
124RubyTester::getCpuPort(int idx)
125{
126    assert(idx >= 0 && idx < ports.size());
127
128    return ports[idx];
129}
130
131void
132RubyTester::hitCallback(NodeID proc, SubBlock* data)
133{
134    // Mark that we made progress
135    m_last_progress_vector[proc] = g_eventQueue_ptr->getTime();
136
137    DPRINTF(RubyTest, "completed request for proc: %d\n", proc);
138    DPRINTF(RubyTest, "addr: 0x%x, size: %d, data: ",
139            data->getAddress(), data->getSize());
140    for (int byte = 0; byte < data->getSize(); byte++) {
141        DPRINTF(RubyTest, "%d", data->getByte(byte));
142    }
143    DPRINTF(RubyTest, "\n");
144
145    // This tells us our store has 'completed' or for a load gives us
146    // back the data to make the check
147    Check* check_ptr = m_checkTable_ptr->getCheck(data->getAddress());
148    assert(check_ptr != NULL);
149    check_ptr->performCallback(proc, data);
150}
151
152void
153RubyTester::wakeup()
154{
155    if (m_checks_completed < m_checks_to_complete) {
156        // Try to perform an action or check
157        Check* check_ptr = m_checkTable_ptr->getRandomCheck();
158        assert(check_ptr != NULL);
159        check_ptr->initiate();
160
161        checkForDeadlock();
162
163        schedule(checkStartEvent, curTick() + m_wakeup_frequency);
164    } else {
165        exitSimLoop("Ruby Tester completed");
166    }
167}
168
169void
170RubyTester::checkForDeadlock()
171{
172    int size = m_last_progress_vector.size();
173    Time current_time = g_eventQueue_ptr->getTime();
174    for (int processor = 0; processor < size; processor++) {
175        if ((current_time - m_last_progress_vector[processor]) >
176                m_deadlock_threshold) {
177            panic("Deadlock detected: current_time: %d last_progress_time: %d "
178                  "difference:  %d processor: %d\n",
179                  current_time, m_last_progress_vector[processor],
180                  current_time - m_last_progress_vector[processor], processor);
181        }
182    }
183}
184
185void
186RubyTester::print(std::ostream& out) const
187{
188    out << "[RubyTester]" << std::endl;
189}
190
191RubyTester *
192RubyTesterParams::create()
193{
194    return new RubyTester(this);
195}
196