tst: memfs: code cleanup and testing

This commit is contained in:
Bill Zissimopoulos 2017-11-02 15:43:14 -07:00
parent 0ab35fde1a
commit b672312c79
No known key found for this signature in database
GPG Key ID: 3D4F95D52C7B3EA3
2 changed files with 25 additions and 30 deletions

View File

@ -44,9 +44,9 @@ NTSTATUS SvcStart(FSP_SERVICE *Service, ULONG argc, PWSTR *argv)
ULONG FileInfoTimeout = INFINITE; ULONG FileInfoTimeout = INFINITE;
ULONG MaxFileNodes = 1024; ULONG MaxFileNodes = 1024;
ULONG MaxFileSize = 16 * 1024 * 1024; ULONG MaxFileSize = 16 * 1024 * 1024;
ULONG SlowioMaxDelay = 0; // From -M : maximum delay in milliseconds ULONG SlowioMaxDelay = 0; /* -M: maximum slow IO delay in millis */
ULONG SlowioPercentDelay = 0; // From -D : percent of IO to slow down ULONG SlowioPercentDelay = 0; /* -P: percent of slow IO to make pending */
ULONG SlowioRarefyDelay = 0; // From -R : adjust the rarity of long delays ULONG SlowioRarefyDelay = 0; /* -R: adjust the rarity of pending slow IO */
PWSTR FileSystemName = 0; PWSTR FileSystemName = 0;
PWSTR MountPoint = 0; PWSTR MountPoint = 0;
PWSTR VolumePrefix = 0; PWSTR VolumePrefix = 0;
@ -205,8 +205,8 @@ usage:
" -n MaxFileNodes\n" " -n MaxFileNodes\n"
" -s MaxFileSize [bytes]\n" " -s MaxFileSize [bytes]\n"
" -M MaxDelay [maximum slow IO delay in millis]\n" " -M MaxDelay [maximum slow IO delay in millis]\n"
" -P PercentDelay [percent of IO to make slow IO]\n" " -P PercentDelay [percent of slow IO to make pending]\n"
" -R RarefyDelay [adjust the rarity of slow IO]\n" " -R RarefyDelay [adjust the rarity of pending slow IO]\n"
" -F FileSystemName\n" " -F FileSystemName\n"
" -S RootSddl [file rights: FA, etc; NO generic rights: GA, etc.]\n" " -S RootSddl [file rights: FA, etc; NO generic rights: GA, etc.]\n"
" -u \\Server\\Share [UNC prefix (single backslash)]\n" " -u \\Server\\Share [UNC prefix (single backslash)]\n"

View File

