Skip to content
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

Fix the OOM error in scripting tests #75163

Merged
merged 3 commits into from
Sep 19, 2024
Merged

Fix the OOM error in scripting tests #75163

merged 3 commits into from
Sep 19, 2024

Conversation

jaredpar
Copy link
Member

Was able to reproduce the Scripting OutOfMemoryException issues on my local machine. After looking at the test run in PerfView it was clear that we were allocating a lot more memory than necessary and that was, likey, the cause of the OOM that we saw. Given that it was running under x86 this seems fairly likely.

The allocations were coming more from the ResultsProvider and IOperation test assemlbies. But given that scripting is small our scheduling algorithm often puts scripting into a slice that includes both these test assemblies. Their allocations are what is pushing us to OOM. That is also likely why we couldn't reproduce this by just running the scripting tests locally.

This PR attempts to address the cases that I saw. There are a few cases where I moved eager operations to lazy. The bigger change though is moving a to using an interpolated string handler for RoslynDebug.Assert. There are a number of cases where had roughly the following code:

Debug.Assert(condition, $"... {syntaxNode} ...");

On net472 that is going to eagerly allocate the message string which realizes the entire syntax node. One assert alone was contributing 9GB of allocations all by itself. Did a simple regex search for Debug.Assert that had an interpolation and moved them in mass to RoslynDebug.Assert. Now the strings are only realized if the assert fails. Will discuss with the team if we want to think about an analyzer here

PS: if the scripting tests OOM on this PR I may take tomorrow off.

Was able to reproduce the Scripting `OutOfMemoryException` issues on my
local machine. After looking at the test run in PerfView it was clear
that we were allocating a lot more memory than necessary and that was,
likey, the cause of the OOM that we saw. Given that it was running under
x86 this seems fairly likely.

The allocations were coming more from the ResultsProvider and IOperation
test assemlbies. But given that scripting is small our scheduling
algorithm often puts scripting into a slice that includes both these
test assemblies. Their allocations are what is pushing us to OOM. That
is also likely why we couldn't reproduce this by just running the
scripting tests locally.

This PR attempts to address the cases that I saw. There are a few cases
where I moved eager operations to lazy. The bigger change though is
moving a to using an interpolated string handler for
`RoslynDebug.Assert`. There are a number of cases where had roughly the
following code:

```csharp
Debug.Assert(condition, $"... {syntaxNode} ...");
```

On net472 that is going to eagerly allocate the message string which
realizes the entire syntax node. One assert alone was contributing 9GB
of allocations all by itself. Did a simple regex search for
`Debug.Assert` that had an interpolation and moved them in mass to
`RoslynDebug.Assert`. Now the strings are only realized if the assert
fails. Will discuss with the team if we want to think about an
analyzer here

PS: if the scripting tests OOM on this PR I may take tomorrow off.
@jaredpar jaredpar requested review from a team as code owners September 19, 2024 04:45
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 19, 2024
@@ -252,7 +252,7 @@ private static void AssertTrivialConversion(ConversionKind kind)
break;
}

Debug.Assert(isTrivial, "this conversion needs additional data: " + kind);
RoslynDebug.Assert(isTrivial, $"this conversion needs additional data: {kind}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To emphasize how much these changes matter: this Debug.Assert alone was causing 50MB of allocations in the slice I was debugging.

@@ -44,7 +44,9 @@ internal DkmClrType(DkmClrModuleInstance module, DkmClrAppDomain appDomain, Type
ModuleInstance = module;
AppDomain = appDomain;
_lmrType = lmrType;
_evalAttributes = GetEvalAttributes(lmrType);
_lazyEvalAttributes = new System.Lazy<ReadOnlyCollection<DkmClrEvalAttribute>>(
() => GetEvalAttributes(lmrType),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetEvalAttribute realizes a huge number of string and arrays inside the reflection libraries. About 150MB of savings from this change.

Comment on lines +69 to +71
RoslynDebug.Assert(
!argument.ContainsKey(operation.Syntax),
$"Duplicate operation node for {operation.Syntax}. Existing node is {(argument.TryGetValue(operation.Syntax, out var original) ? (OperationKind?)original.Kind : null)}, new node is {operation.Kind}.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About 9GB of allocations mostly due to the {operation.Syntax} portion.

@tmat
Copy link
Member

tmat commented Sep 19, 2024

Nice!


/// <inheritdoc cref="Debug.Assert(bool, string)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref AssertInterpolatedStringHandler message)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a static method so can't polyfill until we get extension everything 😦

}

internal string ToStringAndClear() => _builder is null
? ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: seems like this should never be hit. Consider just throwing.


/// <inheritdoc cref="Debug.Assert(bool, string)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b, string message)
=> Debug.Assert(b, message);
public static void Assert([DoesNotReturnIf(false)] bool condition, string message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this overload be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@333fred as I'm not sure about the implications here of doing this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, a regular string won't work with the handler.

@jaredpar jaredpar enabled auto-merge (squash) September 19, 2024 15:16
}
}

internal string ToStringAndClear() => _builder!.ToString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_builder!.ToString();

Should this call _builder.Clear() as well?

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (commit 3)

@jaredpar
Copy link
Member Author

image

Sigh .... Still some more work to do here.

@jaredpar jaredpar merged commit 2b40221 into dotnet:main Sep 19, 2024
26 of 28 checks passed
@jaredpar jaredpar deleted the oom branch September 19, 2024 17:54
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Sep 19, 2024
333fred added a commit to dotnet/roslyn-analyzers that referenced this pull request Sep 26, 2024
* Add an analyzer for Debug.Assert

As @jaredpar found in dotnet/roslyn#75163, interpolated strings in `Debug.Assert` can consume a surprising amount of memory. On modern .NET, this is fine; `Debug.Assert` has an interpolated string handler that will avoid the allocations if the assert isn't triggered. However, on our framework tests, this can be very bad and OOM our tests. So, this analyzer looks for cases where interpolated strings are passed to `Debug.Assert`, and recommends moving over to `RoslynDebug.Assert` instead, which is an interpolated string handler on all platforms. Note that I only did C# support, as there's no equivalent handler API for VB.

* Make sure RoslynDebug is available in one test.

* Trailing whitespace

* Run pack.

* Remove VB

* Pack again

* Use batch fixall provider

* Avoid passing empty string as command line argument

See microsoft/dotnet#1062

* Don't warn for constant strings.

* Apply suggestions from code review

Co-authored-by: Jared Parsons <jared@paranoidcoding.org>

---------

Co-authored-by: Sam Harwell <Sam.Harwell@microsoft.com>
Co-authored-by: Jared Parsons <jared@paranoidcoding.org>
@akhera99 akhera99 modified the milestones: Next, 17.12 P3 Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants