October 21st, 2016

Is there anything better than GetThreadTimes for obtaining per-thread CPU usage information?

A customer was using the Get­Thread­Times function for high-resolution profiling of performance-sensitive code, but found that its accuracy is rather poor. They were hoping there would be something more along the lines of a Query­Performance­Counter that reported only CPU time consumed by a particular thread, rather than by the system in general.

Fortunately, there is. The Query­Thread­Cycle­Time function gives you the CPU cycles consumed by a particular thread. This includes time spent both in user mode and in kernel mode.

Note, however, that these values are reported directly from the CPU using mechanisms like RDTSC or the performance monitor control register. This means that the actual results are at the mercy of whatever the CPU manufacturer decides the CPU cycle counter means. Maybe they correspond to wall clock time; maybe they don’t.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback