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
Enabling a CPU profiler on Windows automatically enables ETW tracing and slows the app down by over 100x #81165
Comments
Tagging subscribers to this area: @tommcdon Issue DetailsDescriptionWhen using Visual Studio 2022 on Windows to profile CPU Usage of a dotnet application, the application can run over 100 times slower and produce a CPU profile that is heavily dominated by instrumentation code. Reproduction Steps
Expected behaviorGiven that without a CPU profiler it takes the application around 8ms to do the work, I expect that with the CPU profiler it won't take more than 10ms. I also expect that the CPU profile presented by Visual Studio closely resembles the actual CPU profile of the application when it runs without being profiled. Actual behaviorThe actual behavior is described in Reproduction Steps. Regression?No response Known WorkaroundsI'd love to know a workaround if there is one. My goal is to get a CPU profile of my application. The application reads many small messages from WebSocket, which implies a lot of I've tried ticking Instrumentation instead of CPU Usage. This incurs about 4x slowdown (much better than 100x but still fairly high). More importantly, it measures wall time instead of CPU time, so the profile is dominated by various sleep and wait functions. I've tried disabling EWT tracing by setting <?xml version="1.0"?>
<configuration>
<runtime>
<etwEnable enabled="false" />
</runtime>
</configuration> This also made no difference. I'm open to using a different tool (instead of Visual Studio) to generate/analyze a CPU profile. Any suggestions would be helpful. Configuration
Other informationI'm not sure if I'm filing this issue against the correct component. My apologies if this is a wrong channel.
|
I came up with a workaround. I added the following statement at the very beginning of my application: typeof(System.Diagnostics.Tracing.EventSource)
.GetField("m_eventSourceEnabled", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(Type.GetType("System.Threading.Tasks.TplEventSource")
.GetField("Log", BindingFlags.Static | BindingFlags.Public)
.GetValue(null),
false); This sets This won't help when attaching a CPU profiler to a running process. This can be worked around by starting a thread within the application to periodically set |
@brianrob I thought we disabled TPL for all scenarios but it looks like we still have them enabled in the CPU Usage tool in the Profiler scenario, they are just disabled during the debugging scenario. I'll follow up with Evan, I think we should turn them off in the profiler as well (I thought we had). @romkatv we have a registry key that you can set to disable these events. Running the following from a command line will disable the TPL events in the profiler and should workaround your issue without the need for the code change: |
TPL logging is enabled when debugging (F5). My workaround incidentally solved the long-standing performance issue I had with debugging my project.
Thanks! Is there documentation for this switch somewhere? If not, a reference to the code would suffice. |
Turns out the debugger is getting the TPL events to stitch async tasks together for the Parallel Stacks view. I'll follow up with them and see if there is anything we can do.
Nope, this is really just an internal switch that we use for our own testing and lives inside the Visual Studio code base. I'm going to see if we can just expose this as a setting in the CPU Usage settings (gear icon) so users can optionally enable/disable it. |
Marking as future since this is not tracked by work in the runtime, but rather VS work. |
Description
When using Visual Studio 2022 on Windows to profile CPU Usage of a dotnet application, the application can run over 100 times slower and produce a CPU profile that is heavily dominated by instrumentation code.
Reproduction Steps
CpuProfilerTest.csproj
with the following content:CpuProfilerTest.cs
with the following content:CpuProfilerTest.csproj
in Microsoft Visual Studio Community 2022 (64-bit) 17.4.4 on Windows 10.Note that 93% of CPU time is spent in
System.Diagnostics.Tracing.EventSource.WriteEventWithRelatedActivityIdCore
.Expected behavior
Given that without a CPU profiler it takes the application around 8ms to do the work, I expect that with the CPU profiler it won't take more than 10ms.
I also expect that the CPU profile presented by Visual Studio closely resembles the actual CPU profile of the application when it runs without being profiled.
Actual behavior
The actual behavior is described in Reproduction Steps.
Regression?
No response
Known Workarounds
I'd love to know a workaround if there is one.
My goal is to get a CPU profile of my application. The application reads many small messages from WebSocket, which implies a lot of
Task
objects. If I use the CPU profiler from Visual Studio, the application slows down to such a great extent that the profile is no longer relevant.I've tried ticking Instrumentation instead of CPU Usage.
This incurs about 4x slowdown (much better than 100x but still fairly high). More importantly, it measures wall time instead of CPU time, so the profile is dominated by various sleep and wait functions.
I've tried disabling ETW tracing by setting
COMPlus_ETWEnabled=0
environment variable. This made no difference. I've also tried using the followingApp.config
:This also made no difference.
I'm open to using a different tool (instead of Visual Studio) to generate/analyze a CPU profile. Any suggestions would be helpful.
Configuration
Other information
I'm not sure if I'm filing this issue against the correct component. My apologies if this is a wrong channel.
The text was updated successfully, but these errors were encountered: