Skip to content

common/PriorityCache: credit grants made at the last priority - #73570

Open
ming1 wants to merge 2 commits into
ceph:mainfrom
ming1:wip-pricache-last-credit
Open

ming1 wants to merge 2 commits into
ceph:mainfrom
ming1:wip-pricache-last-credit

Conversation

@ming1

@ming1 ming1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

balance_priority() grants each cache's request at Priority::LAST with add_cache_bytes(), then splits the remainder by ratio with set_cache_bytes(LAST, share), which overwrites the grant. The bytes left the pool, but no cache is committed them. BlueStore's caches ask at LAST for content older than their last age bin (720 s), so the grant is lost on every balance and the OSD runs below osd_memory_target: 170 to 270 MiB of a 1484 MiB pool with 2 M small objects, about 1 % on a small hot set.

The fix adds the share on top of the grant, and skips a zero share because rbd-mirror's PriCache::add_cache_bytes() asserts at a priority it did not ask at. With nothing asked at LAST the result is byte-identical to today; the ratios still split only the memory nobody asked for.

4-OSD vstart, 2 GiB target, same load (osd.1): onode cache commit 368 -> 792 MiB, 371k -> 510k onodes, 93 -> 99 % hit rate, OSD mapped 1546 -> 1745 MiB of 2048. The RocksDB block cache goes 288 -> 184 MiB because it loses its 24 MiB share of the unspent memory and asks for less; its request was granted in full in both runs.

Harmful since Cache Age Binning (56351838337, v17.1.0) made the caches ask at LAST. The second commit adds a gtest driving the real Manager::balance() with fake caches: four cases, two fail without the fix and the zero-ratio case fails without the fair_share > 0 guard.

PendingReleaseNotes: OSDs holding cache content older than the last age bin now use that memory, so their RSS moves closer to osd_memory_target after the upgrade.

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

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)

@github-actions github-actions Bot added the common label Nov 6, 2026
@ming1
ming1 requested a review from markhpc November 6, 2026 03:10
@ming1
ming1 requested review from aclamk, dvanders and ifed01 November 6, 2026 03:11
ming1 added a commit to ming1/ceph that referenced this pull request Nov 6, 2026
_resize_shards() gives every buffer cache shard the same limit,
data_alloc / shards. Collections map to shards by pg seed modulo the
shard count, so an OSD's PGs spread unevenly over them: the loaded
shards trim on every insert while the light ones never fill, and the
data cache stops below its committed size (574 of 816 MiB on one OSD
of a 4-OSD vstart, 85 % buffer hit rate on a hot set that fits).

With the new option bluestore_cache_data_shard_weighted (default true),
give each shard 1/8 of its even share as a floor and split the rest in
proportion to the bytes it holds, from one snapshot of the counters so
the limits sum to at most the committed size. A shard at exactly its
even share is a fixed point, so an evenly loaded OSD keeps today's
split; false restores it.

A cache that can fill its commit must not ask the autotuner for all it
holds: the tuner commits held + 64 MiB, so the request would grow at
every balance and take the other caches' oldest bins. By age, ask only
for the fraction of the held bytes that an even split would have held;
ask for the rest at LAST, which is served after every cache's binned
content and only from memory nobody claimed by age. The commit grows
into unclaimed memory, never into another cache's recent content. This
needs "common/PriorityCache: credit grants made at the last priority"
(PR ceph#73570). The split and the fraction are pure functions in
BlueStore_cache_shards.h, covered by unit tests.

On the tracker's hot-set test the data cache now holds the whole hot
set instead of about two thirds of its commit, and disk reads during
the pass all but disappear. With the pool taken by the onode and
RocksDB caches of millions of small objects, switching the option on
at runtime leaves those caches where they are and grows the data
commit only into memory nobody claimed by age. Writes are unchanged.
Numbers are in the pull request.

Fixes: https://tracker.ceph.com/issues/82021
Signed-off-by: Ming Lei <[email protected]>
ming1 added a commit to ming1/ceph that referenced this pull request Nov 6, 2026
_resize_shards() gives every buffer cache shard the same limit,
data_alloc / shards. Collections map to shards by pg seed modulo the
shard count, so an OSD's PGs spread unevenly over them: the loaded
shards trim on every insert while the light ones never fill, and the
data cache stops below its committed size (574 of 816 MiB on one OSD
of a 4-OSD vstart, 85 % buffer hit rate on a hot set that fits).

With the new option bluestore_cache_data_shard_weighted (default true),
give each shard 1/8 of its even share as a floor and split the rest in
proportion to the bytes it holds, from one snapshot of the counters so
the limits sum to at most the committed size. A shard at exactly its
even share is a fixed point, so an evenly loaded OSD keeps today's
split; false restores it.

A cache that can fill its commit must not ask the autotuner for all it
holds: the tuner commits held + 64 MiB, so the request would grow at
every balance and take the other caches' oldest bins. By age, ask only
for the fraction of the held bytes that an even split would have held;
ask for the rest at LAST, which is served after every cache's binned
content and only from memory nobody claimed by age. The commit grows
into unclaimed memory, never into another cache's recent content. This
needs "common/PriorityCache: credit grants made at the last priority"
(PR ceph#73570). The split and the fraction are pure functions in
BlueStore_cache_shards.h, covered by unit tests.

On the tracker's hot-set test the data cache now holds the whole hot
set instead of about two thirds of its commit, and disk reads during
the pass all but disappear. With the pool taken by the onode and
RocksDB caches of millions of small objects, switching the option on
at runtime leaves those caches where they are and grows the data
commit only into memory nobody claimed by age. Writes are unchanged.
Numbers are in the pull request.

Fixes: https://tracker.ceph.com/issues/82021
Signed-off-by: Ming Lei <[email protected]>
@ming1

ming1 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Hello @dvanders @ifed01 @aclamk @markhpc

Can you review this PR?

Thanks,

@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.

The fix is right. The loop's grant at LAST is subtracted from mem_avail and then overwritten by set_cache_bytes(), and with the pool already at max_mem the tuner has no way to give it back. I checked the other PriCache users: the mon's OSDMap caches only ask at PRI1, and rbd-mirror asks at PRI10 with a ratio of 0, so the fair_share > 0 guard covers its assert.

I built the gtest from the tracker on this branch: 4/4 pass; with the parent's PriorityCache.cc the two LAST cases fail; with the fix but without the fair_share > 0 guard the zero-ratio case fails. So both halves of the change are covered.

Requests:

  1. Please add that gtest as a second commit. There is no PriorityCache unit test in the tree today. It needs $<TARGET_OBJECTS:common_prioritycache_obj> in the executable and global heap_profiler to link.
  2. Please set the Backport field on the tracker (umbrella, tentacle, squid).
  3. Consider a PendingReleaseNotes line: OSDs holding cache content older than the last age bin will now use that memory, so their RSS moves closer to osd_memory_target after the upgrade.

Nit: the commit message says the OSD mapped 1744 MiB and the PR text says 1745.

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 1745 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]>
Drive PriorityCache::Manager with fake caches and check that the pool is
handed out in full, that a request served at the last priority stays with
the cache once the ratio split has run, that two caches asking for more
than is left at the last priority share it by ratio, and that a cache with
a ratio of 0 is not handed a zero-byte share it would assert on (as
rbd-mirror's does). The second and third fail without the previous commit.

Signed-off-by: Ming Lei <[email protected]>
@ming1
ming1 force-pushed the wip-pricache-last-credit branch from 7b0e827 to fca2224 Compare November 12, 2026 02:58
@ming1
ming1 requested a review from a team as a code owner November 12, 2026 02:58
@ming1

ming1 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and for checking the other PriCache users. Pushed v2:

  • the gtest is the second commit (test/common: unit tests for the autotuner's last-priority split), linked with common_prioritycache_obj and global heap_profiler as you note; 4/4 pass on the branch.
  • a PendingReleaseNotes line, in the fix commit, saying what you suggested.
  • the commit message now says 1745 MiB like the PR text (the two runs read 1744 and 1745).
  • Backport set to umbrella, tentacle, squid on the tracker.

@ming1

ming1 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks Igor and Dan's review!

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath29-testing-20261121.045602-main.

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.

(1) I believe that this block should be executed only when pri != LAST.

@ming1 ming1 Nov 26, 2026 •

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.

(1) I believe that this block should be executed only when pri != LAST.
(2) That would make the change from set_cache_bytes to add_cache_bytes fully redundant.

That also stops the loss, but it drops the requests at LAST, and I think keeping them fits the current code better:

  • The loop already runs at LAST today, and since Cache Age Binning (56351838337) the BlueStore caches and the RocksDB block cache each have a LAST case in request_cache_bytes(), asking for content older than the last age bin. With the loop skipped, those cases are never called.

Example. 600 MiB left at LAST, ratios 0.30 / 0.40 / 0.30:

MiB onode data RocksDB
asks for 400 50 50
loop skipped at LAST 180 240 180
this PR 400 + 30 50 + 40 50 + 30

With the loop skipped, the onode cache has to evict 220 MiB while the data cache gets 190 MiB it did not ask for. The two differ only in this case, where one cache wants more than its ratio share and another wants less; under contention the loop hands out by ratio as well.

Measurement. 4-OSD vstart, osd_memory_target 2 GiB, 700k 4 KiB objects, random reads, one run each:

per OSD main loop skipped at LAST this PR
lost from the pool 193–241 MiB 0 0
onode cache committed 312–336 MiB 376–400 MiB 592–696 MiB
onode hit rate 86.9–91.4 % 88.8–92.8 % 95.1–96.8 %
onode misses, 6 min, all OSDs 92,203 78,405 34,580
data cache committed / used 408–464 / 108–238 MiB 496–576 / 129–297 MiB 328–408 / 98–213 MiB

Small objects favour the onode cache, so this is the good case for the PR.

So I would keep granting the requests at LAST as at the other priorities, and split by ratio only what nobody asked for.

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 think this part of the discussion should wait for acceptance on design/documentation of PriorityCache.

In this specific case I have in mind my claim (as a documentation creator of code I did not design nor wrote) that:
"The LAST means that the cache is using the memory, but it does not affect its performance."
From:
https://github.com/ceph/ceph/pull/74359/changes#diff-4280052187eed37ef193fd7e299e7ca3956f3e45385698e31c4f19a07ffecf44R54

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.

But this PR follows the current code base's implementation. If you think the current implementation is wrong
wrt. LAST, please send a fix.

PR # 74359 is not a design of PriorityCache at all, which is retrieved from current code. Design doc is supposed for guiding implementation.

So I don't buy your point, sorry.

it->second->set_cache_bytes(Priority::LAST, fair_share);
total_assigned += fair_share;
if (fair_share > 0) {
it->second->add_cache_bytes(Priority::LAST, fair_share);

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.

(2) That would make the change from set_cache_bytes to add_cache_bytes fully redundant.

@ming1

ming1 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

@sseshasa

sseshasa commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@aclamk

aclamk commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@ming1

ming1 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Please note the following: a) Ceph guidelines on AI - assisted contributions https://docs.ceph.com/en/latest/dev/developer_guide/ai-coding-assistants/ b) Guidelines for contributors using AI tools in BlueStore https://docs.ceph.com/en/latest/dev/bluestore/ai-contribution-guidelines/

Can you share anything this PR violates the above?

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.

6 participants