簡體   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