Skip to contentSkip to conversation

Instrumenting high latency

PATCH In review Con Kolivas 7 participants 21 messages Jul 12, 2004

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Instrumenting high latency

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

It is enabled via a config option and by setting the threshold at boot by passing the parameter:
preempt_thresh=2

to set the threshold at 2ms for example.

The output is a warning in syslog like so:

5ms non-preemptible critical section violated 2 ms preempt threshold starting at add_wait_queue+0x21/0x82 and ending at add_wait _queue+0x4a/0x82

I would not recommend using this patch for daily use but please try it out on multiple setups/filesystems etc and help us track down the areas. Unfortunately I am not personally capable of fixing the code paths in question so I'll need the help of others in this.

The patch appears to require CONFIG_PREEMPT enabled on uniprocessor and is i386 only at the moment.

Patch applies to 2.6.8-rc1

Cheers,
Con Kolivas

arch/i386/Kconfig +7 -0
Index: linux-2.6.8-rc1/arch/i386/Kconfig
===================================================================
@@ -504,6 +504,13 @@
 	  Say Y here if you are building a kernel for a desktop, embedded
 	  or real-time system.  Say N if you are unsure.
 
+config PREEMPT_TIMING
+	bool "Non-preemptible critical section timing"
+	help
+	  This option measures the time spent in non-preemptible critical
+	  sections and reports warnings when a boot-time configurable
+	  latency threshold is exceeded.
+
 config X86_UP_APIC
 	bool "Local APIC support on uniprocessors" if !SMP
 	depends on !(X86_VISWS || X86_VOYAGER)
arch/i386/kernel/traps.c +65 -0

68 lines not on this page · raw message

include/linux/preempt.h +18 -13
Index: linux-2.6.8-rc1/include/linux/preempt.h
===================================================================
@@ -9,17 +9,22 @@
 #include <linux/config.h>
 #include <linux/linkage.h>
 
-#define preempt_count()	(current_thread_info()->preempt_count)
-
-#define inc_preempt_count() \
-do { \
-	preempt_count()++; \
-} while (0)
+#ifdef CONFIG_PREEMPT_TIMING
+void __inc_preempt_count(void);
+void __dec_preempt_count(void);
+#else
+#ifdef CONFIG_PREEMPT
+#define __inc_preempt_count()	do { preempt_count()++; } while (0)
+#define __dec_preempt_count()	do { preempt_count()--; } while (0)
+#else
+#define __inc_preempt_count()	do { } while (0)
+#define __dec_preempt_count()	do { } while (0)
+#endif
+#endif
 
-#define dec_preempt_count() \
-do { \
-	preempt_count()--; \
-} while (0)
+#define preempt_count()	(current_thread_info()->preempt_count)
+#define inc_preempt_count()	__inc_preempt_count()
+#define dec_preempt_count()	__dec_preempt_count()
 
 #ifdef CONFIG_PREEMPT
 
@@ -51,9 +56,9 @@
 
 #else
 
-#define preempt_disable()		do { } while (0)
-#define preempt_enable_no_resched()	do { } while (0)
-#define preempt_enable()		do { } while (0)
+#define preempt_disable()		__inc_preempt_count()
+#define preempt_enable_no_resched()	__dec_preempt_count()
+#define preempt_enable()		__dec_preempt_count()
 #define preempt_check_resched()		do { } while (0)
 
 #endif
#1 lore raw
Quoted 6 lines · Con Kolivas

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count.

Looks sane.

Quoted 2 lines

The patch appears to require CONFIG_PREEMPT enabled on uniprocessor and is i386 only at the moment.

Not sure what you mean by "on uniprocessor"? AFAICT the patch will work as-is on uniprocessor and on SMP. Looks like it'll work with CONFIG_PREEMPT=n, too, although that would be a slightly bizarre thing to do.

+ print_symbol("%s\n",
+ __get_cpu_var(preempt_exit));

I'll change this to

				print_symbol("%s",
					__get_cpu_var(preempt_exit));
				printk("\n");

so it doesn't make a mess with CONFIG_KALLSYMS=n.

#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 2 lines · Con Kolivas

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count.

Looks sane.

Quoted 1 line

The patch appears to require CONFIG_PREEMPT enabled on uniprocessor and is i386 only at the moment.

Not sure what you mean by "on uniprocessor"? AFAICT the patch will work as-is on uniprocessor and on SMP. Looks like it'll work with CONFIG_PREEMPT=n, too, although that would be a slightly bizarre thing to

That was WLI's aim, but I had one user report an OOPS with that combination.

Quoted 5 lines

do.

arch/i386/kernel/traps.c +2 -0
+				print_symbol("%s\n",
+					__get_cpu_var(preempt_exit));
Quoted 7 lines

I'll change this to

				print_symbol("%s",
					__get_cpu_var(preempt_exit));
				printk("\n");

so it doesn't make a mess with CONFIG_KALLSYMS=n.

Ok

It's not heavily tested but ran for a day on my box at home. It can trip over itself saying that it's own code paths are a problem too if it gets real busy.

Cheers,
Con

#3 lore raw
Quoted 4 lines · Con Kolivas

It can trip
over itself saying that it's own code paths are a problem too if it gets real busy.

This might fix it?

arch/i386/kernel/traps.c renamed from arch/i386/kernel/traps.c~instrumenting-high-latency-fixes +6 -2
diff -puN arch/i386/kernel/traps.c~instrumenting-high-latency-fixes arch/i386/kernel/traps.c
@@ -1065,6 +1065,9 @@ void __dec_preempt_count(void)
 	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING &&
 					__get_cpu_var(preempt_entry)) {
 		u64 exit;
+
+		preempt_count()++;	/* Prevent recursive warnings */
+
 		__get_cpu_var(preempt_exit)
 				= (unsigned long)__builtin_return_address(0);
 		rdtscll(exit);
@@ -1082,12 +1085,13 @@ void __dec_preempt_count(void)
 					preempt_thresh);
 				print_symbol("%s and ending at ",
 					__get_cpu_var(preempt_entry));
-				print_symbol("%s\n",
-					__get_cpu_var(preempt_exit));
+				print_symbol("%s", __get_cpu_var(preempt_exit));
+				printk("\n");
 				dump_stack();
 			}
 		}
 		__get_cpu_var(preempt_exit) = __get_cpu_var(preempt_entry) = 0;
+		preempt_count()--;
 	}
 	preempt_count()--;
 }

_

#4 lore raw
In reply to Andrew Morton
Quoted 2 lines · Con Kolivas
Quoted 2 lines

The patch appears to require CONFIG_PREEMPT enabled on uniprocessor and is i386 only at the moment.

Quoted 11 lines · Andrew Morton

Not sure what you mean by "on uniprocessor"? AFAICT the patch will work as-is on uniprocessor and on SMP. Looks like it'll work with CONFIG_PREEMPT=n, too, although that would be a slightly bizarre thing to do.

arch/i386/kernel/traps.c +2 -0
+				print_symbol("%s\n",
+					__get_cpu_var(preempt_exit));

I'll change this to

				print_symbol("%s",
					__get_cpu_var(preempt_exit));
				printk("\n");

so it doesn't make a mess with CONFIG_KALLSYMS=n.

I'm not 100% sure why UP goes down in flames yet, fscking laptop has no loggable console. Guess I'd better figure out netconsole or some such.

-- wli

#5 lore raw
In reply to Con Kolivas
Quoted 4 lines · Con Kolivas

He hacked
together this simple patch which times periods according to the preempt count.

OK, small problem. We have code which does, effectively,

	if (need_resched()) {
		drop_the_lock();
		schedule();
		grab_the_lock();
	}

so if need_resched() stays false then this will hold the lock for a long time and bogus reports are generated:

46ms non-preemptible critical section violated 1 ms preempt threshold starting at exit_mmap+0x26/0x188 and ending at exit_mmap+0x154/0x188

To fix that you need to generate high scheduling pressure so that need_resched() is frequently true. On all CPUs. Modify realfeel to pin itself to each CPU, or something like that.

This rather decreases the patch's usefulness.

The way I normally do this stuff is with

http://www.zip.com.au/~akpm/linux/patches/stuff/rtc-debug.patch

and `amlat', from http://www.zip.com.au/~akpm/linux/amlat.tar.gz

It _might_ be sufficient to redefine need_resched() to just return 1 all the time. If that causes the kernel to livelock then we need to fix that up anyway.

#6 lore raw
Quoted 6 lines · Andrew Morton

OK, small problem. We have code which does, effectively,

	if (need_resched()) {
		drop_the_lock();
		schedule();
		grab_the_lock();
	}
Quoted 6 lines

so if need_resched() stays false then this will hold the lock for a long
time and bogus reports are generated:
46ms non-preemptible critical section violated 1 ms preempt threshold starting at exit_mmap+0x26/0x188 and ending at exit_mmap+0x154/0x188 To fix that you need to generate high scheduling pressure so that
need_resched() is frequently true. On all CPUs. Modify realfeel to pin
itself to each CPU, or something like that.

I suspect it's better to drop in hooks to trap those e.g. in cond_resched() and cond_resched_lock().

Quoted 7 lines · Andrew Morton

This rather decreases the patch's usefulness.
The way I normally do this stuff is with
http://www.zip.com.au/~akpm/linux/patches/stuff/rtc-debug.patch
and `amlat', from http://www.zip.com.au/~akpm/linux/amlat.tar.gz
It _might_ be sufficient to redefine need_resched() to just return 1 all the time. If that causes the kernel to livelock then we need to fix that up anyway.

Less code... not sure how nasty performance the implications are.

-- wli

#7 lore raw
Quoted 2 lines · William Lee Irwin III

I suspect it's better to drop in hooks to trap those e.g. in cond_resched() and cond_resched_lock().

Something like this should do, modulo everything outside kernel/ and mm/.

include/linux/sched.h +2 -0
Index: timing-2.6.7/include/linux/sched.h
===================================================================
@@ -1008,6 +1008,7 @@
 extern void __cond_resched(void);
 static inline void cond_resched(void)
 {
+	touch_preempt_timing();
 	if (need_resched())
 		__cond_resched();
 }
@@ -1022,6 +1023,7 @@
  */
 static inline void cond_resched_lock(spinlock_t * lock)
 {
+	touch_preempt_timing();
 	if (need_resched()) {
 		_raw_spin_unlock(lock);
 		preempt_enable_no_resched();
include/linux/preempt.h +2 -0
Index: timing-2.6.7/include/linux/preempt.h
===================================================================
@@ -12,7 +12,9 @@
 #ifdef CONFIG_PREEMPT_TIMING
 void __inc_preempt_count(void);
 void __dec_preempt_count(void);
+void touch_preempt_timing(void);
 #else
+#define touch_preempt_timing()	do { } while (0)
 #ifdef CONFIG_PREEMPT
 #define __inc_preempt_count()	do { preempt_count()++; } while (0)
 #define __dec_preempt_count()	do { preempt_count()--; } while (0)
arch/i386/kernel/traps.c +15 -5
Index: timing-2.6.7/arch/i386/kernel/traps.c
===================================================================
@@ -969,14 +969,24 @@
 }
 __setup("preempt_thresh=", setup_preempt_thresh);
 
+static void __touch_preempt_timing(void *addr)
+{
+	rdtscll(__get_cpu_var(preempt_timings));
+	__get_cpu_var(preempt_entry) = (unsigned long)addr;
+}
+
+void touch_preempt_timing(void)
+{
+	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING)
+		__touch_preempt_timing(__builtin_return_address(0));
+}
+EXPORT_SYMBOL(touch_preempt_timing);
+
 void __inc_preempt_count(void)
 {
 	preempt_count()++;
-	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING) {
-		rdtscll(__get_cpu_var(preempt_timings));
-		__get_cpu_var(preempt_entry)
-				= (unsigned long)__builtin_return_address(0);
-	}
+	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING)
+		__touch_preempt_timing(__builtin_return_address(0));
 }
 EXPORT_SYMBOL(__inc_preempt_count);
 
mm/memory.c +2 -0
Index: timing-2.6.7/mm/memory.c
===================================================================
@@ -558,6 +558,8 @@
 			zap_bytes -= block;
 			if ((long)zap_bytes > 0)
 				continue;
+			if (!atomic)
+				touch_preempt_timing();
 			if (!atomic && need_resched()) {
 				int fullmm = tlb_is_full_mm(*tlbp);
 				tlb_finish_mmu(*tlbp, tlb_start, start);
kernel/printk.c +2 -0
Index: timing-2.6.7/kernel/printk.c
===================================================================
@@ -650,6 +650,8 @@
  */
 void console_conditional_schedule(void)
 {
+	if (console_may_schedule)
+		touch_preempt_timing();
 	if (console_may_schedule && need_resched()) {
 		set_current_state(TASK_RUNNING);
 		schedule();
#8 lore raw
Quoted 2 lines · William Lee Irwin III
Quoted 2 lines

I suspect it's better to drop in hooks to trap those e.g. in cond_resched() and cond_resched_lock().

Quoted 1 line · William Lee Irwin III

Something like this should do, modulo everything outside kernel/ and mm/.

Okay, finally, it should be possible to genericize this all, since sched_clock() is an arch-independent interface for high-precision timings in units of nanoseconds.

Some cleanups and fixing a minor oversight in touch_preempt_timing(), which was that in principle things can break out of preempt_count() > 1 after checking need_resched(). Cleaner, smaller, yes. How crippled it just got with the various potentially false negatives introduced by using need_resched() as a hook for touch_preempt_timing() is unclear.

Also, the major oversight of nopping out inc_preempt_count() and dec_preempt_count(), which will take out boxen, is corrected here. Happily, some underscore-itis is also removed.

I couldn't think of a better place to put the config option for generic use than under CONFIG_EMBEDDED, otherwise I'd have to drop it in alongside CONFIG_PREEMPT on all arches, as it's basically more generic than CONFIG_PREEMPT in and of itself.

I guess the other things are:
(a) do people want this sysctl-configurable instead of boot-time? I chose boot-time because sysctls seem to introduce more risk of people shooting themselves in the foot.
(b) the damn thing finally boots on my craptop, where else did it break?
(c) everything else and the kitchen sink

-- wli

include/linux/preempt.h +16 -10
Index: timing-2.6.7/include/linux/preempt.h
===================================================================
@@ -9,17 +9,17 @@
 #include <linux/config.h>
 #include <linux/linkage.h>
 
-#define preempt_count()	(current_thread_info()->preempt_count)
-
-#define inc_preempt_count() \
-do { \
-	preempt_count()++; \
-} while (0)
+#ifdef CONFIG_PREEMPT_TIMING
+void inc_preempt_count(void);
+void dec_preempt_count(void);
+void touch_preempt_timing(void);
+#else
+#define touch_preempt_timing()	do { } while (0)
+#define inc_preempt_count()	do { preempt_count()++; } while (0)
+#define dec_preempt_count()	do { preempt_count()--; } while (0)
+#endif
 
-#define dec_preempt_count() \
-do { \
-	preempt_count()--; \
-} while (0)
+#define preempt_count()	(current_thread_info()->preempt_count)
 
 #ifdef CONFIG_PREEMPT
 
@@ -51,9 +51,15 @@
 
 #else
 
+#ifdef CONFIG_PREEMPT_TIMING
+#define preempt_disable()		inc_preempt_count()
+#define preempt_enable_no_resched()	dec_preempt_count()
+#define preempt_enable()		dec_preempt_count()
+#else
 #define preempt_disable()		do { } while (0)
 #define preempt_enable_no_resched()	do { } while (0)
 #define preempt_enable()		do { } while (0)
+#endif
 #define preempt_check_resched()		do { } while (0)
 
 #endif
include/linux/sched.h +2 -1
Index: timing-2.6.7/include/linux/sched.h
===================================================================
@@ -1000,7 +1000,8 @@
 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
 }
   
-static inline int need_resched(void)
+#define need_resched()	({ touch_preempt_timing(); __need_resched(); })
+static inline int __need_resched(void)
 {
 	return unlikely(test_thread_flag(TIF_NEED_RESCHED));
 }
init/Kconfig +8 -0
Index: timing-2.6.7/init/Kconfig
===================================================================
@@ -261,6 +261,14 @@
 	  Disabling this option will cause the kernel to be built without
 	  support for epoll family of system calls.
 
+config PREEMPT_TIMING
+	bool "Non-preemptible critical section timing"
+	default n
+	help
+	  This option measures the time spent in non-preemptible critical
+	  sections and reports warnings when a boot-time configurable
+	  latency threshold is exceeded.
+
 source "drivers/block/Kconfig.iosched"
 
 config CC_OPTIMIZE_FOR_SIZE
kernel/sched.c +71 -0

74 lines not on this page · raw message

#9 lore raw
Quoted 5 lines · William Lee Irwin III

Some cleanups and fixing a minor oversight in touch_preempt_timing(), which was that in principle things can break out of preempt_count() > 1 after checking need_resched(). Cleaner, smaller, yes. How crippled it just got with the various potentially false negatives introduced by using need_resched() as a hook for touch_preempt_timing() is unclear.

Something's gone wrong with using need_resched() as a hook for that. The following, incremental atop the cleaned up patch, backs that out and restores the apparent accuracy of reporting, though there isn't any rigorous correlation of this with other scheduling latency stuff (it should report lower worst-case latencies since it's measuring what are tighter sections of code and ignores waiting time on runqueues).

vs. 2.6.7 (and the prior patch too). I'll probably repost a full patch vs. -mm7 (or whatever's current when I do) in a dedicated thread.

-- wli

include/linux/sched.h +2 -2
Index: timing-2.6.7/include/linux/sched.h
===================================================================
@@ -1000,8 +1000,7 @@
 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
 }
   
-#define need_resched()	({ touch_preempt_timing(); __need_resched(); })
-static inline int __need_resched(void)
+static inline int need_resched(void)
 {
 	return unlikely(test_thread_flag(TIF_NEED_RESCHED));
 }
@@ -1009,6 +1008,7 @@
 extern void __cond_resched(void);
 static inline void cond_resched(void)
 {
+	touch_preempt_timing();
 	if (need_resched())
 		__cond_resched();
 }
kernel/printk.c +2 -4
Index: timing-2.6.7/kernel/printk.c
===================================================================
@@ -650,10 +650,8 @@
  */
 void console_conditional_schedule(void)
 {
-	if (console_may_schedule && need_resched()) {
-		set_current_state(TASK_RUNNING);
-		schedule();
-	}
+	if (console_may_schedule)
+		cond_resched();
 }
 EXPORT_SYMBOL(console_conditional_schedule);
 
mm/memory.c +5 -2
Index: timing-2.6.7/mm/memory.c
===================================================================
@@ -558,14 +558,17 @@
 			zap_bytes -= block;
 			if ((long)zap_bytes > 0)
 				continue;
-			if (!atomic && need_resched()) {
+			zap_bytes = ZAP_BLOCK_SIZE;
+			if (!atomic)
+				continue;
+			touch_preempt_timing();
+			if (need_resched()) {
 				int fullmm = tlb_is_full_mm(*tlbp);
 				tlb_finish_mmu(*tlbp, tlb_start, start);
 				cond_resched_lock(&mm->page_table_lock);
 				*tlbp = tlb_gather_mmu(mm, fullmm);
 				tlb_start_valid = 0;
 			}
-			zap_bytes = ZAP_BLOCK_SIZE;
 		}
 	}
 	return ret;
