Skip to content

Kirby and the Forgotten Land: frame limits run on the game's own clock, and the 4 fps collapse is gone - #624

Open
intermission88 wants to merge 1 commit into
igawa6:mainfrom
intermission88:kirby-frame-timer
Open

intermission88 wants to merge 1 commit into
igawa6:mainfrom
intermission88:kirby-frame-timer

Conversation

@intermission88

Copy link
Copy Markdown
Contributor

What and why

The symptom. Under a frame limit, Kirby held its rate for a minute or two and then fell off a cliff in the overworld: 4-9 fps for over a minute, whole frames of 210-325 ms, the GPU idle at 5 ms a frame. GitHub issue #153 reported it on a Snapdragon 8+ Gen 1 (3-6 fps at the 30 and 60 limits); it also happens on an AYN Thor at 30. The profile then started the game Game owned to avoid it; since 0.1.8 it starts at 60 (the owner's call) with the same mode-0 row, so every player now starts in the mode that collapses.

The cause (read in main 1.1.0, and the same in 1.0.0). [main+0xA76728] points 0x10 into the game's frame timer. Its mode at +0x60 picks the pacing:

Mode How a frame is paced
2, as shipped one update a frame; the step is the 1/60 s ticks the frame took, averaged over 4 frames; a frame waits at least +0x3c ticks (2 = the game's 30 fps)
0 fixed 1/60 s updates; a late frame is caught up with up to +0x20 (10) updates and no drawing (checked at main+0x532FB8)

The FPSLocker Warehouse row the limits used (zero_i32 at +0x50) set the mode to 0. At 30 fps that is two updates a frame, twice the game's CPU work, and once the updates outlast 16.7 ms the catch-up never ends: 10-11 updates for each drawn frame. That is the collapse.

The change (dm/games/01004D300C5AE000/game.tsv, one profile, no code):

  • The mode-0 write is gone. One row writes the least wait of the game's own mode instead: constant_i32 0xa76728/0x2c 1 1:1 (2 as shipped, 1 lets the game run to 60). The host's frame limit sets the pace and the game keeps one update a frame at every rate. The dynamic resolution budget rows stay.
  • The default stays 60 (0.1.8, the owner's call); with the game's own timer it no longer collapses. On the Thor a limit is also evener on screen than Game owned (2,890 of 3,288 frames uneven under Game owned, a 50-80 ms hitch every 2-3 s; 3 of 4,490 at 30).

Keeping it fixed (so this does not come back, here or in another game):

  • A guard test, peanuts/tests/test_profile_fpslocker.py: it fails if Kirby's rows write the timer mode (+0x50) or the catch-up bound (+0x10) again or if the +0x2c row goes missing. It also parses every profile's fpslocker rows with the host's own parser (fpslocker_rows.h), because one refused row makes the host drop the whole set (invalid reviewed rows; no memory writes applied) and a limit then runs without any of the game's writes. Checked against both mistakes: a constant_i32 value written 2 instead of 2:1, and the Warehouse's mode-0 row added back.
  • A log digest rule, game-catchup-stall (dm/tools/log_rules.tsv, read by log_digest.py, issue_digest.py and the launcher): a [longframe] of 200 ms or more with the GPU idle, nothing loading and the game's threads waiting on each other. A player's log of this collapse, in any game, now says what it is and links the entry. Unit-tested against the real collapse line, and against loading frames and small hitches that must not match.
  • A review rule in the reference entry: before a profile takes a Warehouse write that changes a game's interval or timer mode, read what the mode does; the signs of a fixed-step catch-up in the code (sdiv by 16,666,666 ns, an update count compared to a cap, drawing skipped while it catches up).
  • A guide for the next Kirby build in the game note: the rows are keyed by the main's build ID, so a new update needs them carried over; where the constructor, the pacing and the two offsets are in 1.1.0 and 1.0.0, and that nso_fnmap.py maps them.

Docs: the frame timer, its addresses and the runs in Kirby's note; a reference entry, A fixed-step mode chosen by a frame limit catches up until the frame rate collapses, with the symptom index.

Fixes the cause of #153 (not yet run on that phone; the reporter is welcome to confirm).

Checked

  • Python tests: the new peanuts/tests/test_profile_fpslocker.py (every profile's rows, the Kirby guard) and test_log_digest (the new rule), test_fpslocker_rows, test_fpslocker_plan, test_android_frame_rate_caps, test_env_switches and the other test_android_* (with test_android_logdigest, the launcher reading the same rule table on the JVM) pass; peanuts/scripts/check-profiles.sh 01004D300C5AE000 passes. test_docs passes. test_android_packfind fails on the Mac's own Python 3.9 (type | type), unrelated.
  • Phone: AYN Thor, Snapdragon 8 Gen 2 (Adreno 740), 16 GB, Android 13, test app, Kirby 1.1.0 (main a6ce40dc…), the middle CPU cores held at 1.79 GHz by a clock tune.
  • GPU drivers: Turnip 27.0.0 R2 (and R3 for one short run). Not run: the system driver.

A build of main 0.1.7 with these rows only (that build started at 30), Game settings reset to the defaults first:

Limit Frames a second Frame time On screen
30 (2.6 min: overworld, levels, movies) 29.8 in almost every 5 s window p50 33.5 ms, p95 33.6 ms 3 of 4,490 frames uneven
20 (2.5 min) 19.7-20.1 p50 50.3 ms, p95 50.3 ms 3 of 3,290
40 (1.5 min) 39.1-40.0 p50 25.0 ms, p95 25.0 ms 49 of 4,790

The game's speed is normal by eye at every limit. The longest frames (130-180 ms, once a run) are an area loading 50-160 MB of textures.

Before and after, the same overworld at 30:

Mode-0 rows (before) This change
Frames a second 29.8, then 4-9 for over a minute after about 100 s 29.8-29.9 throughout
Worst frames in play 210-325 ms none outside loading

Also run on an earlier build with these rows: 45 (43.2-45.2 fps over 3.5 min, about 60 °C) and 60 (reached, normal speed; 47-51 fps in busy levels at 1.79 GHz, see Known issues).

Known issues (not from this change)

Recommended

  • 60 (the default): where the CPU clocks allow it; on a Thor at stock clocks 55-57 in busy levels, and warm.
  • 30: even and cool, the Switch's own rate; the pick for a phone with its clocks held down.
  • 40 or 45: in between (45 shows as frames of one and two refreshes on a 60 Hz panel).
  • Game owned stays a choice; on the Thor it is less even on screen than a limit.

Docs

  • The finding is in the game note and a docs/reference/frame-rate.md entry; python3 dm/tools/docs_symptoms.py was run
  • No game files, keys or data extracted from a game
  • Source only; the branch starts from the current main (0.1.8)

…k, and the 4 fps collapse is gone

The overworld no longer falls off a cliff. Under every frame limit from 20 to
120, Kirby keeps the game's own measured frame timer, one update a frame, and
the launcher's limit sets the pace. The fixed-step catch-up that dragged a 30
fps limit down to 4-9 fps for over a minute (GitHub issue igawa6#153) is never
switched on, also at 0.1.8's default 60, and guards keep it that way.

Root cause, read in main 1.1.0 (and 1.0.0):
- [main+0xA76728] points 0x10 into the game's frame timer. Its mode at +0x60
  picks the pacing: 2 as shipped (one update a frame, the step measured in
  1/60 s ticks, averaged over 4 frames, at least +0x3c ticks a frame), 0 a
  fixed 1/60 s step.
- The FPSLocker Warehouse row these limits used set the mode to 0. At 30 fps
  that is two updates a frame, twice the game's CPU work, and a late frame is
  caught up with up to 10 updates and no drawing (+0x20, checked at
  main+0x532FB8). Once the updates outlast 16.7 ms every frame is late:
  10-11 updates per drawn frame, 210-325 ms frames, the GPU idle at 5 ms.

Fixes:
- game.tsv: the mode-0 write is replaced by one write of the least wait of
  the game's own mode (constant_i32 +0x2c = 1; 2 as shipped, the game's
  30 fps). The dynamic resolution budget rows stay.
- The default stays the owner's 60 (0.1.8); with the game's own timer it no
  longer collapses. On the Thor a limit is also evener on screen than Game
  owned: 2,890 of 3,288 frames uneven under Game owned, 3 of 4,490 at 30.

Keeping it fixed:
- peanuts/tests/test_profile_fpslocker.py parses every profile's fpslocker
  rows with the host's own parser (one refused row makes the host drop the
  whole set) and fails if Kirby's rows write the timer mode or the catch-up
  bound again or lose the +0x2c row.
- dm/tools/log_rules.tsv: game-catchup-stall names such frames in a player's
  log (a [longframe] of 200 ms or more, the GPU idle, nothing loading, the
  game's threads waiting on each other), in log_digest, issue_digest and the
  launcher; unit-tested on the real collapse line.
- docs/reference/frame-rate.md: a new entry, "A fixed-step mode chosen by a
  frame limit catches up until the frame rate collapses", with a review rule
  for Warehouse writes that change a timer mode; the symptom index updated.
- Kirby's note: the frame timer, its addresses, where to find them in a new
  main build, and the runs below.

Measured on an AYN Thor (Snapdragon 8 Gen 2, Turnip 27.0.0 R2, the middle
cores held at 1.79 GHz by a clock tune), a build of main with this profile:
- 30 (that build's default, after a settings reset): 29.8 fps in almost every 5 s
  window over 2.6 minutes (overworld, levels, movies), p95 33.6 ms; the old
  rows collapsed to 4-9 fps in the same area.
- 20: 19.7-20.1 fps, p95 50.3 ms. 40: 39.1-40.0 fps, p95 25.0 ms.
- 45 (43.2-45.2) and 60 (reached) on an earlier build with these rows; the
  game's speed normal by eye at every limit.

Known issues, not from this change:
- 60 in busy levels is bound by the CPU (47-51 fps at 1.79 GHz, 55-57 at
  the stock clocks near 84 C). The Thor's kernel pauses its fast core while
  idle (core_ctl min_cpus 0), so the replay thread meant for it runs on all
  cores, the small ones included.
- The overworld's round trees are drawn nearly black on Turnip; the same
  with DM_VK_PUSH_DELTA=0 and DM_VK_UBWC=0.
- Not run: the system driver, 90, a session of 10 minutes or more, the
  8+ Gen 1 phone of igawa6#153.

Recommended on a Thor: 60 (the default) where the CPU clocks allow it; 30 for
an even, cool picture with a clock tune; 40 or 45 in between.
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