@ -679,25 +679,29 @@ static inline UINT64 Hash(UINT64 x)
return x; return x;
} }
static inline UINT32 PseudoRandom(UINT32 to) static inline ULONG PseudoRandom(ULONG to)
{ {
/* John Oberschelp's PRNG */
static UINT64 spin = 0; static UINT64 spin = 0;
InterlockedIncrement(&spin); InterlockedIncrement(&spin);
return Hash(spin) % to; return Hash(spin) % to;
} }
static inline UINT32 SlowioMillisecondsOfDelay(FSP_FILE_SYSTEM *FileSystem) static inline BOOLEAN SlowioReturnPending(FSP_FILE_SYSTEM *FileSystem)
{ {
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
return 0 != Memfs->SlowioMaxDelay ? if (0 == Memfs->SlowioMaxDelay)
PseudoRandom(Memfs->SlowioMaxDelay + 1) >> PseudoRandom(Memfs->SlowioRarefyDelay + 1) : 0; return FALSE;
return PseudoRandom(100) < Memfs->SlowioPercentDelay;
} }
static inline bool SlowioReturnPending(FSP_FILE_SYSTEM *FileSystem) static inline VOID SlowioSnooze(FSP_FILE_SYSTEM *FileSystem)
{ {
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
return 0 != Memfs->SlowioMaxDelay && if (0 == Memfs->SlowioMaxDelay)
PseudoRandom(100) < Memfs->SlowioPercentDelay; return;
ULONG millis = PseudoRandom(Memfs->SlowioMaxDelay + 1) >> PseudoRandom(Memfs->SlowioRarefyDelay + 1);
Sleep(millis);
} }
void SlowioReadThread( void SlowioReadThread(
@ -708,10 +712,7 @@ void SlowioReadThread(
UINT64 EndOffset, UINT64 EndOffset,
UINT64 RequestHint) UINT64 RequestHint)
{ {
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; SlowioSnooze(FileSystem);
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem))
Sleep(ms);
memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset)); memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset));
UINT32 BytesTransferred = (ULONG)(EndOffset - Offset); UINT32 BytesTransferred = (ULONG)(EndOffset - Offset);
@ -725,6 +726,7 @@ void SlowioReadThread(
ResponseBuf.IoStatus.Information = BytesTransferred; // bytes read ResponseBuf.IoStatus.Information = BytesTransferred; // bytes read
FspFileSystemSendResponse(FileSystem, &ResponseBuf); FspFileSystemSendResponse(FileSystem, &ResponseBuf);
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
InterlockedDecrement(&Memfs->SlowioThreadsRunning); InterlockedDecrement(&Memfs->SlowioThreadsRunning);
} }
@ -737,10 +739,7 @@ void SlowioWriteThread(
UINT64 RequestHint, UINT64 RequestHint,
FSP_FSCTL_FILE_INFO *FileInfo) FSP_FSCTL_FILE_INFO *FileInfo)
{ {
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; SlowioSnooze(FileSystem);
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem))
Sleep(ms);
memcpy((PUINT8)FileNode->FileData + Offset, Buffer, (size_t)(EndOffset - Offset)); memcpy((PUINT8)FileNode->FileData + Offset, Buffer, (size_t)(EndOffset - Offset));
UINT32 BytesTransferred = (ULONG)(EndOffset - Offset); UINT32 BytesTransferred = (ULONG)(EndOffset - Offset);
@ -755,6 +754,7 @@ void SlowioWriteThread(
ResponseBuf.Rsp.Write.FileInfo = *FileInfo; // FileInfo of file after Write ResponseBuf.Rsp.Write.FileInfo = *FileInfo; // FileInfo of file after Write
FspFileSystemSendResponse(FileSystem, &ResponseBuf); FspFileSystemSendResponse(FileSystem, &ResponseBuf);
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
InterlockedDecrement(&Memfs->SlowioThreadsRunning); InterlockedDecrement(&Memfs->SlowioThreadsRunning);
} }
@ -763,10 +763,7 @@ void SlowioReadDirectoryThread(
ULONG BytesTransferred, ULONG BytesTransferred,
UINT64 RequestHint) UINT64 RequestHint)
{ {
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; SlowioSnooze(FileSystem);
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem))
Sleep(ms);
FSP_FSCTL_TRANSACT_RSP ResponseBuf; FSP_FSCTL_TRANSACT_RSP ResponseBuf;
memset(&ResponseBuf, 0, sizeof ResponseBuf); memset(&ResponseBuf, 0, sizeof ResponseBuf);
@ -777,6 +774,7 @@ void SlowioReadDirectoryThread(
ResponseBuf.IoStatus.Information = BytesTransferred; // bytes of directory info read ResponseBuf.IoStatus.Information = BytesTransferred; // bytes of directory info read
FspFileSystemSendResponse(FileSystem, &ResponseBuf); FspFileSystemSendResponse(FileSystem, &ResponseBuf);
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
InterlockedDecrement(&Memfs->SlowioThreadsRunning); InterlockedDecrement(&Memfs->SlowioThreadsRunning);
} }
#endif #endif
@ -1184,8 +1182,7 @@ static NTSTATUS Read(FSP_FILE_SYSTEM *FileSystem,
return STATUS_PENDING; return STATUS_PENDING;
} }
regular: regular:
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) SlowioSnooze(FileSystem);
Sleep(ms);
#endif #endif
memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset)); memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset));
@ -1261,8 +1258,7 @@ static NTSTATUS Write(FSP_FILE_SYSTEM *FileSystem,
return STATUS_PENDING; return STATUS_PENDING;
} }
regular: regular:
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) SlowioSnooze(FileSystem);
Sleep(ms);
#endif #endif
memcpy((PUINT8)FileNode->FileData + Offset, Buffer, (size_t)(EndOffset - Offset)); memcpy((PUINT8)FileNode->FileData + Offset, Buffer, (size_t)(EndOffset - Offset));
@ -1653,8 +1649,7 @@ static NTSTATUS ReadDirectory(FSP_FILE_SYSTEM *FileSystem,
return STATUS_PENDING; return STATUS_PENDING;
} }
regular: regular:
if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) SlowioSnooze(FileSystem);
Sleep(ms);
#endif #endif
return STATUS_SUCCESS; return STATUS_SUCCESS;