From: Jack Steiner Currently, count_active_tasks() calls both nr_running() & nr_interruptible(). Each of these functions does a "for_each_cpu" & reads values from the runqueue of each cpu. Although this is not a lot of instructions, each runqueue may be located on different node. Depending on the architecture, a unique TLB entry may be required to access each runqueue. Since there may be more runqueues than cpu TLB entries, a scan of all runqueues can trash the TLB. Each memory reference incurs a TLB miss & refill. In addition, the runqueue cacheline that contains nr_running & nr_uninterruptible may be evicted from the cache between the two passes. This causes unnecessary cache misses. Combining nr_running() & nr_interruptible() into a single function substantially reduces the TLB & cache misses on large systems. This should have no measureable effect on smaller systems. On a 128p IA64 system running a memory stress workload, the new function reduced the overhead of calc_load() from 605 usec/call to 324 usec/call. Signed-off-by: Jack Steiner Acked-by: Ingo Molnar Signed-off-by: Andrew Morton --- include/linux/sched.h | 1 + kernel/sched.c | 15 +++++++++++++++ kernel/timer.c | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff -puN include/linux/sched.h~sched-reduce-overhead-of-calc_load include/linux/sched.h --- devel/include/linux/sched.h~sched-reduce-overhead-of-calc_load 2006-03-17 23:39:06.000000000 -0800 +++ devel-akpm/include/linux/sched.h 2006-03-17 23:54:43.000000000 -0800 @@ -99,6 +99,7 @@ DECLARE_PER_CPU(unsigned long, process_c extern int nr_processes(void); extern unsigned long nr_running(void); extern unsigned long nr_uninterruptible(void); +extern unsigned long nr_active(void); extern unsigned long nr_iowait(void); extern unsigned long weighted_cpuload(const int cpu); diff -puN kernel/sched.c~sched-reduce-overhead-of-calc_load kernel/sched.c --- devel/kernel/sched.c~sched-reduce-overhead-of-calc_load 2006-03-17 23:39:06.000000000 -0800 +++ devel-akpm/kernel/sched.c 2006-03-17 23:54:48.000000000 -0800 @@ -1755,6 +1755,21 @@ unsigned long nr_iowait(void) return sum; } +unsigned long nr_active(void) +{ + unsigned long i, running = 0, uninterruptible = 0; + + for_each_online_cpu(i) { + running += cpu_rq(i)->nr_running; + uninterruptible += cpu_rq(i)->nr_uninterruptible; + } + + if (unlikely((long)uninterruptible < 0)) + uninterruptible = 0; + + return running + uninterruptible; +} + #ifdef CONFIG_SMP /* diff -puN kernel/timer.c~sched-reduce-overhead-of-calc_load kernel/timer.c --- devel/kernel/timer.c~sched-reduce-overhead-of-calc_load 2006-03-17 23:39:06.000000000 -0800 +++ devel-akpm/kernel/timer.c 2006-03-17 23:39:06.000000000 -0800 @@ -850,7 +850,7 @@ void update_process_times(int user_tick) */ static unsigned long count_active_tasks(void) { - return (nr_running() + nr_uninterruptible()) * FIXED_1; + return nr_active() * FIXED_1; } /* _