C在Linux和Windows上获得cpu使用率 [英] C get cpu usage on linux and windows
问题描述
我在linux和windows上使用以下程序来获取当前进程的cpu利用率。
Linux:
I am using below programs on linux and windows to get cpu utilization of current processes.
Linux:
int main()
{
int ret;
char *buf;
int i=0;
int who= RUSAGE_SELF;
struct rusage usage;
struct rusage *p=&usage;
ret=getrusage(who,p);
printf("user time used: %16lf %16lf\n",p->ru_utime.tv_sec,p->ru_utime.tv_usec);
printf("system time used: %16lf %16lf\n",p->ru_stime.tv_sec,p->ru_stime.tv_usec);
system("ls");
printf("user time used: %16lf %16lf\n",p->ru_utime.tv_sec,p->ru_utime.tv_usec);
printf("system time used: %16lf %16lf\n", p->ru_stime.tv_sec,p->ru_stime.tv_usec);
return 0;
}
linux输出:
user time used: 0.000000 -1.999568
system time used: 0.000000 -1.999568
a.out check.c
user time used: 0.000000 -1.999568
system time used: 0.000000 -1.999568
这是否意味着系统(ls)命令没有执行任何cpu周期?我如何获得任何命令或程序使用的确切cpu周期?
Does this mean that the system("ls") command did not take any cpu cycles to execute? How do i get the exact cpu cycles used by any command or program?
我在Windows上遇到类似的问题。对于以下代码。
Windows:
I am facing similar problems on windows. for the below code.
Windows:
int main()
{
int i=0;
HANDLE hProcess = GetCurrentProcess();
FILETIME ftCreation, ftExit, ftKernel, ftUser;
SYSTEMTIME stKernel;
SYSTEMTIME stUser;
GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
FileTimeToSystemTime(&ftKernel, &stKernel);
FileTimeToSystemTime(&ftUser, &stUser);
printf("\nTime in kernel mode = %uh %um %us %ums", stKernel.wHour,stKernel.wMinute, stKernel.wSecond, stKernel.wMilliseconds);
printf("\nTime in user mode = %uh %um %us %ums \n", stUser.wHour,stUser.wMinute, stUser.wSecond, stUser.wMilliseconds);
system("dir");
GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
FileTimeToSystemTime(&ftKernel, &stKernel);
FileTimeToSystemTime(&ftUser, &stUser);
printf("\nTime in kernel mode = %uh %um %us %ums", stKernel.wHour,stKernel.wMinute, stKernel.wSecond, stKernel.wMilliseconds);
printf("\nTime in user mode = %uh %um %us %ums \n", stUser.wHour,stUser.wMinute, stUser.wSecond, stUser.wMilliseconds);
system("PAUSE");
return 0;
}
以上程序在Windows上输出 dev c ++:
Above program output on windows dev c++:
Time in kernel mode: 0h 0m 0s 15ms
Time in user mode: 0h 0m 0s 15ms
<directory listing>
Time in kernel mode: 0h 0m 0s 15ms
Time in user mode: 0h 0m 0s 15ms
您能否告诉我们如何才能获得上述程序的正确CPU使用率?还有办法了解IO使用情况或读写磁盘/内存的字符数吗?
提前致谢。
Can you please let me know how can we get correct cpu usage for the above programs? Also is there a way to get to know IO usage or number of characters read and write to disk/memory? Thanks in advance.
推荐答案
在Linux版本中,您要求 RUSAGE_SELF
,这是父进程的所有线程,而不是子进程的 RUSAGE_CHILDREN
。对于Linux下的IO使用,你需要2.6.20之后的内核,并查看 / proc / [pid] / io
。
In the Linux version you've asked for RUSAGE_SELF
, which is all threads of the parent process, rather than RUSAGE_CHILDREN
for child processes. For IO usage under Linux you'll need a kernel after 2.6.20, and look in /proc/[pid]/io
.
我认为你在Windows上遇到了类似的问题。您需要使用 CreateProcess
而不是 system
,以便您可以获取子进程的句柄并记录其时间。对于Windows上的IO使用,我认为你需要使用WMI,这是一个很大的主题。
I think you have a similar problem on Windows. You'll need to use CreateProcess
rather than system
, so that you can get a handle to the child process and record its times. For IO usage on windows I think you'll need to use WMI, which is a large subject.
这篇关于C在Linux和Windows上获得cpu使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!