1

I need to retrieve total CPU usage on the fly for a feedback system to change the behavior based on whether CPU is getting throttled or not. For this, I looked into NtQuerySystemInformation sys call which provides system information at any given time but it seems like this function has been deprecated in the latest versions of Windows since MSDN page says

[NtQuerySystemInformation may be altered or unavailable in future versions of Windows. Applications should use the alternate functions listed in this topic.]

Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724509(v=vs.85).aspx

Does anyone know what OS versions this call is supported on? Win 7/8/8.1/10? Is there any other way to directly retrieve total CPU usage?

hellSigma
  • 301
  • 4
  • 13
  • Helpful reading: [How to determine CPU and memory consumption from inside a process?](http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process) Probably even a dupe, but not sure enough to lodge a close vote. – user4581301 Apr 11 '17 at 23:37

1 Answers1

1

Officaially, there are two functions are in the MSDN.

  1. GetSystemTimes(): It looks more easier but posted a long time ago. https://msdn.microsoft.com/en-us/library/windows/desktop/ms724400(v=vs.85).aspx

  2. GetSystemInfo() : I've got the sample code from https://code.msdn.microsoft.com/windowsdesktop/Use-PerformanceCounter-to-272d57a1

     DWORD GetProcessorCount() 
     {
          SYSTEM_INFO sysinfo;  
          DWORD dwNumberOfProcessors; 
          GetSystemInfo(&sysinfo); 
          dwNumberOfProcessors = sysinfo.dwNumberOfProcessors; 
          return dwNumberOfProcessors; 
     } 
     vector<PCTSTR> GetValidCounterNames() 
     { 
         vector<PCTSTR> validCounterNames; 
         DWORD dwNumberOfProcessors = GetProcessorCount(); 
         DWORD index; 
         vector<PCTSTR> vszProcessNames; 
         TCHAR * szCounterName; 
         validCounterNames.push_back(TEXT("\\Processor(_Total)\\% Processor Time")); 
         validCounterNames.push_back(TEXT("\\Processor(_Total)\\% Idle Time")); 
         // skip some codes
         return validCounterNames;
     } 
    

Good Luck !

In my opinion, Windows command "tasklist /v " will show the CPU TIME and MEMORY from the OS and you can call the command through 'system' or other functions.

PS. I still think looking at the OS side is the best way for monitoring CPU usage. (If I use the function, it will also do the same thing inside.)

Kwang-Chun Kang
  • 351
  • 3
  • 12