Skip to content

dotnet-gcdump - #581

Merged
josalem merged 3 commits into
dotnet:masterfrom
josalem:dev/josalem/gcdump-tool
Nov 2, 2019
Merged

josalem merged 3 commits into
dotnet:masterfrom
josalem:dev/josalem/gcdump-tool

Conversation

@josalem

@josalem josalem commented Oct 23, 2019

Copy link
Copy Markdown
Contributor

Adds the dotnet-gcdump tool. The spec for this tool is in another PR (#512).

This PR includes some code that has been copied from Microsoft/PerfView. The reasoning is in the README that is included in the directory they were copied to. To sum it up, the refactoring required to expose these classes via TraceEvent was turning into more of a hassle than it was worth. There were diamond dependencies and target framework conflicts that would have been very disruptive to TraceEvent.

This tool will only work with .NET Core 3.1-preview2 or newer (.NET 5).

If this tool is used with runtimes older than that, then only the first gcdump will be viable. All subsequent dumps will be missing type information due to an issue in the runtime. Additionally, older runtimes will be missing COM and static types in the dump.

The output files can be opened in VS or PerfView. Opening multiple gcdumps in VS allows you to compare the heap stats over time.

CC - @tommcdon @caslan @shirhatti

@josalem josalem added this to the 3.1 milestone Oct 23, 2019
@josalem josalem self-assigned this Oct 23, 2019
@josalem

josalem commented Oct 23, 2019

Copy link
Copy Markdown
Contributor Author

CC @davidfowl

@tommcdon

Copy link
Copy Markdown
Member

cc @brianrob

try
{
output = string.IsNullOrEmpty(output) ?
$"{DateTime.Now.ToString(@"yyyyMMdd\_hhmmss")}_{processId}.gcdump" :

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tell me we're going to fix dotnet-trace as well 😄

@sywhang sywhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

left some more comments. Is there any plan to add tests? :D

@josalem

josalem commented Oct 25, 2019

Copy link
Copy Markdown
Contributor Author

left some more comments. Is there any plan to add tests? :D

I'm planning on writing some manual testing scripts for now, and eventually automating them via #571.

The manual testing will involve creating 2 gcdumps of an app that has a known quantity of specific object types in various configurations (local, static, pinned, etc.), then validating that the 2 dumps show the same counts and that the counts match what's expected. This is how I've been locally testing this tool.

@sywhang

sywhang commented Oct 25, 2019

Copy link
Copy Markdown
Contributor

Sounds good. And thanks for letting me know about your plans to use #571 - I'll keep that in mind when implementing it.

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yey! Looks good apart from some misc comments.

John Salem added 2 commits October 25, 2019 14:55
* NOTE: use this commit as baseline for file copied from Microsoft/PerfView
@josalem
josalem force-pushed the dev/josalem/gcdump-tool branch from e857d0a to 333b89d Compare October 25, 2019 22:05
@markvincze

markvincze commented Oct 28, 2019 •

Copy link
Copy Markdown

I've been using this to investigate dotnet/aspnetcore#13299, and just wanted to mention that once when doing dotnet-gcdump collect, it failed with this exception:

$ dotnet-gcdump collect --process-id 1
Writing gcdump to '/app/20191028_114158_1.gcdump'...
[ERROR] System.ApplicationException: RootIndex not set.
   at Graphs.Graph.AllowReading() in /app/diagnostics/src/Tools/dotnet-gcdump/DotNetHeapDump/Graph.cs:line 236
   at Microsoft.Diagnostics.Tools.GCDump.CollectCommandHandler.<>c__DisplayClass1_0.<Collect>b__0() in /app/diagnostics/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs:line 58
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Diagnostics.Tools.GCDump.CollectCommandHandler.Collect(CancellationToken ct, IConsole console, Int32 processId, String output, Boolean verbose) in /app/diagnostics/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs:line 63

Then I retried, and it worked.

Update: I have now an instance on which I'm consistently getting this exception even if I wait and try again.

@josalem

josalem commented Oct 28, 2019 •

Copy link
Copy Markdown
Contributor Author

@markvincze could you try running the tool with the -v flag and saving the output to a file? That should spit out the log from the tool leading up to that exception. Is it possible to share the repro or a simplified version of it? Lastly, there were some code changes since the commit you had built in the other thread. Have you tried pulling the tip of this PR and using that version?

@markvincze

Copy link
Copy Markdown

@josalem it gives the following output.

$ dotnet-gcdump collect --process-id 1 -v
Writing gcdump to '/app/20191028_090928_1.gcdump'...
  0.0s: Creating type table flushing task
  6.7s: Flushing the type table
 26.8s: Done flushing the type table
 27.3s: Requesting a .NET Heap Dump
 41.0s: Assume no .NET Heap
 41.0s: Shutting down EventPipe session
05:n1s: [Error] Exception during gcdump: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Diagnostics.Tools.GCDump.EventPipeDotNetHeapDumper.DumpFromEventPipe(CancellationToken ct, Int32 processID, MemoryGraph memoryGraph, TextWriter log, DotNetHeapInfo dotNetInfo) in /app/diagnostics/src/Tools/dotnet-gcdump/DotNetHeapDump/EventPipeDotNetHeapDumper.cs:line 169
[ 53.4s: Done Dumping .NET heap success=False]
[ERROR] System.ApplicationException: RootIndex not set.
   at Graphs.Graph.AllowReading() in /app/diagnostics/src/Tools/dotnet-gcdump/DotNetHeapDump/Graph.cs:line 236
   at Microsoft.Diagnostics.Tools.GCDump.CollectCommandHandler.<>c__DisplayClass1_0.<Collect>b__0() in /app/diagnostics/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs:line 58
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Diagnostics.Tools.GCDump.CollectCommandHandler.Collect(CancellationToken ct, IConsole console, Int32 processId, String output, Boolean verbose) in /app/diagnostics/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs:line 63

@josalem

josalem commented Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Thanks, that is very helpful!

@josalem

josalem commented Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Have all of your gcdumps been taking 40-ish seconds?

@markvincze

Copy link
Copy Markdown

@josalem not really, these failing ones only take 1-2 seconds, but I think the earlier successful ones were also faster than 40s.

@markvincze

Copy link
Copy Markdown

@josalem I realized I didn't answer the second part of your question.
Yep, I've been using the last commit at josalem:dev/josalem/gcdump-tool.

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, modulo a few more comments

}
else
{
throw new ApplicationException("EventPipe session for gcDumpSession failed");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For practical purposes this is probably fine so I'm not going to complain if you leave it as-is, but if we needed to be rigorous there are problems lurking here:
a) If cancellation happens quickly enough gcDumpSession will be null by design and that shouldn't be an error.
b) gcDumpSession is being modified on one thread and read on another without any locks. In a race condition thread 1 could write a non-null value but here on thread 2 a null value could still be observed. This would lead to the exception being thrown instead of the correct EndSession() being called.
c) This exception appears to mask whatever the real issue was that caused gcDumpSession to be null. Ideally the code would wait on readerTask to ensure that any exception thrown from readerTask is what propagates here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I refactored this a bit to remove some of the multi-threading and hiding of errors. I'll ping again for another look. The fixes got rid of a race that I found while refactoring.

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@josalem

