Skip to content

os/bluestore: implement get_free_extents - #69039

Merged
Naveenaidu merged 12 commits into
ceph:mainfrom
Naveenaidu:wip-naveen-76420-allocator-foreach-interruptible
Aug 19, 2026
Merged

Naveenaidu merged 12 commits into
ceph:mainfrom
Naveenaidu:wip-naveen-76420-allocator-foreach-interruptible

Conversation

@Naveenaidu

@Naveenaidu Naveenaidu commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Tracker: https://tracker.ceph.com/issues/76420

Allocator::foreach() holds the allocator lock for the entire walk over all free extents. On a fragmented OSD this can block for several seconds. BlueStore::MempoolThread::entry() calls Allocator::get_fragmentation_score(), which internally calls foreach() every hour, causing a visible write-latency spike where all OSD writes appear stalled.

This PR introduces two new primitives to break the full-walk lock hold into bounded batches.

  1. get_free_extents(range_begin, range_end, max_count, out) → cursor, A ranged, batched enumeration primitive added to the Allocator interface as a pure-virtual function. Each call:
    • Takes the allocator lock only for the duration of a single batch (up to max_count extents).
    • Returns a resume cursor; callers loop until cursor >= range_end.
    • Appends results to *out in ascending offset order, clipped to [range_begin, range_end).
    • Important points:
      • Each batch observes the allocator state independently. The union of all batches is an approximation, off by at most the extents concurrently allocated or released during enumeration
      • This is acceptable for fragmentation scoring, defrag planning, and emergency discard, but not for allocation decisions.
      • Use foreach() when an exact, consistent snapshot is required.
      • The contract and all caveats are documented on the declaration in Allocator.h.
  2. foreach_interruptible(notify)
    • A non-virtual convenience wrapper on Allocator that drives get_free_extents() in 1024-extent batches over [0, device_size). This is the lock-releasing replacement for foreach() for best-effort consumers.

Implementation Status

get_free_extents is implemented for:

  • AVLAllocator
  • BTreeAllocator
  • BitMapAllocator
  • StupidAllocator
  • HybridAllocator
  • Btree2Allocator

TODO (In follow-up PR)

foreach_interruptible is not yet wired into MempoolThread / get_fragmentation_score(). That wiring will be done in a follow-up once all allocators are covered or a fallback to foreach() for unsupported allocators is in place.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@Naveenaidu Naveenaidu self-assigned this May 21, 2026
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch from 644a71b to 73c4383 Compare May 21, 2026 10:10
Comment thread src/os/bluestore/Allocator.h Resolved
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch 2 times, most recently from adb572a to ff2f690 Compare June 2, 2026 16:54
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch 3 times, most recently from 5dcb735 to e2ba1a2 Compare June 18, 2026 05:18
@github-actions github-actions Bot added the tests label Jun 18, 2026
@Naveenaidu
Naveenaidu marked this pull request as ready for review June 18, 2026 09:15
@Naveenaidu
Naveenaidu requested a review from a team as a code owner June 18, 2026 09:15
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch 3 times, most recently from cafbf4f to b5cb34b Compare June 19, 2026 10:14
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch 2 times, most recently from 505a7a7 to 171ad20 Compare June 25, 2026 14:30
@ifed01
ifed01 self-requested a review June 29, 2026 11:19
@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch from 171ad20 to 9148846 Compare July 15, 2026 08:15
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch 2 times, most recently from 30cf4b2 to d6e5b84 Compare July 15, 2026 10:52
Comment thread src/os/bluestore/Allocator.h Resolved
Comment thread src/os/bluestore/Allocator.h Outdated
* - cursor >= range_end: the window is fully enumerated, stop.
* - cursor < range_end: pass it as range_begin to resume. The cursor is a
* valid resume point but is NOT guaranteed to land on a free extent; it
* may point into allocated space. An extra call that returns zero extents

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extra call that returns zero extents
and advances cursor to range_end is possible when max_count is reached
exactly at the last free extent in the window.

I don't get it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cursor is a
valid resume point but is NOT guaranteed to land on a free extent; it
may point into allocated space. An extra call that returns zero extents
and advances cursor to range_end is possible when max_count is reached
exactly at the last free extent in the window.

Is it intended to mean:

The cursor is a
valid resume point but is NOT guaranteed there are any free extent
in range <cursor, range_end).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is what I meant.

I wrote it to mean that (more as a note to myself): an implementation is allowed to hit max_count right as it closes the very last free extent in the window, without doing a look ahead to check whether anything follows, in which case the next call would just confirm "nothing left" with an empty batch and cursor snapped to range_end.

