[PATCHv3] Make for_each_cpu_mask a bit smaller

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Alexander van Heukelum
Date: Tuesday, May 13, 2008 - 2:28 am

The for_each_cpu_mask loop is used quite often in the kernel. It
makes use of two functions: first_cpu and next_cpu. This patch
changes for_each_cpu_mask to use only the latter. Because next_cpu
finds the next eligible cpu _after_ the given one, the iteration
variable has to be initialized to -1 and next_cpu has to be
called with this value before the first iteration. An x86_64
defconfig kernel (from sched/latest) is about 2500 bytes smaller
with this patch applied:

   text	   data	    bss	    dec	    hex	filename
6222517	 917952	 749932	7890401	 7865e1	vmlinux.orig
6219922	 917952	 749932	7887806	 785bbe	vmlinux

The same size reduction is seen for defconfig+MAXSMP

   text	   data	    bss	    dec	    hex	filename
6241772	2563968	1492716	10298456	 9d2458	vmlinux.orig
6239211	2563968	1492716	10295895	 9d1a57	vmlinux

Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>

---

On Mon, May 12, 2008, Andreas Schwab wrote:

Indeed, thanks.

This version applies on top of sched/latest.

This version reuses the already-existing api next_cpu instead
of inventing a new one; initializing the iteration counter to
-1 was suggested by Matthew Wilcox. Now with a -1 instead of
an overly carefull ~((typeof(cpu))0). "-1" is properly sign-
extended even if cpu is u64 in a 32-bit environment.

Greetings,
	Alexander

 include/linux/cpumask.h |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 73434e5..c24a556 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -377,10 +377,10 @@ int __any_online_cpu(const cpumask_t *mask);
 #define first_cpu(src)		__first_cpu(&(src))
 #define next_cpu(n, src)	__next_cpu((n), &(src))
 #define any_online_cpu(mask) __any_online_cpu(&(mask))
-#define for_each_cpu_mask(cpu, mask)		\
-	for ((cpu) = first_cpu(mask);		\
-		(cpu) < NR_CPUS;		\
-		(cpu) = next_cpu((cpu), (mask)))
+#define for_each_cpu_mask(cpu, mask)			\
+	for ((cpu) = -1;				\
+		(cpu) = next_cpu((cpu), (mask)),	\
+		(cpu) < NR_CPUS; )
 #endif
 
 #if NR_CPUS <= 64
@@ -394,10 +394,10 @@ int __any_online_cpu(const cpumask_t *mask);
 int __next_cpu_nr(int n, const cpumask_t *srcp);
 #define next_cpu_nr(n, src)	__next_cpu_nr((n), &(src))
 #define cpus_weight_nr(cpumask)	__cpus_weight(&(cpumask), nr_cpu_ids)
-#define for_each_cpu_mask_nr(cpu, mask)		\
-	for ((cpu) = first_cpu(mask);		\
-		(cpu) < nr_cpu_ids;		\
-		(cpu) = next_cpu_nr((cpu), (mask)))
+#define for_each_cpu_mask_nr(cpu, mask)			\
+	for ((cpu) = -1;				\
+		(cpu) = next_cpu_nr((cpu), (mask)),	\
+		(cpu) < nr_cpu_ids; )
 
 #endif /* NR_CPUS > 64 */
 
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] Make for_each_cpu_mask a bit smaller, Alexander van Heukelum, (Sun May 11, 6:50 am)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Paul Jackson, (Sun May 11, 6:57 am)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Paul Jackson, (Sun May 11, 7:14 am)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Matthew Wilcox, (Sun May 11, 8:24 am)
[RFC/PATCH] Make for_each_node_mask out-of-line, Alexander van Heukelum, (Sun May 11, 9:06 am)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Alexander van Heukelum, (Sun May 11, 9:19 am)
Re: [RFC/PATCH] Make for_each_node_mask out-of-line, Paul Jackson, (Sun May 11, 2:01 pm)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Matthew Wilcox, (Sun May 11, 3:01 pm)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Alexander van Heukelum, (Mon May 12, 4:04 am)
Re: [PATCH] Make for_each_cpu_mask a bit smaller, Matthew Wilcox, (Mon May 12, 4:56 am)
Re: [RFC/PATCH] Make for_each_node_mask out-of-line, Alexander van Heukelum, (Mon May 12, 5:04 am)
Re: [RFC/PATCH] Make for_each_node_mask out-of-line, Mike Travis, (Mon May 12, 9:45 am)
[PATCHv2] Make for_each_cpu_mask a bit smaller, Alexander van Heukelum, (Mon May 12, 12:00 pm)
Re: [PATCHv2] Make for_each_cpu_mask a bit smaller, Andreas Schwab, (Mon May 12, 2:45 pm)
[PATCHv3] Make for_each_cpu_mask a bit smaller, Alexander van Heukelum, (Tue May 13, 2:28 am)
Re: [PATCHv3] Make for_each_cpu_mask a bit smaller, Ingo Molnar, (Tue May 13, 5:02 am)