How to measure CPU usage of a specific logical stream?

(This does not apply to the main programming questions that I usually ask!)

We have an interesting question. We have a multi-threaded non-visual server application (which starts 10-20 workflows for each process), and it uses a ton of processor. This is by design.

We cannot profile the workload of the real world of this code in the development system. The load is very high (very high). To optimize this code, we must be able to keep a log for production bits, which work tasks take up so much CPU.

In our current implementation, the System.Diagnostics.ProcessThread.TotalProcessorTime parameter is used for measurement, but in .NET v4 it does not guarantee the proximity of the .NET logical stream to ProcessThread, so we cannot be sure what exactly we are measuring.

What can we do to measure the CPU usage of each thread / each function?

We heard that an alternative lib for streaming is one way, any suggestions?

+6
source share
1 answer

The separation between ProcessThread and Thread dates from .NET 2.0. Inspired by a project within the SQL Server team that planned to implement .NET threads using fibers. This project was abandoned, there are no known CLR hosts that do not use the operating system thread (aka ProcessThread) to implement the .NET thread.

The only problem you will encounter is mapping ProcessThread to a .NET thread. This was done specifically for .NET 2.0. The only decent way is for Thread to print GetCurrentThreadId (), and then find a match for ProcessThread.Id. Once you are there, you can also output GetThreadTimes (). It also requires OpenThread () to get the handle and CloseHandle () to close it again. Visit pinvoke.net for ads.

+8
source

Source: https://habr.com/ru/post/903632/


All Articles