#10 lore raw
In reply to William Lee Irwin III
Quoted 1 line · William Lee Irwin III

(b) the damn thing finally boots on my craptop, where else did it break?

Con sent me the version he later posted here, and it broke during boot with a preempt_count() of 0xffffff6d which didn't make in_interrupt() very friendly and kmem_cache_create() doesn't like that...

I'll test this new version in a while.

Signature · 1 line
/Martin
#11 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 4 lines · Con Kolivas

He hacked
together this simple patch which times periods according to the preempt count.

OK, small problem. We have code which does, effectively,

Quoted 8 lines
	if (need_resched()) {
		drop_the_lock();
		schedule();
		grab_the_lock();
	}

so if need_resched() stays false then this will hold the lock for a long time and bogus reports are generated:

... or reset the time(r) in need_resched() under the assumption that all need_resched() callers will yield when it returns true...

#12 lore raw
Quoted 2 lines · Andrew Morton
Quoted 2 lines

so if need_resched() stays false then this will hold the lock for a long time and bogus reports are generated:

Quoted 2 lines · Arjan van de Ven

... or reset the time(r) in need_resched() under the assumption that all need_resched() callers will yield when it returns true...

Might be a good enough approximation. Two examples and a counterexample...

-- wli

#13 lore raw
Quoted 2 lines · Arjan van de Ven
Quoted 2 lines

... or reset the time(r) in need_resched() under the assumption that all need_resched() callers will yield when it returns true...

Quoted 1 line · William Lee Irwin III

Might be a good enough approximation. Two examples and a counterexample...

include/linux/sched.h +2 -1
Index: timing-2.6.7/include/linux/sched.h
===================================================================
@@ -1000,7 +1000,8 @@
 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
 }
   
-static inline int need_resched(void)
+#define need_resched()	({ touch_preempt_timing(); __need_resched(); })
+static inline int __need_resched(void)
 {
 	return unlikely(test_thread_flag(TIF_NEED_RESCHED));
 }
include/linux/preempt.h +2 -0
Index: timing-2.6.7/include/linux/preempt.h
===================================================================
@@ -12,7 +12,9 @@
 #ifdef CONFIG_PREEMPT_TIMING
 void __inc_preempt_count(void);
 void __dec_preempt_count(void);
+void touch_preempt_timing(void);
 #else
+#define touch_preempt_timing()	do { } while (0)
 #ifdef CONFIG_PREEMPT
 #define __inc_preempt_count()	do { preempt_count()++; } while (0)
 #define __dec_preempt_count()	do { preempt_count()--; } while (0)
arch/i386/kernel/traps.c +15 -5
Index: timing-2.6.7/arch/i386/kernel/traps.c
===================================================================
@@ -969,14 +969,24 @@
 }
 __setup("preempt_thresh=", setup_preempt_thresh);
 
+static void __touch_preempt_timing(void *addr)
+{
+	rdtscll(__get_cpu_var(preempt_timings));
+	__get_cpu_var(preempt_entry) = (unsigned long)addr;
+}
+
+void touch_preempt_timing(void)
+{
+	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING)
+		__touch_preempt_timing(__builtin_return_address(0));
+}
+EXPORT_SYMBOL(touch_preempt_timing);
+
 void __inc_preempt_count(void)
 {
 	preempt_count()++;
-	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING) {
-		rdtscll(__get_cpu_var(preempt_timings));
-		__get_cpu_var(preempt_entry)
-				= (unsigned long)__builtin_return_address(0);
-	}
+	if (preempt_count() == 1 && system_state == SYSTEM_RUNNING)
+		__touch_preempt_timing(__builtin_return_address(0));
 }
 EXPORT_SYMBOL(__inc_preempt_count);
 
