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

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

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

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

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

答案1

从手册页:

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.

从内核源代码(.../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;
        }
}

将通过以下方式显示的用户时间和访客时间/proc/[pid]/stat在中检索.../fs/proc/array.c尽管 gtime 可能会被调整,但对和 的do_task_stat()调用分别返回task_cputime_adjusted()和的字段:task_gtime()utimegtimestruct task_struct

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;
}

[本文引用的代码来自 commit 29b4817 2016-08-07 Linus Torvalds Linux 4.8-rc1]

相关内容