How to get the cpu usage per thread on windows (win32) How to get the cpu usage per thread on windows (win32) multithreading multithreading

How to get the cpu usage per thread on windows (win32)


You must use these functions to get the cpu usage per thread and process.

GetThreadTimes (Retrieves timing information for the specified thread.)

GetProcessTimes (Retrieves timing information for the specified process.)

GetSystemTime (Retrieves the current system date and time. The system time is expressed in Coordinated Universal Time UTC)

Here a excellent article from Dr. Dobb's Win32 Performance Measurement Options

Bye.


The data you are refering to is available using specific WMI calls. You can query Win32_Process to get all sorts of process specific information, and query Win32_PerfFormattedData_PerfProc_Process to get the thread count, and given a handle to a thread (what I believe your looking for) you can query Win32_PerfRawData_PerfProc_Thread to get the percent of processor time used.

There is a library available for Delphi which provides wrappers for most of the WMI queries, however it will take some experimentation to get the exact query your looking for. The query syntax is very sql like, for example on my system to return the percent of processor time for threadid 8, for process id 4 is:

SELECT PercentProcessorTime FROM Win32_PerfRawData_PerfProc_Thread   WHERE IdProcess=4 and IdThread=8

Most of the programs which present statistical information about running processes now use WMI to query for this information.


Is important to know that in certain situations, the execution time of a thread may be worthless.The execution times of each thread are updated every 15 milliseconds usually for multi-core systems, so if a thread completes its task before this time, the runtime will be reset.More details can be obtained on the link: GetThreadTimes function and I was surprised by the result!
andWhy GetThreadTimes is wrong