Skip to content

fix(tools): measure a real cold start and set benchmark settings reliably - #31

Merged
johnneerdael merged 14 commits into
mainfrom
bug/tv-diagnostics-cold-start
Oct 4, 2026
Merged

johnneerdael merged 14 commits into
mainfrom
bug/tv-diagnostics-cold-start

Conversation

@johnneerdael

@johnneerdael johnneerdael commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

tools/tv-diagnostics.sh reported a "cold start" that was usually a warm start. The app's notification listener (TrackListenerService) makes Android rebind it about a second after am force-stop, which starts the app's process again, so the script's force-stop; sleep 1; am start -W started the activity in an existing process. The same restart broke fixed-resolution benchmarks that write projectm_settings.xml from outside the app (handover AM6 benchmarks: requested render height not applied, 2026-10-04): 12 of 72 runs rendered the previous run's height or the defaults.

Testing on an AM9 Pro showed that the handover's fix (a second force-stop after the write) is not sufficient. A process that reads the file while it is being rewritten loads an empty map, and the one-time migrations in ProjectMApplication then save that map over the new file. The documented procedure therefore disallows the listener during the write.

No app code changes. The listener restarting the whole app (and running ProjectMCore.init) in the background after any kill is noted in the handover as a product decision, not a defect; it is out of scope here.

Changes

  • tools/tv-diagnostics.sh:
    • Cold start: disallows the app's enabled notification listeners (read for the captured foreground Android user; left alone if notification access cannot be read), force-stops the app, waits up to 10 s until its process is gone, runs am start -W, and allows the listeners again right away. The listeners are also restored by the exit trap (Ctrl-C, errors); a failed restore is printed with the manual command, in the log and in summary.md.
    • The summary says cold start only when no app process existed before the start and ActivityManager logged Start proc <pid>:<package>/… for …activity, or Android reports LaunchState: COLD; without positive evidence it says unverified, and otherwise explains why the time is not a cold-start time.
    • The cold-started process had no listener access (no track titles), so it is stopped and the app is started again for the observation, reported as Observation start. New file am_start_cold.txt.
    • --package ID to test the profile build (nl.neerdael.projectmtv.profile) with a matching --apk or --no-install, without replacing the release app. Alternate package IDs are rejected with the default build or --release before adb/device access, because both modes supply the production package APK.
  • tools/tv-diagnostics.sh also captures a numeric Android user once, scopes installation, permission grants, listener commands and activity stops/starts to that user, and filters numeric ps UID/PID/NAME data by exact package process name and user. Failed, malformed or ambiguous lookups abort; the summary and manual listener-restore commands record the user. Signing-key recovery checks other users' exact package installations before deleting foreground-user data; another installation or unverifiable state is refused, and a retained conflict after the scoped retry is reported explicitly. No all-user removal is attempted.
  • docs/PROFILING.md: new Fixed settings for a benchmark procedure (if the listener is enabled, disallow it → force-stop → pidof empty → write → allow it again only if it was enabled → start → check surface=), with the measured reasons. Notification access ends up as it was. The procedure writes /data/user/<userId>/<package>/shared_prefs/projectm_settings.xml for the captured user and uses the same ID for its listener and activity commands; checked process queries and an exit trap protect the write/restore sequence.
  • docs/DIAGNOSTICS.md: --package, am_start_cold.txt, and a Cold start section explaining the method and the manual restore command.
  • README.md, AGENTS.md: the new option, supported package/APK-source combinations, the cold-start method and the listener restart lesson.
  • tools/test_tv_diagnostics.py and Android CI: Fake-adb tests cover 16 package/source combinations, Android 9/14-style user-10 launches alongside user-0 same-package processes, failed/malformed/ambiguous user/process queries, and listener restoration after a lookup failure, without a TV or build.

Release notes

Internal

  • tools/tv-diagnostics.sh measures a real cold start. Before, Android restarted the app for its notification listener right after the script stopped it, so the reported start time was usually a warm start. The script now disallows the listener briefly, waits until the app has exited, and allows the listener again; the summary labels each start time as cold or warm. New --package option to test the profile build with a matching --apk or --no-install. The default build and --release reject alternate package IDs before connecting to the TV, preventing installation of an unrelated APK or measurement of a stale profile build.
  • The profiling guide describes how to set fixed benchmark settings for the foreground Android user from outside the app without the listener restart replacing them with the previous run's settings or the defaults. Diagnostics uses the same captured user for app operations and process evidence, preventing other users' same-named processes from contaminating the measurements. Signing-conflict recovery refuses removal when another user owns the package or that state cannot be verified, preserving their data.

