Skip to content

Commit 7988c4b

Browse files
Revert "fix(sysman): fixes multithread access issue with FSAccess"
This reverts commit d14cefb. Signed-off-by: ocldev <[email protected]>
1 parent a5240b3 commit 7988c4b

File tree

8 files changed

+62
-89
lines changed

8 files changed

+62
-89
lines changed

level_zero/sysman/source/linux/sysman_fs_access.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ FdCache::~FdCache() {
7474

7575
template <typename T>
7676
ze_result_t FsAccess::readValue(const std::string file, T &val) {
77-
auto lock = this->obtainMutex();
7877

7978
std::string readVal(64, '\0');
8079
int fd = pFdCache->getFd(file);
@@ -304,10 +303,6 @@ bool FsAccess::directoryExists(const std::string path) {
304303
return true;
305304
}
306305

307-
std::unique_lock<std::mutex> FsAccess::obtainMutex() {
308-
return std::unique_lock<std::mutex>(this->fsMutex);
309-
}
310-
311306
// Procfs Access
312307
const std::string ProcfsAccess::procDir = "/proc/";
313308
const std::string ProcfsAccess::fdDir = "/fd/";

level_zero/sysman/source/linux/sysman_fs_access.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <list>
1818
#include <map>
1919
#include <memory>
20-
#include <mutex>
2120
#include <sstream>
2221
#include <string>
2322
#include <sys/stat.h>
@@ -78,13 +77,11 @@ class FsAccess {
7877
FsAccess();
7978
decltype(&NEO::SysCalls::access) accessSyscall = NEO::SysCalls::access;
8079
decltype(&stat) statSyscall = stat;
81-
MOCKABLE_VIRTUAL std::unique_lock<std::mutex> obtainMutex();
8280

8381
private:
8482
template <typename T>
8583
ze_result_t readValue(const std::string file, T &val);
8684
std::unique_ptr<FdCache> pFdCache = nullptr;
87-
std::mutex fsMutex{};
8885
};
8986

9087
class ProcfsAccess : private FsAccess {

level_zero/sysman/source/shared/linux/sysman_fs_access_interface.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void FdCacheInterface::eraseLeastUsedEntryFromCache() {
4545
}
4646

4747
int FdCacheInterface::getFd(std::string file) {
48+
std::unique_lock<std::mutex> lock(fdMutex);
4849
int fd = -1;
4950
if (fdMap.find(file) == fdMap.end()) {
5051
fd = NEO::SysCalls::open(file.c_str(), O_RDONLY);
@@ -71,7 +72,6 @@ FdCacheInterface::~FdCacheInterface() {
7172

7273
template <typename T>
7374
ze_result_t FsAccessInterface::readValue(const std::string file, T &val) {
74-
auto lock = this->obtainMutex();
7575

7676
std::string readVal(64, '\0');
7777
int fd = pFdCacheInterface->getFd(file);
@@ -303,10 +303,6 @@ bool FsAccessInterface::directoryExists(const std::string path) {
303303
return true;
304304
}
305305

306-
std::unique_lock<std::mutex> FsAccessInterface::obtainMutex() {
307-
return std::unique_lock<std::mutex>(this->fsMutex);
308-
}
309-
310306
// Procfs Access
311307
ProcFsAccessInterface::ProcFsAccessInterface() = default;
312308
ProcFsAccessInterface::~ProcFsAccessInterface() = default;

level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FdCacheInterface {
3232
std::map<std::string, std::pair<int, uint32_t>> fdMap = {};
3333

3434
private:
35+
std::mutex fdMutex{};
3536
void eraseLeastUsedEntryFromCache();
3637
};
3738

@@ -66,13 +67,11 @@ class FsAccessInterface {
6667
FsAccessInterface();
6768
decltype(&NEO::SysCalls::access) accessSyscall = NEO::SysCalls::access;
6869
decltype(&stat) statSyscall = stat;
69-
MOCKABLE_VIRTUAL std::unique_lock<std::mutex> obtainMutex();
7070

7171
private:
7272
template <typename T>
7373
ze_result_t readValue(const std::string file, T &val);
7474
std::unique_ptr<FdCacheInterface> pFdCacheInterface = nullptr;
75-
std::mutex fsMutex{};
7675
};
7776

7877
class ProcFsAccessInterface : private FsAccessInterface {

level_zero/sysman/test/unit_tests/sources/linux/test_sysman.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -219,39 +219,6 @@ TEST_F(SysmanDeviceFixture, GivenSysfsAccessClassAndIntegerWhenCallingReadOnMult
219219
delete tempSysfsAccess;
220220
}
221221

222-
TEST_F(SysmanDeviceFixture, GivenValidMockMutexFsAccessWhenCallingReadThenMutexLockCounterMatchesNumberOfReadCalls) {
223-
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
224-
return 1;
225-
});
226-
227-
VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
228-
std::string value = "123";
229-
memcpy(buf, value.data(), value.size());
230-
return value.size();
231-
});
232-
233-
class MockMutexFsAccess : public L0::Sysman::FsAccessInterface {
234-
public:
235-
uint32_t mutexLockCounter = 0;
236-
std::unique_lock<std::mutex> obtainMutex() override {
237-
mutexLockCounter++;
238-
std::unique_lock<std::mutex> mutexLock = L0::Sysman::FsAccessInterface::obtainMutex();
239-
EXPECT_TRUE(mutexLock.owns_lock());
240-
return mutexLock;
241-
}
242-
};
243-
MockMutexFsAccess *tempFsAccess = new MockMutexFsAccess();
244-
std::string fileName = {};
245-
uint32_t iVal32;
246-
uint32_t testReadCount = 10;
247-
for (uint32_t i = 0; i < testReadCount; i++) {
248-
fileName = "mockfile" + std::to_string(i) + ".txt";
249-
EXPECT_EQ(ZE_RESULT_SUCCESS, tempFsAccess->read(fileName, iVal32));
250-
}
251-
EXPECT_EQ(tempFsAccess->mutexLockCounter, testReadCount);
252-
delete tempFsAccess;
253-
}
254-
255222
TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnSameFileThenVerifyCacheIsUpdatedProperly) {
256223

257224
class MockFdCache : public FdCache {
@@ -290,6 +257,35 @@ TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnSameFileThenVerifyCacheIsUp
290257
EXPECT_EQ(pFdCache->fdMap.end(), pFdCache->fdMap.find("mockfile9.txt"));
291258
}
292259

260+
TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnMultipleFilesManyTimesThenVerifyCacheIsUpdatedCorrectly) {
261+
262+
class MockFdCache : public FdCacheInterface {
263+
public:
264+
using FdCacheInterface::fdMap;
265+
};
266+
267+
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
268+
return 1;
269+
});
270+
271+
std::unique_ptr<MockFdCache> pFdCache = std::make_unique<MockFdCache>();
272+
std::string fileName = {};
273+
for (auto i = 0; i < L0::Sysman::FdCacheInterface::maxSize; i++) {
274+
fileName = "mockfile" + std::to_string(i) + ".txt";
275+
int j = i + 1;
276+
while (j--) {
277+
EXPECT_LE(0, pFdCache->getFd(fileName));
278+
}
279+
}
280+
281+
// replace a least referred file and add new file
282+
fileName = "mockfile100.txt";
283+
EXPECT_LE(0, pFdCache->getFd(fileName));
284+
285+
// Verify cache doesn't have an element that is accessed less number of times.
286+
EXPECT_EQ(pFdCache->fdMap.end(), pFdCache->fdMap.find("mockfile0.txt"));
287+
}
288+
293289
TEST(FdCacheTest, GivenValidFdCacheWhenClearingCacheThenVerifyProperFdsAreClosedAndCacheIsUpdatedProperly) {
294290

295291
class MockFdCache : public FdCache {

level_zero/tools/source/sysman/linux/fs_access.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void FdCache::eraseLeastUsedEntryFromCache() {
4646
}
4747

4848
int FdCache::getFd(std::string file) {
49+
std::unique_lock<std::mutex> lock(fdMutex);
4950
int fd = -1;
5051
if (fdMap.find(file) == fdMap.end()) {
5152
fd = NEO::SysCalls::open(file.c_str(), O_RDONLY);
@@ -72,7 +73,6 @@ FdCache::~FdCache() {
7273

7374
template <typename T>
7475
ze_result_t FsAccess::readValue(const std::string file, T &val) {
75-
auto lock = this->obtainMutex();
7676

7777
std::string readVal(64, '\0');
7878
int fd = pFdCache->getFd(file);
@@ -308,10 +308,6 @@ bool FsAccess::directoryExists(const std::string path) {
308308
return true;
309309
}
310310

311-
std::unique_lock<std::mutex> FsAccess::obtainMutex() {
312-
return std::unique_lock<std::mutex>(this->fsMutex);
313-
}
314-
315311
// Procfs Access
316312
const std::string ProcfsAccess::procDir = "/proc/";
317313
const std::string ProcfsAccess::fdDir = "/fd/";

level_zero/tools/source/sysman/linux/fs_access.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class FdCache {
4242
std::map<std::string, std::pair<int, uint32_t>> fdMap = {};
4343

4444
private:
45+
std::mutex fdMutex{};
4546
void eraseLeastUsedEntryFromCache();
4647
};
4748

@@ -78,13 +79,11 @@ class FsAccess {
7879
FsAccess();
7980
decltype(&NEO::SysCalls::access) accessSyscall = NEO::SysCalls::access;
8081
decltype(&stat) statSyscall = stat;
81-
MOCKABLE_VIRTUAL std::unique_lock<std::mutex> obtainMutex();
8282

8383
private:
8484
template <typename T>
8585
ze_result_t readValue(const std::string file, T &val);
8686
std::unique_ptr<FdCache> pFdCache = nullptr;
87-
std::mutex fsMutex{};
8887
};
8988

9089
class ProcfsAccess : private FsAccess {

level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -414,40 +414,6 @@ TEST_F(SysmanDeviceFixture, GivenSysfsAccessClassAndIntegerWhenCallingReadThenSu
414414
delete tempSysfsAccess;
415415
}
416416

417-
TEST_F(SysmanDeviceFixture, GivenValidMockMutexFsAccessWhenCallingReadThenMutexLockCounterMatchesNumberOfReadCalls) {
418-
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
419-
return 1;
420-
});
421-
422-
VariableBackup<decltype(NEO::SysCalls::sysCallsPread)> mockPread(&NEO::SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
423-
std::string value = "123";
424-
memcpy(buf, value.data(), value.size());
425-
return value.size();
426-
});
427-
428-
class MockMutexFsAccess : public L0::FsAccess {
429-
public:
430-
uint32_t mutexLockCounter = 0;
431-
std::unique_lock<std::mutex> obtainMutex() override {
432-
mutexLockCounter++;
433-
std::unique_lock<std::mutex> mutexLock = L0::FsAccess::obtainMutex();
434-
EXPECT_TRUE(mutexLock.owns_lock());
435-
return mutexLock;
436-
}
437-
};
438-
439-
MockMutexFsAccess *tempFsAccess = new MockMutexFsAccess();
440-
std::string fileName = {};
441-
uint32_t iVal32;
442-
uint32_t testReadCount = 10;
443-
for (uint32_t i = 0; i < testReadCount; i++) {
444-
fileName = "mockfile" + std::to_string(i) + ".txt";
445-
EXPECT_EQ(ZE_RESULT_SUCCESS, tempFsAccess->read(fileName, iVal32));
446-
}
447-
EXPECT_EQ(tempFsAccess->mutexLockCounter, testReadCount);
448-
delete tempFsAccess;
449-
}
450-
451417
TEST_F(SysmanDeviceFixture, GivenSysfsAccessClassAndOpenSysCallFailsWhenCallingReadThenFailureIsReturned) {
452418

453419
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
@@ -574,6 +540,35 @@ TEST(FdCacheTest, GivenValidFdCacheWhenClearingCacheThenVerifyProperFdsAreClosed
574540
delete pFdCache;
575541
}
576542

543+
TEST(FdCacheTest, GivenValidFdCacheWhenCallingGetFdOnMultipleFilesManyTimesThenVerifyCacheIsUpdatedCorrectly) {
544+
545+
class MockFdCache : public FdCache {
546+
public:
547+
using FdCache::fdMap;
548+
};
549+
550+
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
551+
return 1;
552+
});
553+
554+
std::unique_ptr<MockFdCache> pFdCache = std::make_unique<MockFdCache>();
555+
std::string fileName = {};
556+
for (auto i = 0; i < L0::FdCache::maxSize; i++) {
557+
fileName = "mockfile" + std::to_string(i) + ".txt";
558+
int j = i + 1;
559+
while (j--) {
560+
EXPECT_LE(0, pFdCache->getFd(fileName));
561+
}
562+
}
563+
564+
// replace a least referred file and add new file
565+
fileName = "mockfile100.txt";
566+
EXPECT_LE(0, pFdCache->getFd(fileName));
567+
568+
// Verify cache doesn't have an element that is accessed less number of times.
569+
EXPECT_EQ(pFdCache->fdMap.end(), pFdCache->fdMap.find("mockfile0.txt"));
570+
}
571+
577572
TEST_F(SysmanDeviceFixture, GivenSysfsAccessClassAndUnsignedIntegerWhenCallingReadThenSuccessIsReturned) {
578573

579574
VariableBackup<decltype(NEO::SysCalls::sysCallsOpen)> mockOpen(&NEO::SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {

0 commit comments

Comments
 (0)