/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]

相關內容