我知道用户时间和系统时间的区别。但是,我不太确定美好的时光。我以前知道nice time就是nice pr用的时间,但是我做实验的时候发现,在我renic了一个100%-CPU-using的程序(无限循环做add in)后,nice time并没有增长Java) 到 19。所以,我很困惑......
顺便说一句,我打算编写一个 C++ 程序来监视 CPU 使用率。我现在所能做的就是读取/proc/stat 两次并找出不同之处。但是,我不知道如何计算 Totle 时间。
total = user + sys + idle
或
total = user + sys + nice + idle
甚至
total = user + sys + nice + idle + iowait + ...
(整行)。
哪个是对的?
最佳答案
Mpstat(1) 读取 /proc/stat
。深入研究内核源代码树,我发现了一个文件 kernel/sched/cputime.c
,取自 Linux 3.11.7 源代码,它似乎包含更新 /proc/stat 中反射(reflect)的内容的相关位
:
/*
* Account user cpu time to a process.
* @p: the process that the cpu time gets accounted to
* @cputime: the cpu time spent in user space since the last update
* @cputime_scaled: cputime scaled by cpu frequency
*/
void account_user_time(struct task_struct *p, cputime_t cputime,
cputime_t cputime_scaled)
{
int index;
/* Add user time to process. */
p->utime += cputime;
p->utimescaled += cputime_scaled;
account_group_user_time(p, cputime);
index = (TASK_NICE(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
/* Add user time to cpustat. */
task_group_account_field(p, index, (__force u64) cputime);
/* Account for user time used */
acct_account_cputime(p);
}
这暗示运行 niced 任务所花费的时间不包括在显示运行用户模式任务所花费时间的列中(行 index=
似乎与此相关)。
关于linux - 有人可以解释 cpu stat 中什么是美好时光吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927492/