#14 lore raw
In reply to Con Kolivas

what is the difference in interrupt latency with and without your patch.

Regards,

Rajput.

----- Original Message -----
From: "Con Kolivas" <[email protected]>
To: <[email protected]>
Cc: <[email protected]>
Sent: Monday, July 12, 2004 11:59 AM
Subject: [PATCH] Instrumenting high latency

Quoted 1 line

Because of the recent discussion about latency in the kernel I asked

William

Quoted 1 line

Lee Irwin III to help create some instrumentation to determine where in

the

Quoted 1 line

kernel there were still sustained periods of non-preemptible code. He

hacked

Quoted 9 lines

together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

It is enabled via a config option and by setting the threshold at boot by passing the parameter:
preempt_thresh=2

Quoted 6 lines

to set the threshold at 2ms for example.

The output is a warning in syslog like so:

5ms non-preemptible critical section violated 2 ms preempt threshold starting at add_wait_queue+0x21/0x82 and ending at add_wait

_queue+0x4a/0x82

Quoted 6 lines

I would not recommend using this patch for daily use but please try it out on multiple setups/filesystems etc and help us track down the areas. Unfortunately I am not personally capable of fixing the code paths in question so I'll need the help of others in this.

Quoted 9 lines

The patch appears to require CONFIG_PREEMPT enabled on uniprocessor and is i386 only at the moment.

Patch applies to 2.6.8-rc1

Cheers,
Con Kolivas

----------------------------------------------------------------------------
----

Quoted 3 lines

Index: linux-2.6.8-rc1/arch/i386/Kconfig
===================================================================
--- linux-2.6.8-rc1.orig/arch/i386/Kconfig 2004-07-12 16:11:43.531241636

+1000

Quoted 15 lines

+++ linux-2.6.8-rc1/arch/i386/Kconfig 2004-07-12 16:12:45.036678502 +1000

arch/i386/Kconfig +7 -0
@@ -504,6 +504,13 @@
    Say Y here if you are building a kernel for a desktop, embedded
    or real-time system.  Say N if you are unsure.
 
+config PREEMPT_TIMING
+ bool "Non-preemptible critical section timing"
+ help
+   This option measures the time spent in non-preemptible critical
+   sections and reports warnings when a boot-time configurable
+   latency threshold is exceeded.
+
 config X86_UP_APIC
  bool "Local APIC support on uniprocessors" if !SMP
  depends on !(X86_VISWS || X86_VOYAGER)
Quoted 3 lines

Index: linux-2.6.8-rc1/arch/i386/kernel/traps.c
===================================================================
--- linux-2.6.8-rc1.orig/arch/i386/kernel/traps.c 2004-07-12

16:11:43.601230753 +1000

Quoted 1 line

+++ linux-2.6.8-rc1/arch/i386/kernel/traps.c 2004-07-12 16:12:45.037678346

+1000

Quoted 69 lines
arch/i386/kernel/traps.c +65 -0
@@ -947,3 +947,68 @@
 
  trap_init_hook();
 }
+
+#ifdef CONFIG_PREEMPT_TIMING
+
+static int preempt_thresh;
+static DEFINE_PER_CPU(u64, preempt_timings);
+static DEFINE_PER_CPU(unsigned long, preempt_entry);
+static DEFINE_PER_CPU(unsigned long, preempt_exit);
+
+static int setup_preempt_thresh(char *s)
+{
+ int thresh;
+
+ get_option(&s, &thresh);
+ if (thresh > 0) {
+ preempt_thresh = thresh;
+ printk("Preemption threshold = %dms\n", preempt_thresh);
+ }
+ return 1;
+}
+__setup("preempt_thresh=", setup_preempt_thresh);
+
+void __inc_preempt_count(void)
+{
+ preempt_count()++;
+ if (preempt_count() == 1 && system_state == SYSTEM_RUNNING) {
+ rdtscll(__get_cpu_var(preempt_timings));
+ __get_cpu_var(preempt_entry)
+ = (unsigned long)__builtin_return_address(0);
+ }
+}
+
+void __dec_preempt_count(void)
+{
+ if (preempt_count() == 1 && system_state == SYSTEM_RUNNING &&
+ __get_cpu_var(preempt_entry)) {
+ u64 exit;
+ __get_cpu_var(preempt_exit)
+ = (unsigned long)__builtin_return_address(0);
+ rdtscll(exit);
+ if (cpu_khz) {
+ __get_cpu_var(preempt_timings) =
+ exit - __get_cpu_var(preempt_timings);
+ do_div(__get_cpu_var(preempt_timings), cpu_khz);
+ if (__get_cpu_var(preempt_timings) > preempt_thresh &&
+ preempt_thresh) {
+ printk("%lums non-preemptible critical "
+ "section violated %d ms preempt "
+ "threshold starting at ",
+ (unsigned long)
+ __get_cpu_var(preempt_timings),
+ preempt_thresh);
+ print_symbol("%s and ending at ",
+ __get_cpu_var(preempt_entry));
+ print_symbol("%s\n",
+ __get_cpu_var(preempt_exit));
+ dump_stack();
+ }
+ }
+ __get_cpu_var(preempt_exit) = __get_cpu_var(preempt_entry) = 0;
+ }
+ preempt_count()--;
+}
+EXPORT_SYMBOL(__inc_preempt_count);
+EXPORT_SYMBOL(__dec_preempt_count);
+#endif
Quoted 3 lines

