Reputation: 3773
I know the difference between user time and sys time. However, I'm not quite sure about the nice time. I used to know that nice time is the time used in nice pr, but when I did an experiment, I found that the nice time didn't grow up after I reniced a 100%-CPU-using program(infinite loop doing add in Java) to 19. So, I'm confused...
Btw, I'm going to write a C++ program to monitor the CPU usage. All I can do now, is to read /proc/stat twice and get the difference. But, I don't know how to calculate the Totle time.
total = user + sys + idle
or
total = user + sys + nice + idle
or even
total = user + sys + nice + idle + iowait + ...
(the entire line).
Which is right?
Upvotes: 3
Views: 1354
Reputation:
Mpstat(1) reads /proc/stat
. Diving into kernel source tree I found a file kernel/sched/cputime.c
, taken from Linux 3.11.7 sources, which seems to include relevant bits which update things reflected in /proc/stat
:
/*
* 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);
}
This hints that time spent running niced tasks is not included into column displaying time spent running user mode tasks in general (the line index=
seems relevant to this).
Upvotes: 1