All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Use bool for boolean flag in printk_once()
@ 2009-08-13 20:48 Roland Dreier
  2009-08-13 21:21 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Roland Dreier @ 2009-08-13 20:48 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

Using the type bool (instead of int) for the __print_once flag in the
printk_once() macro matches the intent of the code better, and allows
the compiler to generate smaller code; eg a typical callsite with gcc
4.3.3 on i386:

add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-6 (-6)
function                                     old     new   delta
static.__print_once                            4       1      -3
get_cpu_vendor                               146     143      -3

Saving 6 bytes of object size per callsite by slightly improving the
readability of the source seems like a win to me.

Signed-off-by: Roland Dreier <rolandd@cisco.com>
---
 include/linux/kernel.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d6320a3..f828ce9 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -249,10 +249,10 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  * Print a one-time message (analogous to WARN_ONCE() et al):
  */
 #define printk_once(x...) ({			\
-	static int __print_once = 1;		\
+	static bool __print_once = true;	\
 						\
 	if (__print_once) {			\
-		__print_once = 0;		\
+		__print_once = false;		\
 		printk(x);			\
 	}					\
 })

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use bool for boolean flag in printk_once()
  2009-08-13 20:48 [PATCH] Use bool for boolean flag in printk_once() Roland Dreier
@ 2009-08-13 21:21 ` Andrew Morton
  2009-08-13 21:33   ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2009-08-13 21:21 UTC (permalink / raw)
  To: Roland Dreier; +Cc: linux-kernel, x86

On Thu, 13 Aug 2009 13:48:26 -0700
Roland Dreier <rdreier@cisco.com> wrote:

> Using the type bool (instead of int) for the __print_once flag in the
> printk_once() macro matches the intent of the code better, and allows
> the compiler to generate smaller code; eg a typical callsite with gcc
> 4.3.3 on i386:
> 
> add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-6 (-6)
> function                                     old     new   delta
> static.__print_once                            4       1      -3
> get_cpu_vendor                               146     143      -3
> 
> Saving 6 bytes of object size per callsite by slightly improving the
> readability of the source seems like a win to me.
> 
> Signed-off-by: Roland Dreier <rolandd@cisco.com>
> ---
>  include/linux/kernel.h |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6320a3..f828ce9 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -249,10 +249,10 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
>   * Print a one-time message (analogous to WARN_ONCE() et al):
>   */
>  #define printk_once(x...) ({			\
> -	static int __print_once = 1;		\
> +	static bool __print_once = true;	\
>  						\
>  	if (__print_once) {			\
> -		__print_once = 0;		\
> +		__print_once = false;		\
>  		printk(x);			\
>  	}					\
>  })

hm, OK,  in trace_recursive_lock() we get:

	cmpl	$0, __print_once.28104(%rip)	#, __print_once
	je	.L719	#,

changed to

	cmpb	$0, __print_once.28104(%rip)	# __print_once
	je	.L719	#,


so the compiler used a byte for the bool.

Interestingly that bool is in section `data'.  I bet printk_once()
should use __read_mostly.

Also, that bool still uses four bytes of storage:

   text    data     bss     dec     hex filename
  22527    1445    6392   30364    769c kernel/trace/ring_buffer.o
  22524    1445    6392   30361    7699 kernel/trace/ring_buffer.o (patched)

I wonder if there's some way in which we could/should cause all these
little random __read_mostly bytes to get packed together somewhere.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use bool for boolean flag in printk_once()
  2009-08-13 21:21 ` Andrew Morton
@ 2009-08-13 21:33   ` Joe Perches
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Perches @ 2009-08-13 21:33 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Roland Dreier, linux-kernel, x86

On Thu, 2009-08-13 at 14:21 -0700, Andrew Morton wrote:
> On Thu, 13 Aug 2009 13:48:26 -0700
> Roland Dreier <rdreier@cisco.com> wrote:
> 
> > Using the type bool (instead of int) for the __print_once flag in the
> > printk_once() macro matches the intent of the code better, and allows
> > the compiler to generate smaller code; eg a typical callsite with gcc
> > 4.3.3 on i386:
> > 
> > add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-6 (-6)
> > function                                     old     new   delta
> > static.__print_once                            4       1      -3
> > get_cpu_vendor                               146     143      -3
> > 
> > Saving 6 bytes of object size per callsite by slightly improving the
> > readability of the source seems like a win to me.
> > 
> > Signed-off-by: Roland Dreier <rolandd@cisco.com>
> > ---
> >  include/linux/kernel.h |    4 ++--
> >  1 files changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> > index d6320a3..f828ce9 100644
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -249,10 +249,10 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
> >   * Print a one-time message (analogous to WARN_ONCE() et al):
> >   */
> >  #define printk_once(x...) ({			\
> > -	static int __print_once = 1;		\
> > +	static bool __print_once = true;	\
> >  						\
> >  	if (__print_once) {			\
> > -		__print_once = 0;		\
> > +		__print_once = false;		\
> >  		printk(x);			\
> >  	}					\
> >  })
> 
> hm, OK,  in trace_recursive_lock() we get:
> 
> 	cmpl	$0, __print_once.28104(%rip)	#, __print_once
> 	je	.L719	#,
> 
> changed to
> 
> 	cmpb	$0, __print_once.28104(%rip)	# __print_once
> 	je	.L719	#,

The test should be inverted so that it's not initialized.

I submitted this once:
http://lkml.indiana.edu/hypermail/linux/kernel/0905.2/02636.html

	static bool printed;
	if (!printed) {
		printed = true;
		printk...
	}



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-13 21:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-13 20:48 [PATCH] Use bool for boolean flag in printk_once() Roland Dreier
2009-08-13 21:21 ` Andrew Morton
2009-08-13 21:33   ` Joe Perches

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.