Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

tools/metal-run.sh: a measured Metal run in one command, from Git Bash on Windows as from a Mac

Claudee54452eb13a3
diff --git a/docs/windows.md b/docs/windows.md
index 0c9783c..aec592a 100644
--- a/docs/windows.md
+++ b/docs/windows.md
@@ -64,3 +64,31 @@ Nothing is flashed: everything is booted from RAM, and a reboot brings Android b
`system.apfs` built with `MODEM=1` if it uses the modem (`tools/modem-cycle-win.sh`).
- **`tools/modem-cycle.sh` fails under Git Bash:** use `tools/modem-cycle-win.sh`; the original puts the
macOS helpers in `tools/macos-bin` first on `PATH`, and they do not run on Windows.
+
+## Metal: a run, measured, from Windows
+
+The window server drawing through Metal (`METAL_PLUGIN=ca`) and the frame-rate bench, with one command in Git Bash.
+
+```sh
+# WSL, in ~/hakios: QuartzCore's library for CoreAnimation, then the image with the Metal plug-in and the bench.
+# Off a Mac, name a later CoreAnimation's library to take the rest of its shaders from (a Mac's
+# QuartzCore.framework/Versions/A/Resources/default.metallib copied over, or iOS 17's out of its IPSW):
+CA_QC_METALLIB=/mnt/c/.../default.metallib tools/ca-metallib.py ~/ca-share
+METAL_PLUGIN=ca RC_JOBS=rc.jobs.bench EXTRA_SHARE=~/ca-share PREBUILT=1 REST=1 SYSTEM=1 userland/build.sh
+cp userland/root.apfs userland/system.apfs /mnt/d/.../hakios/userland/
+
+# Git Bash, in the Windows checkout (the phone in Android is fine: the script takes it into the loader):
+tools/metal-run.sh # 180 s; the summary in logs/<time>-metal.txt
+MORE="metal_gmem=1" tools/metal-run.sh # with tiled rendering (each pass left in sysmem says why)
+SHOTS=20 tools/metal-run.sh # and a screenshot every 20 s, logs/<time>-shots/
+```
+
+`tools/metal-run.sh` keeps `/private/var` between boots by default (`ufs=2 ufs_write=1 var_store=1`, the partition
+`darwin` only; `PERSIST=0` for none), so the shader stages the window server compiles are compiled once. To put them
+into the image, so that not even the first boot compiles them: with the phone back in Android,
+
+```sh
+tools/metal-cache-seed.sh # reads darwin (read only, adb + root) into userland/hakios-metal-cache
+```
+
+and copy `userland/hakios-metal-cache` to WSL's `userland/` before the next `userland/build.sh`.
diff --git a/tools/ca-metallib.py b/tools/ca-metallib.py
index 955d253..2771eaa 100644
--- a/tools/ca-metallib.py
+++ b/tools/ca-metallib.py
@@ -11,6 +11,11 @@ variant a pipeline's label names (userland/src/HakiosMetalCA.m, hm_bitcode_for()
OUTDIR/QuartzCore-default.metallib is written; build the image with EXTRA_SHARE=OUTDIR and run with
ca_metallib=/redwood/share/<OUTDIR's name>/QuartzCore-default.metallib (userland/src/inject.c).
+
+Off a Mac (Windows, WSL, Linux): CA_QC_METALLIB=FILE names the later CoreAnimation's library to take the air64 slice
+from -- a copy of a Mac's QuartzCore.framework/Versions/A/Resources/default.metallib, or iPadOS/iOS 17's
+QuartzCore.framework/default.metallib out of its IPSW (7z x the root filesystem's dmg), both universal with an air64
+slice. Without it, the Mac's own path above.
"""
import os
import struct
@@ -19,7 +24,7 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import merge_metallib # noqa: E402
-MAC_QC = '/System/Library/Frameworks/QuartzCore.framework/Versions/A/Resources/default.metallib'
+MAC_QC = os.environ.get('CA_QC_METALLIB') or '/System/Library/Frameworks/QuartzCore.framework/Versions/A/Resources/default.metallib'
AIR64 = 0x01000017
@@ -51,6 +56,9 @@ def main():
lib = merge_metallib.read(tmp)
variants.setdefault(dict(lib['funcs'][0]['tags'])[b'NAME'], (lib['funcs'][0], lib['head']))
at += total
+ if not os.path.isfile(MAC_QC):
+ raise SystemExit('%s: not found. Off a Mac, name a later CoreAnimation\'s library: CA_QC_METALLIB=FILE '
+ '(a Mac\'s QuartzCore default.metallib copied over, or iOS 17\'s out of its IPSW)' % MAC_QC)
mac = os.path.join(out, '.air64.metallib')
open(mac, 'wb').write(air64_slice(MAC_QC))
lib = merge_metallib.read(mac)
diff --git a/tools/metal-run.sh b/tools/metal-run.sh
new file mode 100755
index 0000000..d662a91
--- /dev/null
+++ b/tools/metal-run.sh
@@ -0,0 +1,100 @@
+#!/bin/bash
+# One Metal run on the phone, measured: the window server drawing through Metal (an image built with METAL_PLUGIN=ca),
+# the frame-rate bench driving it, and what came of it in one summary. Git Bash on Windows, macOS and Linux alike.
+#
+# tools/metal-run.sh the run (the phone may be in Android, in its bootloader or in the loader)
+# SECS=180 tools/metal-run.sh its length in seconds (default 180: the bench's four rounds; see rc.jobs.bench)
+# MORE="metal_gmem=1" tools/... boot-args beside the ones below (a later one of the same name wins)
+# PERSIST=0 tools/metal-run.sh without var_store: /private/var starts empty and nothing is written to darwin
+# UPLOAD=0 tools/metal-run.sh keep the system image already in the phone's RAM (default: upload this copy's)
+# SHOTS=N a screenshot every N seconds during the run into logs/<stamp>-shots/ (default 0)
+#
+# What it needs, built in WSL (docs/windows.md) and copied here: userland/root.apfs and userland/system.apfs from
+# METAL_PLUGIN=ca RC_JOBS=rc.jobs.bench EXTRA_SHARE=<dir of QuartzCore-default.metallib> PREBUILT=1 REST=1 SYSTEM=1 userland/build.sh
+# (tools/ca-metallib.py writes that file; off a Mac with CA_QC_METALLIB=...). The kernel: hakios-kernel, else prebuilt/.
+#
+# Boot-args, and why each:
+# gpu=1 metal_fps=1 touch_lat=1 the GPU on, Metal's frame and pass counters, the "tl:" lines tools/frames.py reads
+# metal=1 the accelerator names the Metal plug-in (MiniIOMobileFramebuffer.cpp): without it
+# the window server finds no device and stays on OpenGL ES, saying nothing
+# touchscreen=1 the bench taps and swipes through the touch screen's driver
+# ca_metallib=... QuartzCore's library for the window server (tools/ca-metallib.py)
+# gpu_recover=1 a GPU hang is reset and counted, not the end of the run
+# anim_max=1 SpringBoard's animations ask for 80..120 frames a second
+# ufs=2 ufs_write=1 var_store=1 (PERSIST=1, the default) /private/var kept in the partition darwin between boots: the
+# compiled shader stages in /var/db/hakios-metal-cache survive, so only the first boot
+# ever compiles them (tools/metal-cache-seed.sh then puts them in the image)
+# The summary: logs/<stamp>-metal.txt -- tools/frames.py's frames per gesture, the HakiosMetal fps lines (the slowest
+# passes, each sysmem one with its reason), refusals counted by kind, and var_store's save.
+set -u
+cd "$(dirname "$(readlink -f "$0")")/.." || exit 1
+case "$(uname -s)" in
+MINGW*|MSYS*|CYGWIN*)
+ # Git Bash: tools/win-bin's flock, and Google's platform-tools where docs/windows.md puts them
+ export PATH="$PWD/tools/win-bin:/c/platform-tools:$PATH"
+ PY=$(command -v python || command -v python3) ;;
+*)
+ PY=$(command -v python3 || command -v python) ;;
+esac
+[ -n "$PY" ] || { echo "metal-run: no python on PATH"; exit 1; }
+for f in userland/root.apfs userland/system.apfs; do
+ [ -f "$f" ] || { echo "metal-run: $f is missing (build it in WSL: docs/windows.md, METAL_PLUGIN=ca)"; exit 1; }
+done
+command -v fastboot >/dev/null && command -v adb >/dev/null || { echo "metal-run: adb and fastboot are not on PATH (C:\\platform-tools)"; exit 1; }
+
+SECS=${SECS:-180}
+PERSIST=${PERSIST:-1}
+UPLOAD=${UPLOAD:-1}
+SHOTS=${SHOTS:-0}
+CA_METALLIB=${CA_METALLIB:-/redwood/share/ca-share/QuartzCore-default.metallib}
+ARGS="-v gpu=1 metal=1 metal_fps=1 touch_lat=1 touchscreen=1 ca_metallib=$CA_METALLIB gpu_recover=1 anim_max=1"
+[ "$PERSIST" = 1 ] && ARGS="$ARGS ufs=2 ufs_write=1 var_store=1"
+ARGS="$ARGS ${MORE:-}"
+stamp=$(date +%H%M%S)
+mkdir -p logs
+out=logs/$stamp-metal.txt
+
+echo "metal-run: the phone is $(./xl state)"
+./xl enter || { echo "metal-run: the phone did not reach the loader (./xl state; docs/windows.md, When something goes wrong)"; exit 1; }
+if [ "$UPLOAD" = 1 ]; then
+ ./xl ramdisk userland/system.apfs || { echo "metal-run: the system image did not upload"; exit 1; }
+fi
+
+shots_pid=
+if [ "$SHOTS" -gt 0 ] 2>/dev/null; then
+ mkdir -p logs/$stamp-shots
+ ( sleep 30; i=0; while [ $i -lt $(( SECS / SHOTS )) ]; do ./xl screenshot logs/$stamp-shots/$i.png >/dev/null 2>&1; i=$((i + 1)); sleep "$SHOTS"; done ) &
+ shots_pid=$!
+fi
+echo "metal-run: $SECS s with: $ARGS"
+XL_CMDLINE="$ARGS" XL_SECS=$SECS XL_FOLLOW=${XL_FOLLOW:-10} XL_FOLLOW_KEEP=1 ./xl run
+status=$?
+[ -n "$shots_pid" ] && kill "$shots_pid" 2>/dev/null
+log=logs/$(readlink logs/last.log 2>/dev/null || ls -t logs/*-run.log 2>/dev/null | head -1 | xargs -n1 basename)
+[ -f "$log" ] || { echo "metal-run: no run log (xl run said $status)"; exit 1; }
+
+{
+ echo "metal-run $stamp: $log, $SECS s, xl run $status"
+ echo "boot-args: $ARGS"
+ echo
+ echo "== frames per gesture (tools/frames.py)"
+ "$PY" -I tools/frames.py "$log" $(ls "$log".live.* 2>/dev/null) 2>&1 | head -60
+ echo
+ echo "== HakiosMetal"
+ grep -a 'HakiosMetal: fps:' "$log" | tail -40
+ grep -a -E 'HakiosMetal: (inplace_copy|CoreAnimation.s .*compiled as)' "$log" | sort | uniq -c | head
+ echo
+ echo "== refused (count, kind)"
+ grep -a 'HakiosMetal: refused:' "$log" | sed -E 's/.*refused: //; s/[0-9]+ vertices.*: /...: /' | sort | uniq -c | sort -rn | head -15
+ echo
+ echo "== shader cache and var_store"
+ grep -a -E 'var_store|hakios-metal-cache' "$log" | tail -12
+ echo
+ echo "== crashes"
+ grep -a -E 'crashed|Assertion failed|abort|panic' "$log" | head -10
+} > "$out" 2>&1
+cat "$out"
+echo
+echo "metal-run: the summary is $out; the whole log $log"
+[ "$PERSIST" = 1 ] && echo "metal-run: next, with the phone back in Android: tools/metal-cache-seed.sh (the stages into the next image)"
+exit $status