I have a .NET CORE 3.1 Console application running on a Ubuntu20 x64 server, and randomly experiencing High Cpu(100% for 4 cores) cases.
I’m following diagnostics to start a diag during the peak time for my app like:
dotnet-trace collect -p 1039 --providers Microsoft-DotNETCore-SampleProfiler
from the resulted .nettrace
file opened in Visual Studio, I can see the Funcions list with CPU Time for each single function.
But I understand the CPU time here is actually the wall time
that just means the time of a function call stayed in a thread stack, and no matter it consumes real CPU caculation resource or not.
The hotest spot my this .nettrace
now is pointing to these lines of code(pseudo code):
while(true)
{
Thread.Sleep(1000);//<---------hottest spot
socket.Send(bytes);
}
and
while(true)
{
ManualResetEvent.WaitOne();//<---------hottest spot
httpClient.Post(data);
}
Obviously above 2 hottest spot will not consume real CPU resource but just idle waitting, so any way to trace the functions that used the real cpu usage, just like the JetBrains dotTrace provided:
2
Answers
You might want to use external tools like
top
. This could help to identify the process consuming the CPU percentage.If your profiler identifies
Thread.Sleep()
as hottest spot, chances are that your application is waiting for some external process outside the scope of the profiler.I would suggest refactoring this code to use async and use ‘await Task.Wait(xxx)’ instead of doing this on the Thread level.
I’m having this suggestion based on partially similar problem which has been described here
Why Thread.Sleep() is so CPU intensive?