fix(sysman): fixes multithread access issue with FSAccess

Related-To: NEO-9720

Signed-off-by: Kulkarni, Ashwin Kumar <ashwin.kumar.kulkarni@intel.com>
This commit is contained in:
Kulkarni, Ashwin Kumar
2023-12-08 15:22:42 +00:00
committed by Compute-Runtime-Automation
parent a2994e9b29
commit b33d9955a1
6 changed files with 81 additions and 62 deletions

View File

@@ -45,7 +45,6 @@ void FdCacheInterface::eraseLeastUsedEntryFromCache() {
}
int FdCacheInterface::getFd(std::string file) {
std::unique_lock<std::mutex> lock(fdMutex);
int fd = -1;
if (fdMap.find(file) == fdMap.end()) {
fd = NEO::SysCalls::open(file.c_str(), O_RDONLY);
@@ -72,6 +71,7 @@ FdCacheInterface::~FdCacheInterface() {
template <typename T>
ze_result_t FsAccessInterface::readValue(const std::string file, T &val) {
auto lock = this->obtainMutex();
std::string readVal(64, '\0');
int fd = pFdCacheInterface->getFd(file);
@@ -303,6 +303,10 @@ bool FsAccessInterface::directoryExists(const std::string path) {
return true;
}
std::unique_lock<std::mutex> FsAccessInterface::obtainMutex() {
return std::unique_lock<std::mutex>(this->fsMutex);
}
// Procfs Access
ProcFsAccessInterface::ProcFsAccessInterface() = default;
ProcFsAccessInterface::~ProcFsAccessInterface() = default;

View File

@@ -32,7 +32,6 @@ class FdCacheInterface {
std::map<std::string, std::pair<int, uint32_t>> fdMap = {};
private:
std::mutex fdMutex{};
void eraseLeastUsedEntryFromCache();
};
@@ -65,11 +64,13 @@ class FsAccessInterface {
protected:
FsAccessInterface();
MOCKABLE_VIRTUAL std::unique_lock<std::mutex> obtainMutex();
private:
template <typename T>
ze_result_t readValue(const std::string file, T &val);
std::unique_ptr<FdCacheInterface> pFdCacheInterface = nullptr;
std::mutex fsMutex{};
};
class ProcFsAccessInterface : private FsAccessInterface {

View File

@@ -180,6 +180,39 @@ TEST_F(SysmanDeviceFixture, GivenSysfsAccessClassAndIntegerWhenCallingReadOnMult
delete tempSysfsAccess;
}
TEST_F(SysmanDeviceFixture, GivenValidMockMutexFsAccessWhenCallingReadThenMutexLockCounterMatchesNumberOfReadCalls) {
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
std::string value = "123";
memcpy(buf, value.data(), value.size());
return value.size();
});
class MockMutexFsAccess : public L0::Sysman::FsAccessInterface {
public:
uint32_t mutexLockCounter = 0;
std::unique_lock<std::mutex> obtainMutex() override {
mutexLockCounter++;
std::unique_lock<std::mutex> mutexLock = L0::Sysman::FsAccessInterface::obtainMutex();
EXPECT_TRUE(mutexLock.owns_lock());
return mutexLock;
}
};
MockMutexFsAccess *tempFsAccess = new MockMutexFsAccess();
std::string fileName = {};
uint32_t iVal32;
uint32_t testReadCount = 10;
for (uint32_t i = 0; i < testReadCount; i++) {
fileName = "mockfile" + std::to_string(i) + ".txt";
EXPECT_EQ(ZE_RESULT_SUCCESS, tempFsAccess->read(fileName, iVal32));
}
EXPECT_EQ(tempFsAccess->mutexLockCounter, testReadCount);
delete tempFsAccess;
}
TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnSameFileThenVerifyCacheIsUpdatedProperly) {
class MockFdCache : public FdCacheInterface {
@@ -218,35 +251,6 @@ TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnSameFileThenVerifyCacheIsUp
EXPECT_EQ(pFdCache->fdMap.end(), pFdCache->fdMap.find("mockfile9.txt"));
}
TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnMultipleFilesManyTimesThenVerifyCacheIsUpdatedCorrectly) {
class MockFdCache : public FdCacheInterface {
public:
using FdCacheInterface::fdMap;
};
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
std::unique_ptr<MockFdCache> pFdCache = std::make_unique<MockFdCache>();
std::string fileName = {};
for (auto i = 0; i < L0::Sysman::FdCacheInterface::maxSize; i++) {
fileName = "mockfile" + std::to_string(i) + ".txt";
int j = i + 1;
while (j--) {
EXPECT_LE(0, pFdCache->getFd(fileName));
}
}
// replace a least referred file and add new file
fileName = "mockfile100.txt";
EXPECT_LE(0, pFdCache->getFd(fileName));
// Verify cache doesn't have an element that is accessed less number of times.
EXPECT_EQ(pFdCache->fdMap.end(), pFdCache->fdMap.find("mockfile0.txt"));
}
TEST(FdCacheTest, GivenValidFdCacheWhenClearingCacheThenVerifyProperFdsAreClosedAndCacheIsUpdatedProperly) {
class MockFdCache : public FdCacheInterface {