timing.cc

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· CC 代码 · 共 880 行 · 第 1/2 页

CC
880
字号
/* * Copyright (c) 2002, 2003, 2004, 2005 * The Regents of The University of Michigan * All Rights Reserved * * This code is part of the M5 simulator. * * Permission is granted to use, copy, create derivative works and * redistribute this software and such derivative works for any * purpose, so long as the copyright notice above, this grant of * permission, and the disclaimer below appear in all copies made; and * so long as the name of The University of Michigan is not used in * any advertising or publicity pertaining to the use or distribution * of this software without specific, written prior authorization. * * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT, * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH * DAMAGES. * * Authors: Steven K. Reinhardt */#include "arch/locked_mem.hh"#include "arch/mmaped_ipr.hh"#include "arch/utility.hh"#include "base/bigint.hh"#include "cpu/exetrace.hh"#include "cpu/simple/timing.hh"#include "mem/packet.hh"#include "mem/packet_access.hh"#include "params/TimingSimpleCPU.hh"#include "sim/system.hh"using namespace std;using namespace TheISA;Port *TimingSimpleCPU::getPort(const std::string &if_name, int idx){    if (if_name == "dcache_port")        return &dcachePort;    else if (if_name == "icache_port")        return &icachePort;    else        panic("No Such Port\n");}voidTimingSimpleCPU::init(){    BaseCPU::init();    cpuId = tc->readCpuId();#if FULL_SYSTEM    for (int i = 0; i < threadContexts.size(); ++i) {        ThreadContext *tc = threadContexts[i];        // initialize CPU, including PC        TheISA::initCPU(tc, cpuId);    }#endif}TickTimingSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt){    panic("TimingSimpleCPU doesn't expect recvAtomic callback!");    return curTick;}voidTimingSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt){    //No internal storage to update, jusst return    return;}voidTimingSimpleCPU::CpuPort::recvStatusChange(Status status){    if (status == RangeChange) {        if (!snoopRangeSent) {            snoopRangeSent = true;            sendStatusChange(Port::RangeChange);        }        return;    }    panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");}voidTimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt, Tick t){    pkt = _pkt;    Event::schedule(t);}TimingSimpleCPU::TimingSimpleCPU(Params *p)    : BaseSimpleCPU(p), icachePort(this, p->clock), dcachePort(this, p->clock){    _status = Idle;    icachePort.snoopRangeSent = false;    dcachePort.snoopRangeSent = false;    ifetch_pkt = dcache_pkt = NULL;    drainEvent = NULL;    fetchEvent = NULL;    previousTick = 0;    changeState(SimObject::Running);}TimingSimpleCPU::~TimingSimpleCPU(){}voidTimingSimpleCPU::serialize(ostream &os){    SimObject::State so_state = SimObject::getState();    SERIALIZE_ENUM(so_state);    BaseSimpleCPU::serialize(os);}voidTimingSimpleCPU::unserialize(Checkpoint *cp, const string &section){    SimObject::State so_state;    UNSERIALIZE_ENUM(so_state);    BaseSimpleCPU::unserialize(cp, section);}unsigned intTimingSimpleCPU::drain(Event *drain_event){    // TimingSimpleCPU is ready to drain if it's not waiting for    // an access to complete.    if (status() == Idle || status() == Running || status() == SwitchedOut) {        changeState(SimObject::Drained);        return 0;    } else {        changeState(SimObject::Draining);        drainEvent = drain_event;        return 1;    }}voidTimingSimpleCPU::resume(){    DPRINTF(SimpleCPU, "Resume\n");    if (_status != SwitchedOut && _status != Idle) {        assert(system->getMemoryMode() == Enums::timing);        // Delete the old event if it existed.        if (fetchEvent) {            if (fetchEvent->scheduled())                fetchEvent->deschedule();            delete fetchEvent;        }        fetchEvent = new FetchEvent(this, nextCycle());    }    changeState(SimObject::Running);}voidTimingSimpleCPU::switchOut(){    assert(status() == Running || status() == Idle);    _status = SwitchedOut;    numCycles += tickToCycles(curTick - previousTick);    // If we've been scheduled to resume but are then told to switch out,    // we'll need to cancel it.    if (fetchEvent && fetchEvent->scheduled())        fetchEvent->deschedule();}voidTimingSimpleCPU::takeOverFrom(BaseCPU *oldCPU){    BaseCPU::takeOverFrom(oldCPU, &icachePort, &dcachePort);    // if any of this CPU's ThreadContexts are active, mark the CPU as    // running and schedule its tick event.    for (int i = 0; i < threadContexts.size(); ++i) {        ThreadContext *tc = threadContexts[i];        if (tc->status() == ThreadContext::Active && _status != Running) {            _status = Running;            break;        }    }    if (_status != Running) {        _status = Idle;    }    assert(threadContexts.size() == 1);    cpuId = tc->readCpuId();     previousTick = curTick;}voidTimingSimpleCPU::activateContext(int thread_num, int delay){    DPRINTF(SimpleCPU, "ActivateContext %d (%d cycles)\n", thread_num, delay);    assert(thread_num == 0);    assert(thread);    assert(_status == Idle);    notIdleFraction++;    _status = Running;    // kick things off by initiating the fetch of the next instruction    fetchEvent = new FetchEvent(this, nextCycle(curTick + ticks(delay)));}voidTimingSimpleCPU::suspendContext(int thread_num){    DPRINTF(SimpleCPU, "SuspendContext %d\n", thread_num);    assert(thread_num == 0);    assert(thread);    assert(_status == Running);    // just change status to Idle... if status != Running,    // completeInst() will not initiate fetch of next instruction.    notIdleFraction--;    _status = Idle;}template <class T>FaultTimingSimpleCPU::read(Addr addr, T &data, unsigned flags){    Request *req =        new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),                    cpuId, /* thread ID */ 0);    if (traceData) {        traceData->setAddr(req->getVaddr());    }   // translate to physical address    Fault fault = thread->translateDataReadReq(req);    // Now do the access.    if (fault == NoFault) {        PacketPtr pkt =            new Packet(req,                       (req->isLocked() ?                        MemCmd::LoadLockedReq : MemCmd::ReadReq),                       Packet::Broadcast);        pkt->dataDynamic<T>(new T);        if (req->isMmapedIpr()) {            Tick delay;            delay = TheISA::handleIprRead(thread->getTC(), pkt);            new IprEvent(pkt, this, nextCycle(curTick + delay));            _status = DcacheWaitResponse;            dcache_pkt = NULL;        } else if (!dcachePort.sendTiming(pkt)) {            _status = DcacheRetry;            dcache_pkt = pkt;        } else {            _status = DcacheWaitResponse;            // memory system takes ownership of packet            dcache_pkt = NULL;        }        // This will need a new way to tell if it has a dcache attached.        if (req->isUncacheable())            recordEvent("Uncached Read");    } else {        delete req;    }    return fault;}FaultTimingSimpleCPU::translateDataReadAddr(Addr vaddr, Addr &paddr,        int size, unsigned flags){    Request *req =        new Request(0, vaddr, size, flags, thread->readPC(), cpuId, 0);    if (traceData) {        traceData->setAddr(vaddr);    }    Fault fault = thread->translateDataWriteReq(req);    if (fault == NoFault)        paddr = req->getPaddr();    delete req;    return fault;}#ifndef DOXYGEN_SHOULD_SKIP_THIStemplateFaultTimingSimpleCPU::read(Addr addr, Twin64_t &data, unsigned flags);templateFaultTimingSimpleCPU::read(Addr addr, Twin32_t &data, unsigned flags);templateFaultTimingSimpleCPU::read(Addr addr, uint64_t &data, unsigned flags);templateFaultTimingSimpleCPU::read(Addr addr, uint32_t &data, unsigned flags);templateFaultTimingSimpleCPU::read(Addr addr, uint16_t &data, unsigned flags);templateFaultTimingSimpleCPU::read(Addr addr, uint8_t &data, unsigned flags);#endif //DOXYGEN_SHOULD_SKIP_THIStemplate<>FaultTimingSimpleCPU::read(Addr addr, double &data, unsigned flags){    return read(addr, *(uint64_t*)&data, flags);}template<>FaultTimingSimpleCPU::read(Addr addr, float &data, unsigned flags){    return read(addr, *(uint32_t*)&data, flags);}template<>FaultTimingSimpleCPU::read(Addr addr, int32_t &data, unsigned flags){    return read(addr, (uint32_t&)data, flags);}template <class T>FaultTimingSimpleCPU::write(T data, Addr addr, unsigned flags, uint64_t *res){    Request *req =        new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),                    cpuId, /* thread ID */ 0);    if (traceData) {        traceData->setAddr(req->getVaddr());    }    // translate to physical address    Fault fault = thread->translateDataWriteReq(req);    // Now do the access.    if (fault == NoFault) {        MemCmd cmd = MemCmd::WriteReq; // default        bool do_access = true;  // flag to suppress cache access        if (req->isLocked()) {            cmd = MemCmd::StoreCondReq;            do_access = TheISA::handleLockedWrite(thread, req);        } else if (req->isSwap()) {            cmd = MemCmd::SwapReq;            if (req->isCondSwap()) {                assert(res);                req->setExtraData(*res);            }        }        // Note: need to allocate dcache_pkt even if do_access is        // false, as it's used unconditionally to call completeAcc().        assert(dcache_pkt == NULL);        dcache_pkt = new Packet(req, cmd, Packet::Broadcast);        dcache_pkt->allocate();        dcache_pkt->set(data);        if (do_access) {            if (req->isMmapedIpr()) {                Tick delay;                dcache_pkt->set(htog(data));                delay = TheISA::handleIprWrite(thread->getTC(), dcache_pkt);                new IprEvent(dcache_pkt, this, nextCycle(curTick + delay));                _status = DcacheWaitResponse;                dcache_pkt = NULL;            } else if (!dcachePort.sendTiming(dcache_pkt)) {                _status = DcacheRetry;            } else {                _status = DcacheWaitResponse;                // memory system takes ownership of packet                dcache_pkt = NULL;            }        }        // This will need a new way to tell if it's hooked up to a cache or not.        if (req->isUncacheable())            recordEvent("Uncached Write");    } else {        delete req;    }    // If the write needs to have a fault on the access, consider calling    // changeStatus() and changing it to "bad addr write" or something.    return fault;}Fault

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?