Skip to contentSkip to conversation

i386: Optimize local APIC timer interrupt code

PATCH In review Chuck Ebbert 3 participants 3 messages Jan 4, 2006 – Jan 5, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386: Optimize local APIC timer interrupt code

Local APIC timer interrupt happens HZ times per second on each CPU.

Optimize it for the case where profile multiplier equals one and does not change (99+% of cases); this saves about 20 CPU cycles on Pentium II.

Also update the old multiplier immediately after noticing it changed, while values are register-hot, saving eight bytes of stack depth.

arch/i386/kernel/apic.c +5 -5
diff -up 2.6.15a.orig/arch/i386/kernel/apic.c 2.6.15a/arch/i386/kernel/apic.c
@@ -1137,7 +1137,7 @@ inline void smp_local_timer_interrupt(st
 	int cpu = smp_processor_id();
 
 	profile_tick(CPU_PROFILING, regs);
-	if (--per_cpu(prof_counter, cpu) <= 0) {
+	if (likely(--per_cpu(prof_counter, cpu)) <= 0) {
 		/*
 		 * The multiplier may have changed since the last time we got
 		 * to this point as a result of the user writing to
@@ -1147,13 +1147,13 @@ inline void smp_local_timer_interrupt(st
 		 * Interrupts are already masked off at this point.
 		 */
 		per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
-		if (per_cpu(prof_counter, cpu) !=
-					per_cpu(prof_old_multiplier, cpu)) {
+		if (unlikely(per_cpu(prof_counter, cpu) !=
+					per_cpu(prof_old_multiplier, cpu))) {
+			per_cpu(prof_old_multiplier, cpu) =
+						per_cpu(prof_counter, cpu);
 			__setup_APIC_LVTT(
 					calibration_result/
 					per_cpu(prof_counter, cpu));
-			per_cpu(prof_old_multiplier, cpu) =
-						per_cpu(prof_counter, cpu);
 		}
 
 #ifdef CONFIG_SMP
Signature · 1 line
Chuck
#1 lore raw
Quoted 8 lines · Chuck Ebbert

Local APIC timer interrupt happens HZ times per second on each CPU.

Optimize it for the case where profile multiplier equals one and does not change (99+% of cases); this saves about 20 CPU cycles on Pentium II.

Also update the old multiplier immediately after noticing it changed, while values are register-hot, saving eight bytes of stack depth.

The code which you're patching is cheerfully nuked by a patch in Andi's tree:
ftp://ftp.firstfloor.org/pub/ak/x86_64/quilt-current/patches/no-subjiffy-profile

I don't immediately understand that patch and I don't recall seeing it discussed - maybe I was asleep.

It removes the profile multiplier (readprofile -M). I've used that occasionally, but can't say that I noticed much benefit from it.

What's the thinking here?

#2 lore raw
In reply to Chuck Ebbert
Quoted 14 lines · Chuck Ebbert

Local APIC timer interrupt happens HZ times per second on each CPU.

Optimize it for the case where profile multiplier equals one and does not change (99+% of cases); this saves about 20 CPU cycles on Pentium II.

Also update the old multiplier immediately after noticing it changed, while values are register-hot, saving eight bytes of stack depth.

diff -up 2.6.15a.orig/arch/i386/kernel/apic.c 2.6.15a/arch/i386/kernel/apic.c

arch/i386/kernel/apic.c inline void smp_local_timer_interrupt(st · line 1140

Quoted 5 lines
arch/i386/kernel/apic.c +1 -1
@@ -1137,7 +1137,7 @@ inline void smp_local_timer_interrupt(st
 	int cpu = smp_processor_id();
 
 	profile_tick(CPU_PROFILING, regs);
-	if (--per_cpu(prof_counter, cpu) <= 0) {
+	if (likely(--per_cpu(prof_counter, cpu)) <= 0) {
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

are you sure about this ? it looks suspicious to me. I would have expected something like this instead :

+ if (likely(--per_cpu(prof_counter, cpu) <= 0)) {

Willy