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?

1

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

5 Comments

Strongly recommend summarizing the link or providing enough in formation for future readers to find the content again when the link rots.
Okay You're right. I added some information in there.
tasklist /v does not show CPU usage. "CPU Time" is not the same as CPU usage.
Those are the same but different expression. Please Refer, stackoverflow.com/questions/28240978/…
Thanks @Kwang-ChunKang. This solution worked for me. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.