开发者

C get cpu usage on linux and windows

开发者 https://www.devze.com 2023-02-16 18:12 出处:网络
I am using below programs on linux and windows to get cpu utilization of current processes. Linux: int main()

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.


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.


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.


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);
    
0

精彩评论

暂无评论...
验证码 换一张
取 消