Skip to content

Commit 5280eae

Browse files
jankarakdave
authored andcommitted
btrfs: Limit size of bios submitted from writeback
Currently btrfs_writepages() just accumulates as large bio as possible (within writeback_control constraints) and then submits it. This can however lead to significant latency in writeback IO submission (I have observed tens of milliseconds) because the submitted bio easily has over hundred of megabytes. Consequently this leads to IO pipeline stalls and reduced throughput. At the same time beyond certain size submitting so large bio provides diminishing returns because the bio is split by the block layer immediately anyway. So compute (estimate of) bio size beyond which we are unlikely to improve performance and just submit the bio for writeback once we accumulate that much to keep the IO pipeline busy. This improves writeback throughput for sequential writes by about 15% on the test machine I was using. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 4c76a5e commit 5280eae

5 files changed

Lines changed: 48 additions & 0 deletions

File tree

fs/btrfs/disk-io.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,6 +3587,13 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
35873587
}
35883588
}
35893589

3590+
ret = btrfs_init_writeback_bio_size(fs_info);
3591+
if (ret) {
3592+
btrfs_err(fs_info, "failed to get optimum writeback size: %d",
3593+
ret);
3594+
goto fail_sysfs;
3595+
}
3596+
35903597
btrfs_free_zone_cache(fs_info);
35913598

35923599
btrfs_check_active_zone_reservation(fs_info);

fs/btrfs/extent_io.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,16 @@ static void submit_extent_folio(struct btrfs_bio_ctrl *bio_ctrl,
857857
/* Ordered extent boundary: move on to a new bio. */
858858
if (bio_ctrl->len_to_oe_boundary == 0)
859859
submit_one_bio(bio_ctrl);
860+
/*
861+
* If we have accumulated decent amount of IO, send it to the
862+
* block layer so that IO can run while we are accumulating
863+
* more folios to write.
864+
*/
865+
else if (bio_ctrl->wbc &&
866+
bio_ctrl->bbio->bio.bi_iter.bi_size >=
867+
inode->root->fs_info->writeback_bio_size)
868+
submit_one_bio(bio_ctrl);
869+
860870
} while (size);
861871
}
862872

fs/btrfs/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ struct btrfs_fs_info {
866866
u32 block_min_order;
867867
u32 block_max_order;
868868
u32 stripesize;
869+
u32 writeback_bio_size;
869870
u32 csum_size;
870871
u32 csums_per_leaf;
871872
u32 csum_type;

fs/btrfs/volumes.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8207,6 +8207,35 @@ int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
82078207
return ret;
82088208
}
82098209

8210+
int btrfs_init_writeback_bio_size(struct btrfs_fs_info *fs_info)
8211+
{
8212+
struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
8213+
struct btrfs_device *device;
8214+
u32 writeback_bio_size = fs_info->sectorsize;
8215+
8216+
mutex_lock(&fs_devices->device_list_mutex);
8217+
/*
8218+
* Let's take maximum over optimal request sizes for all devices. For
8219+
* RAID profiles writeback will submit stripe (64k) sized bios anyway
8220+
* so our value doesn't matter and for simple profiles this is a good
8221+
* approximation of sensible IO chunking.
8222+
*/
8223+
list_for_each_entry(device, &fs_devices->devices, dev_list) {
8224+
struct request_queue *queue;
8225+
unsigned int io_opt;
8226+
8227+
queue = bdev_get_queue(device->bdev);
8228+
io_opt = queue_io_opt(queue) ? :
8229+
queue_max_sectors(queue) << SECTOR_SHIFT;
8230+
writeback_bio_size = max(writeback_bio_size, io_opt);
8231+
}
8232+
mutex_unlock(&fs_devices->device_list_mutex);
8233+
8234+
fs_info->writeback_bio_size = writeback_bio_size;
8235+
8236+
return 0;
8237+
}
8238+
82108239
static int update_dev_stat_item(struct btrfs_trans_handle *trans,
82118240
struct btrfs_device *device)
82128241
{

fs/btrfs/volumes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
784784
struct btrfs_ioctl_get_dev_stats *stats);
785785
int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
786786
int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
787+
int btrfs_init_writeback_bio_size(struct btrfs_fs_info *fs_info);
787788
int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
788789
void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
789790
void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);

0 commit comments

Comments
 (0)