精慕HU
您需要从/proc//stat..这些是最初的几个字段Documentation/filesystems/proc.txt在您的内核源代码中):Table 1-3: Contents of the stat files (as of 2.6.22-rc3).............................................................................. Field Content pid process id tcomm filename of the executable state state (R is running, S is sleeping, D is sleeping in an uninterruptible wait, Z is zombie, T is traced or stopped) ppid process id of the parent process pgrp pgrp of the process sid session id tty_nr tty the process uses tty_pgrp pgrp of the tty flags task flags min_flt number of minor faults cmin_flt number of minor faults with child's maj_flt number of major faults cmaj_flt number of major faults with child's utime user mode jiffies stime kernel mode jiffies cutime user mode jiffies with child's cstime kernel mode jiffies with child's你可能是在找utime和/或stime..您还需要阅读cpu从/proc/stat,看起来是:cpu 192369 7119 480152 122044337 14142 9937 26747 0 0这将告诉您在各种类别中使用的累计CPU时间(单位为Jiffies)。您需要取这一行上的值之和,才能获得time_total测量。两者都读utime和stime对于您感兴趣的过程,请阅读time_total从…/proc/stat..然后睡一会儿左右,然后再读一遍。现在,您可以通过以下方法计算采样时间内进程的CPU使用情况:user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before);
sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before);讲得通?