2014-03-26 27 views
0

需要計算進程的總CPU佔用時間,併除以當前時間減去進程開始時間。如何計算C中每個進程的平均CPU使用情況?

到目前爲止,我有以下代碼:

#include <linux/time.h> 

    cputime_t kernel_time = task_cputime->stime; //total time running in kernel space 
    cputime_t user_time = task_cputime->utime; //total time running in user space 
    cputime_t total_occupy = kernel_time + user_time; //Total CPU occupy time 

    //convert to ms 
    unsigned long total_occupy_ms = mulhdu(total_occupy, __cputime_msec_factor); 

我將如何使用do_gettimeofday獲取當前時間,以毫秒爲task_strcut->real_start_time?這可能很容易,但我對內核編程還很陌生。

回答

0
struct timeval tv; 
unsigned long timeofday_in_ms = 0; 
unsigned logn real_start_time_in_ms = 0; 
do_gettimeofday(&tv); 
timeofday = jiffies_to_ms(timeval_to_jiffies(&tv)); 
real_start_time = jiffies_to_ms(timespec_to_jiffies(&current->real_start_time)); 

上述所有涉及的功能是不言自明,可以在內核

可以使用
相關問題