linux - 有人可以解释 cpu stat 中什么是美好时光吗?

标签 linux system

我知道用户时间和系统时间的区别。但是,我不太确定美好的时光。我以前知道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/

相关文章:

linux - Eclipse 的更新和安装问题

linux - 我如何在 Linux 中获取 inode 的世代号?

java - 从 Android 应用程序通过电子邮件发送崩溃报告

c - 中断和DMA,后台发生了什么?

algorithm - 如何实现标签系统

linux - nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 即使在使用端口 80 终止进程之后

java - 我将什么设置为 JAVA_HOME?

C 相当于 Perl "system()"或 Python 子进程

linux - 规避脚本中的参数列表太长(for 循环)

linux - linux load 到底是什么意思?