[英]How can I calculate the Average CPU usage of every process in C
In need to calculate the total CPU occupy time of process and divide that by the current time minus the process start time. 需要计算进程的总CPU占用时间,然后将其除以当前时间减去进程启动时间。
So far I have the following code: 到目前为止,我有以下代码:
#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);
How would I use do_gettimeofday
to get current time and task_strcut->real_start_time
in ms? 我将如何使用
do_gettimeofday
来获取当前时间和task_strcut->real_start_time
以毫秒为单位)? It may be easy, but I am fairly new to kernel programming. 这可能很容易,但是我对内核编程还是相当陌生的。
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));
All the function involving above are self-explanatory and can be used in kernel 以上涉及的所有功能都是不言自明的,可以在内核中使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.