I have a process which in earlier release took some time.
Now the same process in the current release is taking more time. But there are no changes in the code when compared to earlier. One observation I saw is there are some other processes on the server which are consuming high cpu when compared to earlier releases. So I doubt my process is not getting the cpu time which is why it is taking more time.
But I need to prove this that the process is waiting(idle) for most of the time.
So for doing this i am thinking of using getrusage()
. But asI am not sure how do I calculate the process idle time using that.
Could anybody please tell me a simple example as to how to use getrusage
specifically for calculating the idle time of the process.
Thanks in advance.
Rather than calculating the process idle time, you can calculate it's execution time and reduce that from wall-clock time.
In an OS conforming to newer POSIX standards (read Linux), you can easily do so using the
clock_gettime
function:You can use
CLOCK_REALTIME
to get the wall-clock time, and take the difference between the returned time at the beginning and the end of the program to understand how long the program took to run (including idle time). UsingCLOCK_PROCESS_CPUTIME_ID
, you can measure exactly how much your process spent running on the CPU in a similar way. UsingCLOCK_THREAD_CPUTIME_ID
you can even get the execution time of each thread separately.Note: if a process/thread migrates from a CPU to another, the
CLOCK_*_CPUTIME_ID
may become inconsistent. Keep that in mind. If it does happen, you can try to bind your process or its threads to specific CPUs.As a side note,
time(1)
gives you information on the execution time of a process, so if the wall-clock time is large enough to be measured by your watch for example, you can also use this tool.P.S. Don't forget to link with
-lrt
.