Check.cc revision 9475:736909f5c13b
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 "cpu/testers/rubytest/Check.hh"
31#include "debug/RubyTest.hh"
32#include "mem/ruby/common/SubBlock.hh"
33#include "mem/ruby/system/Sequencer.hh"
34#include "mem/ruby/system/System.hh"
35
36typedef RubyTester::SenderState SenderState;
37
38Check::Check(const Address& address, const Address& pc,
39             int _num_writers, int _num_readers, RubyTester* _tester)
40    : m_num_writers(_num_writers), m_num_readers(_num_readers),
41      m_tester_ptr(_tester)
42{
43    m_status = TesterStatus_Idle;
44
45    pickValue();
46    pickInitiatingNode();
47    changeAddress(address);
48    m_pc = pc;
49    m_access_mode = RubyAccessMode(random() % RubyAccessMode_NUM);
50    m_store_count = 0;
51}
52
53void
54Check::initiate()
55{
56    DPRINTF(RubyTest, "initiating\n");
57    debugPrint();
58
59    // currently no protocols support prefetches
60    if (false && (random() & 0xf) == 0) {
61        initiatePrefetch(); // Prefetch from random processor
62    }
63
64    if (m_tester_ptr->getCheckFlush() && (random() & 0xff) == 0) {
65        initiateFlush(); // issue a Flush request from random processor
66    }
67
68    if (m_status == TesterStatus_Idle) {
69        initiateAction();
70    } else if (m_status == TesterStatus_Ready) {
71        initiateCheck();
72    } else {
73        // Pending - do nothing
74        DPRINTF(RubyTest,
75                "initiating action/check - failed: action/check is pending\n");
76    }
77}
78
79void
80Check::initiatePrefetch()
81{
82    DPRINTF(RubyTest, "initiating prefetch\n");
83
84    int index = random() % m_num_readers;
85    MasterPort* port = m_tester_ptr->getReadableCpuPort(index);
86
87    Request::Flags flags;
88    flags.set(Request::PREFETCH);
89
90    Packet::Command cmd;
91
92    // 1 in 8 chance this will be an exclusive prefetch
93    if ((random() & 0x7) != 0) {
94        cmd = MemCmd::ReadReq;
95
96        // if necessary, make the request an instruction fetch
97        if (m_tester_ptr->isInstReadableCpuPort(index)) {
98            flags.set(Request::INST_FETCH);
99        }
100    } else {
101        cmd = MemCmd::WriteReq;
102        flags.set(Request::PF_EXCLUSIVE);
103    }
104
105    // Prefetches are assumed to be 0 sized
106    Request *req = new Request(m_address.getAddress(), 0, flags,
107            m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
108    req->setThreadContext(index, 0);
109
110    PacketPtr pkt = new Packet(req, cmd);
111
112    // push the subblock onto the sender state.  The sequencer will
113    // update the subblock on the return
114    pkt->senderState =
115        new SenderState(m_address, req->getSize(), pkt->senderState);
116
117    if (port->sendTimingReq(pkt)) {
118        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
119    } else {
120        // If the packet did not issue, must delete
121        SenderState* senderState =  safe_cast<SenderState*>(pkt->senderState);
122        pkt->senderState = senderState->saved;
123        delete senderState;
124        delete pkt->req;
125        delete pkt;
126
127        DPRINTF(RubyTest,
128                "prefetch initiation failed because Port was busy.\n");
129    }
130}
131
132void
133Check::initiateFlush()
134{
135
136    DPRINTF(RubyTest, "initiating Flush\n");
137
138    int index = random() % m_num_writers;
139    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
140
141    Request::Flags flags;
142
143    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
144            m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
145
146    Packet::Command cmd;
147
148    cmd = MemCmd::FlushReq;
149
150    PacketPtr pkt = new Packet(req, cmd);
151
152    // push the subblock onto the sender state.  The sequencer will
153    // update the subblock on the return
154    pkt->senderState =
155        new SenderState(m_address, req->getSize(), pkt->senderState);
156
157    if (port->sendTimingReq(pkt)) {
158        DPRINTF(RubyTest, "initiating Flush - successful\n");
159    }
160}
161
162void
163Check::initiateAction()
164{
165    DPRINTF(RubyTest, "initiating Action\n");
166    assert(m_status == TesterStatus_Idle);
167
168    int index = random() % m_num_writers;
169    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
170
171    Request::Flags flags;
172
173    // Create the particular address for the next byte to be written
174    Address writeAddr(m_address.getAddress() + m_store_count);
175
176    // Stores are assumed to be 1 byte-sized
177    Request *req = new Request(writeAddr.getAddress(), 1, flags,
178            m_tester_ptr->masterId(), curTick(),
179                               m_pc.getAddress());
180
181    req->setThreadContext(index, 0);
182    Packet::Command cmd;
183
184    // 1 out of 8 chance, issue an atomic rather than a write
185    // if ((random() & 0x7) == 0) {
186    //     cmd = MemCmd::SwapReq;
187    // } else {
188    cmd = MemCmd::WriteReq;
189    // }
190
191    PacketPtr pkt = new Packet(req, cmd);
192    uint8_t *writeData = new uint8_t;
193    *writeData = m_value + m_store_count;
194    pkt->dataDynamic(writeData);
195
196    DPRINTF(RubyTest, "data 0x%x check 0x%x\n",
197            *(pkt->getPtr<uint8_t>()), *writeData);
198
199    // push the subblock onto the sender state.  The sequencer will
200    // update the subblock on the return
201    pkt->senderState =
202        new SenderState(writeAddr, req->getSize(), pkt->senderState);
203
204    if (port->sendTimingReq(pkt)) {
205        DPRINTF(RubyTest, "initiating action - successful\n");
206        DPRINTF(RubyTest, "status before action update: %s\n",
207                (TesterStatus_to_string(m_status)).c_str());
208        m_status = TesterStatus_Action_Pending;
209    } else {
210        // If the packet did not issue, must delete
211        // Note: No need to delete the data, the packet destructor
212        // will delete it
213        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
214        pkt->senderState = senderState->saved;
215        delete senderState;
216        delete pkt->req;
217        delete pkt;
218
219        DPRINTF(RubyTest, "failed to initiate action - sequencer not ready\n");
220    }
221
222    DPRINTF(RubyTest, "status after action update: %s\n",
223            (TesterStatus_to_string(m_status)).c_str());
224}
225
226void
227Check::initiateCheck()
228{
229    DPRINTF(RubyTest, "Initiating Check\n");
230    assert(m_status == TesterStatus_Ready);
231
232    int index = random() % m_num_readers;
233    MasterPort* port = m_tester_ptr->getReadableCpuPort(index);
234
235    Request::Flags flags;
236
237    // If necessary, make the request an instruction fetch
238    if (m_tester_ptr->isInstReadableCpuPort(index)) {
239        flags.set(Request::INST_FETCH);
240    }
241
242    // Checks are sized depending on the number of bytes written
243    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
244                               m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
245
246    req->setThreadContext(index, 0);
247    PacketPtr pkt = new Packet(req, MemCmd::ReadReq);
248    uint8_t *dataArray = new uint8_t[CHECK_SIZE];
249    pkt->dataDynamicArray(dataArray);
250
251    // push the subblock onto the sender state.  The sequencer will
252    // update the subblock on the return
253    pkt->senderState =
254        new SenderState(m_address, req->getSize(), pkt->senderState);
255
256    if (port->sendTimingReq(pkt)) {
257        DPRINTF(RubyTest, "initiating check - successful\n");
258        DPRINTF(RubyTest, "status before check update: %s\n",
259                TesterStatus_to_string(m_status).c_str());
260        m_status = TesterStatus_Check_Pending;
261    } else {
262        // If the packet did not issue, must delete
263        // Note: No need to delete the data, the packet destructor
264        // will delete it
265        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
266        pkt->senderState = senderState->saved;
267        delete senderState;
268        delete pkt->req;
269        delete pkt;
270
271        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
272    }
273
274    DPRINTF(RubyTest, "status after check update: %s\n",
275            TesterStatus_to_string(m_status).c_str());
276}
277
278void
279Check::performCallback(NodeID proc, SubBlock* data, Time curTime)
280{
281    Address address = data->getAddress();
282
283    // This isn't exactly right since we now have multi-byte checks
284    //  assert(getAddress() == address);
285
286    assert(getAddress().getLineAddress() == address.getLineAddress());
287    assert(data != NULL);
288
289    DPRINTF(RubyTest, "RubyTester Callback\n");
290    debugPrint();
291
292    if (m_status == TesterStatus_Action_Pending) {
293        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
294                (m_value + m_store_count), data->getByte(0));
295        // Perform store one byte at a time
296        data->setByte(0, (m_value + m_store_count));
297        m_store_count++;
298        if (m_store_count == CHECK_SIZE) {
299            m_status = TesterStatus_Ready;
300        } else {
301            m_status = TesterStatus_Idle;
302        }
303        DPRINTF(RubyTest, "Action callback return data now %d\n",
304                data->getByte(0));
305    } else if (m_status == TesterStatus_Check_Pending) {
306        DPRINTF(RubyTest, "Check callback\n");
307        // Perform load/check
308        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
309            if (uint8_t(m_value + byte_number) != data->getByte(byte_number)) {
310                panic("Action/check failure: proc: %d address: %s data: %s "
311                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
312                      "Time: %d\n",
313                      proc, address, data, byte_number,
314                      (int)m_value + byte_number,
315                      (int)data->getByte(byte_number), *this, curTime);
316            }
317        }
318        DPRINTF(RubyTest, "Action/check success\n");
319        debugPrint();
320
321        // successful check complete, increment complete
322        m_tester_ptr->incrementCheckCompletions();
323
324        m_status = TesterStatus_Idle;
325        pickValue();
326
327    } else {
328        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
329              "time: %d\n", *this, proc, data, m_status, curTime);
330    }
331
332    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
333            getAddress().getLineAddress());
334    DPRINTF(RubyTest, "Callback done\n");
335    debugPrint();
336}
337
338void
339Check::changeAddress(const Address& address)
340{
341    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
342    m_status = TesterStatus_Idle;
343    m_address = address;
344    m_store_count = 0;
345}
346
347void
348Check::pickValue()
349{
350    assert(m_status == TesterStatus_Idle);
351    m_status = TesterStatus_Idle;
352    m_value = random() & 0xff; // One byte
353    m_store_count = 0;
354}
355
356void
357Check::pickInitiatingNode()
358{
359    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
360    m_status = TesterStatus_Idle;
361    m_initiatingNode = (random() % m_num_writers);
362    DPRINTF(RubyTest, "picked initiating node %d\n", m_initiatingNode);
363    m_store_count = 0;
364}
365
366void
367Check::print(std::ostream& out) const
368{
369    out << "["
370        << m_address << ", value: "
371        << (int)m_value << ", status: "
372        << m_status << ", initiating node: "
373        << m_initiatingNode << ", store_count: "
374        << m_store_count
375        << "]" << std::flush;
376}
377
378void
379Check::debugPrint()
380{
381    DPRINTF(RubyTest,
382        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
383        m_address.getAddress(), (int)m_value,
384        TesterStatus_to_string(m_status).c_str(),
385        m_initiatingNode, m_store_count);
386}
387