Skip to content

Commit b27f03d

Browse files
author
Ingo Molnar
committed
sched: make use of precise accounting for /proc task stats
make use of CFS's precise accounting to drive /proc/<pid>/stat statistics. this code was co-authored by: Balbir Singh <balbir@linux.vnet.ibm.com> Dmitry Adamushko <dmitry.adamushko@gmail.com> Ingo Molnar <mingo@elte.hu> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Dmitry Adamushko <dmitry.adamushko@gmail.com>
1 parent 1b9f19c commit b27f03d

1 file changed

Lines changed: 47 additions & 10 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,41 @@ int proc_pid_status(struct task_struct *task, char * buffer)
310310
return buffer - orig;
311311
}
312312

313+
static clock_t task_utime(struct task_struct *p)
314+
{
315+
clock_t utime = cputime_to_clock_t(p->utime),
316+
total = utime + cputime_to_clock_t(p->stime);
317+
u64 temp;
318+
319+
/*
320+
* Use CFS's precise accounting:
321+
*/
322+
temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
323+
324+
if (total) {
325+
temp *= utime;
326+
do_div(temp, total);
327+
}
328+
utime = (clock_t)temp;
329+
330+
return utime;
331+
}
332+
333+
static clock_t task_stime(struct task_struct *p)
334+
{
335+
clock_t stime = cputime_to_clock_t(p->stime);
336+
337+
/*
338+
* Use CFS's precise accounting. (we subtract utime from
339+
* the total, to make sure the total observed by userspace
340+
* grows monotonically - apps rely on that):
341+
*/
342+
stime = nsec_to_clock_t(p->se.sum_exec_runtime) - task_utime(p);
343+
344+
return stime;
345+
}
346+
347+
313348
static int do_task_stat(struct task_struct *task, char * buffer, int whole)
314349
{
315350
unsigned long vsize, eip, esp, wchan = ~0UL;
@@ -324,7 +359,8 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
324359
unsigned long long start_time;
325360
unsigned long cmin_flt = 0, cmaj_flt = 0;
326361
unsigned long min_flt = 0, maj_flt = 0;
327-
cputime_t cutime, cstime, utime, stime;
362+
cputime_t cutime, cstime;
363+
clock_t utime, stime;
328364
unsigned long rsslim = 0;
329365
char tcomm[sizeof(task->comm)];
330366
unsigned long flags;
@@ -342,7 +378,8 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
342378

343379
sigemptyset(&sigign);
344380
sigemptyset(&sigcatch);
345-
cutime = cstime = utime = stime = cputime_zero;
381+
cutime = cstime = cputime_zero;
382+
utime = stime = 0;
346383

347384
rcu_read_lock();
348385
if (lock_task_sighand(task, &flags)) {
@@ -368,15 +405,15 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
368405
do {
369406
min_flt += t->min_flt;
370407
maj_flt += t->maj_flt;
371-
utime = cputime_add(utime, t->utime);
372-
stime = cputime_add(stime, t->stime);
408+
utime += task_utime(t);
409+
stime += task_stime(t);
373410
t = next_thread(t);
374411
} while (t != task);
375412

376413
min_flt += sig->min_flt;
377414
maj_flt += sig->maj_flt;
378-
utime = cputime_add(utime, sig->utime);
379-
stime = cputime_add(stime, sig->stime);
415+
utime += cputime_to_clock_t(sig->utime);
416+
stime += cputime_to_clock_t(sig->stime);
380417
}
381418

382419
sid = signal_session(sig);
@@ -392,8 +429,8 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
392429
if (!whole) {
393430
min_flt = task->min_flt;
394431
maj_flt = task->maj_flt;
395-
utime = task->utime;
396-
stime = task->stime;
432+
utime = task_utime(task);
433+
stime = task_stime(task);
397434
}
398435

399436
/* scale priority and nice values from timeslices to -20..20 */
@@ -424,8 +461,8 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
424461
cmin_flt,
425462
maj_flt,
426463
cmaj_flt,
427-
cputime_to_clock_t(utime),
428-
cputime_to_clock_t(stime),
464+
utime,
465+
stime,
429466
cputime_to_clock_t(cutime),
430467
cputime_to_clock_t(cstime),
431468
priority,

0 commit comments

Comments
 (0)