vkback: free idle empty image-arena blocks back to Vulkan #195

Open
github-bot wants to merge 1 commit from github-bot/gh-195 into main AGit
Owner

@⁠deuxque on GitHub · 2026-12-02 07:07 UTC · original
deuxque:pr/arena-free-empty-blocks → main · +269 −17 · 2 files · 1 commits · open

Problem

The vkback image arena (greenfield/vkback/src/arena.rs) allocates 256 MiB blocks and
never frees completely empty ones until shutdown. On both 8 GB and 16 GB cards, unused
256 MiB blocks remain counted in Vulkan heap usage (heap_usage), reducing physical
headroom for the compositor, driver allocations, and umem::can_grow().

Observed on RX 6600 8 GB (Series S, UMEM_MEMREPORT=1 on d59b4f8):
arena blocks 10 (326 MB free, 0 MB of it in GTT blocks, 1 empty, largest free range 256 MB)
with vkback arena block 2560/10 held indefinitely while VRAM stood over its reserve line.

Change

  • Change Arena::blocks from Vec<Block> to Vec<Option<Block>> so freeing an empty
    block tombstones its slot and push_block reuses empty slots, keeping existing
    Piece.0 block indices in VulkanBackend::arena_of stable.
  • Track empty_since: Option<Instant> per block and last_alloc_at: HashMap<u32, Instant>
    per memory type (Arena::alloc, Arena::free, push_block_at).
  • Add Arena::trim_empty (called once per present from pipewait.rs after source_sweep)
    enforcing GPU fence safety and anti-thrash rules per memory type:
    • Fence safety: Only frees blocks with b.ready <= done && b.is_empty() (Arena::free
      runs only in VulkanBackend::retire strictly after timeline completion and vkDestroyImage).
    • Rule 1 (no over_line() query): Frees excess empty blocks (>= 2 empty, or another
      ready block of the same type has >= 128 MiB free) after 1 s, keeping 1 warm spare
      whenever remaining blocks have < 128 MiB largest free range.
    • Rule 2 (anti-sawtooth, throttled <= 1 / 1 s): Frees the last empty spare only
      when empty_since >= 5 s, last_alloc_at(ty) >= 5 s (preventing sawtooth against
      Maker pre-allocation and synchronous fresh-block waits), and that heap is over its
      line (over_line() >= 0).
    • Zero hot-path cost: has_empty() skips both timeline and over_line() queries
      when no block is empty; needs_pressure_check() avoids per-present over_line() calls
      on healthy cards holding 1 warm spare.

Measurements & Tests

  • On-card RX 6600 8 GB (RDNA 2, RADV, CachyOS Linux, 16 GB RAM, Series S, 120 s, UMEM_MEMREPORT=1):
Metric upstream/main (d59b4f8) This PR
arena blocks (end) 10 (326 MB free, 1 empty, largest 256 MB) 9 (70 MB free, 0 empty, largest 54 MB)
VRAM usage 5,392 MB 5,136 MB (-256 MB)
UMEM_MEMREPORT vkback arena block 2,560 MB / 10 (heap 1: 5,380 MB) 2,304 MB / 9 (heap 1: 5,124 MB)
Fresh-block waits 6 6 (+0)
Median fps / loud errors 30.0 fps / 0 30.0 fps / 0
  • 7 new pure unit tests in greenfield/vkback/src/arena.rs (no GPU required):
    • tombstone_slot_keeps_other_block_indices_valid_and_reuses_slot
    • fresh_empty_block_is_never_freed_before_ready
    • excess_empty_blocks_keep_one_spare_when_others_lack_half_block
    • last_empty_spare_freed_only_after_idle_under_pressure
    • per_type_isolation
    • no_maker_sawtooth_during_active_alloc_churn
    • pressure_probe_skipped_until_candidate_is_aged_and_throttled
  • cargo test --manifest-path greenfield/Cargo.toml --workspace --no-fail-fast:
    252 passed, 6 ignored, 0 failed.
  • cargo clippy --manifest-path greenfield/Cargo.toml -p vkback --all-targets -- -D warnings:
    0 warnings.
  • cargo test --manifest-path greenfield/Cargo.toml -p vkback --test policy:
    6 passed, 0 failed (lib.rs unchanged at 4,250 non-test lines).
  • Capped tools/build.sh: exit 0 (41 s warm cache).

AI use

Model: Gemini 4 Neon, human-directed scope and review. Verified via isolated Linux unit,
clippy, and policy tests plus a 120 s on-card RX 6600 8 GB Series S UMEM_MEMREPORT=1
capture (37 draws/frame, without BCPack tables). Full 3D-world streaming and 16 GB
reference-card testing remain pending.

Refs #17 #103

**@⁠deuxque** on GitHub · 2026-12-02 07:07 UTC · [original](https://github.com/vktory6/VKtory/pull/195) `deuxque:pr/arena-free-empty-blocks` → `main` · +269 −17 · 2 files · 1 commits · open ## Problem The `vkback` image arena (`greenfield/vkback/src/arena.rs`) allocates 256 MiB blocks and never frees completely empty ones until shutdown. On both 8 GB and 16 GB cards, unused 256 MiB blocks remain counted in Vulkan heap usage (`heap_usage`), reducing physical headroom for the compositor, driver allocations, and `umem::can_grow()`. Observed on RX 6600 8 GB (Series S, `UMEM_MEMREPORT=1` on `d59b4f8`): `arena blocks 10 (326 MB free, 0 MB of it in GTT blocks, 1 empty, largest free range 256 MB)` with `vkback arena block 2560/10` held indefinitely while VRAM stood over its reserve line. ## Change - Change `Arena::blocks` from `Vec<Block>` to `Vec<Option<Block>>` so freeing an empty block tombstones its slot and `push_block` reuses empty slots, keeping existing `Piece.0` block indices in `VulkanBackend::arena_of` stable. - Track `empty_since: Option<Instant>` per block and `last_alloc_at: HashMap<u32, Instant>` per memory type (`Arena::alloc`, `Arena::free`, `push_block_at`). - Add `Arena::trim_empty` (called once per present from `pipewait.rs` after `source_sweep`) enforcing GPU fence safety and anti-thrash rules per memory type: - **Fence safety:** Only frees blocks with `b.ready <= done && b.is_empty()` (`Arena::free` runs only in `VulkanBackend::retire` strictly after timeline completion and `vkDestroyImage`). - **Rule 1 (no `over_line()` query):** Frees excess empty blocks (`>= 2` empty, or another ready block of the same type has `>= 128 MiB` free) after `1 s`, keeping 1 warm spare whenever remaining blocks have `< 128 MiB` largest free range. - **Rule 2 (anti-sawtooth, throttled `<= 1 / 1 s`):** Frees the last empty spare only when `empty_since >= 5 s`, `last_alloc_at(ty) >= 5 s` (preventing sawtooth against `Maker` pre-allocation and synchronous fresh-block waits), and that heap is over its line (`over_line() >= 0`). - **Zero hot-path cost:** `has_empty()` skips both timeline and `over_line()` queries when no block is empty; `needs_pressure_check()` avoids per-present `over_line()` calls on healthy cards holding 1 warm spare. ## Measurements & Tests - **On-card RX 6600 8 GB (RDNA 2, RADV, CachyOS Linux, 16 GB RAM, Series S, 120 s, `UMEM_MEMREPORT=1`):** | Metric | `upstream/main` (`d59b4f8`) | This PR | |---|---|---| | `arena blocks` (end) | `10 (326 MB free, 1 empty, largest 256 MB)` | `9 (70 MB free, 0 empty, largest 54 MB)` | | `VRAM` usage | `5,392 MB` | `5,136 MB` (`-256 MB`) | | `UMEM_MEMREPORT` `vkback arena block` | `2,560 MB / 10` (`heap 1: 5,380 MB`) | `2,304 MB / 9` (`heap 1: 5,124 MB`) | | Fresh-block waits | `6` | `6` (`+0`) | | Median fps / loud errors | `30.0 fps` / `0` | `30.0 fps` / `0` | - **7 new pure unit tests in `greenfield/vkback/src/arena.rs` (no GPU required):** - `tombstone_slot_keeps_other_block_indices_valid_and_reuses_slot` - `fresh_empty_block_is_never_freed_before_ready` - `excess_empty_blocks_keep_one_spare_when_others_lack_half_block` - `last_empty_spare_freed_only_after_idle_under_pressure` - `per_type_isolation` - `no_maker_sawtooth_during_active_alloc_churn` - `pressure_probe_skipped_until_candidate_is_aged_and_throttled` - `cargo test --manifest-path greenfield/Cargo.toml --workspace --no-fail-fast`: 252 passed, 6 ignored, 0 failed. - `cargo clippy --manifest-path greenfield/Cargo.toml -p vkback --all-targets -- -D warnings`: 0 warnings. - `cargo test --manifest-path greenfield/Cargo.toml -p vkback --test policy`: 6 passed, 0 failed (`lib.rs` unchanged at 4,250 non-test lines). - Capped `tools/build.sh`: exit 0 (41 s warm cache). ## AI use Model: Gemini 4 Neon, human-directed scope and review. Verified via isolated Linux unit, clippy, and policy tests plus a 120 s on-card RX 6600 8 GB Series S `UMEM_MEMREPORT=1` capture (37 draws/frame, without BCPack tables). Full 3D-world streaming and 16 GB reference-card testing remain pending. Refs #17 #103
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 07:08:46 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-12-02 07:07 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-12-02 07:07 UTC · [original](https://github.com/vktory6/VKtory/pull/195#pullrequestreview-6146190495) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks, giving empty arena blocks back is worth having (−256 MB in your run). Three things:

  • The reference (rule 12). vkd3d-proton frees a chunk as soon as it empties (memory.c, vkd3d_free_memory → remove_chunk). This adds timers instead (1 s / 5 s / 128 MiB / keep one spare / probe every 1 s). Please say why we differ (the maker thread and the waits for fresh blocks are plausible reasons) and derive the rule from that, not from tuned numbers.
  • The hot path. Every allocation now writes last_alloc_at (an Instant plus a hash insert).
  • The pressure check should use #188's cached room check once it lands.

The fence-safety argument in the SAFETY comment holds.

**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/195#issuecomment-7350390155) Thanks, giving empty arena blocks back is worth having (−256 MB in your run). Three things: - **The reference (rule 12).** vkd3d-proton frees a chunk as soon as it empties (`memory.c`, `vkd3d_free_memory` → `remove_chunk`). This adds timers instead (1 s / 5 s / 128 MiB / keep one spare / probe every 1 s). Please say why we differ (the maker thread and the waits for fresh blocks are plausible reasons) and derive the rule from that, not from tuned numbers. - **The hot path.** Every allocation now writes `last_alloc_at` (an `Instant` plus a hash insert). - **The pressure check** should use #188's cached room check once it lands. The fence-safety argument in the SAFETY comment holds.
This pull request has changes conflicting with the target branch.
  • greenfield/vkback/src/arena.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/195/head:github-bot/gh-195'
git switch 'github-bot/gh-195'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-195'
git switch 'github-bot/gh-195'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-195'
git switch 'github-bot/gh-195'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-195'
git switch 'main'
git merge --squash 'github-bot/gh-195'
git switch 'main'
git merge --ff-only 'github-bot/gh-195'
git switch 'main'
git merge 'github-bot/gh-195'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.