Click here to Skip to main content
15,797,052 members
Please Sign up or sign in to vote.
1.21/5 (4 votes)
See more:
Programmatically obtain the cpuid on which every process is running and also the cpu core usage of various process on every core.
Name Core1 Core2 Core3 Core4
notepad.exe 0.5% 0.2% 0.1% 0.3%

I need the api c++ code for windows platform

What I have tried:

Yes I have is possible on linux using ps -p "pid" -L -o pid,psr,pcpu i need to obtain an equivalent command or code to get psr and pcpu on windows.
On windows using wmi could obtain only total cpu usage for a process and not per core per process usage.please help me.
Updated 19-Oct-17 0:21am
Dave Kreskowiak 29-Mar-17 13:25pm    
You can't. Windows doesn't track Processes "per core".
Member 13036251 29-Mar-17 15:44pm    
Have you tried powershell? Dont know how you can get that granular.
Member 12113322 30-Mar-17 1:53am    
I did try PowerShell. ps command does not list psr and pcpu attribute and also the equivalent tasklist and GetProcess() api doesnt list those properties.any tool which provides those informtaion of a process?
Foothill 29-Mar-17 16:56pm    
You could get close using the Windows Management Instrumentation classes to get close to that information but not without a good bit of real-time calculations that would most likely disrupt the process you want to monitor.
Member 12113322 30-Mar-17 1:55am    
Win32_PerfRawData_PerfProc_Process and Win32_PerfFormattedData_PerfOS_Processor classes does provide total processor time for a process and total core PercentProcessorTime for individual cores respectively.

1 solution

I should made a separated program named "cpu timer" with the same numbers of threads than cores that makes a simple operation in a loop. Every second its stop to advises how many sums have you performed in every 4 threads.
Then run your app, then it stores the sums/threads without your app running.

Then throw your app, so the performance of the "cpu timer" will be lower, so you can display the performance of every thread again initial values.

You should have some error because windows system maintenance will run another threads
You will have a problem, because when the main processor heats up it can run at slower speed so you should read the cpu clock speed also to take it in account.

The code could be something like this:

Every thread:

cout<<"Wait a second, please\n";
long count,sum=0;auto time0=clock()/CLOCKS_PER_SEC,time=time0;
for (count=0;;count++)
    time=clock();if (time/CLOCKS_PER_SEC>time0) break;
long performance_before=count;

cout<<"Now you can throw your app, please\n"; //to be performed one time.
    for (count=0;;count++)
        time=clock();if (time/CLOCKS_PER_SEC>time0) break;
    cout<<"Thread num percentage occupancy="<<100.0*count/performance_before<<" ./.\n";
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900