Skip to content

os/bluestore: weight buffer cache shard limits by occupancy - #73018

Open
ming1 wants to merge 3 commits into
ceph:mainfrom
ming1:wip-82021-cache-shard-limits
Open

ming1 wants to merge 3 commits into
ceph:mainfrom
ming1:wip-82021-cache-shard-limits

Conversation

@ming1

@ming1 ming1 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

_resize_shards() gives every buffer cache shard the same limit, but PGs map to shards by pg seed % shards, so the loaded shards trim on every insert while the light ones never fill: the data cache stops well below its commit (574 of 816 MiB on one OSD, 85 % hit rate on a hot set that fits).

This PR splits the data cache budget in proportion to what each shard holds (floor 1/8 of the even share, option bluestore_cache_data_shard_weighted, default true), and changes what the data cache asks the autotuner for: by age only the fraction an even split would have held, the rest at LAST, so the commit grows only into memory no cache claimed by age and never drains the onode or RocksDB caches.

Carries the one commit of #73570 (common/PriorityCache: credit grants made at the last priority) as its first commit, so it can merge before or after that PR; the tuner fix is needed here because without it a request served at LAST is taken from the pool but credited to no cache, the data cache would come out below the even split.

Measurements

Hot-set test of the tracker: 4-OSD vstart, osd_memory_target 2 GiB, osd_memory_base 256 MiB, meta/kv ratios 0.30, 128 PGs, 3 GiB hot region of an RBD image, 4K random reads, 120 s pass, two reps.

2026-11-06, two reps each, same binary tree, stock vs this branch. IOPS and p99 are rep 1 / rep 2; the cache columns are from the end of rep 1 (rep 2 differs by at most 1 MiB in held and by 8 MiB in osd.0's commit, 816):

IOPS p99 ms data committed, MiB (4 OSDs) data held, MiB disk reads per pass, MiB
stock 58.0k / 49.2k 4.6 / 5.2 688 / 768 / 712 / 816 375 / 512 / 419 / 590 1708–3708
this PR 108.9k / 109.6k 1.0 / 1.0 808 / 944 / 944 / 1048 568 / 776 / 780 / 948 5–338

The four held values of the PR sum to 3072 MiB, the whole hot region split by primaries. The commit above held is the data cache's ratio share of memory no cache asked for, plus the 64 MiB chunk headroom; the accounting identity pool − reserve = Σ grants holds to the byte on every OSD.

The drain concern from Adam's 2026-11-03 review

Point 2 of the review: a cache whose shards fill their commit asks for more at every balance, gets it back by ratio, fills that too, and "asymptotically ... drain[s] other caches from space". This version closes the loop by construction: by age the data cache asks for what an even split would have held, the same as today, so it cannot get back more than it chipped in; only the surplus is asked at LAST, served after every cache's binned content and only from memory nobody claimed by age.

Measured where a drain would show, a pool with nothing spare (2026-11-06, this branch): 4 OSDs, osd_memory_target 1 GiB (pool 614 MiB, fully claimed by age bins in every phase), 606k 4 KiB objects under a stat storm plus 4K random reads of a sparse 4 GiB image; option on at runtime, then off; 6-minute phases, means over the last 120 s (MiB, 4 OSDs):

data committed data held onode cache kv kv_onode onodes buffer hit %
off 106 / 96 / 124 / 124 13 / 21 / 23 / 58 158 / 170 / 147 / 148 171 / 160 / 180 / 148 172 / 180 / 156 / 184 46k / 63k / 33k / 61k 2–3
on 132 / 117 / 168 / 149 118 / 106 / 151 / 139 160 / 157 / 136 / 137 148 / 152 / 148 / 141 168 / 179 / 152 / 181 68k / 89k / 41k / 73k 6–23
off again 92 / 84 / 120 / 124 12 / 18 / 22 / 58 184 / 196 / 156 / 168 160 / 152 / 172 / 148 172 / 172 / 156 / 168 69k / 90k / 41k / 78k 2–3
  • No ratchet. osd.0's commit after switch-on: 104 → 124 → 144 at +9 s, then 140, 136, 132 for the rest of the phase. The review's model predicts monotonic growth.
  • The shards fill the commit (held 13–58 → 106–151) for a commit 21 to 44 MiB larger.
  • The other caches keep their content. Onode cache −11 to +2 MiB with onode counts rising; kv and kv_onode within 32 MiB and far from capacity (RocksDB block cache using 16–23 of 148–180). The gain came only from the LAST grants (data 0–33 → 15–72, onode cache 33–58 → 13–41, content older than 12 minutes), never from an age bin.
  • Switching off restores the old state within a minute.

Caveat: no other cache was capacity-bound here, so there was little to lose. When one really holds more than its commit, the LAST split is by ratio (0.36 against the onode cache's 0.30) and costs it part of its cold content; whether the data cache should take only unclaimed memory at LAST is open for discussion.

4K random and 4M sequential writes unchanged within noise.

Tests

unittest_bluestore_types --gtest_filter='bluestore_cache_shards.*': 11 tests on the pure functions (weighted_limits, even_split_fraction) and on the request path through the real PriorityCache::Manager with fake caches.

Fixes: https://tracker.ceph.com/issues/82021

Checklist

  • Tracker (select at least one)
    • References tracker ticket
  • Component impact
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation (bluestore-config-ref.rst, PendingReleaseNotes)
  • Tests (select at least one)
    • Includes unit test(s) (unittest_bluestore_types, bluestore_cache_shards.*)
    • Includes bug reproducer (attached to the tracker)

@dvanders dvanders left a comment

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.

Thanks @ming1, nice find. I reproduced it.

Benchmark: vstart, 6 OSDs, local NVMe, a 3 GiB rbd image as the hot set, fio with the rbd engine, caches dropped first. Base vs this PR, interleaved, two runs each; values are means. 4K random read, second (warm) 60 s pass, and 4 MiB sequential read:

osd_memory_target base this PR
2 GiB 4K random read 49,375 IOPS, p99 1.21 ms 62,021 IOPS, p99 1.00 ms (+26%)
data cache hit rate 39% 97%
data cache per OSD (used / committed) 315 MiB (79% of 385) 525 MiB (89% of 565)
4M sequential read 3.9 GiB/s 5.1 GiB/s (+31%)
4 GiB 4K random read 54,100 IOPS 62,789 IOPS (+16%)
data cache hit rate 63% 100%
4M sequential read 4.8 GiB/s 5.1 GiB/s

The data cache stalls at 69–79% of its committed size without the patch, as you describe. With it, the hot set fits, and PriorityCache even commits more to the data cache because it's being used. My gain is smaller than yours (+16–26% vs 19×) since my baseline wasn't disk-bound, but the direction and the hit rates match.

I went through the maths: limits sum to at most data_alloc, every shard keeps its ⅛ floor, a uniformly full OSD converges back to the even split, and a shard below its even share grows back by about ⅛ of a share per resize, so nothing starves. The lock-free _get_bytes() read matches DataCache::_get_used_bytes().

A few suggestions, none blocking:

  • Could shard_limit become a small static function with a unit test (sum ≤ alloc, even split when nothing is held, the floor, an evenly loaded OSD unchanged, no overflow past 4 GiB)? The tracker reproducer is good evidence but isn't a regression test.
  • Onode shards are split evenly by the same PG mapping (max_shard_onodes), so they may have the same imbalance. Worth a follow-up.
  • A dout(20) of the per-shard limits would make this observable in the field.

@ming1 ming1 added the needs-qa label Oct 26, 2026
@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath9-testing-20261027.065249-main.

@aclamk

aclamk commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the problem of unused cache, which by itself is an 100% admirable goal.

I believe in many conditions it can degrade performance. It should get the config parameter (on/off) and a documentation what the change is.

I am not objecting to the feature.

Hand-waving explanation of how this change can degrade performance.

Current state
Imagine budget of 100 that is allocated to 4 shards equally: 25/25/25/25.
The actual utilization of shards is 40/30/20/10.
The trim process keeps it at 25/25/20/10.
In the next iteration of mempool PriorityCacheManager will recalculate memory use and conclude that cache needs only 80.
It would then allocate shards: 20/20/20/20.
It would, but there are other caches following simple equal divide scheme and they are also have some underutilized shards. All together all caches chip in for "unutilized budget" that PriorityCacheManager can redistribute back.
It is usually more or less what was missing ~20.
So we are back to budget of 100.

New variant
Imagine budget of 100 that is allocated to 4 shards equally: 25/25/25/25.
The actual utilization of shards is 40/30/20/10.
The trim process keeps it at 25/25/20/10.
In the next iteration of mempool PriorityCacheManager will recalculate memory use and conclude that cache needs only 80.
It chips in extra ~20.
We now split proportionally ~31/~31/15/~12.
Trimmed to utilization we get 31/30/15/10.
Now the cache needs 86.
This feels good, we are improving!
But the utilization of other caches has not changed, so our cache will chip in 14 this time, but get back proportionally, which will be more than 14.
I believe this cache would asymptotically adapt to the load in the shards and in long term will drain other caches from space.

Insensitivity to change
Imagine that with budget 100 the cache has reached stable 40/20/20/20.
It was because shard 0 was so much more utilized, for example work pressure was 50/25/25/25.
Now the work pressure has changed and is 50/25/25/100.
Because cache is hard-capped, the observed sizes are still 40/20/20/20.
We will not know that the cache should retune to 25/12/12/50.

(Please bear in mind that I am recalling all algorithms from memory, I welcome corrections.)

@aclamk
aclamk self-requested a review November 3, 2026 19:14

@aclamk aclamk left a comment

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.

Please add configuration parameter controlling the feature.

@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 35e5088 to ab36bcc Compare November 4, 2026 00:30
@ming1
ming1 requested a review from a team as a code owner November 4, 2026 00:30
@ming1

ming1 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@aclamk thanks for the review.

It should get the config parameter (on/off) and a documentation what the change is.

Added bluestore_cache_data_shard_weighted (bool, runtime) with a paragraph under Automatic Cache Sizing; false gives today's even split.

I believe this cache would asymptotically adapt to the load in the shards and in long term will drain other caches from space.

You are right, I measured it. 4-OSD vstart, rbd 4K random reads plus rados bench rand on 1.4M small objects, two runs each:

osd_memory_target stock this PR
2 GiB rbd IOPS 26.2k / 22.9k 26.7k / 25.3k
rados IOPS 12.6k / 11.1k 11.4k / 9.2k
1.25 GiB rbd IOPS 23.9k / 21.3k 23.7k / 21.2k
rados IOPS 11.1k / 10.5k 11.2k / 10.0k

With memory to spare, the data cache takes what it used to leave unused and meta loses about a quarter of its onodes; under contention both are the same.

Because cache is hard-capped, the observed sizes are still 40/20/20/20.
We will not know that the cache should retune to 25/12/12/50.

The split does not freeze: a shard below its even share always gets a limit above what it holds (limit >= even/8 + 7/8 * held), so it grows every resize. Modelling your example, it is back to 25/25/25/25 within about 30 resizes (1.5 s). What it cannot do is go above its even share while the other shards stay full, same as today.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Config Diff Tool Output

+ added: bluestore_cache_data_shard_weighted (global.yaml.in)

The above configuration changes are found in the PR. Please update the relevant release documentation if necessary.
Ignore this comment if docs are already updated. To make the "Check ceph config changes" CI check pass, please comment /config check ok and re-run the test.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

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

@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 7bc2cda to 7b18587 Compare November 6, 2026 11:45
@ming1

ming1 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/config check ok

The new option bluestore_cache_data_shard_weighted is documented in doc/rados/configuration/bluestore-config-ref.rst and PendingReleaseNotes in this PR.

Comment thread src/os/bluestore/BlueStore.h Resolved
@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 7b18587 to 9f9b723 Compare November 17, 2026 03:19
@ming1

ming1 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

New push for @aclamk's two comments, no behaviour change:

  • Request logic is in MempoolCache::_request_cache_bytes(pri, scale); the DataCache override only computes the scale. _request_scale() is gone.
  • BlueStore_cache_shards.h is removed; the two functions are statics of MempoolThread::DataCache, tested via TEST_F(UT_bluestore_cache_shards, ...).
  • _resize_shards() logs the per-shard limits at dout(20) (@dvanders).
  • Doc: the split also applies with autotune off. Shorter commit messages.

Unit tests and the tracker's hot-set test re-run on the new code.

@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 9f9b723 to bc91096 Compare November 17, 2026 03:54
@ming1

ming1 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@ming1

ming1 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/BlueStore.h Resolved
Comment thread src/common/PriorityCache.cc Resolved
@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from bc91096 to 809d4b2 Compare November 18, 2026 02:13
@ming1

ming1 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Hi @aclamk

Thanks for your review!

All your comments have been addressed, can you unblock this PR so we can move on? Or if you have any other concerns, please let us know.

thanks,
Ming

@aclamk aclamk added the bluestore-needs-qa The PR is ready for teuthology testing. Bluestore team will do it. label Nov 20, 2026
@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

balance_priority() serves each cache's request in a loop that adds the
grant to the cache's bytes for that priority. At LAST it then splits the
remaining memory by ratio with set_cache_bytes(), which overwrites what
the loop just granted: the memory left the pool, but no cache is
committed it.

BlueStore's caches ask at LAST for content older than their last age
bin, so the grant is lost on every balance and the OSD runs that far
below its target: 170 to 270 MiB of a 1484 MiB pool on a 4-OSD vstart
with 2 M small objects, about 1 % on a small hot set.

Add the ratio share on top of the loop's grants. Skip a zero share:
rbd-mirror's cache asserts on add_cache_bytes() at a priority it did
not ask at, and its ratio is 0.

Same load with the fix: the onode cache's commit went from 368 to 792
MiB (510k instead of 371k onodes, 99 % instead of 93 % hits) and the OSD
mapped 1744 instead of 1546 MiB of its 2048 MiB target. The RocksDB
block cache went from 288 to 184 MiB because it lost its 24 MiB share of
the unspent memory and asked for less (130 to 49 MiB, fewer lookups);
its request was granted in full in both runs.

Fixes: https://tracker.ceph.com/issues/82836
Signed-off-by: Ming Lei <[email protected]>
_resize_shards() gives every buffer cache shard the same limit, but
collections map to shards by pg seed % shards, so the loaded shards
trim on every insert while the light ones never fill: the data cache
stops below its commit (574 of 816 MiB on one OSD, 85 % hit rate on a
hot set that fits).

With bluestore_cache_data_shard_weighted (default true) each shard
keeps 1/8 of its even share and the rest is split by the bytes each
shard holds. An evenly loaded OSD keeps today's split.

A cache that fills its commit must not ask the autotuner for all it
holds, or the commit grows at every balance out of the other caches'
age bins. By age the data cache asks only for what an even split would
have held, and for the rest at LAST, which is served from memory no
cache claimed by age. This needs "common/PriorityCache: credit grants
made at the last priority".

Numbers are in the pull request.

Fixes: https://tracker.ceph.com/issues/82021
Signed-off-by: Ming Lei <[email protected]>
@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 809d4b2 to 7032f30 Compare November 24, 2026 15:00
weighted_limits(): the even split with nothing held, the 1/8 floor, the
sum within the budget, and full shards converging to the even split.
even_split_fraction(): shards over their even share are capped.
The commit loop: the even-split request keeps the commit at 128 MiB
where asking for everything grows it to 576 MiB in ten rounds.
Through PriorityCache::Manager with fake caches: a LAST request is
credited, is served from unclaimed memory, and gets nothing when the
other caches claim the pool by age.

Signed-off-by: Ming Lei <[email protected]>
@ming1
ming1 force-pushed the wip-82021-cache-shard-limits branch from 7032f30 to 7d76951 Compare November 24, 2026 15:21
@ming1

ming1 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@ming1

ming1 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

1 similar comment
@ming1

ming1 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@ming1

ming1 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Hi @SrinivasaBharath

Could you cover this PR in your test so that finally it can be marked as TESTED?

Thanks,
Ming

@RaminNietzsche

Copy link
Copy Markdown
Member

Ceph PR triage — related open PRs

Queried #73018 against KB snapshot 2026-11-10T06:02:21 in 1.7s.
Signals are candidates for human review, not merge blockers.

Category Confidence Other PR(s) Note
stated-supersede medium #72964 #72964 description says: "Running OSDs at 1.5 GiB (HDD) / 2 GiB (flash) instead of 4 / 6 GiB, together with #73018" (both still open)
stacked low #73570 one PR's new commits are a strict subset of the other's (stacked/superseding branch); text sim 0.772; file Jaccard 0.12; 2 overlapping hunks; same tracker 82836

This comment is updated in place when the workflow re-runs (hidden HTML marker). Data: read-only GitHub + local git analysis; no labels or merges were changed.

@xiubli xiubli left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

Tested the shard split on a bare-metal 3-OSD cluster, where it fixes a case we hit in practice: reads served by the primary only.

Setup: 3 OSDs on NVMe, 3 replicas, 64-PG data pool, osd_memory_target 32G, CephFS kernel client, 16 jobs of 4K random reads, page cache and OSD caches dropped before each run, one run per arm. Note this is the earlier single-commit version of the shard split ("give each shard 1/8 of its even share as a floor and split the rest by the bytes it holds").

To see the shards I added a debug line after _resize_shards() that prints every buffer cache shard's held/max bytes.

Without the PR, with primary-only reads an OSD only serves the PGs it is primary for, about a third of them, so many of the 32 shards get no data at all. At the end of a 120 s O_DIRECT run:

┌───────┬──────────────┬─────────────────┬─────────────────┐
│       │ empty shards │  held / limit   │ per-shard limit │
├───────┼──────────────┼─────────────────┼─────────────────┤
│ osd.0 │ 10 of 32     │ 2.99 / 4.44 GiB │ 142 MiB         │
├───────┼──────────────┼─────────────────┼─────────────────┤
│ osd.1 │ 15 of 32     │ 1.73 / 3.25 GiB │ 104 MiB         │
├───────┼──────────────┼─────────────────┼─────────────────┤
│ osd.2 │ 17 of 32     │ 1.35 / 2.88 GiB │ 92 MiB          │
└───────┴──────────────┴─────────────────┴─────────────────┘

Every non-empty shard sits at its limit and trims on each insert, so no buffer ages into the older priority bins and the autotuner never grows the data cache: it stays at 3-4 GiB while kv and meta keep 10+ GiB each. With read_from_replica=balance every OSD serves every PG, no shard is empty and the data cache grows to 7.9 GiB in the same run; this is why balanced reads looked better than primary reads in our IO500 runs.

With the PR the empty shards drop to the floor (16-66 MiB) and the loaded ones get 300-840 MiB. Primary-only reads:

┌───────────────────────────────────────────────────────┬──────────────────────────────────┬──────────────────────────────────┐
│                                                       │              before              │              after               │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ buffered, 16 x 4 GiB, 300 s: data cache held, osd.0   │ 3.13 GiB                         │ 15.90 GiB                        │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ IOPS                                                  │ 1034                             │ 1256 (+21%)                      │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ latency p50 / p90 / p99 / p99.9 / max (ms)            │ 13.7 / 29.2 / 39.6 / 47.4 / 62.0 │ 11.3 / 24.8 / 35.9 / 44.8 / 77.0 │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ O_DIRECT, 16 x 640 MiB, 120 s: data cache held, osd.0 │ 2.99 GiB                         │ 4.41 GiB                         │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ IOPS                                                  │ 69291                            │ 77685 (+12%)                     │
├───────────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────┤
│ latency p50 / p90 / p99 / p99.9 / max (us)            │ 222 / 338 / 461 / 618 / 11246    │ 194 / 326 / 444 / 569 / 10929    │
└───────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┘

Balanced reads, where every shard is loaded, do not change, as the description says: 71362 vs 71014 IOPS, p99 416 vs 416 us, held 6.61 vs 6.60 GiB, every shard limit within 200-296 MiB.

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.

8 participants