Check.cc revision 8232
112854Sgabeblack@google.com/*
212854Sgabeblack@google.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
312854Sgabeblack@google.com * Copyright (c) 2009 Advanced Micro Devices, Inc.
412854Sgabeblack@google.com * All rights reserved.
512854Sgabeblack@google.com *
612854Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
712854Sgabeblack@google.com * modification, are permitted provided that the following conditions are
812854Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
912854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1012854Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1112854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1212854Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1312854Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1412854Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1512854Sgabeblack@google.com * this software without specific prior written permission.
1612854Sgabeblack@google.com *
1712854Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812854Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912854Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012854Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112854Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212854Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312854Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412854Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512854Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612854Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712854Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812854Sgabeblack@google.com */
2912854Sgabeblack@google.com
3012854Sgabeblack@google.com#include "cpu/testers/rubytest/Check.hh"
3112854Sgabeblack@google.com#include "debug/RubyTest.hh"
3212854Sgabeblack@google.com#include "mem/ruby/common/SubBlock.hh"
3312854Sgabeblack@google.com#include "mem/ruby/system/Sequencer.hh"
3412854Sgabeblack@google.com#include "mem/ruby/system/System.hh"
3512854Sgabeblack@google.com
3612854Sgabeblack@google.comtypedef RubyTester::SenderState SenderState;
3712854Sgabeblack@google.com
3812854Sgabeblack@google.comCheck::Check(const Address& address, const Address& pc,
3912854Sgabeblack@google.com             int _num_cpu_sequencers, RubyTester* _tester)
4012854Sgabeblack@google.com    : m_num_cpu_sequencers(_num_cpu_sequencers), m_tester_ptr(_tester)
4112854Sgabeblack@google.com{
4212854Sgabeblack@google.com    m_status = TesterStatus_Idle;
4312854Sgabeblack@google.com
4412854Sgabeblack@google.com    pickValue();
4512854Sgabeblack@google.com    pickInitiatingNode();
4612854Sgabeblack@google.com    changeAddress(address);
4712854Sgabeblack@google.com    m_pc = pc;
4812854Sgabeblack@google.com    m_access_mode = RubyAccessMode(random() % RubyAccessMode_NUM);
4912854Sgabeblack@google.com    m_store_count = 0;
5012854Sgabeblack@google.com}
5112854Sgabeblack@google.com
5212854Sgabeblack@google.comvoid
5312854Sgabeblack@google.comCheck::initiate()
5412854Sgabeblack@google.com{
5512854Sgabeblack@google.com    DPRINTF(RubyTest, "initiating\n");
5612854Sgabeblack@google.com    debugPrint();
5712854Sgabeblack@google.com
5813325Sgabeblack@google.com    // currently no protocols support prefetches
5912854Sgabeblack@google.com    if (false && (random() & 0xf) == 0) {
6012854Sgabeblack@google.com        initiatePrefetch(); // Prefetch from random processor
6112854Sgabeblack@google.com    }
6212854Sgabeblack@google.com
6312854Sgabeblack@google.com    if (m_tester_ptr->getCheckFlush() && (random() & 0xff) == 0) {
6412854Sgabeblack@google.com        initiateFlush(); // issue a Flush request from random processor
6512854Sgabeblack@google.com    }
6612854Sgabeblack@google.com
6712854Sgabeblack@google.com    if (m_status == TesterStatus_Idle) {
6812854Sgabeblack@google.com        initiateAction();
6912854Sgabeblack@google.com    } else if (m_status == TesterStatus_Ready) {
7012854Sgabeblack@google.com        initiateCheck();
7112854Sgabeblack@google.com    } else {
7212854Sgabeblack@google.com        // Pending - do nothing
7312854Sgabeblack@google.com        DPRINTF(RubyTest,
7412854Sgabeblack@google.com                "initiating action/check - failed: action/check is pending\n");
7512854Sgabeblack@google.com    }
7612854Sgabeblack@google.com}
7712854Sgabeblack@google.com
7813325Sgabeblack@google.comvoid
7912854Sgabeblack@google.comCheck::initiatePrefetch()
8012854Sgabeblack@google.com{
8112854Sgabeblack@google.com    DPRINTF(RubyTest, "initiating prefetch\n");
8212854Sgabeblack@google.com
8312854Sgabeblack@google.com    int index = random() % m_num_cpu_sequencers;
8412854Sgabeblack@google.com    RubyTester::CpuPort* port =
8512854Sgabeblack@google.com        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
8612854Sgabeblack@google.com
8712854Sgabeblack@google.com    Request::Flags flags;
8812854Sgabeblack@google.com    flags.set(Request::PREFETCH);
8912854Sgabeblack@google.com
9012854Sgabeblack@google.com    Packet::Command cmd;
9112854Sgabeblack@google.com
9212854Sgabeblack@google.com    // 1 in 8 chance this will be an exclusive prefetch
9312854Sgabeblack@google.com    if ((random() & 0x7) != 0) {
9412854Sgabeblack@google.com        cmd = MemCmd::ReadReq;
9512854Sgabeblack@google.com
9612854Sgabeblack@google.com        // 50% chance that the request will be an instruction fetch
9712854Sgabeblack@google.com        if ((random() & 0x1) == 0) {
9812854Sgabeblack@google.com            flags.set(Request::INST_FETCH);
9912854Sgabeblack@google.com        }
10012854Sgabeblack@google.com    } else {
10112854Sgabeblack@google.com        cmd = MemCmd::WriteReq;
10212854Sgabeblack@google.com        flags.set(Request::PF_EXCLUSIVE);
10312854Sgabeblack@google.com    }
10412854Sgabeblack@google.com
10513325Sgabeblack@google.com    // Prefetches are assumed to be 0 sized
10612854Sgabeblack@google.com    Request *req = new Request(m_address.getAddress(), 0, flags, curTick(),
10712854Sgabeblack@google.com                               m_pc.getAddress());
10812854Sgabeblack@google.com    req->setThreadContext(index, 0);
10912854Sgabeblack@google.com
11012854Sgabeblack@google.com    PacketPtr pkt = new Packet(req, cmd, port->idx);
11112854Sgabeblack@google.com
11212854Sgabeblack@google.com    // push the subblock onto the sender state.  The sequencer will
11312854Sgabeblack@google.com    // update the subblock on the return
11412854Sgabeblack@google.com    pkt->senderState =
11512854Sgabeblack@google.com        new SenderState(m_address, req->getSize(), pkt->senderState);
11612854Sgabeblack@google.com
11712854Sgabeblack@google.com    if (port->sendTiming(pkt)) {
11812854Sgabeblack@google.com        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
11912854Sgabeblack@google.com    } else {
12012854Sgabeblack@google.com        // If the packet did not issue, must delete
12112854Sgabeblack@google.com        SenderState* senderState =  safe_cast<SenderState*>(pkt->senderState);
12212854Sgabeblack@google.com        pkt->senderState = senderState->saved;
12312854Sgabeblack@google.com        delete senderState;
12412854Sgabeblack@google.com        delete pkt->req;
12512854Sgabeblack@google.com        delete pkt;
12612854Sgabeblack@google.com
12712854Sgabeblack@google.com        DPRINTF(RubyTest,
12812854Sgabeblack@google.com                "prefetch initiation failed because Port was busy.\n");
12912854Sgabeblack@google.com    }
13012854Sgabeblack@google.com}
13112854Sgabeblack@google.com
13212854Sgabeblack@google.comvoid
13312854Sgabeblack@google.comCheck::initiateFlush()
13412854Sgabeblack@google.com{
13512854Sgabeblack@google.com
13612854Sgabeblack@google.com    DPRINTF(RubyTest, "initiating Flush\n");
13712854Sgabeblack@google.com
13812854Sgabeblack@google.com    int index = random() % m_num_cpu_sequencers;
13912854Sgabeblack@google.com    RubyTester::CpuPort* port =
14012854Sgabeblack@google.com        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
14112854Sgabeblack@google.com
14212854Sgabeblack@google.com    Request::Flags flags;
14312854Sgabeblack@google.com
14412854Sgabeblack@google.com    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags, curTick(),
14512854Sgabeblack@google.com                               m_pc.getAddress());
14612854Sgabeblack@google.com
14712854Sgabeblack@google.com    Packet::Command cmd;
14812854Sgabeblack@google.com
14912854Sgabeblack@google.com    cmd = MemCmd::FlushReq;
15012854Sgabeblack@google.com
15112854Sgabeblack@google.com    PacketPtr pkt = new Packet(req, cmd, port->idx);
15212854Sgabeblack@google.com
15312854Sgabeblack@google.com    // push the subblock onto the sender state.  The sequencer will
15412854Sgabeblack@google.com    // update the subblock on the return
15512854Sgabeblack@google.com    pkt->senderState =
15612854Sgabeblack@google.com        new SenderState(m_address, req->getSize(), pkt->senderState);
15712854Sgabeblack@google.com
15812854Sgabeblack@google.com    if (port->sendTiming(pkt)) {
15912854Sgabeblack@google.com        DPRINTF(RubyTest, "initiating Flush - successful\n");
16012854Sgabeblack@google.com    }
16112854Sgabeblack@google.com}
16212854Sgabeblack@google.com
16312854Sgabeblack@google.comvoid
16412854Sgabeblack@google.comCheck::initiateAction()
16513325Sgabeblack@google.com{
16613325Sgabeblack@google.com    DPRINTF(RubyTest, "initiating Action\n");
16712854Sgabeblack@google.com    assert(m_status == TesterStatus_Idle);
16812854Sgabeblack@google.com
16912854Sgabeblack@google.com    int index = random() % m_num_cpu_sequencers;
17013325Sgabeblack@google.com    RubyTester::CpuPort* port =
17112854Sgabeblack@google.com        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
17212854Sgabeblack@google.com
17312854Sgabeblack@google.com    Request::Flags flags;
17412854Sgabeblack@google.com
17512854Sgabeblack@google.com    // Create the particular address for the next byte to be written
17612854Sgabeblack@google.com    Address writeAddr(m_address.getAddress() + m_store_count);
17712854Sgabeblack@google.com
17812854Sgabeblack@google.com    // Stores are assumed to be 1 byte-sized
17912854Sgabeblack@google.com    Request *req = new Request(writeAddr.getAddress(), 1, flags, curTick(),
18012854Sgabeblack@google.com                               m_pc.getAddress());
18112854Sgabeblack@google.com
18212854Sgabeblack@google.com    req->setThreadContext(index, 0);
18312854Sgabeblack@google.com    Packet::Command cmd;
18412854Sgabeblack@google.com
18512854Sgabeblack@google.com    // 1 out of 8 chance, issue an atomic rather than a write
18612854Sgabeblack@google.com    // if ((random() & 0x7) == 0) {
18712854Sgabeblack@google.com    //     cmd = MemCmd::SwapReq;
18812854Sgabeblack@google.com    // } else {
18912854Sgabeblack@google.com    cmd = MemCmd::WriteReq;
19012854Sgabeblack@google.com    // }
19112854Sgabeblack@google.com
19212854Sgabeblack@google.com    PacketPtr pkt = new Packet(req, cmd, port->idx);
19312854Sgabeblack@google.com    uint8_t* writeData = new uint8_t;
19412854Sgabeblack@google.com    *writeData = m_value + m_store_count;
19512854Sgabeblack@google.com    pkt->dataDynamic(writeData);
19612854Sgabeblack@google.com
19712854Sgabeblack@google.com    DPRINTF(RubyTest, "data 0x%x check 0x%x\n",
19812854Sgabeblack@google.com            *(pkt->getPtr<uint8_t>()), *writeData);
19912854Sgabeblack@google.com
20012854Sgabeblack@google.com    // push the subblock onto the sender state.  The sequencer will
20112854Sgabeblack@google.com    // update the subblock on the return
20212854Sgabeblack@google.com    pkt->senderState =
20312854Sgabeblack@google.com        new SenderState(writeAddr, req->getSize(), pkt->senderState);
20412854Sgabeblack@google.com
20512854Sgabeblack@google.com    if (port->sendTiming(pkt)) {
20612854Sgabeblack@google.com        DPRINTF(RubyTest, "initiating action - successful\n");
20713325Sgabeblack@google.com        DPRINTF(RubyTest, "status before action update: %s\n",
20812854Sgabeblack@google.com                (TesterStatus_to_string(m_status)).c_str());
20912854Sgabeblack@google.com        m_status = TesterStatus_Action_Pending;
21012854Sgabeblack@google.com    } else {
21112854Sgabeblack@google.com        // If the packet did not issue, must delete
21212854Sgabeblack@google.com        // Note: No need to delete the data, the packet destructor
21312854Sgabeblack@google.com        // will delete it
21412854Sgabeblack@google.com        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
21512854Sgabeblack@google.com        pkt->senderState = senderState->saved;
21612854Sgabeblack@google.com        delete senderState;
21712854Sgabeblack@google.com        delete pkt->req;
21812854Sgabeblack@google.com        delete pkt;
21912854Sgabeblack@google.com
22012854Sgabeblack@google.com        DPRINTF(RubyTest, "failed to initiate action - sequencer not ready\n");
22112854Sgabeblack@google.com    }
22212854Sgabeblack@google.com
22312854Sgabeblack@google.com    DPRINTF(RubyTest, "status after action update: %s\n",
22412854Sgabeblack@google.com            (TesterStatus_to_string(m_status)).c_str());
22512854Sgabeblack@google.com}
22612854Sgabeblack@google.com
22712854Sgabeblack@google.comvoid
22812854Sgabeblack@google.comCheck::initiateCheck()
22912854Sgabeblack@google.com{
23012854Sgabeblack@google.com    DPRINTF(RubyTest, "Initiating Check\n");
23112854Sgabeblack@google.com    assert(m_status == TesterStatus_Ready);
23212854Sgabeblack@google.com
233    int index = random() % m_num_cpu_sequencers;
234    RubyTester::CpuPort* port =
235        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
236
237    Request::Flags flags;
238
239    // 50% chance that the request will be an instruction fetch
240    if ((random() & 0x1) == 0) {
241        flags.set(Request::INST_FETCH);
242    }
243
244    // Checks are sized depending on the number of bytes written
245    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
246                               curTick(), m_pc.getAddress());
247
248    req->setThreadContext(index, 0);
249    PacketPtr pkt = new Packet(req, MemCmd::ReadReq, port->idx);
250    uint8_t* dataArray = new uint8_t[CHECK_SIZE];
251    pkt->dataDynamicArray(dataArray);
252
253    // push the subblock onto the sender state.  The sequencer will
254    // update the subblock on the return
255    pkt->senderState =
256        new SenderState(m_address, req->getSize(), pkt->senderState);
257
258    if (port->sendTiming(pkt)) {
259        DPRINTF(RubyTest, "initiating check - successful\n");
260        DPRINTF(RubyTest, "status before check update: %s\n",
261                TesterStatus_to_string(m_status).c_str());
262        m_status = TesterStatus_Check_Pending;
263    } else {
264        // If the packet did not issue, must delete
265        // Note: No need to delete the data, the packet destructor
266        // will delete it
267        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
268        pkt->senderState = senderState->saved;
269        delete senderState;
270        delete pkt->req;
271        delete pkt;
272
273        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
274    }
275
276    DPRINTF(RubyTest, "status after check update: %s\n",
277            TesterStatus_to_string(m_status).c_str());
278}
279
280void
281Check::performCallback(NodeID proc, SubBlock* data)
282{
283    Address address = data->getAddress();
284
285    // This isn't exactly right since we now have multi-byte checks
286    //  assert(getAddress() == address);
287
288    assert(getAddress().getLineAddress() == address.getLineAddress());
289    assert(data != NULL);
290
291    DPRINTF(RubyTest, "RubyTester Callback\n");
292    debugPrint();
293
294    if (m_status == TesterStatus_Action_Pending) {
295        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
296                (m_value + m_store_count), data->getByte(0));
297        // Perform store one byte at a time
298        data->setByte(0, (m_value + m_store_count));
299        m_store_count++;
300        if (m_store_count == CHECK_SIZE) {
301            m_status = TesterStatus_Ready;
302        } else {
303            m_status = TesterStatus_Idle;
304        }
305        DPRINTF(RubyTest, "Action callback return data now %d\n",
306                data->getByte(0));
307    } else if (m_status == TesterStatus_Check_Pending) {
308        DPRINTF(RubyTest, "Check callback\n");
309        // Perform load/check
310        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
311            if (uint8(m_value + byte_number) != data->getByte(byte_number)) {
312                panic("Action/check failure: proc: %d address: %s data: %s "
313                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
314                      "Time: %d\n",
315                      proc, address, data, byte_number,
316                      (int)m_value + byte_number,
317                      (int)data->getByte(byte_number), *this,
318                      g_eventQueue_ptr->getTime());
319            }
320        }
321        DPRINTF(RubyTest, "Action/check success\n");
322        debugPrint();
323
324        // successful check complete, increment complete
325        m_tester_ptr->incrementCheckCompletions();
326
327        m_status = TesterStatus_Idle;
328        pickValue();
329
330    } else {
331        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
332              "time: %d\n",
333              *this, proc, data, m_status, g_eventQueue_ptr->getTime());
334    }
335
336    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
337            getAddress().getLineAddress());
338    DPRINTF(RubyTest, "Callback done\n");
339    debugPrint();
340}
341
342void
343Check::changeAddress(const Address& address)
344{
345    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
346    m_status = TesterStatus_Idle;
347    m_address = address;
348    m_store_count = 0;
349}
350
351void
352Check::pickValue()
353{
354    assert(m_status == TesterStatus_Idle);
355    m_status = TesterStatus_Idle;
356    m_value = random() & 0xff; // One byte
357    m_store_count = 0;
358}
359
360void
361Check::pickInitiatingNode()
362{
363    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
364    m_status = TesterStatus_Idle;
365    m_initiatingNode = (random() % m_num_cpu_sequencers);
366    DPRINTF(RubyTest, "picked initiating node %d\n", m_initiatingNode);
367    m_store_count = 0;
368}
369
370void
371Check::print(std::ostream& out) const
372{
373    out << "["
374        << m_address << ", value: "
375        << (int)m_value << ", status: "
376        << m_status << ", initiating node: "
377        << m_initiatingNode << ", store_count: "
378        << m_store_count
379        << "]" << std::flush;
380}
381
382void
383Check::debugPrint()
384{
385    DPRINTF(RubyTest,
386        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
387        m_address.getAddress(), (int)m_value,
388        TesterStatus_to_string(m_status).c_str(),
389        m_initiatingNode, m_store_count);
390}
391