How can I try CPU usage of a process without waiting 1 second

I'm trying to try using the cpu of all running processes, but all the solutions I've found need to sleep 1 second, which is a problem because I want to try it as quickly as possible to update the remote service. I have also tried using "PercentProcessorTime" using the Win32_PerfFormattedData_PerfProc_Process WMI query. but it returns wrong values ​​(is it on the processor?)

+3


source to share


1 answer


This is not possible, waiting long enough to receive the next sample is important . The processor operates in only one of two states. It either executes the code as quickly as possible. Or, it is completely stopped by an HLT instruction that occurs when the operating system's thread scheduler cannot find any work to do. 64 times per second, a clock interrupt wakes it up from this stop state and the scheduler goes to see if anything is ready to run.

The perf counter tells you how often the processor is running at full level and how often it has been stopped since the last time the counter was polled. If you don't wait long enough, the accuracy of the number starts to suffer. Until you reach 0 or 100% if you wait less than 16 milliseconds. Software version of the Nyquist-Shannon sampling theorem.



Waiting 1 second between samples is a pattern, this is what you see in Perfmon.exe and Taskmgr.exe

+3


source







All Articles