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

Fixing the OOM in scripting #75372

Merged
merged 13 commits into from
Oct 10, 2024
Merged

Fixing the OOM in scripting #75372

merged 13 commits into from
Oct 10, 2024

Conversation

jaredpar
Copy link
Member

@jaredpar jaredpar commented Oct 3, 2024

Dug into the Scripting OOM tests more and figured out what was going on. The scripting tests predominantly create PortableExecutableReference instances using Stream as the source. Under the hood this creates a lot of native allocations in the form of memory mapped files and AllocHGlobal memory. As PortableExecutableReference is not disposable, these allocations are cleaned up passively in finalizers.

This means that our tests rely on the finalizer running fast enough that it frees up enough native memory for the next scripting test that allocates native memory to succeed. When the scripting tests are run in isolation or in an x64 process this works out just fine. But when we run the tests on x86 and in combination with other test assemblies this can break down and lead to OOM situations.

This fix approaches the problem from three directions:

  1. Scripting had an existing mechanism for intercepting files being converted to PortableExecutableReference. This change expands on that to make it more thoroughly plumbed through the scripting code base and to have a single place, ScriptOptions, where the intercept can be hooked up.
  2. Creating a ScriptTestBase type which sets up the intercepting of file loads and then actively disposing the resulting MetadataImageReference on Dispose. This means the native memory is actively freed at the end of each test. It also provides a ScriptOptions value that derived tests can leverage.
  3. Plumbing through ScriptOptions to the individual tests.

After doing this I ran the script tests under PerfView in a number of configurations and verified that the memory was being actively freed and the heap size of the process was reduced as expected.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 3, 2024
@jaredpar jaredpar marked this pull request as ready for review October 4, 2024 15:26
@jaredpar jaredpar requested review from a team as code owners October 4, 2024 15:26
@jaredpar
Copy link
Member Author

jaredpar commented Oct 4, 2024

@dotnet/roslyn-compiler PTAL

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Can we use the BannedAPIAnalyzer to ban the non-options-taking versions of these methods in our test assemblies so we don't end up here again in 5 years?

@jaredpar
Copy link
Member Author

jaredpar commented Oct 4, 2024

Can we use the BannedAPIAnalyzer to ban the non-options-taking versions of these methods in our test assemblies so we don't end up here again in 5 years?

I don't think we can. I definitely considered this but the problem is that every overload has an options parameter, it's just that in some cases it's a default value. Don't think the banned API analyzer can deal with that.

@333fred
Copy link
Member

333fred commented Oct 4, 2024

Gotcha. This doesn't feel worth an analyzer to me, but we should document this somewhere; maybe a README in the scripting test project? Or a note in the base class?

@jaredpar
Copy link
Member Author

jaredpar commented Oct 8, 2024

@dotnet/roslyn-compiler PTAL
@333fred doc added.

src/Scripting/CoreTestUtilities/README.md Outdated Show resolved Hide resolved
## Native Memory Pressure

The scripting engine in Roslyn makes heavy use of creating `MetadatImageReference` over a `Stream`. That is the way in which runtime assembly references are added. This form of `MetadataReference` results in a lot of native allocations as opposed to using an `ImmutableArray<byte>` which has none. By default these native allocations are collected by the finalizer as `MetadadataImageReference` is not disposable.

Copy link
Member

@jjonescz jjonescz Oct 9, 2024

Choose a reason for hiding this comment

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

Given that "MetadataImageReference over Stream results in a lot of native allocations as opposed to using an ImmutableArray<byte> which has none", have we considered fixing this by using ImmutableArray<byte> instead of relying on MetadataImageReference over Stream in the scripting engine?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe the reason scripting uses Stream is that in the normal use case the belief is that it should result in less overall allocations. The compiler lazily loads data from the PE file so in theory you could load a lot of assemblies with only targeted allocations. As opposed to the ImmutableArray<byte> case where you have to eagerly load them all into memory.

The unit tests are just taking the downsides of this approach to the extreme.

@jaredpar
Copy link
Member Author

jaredpar commented Oct 9, 2024

@dotnet/roslyn-ide can I get a review on the scripting side? This will resolve the OOM issues we've been seeing in CI

Copy link
Member

@dibarbet dibarbet left a comment

Choose a reason for hiding this comment

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

seems reasonable

src/Scripting/Core/Script.cs Outdated Show resolved Hide resolved
@jaredpar jaredpar enabled auto-merge (squash) October 10, 2024 20:12
@jaredpar jaredpar merged commit ee185b2 into dotnet:main Oct 10, 2024
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 10, 2024
@jaredpar jaredpar deleted the oom branch October 11, 2024 15:24
@akhera99 akhera99 modified the milestones: Next, 17.13 P1 Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Interactive 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.

5 participants