This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 523
Improved Memoryblock tracking #998
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure | ||
{ | ||
|
@@ -64,16 +65,33 @@ public class MemoryPool : IDisposable | |
/// Called to take a block from the pool. | ||
/// </summary> | ||
/// <returns>The block that is reserved for the called. It must be passed to Return when it is no longer being used.</returns> | ||
#if DEBUG | ||
public MemoryPoolBlock Lease([CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0) | ||
{ | ||
Debug.Assert(!_disposedValue, "Block being leased from disposed pool!"); | ||
#else | ||
public MemoryPoolBlock Lease() | ||
{ | ||
#endif | ||
MemoryPoolBlock block; | ||
if (_blocks.TryDequeue(out block)) | ||
{ | ||
// block successfully taken from the stack - return it | ||
#if DEBUG | ||
block.Leaser = memberName + ", " + sourceFilePath + ", " + sourceLineNumber; | ||
block.IsLeased = true; | ||
#endif | ||
return block; | ||
} | ||
// no blocks available - grow the pool | ||
return AllocateSlab(); | ||
block = AllocateSlab(); | ||
#if DEBUG | ||
block.Leaser = memberName + ", " + sourceFilePath + ", " + sourceLineNumber; | ||
block.IsLeased = true; | ||
#endif | ||
return block; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -100,6 +118,9 @@ private MemoryPoolBlock AllocateSlab() | |
basePtr, | ||
this, | ||
slab); | ||
#if DEBUG | ||
block.IsLeased = true; | ||
#endif | ||
Return(block); | ||
} | ||
|
||
|
@@ -123,19 +144,33 @@ private MemoryPoolBlock AllocateSlab() | |
/// <param name="block">The block to return. It must have been acquired by calling Lease on the same memory pool instance.</param> | ||
public void Return(MemoryPoolBlock block) | ||
{ | ||
#if DEBUG | ||
Debug.Assert(block.Pool == this, "Returned block was not leased from this pool"); | ||
Debug.Assert(block.IsLeased, $"Block being returned to pool twice: {block.Leaser}{Environment.NewLine}"); | ||
block.IsLeased = false; | ||
#endif | ||
|
||
if (block.Slab != null && block.Slab.IsActive) | ||
{ | ||
block.Reset(); | ||
_blocks.Enqueue(block); | ||
} | ||
else | ||
{ | ||
GC.SuppressFinalize(block); | ||
} | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
_disposedValue = true; | ||
#if DEBUG | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
GC.Collect(); | ||
#endif | ||
if (disposing) | ||
{ | ||
MemoryPoolSlab slab; | ||
|
@@ -146,7 +181,9 @@ protected virtual void Dispose(bool disposing) | |
} | ||
} | ||
|
||
foreach (var block in _blocks) | ||
// Discard blocks in pool | ||
MemoryPoolBlock block; | ||
while (_blocks.TryDequeue(out block)) | ||
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. Good change. Did you actually see this race in your tests? |
||
{ | ||
GC.SuppressFinalize(block); | ||
} | ||
|
@@ -155,7 +192,6 @@ protected virtual void Dispose(bool disposing) | |
|
||
// N/A: set large fields to null. | ||
|
||
_disposedValue = true; | ||
} | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -59,6 +59,8 @@ private void ExceptionThrownForZeroOrNonAscii(byte b) | |
var end = GetIterator(begin, byteRange.Length); | ||
|
||
Assert.Throws<BadHttpRequestException>(() => begin.GetAsciiString(end)); | ||
|
||
pool.Return(mem); | ||
} | ||
} | ||
} | ||
|
@@ -150,7 +152,10 @@ private void LargeAllocationProducesCorrectResults() | |
{ | ||
var returnBlock = block; | ||
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. Wouldn't it be simpler to keep this as is and remove
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. probably :) |
||
block = block.Next; | ||
pool.Return(returnBlock); | ||
if (returnBlock != mem0 && returnBlock != mem1) | ||
{ | ||
pool.Return(returnBlock); | ||
} | ||
} | ||
|
||
pool.Return(mem0); | ||
|
This file contains hidden or 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
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.
Is it the switching
Environment.StackTrace
to this that made the tests faster?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.
Another one of my super nits :) Put the first arg in its own line, and indent all args with 4 spaces only.
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.
Environment.StackTrace
caused the large response test to crash, which then caused a block to finalize. On the one hand it crashed; on the other it finalized a block - so may still be a circumstance...