-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More WriteGather fixes #109826
Open
adamsitnik
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
adamsitnik:evenMore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−68
Open
More WriteGather fixes #109826
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
02f7f4f
macOS does not define IOV_MAX, it uses UIO_MAXIOV instead
adamsitnik 93c6b8c
don't run these tests in parallel, as each test cases uses more than …
adamsitnik e7a288c
fix the test: handle incomplete reads that should happen when we hit …
adamsitnik cb37a35
incomplete write fix:
adamsitnik 00d209a
try one more thing: IOV_MAX should be available everywhere
adamsitnik 31a3a00
I can't reproduce it locally, so here we go...
adamsitnik 2de6b8c
IOV_MAX is reported as 1024, but it fails with EINVAL. What value is …
adamsitnik 6a53ac4
new approach: prefer sysconf(_SC_IOV_MAX) over other options
adamsitnik e389425
Apply suggestions from code review
adamsitnik 6e5c47a
Use native memory to get OOM a soon as we run out of memory (hoping t…
adamsitnik 87ee2e5
For macOS preadv and pwritev can fail with EINVAL when the total leng…
adamsitnik 3c11b65
move the code to finally block so when Pin throws, we dispose other h…
adamsitnik 2c685d4
add an assert that is going to warn us if vector.Count is ever more t…
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
|
@@ -13,6 +14,7 @@ | |
namespace System.IO.Tests | ||
{ | ||
[SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")] | ||
[Collection(nameof(DisableParallelization))] // don't run in parallel, as some of these tests use a LOT of resources | ||
public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask> | ||
{ | ||
protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset) | ||
|
@@ -151,21 +153,6 @@ public async Task NoInt32OverflowForLargeInputs(bool asyncFile, bool asyncMethod | |
const int BufferSize = int.MaxValue / 1000; | ||
const long FileSize = (long)BufferCount * BufferSize; | ||
string filePath = GetTestFilePath(); | ||
ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); | ||
List<ReadOnlyMemory<byte>> writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToList(); | ||
List<Memory<byte>> readBuffers = new List<Memory<byte>>(BufferCount); | ||
|
||
try | ||
{ | ||
for (int i = 0; i < BufferCount; i++) | ||
{ | ||
readBuffers.Add(new byte[BufferSize]); | ||
} | ||
} | ||
catch (OutOfMemoryException) | ||
{ | ||
throw new SkipTestException("Not enough memory."); | ||
} | ||
|
||
FileOptions options = asyncFile ? FileOptions.Asynchronous : FileOptions.None; // we need to test both code paths | ||
options |= FileOptions.DeleteOnClose; | ||
|
@@ -180,29 +167,86 @@ public async Task NoInt32OverflowForLargeInputs(bool asyncFile, bool asyncMethod | |
throw new SkipTestException("Not enough disk space."); | ||
} | ||
|
||
long fileOffset = 0, bytesRead = 0; | ||
try | ||
using (sfh) | ||
{ | ||
if (asyncMethod) | ||
ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); | ||
List<ReadOnlyMemory<byte>> writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToList(); | ||
|
||
List<NativeMemoryManager> memoryManagers = new List<NativeMemoryManager>(BufferCount); | ||
List<Memory<byte>> readBuffers = new List<Memory<byte>>(BufferCount); | ||
|
||
try | ||
{ | ||
await RandomAccess.WriteAsync(sfh, writeBuffers, fileOffset); | ||
bytesRead = await RandomAccess.ReadAsync(sfh, readBuffers, fileOffset); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was a test bug, the test assumed that the read won't ever be a partial read |
||
try | ||
{ | ||
for (int i = 0; i < BufferCount; i++) | ||
{ | ||
// We are using native memory here to get OOM as soon as possible. | ||
NativeMemoryManager nativeMemoryManager = new(BufferSize); | ||
memoryManagers.Add(nativeMemoryManager); | ||
readBuffers.Add(nativeMemoryManager.Memory); | ||
} | ||
} | ||
catch (OutOfMemoryException) | ||
{ | ||
throw new SkipTestException("Not enough memory."); | ||
} | ||
|
||
await Verify(asyncMethod, FileSize, sfh, writeBuffer, writeBuffers, readBuffers); | ||
} | ||
else | ||
finally | ||
{ | ||
RandomAccess.Write(sfh, writeBuffers, fileOffset); | ||
bytesRead = RandomAccess.Read(sfh, readBuffers, fileOffset); | ||
foreach (IDisposable memoryManager in memoryManagers) | ||
{ | ||
memoryManager.Dispose(); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
sfh.Dispose(); // delete the file ASAP to avoid running out of resources in CI | ||
} | ||
|
||
Assert.Equal(FileSize, bytesRead); | ||
for (int i = 0; i < BufferCount; i++) | ||
static async Task Verify(bool asyncMethod, long FileSize, SafeFileHandle sfh, ReadOnlyMemory<byte> writeBuffer, List<ReadOnlyMemory<byte>> writeBuffers, List<Memory<byte>> readBuffers) | ||
{ | ||
Assert.Equal(writeBuffer, readBuffers[i]); | ||
if (asyncMethod) | ||
{ | ||
await RandomAccess.WriteAsync(sfh, writeBuffers, 0); | ||
} | ||
else | ||
{ | ||
RandomAccess.Write(sfh, writeBuffers, 0); | ||
} | ||
|
||
Assert.Equal(FileSize, RandomAccess.GetLength(sfh)); | ||
|
||
long fileOffset = 0; | ||
while (fileOffset < FileSize) | ||
{ | ||
long bytesRead = asyncMethod | ||
? await RandomAccess.ReadAsync(sfh, readBuffers, fileOffset) | ||
: RandomAccess.Read(sfh, readBuffers, fileOffset); | ||
|
||
Assert.InRange(bytesRead, 0, FileSize); | ||
|
||
while (bytesRead > 0) | ||
{ | ||
Memory<byte> readBuffer = readBuffers[0]; | ||
if (bytesRead >= readBuffer.Length) | ||
{ | ||
AssertExtensions.SequenceEqual(writeBuffer.Span, readBuffer.Span); | ||
|
||
bytesRead -= readBuffer.Length; | ||
fileOffset += readBuffer.Length; | ||
|
||
readBuffers.RemoveAt(0); | ||
} | ||
else | ||
{ | ||
// A read has finished somewhere in the middle of one of the read buffers. | ||
// Example: buffer had 30 bytes and only 10 were read. | ||
// We don't read the missing part, but try to read the whole buffer again. | ||
// It's not optimal from performance perspective, but it keeps the test logic simple. | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far, for incomplete writes we were pinning the memory for every retry attempt. I am not sure if this can create some kind of edge case bugs, but I think we can do it just once, before we enter the main loop