L0Debug - close debug fd when DebugSession is destroyed

- after last tile DebugSession is detached, root DebugSession
must close fd

Related-To: NEO-5784

Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
This commit is contained in:
Mateusz Hoppe
2022-08-24 12:03:47 +00:00
committed by Compute-Runtime-Automation
parent ca0686b404
commit aa59ee94b2
5 changed files with 73 additions and 12 deletions

View File

@@ -47,6 +47,7 @@ DebugSessionLinux::~DebugSessionLinux() {
delete session.first;
}
tileSessions.resize(0);
closeFd();
}
DebugSession *DebugSession::create(const zet_debug_config_t &config, Device *device, ze_result_t &result) {
@@ -341,6 +342,21 @@ void DebugSessionLinux::closeAsyncThread() {
internalEventThread.close();
}
bool DebugSessionLinux::closeFd() {
if (fd == 0) {
return false;
}
auto res = NEO::SysCalls::close(fd);
if (res != 0) {
PRINT_DEBUGGER_ERROR_LOG("Debug connection close() on fd: %d failed: retCode: %d\n", fd, res);
return false;
}
fd = 0;
return true;
}
std::unique_ptr<uint64_t[]> DebugSessionLinux::getInternalEvent() {
std::unique_ptr<uint64_t[]> eventMemory;
@@ -427,18 +443,8 @@ void DebugSessionLinux::readInternalEventsAsync() {
bool DebugSessionLinux::closeConnection() {
closeAsyncThread();
internalEventThread.close();
if (fd == 0) {
return false;
}
auto res = NEO::SysCalls::close(fd);
if (res != 0) {
PRINT_DEBUGGER_ERROR_LOG("Debug connection close() on fd: %d failed: retCode: %d\n", fd, res);
return false;
}
return true;
closeInternalEventsThread();
return closeFd();
}
void DebugSessionLinux::handleEvent(prelim_drm_i915_debug_event *event) {

View File

@@ -211,6 +211,8 @@ struct DebugSessionLinux : DebugSessionImp {
internalEventThread.close();
}
bool closeFd();
virtual std::vector<uint64_t> getAllMemoryHandles() {
std::vector<uint64_t> allVms;
std::unique_lock<std::mutex> memLock(asyncThreadMutex);

View File

@@ -256,6 +256,7 @@ struct MockDebugSessionLinux : public L0::DebugSessionLinux {
using L0::DebugSessionLinux::euControlInterruptSeqno;
using L0::DebugSessionLinux::eventsToAck;
using L0::DebugSessionLinux::extractVaFromUuidString;
using L0::DebugSessionLinux::fd;
using L0::DebugSessionLinux::getRegisterSetProperties;
using L0::DebugSessionLinux::getSbaBufferGpuVa;
using L0::DebugSessionLinux::getStateSaveAreaHeader;

View File

@@ -954,6 +954,8 @@ TEST_F(DebugApiLinuxTest, GivenDebugSessionWhenClosingConnectionThenSysCallClose
EXPECT_NE(nullptr, session);
NEO::SysCalls::closeFuncCalled = 0;
NEO::SysCalls::closeFuncArgPassed = 0;
auto ret = session->closeConnection();
EXPECT_TRUE(ret);
@@ -965,6 +967,24 @@ TEST_F(DebugApiLinuxTest, GivenDebugSessionWhenClosingConnectionThenSysCallClose
NEO::SysCalls::closeFuncArgPassed = 0;
}
TEST_F(DebugApiLinuxTest, GivenDebugSessionWhenDestroyedThenSysCallCloseOnFdIsCalled) {
zet_debug_config_t config = {};
config.pid = 0x1234;
auto session = std::make_unique<MockDebugSessionLinux>(config, device, 10);
EXPECT_NE(nullptr, session);
NEO::SysCalls::closeFuncCalled = 0;
NEO::SysCalls::closeFuncArgPassed = 0;
session.reset(nullptr);
EXPECT_EQ(1u, NEO::SysCalls::closeFuncCalled);
EXPECT_EQ(10, NEO::SysCalls::closeFuncArgPassed);
NEO::SysCalls::closeFuncCalled = 0;
NEO::SysCalls::closeFuncArgPassed = 0;
}
TEST_F(DebugApiLinuxTest, GivenDebugSessionWithFdEqualZeroWhenClosingConnectionThenSysCallIsNotCalledAndFalseReturned) {
zet_debug_config_t config = {};
config.pid = 0x1234;

View File

@@ -15,6 +15,15 @@
#include "level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h"
#include <memory>
namespace NEO {
namespace SysCalls {
extern uint32_t closeFuncCalled;
extern int closeFuncArgPassed;
extern int closeFuncRetVal;
} // namespace SysCalls
} // namespace NEO
namespace L0 {
namespace ult {
@@ -190,6 +199,29 @@ TEST_F(TileAttachTest, GivenTileAttachDisabledAndMultitileDeviceWhenCreatingTile
ASSERT_EQ(0u, session->tileSessions.size());
}
TEST_F(TileAttachTest, givenTileDeviceWhenCallingDebugDetachOnLastSessionThenRootSessionIsClosed) {
zet_debug_config_t config = {};
config.pid = 0x1234;
zet_debug_session_handle_t debugSession0 = nullptr;
auto result = zetDebugAttach(neoDevice->getSubDevice(0)->getSpecializedDevice<L0::Device>()->toHandle(), &config, &debugSession0);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, debugSession0);
NEO::SysCalls::closeFuncCalled = 0;
NEO::SysCalls::closeFuncArgPassed = 0;
auto debugFd = rootSession->fd;
result = zetDebugDetach(debugSession0);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_EQ(1u, NEO::SysCalls::closeFuncCalled);
EXPECT_EQ(debugFd, NEO::SysCalls::closeFuncArgPassed);
NEO::SysCalls::closeFuncCalled = 0;
NEO::SysCalls::closeFuncArgPassed = 0;
}
TEST_F(TileAttachTest, givenTileDeviceWhenCallingDebugAttachAndDetachThenSuccessAndValidSessionHandleAreReturned) {
zet_debug_config_t config = {};
config.pid = 0x1234;