dotnet/runtime

Consider using process snapshotting APIs for System.Diagnostics.Process

Open

#99,906 opened on Mar 18, 2024

View on GitHub
 (13 comments) (6 reactions) (0 assignees)C# (5,445 forks)batch import
area-System.Diagnostics.Processhelp wantedos-windowstenet-performance

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

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?

Contributor guide