Index: linux-2.6.8-rc1/include/linux/preempt.h
===================================================================
--- linux-2.6.8-rc1.orig/include/linux/preempt.h 2004-03-11

21:29:26.000000000 +1100

Quoted 1 line

+++ linux-2.6.8-rc1/include/linux/preempt.h 2004-07-12 16:12:45.055675548

+1000

Quoted 47 lines
include/linux/preempt.h +18 -13
@@ -9,17 +9,22 @@
 #include <linux/config.h>
 #include <linux/linkage.h>
 
-#define preempt_count() (current_thread_info()->preempt_count)
-
-#define inc_preempt_count() \
-do { \
- preempt_count()++; \
-} while (0)
+#ifdef CONFIG_PREEMPT_TIMING
+void __inc_preempt_count(void);
+void __dec_preempt_count(void);
+#else
+#ifdef CONFIG_PREEMPT
+#define __inc_preempt_count() do { preempt_count()++; } while (0)
+#define __dec_preempt_count() do { preempt_count()--; } while (0)
+#else
+#define __inc_preempt_count() do { } while (0)
+#define __dec_preempt_count() do { } while (0)
+#endif
+#endif
 
-#define dec_preempt_count() \
-do { \
- preempt_count()--; \
-} while (0)
+#define preempt_count() (current_thread_info()->preempt_count)
+#define inc_preempt_count() __inc_preempt_count()
+#define dec_preempt_count() __dec_preempt_count()
 
 #ifdef CONFIG_PREEMPT
 
@@ -51,9 +56,9 @@
 
 #else
 
-#define preempt_disable() do { } while (0)
-#define preempt_enable_no_resched() do { } while (0)
-#define preempt_enable() do { } while (0)
+#define preempt_disable() __inc_preempt_count()
+#define preempt_enable_no_resched() __dec_preempt_count()
+#define preempt_enable() __dec_preempt_count()
 #define preempt_check_resched() do { } while (0)
 
 #endif
#15 lore raw
Quoted 3 lines · Rajput

what is the difference in interrupt latency with and without your patch. Regards,
Rajput.

This is meant to instrument scheduling latency, it doesn't improve anything directly. It likely introduces a fair amount of overhead. In the event a poor preempt_thresh= parameter is chosen, it may render systems effectively unusable due to endless printk()'ing (which I should fix via printk_ratelimit() shortly).

I recommend applying and/or enabling this only for diagnostic purposes.

-- wli

#16 lore raw
In reply to Con Kolivas
Quoted 7 lines

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

Is this much different from Rick's schedstat's work, which was itself based on some earlier patches by Bill? I'd hate to end up with two sets of patches, and schedstats seemed pretty comprehensive to me. He's on vacation, but his stuff is here, if you want to take a look:

http://eaglet.rain.com/rick/linux/schedstats/

#17 lore raw
Quoted 9 lines · Martin J. Bligh
Quoted 1 line

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

Is this much different from Rick's schedstat's work, which was itself based on some earlier patches by Bill? I'd hate to end up with two sets of patches, and schedstats seemed pretty comprehensive to me. He's on vacation, but his stuff is here, if you want to take a look:

http://eaglet.rain.com/rick/linux/schedstats/

No I remember his work and this is tackling it via a different area if I recall correctly. He was looking at scheduler latencies as opposed to non-preemptible kernel code.

Con

#18 lore raw

--Con Kolivas <[email protected]> wrote (on Tuesday, July 13, 2004 00:27:27 +1000):

Quoted 15 lines
Quoted 14 lines · Martin J. Bligh
Quoted 7 lines

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

