C 获取linux和windows上的cpu使用率
我在 Linux 和 Windows 上使用以下程序来获取当前进程的 CPU 利用率。
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
这是否意味着 system("ls") 命令没有占用任何 cpu 周期来执行?如何获得任何命令或程序使用的确切 CPU 周期?
我在 Windows 上也面临类似的问题。对于下面的代码。
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++:
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 使用情况或读写磁盘/内存的字符数? 提前致谢。
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;
}
Output on 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
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?
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;
}
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Linux 版本中,您要求使用
RUSAGE_SELF
,即父进程的所有线程,而不是子进程的RUSAGE_CHILDREN
。对于 Linux 下的 IO 使用,您需要 2.6.20 之后的内核,并查看/proc/[pid]/io
。我想你在 Windows 上也有类似的问题。您需要使用
CreateProcess< /code>
而不是
system
,这样您就可以获得子进程的句柄并记录其时间。对于 Windows 上的 IO 使用,我认为您需要使用 WMI,这是一个很大的主题。In the Linux version you've asked for
RUSAGE_SELF
, which is all threads of the parent process, rather thanRUSAGE_CHILDREN
for child processes. For IO usage under Linux you'll need a kernel after 2.6.20, and look in/proc/[pid]/io
.I think you have a similar problem on Windows. You'll need to use
CreateProcess
rather thansystem
, 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.这是正确的输出。
ls
不是当前进程;当ls
正在执行时,您的进程不会花费任何 CPU 时间。顺便说一句,Dev-C++ 只是一个[糟糕的、未维护的] IDE,而不是编译器。你可能想说 MinGW。
It is the correct output.
ls
is not the current process; whilstls
is executing, no CPU time is spent on your process.BTW Dev-C++ is just a [bad, unmaintained] IDE, not a compiler. You probably meant to say MinGW.
你在linux版本中犯了一些错误。
RUSAGE_CHILDREN
来计算子进程消耗的资源。system()
后,您没有再次调用getrusage()
。在
printf()
中,不应使用表示 long double 类型的%lf
。tv_sec
的类型为time_t
,tv_usec
的类型为susecond_t
。在 64 位 Linux 中,它们都是signed long
,这与long double
不兼容(请注意,这里的结果为负)。为了可移植性,您应该使用显式强制转换,例如:You made a few errors in the linux version here.
RUSAGE_CHILDREN
to count in the resource consumed by the child process.getrusage()
again after you invokedsystem()
.In your
printf()
, you should not use%lf
which represents a long double type.tv_sec
is of typetime_t
andtv_usec
is of typesusecond_t
. In 64-bit linux, they are bothsigned long
, which is incompatible withlong double
(note you have negative result here). For portability, you should have used explicit cast, such as: