linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>
Subject: [PATCH 05/10] btrfs: precalculate checksums per leaf once
Date: Thu, 29 Oct 2020 15:27:47 +0100	[thread overview]
Message-ID: <33195f212e58bb0150017b3c1ac6df5c2d8c8dc7.1603981453.git.dsterba@suse.com> (raw)
In-Reply-To: <cover.1603981452.git.dsterba@suse.com>

btrfs_csum_bytes_to_leaves shows up in system profiles, which makes it a
candidate for optimizations. After the 64bit division has been replaced
by shift, there's still a calculation done each time the function is
called: checksums per leaf.

As this is a constanat value for the entire filesystem lifetime, we
can calculate it once at mount time and reuse. This also allows to
reduce the division to 64bit/32bit as we know the constant will always
fit to 32bit type.

Replace the open-coded rounding up with a macro that internally handles
the 64bit division.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/ctree.h       | 1 +
 fs/btrfs/disk-io.c     | 1 +
 fs/btrfs/extent-tree.c | 9 +--------
 3 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 1f2162fc1daa..8c4cd79b2810 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -933,6 +933,7 @@ struct btrfs_fs_info {
 	u32 sectorsize;
 	u32 sectorsize_bits;
 	u32 csum_size;
+	u32 csums_per_leaf;
 	u32 stripesize;
 
 	/* Block groups and devices containing active swapfiles. */
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 25dbdfa8bc4b..f870e252aa37 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3079,6 +3079,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	fs_info->sectorsize = sectorsize;
 	fs_info->sectorsize_bits = ilog2(sectorsize);
 	fs_info->csum_size = btrfs_super_csum_size(disk_super);
+	fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
 	fs_info->stripesize = stripesize;
 
 	/*
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 29ac97248942..81440a0ba106 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -2138,17 +2138,10 @@ static u64 find_middle(struct rb_root *root)
  */
 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
 {
-	u64 csum_size;
-	u64 num_csums_per_leaf;
 	u64 num_csums;
 
-	csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
-	num_csums_per_leaf = div64_u64(csum_size,
-			(u64)btrfs_super_csum_size(fs_info->super_copy));
 	num_csums = csum_bytes >> fs_info->sectorsize_bits;
-	num_csums += num_csums_per_leaf - 1;
-	num_csums = div64_u64(num_csums, num_csums_per_leaf);
-	return num_csums;
+	return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
 }
 
 /*
-- 
2.25.0


  parent reply	other threads:[~2020-10-29 14:29 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-29 14:27 [PATCH 00/10] Sectorsize, csum_size lifted to fs_info David Sterba
2020-10-29 14:27 ` [PATCH 01/10] btrfs: use precalculated sectorsize_bits from fs_info David Sterba
2020-11-02 14:05   ` Qu Wenruo
2020-11-02 14:18     ` Qu Wenruo
2020-11-02 14:31       ` Johannes Thumshirn
2020-11-02 15:08         ` David Sterba
2020-11-02 15:12           ` Johannes Thumshirn
2020-11-02 15:15     ` David Sterba
2020-11-03  9:31   ` David Sterba
2020-10-29 14:27 ` [PATCH 02/10] btrfs: replace div_u64 by shift in free_space_bitmap_size David Sterba
2020-11-02 14:07   ` Qu Wenruo
2020-10-29 14:27 ` [PATCH 03/10] btrfs: replace s_blocksize_bits with fs_info::sectorsize_bits David Sterba
2020-11-02 14:23   ` Qu Wenruo
2020-11-02 15:18     ` David Sterba
2020-10-29 14:27 ` [PATCH 04/10] btrfs: store precalculated csum_size in fs_info David Sterba
2020-10-29 14:27 ` David Sterba [this message]
2020-11-02 14:27   ` [PATCH 05/10] btrfs: precalculate checksums per leaf once Qu Wenruo
2020-11-02 15:24     ` David Sterba
2020-11-02 16:00       ` David Sterba
2020-10-29 14:27 ` [PATCH 06/10] btrfs: use cached value of fs_info::csum_size everywhere David Sterba
2020-11-02 14:28   ` Qu Wenruo
2020-10-29 14:27 ` [PATCH 07/10] btrfs: switch cached fs_info::csum_size from u16 to u32 David Sterba
2020-10-29 14:27 ` [PATCH 08/10] btrfs: remove unnecessary local variables for checksum size David Sterba
2020-10-29 14:43   ` Johannes Thumshirn
2020-10-29 14:27 ` [PATCH 09/10] btrfs: check integrity: remove local copy of csum_size David Sterba
2020-10-29 14:44   ` Johannes Thumshirn
2020-10-29 14:27 ` [PATCH 10/10] btrfs: scrub: remove local copy of csum_size from context David Sterba
2020-10-29 14:45   ` Johannes Thumshirn
2020-10-29 14:54     ` David Sterba
2020-10-29 15:01       ` Johannes Thumshirn
2020-10-29 14:50 ` [PATCH 00/10] Sectorsize, csum_size lifted to fs_info Johannes Thumshirn
2020-10-29 16:25   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=33195f212e58bb0150017b3c1ac6df5c2d8c8dc7.1603981453.git.dsterba@suse.com \
    --to=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).