Validation

  • bash -n tools/tv-diagnostics.sh, shellcheck -S warning tools/tv-diagnostics.sh: clean.
  • Simulated adb (stub reproducing the listener restart): normal TV → cold; process kept alive → labelled warm; Android 10+ LaunchState path → cold; SIGTERM during the cold start → listener restored, exit 130; SIGTERM while allow_listener runs → the exit trap restores it; listener never enabled → nothing disallowed or allowed; restore failure → warning in log and summary; settings --user current unreadable → listener left alone, start labelled warm.
  • settings --user current get secure enabled_notification_listeners returns the list on the AM6 (Android 9) and the AM9 Pro (Android 14).
  • Branch merged with main at 6ef76b6 (v2.3.0) in fd39a19, because CI's version step rejected the 2.2 base; no conflicts.
  • After integrating PR feat(core): define legacy shader inputs and prove initialization #33 (main at 6244a3ec, merge 35a24c4c), the APK, native engine and Preset Lab CI checks passed. Subsequent multi-user review corrections pass python3 -m unittest discover -s .github/scripts/tests (80 tests), diagnostics regression tests (9 methods / 37 cases, including 16 option combinations plus multi-user, lookup-failure, signing-conflict preservation and recovery scenarios), bash -n, ShellCheck, git diff --check, profiling snippet sh -n, and strict MkDocs build. Final head 08c0666f2161e48d80f77a2f520f3ae930da428e: both push/PR APK builds, native engine tests and Preset Lab pass. Codex review completed for this head with no findings; all review threads are resolved.
  • PR feat(core): define legacy shader inputs and prove initialization #33 changes shader initialization/analyzer coverage, not the diagnostics script. It merged without conflicts; the final CI run validates the integrated APK, native engine and analyzer.
  • No app code changed, so native/JVM tests and builds were not rerun; ./gradlew :app:assembleProfile built the APK used on the TV.

On a TV

  • Device: Ugoos AM9 Pro (Android 14, panel 3840x2160, UI 1920x1080), profile build of this branch (nl.neerdael.projectmtv.profile), its listener enabled for the test. The Ugoos AM6 (Android 9) was asleep and was not used; Android 9 support of cmd notification allow_listener/disallow_listener was checked there read-only.
  • Root cause reproduced on Android 14: after am force-stop, Start proc …profile/u0a114 for service {…TrackListenerService} within a second and LaunchState: WARM on the next start.
  • tools/tv-diagnostics.sh 192.168.51.53:5555 --no-install --package nl.neerdael.projectmtv.profile --duration 30, 6 runs. Every cold start was a new process for next-top-activity with LaunchState: COLD; the observation start was the listener-started process. Listener enabled after each run; debug.projectmtv.preset empty. "Track titles off" appears only in the cold-start process; no dialog in the screenshots.
  • Settings procedure, alternating render_height 720/1080, checking every STATS surface=: single force-stop 8/8 correct (the race is timing-dependent and rarer on this box); second force-stop after the write 15/16 (one run got the defaults: 1080p Auto at the 30 fps cap); listener disallowed during the write 3/3 before the TV went to sleep.
  • debug.projectmtv.* properties cleared and app settings restored afterwards (profile settings file and listener list returned to their state before the test; the profile build stays installed).

Evidence

Run (AM9 Pro) Cold start TotalTime Observation start TotalTime
1 (first after install) 1443 ms, cold 1142 ms, warm
2 1115 ms, cold 519 ms, warm
3 843 ms, cold 618 ms, warm
4 848 ms, cold 625 ms, warm
5 758 ms, cold 755 ms, warm
6 800 ms, cold 499 ms, warm

Runs 1–3 still had the restore check's false warning (Android 14 writes the setting asynchronously), fixed in bf84ff0; the listener was in fact restored after each run. Runs 4–6 were made with the final script.

Docs and release

  • Documentation assessment: evaluated README.md, the user guide (docs/user-guide/*.md; none mentions the diagnostics script or benchmark settings), docs/DIAGNOSTICS.md, docs/PROFILING.md, docs/ARCHITECTURE.md (mentions the script only as a command for startup and FPS data; still accurate), AGENTS.md and the other Markdown files that mention tv-diagnostics.sh (RELEASE_NOTES.md, historical). Updated README.md (developer diagnostics paragraph), docs/DIAGNOSTICS.md, docs/PROFILING.md, AGENTS.md. The user guide needs no diagnostics-source change: its development page links the corrected README and contains no conflicting package command. Verified Pages destination: https://johnneerdael.github.io/ProjectM-TV/, published from docs/user-guide/ via .github/workflows/docs.yml; strict build passes after integrating feat(core): define legacy shader inputs and prove initialization #33.
  • The public Release notes section matches the final change and contains no placeholders or test logs
  • No routine version bump

Not tested / known limitations

  • The new multi-user corrections use fake-adb regression coverage and Android 9/14 source verification; they were not rerun on a physical TV. Older Android command availability remains unverified; unavailable user/process queries abort. Supplied APK IDs must match --package; the script does not inspect them.
  • Not run on the AM6 (asleep) or on an NVIDIA SHIELD; the Android 9 path (no LaunchState, Start proc … for activity) is covered by the simulation and by the handover's AM6 logs only.
  • The listener-disallowed settings procedure got 3 runs before the AM9 slept, not the planned 16.
  • Cold-start TotalTime varies run to run (758–848 ms on runs 4–6 after the first launches); it is now a cold-start measurement, not a stable benchmark.
  • If the script is killed with kill -9 during the cold start, the listener stays disallowed until restored by hand (command in docs/DIAGNOSTICS.md).

🤖 Generated with Claude Code

jneerdael-netskope and others added 3 commits October 4, 2026 22:18
The app's notification listener is rebound about a second after
am force-stop, which restarts the process, so the previous stop/sleep/
start measured a warm start. Disallow the app's enabled listeners
around a dedicated cold start, wait until the process is gone, label
the result cold or warm from the pid and ActivityManager's Start proc
line, and restore the listeners right away and in the exit trap. The
observation run uses a normal second start. Add --package to test the
profile build.

Document the second force-stop and the surface= check for settings
written from outside the app (docs/PROFILING.md).

Device validation on the AM6 pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tore

Android 14 logs 'for next-top-activity {component}' and 'for service
{component}': compare only the reason word. The listener setting is
written asynchronously on Android 14, so poll for up to 5 s before
warning that it was not restored (verified on an AM9 Pro).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A process restarted by the listener that reads projectm_settings while
it is being rewritten loads an empty map, and ProjectMApplication's
one-time migrations then save that map over the new file. A second
force-stop does not undo that: 1 of 16 runs on an AM9 Pro still got
the defaults. Disallow the listener, stop, write, allow it again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-04T22:47:19.260622Z 08c0666 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d9aba3823

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/PROFILING.md Resolved
Record whether the profile app's listener is enabled and disallow and
allow it only in that case, so the procedure does not grant notification
access (Codex review). Use a shell function instead of an unquoted
command variable so the snippet also works in zsh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 91454b9eb3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
settings get without --user reads user 0, while cmd notification
allow_listener/disallow_listener act on the current user. Read the
current user's enabled_notification_listeners in tv-diagnostics.sh and
in the profiling procedure, falling back to a plain read when an older
settings command rejects --user current (Codex review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc56e4f69e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
If the current user's enabled_notification_listeners cannot be read,
leave the listener alone instead of reading user 0's setting, which
could grant the current user access it never gave. The cold start is
then reported as warm if Android restarts the app (Codex review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9869fb50f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
Clear LISTENERS_OFF only after the restore loop, so a Ctrl-C, TERM or
lost adb connection during allow_listener leaves the exit trap to try
again instead of skipping the restore (Codex review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b819348f40

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
Without a Start proc line for the activity or LaunchState COLD, an empty
pid before the start does not prove a cold start (the listener could
rebind in between). Label it unverified instead of cold (Codex review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca75f78aae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
Require alternate package IDs to use a supplied matching APK or the installed app. Validate final option state before device access and cover source precedence in CI.
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 35a24c4c0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
Comment thread docs/PROFILING.md Resolved
Use a captured user ID for app operations and checked process discovery so other users cannot contaminate cold-start evidence. Write benchmark settings to that user’s data path and cover lookup failures and listener restoration.
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7ea3a02214

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/tv-diagnostics.sh Resolved
Check other users before removing foreground-user app data. Fail closed when installation ownership cannot be verified, and report retained signing conflicts without attempting broader uninstall.
@johnneerdael

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 08c0666f21

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@johnneerdael
johnneerdael merged commit d90e5ff into main Oct 4, 2026
13 checks passed
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.

2 participants