Is this much different from Rick's schedstat's work, which was itself based on some earlier patches by Bill? I'd hate to end up with two sets of patches, and schedstats seemed pretty comprehensive to me. He's on vacation, but his stuff is here, if you want to take a look:

Quoted 5 lines
Quoted 1 line

http://eaglet.rain.com/rick/linux/schedstats/

No I remember his work and this is tackling it via a different area if I recall correctly. He was looking at scheduler latencies as opposed to non-preemptible kernel code.

Fair enough ... is it worth adding it to the same harness though? He had lots of nice analysis tools set up to do comparisons and graphing, etc.

M.

#19 lore raw
Quoted 13 lines · Martin J. Bligh

--Con Kolivas <[email protected]> wrote (on Tuesday, July 13, 2004 00:27:27 +1000):

Quoted 10 lines
Quoted 8 lines · Martin J. Bligh
Quoted 1 line

Because of the recent discussion about latency in the kernel I asked William Lee Irwin III to help create some instrumentation to determine where in the kernel there were still sustained periods of non-preemptible code. He hacked together this simple patch which times periods according to the preempt count. Hopefully we can use this patch in the advice of Linus to avoid the "mental masturbation" at guessing where latency is and track down real problem areas.

Is this much different from Rick's schedstat's work, which was itself based on some earlier patches by Bill? I'd hate to end up with two sets of patches, and schedstats seemed pretty comprehensive to me. He's on vacation, but his stuff is here, if you want to take a look:

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

http://eaglet.rain.com/rick/linux/schedstats/

No I remember his work and this is tackling it via a different area if I recall correctly. He was looking at scheduler latencies as opposed to non-preemptible kernel code.

Fair enough ... is it worth adding it to the same harness though? He had lots of nice analysis tools set up to do comparisons and graphing, etc.

This works very nicely standalone getting us this for example with the fixed patch:

6ms non-preemptible critical section violated 1 ms preempt threshold starting at exit_mmap+0x1c/0x188 and ending at exit_mmap+0x118/0x188

 [<c011d769>] dec_preempt_count+0x14f/0x151
 [<c014d0d4>] exit_mmap+0x118/0x188
 [<c014d0d4>] exit_mmap+0x118/0x188
 [<c011df0a>] mmput+0x61/0x7b
 [<c01226fa>] do_exit+0x142/0x510
 [<c014c928>] unmap_vma_list+0xe/0x17
 [<c0122b8a>] do_group_exit+0x41/0xf9
 [<c0105fd5>] sysenter_past_esp+0x52/0x71

which then an objdump of the inlined code has allowed us to track it down to this:

         profile_exit_mmap(mm);
         lru_add_drain();
c014cfce:       e8 18 72 ff ff          call   c01441eb <lru_add_drain>
         spin_lock(&mm->page_table_lock);

c014cfd3: e8 16 06 fd ff call c011d5ee <inc_preempt_count>

That's pretty specific. I dont think this comes under the umbrella of statistics as such. Sure it can be modified to do it but I was looking for a tool to find where specific latency hotspots still exist. Of course I'm not capable myself of tackling the actual hotspots but those who code those areas certainly can tackle them knowing what firm data shows.

Cheers,
Con

#20 lore raw
Quoted 15 lines

This works very nicely standalone getting us this for example with the fixed patch:

6ms non-preemptible critical section violated 1 ms preempt threshold starting at exit_mmap+0x1c/0x188 and ending at exit_mmap+0x118/0x188

  [<c011d769>] dec_preempt_count+0x14f/0x151
  [<c014d0d4>] exit_mmap+0x118/0x188
  [<c014d0d4>] exit_mmap+0x118/0x188
  [<c011df0a>] mmput+0x61/0x7b
  [<c01226fa>] do_exit+0x142/0x510
  [<c014c928>] unmap_vma_list+0xe/0x17
  [<c0122b8a>] do_group_exit+0x41/0xf9
  [<c0105fd5>] sysenter_past_esp+0x52/0x71

which then an objdump of the inlined code has allowed us to track it down to this:

Quoted 8 lines
          profile_exit_mmap(mm);
          lru_add_drain();
c014cfce:       e8 18 72 ff ff          call   c01441eb <lru_add_drain>
          spin_lock(&mm->page_table_lock);

c014cfd3: e8 16 06 fd ff call c011d5ee <inc_preempt_count>

That's pretty specific. I dont think this comes under the umbrella of statistics as such. Sure it can be modified to do it but I was looking for a tool to find where specific latency hotspots still exist. Of course I'm not capable myself of tackling the actual hotspots but those who code those areas certainly can tackle them knowing what firm data shows.

Guess so. Thought it might be nice to have a measurement of worst case latencies across various functions ... I'll have a play with it, and see if I can come up with something.

M.