• Qu Wenruo's avatar
    btrfs: qgroup: Use delayed subtree rescan for balance · f616f5cd
    Qu Wenruo authored
    
    
    Before this patch, qgroup code traces the whole subtree of subvolume and
    reloc trees unconditionally.
    
    This makes qgroup numbers consistent, but it could cause tons of
    unnecessary extent tracing, which causes a lot of overhead.
    
    However for subtree swap of balance, just swap both subtrees because
    they contain the same contents and tree structure, so qgroup numbers
    won't change.
    
    It's the race window between subtree swap and transaction commit could
    cause qgroup number change.
    
    This patch will delay the qgroup subtree scan until COW happens for the
    subtree root.
    
    So if there is no other operations for the fs, balance won't cause extra
    qgroup overhead. (best case scenario)
    Depending on the workload, most of the subtree scan can still be
    avoided.
    
    Only for worst case scenario, it will fall back to old subtree swap
    overhead. (scan all swapped subtrees)
    
    [[Benchmark]]
    Hardware:
    	VM 4G vRAM, 8 vCPUs,
    	disk is using 'unsafe' cache mode,
    	backing device is SAMSUNG 850 evo SSD.
    	Host has 16G ram.
    
    Mkfs parameter:
    	--nodesize 4K (To bump up tree size)
    
    Initial subvolume contents:
    	4G data copied from /usr and /lib.
    	(With enough regular small files)
    
    Snapshots:
    	16 snapshots of the original subvolume.
    	each snapshot has 3 random files modified.
    
    balance parameter:
    	-m
    
    So the content should be pretty similar to a real world root fs layout.
    
    And after file system population, there is no other activity, so it
    should be the best case scenario.
    
                         | v4.20-rc1            | w/ patchset    | diff
    -----------------------------------------------------------------------
    relocated extents    | 22615                | 22457          | -0.1%
    qgroup dirty extents | 163457               | 121606         | -25.6%
    time (sys)           | 22.884s              | 18.842s        | -17.6%
    time (real)          | 27.724s              | 22.884s        | -17.5%
    
    Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    f616f5cd