Skip to content

Profiling

Rolf Bjarne Kvinge edited this page Jun 21, 2022 · 6 revisions

Profiling in .NET

Mobile apps

Initial configuration

We need the dotnet-dsrouter and dotnet-trace tools, so let's install them:

$ dotnet tool install --global dotnet-dsrouter
$ dotnet tool install --global dotnet-trace

Note This pull request is required for profiling on device to work: https://github.com/dotnet/diagnostics/pull/3134

Simulator

The first step is to launch the tool that provides a connection between the app and the .NET tracing tools:

$ dotnet-dsrouter client-server -ipcc ~/my-sim-port -tcps 127.0.0.1:9000

Launch the app and make it suspend upon launch (waiting for the .NET tooling to connect):

$ mlaunch --launchsim bin/Debug/net*/*/*.app --device :v2:runtime=com.apple.CoreSimulator.SimRuntime.iOS-15-4,devicetype=com.CoreSimulator.SimDeviceType.iPhone-11 --wait-for-exit --stdout=$(tty) --stderr=$(tty) --argument --connection-mode --argument none '--setenv:DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend'

At this point it's necessary to wait until the following line shows up in the terminal:

The runtime has been configured to pause during startup and is awaiting a Diagnostics IPC ResumeStartup command from a Diagnostic Port

Once that's printed, go ahead and start profiling:

$ dotnet-trace collect --diagnostic-port ~/my-sim-port --format speedscope

Device

The first step is to connect an iOS device to the Mac using a USB cable.

Note This pull request is required for profiling on device to work: https://github.com/dotnet/diagnostics/pull/3134

The rest of the process is very similar to the process for the simulator:

Launch the tool that bridges the app and the .NET tracing tools:

$ dotnet-dsrouter client-server -ipcc ~/my-dev-port -tcps 127.0.0.1:9001 --forward-port iOS

Compared to the simulator, this:

  • Adds --forward-port iOS, this makes the connection between the app and the tracing tools work through the USB cable.
  • Changes the local ports, both client (~/my-dev-port vs ~/my-sim-port) and server (:9001 vs :9000) - because it's easier to debug any problems when using different ports.

Install & launch the app and make it suspend upon launch (waiting for the .NET tooling to connect):

$ mlaunch --installdev bin/Debug/net*/*/*.app --devname ... 
$ mlaunch --launchdev bin/Debug/net*/*/*.app --devname ... --wait-for-exit --argument --connection-mode --argument none '--setenv:DOTNET_DiagnosticPorts=127.0.0.1:9001,suspend,listen'

At this point it's necessary to wait until the following line shows up in the terminal:

The runtime has been configured to pause during startup and is awaiting a Diagnostics IPC ResumeStartup command from a Diagnostic Port

Once that's printed, go ahead and start profiling:

$ dotnet-trace collect --diagnostic-port ~/my-dev-port,connect --format speedscope

References

Clone this wiki locally