/proc/stat - 访客是否计入用户时间?

KaP*_*KaP 5 linux cpu proc

我有一个快速的问题。在 /proc/stat 的手册页中,我不清楚:在 /proc/stat 的用户时间中是否包含来宾和来宾_nice 时间?

http://man7.org/linux/man-pages/man5/proc.5.html 手册中只有关于 /proc/[pid]/stat 的提示

https://lkml.org/lkml/2008/6/23/65 在这里,据我所知,他们在谈论 /proc/stat 和 /proc/[pid]/stat

有人可以解释一下吗?并希望指出此信息的任何来源?

use*_*274 6

从手册页:

This includes guest time,
guest_time (time spent running a virtual CPU, see
below), so that applications that are not aware of
the guest time field do not lose that time from
their calculations.
Run Code Online (Sandbox Code Playgroud)

从内核源代码 ( .../kernel/sched/cputime.c) 中我们看到,当考虑来宾时间时,所有来宾时间也会添加到用户时间中(对于 nice 也是如此)。

/*                                                                                
 * Account guest cpu time to a process.                                           
 * @p: the process that the cpu time gets accounted to                            
 * @cputime: the cpu time spent in virtual machine since the last update          
 * @cputime_scaled: cputime scaled by cpu frequency                               
 */
static void account_guest_time(struct task_struct *p, cputime_t cputime,
                               cputime_t cputime_scaled)
{
        u64 *cpustat = kcpustat_this_cpu->cpustat;

        /* Add guest time to process. */
        p->utime += cputime;
        p->utimescaled += cputime_scaled;
        account_group_user_time(p, cputime);
        p->gtime += cputime;

        /* Add guest time to cpustat. */
        if (task_nice(p) > 0) {
                cpustat[CPUTIME_NICE] += (__force u64) cputime;
                cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
        } else {
                cpustat[CPUTIME_USER] += (__force u64) cputime;
                cpustat[CPUTIME_GUEST] += (__force u64) cputime;
        }
}
Run Code Online (Sandbox Code Playgroud)

将通过被显示在用户的时间和客人时间/proc/[pid]/stat中检索.../fs/proc/array.cdo_task_stat()与调用task_cputime_adjusted()task_gtime()其返回utimegtime字段struct task_struct分别,虽然gtime可以进行调整:

cputime_t task_gtime(struct task_struct *t)
{
        unsigned int seq;
        cputime_t gtime;

        if (!vtime_accounting_enabled())
                return t->gtime;

        do {
                seq = read_seqcount_begin(&t->vtime_seqcount);

                gtime = t->gtime;
                if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU)
                        gtime += vtime_delta(t);

        } while (read_seqcount_retry(&t->vtime_seqcount, seq));

        return gtime;
}
Run Code Online (Sandbox Code Playgroud)

[这篇文章中引用的代码来自提交29b4817 2016-08-07 Linus Torvalds Linux 4.8-rc1]