C 在 linux 和 windows 上获取 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5272470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:24:31  来源:igfitidea点击:

C get cpu usage on linux and windows

cwindowslinuxiocpu

提问by Enjoy coding

I am using below programs on linux and windows to get cpu utilization of current processes.

我在 linux 和 windows 上使用以下程序来获取当前进程的 CPU 利用率。

Linux:

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:

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?

这是否意味着 system("ls") 命令没有执行任何 CPU 周期?我如何获得任何命令或程序使用的确切 CPU 周期?

I am facing similar problems on windows. for the below code.

我在 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;
}

Above program output on windowsdev c++:

以上程序输出在 windowsdev 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.

您能否让我知道我们如何为上述程序获得正确的 CPU 使用率?还有一种方法可以了解 IO 使用情况或读写磁盘/内存的字符数吗?提前致谢。

回答by Adrian Cox

In the Linux version you've asked for RUSAGE_SELF, which is all threads of the parent process, rather than RUSAGE_CHILDRENfor child processes. For IO usage under Linux you'll need a kernel after 2.6.20, and look in /proc/[pid]/io.

在您要求的 Linux 版本中RUSAGE_SELF,它是父进程的所有线程,而不是RUSAGE_CHILDREN子进程。对于 Linux 下的 IO 使用,您需要 2.6.20 之后的内核,并查看/proc/[pid]/io.

I think you have a similar problem on Windows. You'll need to use CreateProcessrather 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.

我认为您在 Windows 上也有类似的问题。您需要使用CreateProcess而不是system,以便您可以获得子进程的句柄并记录其时间。对于 Windows 上的 IO 使用,我认为您需要使用 WMI,这是一个很大的主题。

回答by Thunder Liu

You made a few errors in the linux version here.

您在此处的 linux 版本中犯了一些错误。

  1. As Adrian pointed out, you should use RUSAGE_CHILDRENto count in the resource consumed by the child process.
  2. You did not call getrusage()again after you invoked system().
  3. In your printf(), you should not use %lfwhich represents a long double type. tv_secis of type time_tand tv_usecis of type susecond_t. In 64-bit linux, they are both signed long, which is incompatible with long double(note you have negative result here). For portability, you should have used explicit cast, such as:

    printf("user time used: %ld  %ld\n",(long)p->ru_utime.tv_sec,(long)p->ru_utime.tv_usec);
    
  1. 正如 Adrian 所指出的,您应该使用RUSAGE_CHILDREN来计算子进程消耗的资源。
  2. 调用getrusage()后没有再次调用system()
  3. 在您的 中printf(),您不应使用%lfwhich 代表 long double 类型。tv_sec属于 类型time_t并且tv_usec属于 类型susecond_t。在 64 位 linux 中,它们都是signed long,这是不兼容的long double(注意这里有否定结果)。为了可移植性,您应该使用显式转换,例如:

    printf("user time used: %ld  %ld\n",(long)p->ru_utime.tv_sec,(long)p->ru_utime.tv_usec);
    

回答by Lightness Races in Orbit

It is the correct output. lsis not the current process; whilst lsis executing, no CPU time is spent on your process.

这是正确的输出。ls不是当前进程;在ls执行过程中,您的进程不会花费任何 CPU 时间。

BTW Dev-C++ is just a [bad, unmaintained] IDE, not a compiler. You probably meant to say MinGW.

顺便说一句,Dev-C++ 只是一个 [糟糕的、未维护的] IDE,而不是编译器。您可能想说的是 MinGW。