Skip to content

vk: per-step present timers in MALI_FRAME_PROF - #387

Draft
pablogonz12 wants to merge 2 commits into
mainfrom
lane/present-prof
Draft

pablogonz12 wants to merge 2 commits into
mainfrom
lane/present-prof

Conversation

@pablogonz12

Copy link
Copy Markdown
Owner

Draft: the numbers come from the CMF run this PR enables. Host-only proof so far (it compiles, the line is printed, check.sh is green apart from a load-bound timing row).

Why

The CMF measurement (9 961 frames, submit_us 81, present_ms ≈ 41 at 26 fps, 68 % kernel) reads as "the present costs the frame". It does not: mali_fprof_present() is called from the present submit (mali_queue.c:717) and present_ms is the interval between two of those calls (mali_fprof.c:201-212) — 41 ms at 26 fps is exactly 1/fps. Nothing inside the present is timed today, so the 68 % kernel time is unattributed.

What this adds

Six timers, each one clock read and only when MALI_FRAME_PROF is set:

field step mali_wsi.c
acquire wsi_common_acquire_next_image2 (the image fence, or the gate ring on a shm chain) :1248-1250
wait present_wait_submit_fence: dup() + be->fence_wait(fd, 1000) :1050-1056
sync present_image_cpu_sync: kbase cache maintenance over the whole image BO :1021-1026
copy the full-frame memcpy into the shm segment :1185-1187
put xcb_shm_put_image + xcb_flush :1190-1223
call the whole vkQueuePresentKHR body (wrapper around it, early returns preserved) :1712-1724

FRAMEPROF lines become ... present_ms=41.2 ... wsi_us=a/w/s/c/p/call, the summary line gains wsi_avg_us=.

mali_device_fprof() (mali_queue.c:571) is the accessor: struct mali_hwctx is private to mali_queue.c, so mali_wsi.c asks rather than including it.

The step list (M/notes/present-cost.md, host read)

The CMF takes the software MIT-SHM path (Xwayland on Mali has no DRI3, mali_wsi.c:104-109), and the steps are: trace (MALI_WSI_DUMP only) → shm drain (non-blocking) → wait the app's present semaphores, UINT64_MAX (:1759-1770, this is where a GPU-bound frame's time lands) → per image: fence wait → device→CPU cache maintenance → memcpy of 1920×1080×4 = 8.3 MB from a write-combined mapping (:1186, 10-40 ms at 200-800 MB/s, the only step that can be the frame on its own) → XShmPutImage + flush (µs, no image bytes on the socket) → release.

So the expectation to test on the phone: if wsi_us's copy is ~20-40 ms, the fix is to stop copying (dma-buf/DRI3, or a shm segment we render into directly); if wait+call dominate, the frame is GPU-bound and present is innocent; if sync dominates, the cache maintenance per frame is the target.

Checks

  • ./check.sh: 241 ok, one failure — wl mock host v10/v13 (present_ms max 8.68 vs a 5.00 limit) with the laptop at load 13.9 and 25 concurrent build/check processes from other lanes; the same row failed the same way on an unrelated lane and on main's behaviour, and this PR adds no clock read when MALI_FRAME_PROF is unset.
  • Fake backend: MALI_FRAME_PROF=1 prints wsi_us=0.0/0.0/0.0/0.0/0.0/0.0 on every line (no swapchain in those runs) and the summary line carries wsi_avg_us.
  • The shm path itself cannot run on the headless laptop (xwsi-host.sh skips: no gamescope/Xwayland).

MALI_FRAME_PROF's present_ms is the interval between two present submits (mali_fprof.c:201-212), i.e.
1/fps at a steady frame rate: it says when presents happened, never how long one took. The CMF reading
'present_ms ~41 at 26 fps' is therefore the frame, not a 41 ms present call.

Add the times inside the call, one clock read each, only when the env var is set: acquire
(mali_wsi.c:1248), the submit-fence wait (:1050), the device->CPU sync of the image (:1021), the
full-frame memcpy into the shm segment (:1185), the X request + flush (:1190), and the whole
vkQueuePresentKHR body (:1712-1724: the wrapper times the body, which keeps its early returns).

Frame lines get 'wsi_us=acquire/wait/sync/copy/put/call', the summary gets wsi_avg_us. The steps
are named in M/notes/present-cost.md with their expected cost; the copy of an 8.3 MB 1080p frame from
a write-combined mapping (10-40 ms) is the only one that can be the frame by itself.
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pablogonz12

Copy link
Copy Markdown
Owner Author

A detail from the earlier run's own log that sharpens the question (added to M/notes/present-cost.md § 4):

FRAMEPROF f=6338 submit_us=50.8 gpu_ms=1.71 est=sat depth=23 present_ms=41.00 frame_ms=2.29 bound=gpu

Frames are kicked every 2.29 ms and presented every 41 ms: the app queues ~18 frames per present and the GPU retires each in ~1.7 ms. So the present is the pacing element, and the new wsi_us splits the two possibilities:

  • call ≈ 41 000 µs → our present blocks, and wait/copy/put says on what;
  • call ≈ 1-2 ms → our present returns at once and the 41 ms cadence lives above us (swapchain depth / FIFO vs a compositor not consuming at 120 Hz), i.e. gamescope/Xwayland, not this driver.

Either way submit_us is out: 18 × 81 µs = 1.5 ms per presented frame.

Store successful acquire samples with their swapchain images, then consume each sample when that image is presented. Cover out-of-order images, consecutive presents and the reported acquire average with a synthetic host test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant