Skip to content

Commit

Permalink
Refactor register access functions
Browse files Browse the repository at this point in the history
- use ThreadId instead of api ze_dvice_thread_t

Related-To: NEO-5784

Signed-off-by: Mateusz Hoppe <[email protected]>
  • Loading branch information
HoppeMateusz authored and Compute-Runtime-Automation committed Jul 13, 2022
1 parent 5dd25d9 commit e8a9099
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
32 changes: 15 additions & 17 deletions level_zero/tools/source/debug/debug_session_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,13 @@ bool DebugSessionImp::writeResumeCommand(const std::vector<EuThread::ThreadId> &
auto reg = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));

for (auto &threadID : threadIds) {
auto apiThread = convertToApi(threadID);
memset(reg.get(), 0, regSize);

if (readRegistersImp(apiThread, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
if (readRegistersImp(threadID, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
success = false;
} else {
reg[dword] |= sipResumeValue;
if (writeRegistersImp(apiThread, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
if (writeRegistersImp(threadID, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
success = false;
}
}
Expand Down Expand Up @@ -636,8 +635,7 @@ void DebugSessionImp::fillResumeAndStoppedThreadsFromNewlyStopped(std::vector<Eu
for (auto &newlyStopped : newlyStoppedThreads) {
if (allThreads[newlyStopped]->isStopped()) {
memset(reg.get(), 0, regSize);
ze_device_thread_t apiThread = convertToApi(newlyStopped);
readRegistersImp(apiThread, ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0, 1, reg.get());
readRegistersImp(newlyStopped, ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0, 1, reg.get());

if (isForceExceptionOrForceExternalHaltOnlyExceptionReason(reg.get())) {
PRINT_DEBUGGER_THREAD_LOG("RESUME accidentally stopped thread = %s\n", allThreads[newlyStopped]->toString().c_str());
Expand Down Expand Up @@ -822,7 +820,7 @@ size_t DebugSessionImp::calculateRegisterOffsetInThreadSlot(const SIP::regset_de
return regdesc->offset + regdesc->bytes * start;
}

ze_result_t DebugSessionImp::readSbaRegisters(ze_device_thread_t thread, uint32_t start, uint32_t count, void *pRegisterValues) {
ze_result_t DebugSessionImp::readSbaRegisters(EuThread::ThreadId threadId, uint32_t start, uint32_t count, void *pRegisterValues) {
auto sbaRegDesc = DebugSessionImp::getSbaRegsetDesc();

if (start >= sbaRegDesc->num) {
Expand All @@ -836,7 +834,7 @@ ze_result_t DebugSessionImp::readSbaRegisters(ze_device_thread_t thread, uint32_
ze_result_t ret = ZE_RESULT_SUCCESS;

NEO::SbaTrackedAddresses sbaBuffer;
ret = readSbaBuffer(convertToThreadId(thread), sbaBuffer);
ret = readSbaBuffer(threadId, sbaBuffer);
if (ret != ZE_RESULT_SUCCESS) {
return ret;
}
Expand All @@ -845,7 +843,7 @@ ze_result_t DebugSessionImp::readSbaRegisters(ze_device_thread_t thread, uint32_
const auto regSize = std::max(getRegisterSize(ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU), hwInfo.capabilityTable.grfSize);
auto r0 = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));

ret = readRegistersImp(thread, ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU, 0, 1, r0.get());
ret = readRegistersImp(threadId, ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU, 0, 1, r0.get());
if (ret != ZE_RESULT_SUCCESS) {
return ret;
}
Expand All @@ -862,14 +860,14 @@ ze_result_t DebugSessionImp::readSbaRegisters(ze_device_thread_t thread, uint32_
constexpr size_t renderSurfaceStateSize = 64;
std::vector<char> renderSurfaceState(renderSurfaceStateSize, 0);

ret = readGpuMemory(allThreads[convertToThreadId(thread)]->getMemoryHandle(), renderSurfaceState.data(), renderSurfaceStateSize, renderSurfaceStateGpuVa);
ret = readGpuMemory(allThreads[threadId]->getMemoryHandle(), renderSurfaceState.data(), renderSurfaceStateSize, renderSurfaceStateGpuVa);

if (ret != ZE_RESULT_SUCCESS) {
return ret;
}

auto scratchSpacePTSize = hwHelper.getRenderSurfaceStatePitch(renderSurfaceState.data());
auto threadOffset = getPerThreadScratchOffset(scratchSpacePTSize, convertToThreadId(thread));
auto threadOffset = getPerThreadScratchOffset(scratchSpacePTSize, threadId);
auto gmmHelper = connectedDevice->getNEODevice()->getGmmHelper();
auto scratchAllocationBase = gmmHelper->decanonize(hwHelper.getRenderSurfaceStateBaseAddress(renderSurfaceState.data()));
if (scratchAllocationBase != 0) {
Expand Down Expand Up @@ -1017,19 +1015,19 @@ ze_result_t DebugSessionImp::readRegisters(ze_device_thread_t thread, uint32_t t
}

if (type == ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU) {
return readSbaRegisters(thread, start, count, pRegisterValues);
return readSbaRegisters(convertToThreadId(thread), start, count, pRegisterValues);
}

return readRegistersImp(thread, type, start, count, pRegisterValues);
return readRegistersImp(convertToThreadId(thread), type, start, count, pRegisterValues);
}

ze_result_t DebugSessionImp::readRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
ze_result_t DebugSessionImp::readRegistersImp(EuThread::ThreadId threadId, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
auto regdesc = typeToRegsetDesc(type);
if (nullptr == regdesc) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}

return registersAccessHelper(allThreads[convertToThreadId(thread)].get(), regdesc, start, count, pRegisterValues, false);
return registersAccessHelper(allThreads[threadId].get(), regdesc, start, count, pRegisterValues, false);
}

ze_result_t DebugSessionImp::writeRegisters(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
Expand All @@ -1042,10 +1040,10 @@ ze_result_t DebugSessionImp::writeRegisters(ze_device_thread_t thread, uint32_t
return ZE_RESULT_ERROR_UNKNOWN;
}

return writeRegistersImp(thread, type, start, count, pRegisterValues);
return writeRegistersImp(convertToThreadId(thread), type, start, count, pRegisterValues);
}

ze_result_t DebugSessionImp::writeRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
ze_result_t DebugSessionImp::writeRegistersImp(EuThread::ThreadId threadId, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
auto regdesc = typeToRegsetDesc(type);
if (nullptr == regdesc) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
Expand All @@ -1055,7 +1053,7 @@ ze_result_t DebugSessionImp::writeRegistersImp(ze_device_thread_t thread, uint32
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}

return registersAccessHelper(allThreads[convertToThreadId(thread)].get(), regdesc, start, count, pRegisterValues, true);
return registersAccessHelper(allThreads[threadId].get(), regdesc, start, count, pRegisterValues, true);
}

bool DebugSessionImp::isValidGpuAddress(uint64_t address) const {
Expand Down
6 changes: 3 additions & 3 deletions level_zero/tools/source/debug/debug_session_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct DebugSessionImp : DebugSession {
constexpr static int64_t interruptTimeout = 2000;

protected:
MOCKABLE_VIRTUAL ze_result_t readRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues);
MOCKABLE_VIRTUAL ze_result_t writeRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues);
MOCKABLE_VIRTUAL ze_result_t readRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues);
MOCKABLE_VIRTUAL ze_result_t writeRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues);
Error resumeThreadsWithinDevice(uint32_t deviceIndex, ze_device_thread_t physicalThread);
MOCKABLE_VIRTUAL bool writeResumeCommand(const std::vector<EuThread::ThreadId> &threadIds);
MOCKABLE_VIRTUAL bool checkThreadIsResumed(const EuThread::ThreadId &threadID);
Expand All @@ -59,7 +59,7 @@ struct DebugSessionImp : DebugSession {
virtual void enqueueApiEvent(zet_debug_event_t &debugEvent) = 0;
virtual bool readSystemRoutineIdent(EuThread *thread, uint64_t vmHandle, SIP::sr_ident &srMagic) = 0;

ze_result_t readSbaRegisters(ze_device_thread_t thread, uint32_t start, uint32_t count, void *pRegisterValues);
ze_result_t readSbaRegisters(EuThread::ThreadId thread, uint32_t start, uint32_t count, void *pRegisterValues);
MOCKABLE_VIRTUAL bool isForceExceptionOrForceExternalHaltOnlyExceptionReason(uint32_t *cr0);
void sendInterrupts();
MOCKABLE_VIRTUAL void markPendingInterruptsOrAddToNewlyStoppedFromRaisedAttention(EuThread::ThreadId threadId, uint64_t memoryHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct MockDebugSession : public L0::DebugSessionImp {
ze_result_t acknowledgeEvent(const zet_debug_event_t *event) override {
return ZE_RESULT_SUCCESS;
}
ze_result_t readRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
ze_result_t readRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
readRegistersCallCount++;
readRegistersReg = type;

Expand All @@ -92,7 +92,7 @@ struct MockDebugSession : public L0::DebugSessionImp {
return DebugSessionImp::readRegistersImp(thread, type, start, count, pRegisterValues);
}

ze_result_t writeRegistersImp(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
ze_result_t writeRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
writeRegistersCallCount++;
writeRegistersReg = type;

Expand Down Expand Up @@ -798,7 +798,7 @@ TEST(DebugSessionTest, givenErrorFromReadSbaBufferWhenReadSbaRegistersCalledThen

ze_device_thread_t thread = {0, 0, 0, 0};
uint64_t sba[9];
auto result = sessionMock->readSbaRegisters(thread, 0, 9, sba);
auto result = sessionMock->readSbaRegisters(EuThread::ThreadId(0, thread), 0, 9, sba);
EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, result);
}

Expand All @@ -815,7 +815,7 @@ TEST(DebugSessionTest, givenErrorFromReadRegistersWhenReadSbaRegistersCalledThen

ze_device_thread_t thread = {0, 0, 0, 0};
uint64_t sba[9];
auto result = sessionMock->readSbaRegisters(thread, 0, 9, sba);
auto result = sessionMock->readSbaRegisters(EuThread::ThreadId(0, thread), 0, 9, sba);
EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, result);
}

Expand All @@ -838,7 +838,7 @@ HWTEST2_F(DebugSessionTest, givenErrorFromReadMemoryWhenReadSbaRegistersCalledTh
ze_device_thread_t thread = {0, 0, 0, 0};
uint64_t sba[9];

auto result = sessionMock->readSbaRegisters(thread, 0, 9, sba);
auto result = sessionMock->readSbaRegisters(EuThread::ThreadId(0, thread), 0, 9, sba);

auto &hwHelper = HwHelper::get(neoDevice->getHardwareInfo().platform.eRenderCoreFamily);
EXPECT_TRUE(hwHelper.isScratchSpaceSurfaceStateAccessible());
Expand Down Expand Up @@ -2029,7 +2029,7 @@ TEST_F(DebugSessionRegistersAccessTest, WhenReadingSbaRegistersThenCorrectAddres
r0Thread0[5] = 0;
}

EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(thread, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(threadId, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(0ULL, sba[ZET_DEBUG_SBA_SCRATCH_SPACE_INTEL_GPU]);

scratchAllocationBase = sbaExpected[ZET_DEBUG_SBA_SCRATCH_SPACE_INTEL_GPU];
Expand All @@ -2054,7 +2054,7 @@ TEST_F(DebugSessionRegistersAccessTest, WhenReadingSbaRegistersThenCorrectAddres

sbaExpected[ZET_DEBUG_SBA_BINDING_TABLE_INTEL_GPU] = ((r0Thread0[4] >> 5) << 5) + sbaExpected[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU];

EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(thread, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(threadId, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));

EXPECT_EQ(sbaExpected[ZET_DEBUG_SBA_GENERAL_STATE_INTEL_GPU], sba[ZET_DEBUG_SBA_GENERAL_STATE_INTEL_GPU]);
EXPECT_EQ(sbaExpected[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU], sba[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU]);
Expand Down Expand Up @@ -2083,7 +2083,7 @@ TEST_F(DebugSessionRegistersAccessTest, WhenReadingSbaRegistersThenCorrectAddres

sbaExpected[ZET_DEBUG_SBA_BINDING_TABLE_INTEL_GPU] = ((r0Thread1[4] >> 5) << 5) + sbaExpected[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU];

EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(thread1, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(threadId1, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));

EXPECT_EQ(sbaExpected[ZET_DEBUG_SBA_GENERAL_STATE_INTEL_GPU], sba[ZET_DEBUG_SBA_GENERAL_STATE_INTEL_GPU]);
EXPECT_EQ(sbaExpected[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU], sba[ZET_DEBUG_SBA_SURFACE_STATE_INTEL_GPU]);
Expand All @@ -2108,7 +2108,7 @@ TEST_F(DebugSessionRegistersAccessTest, WhenReadingSbaRegistersThenCorrectAddres
}
sbaExpected[ZET_DEBUG_SBA_SCRATCH_SPACE_INTEL_GPU] = 0;

EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(thread1, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(ZE_RESULT_SUCCESS, session->readSbaRegisters(threadId1, 0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, sba));
EXPECT_EQ(sbaExpected[ZET_DEBUG_SBA_SCRATCH_SPACE_INTEL_GPU], sba[ZET_DEBUG_SBA_SCRATCH_SPACE_INTEL_GPU]);
}

Expand Down

0 comments on commit e8a9099

Please sign in to comment.