In all our current implementations (StupidAllocator, AvlAllocator, Btree2Allocator, BitmapAllocator's get_free_extents_internal) each one does do that lookahead, so none of them actually exercises this path today; they all detect "that was the last extent" and return range_end immediately instead of an extra empty round-trip.

I'll simplify the comment, thanks!

Comment thread src/os/bluestore/Allocator.h Resolved
Comment thread src/os/bluestore/Allocator.cc Resolved
Comment thread src/os/bluestore/Btree2Allocator.cc Resolved
Comment thread src/os/bluestore/Btree2Allocator.cc Resolved
Comment thread src/os/bluestore/Btree2Allocator.cc Outdated
size_t ci = 0;
const bool unbounded = (max_count == 0);
while (((it != range_tree.end() && it->first < range_end) || ci < cached.size()) &&
(unbounded || n < max_count)) {

@aclamk aclamk Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add last element to cached {range_end, 1} then ci < cached.size() will be always true.
Another hack. Its up to you if you like it.

Comment thread src/os/bluestore/StupidAllocator.cc Resolved
Comment thread src/os/bluestore/HybridAllocator_impl.h Outdated
}

// Fetch up to max_count from each sub-allocator independently.
// Both return extents sorted by offset; we merge them in O(n+m).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to propose a different algorithmic approach.

  1. take free range from tree -> Astart, Aend
  2. add range <Astart, Aend> to output
  3. take next free range from tree -> Bstart, Bend
  4. ask bitmap for get_free_extents(Aend, Bstart)
  5. A = B, goto 2

There could be some locks to avoid, but I think it should work. If reentrance cost for bitmap.get_free_extents is not too large, it should be faster.
We care for HybridAllocator performance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented the approach!

@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch from d6e5b84 to 6c8c6bd Compare July 21, 2026 09:54
@Naveenaidu

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@Naveenaidu
Naveenaidu requested a review from aclamk August 3, 2026 08:52

size_t free_count =
std::min<size_t>(count_1s(pattern, p), p_end - p);
if (free_count > 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free_count == 0 is only achievable if p == p_end at the beginning of the loop.
But there already is a guarantee that lo < ho.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry do not understand the comment, trying to clarify: were you talking about the case where free_count == 0 also when p == p_end ?

If yes, then I think there is no need to worry about running off the edge of bits while scanning this word because we already have the check of p < p_end even before we check for free_count.

@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch from 6c8c6bd to eadba8a Compare August 10, 2026 10:52
Add three parameterized tests that exercise get_free_extents through the
real allocate()/release() code paths rather than just init_add_free():
- verify free extents don't overlap allocated regions after allocation
- verify released extents become visible in the free set after release
- verify correct free-set accounting under maximum fragmentation created
  by allocating all blocks one-by-one and releasing every other one

Fixes: https://tracker.ceph.com/issues/76420
Signed-off-by: Naveen Naidu <[email protected]>
AvlAllocator::get_free_extents used lower_bound({range_begin, range_begin})
which skips any free extent that starts before range_begin even if it extends
into the query window.  Step back one iterator position before the walk loop
so that straddling extents are found; the existing lo=max(start, range_begin)
clipping already handles trimming the left edge correctly.

Add three adversarial parameterized tests:
- clipping_range_begin: free [0,6b), query [4b,end) must return [4b,2b)
- massive_fragmentation: 5000 alternating free/allocated blocks via cursor walk
- needle_at_end: free space only in the last block of a 10000-block device

Fixes: https://tracker.ceph.com/issues/76420
Signed-off-by: Naveen Naidu <[email protected]>
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-76420-allocator-foreach-interruptible branch from eadba8a to 6ed9e61 Compare August 11, 2026 14:05
@Naveenaidu Naveenaidu changed the title bluestore: implement get_free_extents os/bluestore: implement get_free_extents Aug 13, 2026
@Jayaprakash-ibm

Copy link
Copy Markdown
Contributor

@Naveenaidu
Naveenaidu merged commit 27051e9 into ceph:main Aug 19, 2026
10 of 12 checks passed
@github-actions

Copy link
Copy Markdown

This is an automated message by src/script/redmine-upkeep.py.

I have resolved the following tracker ticket due to the merge of this PR:

No backports are pending for the ticket. If this is incorrect, please update the tracker
ticket and reset to Pending Backport state.

Update Log: https://github.com/ceph/ceph/actions/runs/32239000878

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants