Consider using process snapshotting APIs for System.Diagnostics.Process
#99.906 aberto em 18 de mar. de 2024
Métricas do repositório
- Stars
- (17.886 stars)
- Métricas de merge de PR
- (Mesclagem média 12d 11h) (661 fundiu PRs em 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?