diff mbox series

[4/4] btrfs: tests: polish ifdefs around testing helper

Message ID 7a6c4814d8467999d9a9eb12906ff28a3f90b284.1536599879.git.dsterba@suse.com (mailing list archive)
State New, archived
Headers show
Series Self-test cleanups | expand

Commit Message

David Sterba Sept. 10, 2018, 5:22 p.m. UTC
Avoid the inline ifdefs and use two sections for self-tests enabled and
disabled.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/ctree.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Omar Sandoval Sept. 10, 2018, 11:43 p.m. UTC | #1
On Mon, Sep 10, 2018 at 07:22:31PM +0200, David Sterba wrote:
> Avoid the inline ifdefs and use two sections for self-tests enabled and
> disabled.
> 
> Signed-off-by: David Sterba <dsterba@suse.com>
> ---
>  fs/btrfs/ctree.h | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> index 32d2fce4ac53..8dafc7bb6ad8 100644
> --- a/fs/btrfs/ctree.h
> +++ b/fs/btrfs/ctree.h
> @@ -3708,17 +3708,20 @@ static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
>  #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
>  void btrfs_test_inode_set_ops(struct inode *inode);
>  void btrfs_test_destroy_inode(struct inode *inode);
> -#endif
>  
>  static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
>  {
> -#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
>  	if (unlikely(test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
>  			      &fs_info->fs_state)))
>  		return 1;
> -#endif
>  	return 0;

How about just:

	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);

We can probably get away without the unlikely() considering that no one
sane is going to run a kernel with CONFIG_BTRFS_FS_RUN_SANITY_TESTS in
production.
David Sterba Sept. 11, 2018, 9:22 a.m. UTC | #2
On Mon, Sep 10, 2018 at 04:43:29PM -0700, Omar Sandoval wrote:
> On Mon, Sep 10, 2018 at 07:22:31PM +0200, David Sterba wrote:
> > Avoid the inline ifdefs and use two sections for self-tests enabled and
> > disabled.
> > 
> > Signed-off-by: David Sterba <dsterba@suse.com>
> > ---
> >  fs/btrfs/ctree.h | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> > 
> > diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> > index 32d2fce4ac53..8dafc7bb6ad8 100644
> > --- a/fs/btrfs/ctree.h
> > +++ b/fs/btrfs/ctree.h
> > @@ -3708,17 +3708,20 @@ static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
> >  #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> >  void btrfs_test_inode_set_ops(struct inode *inode);
> >  void btrfs_test_destroy_inode(struct inode *inode);
> > -#endif
> >  
> >  static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
> >  {
> > -#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> >  	if (unlikely(test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
> >  			      &fs_info->fs_state)))
> >  		return 1;
> > -#endif
> >  	return 0;
> 
> How about just:
> 
> 	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
> 
> We can probably get away without the unlikely() considering that no one
> sane is going to run a kernel with CONFIG_BTRFS_FS_RUN_SANITY_TESTS in
> production.

The unlikely can go away, sure.

I would still like to remove the test_bit call when tests are compiled
out. There are about 10 calls to btrfs_is_testing in various core
functions, followed by further statements. This would have a
(negligible) runtime penalty but generates effectively unused code on
production builds.

The static inline function returning 0 allows to optimize out the unused
code, so smaller code, fewer inctructions, etc.
Omar Sandoval Sept. 11, 2018, 7:14 p.m. UTC | #3
On Tue, Sep 11, 2018 at 11:22:51AM +0200, David Sterba wrote:
> On Mon, Sep 10, 2018 at 04:43:29PM -0700, Omar Sandoval wrote:
> > On Mon, Sep 10, 2018 at 07:22:31PM +0200, David Sterba wrote:
> > > Avoid the inline ifdefs and use two sections for self-tests enabled and
> > > disabled.
> > > 
> > > Signed-off-by: David Sterba <dsterba@suse.com>
> > > ---
> > >  fs/btrfs/ctree.h | 9 ++++++---
> > >  1 file changed, 6 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> > > index 32d2fce4ac53..8dafc7bb6ad8 100644
> > > --- a/fs/btrfs/ctree.h
> > > +++ b/fs/btrfs/ctree.h
> > > @@ -3708,17 +3708,20 @@ static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
> > >  #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> > >  void btrfs_test_inode_set_ops(struct inode *inode);
> > >  void btrfs_test_destroy_inode(struct inode *inode);
> > > -#endif
> > >  
> > >  static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
> > >  {
> > > -#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> > >  	if (unlikely(test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
> > >  			      &fs_info->fs_state)))
> > >  		return 1;
> > > -#endif
> > >  	return 0;
> > 
> > How about just:
> > 
> > 	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
> > 
> > We can probably get away without the unlikely() considering that no one
> > sane is going to run a kernel with CONFIG_BTRFS_FS_RUN_SANITY_TESTS in
> > production.
> 
> The unlikely can go away, sure.
> 
> I would still like to remove the test_bit call when tests are compiled
> out. There are about 10 calls to btrfs_is_testing in various core
> functions, followed by further statements. This would have a
> (negligible) runtime penalty but generates effectively unused code on
> production builds.
> 
> The static inline function returning 0 allows to optimize out the unused
> code, so smaller code, fewer inctructions, etc.

Absolutely, I just mean that the CONFIG_BTRFS_FS_RUN_SANITY_TESTS
version can be cleaner:

#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
{
	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
}
#else
static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
{
	return 0;
}
#endif

I find `if (1) return 1; else return 0;` really icky.
David Sterba Sept. 14, 2018, 2:20 p.m. UTC | #4
On Tue, Sep 11, 2018 at 12:14:47PM -0700, Omar Sandoval wrote:
> > The unlikely can go away, sure.
> > 
> > I would still like to remove the test_bit call when tests are compiled
> > out. There are about 10 calls to btrfs_is_testing in various core
> > functions, followed by further statements. This would have a
> > (negligible) runtime penalty but generates effectively unused code on
> > production builds.
> > 
> > The static inline function returning 0 allows to optimize out the unused
> > code, so smaller code, fewer inctructions, etc.
> 
> Absolutely, I just mean that the CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> version can be cleaner:
> 
> #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
> {
> 	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
> }
> #else
> static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
> {
> 	return 0;
> }
> #endif
> 
> I find `if (1) return 1; else return 0;` really icky.

I see what you mean, will fix it.
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 32d2fce4ac53..8dafc7bb6ad8 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -3708,17 +3708,20 @@  static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 void btrfs_test_inode_set_ops(struct inode *inode);
 void btrfs_test_destroy_inode(struct inode *inode);
-#endif
 
 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
 {
-#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 	if (unlikely(test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
 			      &fs_info->fs_state)))
 		return 1;
-#endif
 	return 0;
 }
+#else
+static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
+{
+	return 0;
+}
+#endif
 
 static inline void cond_wake_up(struct wait_queue_head *wq)
 {