Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Profiling When Running in Docker Container

I am using asp.net core 2.2 and Visual Studio 2019. The containers my application are running on are Debian (one of the official aspnet:2.2 docker images)

So my situation is this. I have an application that consists of 4 microservices running in docker containers and I'm seeing very high cpu usage on the containers nodes when this is under load. What I would like to do is profile the executing code to get an idea where this resource use is happening.

As a starting point I thought I would simply get some profiling running on my local development environment, just to get an idea of what the execution looks like in general. Although in production this runs in Kubernetes I do have a development environment that uses docker compose and I find the Visual Studio Docker tools to be fairly good.

I was hoping to use some of the visual studio profiling tools. I was able to install VSDBG on one of my locally running containers and connect to it with VS BUT in the diagnostic pane I see "the diagnostic tools window does not support the current debugging configuration". I've also tried just running the project from VS using docker compose but I see the same message when I hit a breakpoint. I'm not finding much out there about how to do this.

I also tried getting profiling going using perfcollect but after I generated the trace and opened it with perfviewer I was getting a parsing error when trying to view the cpu stacks . Still not sure what's going on there. I did find an old closed issue on their github describing what I am seeing but there was a fairly recent comment from someone saying they were seeing it with the latest version so maybe it's a regression.

So.. after all this .. my question is this. Are either of the above approaches viable? Is there a better way to achieve this? I'm interested in any way someone has had success viewing some code profiling of a .net core 2.2 application running on a linux docker container. All I really want to do is be able to see where in my code the execution time is going and what resources are being consumed. As I've mentioned I'm not finding much out there when I Google for this and I seem to keep hitting walls. If anyone had any advice or direction on a approach here I would really appreciate it. Thanks much!

like image 783
TheMethod Avatar asked Dec 16 '19 20:12

TheMethod


1 Answers

Are you open to upgrading to .Net Core 3.0 (.Net Core 2.2 is going out of support in a few days: 12/23/2019)

If you're open to that you can take advantage of the new tool dotnet-trace which supports running in a linux container and can be used with the tools in Visual Studio.

Here are the steps I used to add it to my project:

  1. Change your base image to use the sdk image (needed to install the tool).
  2. Add installing the tool to the image:
RUN dotnet tool install --global dotnet-trace
ENV PATH $PATH:/root/.dotnet/tools

Alternatively if you don’t want to add it in your image you can run the following commands in a running container (as long as it as based on the SDK image):

dotnet tool install --global dotnet-trace
export PATH="$PATH:/root/.dotnet/tools"
  1. Start the project without debugging (Ctrl+F5)

  2. Use the Containers Tool Window to open a terminal window

Run the command:

dotnet-trace collect --process-id $(pidof dotnet) --providers Microsoft-DotNETCore-SampleProfiler

When you are done collecting press enter or Ctrl+C to end the collection

This will crate a file called “trace.nettrace”

By default that /app folder that file would be created in is volume mapped to your project folder. You can open the file from there in VS.

like image 55
Nathan Carlson - MSFT Avatar answered Oct 22 '22 10:10

Nathan Carlson - MSFT