Skip to content

Commit 2000552

Browse files
fdmananaChris Mason
authored andcommitted
Btrfs: improve free space cache management and space allocation
While under random IO, a block group's free space cache eventually reaches a state where it has a mix of extent entries and bitmap entries representing free space regions. As later free space regions are returned to the cache, some of them are merged with existing extent entries if they are contiguous with them. But others are not merged, because despite the existence of adjacent free space regions in the cache, the merging doesn't happen because the existing free space regions are represented in bitmap extents. Even when new free space regions are merged with existing extent entries (enlarging the free space range they represent), we create chances of having after an enlarged region that is contiguous with some other region represented in a bitmap entry. Both clustered and non-clustered space allocation work by iterating over our extent and bitmap entries and skipping any that represents a region smaller then the allocation request (and giving preference to extent entries before bitmap entries). By having a contiguous free space region that is represented by 2 (or more) entries (mix of extent and bitmap entries), we end up not satisfying an allocation request with a size larger than the size of any of the entries but no larger than the sum of their sizes. Making the caller assume we're under a ENOSPC condition or force it to allocate multiple smaller space regions (as we do for file data writes), which adds extra overhead and more chances of causing fragmentation due to the smaller regions being all spread apart from each other (more likely when under concurrency). For example, if we have the following in the cache: * extent entry representing free space range: [128Mb - 256Kb, 128Mb[ * bitmap entry covering the range [128Mb, 256Mb[, but only with the bits representing the range [128Mb, 128Mb + 768Kb[ set - that is, only that space in this 128Mb area is marked as free An allocation request for 1Mb, starting at offset not greater than 128Mb - 256Kb, would fail before, despite the existence of such contiguous free space area in the cache. The caller could only allocate up to 768Kb of space at once and later another 256Kb (or vice-versa). In between each smaller allocation request, another task working on a different file/inode might come in and take that space, preventing the former task of getting a contiguous 1Mb region of free space. Therefore this change implements the ability to move free space from bitmap entries into existing and new free space regions represented with extent entries. This is done when a space region is added to the cache. A test was added to the sanity tests that explains in detail the issue too. Some performance test results with compilebench on a 4 cores machine, with 32Gb of ram and using an HDD follow. Test: compilebench -D /mnt -i 30 -r 1000 --makej Before this change: intial create total runs 30 avg 69.02 MB/s (user 0.28s sys 0.57s) compile total runs 30 avg 314.96 MB/s (user 0.12s sys 0.25s) read compiled tree total runs 3 avg 27.14 MB/s (user 1.52s sys 0.90s) delete compiled tree total runs 30 avg 3.14 seconds (user 0.15s sys 0.66s) After this change: intial create total runs 30 avg 68.37 MB/s (user 0.29s sys 0.55s) compile total runs 30 avg 382.83 MB/s (user 0.12s sys 0.24s) read compiled tree total runs 3 avg 27.82 MB/s (user 1.45s sys 0.97s) delete compiled tree total runs 30 avg 3.18 seconds (user 0.17s sys 0.65s) Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Chris Mason <clm@fb.com>
1 parent 3c1dbdf commit 2000552

2 files changed

Lines changed: 653 additions & 1 deletion

File tree

‎fs/btrfs/free-space-cache.c‎

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,128 @@ static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
19971997
return merged;
19981998
}
19991999

2000+
static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2001+
struct btrfs_free_space *info,
2002+
bool update_stat)
2003+
{
2004+
struct btrfs_free_space *bitmap;
2005+
unsigned long i;
2006+
unsigned long j;
2007+
const u64 end = info->offset + info->bytes;
2008+
const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2009+
u64 bytes;
2010+
2011+
bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2012+
if (!bitmap)
2013+
return false;
2014+
2015+
i = offset_to_bit(bitmap->offset, ctl->unit, end);
2016+
j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2017+
if (j == i)
2018+
return false;
2019+
bytes = (j - i) * ctl->unit;
2020+
info->bytes += bytes;
2021+
2022+
if (update_stat)
2023+
bitmap_clear_bits(ctl, bitmap, end, bytes);
2024+
else
2025+
__bitmap_clear_bits(ctl, bitmap, end, bytes);
2026+
2027+
if (!bitmap->bytes)
2028+
free_bitmap(ctl, bitmap);
2029+
2030+
return true;
2031+
}
2032+
2033+
static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2034+
struct btrfs_free_space *info,
2035+
bool update_stat)
2036+
{
2037+
struct btrfs_free_space *bitmap;
2038+
u64 bitmap_offset;
2039+
unsigned long i;
2040+
unsigned long j;
2041+
unsigned long prev_j;
2042+
u64 bytes;
2043+
2044+
bitmap_offset = offset_to_bitmap(ctl, info->offset);
2045+
/* If we're on a boundary, try the previous logical bitmap. */
2046+
if (bitmap_offset == info->offset) {
2047+
if (info->offset == 0)
2048+
return false;
2049+
bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2050+
}
2051+
2052+
bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2053+
if (!bitmap)
2054+
return false;
2055+
2056+
i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2057+
j = 0;
2058+
prev_j = (unsigned long)-1;
2059+
for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2060+
if (j > i)
2061+
break;
2062+
prev_j = j;
2063+
}
2064+
if (prev_j == i)
2065+
return false;
2066+
2067+
if (prev_j == (unsigned long)-1)
2068+
bytes = (i + 1) * ctl->unit;
2069+
else
2070+
bytes = (i - prev_j) * ctl->unit;
2071+
2072+
info->offset -= bytes;
2073+
info->bytes += bytes;
2074+
2075+
if (update_stat)
2076+
bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2077+
else
2078+
__bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2079+
2080+
if (!bitmap->bytes)
2081+
free_bitmap(ctl, bitmap);
2082+
2083+
return true;
2084+
}
2085+
2086+
/*
2087+
* We prefer always to allocate from extent entries, both for clustered and
2088+
* non-clustered allocation requests. So when attempting to add a new extent
2089+
* entry, try to see if there's adjacent free space in bitmap entries, and if
2090+
* there is, migrate that space from the bitmaps to the extent.
2091+
* Like this we get better chances of satisfying space allocation requests
2092+
* because we attempt to satisfy them based on a single cache entry, and never
2093+
* on 2 or more entries - even if the entries represent a contiguous free space
2094+
* region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2095+
* ends).
2096+
*/
2097+
static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2098+
struct btrfs_free_space *info,
2099+
bool update_stat)
2100+
{
2101+
/*
2102+
* Only work with disconnected entries, as we can change their offset,
2103+
* and must be extent entries.
2104+
*/
2105+
ASSERT(!info->bitmap);
2106+
ASSERT(RB_EMPTY_NODE(&info->offset_index));
2107+
2108+
if (ctl->total_bitmaps > 0) {
2109+
bool stole_end;
2110+
bool stole_front = false;
2111+
2112+
stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2113+
if (ctl->total_bitmaps > 0)
2114+
stole_front = steal_from_bitmap_to_front(ctl, info,
2115+
update_stat);
2116+
2117+
if (stole_end || stole_front)
2118+
try_merge_free_space(ctl, info, update_stat);
2119+
}
2120+
}
2121+
20002122
int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
20012123
u64 offset, u64 bytes)
20022124
{
@@ -2009,6 +2131,7 @@ int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
20092131

20102132
info->offset = offset;
20112133
info->bytes = bytes;
2134+
RB_CLEAR_NODE(&info->offset_index);
20122135

20132136
spin_lock(&ctl->tree_lock);
20142137

@@ -2028,6 +2151,14 @@ int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
20282151
goto out;
20292152
}
20302153
link:
2154+
/*
2155+
* Only steal free space from adjacent bitmaps if we're sure we're not
2156+
* going to add the new free space to existing bitmap entries - because
2157+
* that would mean unnecessary work that would be reverted. Therefore
2158+
* attempt to steal space from bitmaps if we're adding an extent entry.
2159+
*/
2160+
steal_from_bitmap(ctl, info, true);
2161+
20312162
ret = link_free_space(ctl, info);
20322163
if (ret)
20332164
kmem_cache_free(btrfs_free_space_cachep, info);
@@ -2204,10 +2335,13 @@ __btrfs_return_cluster_to_free_space(
22042335
entry = rb_entry(node, struct btrfs_free_space, offset_index);
22052336
node = rb_next(&entry->offset_index);
22062337
rb_erase(&entry->offset_index, &cluster->root);
2338+
RB_CLEAR_NODE(&entry->offset_index);
22072339

22082340
bitmap = (entry->bitmap != NULL);
2209-
if (!bitmap)
2341+
if (!bitmap) {
22102342
try_merge_free_space(ctl, entry, false);
2343+
steal_from_bitmap(ctl, entry, false);
2344+
}
22112345
tree_insert_offset(&ctl->free_space_offset,
22122346
entry->offset, &entry->offset_index, bitmap);
22132347
}
@@ -3175,6 +3309,7 @@ int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
31753309
map = NULL;
31763310
add_new_bitmap(ctl, info, offset);
31773311
bitmap_info = info;
3312+
info = NULL;
31783313
}
31793314

31803315
bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
@@ -3185,6 +3320,8 @@ int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
31853320
if (bytes)
31863321
goto again;
31873322

3323+
if (info)
3324+
kmem_cache_free(btrfs_free_space_cachep, info);
31883325
if (map)
31893326
kfree(map);
31903327
return 0;
@@ -3259,6 +3396,7 @@ int test_check_exists(struct btrfs_block_group_cache *cache,
32593396
goto have_info;
32603397
}
32613398

3399+
ret = 0;
32623400
goto out;
32633401
}
32643402

0 commit comments

Comments
 (0)