Skip to content

Commit

Permalink
btrfs: periodic block_group reclaim
Browse files Browse the repository at this point in the history
We currently employ a edge-triggered block group reclaim strategy which
marks block groups for reclaim as they free down past a threshold.

With a dynamic threshold, this is worse than doing it in a
level-triggered fashion periodically. That is because the reclaim
itself happens periodically, so the threshold at that point in time is
what really matters, not the threshold at freeing time. If we mark the
reclaim in a big pass, then sort by usage and do reclaim, we also
benefit from a negative feedback loop preventing unnecessary reclaims as
we crunch through the "best" candidates.

Since this is quite a different model, it requires some additional
support. The edge triggered reclaim has a good heuristic for not
reclaiming fresh block groups, so we need to replace that with a typical
GC sweep mark which skips block groups that have seen an allocation
since the last sweep.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Boris Burkov authored and David Sterba committed Jul 11, 2024
1 parent f5ff64c commit e4ca393
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fs/btrfs/block-group.c
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)

void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
{
btrfs_reclaim_sweep(fs_info);
spin_lock(&fs_info->unused_bgs_lock);
if (!list_empty(&fs_info->reclaim_bgs))
queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
Expand Down Expand Up @@ -3681,6 +3682,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
old_val += num_bytes;
cache->used = old_val;
cache->reserved -= num_bytes;
cache->reclaim_mark = 0;
space_info->bytes_reserved -= num_bytes;
space_info->bytes_used += num_bytes;
space_info->disk_used += num_bytes * factor;
Expand Down
1 change: 1 addition & 0 deletions fs/btrfs/block-group.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ struct btrfs_block_group {
struct work_struct zone_finish_work;
struct extent_buffer *last_eb;
enum btrfs_block_group_size_class size_class;
u64 reclaim_mark;
};

static inline u64 btrfs_block_group_end(struct btrfs_block_group *block_group)
Expand Down
51 changes: 51 additions & 0 deletions fs/btrfs/space-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -1969,3 +1969,54 @@ int btrfs_calc_reclaim_threshold(struct btrfs_space_info *space_info)
return calc_dynamic_reclaim_threshold(space_info);
return READ_ONCE(space_info->bg_reclaim_threshold);
}

static int do_reclaim_sweep(struct btrfs_fs_info *fs_info,
struct btrfs_space_info *space_info, int raid)
{
struct btrfs_block_group *bg;
int thresh_pct;

spin_lock(&space_info->lock);
thresh_pct = btrfs_calc_reclaim_threshold(space_info);
spin_unlock(&space_info->lock);

down_read(&space_info->groups_sem);
list_for_each_entry(bg, &space_info->block_groups[raid], list) {
u64 thresh;
bool reclaim = false;

btrfs_get_block_group(bg);
spin_lock(&bg->lock);
thresh = mult_perc(bg->length, thresh_pct);
if (bg->used < thresh && bg->reclaim_mark)
reclaim = true;
bg->reclaim_mark++;
spin_unlock(&bg->lock);
if (reclaim)
btrfs_mark_bg_to_reclaim(bg);
btrfs_put_block_group(bg);
}
up_read(&space_info->groups_sem);
return 0;
}

int btrfs_reclaim_sweep(struct btrfs_fs_info *fs_info)
{
int ret;
int raid;
struct btrfs_space_info *space_info;

list_for_each_entry(space_info, &fs_info->space_info, list) {
if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
continue;
if (!READ_ONCE(space_info->periodic_reclaim))
continue;
for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
ret = do_reclaim_sweep(fs_info, space_info, raid);
if (ret)
return ret;
}
}

return ret;
}
7 changes: 7 additions & 0 deletions fs/btrfs/space-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ struct btrfs_space_info {
* fixed bg_reclaim_threshold.
*/
bool dynamic_reclaim;

/*
* Periodically check all block groups against the reclaim
* threshold in the cleaner thread.
*/
bool periodic_reclaim;
};

struct reserve_ticket {
Expand Down Expand Up @@ -273,5 +279,6 @@ void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);

int btrfs_calc_reclaim_threshold(struct btrfs_space_info *space_info);
int btrfs_reclaim_sweep(struct btrfs_fs_info *fs_info);

#endif /* BTRFS_SPACE_INFO_H */
34 changes: 34 additions & 0 deletions fs/btrfs/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,39 @@ BTRFS_ATTR_RW(space_info, dynamic_reclaim,
btrfs_sinfo_dynamic_reclaim_show,
btrfs_sinfo_dynamic_reclaim_store);

static ssize_t btrfs_sinfo_periodic_reclaim_show(struct kobject *kobj,
struct kobj_attribute *a,
char *buf)
{
struct btrfs_space_info *space_info = to_space_info(kobj);

return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->periodic_reclaim));
}

static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj,
struct kobj_attribute *a,
const char *buf, size_t len)
{
struct btrfs_space_info *space_info = to_space_info(kobj);
int periodic_reclaim;
int ret;

ret = kstrtoint(buf, 10, &periodic_reclaim);
if (ret)
return ret;

if (periodic_reclaim < 0)
return -EINVAL;

WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0);

return len;
}

BTRFS_ATTR_RW(space_info, periodic_reclaim,
btrfs_sinfo_periodic_reclaim_show,
btrfs_sinfo_periodic_reclaim_store);

/*
* Allocation information about block group types.
*
Expand All @@ -996,6 +1029,7 @@ static struct attribute *space_info_attrs[] = {
BTRFS_ATTR_PTR(space_info, reclaim_count),
BTRFS_ATTR_PTR(space_info, reclaim_bytes),
BTRFS_ATTR_PTR(space_info, reclaim_errors),
BTRFS_ATTR_PTR(space_info, periodic_reclaim),
#ifdef CONFIG_BTRFS_DEBUG
BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
#endif
Expand Down

0 comments on commit e4ca393

Please sign in to comment.