我知道用户时间和系统时间之间的区别。但是,我不太确定美好的时光。我曾经知道好时间是好公关中使用的时间,但是当我做实验时,我发现在我把一个 100% 使用 CPU 的程序(在 Java 中无限循环加法)到 19 后,美好时光并没有长大。所以,我很困惑...
顺便说一句,我将编写一个C++程序来监视CPU使用情况。我现在所能做的,就是阅读/proc/stat 两次并得到差异。但是,我不知道如何计算托尔时间。
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
中反映的内容的相关位:
/*
* 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=
行似乎与此相关)。