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.