Linux中进程的CPU争用(等待时间) [英] CPU contention (wait time) for a process in Linux
问题描述
如何检查一个进程在Linux机器中等待CPU的时间?
How can I check how long a process spends waiting for the CPU in a Linux box?
例如,在一个加载的系统中,我想检查一个 SQL * Loader (sqlldr)进程正在等待。
For example, in a loaded system I want to check how long a SQL*Loader (sqlldr) process waits.
如果有命令行工具可以执行此操作。
It would be useful if there is a command line tool to do this.
推荐答案
我遇到了这个问题回来一段时间我最终使用getrusage:
您可以在以下位置获得详细的帮助:
http://www.opengroup.org/onlinepubs/009695399/functions/getrusage.html
I had this problem some time back. I ended up using getrusage : You can get detailed help at : http://www.opengroup.org/onlinepubs/009695399/functions/getrusage.html
getrusage填充了rusage结构。
getrusage populates the rusage struct.
使用getrusage测量等待时间
您可以在代码的开头调用getrusage,然后在代码结尾或执行过程中的某个适当时间再次调用它。然后,您将拥有initial_rusage和final_rusage。您的进程花费的用户时间由rusage-> ru_utime.tv_sec指示,而该进程花费的系统时间由rusage-> ru_stime.tv_sec指示。
You can call getrusage at the beginning of your code and then again call it at the end, or at some appropriate point during execution. You have then initial_rusage and final_rusage. The user-time spent by your process is indicated by rusage->ru_utime.tv_sec and system-time spent by the process is indicated by rusage->ru_stime.tv_sec.
因此,该流程花费的总用户时间为:
user_time = final_rusage.ru_utime.tv_sec-initial_rusage.ru_utime.tv_sec
Thus the total user-time spent by the process will be: user_time = final_rusage.ru_utime.tv_sec - initial_rusage.ru_utime.tv_sec
该流程花费的总系统时间为:
system_time = final_rusage.ru_stime.tv_sec-initial_rusage.ru_stime.tv_sec
The total system-time spent by the process will be: system_time = final_rusage.ru_stime.tv_sec - initial_rusage.ru_stime.tv_sec
如果total_time是两次调用getrusage之间的时间,则等待时间为
wait_time = total_time-(user_time + system_time)
If total_time is the time elapsed between the two calls of getrusage then the wait time will be wait_time = total_time - (user_time + system_time)
希望这会有所帮助
这篇关于Linux中进程的CPU争用(等待时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!