Skip to content

Commit ff9a9b4

Browse files
Rik van RielIngo Molnar
authored andcommitted
sched, time: Switch VIRT_CPU_ACCOUNTING_GEN to jiffy granularity
When profiling syscall overhead on nohz-full kernels, after removing __acct_update_integrals() from the profile, native_sched_clock() remains as the top CPU user. This can be reduced by moving VIRT_CPU_ACCOUNTING_GEN to jiffy granularity. This will reduce timing accuracy on nohz_full CPUs to jiffy based sampling, just like on normal CPUs. It results in totally removing native_sched_clock from the profile, and significantly speeding up the syscall entry and exit path, as well as irq entry and exit, and KVM guest entry & exit. Additionally, only call the more expensive functions (and advance the seqlock) when jiffies actually changed. This code relies on another CPU advancing jiffies when the system is busy. On a nohz_full system, this is done by a housekeeping CPU. A microbenchmark calling an invalid syscall number 10 million times in a row speeds up an additional 30% over the numbers with just the previous patches, for a total speedup of about 40% over 4.4 and 4.5-rc1. Run times for the microbenchmark: 4.4 3.8 seconds 4.5-rc1 3.7 seconds 4.5-rc1 + first patch 3.3 seconds 4.5-rc1 + first 3 patches 3.1 seconds 4.5-rc1 + all patches 2.3 seconds A non-NOHZ_FULL cpu (not the housekeeping CPU): all kernels 1.86 seconds Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: clark@redhat.com Cc: eric.dumazet@gmail.com Cc: fweisbec@gmail.com Cc: luto@amacapital.net Link: http://lkml.kernel.org/r/1455152907-18495-5-git-send-email-riel@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 9344c92 commit ff9a9b4

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

kernel/sched/cputime.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -668,26 +668,25 @@ void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime
668668
#endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
669669

670670
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
671-
static unsigned long long vtime_delta(struct task_struct *tsk)
671+
static cputime_t vtime_delta(struct task_struct *tsk)
672672
{
673-
unsigned long long clock;
673+
unsigned long now = READ_ONCE(jiffies);
674674

675-
clock = local_clock();
676-
if (clock < tsk->vtime_snap)
675+
if (time_before(now, (unsigned long)tsk->vtime_snap))
677676
return 0;
678677

679-
return clock - tsk->vtime_snap;
678+
return jiffies_to_cputime(now - tsk->vtime_snap);
680679
}
681680

682681
static cputime_t get_vtime_delta(struct task_struct *tsk)
683682
{
684-
unsigned long long delta = vtime_delta(tsk);
683+
unsigned long now = READ_ONCE(jiffies);
684+
unsigned long delta = now - tsk->vtime_snap;
685685

686686
WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
687-
tsk->vtime_snap += delta;
687+
tsk->vtime_snap = now;
688688

689-
/* CHECKME: always safe to convert nsecs to cputime? */
690-
return nsecs_to_cputime(delta);
689+
return jiffies_to_cputime(delta);
691690
}
692691

693692
static void __vtime_account_system(struct task_struct *tsk)
@@ -699,6 +698,9 @@ static void __vtime_account_system(struct task_struct *tsk)
699698

700699
void vtime_account_system(struct task_struct *tsk)
701700
{
701+
if (!vtime_delta(tsk))
702+
return;
703+
702704
write_seqcount_begin(&tsk->vtime_seqcount);
703705
__vtime_account_system(tsk);
704706
write_seqcount_end(&tsk->vtime_seqcount);
@@ -707,7 +709,8 @@ void vtime_account_system(struct task_struct *tsk)
707709
void vtime_gen_account_irq_exit(struct task_struct *tsk)
708710
{
709711
write_seqcount_begin(&tsk->vtime_seqcount);
710-
__vtime_account_system(tsk);
712+
if (vtime_delta(tsk))
713+
__vtime_account_system(tsk);
711714
if (context_tracking_in_user())
712715
tsk->vtime_snap_whence = VTIME_USER;
713716
write_seqcount_end(&tsk->vtime_seqcount);
@@ -718,16 +721,19 @@ void vtime_account_user(struct task_struct *tsk)
718721
cputime_t delta_cpu;
719722

720723
write_seqcount_begin(&tsk->vtime_seqcount);
721-
delta_cpu = get_vtime_delta(tsk);
722724
tsk->vtime_snap_whence = VTIME_SYS;
723-
account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
725+
if (vtime_delta(tsk)) {
726+
delta_cpu = get_vtime_delta(tsk);
727+
account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
728+
}
724729
write_seqcount_end(&tsk->vtime_seqcount);
725730
}
726731

727732
void vtime_user_enter(struct task_struct *tsk)
728733
{
729734
write_seqcount_begin(&tsk->vtime_seqcount);
730-
__vtime_account_system(tsk);
735+
if (vtime_delta(tsk))
736+
__vtime_account_system(tsk);
731737
tsk->vtime_snap_whence = VTIME_USER;
732738
write_seqcount_end(&tsk->vtime_seqcount);
733739
}
@@ -742,7 +748,8 @@ void vtime_guest_enter(struct task_struct *tsk)
742748
* that can thus safely catch up with a tickless delta.
743749
*/
744750
write_seqcount_begin(&tsk->vtime_seqcount);
745-
__vtime_account_system(tsk);
751+
if (vtime_delta(tsk))
752+
__vtime_account_system(tsk);
746753
current->flags |= PF_VCPU;
747754
write_seqcount_end(&tsk->vtime_seqcount);
748755
}
@@ -772,7 +779,7 @@ void arch_vtime_task_switch(struct task_struct *prev)
772779

773780
write_seqcount_begin(&current->vtime_seqcount);
774781
current->vtime_snap_whence = VTIME_SYS;
775-
current->vtime_snap = sched_clock_cpu(smp_processor_id());
782+
current->vtime_snap = jiffies;
776783
write_seqcount_end(&current->vtime_seqcount);
777784
}
778785

@@ -783,7 +790,7 @@ void vtime_init_idle(struct task_struct *t, int cpu)
783790
local_irq_save(flags);
784791
write_seqcount_begin(&t->vtime_seqcount);
785792
t->vtime_snap_whence = VTIME_SYS;
786-
t->vtime_snap = sched_clock_cpu(cpu);
793+
t->vtime_snap = jiffies;
787794
write_seqcount_end(&t->vtime_seqcount);
788795
local_irq_restore(flags);
789796
}

0 commit comments

Comments
 (0)