Skip to content

Commit d180c5b

Browse files
Hidetoshi SetoIngo Molnar
authored andcommitted
sched: Introduce task_times() to replace task_{u,s}time() pair
Functions task_{u,s}time() are called in pair in almost all cases. However task_stime() is implemented to call task_utime() from its inside, so such paired calls run task_utime() twice. It means we do heavy divisions (div_u64 + do_div) twice to get utime and stime which can be obtained at same time by one set of divisions. This patch introduces a function task_times(*tsk, *utime, *stime) to retrieve utime and stime at once in better, optimized way. Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com> Acked-by: Peter Zijlstra <peterz@infradead.org> Cc: Stanislaw Gruszka <sgruszka@redhat.com> Cc: Spencer Candland <spencer@bluehost.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Americo Wang <xiyou.wangcong@gmail.com> LKML-Reference: <4B0E16AE.906@jp.fujitsu.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 16bc67e commit d180c5b

5 files changed

Lines changed: 43 additions & 26 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
535535
if (!whole) {
536536
min_flt = task->min_flt;
537537
maj_flt = task->maj_flt;
538-
utime = task_utime(task);
539-
stime = task_stime(task);
538+
task_times(task, &utime, &stime);
540539
gtime = task_gtime(task);
541540
}
542541

‎include/linux/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ static inline void put_task_struct(struct task_struct *t)
17231723
extern cputime_t task_utime(struct task_struct *p);
17241724
extern cputime_t task_stime(struct task_struct *p);
17251725
extern cputime_t task_gtime(struct task_struct *p);
1726+
extern void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st);
17261727

17271728
/*
17281729
* Per process flags

‎kernel/exit.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static void __exit_signal(struct task_struct *tsk)
9191
if (atomic_dec_and_test(&sig->count))
9292
posix_cpu_timers_exit_group(tsk);
9393
else {
94+
cputime_t utime, stime;
95+
9496
/*
9597
* If there is any task waiting for the group exit
9698
* then notify it:
@@ -110,8 +112,9 @@ static void __exit_signal(struct task_struct *tsk)
110112
* We won't ever get here for the group leader, since it
111113
* will have been the last reference on the signal_struct.
112114
*/
113-
sig->utime = cputime_add(sig->utime, task_utime(tsk));
114-
sig->stime = cputime_add(sig->stime, task_stime(tsk));
115+
task_times(tsk, &utime, &stime);
116+
sig->utime = cputime_add(sig->utime, utime);
117+
sig->stime = cputime_add(sig->stime, stime);
115118
sig->gtime = cputime_add(sig->gtime, task_gtime(tsk));
116119
sig->min_flt += tsk->min_flt;
117120
sig->maj_flt += tsk->maj_flt;

‎kernel/sched.c‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5191,48 +5191,63 @@ cputime_t task_stime(struct task_struct *p)
51915191
{
51925192
return p->stime;
51935193
}
5194+
5195+
void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
5196+
{
5197+
if (ut)
5198+
*ut = task_utime(p);
5199+
if (st)
5200+
*st = task_stime(p);
5201+
}
51945202
#else
51955203

51965204
#ifndef nsecs_to_cputime
51975205
# define nsecs_to_cputime(__nsecs) \
51985206
msecs_to_cputime(div_u64((__nsecs), NSEC_PER_MSEC))
51995207
#endif
52005208

5201-
cputime_t task_utime(struct task_struct *p)
5209+
void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
52025210
{
5203-
cputime_t utime = p->utime, total = utime + p->stime;
5204-
u64 temp;
5211+
cputime_t rtime, utime = p->utime, total = utime + p->stime;
52055212

52065213
/*
52075214
* Use CFS's precise accounting:
52085215
*/
5209-
temp = (u64)nsecs_to_cputime(p->se.sum_exec_runtime);
5216+
rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
52105217

52115218
if (total) {
5212-
temp *= utime;
5219+
u64 temp;
5220+
5221+
temp = (u64)(rtime * utime);
52135222
do_div(temp, total);
5214-
}
5215-
utime = (cputime_t)temp;
5223+
utime = (cputime_t)temp;
5224+
} else
5225+
utime = rtime;
52165226

5227+
/*
5228+
* Compare with previous values, to keep monotonicity:
5229+
*/
52175230
p->prev_utime = max(p->prev_utime, utime);
5218-
return p->prev_utime;
5231+
p->prev_stime = max(p->prev_stime, rtime - p->prev_utime);
5232+
5233+
if (ut)
5234+
*ut = p->prev_utime;
5235+
if (st)
5236+
*st = p->prev_stime;
5237+
}
5238+
5239+
cputime_t task_utime(struct task_struct *p)
5240+
{
5241+
cputime_t utime;
5242+
task_times(p, &utime, NULL);
5243+
return utime;
52195244
}
52205245

52215246
cputime_t task_stime(struct task_struct *p)
52225247
{
52235248
cputime_t stime;
5224-
5225-
/*
5226-
* Use CFS's precise accounting. (we subtract utime from
5227-
* the total, to make sure the total observed by userspace
5228-
* grows monotonically - apps rely on that):
5229-
*/
5230-
stime = nsecs_to_cputime(p->se.sum_exec_runtime) - task_utime(p);
5231-
5232-
if (stime >= 0)
5233-
p->prev_stime = max(p->prev_stime, stime);
5234-
5235-
return p->prev_stime;
5249+
task_times(p, NULL, &stime);
5250+
return stime;
52365251
}
52375252
#endif
52385253

‎kernel/sys.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,7 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
13461346
utime = stime = cputime_zero;
13471347

13481348
if (who == RUSAGE_THREAD) {
1349-
utime = task_utime(current);
1350-
stime = task_stime(current);
1349+
task_times(current, &utime, &stime);
13511350
accumulate_thread_rusage(p, r);
13521351
maxrss = p->signal->maxrss;
13531352
goto out;

0 commit comments

Comments
 (0)