quick and dirty CPU usage counter
-
Monday, August 06, 2012 4:31 AMI'd like to track how much CPU power (ie. cycles) my (multi-threaded) program is consuming over some period of time. I know this information is available without too much overhead because the windows task manager displays it (in the form of a percentage), so how can I get access to that information? I'm not interested in profiling, because I want the compiled release build to display this information.
All Replies
-
Monday, August 06, 2012 5:10 AM
I'd say the Performance counters are the right thing for you. You are basically able to get the same information the Taskmanager offers you. Check out the Pdh* API functions using the PDH.DLL.
Here a few more infos / examples:
http://www.drdobbs.com/windows/win32-performance-measurement-options/184416651
http://www.codeproject.com/Articles/377/CPdh-v1-03-NT-Performance-Info
http://www.codeproject.com/Articles/58240/Performance-Counters-Enumerator
- Marked As Answer by Damon ZhengMicrosoft Contingent Staff, Moderator Sunday, August 12, 2012 7:12 AM
-
Monday, August 06, 2012 1:36 PM
Try
LARGE_INTEGER Start; LARGE_INTEGER Stop; LARGE_INTEGER Freq; void PerformanceStart() { QueryPerformanceFrequency(&Freq); // Set a processor affinity mask for the specified thread in multicore systems DWORD_PTR curThreadMask=SetThreadAffinityMask(GetCurrentThread(),0x1); QueryPerformanceCounter(&Start); // Restore a processor affinity mask SetThreadAffinityMask(GetCurrentThread(),curThreadMask); } double PerformanceStop() { DWORD_PTR curThreadMask=SetThreadAffinityMask(::GetCurrentThread(),0x1); QueryPerformanceCounter(&Stop); SetThreadAffinityMask(GetCurrentThread(),curThreadMask); double Time=static_cast<double>(Stop.QuadPart-Start.QuadPart); double Freqency=(double)m_liFreq.QuadPart/1000000.0; double Elapsed = Time/Freqency; // in microseconds return Elapsed; }
Thanks, Renjith V R
- Edited by Renjith V RamachandranMicrosoft Community Contributor Monday, August 06, 2012 1:39 PM
-
Monday, August 06, 2012 2:56 PMIf you are looking to get a CPU percentage of a particular thread (or set of threads), you probably want GetThreadTimes.

