需要计算进程的总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 :(得分: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(¤t->real_start_time));
上面涉及的所有功能都是不言自明的,可以在内核中使用