Skip to contentSkip to conversation

Profile likely/unlikely macros

PATCH In review Daniel Walker 8 participants 12 messages Apr 25, 2006 – May 1, 2006

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

PATCH 1/1 Profile likely/unlikely macros

This patch adds a very lightweight profiling feature to the likely/unlikely macros. It should work in all contexts including NMI, and during boot. The patch is for 2.6.17-rc2 .

It has a /proc/likely_prof interface which outputs something like the following,

Likely Profiling Results
--------------------------------------------------------------------

[+- ] Type | # True | # False | Function:Filename@Line
 unlikely |        0|        3  tcp_sacktag_write_queue()@:net/ipv4/tcp_input.c@1215
 unlikely |        0|        3  tcp_sacktag_write_queue()@:net/ipv4/tcp_input.c@1214
 unlikely |        0|        3  tcp_sacktag_write_queue()@:net/ipv4/tcp_input.c@1213
+unlikely |     5050|        0  do_sys_vm86()@:arch/i386/kernel/vm86.c@314
 unlikely |        0|     5050  do_sys_vm86()@:arch/i386/kernel/vm86.c@305
+unlikely |    10100|        0  load_esp0()@:include/asm/processor.h@498
+unlikely |    10100|        0  load_esp0()@:include/asm/processor.h@498
-likely   |        0|     1945  audit_free()@:kernel/auditsc.c@715

You can identify which statments are miss labeled from the + or - in front .

include/linux/compiler.h +32 -2
Index: linux-2.6.16/include/linux/compiler.h
===================================================================
@@ -53,14 +53,44 @@ extern void __chk_io_ptr(void __iomem *)
 # include <linux/compiler-intel.h>
 #endif
 
+#ifdef CONFIG_PROFILE_LIKELY
+struct likeliness {
+	const char *func;
+	char *file;
+	int line;
+	int type;
+	unsigned int count[2]; 
+	struct likeliness *next;
+};
+
+extern int do_check_likely(struct likeliness *likeliness, int exp);
+
+# define LIKELY_UNSEEN	4
+
+# define __check_likely(exp, is_likely)							\
+	({										\
+		static __attribute__((__section__(".likely.data")))			\
+			struct likeliness likeliness = {				\
+				.func = __func__,					\
+				.file = __FILE__,					\
+				.line = __LINE__,					\
+				.type = is_likely | LIKELY_UNSEEN,			\
+			};								\
+		do_check_likely(&likeliness, !!(exp));					\
+	})
+
+# define likely(x)	__check_likely(x, 1)
+# define unlikely(x)	__check_likely(x, 0)
+#else
 /*
  * Generic compiler-dependent macros required for kernel
  * build go below this comment. Actual compiler/compiler version
  * specific implementations come from the above header files
  */
 
-#define likely(x)	__builtin_expect(!!(x), 1)
-#define unlikely(x)	__builtin_expect(!!(x), 0)
+# define likely(x)	__builtin_expect(!!(x), 1)
+# define unlikely(x)	__builtin_expect(!!(x), 0)
+#endif
 
 /* Optimization barrier */
 #ifndef barrier
lib/Kconfig.debug +7 -0
Index: linux-2.6.16/lib/Kconfig.debug
===================================================================
@@ -223,3 +223,10 @@ config RCU_TORTURE_TEST
 	  at boot time (you probably don't).
 	  Say M if you want the RCU torture tests to build as a module.
 	  Say N if you are unsure.
+config	PROFILE_LIKELY 
+	bool "Record return values from likely/unlikely macros"
+	default n
+	help
+	  Adds profiling on likely/unlikly macros . To see the
+	  results of the profiling you can view the following,
+		/proc/likely_prof
lib/Makefile +2 -0
Index: linux-2.6.16/lib/Makefile
===================================================================
@@ -49,6 +49,8 @@ obj-$(CONFIG_TEXTSEARCH_FSM) += ts_fsm.o
 
 obj-$(CONFIG_SWIOTLB) += swiotlb.o
 
+obj-$(CONFIG_PROFILE_LIKELY) += likely_prof.o
+
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
 
lib/likely_prof.c new file +134 -0

134 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Daniel Walker

+ if (likeliness->type & LIKELY_UNSEEN) {
+ if (atomic_dec_and_test(&likely_lock)) {
+ if (likeliness->type & LIKELY_UNSEEN) {
+ likeliness->type &= (~LIKELY_UNSEEN);
+ likeliness->next = likeliness_head;
+ likeliness_head = likeliness;
+ }
+ }
+ atomic_inc(&likely_lock);

hm, good enough I guess. It does need a comment explaining why we don't just do spin_lock().

It'd be a bit saner to do

	if (!test_and_set_bit(&foo, 0)) {
		...
		clear_bit(&foo, 0);
	}
#2 lore raw
Quoted 15 lines · Andrew Morton
Quoted 10 lines · Daniel Walker

+ if (likeliness->type & LIKELY_UNSEEN) {
+ if (atomic_dec_and_test(&likely_lock)) {
+ if (likeliness->type & LIKELY_UNSEEN) {
+ likeliness->type &= (~LIKELY_UNSEEN);
+ likeliness->next = likeliness_head;
+ likeliness_head = likeliness;
+ }
+ }
+ atomic_inc(&likely_lock);

hm, good enough I guess. It does need a comment explaining why we don't just do spin_lock().

Quoted 6 lines

It'd be a bit saner to do

	if (!test_and_set_bit(&foo, 0)) {
		...
		clear_bit(&foo, 0);
	}

Ok .. Also a mistaken "define" in the compiler.h .. I'll fix that as well ..

Daniel

#3 lore raw
In reply to Andrew Morton
Quoted 15 lines · Andrew Morton

lib/likely_prof.c line 42

Quoted 9 lines · Daniel Walker
lib/likely_prof.c new file +9 -0
+	if (likeliness->type & LIKELY_UNSEEN) {
+		if (atomic_dec_and_test(&likely_lock)) {
+			if (likeliness->type & LIKELY_UNSEEN) {
+				likeliness->type &= (~LIKELY_UNSEEN);
+				likeliness->next = likeliness_head;
+				likeliness_head = likeliness;
+			}
+		}
+		atomic_inc(&likely_lock);

hm, good enough I guess. It does need a comment explaining why we don't just do spin_lock().

I guess it is so it can be used in NMIs and interrupts without turning interrupts off (so is somewhat lightweight).

But please Daniel, just use spinlocks and trylock. This is buggy because it doesn't get the required release consistency correct.

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#4 lore raw
Quoted 19 lines
Quoted 15 lines · Andrew Morton

lib/likely_prof.c line 42

Quoted 9 lines · Daniel Walker
lib/likely_prof.c new file +9 -0
+	if (likeliness->type & LIKELY_UNSEEN) {
+		if (atomic_dec_and_test(&likely_lock)) {
+			if (likeliness->type & LIKELY_UNSEEN) {
+				likeliness->type &= (~LIKELY_UNSEEN);
+				likeliness->next = likeliness_head;
+				likeliness_head = likeliness;
+			}
+		}
+		atomic_inc(&likely_lock);

hm, good enough I guess. It does need a comment explaining why we don't just do spin_lock().

Quoted 5 lines

I guess it is so it can be used in NMIs and interrupts without turning interrupts off (so is somewhat lightweight).

But please Daniel, just use spinlocks and trylock. This is buggy because it doesn't get the required release consistency correct.

Could you elaborate a bit what's wrong here? (memory barriers, etc? What about the test_and_set_bit() thing Andrew suggested?)

Trylock is a bit more dirty because we need to avoid recursion (it used likely/unlikely too). While there are ways to work around it, atomic operations seem to be cleaner.

#5 lore raw
In reply to Nick Piggin
Quoted 5 lines · Nick Piggin

I guess it is so it can be used in NMIs and interrupts without turning interrupts off (so is somewhat lightweight).

But please Daniel, just use spinlocks and trylock. This is buggy because it doesn't get the required release consistency correct.

To use spinlock we would need to used the __raw_ types . As Hua explained all of the vanilla spinlock calls use the unlikely macro. The result is that we end up using atomic operations. So using them directly seems like the cleanest method .

I'm not exactly sure what you mean by "release consistency" ?

Daniel

#6 lore raw
Quoted 11 lines · Daniel Walker
Quoted 6 lines · Nick Piggin

I guess it is so it can be used in NMIs and interrupts without turning interrupts off (so is somewhat lightweight).

But please Daniel, just use spinlocks and trylock. This is buggy because it doesn't get the required release consistency correct.

Quoted 5 lines

To use spinlock we would need to used the __raw_ types . As Hua explained all of the vanilla spinlock calls use the unlikely macro. The result is that we end up using atomic operations. So using them directly seems like the cleanest method .

Ah, I see. Then you should be OK with either your current scheme, or Andrew's suggestion, so long as you have a memory barrier before the unlock (eg. smp_mb__before_clear_bit()).

Quoted 3 lines

I'm not exactly sure what you mean by "release consistency" ?

Without a barrier, the stores to the linked list may be visible to another CPU after the store that unlocks the atomic_t. Ie. the critical section can leak out of the lock.

--

Send instant messages to your online friends http://au.messenger.yahoo.com

#7 lore raw
Quoted 11 lines · Nick Piggin

Daniel Walker wrote:

Ah, I see. Then you should be OK with either your current scheme, or Andrew's suggestion, so long as you have a memory barrier before the unlock (eg. smp_mb__before_clear_bit()).

Quoted 1 line

I'm not exactly sure what you mean by "release consistency" ?

Without a barrier, the stores to the linked list may be visible to another CPU after the store that unlocks the atomic_t. Ie. the critical section can leak out of the lock.

Admitted, I'm a bit slow at times. But why does this matter? According to my fairly limited brain, you take a potentially expensive barrier, so you pay with a bit of runtime. What you buy is a smaller critical section, so you can save some runtime on other cpus. When optimizing for the common case, which is one cpu, this is a net loss.

There must be some correctness issue hidden that I cannot see. Can you explain that to me?

Jörn

Signature · 2 lines
A victorious army first wins and then seeks battle.
-- Sun Tzu
#8 lore raw
Quoted 8 lines · Jörn Engel

Admitted, I'm a bit slow at times. But why does this matter? According to my fairly limited brain, you take a potentially expensive barrier, so you pay with a bit of runtime. What you buy is a smaller critical section, so you can save some runtime on other cpus. When optimizing for the common case, which is one cpu, this is a net loss.

There must be some correctness issue hidden that I cannot see. Can you explain that to me?

Another CPU may find the bit clear, enter the critical section, and load the old `likeliness_head' (value before being changed by this CPU).

Then it stores the old value to likeliness->next, and overwrites likeliness_head.

One CPU's update has now gotten lost.

(there are probably other examples, like missing likliness->type)

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#9 lore raw
In reply to Nick Piggin

Nick Piggin wrote on Tuesday, April 25, 2006 3:11 AM

Quoted 17 lines
Quoted 15 lines · Andrew Morton

lib/likely_prof.c line 42

Quoted 9 lines · Daniel Walker
lib/likely_prof.c new file +9 -0
+	if (likeliness->type & LIKELY_UNSEEN) {
+		if (atomic_dec_and_test(&likely_lock)) {
+			if (likeliness->type & LIKELY_UNSEEN) {
+				likeliness->type &= (~LIKELY_UNSEEN);
+				likeliness->next = likeliness_head;
+				likeliness_head = likeliness;
+			}
+		}
+		atomic_inc(&likely_lock);

hm, good enough I guess. It does need a comment explaining why we don't just do spin_lock().

Quoted 5 lines

I guess it is so it can be used in NMIs and interrupts without turning interrupts off (so is somewhat lightweight).

But please Daniel, just use spinlocks and trylock. This is buggy because it doesn't get the required release consistency correct.

It looks to me that there is really no need to construct a linked list for "unseen" likely/unlikely usage. The information is already in the struct "likeliness". do_check_likely always increment one of the counter, so if both count values are zero, you know it is "unseen" and can be skipped while printing the profile. So you can get rid of all that code by beefing up seq_next().

- Ken

#10 lore raw
In reply to Daniel Walker

Hi,

Quoted 3 lines · Daniel Walker

This patch adds a very lightweight profiling feature to the likely/unlikely macros. It should work in all contexts including NMI, and during boot. The patch is for 2.6.17-rc2 .

Wow, very nice! I had actually had that idea myself, so it's nice to see it already implemented, and with very nice output.
Rest assured that I'll do my fair share of testing with it!

Thanks a lot!

Andreas Mohr

#11 lore raw
In reply to Daniel Walker

Daniel> It has a /proc/likely_prof interface which outputs Daniel> something like the following,

If we're thinking of applying this to mainline then probably this file should be in debugfs...

- R.