From b672312c79f4aca3af6a1eb43e058904d6c5f911 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Thu, 2 Nov 2017 15:43:14 -0700 Subject: [PATCH] tst: memfs: code cleanup and testing --- tst/memfs/memfs-main.c | 10 +++++----- tst/memfs/memfs.cpp | 45 +++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/tst/memfs/memfs-main.c b/tst/memfs/memfs-main.c index a03bd94e..da5e3a2e 100644 --- a/tst/memfs/memfs-main.c +++ b/tst/memfs/memfs-main.c @@ -44,9 +44,9 @@ NTSTATUS SvcStart(FSP_SERVICE *Service, ULONG argc, PWSTR *argv) ULONG FileInfoTimeout = INFINITE; ULONG MaxFileNodes = 1024; ULONG MaxFileSize = 16 * 1024 * 1024; - ULONG SlowioMaxDelay = 0; // From -M : maximum delay in milliseconds - ULONG SlowioPercentDelay = 0; // From -D : percent of IO to slow down - ULONG SlowioRarefyDelay = 0; // From -R : adjust the rarity of long delays + ULONG SlowioMaxDelay = 0; /* -M: maximum slow IO delay in millis */ + ULONG SlowioPercentDelay = 0; /* -P: percent of slow IO to make pending */ + ULONG SlowioRarefyDelay = 0; /* -R: adjust the rarity of pending slow IO */ PWSTR FileSystemName = 0; PWSTR MountPoint = 0; PWSTR VolumePrefix = 0; @@ -205,8 +205,8 @@ usage: " -n MaxFileNodes\n" " -s MaxFileSize [bytes]\n" " -M MaxDelay [maximum slow IO delay in millis]\n" - " -P PercentDelay [percent of IO to make slow IO]\n" - " -R RarefyDelay [adjust the rarity of slow IO]\n" + " -P PercentDelay [percent of slow IO to make pending]\n" + " -R RarefyDelay [adjust the rarity of pending slow IO]\n" " -F FileSystemName\n" " -S RootSddl [file rights: FA, etc; NO generic rights: GA, etc.]\n" " -u \\Server\\Share [UNC prefix (single backslash)]\n" diff --git a/tst/memfs/memfs.cpp b/tst/memfs/memfs.cpp index b126f6a0..e5bac9ea 100644 --- a/tst/memfs/memfs.cpp +++ b/tst/memfs/memfs.cpp @@ -679,25 +679,29 @@ static inline UINT64 Hash(UINT64 x) return x; } -static inline UINT32 PseudoRandom(UINT32 to) +static inline ULONG PseudoRandom(ULONG to) { + /* John Oberschelp's PRNG */ static UINT64 spin = 0; InterlockedIncrement(&spin); 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; - return 0 != Memfs->SlowioMaxDelay ? - PseudoRandom(Memfs->SlowioMaxDelay + 1) >> PseudoRandom(Memfs->SlowioRarefyDelay + 1) : 0; + if (0 == Memfs->SlowioMaxDelay) + 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; - return 0 != Memfs->SlowioMaxDelay && - PseudoRandom(100) < Memfs->SlowioPercentDelay; + if (0 == Memfs->SlowioMaxDelay) + return; + ULONG millis = PseudoRandom(Memfs->SlowioMaxDelay + 1) >> PseudoRandom(Memfs->SlowioRarefyDelay + 1); + Sleep(millis); } void SlowioReadThread( @@ -708,10 +712,7 @@ void SlowioReadThread( UINT64 EndOffset, UINT64 RequestHint) { - MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; - - if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) - Sleep(ms); + SlowioSnooze(FileSystem); memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset)); UINT32 BytesTransferred = (ULONG)(EndOffset - Offset); @@ -725,6 +726,7 @@ void SlowioReadThread( ResponseBuf.IoStatus.Information = BytesTransferred; // bytes read FspFileSystemSendResponse(FileSystem, &ResponseBuf); + MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; InterlockedDecrement(&Memfs->SlowioThreadsRunning); } @@ -737,11 +739,8 @@ void SlowioWriteThread( UINT64 RequestHint, 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)); UINT32 BytesTransferred = (ULONG)(EndOffset - Offset); @@ -755,6 +754,7 @@ void SlowioWriteThread( ResponseBuf.Rsp.Write.FileInfo = *FileInfo; // FileInfo of file after Write FspFileSystemSendResponse(FileSystem, &ResponseBuf); + MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; InterlockedDecrement(&Memfs->SlowioThreadsRunning); } @@ -763,10 +763,7 @@ void SlowioReadDirectoryThread( ULONG BytesTransferred, UINT64 RequestHint) { - MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; - - if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) - Sleep(ms); + SlowioSnooze(FileSystem); FSP_FSCTL_TRANSACT_RSP ResponseBuf; memset(&ResponseBuf, 0, sizeof ResponseBuf); @@ -777,6 +774,7 @@ void SlowioReadDirectoryThread( ResponseBuf.IoStatus.Information = BytesTransferred; // bytes of directory info read FspFileSystemSendResponse(FileSystem, &ResponseBuf); + MEMFS *Memfs = (MEMFS *)FileSystem->UserContext; InterlockedDecrement(&Memfs->SlowioThreadsRunning); } #endif @@ -1184,8 +1182,7 @@ static NTSTATUS Read(FSP_FILE_SYSTEM *FileSystem, return STATUS_PENDING; } regular: - if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) - Sleep(ms); + SlowioSnooze(FileSystem); #endif memcpy(Buffer, (PUINT8)FileNode->FileData + Offset, (size_t)(EndOffset - Offset)); @@ -1261,8 +1258,7 @@ static NTSTATUS Write(FSP_FILE_SYSTEM *FileSystem, return STATUS_PENDING; } regular: - if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) - Sleep(ms); + SlowioSnooze(FileSystem); #endif memcpy((PUINT8)FileNode->FileData + Offset, Buffer, (size_t)(EndOffset - Offset)); @@ -1653,8 +1649,7 @@ static NTSTATUS ReadDirectory(FSP_FILE_SYSTEM *FileSystem, return STATUS_PENDING; } regular: - if (UINT32 ms = SlowioMillisecondsOfDelay(FileSystem)) - Sleep(ms); + SlowioSnooze(FileSystem); #endif return STATUS_SUCCESS;