繁体   English   中英

如何计算C中每个进程的平均CPU使用率

[英]How can I calculate the Average CPU usage of every process in 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以毫秒为单位)? 这可能很容易,但是我对内核编程还是相当陌生的。

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));

以上涉及的所有功能都是不言自明的,可以在内核中使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM