C 获取linux和windows上的cpu使用率

发布于 2024-10-21 17:59:28 字数 2549 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想挽留 2024-10-28 17:59:28

在 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 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.

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.

半边脸i 2024-10-28 17:59:28

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

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

It is the correct output. ls is not the current process; whilst ls 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.

梦里南柯 2024-10-28 17:59:28

你在linux版本中犯了一些错误。

  1. 正如 Adrian 指出的,您应该使用 RUSAGE_CHILDREN 来计算子进程消耗的资源。
  2. 调用 system() 后,您没有再次调用 getrusage()
  3. printf() 中,不应使用表示 long double 类型的 %lftv_sec 的类型为 time_ttv_usec 的类型为 susecond_t。在 64 位 Linux 中,它们都是 signed long,这与 long double 不兼容(请注意,这里的结果为负)。为了可移植性,您应该使用显式强制转换,例如:

    printf("用户使用时间:%ld %ld\n",(long)p->ru_utime.tv_sec,(long)p->ru_utime.tv_usec);
    

You made a few errors in the linux version here.

  1. As Adrian pointed out, you should use RUSAGE_CHILDREN to 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 %lf which represents a long double type. tv_sec is of type time_t and tv_usec is 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);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文