josalem commented Nov 2, 2019

Copy link
Copy Markdown
Contributor Author

Failures appear to be due to Mac failing to build SOS native dependencies and unrelated to this tool.

CC @mikem8361

* Switch to a static function on Stopwatch to avoid multi-threading
concerns with Stopwatch.Elapsed

* Change tool name when writing memory graph

* Add explicit null check on gcDumpSession to catch NRE in the event
EventPipe failed to connect.

* Add comment in README about future of code shared with PerfView

* Refactor to remove wrapping task for type flush session
* make EventPipeSession disposable to ensure stream is closed in case of
exception in the reader
* add using statements around source.Process calls
* add additional logging to verbose log

* Add additional logging
* add timeout option

* Remove unnecessary using statements

* Update src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs

Co-Authored-By: Noah Falk <[email protected]>

* Update src/Tools/dotnet-gcdump/DotNetHeapDump/README.md

Co-Authored-By: Noah Falk <[email protected]>

* Fix weird unintended docs change

* Refactor to reduce multi-threading
* use cancellation token to cancel tasks on ctrl-c
* swallow TaskCancellationExceptions
* use volatile members for bools that are used between threads

* Check dump status on return to amke sure we don't throw additional
errors if event collection fails

* fix last whitespace issue
@josalem
josalem force-pushed the dev/josalem/gcdump-tool branch from 6cabe92 to d415599 Compare November 2, 2019 01:12
@josalem

josalem commented Nov 2, 2019

Copy link
Copy Markdown
Contributor Author

Manually squashed changes in order to keep a separate commit for code copied from Microsoft/PerfView.

@josalem
josalem merged commit 13fe841 into dotnet:master Nov 2, 2019
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants