5

I have a quick question. In man page for /proc/stat this is not clear to me: Is guest and guest_nice time included in user time in /proc/stat?

http://man7.org/linux/man-pages/man5/proc.5.html in manual there is only a hint about /proc/[pid]/stat

https://lkml.org/lkml/2008/6/23/65 here, as far as I understand they are talking about both /proc/stat and /proc/[pid]/stat

Can someone explain it? And hopefully point to any source of this information?

2
  • Yes, according to various sources on the Internet. However since I couldn't find a reliable source I'm just adding this as a comment.
    – scai
    Aug 13 '16 at 16:07
  • The manpage makes it clear that the user time includes guest time in the section on /proc/[pid]/stat. Aug 13 '16 at 16:24
6
+150

From the man page:

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.

From the kernel source (.../kernel/sched/cputime.c) we see that when guest time is being accounted for, all guest time is also added to user time (and similarly for 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;
        }
}

The user time and guest time that will be displayed via /proc/[pid]/stat are retrieved in .../fs/proc/array.c in do_task_stat() with calls to task_cputime_adjusted() and task_gtime() which return the utime and gtime fields of struct task_struct, respectively, although gtime may be tweaked:

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

[code quoted in this post is from commit 29b4817 2016-08-07 Linus Torvalds Linux 4.8-rc1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.