dotnet/runtime

Consider using process snapshotting APIs for System.Diagnostics.Process

Open

#99.906 aperta il 18 mar 2024

Vedi su GitHub
 (13 commenti) (6 reazioni) (0 assegnatari)C# (5445 fork)batch import
area-System.Diagnostics.Processhelp wantedos-windowstenet-performance

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

We've been using Process.GetCurrentProcess() to retrieve information about the current process, such as the total number of threads. We've seen evidence that, on Windows, calling process.Threads could take tens of milliseconds for some customers, and even on my machine it consistently takes more than 3 milliseconds. Under the hood, it relies on NtQuerySystemInformation which actually captures information for all the processes on the machine, and then cherry-picks the information for the current process. Windows 8.1 has introduced process snapshotting APIs which can be used to retrieve this kind of information in a much cheaper way.

I've done a quick prototype where I compare the speed of NtQuerySystemInformation vs PssCaptureSnapshot/PssWalkSnapshot and the results are very encouraging:

Method Mean Error StdDev
PssWalkSnapshot 102.19 us 3.360 us 9.747 us
NtQuerySystemInformation 3,100.48 us 60.486 us 105.937 us

My prototype only retrieves the id of each thread of the process, but it looks like all the information required to populate the .NET ProcessThread class is in there.

Would it be worth pursuing further?

Guida contributor