如何在Windows上获得每个核心CPU使用率的进程

如何在Windows上获得每个核心CPU使用率的进程

问题描述:

以编程方式获取运行每个进程的cpuid以及每个核心上各种进程的cpu核心使用情况。

示例

名称Core1 Core2 Core3 Core4

notepad.exe 0.5%0.2%0.1%0.3%





我需要windows平台的api c ++代码



我尝试了什么:



是的我试过了。可以在Linux上使用ps -ppid-L -o pid,psr,pcpu我需要获取一个等效的命令或代码来获取Windows上的psr和pcpu。

在Windows上使用wmi可以获取进程的总cpu使用量而不是每个进程使用的每个核心。请帮助我。

Programmatically obtain the cpuid on which every process is running and also the cpu core usage of various process on every core.
Example
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 tried.it 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.

我应该创建一个名为cpu timer的独立程序与在循环中进行简单操作的核心相同数量的线程。每隔一秒停下来建议你在每4个线程中执行了多少总和。

然后运行你的应用程序,然后它存储没有你的应用程序运行的总和/线程。



然后抛出你的应用程序,所以性能cpu timer会更低,因此可以再次显示每个线程的初始值的性能。



您应该有一些错误,因为Windows系统维护将运行另一个线程

您将遇到问题,因为当主处理器升温时它可以以较慢的速度运行,所以你应该读取cpu时钟速度也考虑到它。



代码可能是这样的:



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.
while(1)
{
    for (count=0;;count++)
    {
        time=clock();if (time/CLOCKS_PER_SEC>time0) break;
    }
    cout<<"Thread num percentage occupancy="<<100.0*count/performance_before<<" ./.\n";
}