@@ -17,6 +17,13 @@ constexpr uint32_t mockDiagHandleCount = 2;
1717const std::string mockQuiescentGpuFile (" quiesce_gpu" );
1818const std::string mockinvalidateLmemFile (" invalidate_lmem_mmaps" );
1919const std::vector<std::string> mockSupportedDiagTypes = {" MOCKSUITE1" , " MOCKSUITE2" };
20+ const std::string deviceDirDiag (" device" );
21+ const std::string mockdeviceDirDiag (" /sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0" );
22+ const std::string mockRemove (" remove" );
23+ const std::string mockRescan (" rescan" );
24+ const std::string mockRealPathConfig (" /sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/config" );
25+ const std::string mockDeviceName (" /MOCK_DEVICE_NAME" );
26+
2027class DiagnosticsInterface : public FirmwareUtil {};
2128
2229template <>
@@ -56,6 +63,14 @@ struct Mock<DiagnosticsInterface> : public FirmwareUtil {
5663class DiagSysfsAccess : public SysfsAccess {};
5764template <>
5865struct Mock <DiagSysfsAccess> : public DiagSysfsAccess {
66+ ze_result_t getRealPathVal (const std::string file, std::string &val) {
67+ if (file.compare (deviceDirDiag) == 0 ) {
68+ val = mockdeviceDirDiag;
69+ } else {
70+ return ZE_RESULT_ERROR_NOT_AVAILABLE;
71+ }
72+ return ZE_RESULT_SUCCESS;
73+ }
5974
6075 ze_result_t mockwrite (const std::string file, const int val) {
6176 if (std::string::npos != file.find (mockQuiescentGpuFile)) {
@@ -70,14 +85,125 @@ struct Mock<DiagSysfsAccess> : public DiagSysfsAccess {
7085 ze_result_t mockwriteFails (const std::string file, const int val) {
7186 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
7287 }
88+
89+ bool mockIsMyDeviceFile (const std::string dev) {
90+ if (dev.compare (mockDeviceName) == 0 ) {
91+ return true ;
92+ }
93+ return false ;
94+ }
95+
7396 Mock<DiagSysfsAccess>() = default ;
7497
7598 MOCK_METHOD (ze_result_t , write, (const std::string file, const int val), (override ));
99+ MOCK_METHOD (ze_result_t , getRealPath, (const std::string path, std::string &val), (override ));
100+ MOCK_METHOD (bool , isMyDeviceFile, (const std::string dev), (override ));
101+ };
102+
103+ class DiagFsAccess : public FsAccess {};
104+ template <>
105+ struct Mock <DiagFsAccess> : public DiagFsAccess {
106+ ze_result_t mockFsWrite (const std::string file, std::string val) {
107+ if (std::string::npos != file.find (mockRemove)) {
108+ return ZE_RESULT_SUCCESS;
109+ } else if (std::string::npos != file.find (mockRescan)) {
110+ return ZE_RESULT_SUCCESS;
111+ } else {
112+ return ZE_RESULT_ERROR_NOT_AVAILABLE;
113+ }
114+ }
115+ Mock<DiagFsAccess>() = default ;
116+
117+ MOCK_METHOD (ze_result_t , write, (const std::string file, const std::string val), (override ));
118+ };
119+
120+ class DiagProcfsAccess : public ProcfsAccess {};
121+
122+ template <>
123+ struct Mock <DiagProcfsAccess> : public DiagProcfsAccess {
124+
125+ const ::pid_t extraPid = 4 ;
126+ const int extraFd = 5 ;
127+ std::vector<::pid_t > pidList = {1 , 2 , 3 };
128+ std::vector<int > fdList = {0 , 1 , 2 };
129+ ::pid_t ourDevicePid = 0 ;
130+ int ourDeviceFd = 0 ;
131+
132+ ze_result_t mockProcessListDeviceUnused (std::vector<::pid_t > &list) {
133+ list = pidList;
134+ return ZE_RESULT_SUCCESS;
135+ }
136+
137+ ze_result_t mockProcessListDeviceInUse (std::vector<::pid_t > &list) {
138+ list = pidList;
139+ if (ourDevicePid) {
140+ list.push_back (ourDevicePid);
141+ }
142+ return ZE_RESULT_SUCCESS;
143+ }
144+
145+ ::pid_t getMockMyProcessId () {
146+ return ::getpid ();
147+ }
148+
149+ ze_result_t getMockFileDescriptors (const ::pid_t pid, std::vector<int > &list) {
150+ // Give every process 3 file descriptors
151+ // Except the device that MOCK has the device open. Give it one extra.
152+ list.clear ();
153+ list = fdList;
154+ if (ourDevicePid == pid) {
155+ list.push_back (ourDeviceFd);
156+ }
157+ return ZE_RESULT_SUCCESS;
158+ }
159+
160+ ze_result_t getMockFileDescriptorsFailure (const ::pid_t pid, std::vector<int > &list) {
161+ // return failure to verify the error condition check
162+ list.clear ();
163+ return ZE_RESULT_ERROR_UNKNOWN;
164+ }
165+
166+ ze_result_t getMockFileName (const ::pid_t pid, const int fd, std::string &val) {
167+ if (pid == ourDevicePid && fd == ourDeviceFd) {
168+ val = mockDeviceName;
169+ } else {
170+ // return fake filenames for other file descriptors
171+ val = std::string (" /FILENAME" ) + std::to_string (fd);
172+ }
173+ return ZE_RESULT_SUCCESS;
174+ }
175+
176+ bool mockIsAlive (const ::pid_t pid) {
177+ if (pid == ourDevicePid) {
178+ return true ;
179+ }
180+ return false ;
181+ }
182+
183+ void mockKill (const ::pid_t pid) {
184+ ourDevicePid = 0 ;
185+ }
186+
187+ Mock<DiagProcfsAccess>() = default ;
188+
189+ MOCK_METHOD (ze_result_t , listProcesses, (std::vector<::pid_t > & list), (override ));
190+ MOCK_METHOD (::pid_t , myProcessId, (), (override ));
191+ MOCK_METHOD (ze_result_t , getFileDescriptors, (const ::pid_t pid, std::vector<int > &list), (override ));
192+ MOCK_METHOD (ze_result_t , getFileName, (const ::pid_t pid, const int fd, std::string &val), (override ));
193+ MOCK_METHOD (bool , isAlive, (const ::pid_t pid), (override ));
194+ MOCK_METHOD (void , kill, (const ::pid_t pid), (override ));
76195};
77196
78197class PublicLinuxDiagnosticsImp : public L0 ::LinuxDiagnosticsImp {
79198 public:
199+ using LinuxDiagnosticsImp::closeFunction;
200+ using LinuxDiagnosticsImp::openFunction;
201+ using LinuxDiagnosticsImp::pFsAccess;
80202 using LinuxDiagnosticsImp::pFwInterface;
203+ using LinuxDiagnosticsImp::pProcfsAccess;
204+ using LinuxDiagnosticsImp::preadFunction;
205+ using LinuxDiagnosticsImp::pSysfsAccess;
206+ using LinuxDiagnosticsImp::pwriteFunction;
81207};
82208} // namespace ult
83209} // namespace L0
0 commit comments