]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: speed up rmap lookups by using non-overlapped lookups when possible
authorDarrick J. Wong <djwong@kernel.org>
Wed, 22 Jun 2022 19:28:52 +0000 (14:28 -0500)
committerEric Sandeen <sandeen@sandeen.net>
Wed, 22 Jun 2022 19:28:52 +0000 (14:28 -0500)
Source kernel commit: 75d893d19c8e1b4bf4a9acd613fe5e7a80b58974

Reverse mapping on a reflink-capable filesystem has some pretty high
overhead when performing file operations.  This is because the rmap
records for logically and physically adjacent extents might not be
adjacent in the rmap index due to data block sharing.  As a result, we
use expensive overlapped-interval btree search, which walks every record
that overlaps with the supplied key in the hopes of finding the record.

However, profiling data shows that when the index contains a record that
is an exact match for a query key, the non-overlapped btree search
function can find the record much faster than the overlapped version.
Try the non-overlapped lookup first, which will make scrub run much
faster.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
libxfs/xfs_rmap.c

index 0488dcde7a8f3ed2f570d1f330a04ed482e2aced..dcc25d1a93803440a84db6324a3aaa75ae507744 100644 (file)
@@ -366,7 +366,6 @@ xfs_rmap_lookup_le_range_helper(
                return 0;
 
        *info->irec = *rec;
-       *info->stat = 1;
        return -ECANCELED;
 }
 
@@ -387,6 +386,7 @@ xfs_rmap_lookup_le_range(
        int                     *stat)
 {
        struct xfs_find_left_neighbor_info      info;
+       int                     found = 0;
        int                     error;
 
        info.high.rm_startblock = bno;
@@ -399,20 +399,44 @@ xfs_rmap_lookup_le_range(
        info.high.rm_blockcount = 0;
        *stat = 0;
        info.irec = irec;
-       info.stat = stat;
 
-       trace_xfs_rmap_lookup_le_range(cur->bc_mp,
-                       cur->bc_ag.pag->pag_agno, bno, 0, owner, offset, flags);
-       error = xfs_rmap_query_range(cur, &info.high, &info.high,
-                       xfs_rmap_lookup_le_range_helper, &info);
-       if (error == -ECANCELED)
-               error = 0;
-       if (*stat)
-               trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
-                               cur->bc_ag.pag->pag_agno, irec->rm_startblock,
-                               irec->rm_blockcount, irec->rm_owner,
-                               irec->rm_offset, irec->rm_flags);
-       return error;
+       trace_xfs_rmap_lookup_le_range(cur->bc_mp, cur->bc_ag.pag->pag_agno,
+                       bno, 0, owner, offset, flags);
+
+       /*
+        * Historically, we always used the range query to walk every reverse
+        * mapping that could possibly overlap the key that the caller asked
+        * for, and filter out the ones that don't.  That is very slow when
+        * there are a lot of records.
+        *
+        * However, there are two scenarios where the classic btree search can
+        * produce correct results -- if the index contains a record that is an
+        * exact match for the lookup key; and if there are no other records
+        * between the record we want and the key we supplied.
+        *
+        * As an optimization, try a non-overlapped lookup first.  This makes
+        * scrub run much faster on most filesystems because bmbt records are
+        * usually an exact match for rmap records.  If we don't find what we
+        * want, we fall back to the overlapped query.
+        */
+       error = xfs_rmap_lookup_le(cur, bno, owner, offset, flags, irec,
+                       &found);
+       if (error)
+               return error;
+       if (found)
+               error = xfs_rmap_lookup_le_range_helper(cur, irec, &info);
+       if (!error)
+               error = xfs_rmap_query_range(cur, &info.high, &info.high,
+                               xfs_rmap_lookup_le_range_helper, &info);
+       if (error != -ECANCELED)
+               return error;
+
+       *stat = 1;
+       trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
+                       cur->bc_ag.pag->pag_agno, irec->rm_startblock,
+                       irec->rm_blockcount, irec->rm_owner, irec->rm_offset,
+                       irec->rm_flags);
+       return 0;
 }
 
 /*