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
?這可能很容易,但我對內核編程還很陌生。