Consider using process snapshotting APIs for System.Diagnostics.Process
#99,906 建立於 2024年3月18日
倉庫指標
- Star
- (17,886 star)
- PR 合併指標
- (平均合併 12天 11小時) (30 天內合併 661 個 PR)
描述
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?