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
Metal's compiled stages kept across boots, and seeded into the image; each sysmem pass says why
Claude8ecae63d10a6
diff --git a/tools/metal-cache-seed.sh b/tools/metal-cache-seed.shnew file mode 100755index 0000000..d64ff87--- /dev/null+++ b/tools/metal-cache-seed.sh@@ -0,0 +1,31 @@+#!/bin/bash+# Metal's compiled shader stages from the last run into the next image: no boot compiles them again.+#+# tools/metal-cache-seed.sh the phone in Android (adb, root): read var_store's newest archive off+# darwin (read only), take db/hakios-metal-cache out of it into+# userland/hakios-metal-cache; userland/build.sh puts that into the image+# at /redwood/share/hakios-metal-cache, where the window server looks after+# its own cache (HakiosMetalCA.m: HM_CACHE_SEED)+# tools/metal-cache-seed.sh --image F the same from a copy of darwin taken from byte 8589934592+#+# What fills the cache in the first place: a Metal run with var_store=1 ufs_write=1 (tools/metal-run.sh does so by+# default), whose orderly end saves /private/var into darwin. Works under Git Bash on Windows (python3 and adb on PATH).+set -e+cd "$(dirname "$(readlink -f "$0")")/.."+PY=$(command -v python3 || command -v python)+OUT=userland/hakios-metal-cache+rm -rf "$OUT.new"+"$PY" -I tools/var-store-read.py "$@" extract db/hakios-metal-cache "$OUT.new"+n=$(ls "$OUT.new" 2>/dev/null | grep -c '\.stage$' || true)+if [ "$n" = 0 ]; then+ rm -rf "$OUT.new"+ echo "metal-cache-seed: the store holds no compiled stages (was the run a Metal one, with var_store=1 ufs_write=1?)"+ exit 1+fi+# (a stage half written when the save came is a .tmp: never one of the cache's)+rm -f "$OUT.new"/*.tmp+# what was seeded before and is not in this store is kept: another build's stages are only room+mkdir -p "$OUT"+cp "$OUT.new"/*.stage "$OUT"/+rm -rf "$OUT.new"+echo "metal-cache-seed: $n stages from the phone; $(ls "$OUT" | wc -l | tr -d ' ') in $OUT; rebuild the image (userland/build.sh) to carry them"diff --git a/tools/var-store-read.py b/tools/var-store-read.pynew file mode 100755index 0000000..c33d161--- /dev/null+++ b/tools/var-store-read.py@@ -0,0 +1,231 @@+#!/usr/bin/env python3+"""Read what var_store kept of Darwin's /private/var, from the phone's darwin partition, and write nothing.++usage:+ var-store-read.py show both slots' headers: generation, entries, bytes, when+ var-store-read.py list [PREFIX] the newest good archive's entries (under PREFIX, a path in /private/var)+ var-store-read.py extract PREFIX DEST the files under PREFIX into DEST (directories and files only)+ var-store-read.py selftest an archive made here in var_store's format, read back++The store is userland/src/var_store.c's: two slots of 2 GiB from darwin's byte 8589934592, each a header block+(struct head) and then a stream of entries (struct entry, its path, its extended attributes, its contents, padded+to 8 bytes), whose CRC-32 is in the header. The newest slot whose header and stream both check out is read; one+that does not check out is never half used.++Where it reads from, read only, either way:+ --image FILE a copy of darwin from byte 8589934592 (dd if=/dev/block/by-name/darwin bs=4096 skip=2097152)+ (default) the phone in Android, over adb, as root: `su -c dd` of /dev/block/by-name/darwin, the blocks+ needed and no others. Under Git Bash on Windows as anywhere else (adb exec-out passes bytes as+ they are).++What it is for: tools/metal-cache-seed.sh takes db/hakios-metal-cache (Metal's compiled shader stages) out of+the last run's store, so that the next image carries them and no boot compiles them again.+"""+import os+import struct+import subprocess+import sys+import tempfile+import zlib++BLOCK = 4096+STORE_AT = 8589934592+SLOT_BYTES = 0x80000000+SLOTS = 2+HEAD = struct.Struct("<8sIIQQQQII") # magic version slot generation bytes entries seconds crc head_crc+ENTRY = struct.Struct("<IIIIIIIIqQ") # kind mode uid gid flags pathlen nxattr mtime_nsec mtime_sec size+MAGIC = b"RDWVAR\0\0"+ENT_DIR, ENT_FILE, ENT_SYMLINK, ENT_HARDLINK, ENT_DIRTIME = 1, 2, 3, 4, 5+DEVICE = "/dev/block/by-name/darwin"+++class Source:+ """Bytes of darwin counted from STORE_AT."""++ def __init__(self, image=None):+ self.image = image++ def read(self, offset, length):+ if self.image is not None:+ with open(self.image, "rb") as f:+ f.seek(offset)+ return f.read(length)+ first = offset // BLOCK+ count = (offset + length + BLOCK - 1) // BLOCK - first+ skip = STORE_AT // BLOCK + first+ # (read only: if= the partition by name, no of=; the device is darwin's or dd fails)+ cmd = ["adb", "exec-out", "su", "-c", f"dd if={DEVICE} bs={BLOCK} skip={skip} count={count} 2>/dev/null"]+ data = subprocess.run(cmd, stdout=subprocess.PIPE, check=True).stdout+ if len(data) != count * BLOCK:+ raise SystemExit(f"var-store-read: adb gave {len(data)} bytes where {count * BLOCK} were asked for "+ "(is the phone in Android, with root for the shell?)")+ start = offset - first * BLOCK+ return data[start:start + length]+++def head_crc(raw):+ zeroed = raw[:HEAD.size - 4] + b"\0\0\0\0"+ return zlib.crc32(zeroed) & 0xffffffff+++def read_head(src, slot):+ raw = src.read(slot * SLOT_BYTES, HEAD.size)+ if len(raw) < HEAD.size:+ return None+ magic, version, s, generation, nbytes, entries, seconds, crc, hcrc = HEAD.unpack(raw)+ if magic != MAGIC or version != 1 or s != slot or nbytes > SLOT_BYTES - BLOCK or hcrc != head_crc(raw):+ return None+ return dict(slot=slot, generation=generation, bytes=nbytes, entries=entries, seconds=seconds, crc=crc)+++def newest(src):+ best = None+ for slot in range(SLOTS):+ h = read_head(src, slot)+ if h is not None and (best is None or h["generation"] > best["generation"]):+ best = h+ return best+++def stream_of(src, h):+ data = src.read(h["slot"] * SLOT_BYTES + BLOCK, h["bytes"])+ if len(data) != h["bytes"] or (zlib.crc32(data) & 0xffffffff) != h["crc"]:+ raise SystemExit(f"var-store-read: slot {h['slot']}: the archive's checksum is not its header's: not read")+ return data+++def entries(data):+ at = 0+ while at < len(data):+ if len(data) - at < ENTRY.size:+ raise SystemExit("var-store-read: an entry cut short")+ kind, mode, uid, gid, flags, pathlen, nxattr, nsec, sec, size = ENTRY.unpack_from(data, at)+ at += ENTRY.size+ path = data[at:at + pathlen].decode("utf-8", "replace")+ at += pathlen+ if kind != ENT_DIRTIME:+ for _ in range(nxattr):+ namelen, vlen = struct.unpack_from("<II", data, at)+ at += 8 + namelen + vlen+ body = data[at:at + size] if kind != ENT_DIR else b""+ at += size if kind != ENT_DIR else 0+ else:+ body = b""+ at += (-at) % 8+ if at > len(data):+ raise SystemExit("var-store-read: an entry runs past the archive")+ yield dict(kind=kind, mode=mode, path=path, size=size, mtime=sec, body=body)+++def under(path, prefix):+ prefix = prefix.strip("/")+ return prefix == "" or path == prefix or path.startswith(prefix + "/")+++def main(argv):+ image = None+ if len(argv) > 2 and argv[1] == "--image":+ image = argv[2]+ argv = argv[:1] + argv[3:]+ if len(argv) < 2:+ print(__doc__)+ return 2+ cmd = argv[1]+ if cmd == "selftest":+ return selftest()+ src = Source(image)+ if cmd == "show":+ for slot in range(SLOTS):+ h = read_head(src, slot)+ print(f"slot {slot}: " + ("nothing good" if h is None else+ f"generation {h['generation']}, {h['entries']} entries, {h['bytes']} bytes, written at {h['seconds']}"))+ return 0+ h = newest(src)+ if h is None:+ print("var-store-read: no slot holds a good archive (nothing saved yet: var_store=1 ufs_write=1)")+ return 1+ data = stream_of(src, h)+ if cmd == "list":+ prefix = argv[2] if len(argv) > 2 else ""+ for e in entries(data):+ if e["kind"] != ENT_DIRTIME and under(e["path"], prefix):+ print(f"{e['kind']} {e['mode']:04o} {e['size']:10d} {e['path']}")+ return 0+ if cmd == "extract" and len(argv) == 4:+ prefix, dest = argv[2].strip("/"), argv[3]+ n = 0+ for e in entries(data):+ if not under(e["path"], prefix) or e["kind"] not in (ENT_DIR, ENT_FILE):+ continue+ rel = e["path"][len(prefix):].lstrip("/")+ if ".." in rel.split("/"):+ continue+ out = os.path.join(dest, rel) if rel else dest+ if e["kind"] == ENT_DIR:+ os.makedirs(out, exist_ok=True)+ else:+ os.makedirs(os.path.dirname(out) or ".", exist_ok=True)+ with open(out, "wb") as f:+ f.write(e["body"])+ n += 1+ print(f"var-store-read: generation {h['generation']}: {n} files of {prefix or '/'} into {dest}")+ return 0+ print(__doc__)+ return 2+++def selftest():+ def entry(kind, path, body=b"", mode=0o644, xattrs=()):+ x = b"".join(struct.pack("<II", len(k), len(v)) + k + v for k, v in xattrs)+ e = ENTRY.pack(kind, mode, 0, 0, 0, len(path), len(xattrs), 0, 1700000000, len(body) if kind != ENT_DIR else 0)+ b = e + path.encode() + (x if kind != ENT_DIRTIME else b"") + (body if kind not in (ENT_DIR, ENT_DIRTIME) else b"")+ return b++ stream = b""+ for piece in (entry(ENT_DIR, "", mode=0o755), entry(ENT_DIR, "db", mode=0o755),+ entry(ENT_DIR, "db/hakios-metal-cache", mode=0o755),+ entry(ENT_FILE, "db/hakios-metal-cache/0123.stage", b"HMCS" + bytes(range(37))),+ entry(ENT_FILE, "db/hakios-metal-cache/4567.stage", b"HMCS", xattrs=((b"com.x", b"yz"),)),+ entry(ENT_FILE, "db/other", b"not this one"),+ entry(ENT_SYMLINK, "db/hakios-metal-cache/link", b"0123.stage"),+ entry(ENT_DIRTIME, "db/hakios-metal-cache"), entry(ENT_DIRTIME, "db")):+ stream += piece + b"\0" * ((-len(piece)) % 8)+ def head(slot, generation, data):+ raw = HEAD.pack(MAGIC, 1, slot, generation, len(data), 9, 1700000001, zlib.crc32(data) & 0xffffffff, 0)+ return HEAD.pack(MAGIC, 1, slot, generation, len(data), 9, 1700000001, zlib.crc32(data) & 0xffffffff, head_crc(raw))+ failures = 0+ with tempfile.TemporaryDirectory() as tmp:+ img = os.path.join(tmp, "store")+ with open(img, "wb") as f:+ # slot 0 an older generation, slot 1 the newest: the newest is read+ f.write(head(0, 1, b"").ljust(BLOCK, b"\0"))+ f.seek(SLOT_BYTES)+ f.write(head(1, 2, stream).ljust(BLOCK, b"\0") + stream)+ src = Source(img)+ h = newest(src)+ failures += h is None or h["slot"] != 1+ dest = os.path.join(tmp, "out")+ main(["x", "--image", img, "extract", "db/hakios-metal-cache", dest])+ got = sorted(os.listdir(dest))+ failures += got != ["0123.stage", "4567.stage"]+ failures += open(os.path.join(dest, "0123.stage"), "rb").read() != b"HMCS" + bytes(range(37))+ # a torn archive (one byte changed) is refused whole, not half read+ with open(img, "r+b") as f:+ f.seek(SLOT_BYTES + BLOCK + 60)+ f.write(b"\xff")+ try:+ stream_of(src, newest(src))+ failures += 1+ except SystemExit:+ pass+ # a header whose own checksum is wrong is not a slot+ with open(img, "r+b") as f:+ f.seek(SLOT_BYTES + 16)+ f.write(b"\x09")+ failures += read_head(src, 1) is not None+ print("var-store-read selftest: " + ("ok" if failures == 0 else f"{failures} failures"))+ return 1 if failures else 0+++if __name__ == "__main__":+ sys.exit(main(sys.argv))diff --git a/userland/build.sh b/userland/build.shindex d972869..489c297 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -175,6 +175,16 @@ for d in $EXTRA_SHARE; do[ -d "$d" ] || { echo "build.sh: EXTRA_SHARE: $d is not a directory"; exit 1; }cp -rL "$d" root/share/done+# Metal's compiled shader stages from an earlier boot (tools/metal-cache-seed.sh puts them in+# userland/hakios-metal-cache): the window server reads them from /redwood/share/hakios-metal-cache+# (HakiosMetalCA.m: HM_CACHE_SEED) instead of compiling CoreAnimation's shaders during the first minute+# of the boot. Stages compiled by another build of the compiler are filed under other names and simply+# not found, so a stale seed costs room and nothing else.+rm -rf root/share/hakios-metal-cache+if [ -d hakios-metal-cache ] && [ -n "$(ls hakios-metal-cache 2>/dev/null)" ]; then+ cp -r hakios-metal-cache root/share/+ echo "build.sh: $(ls hakios-metal-cache | wc -l | tr -d ' ') compiled Metal stages seeded into /redwood/share/hakios-metal-cache"+fiif [ -d $DARWIN/rootfs/usr/lib ]; thenif [ -z "$PREBUILT" ]; then# ordinary dynamically linked programs of our own, built against the Darwin SDK@@ -383,6 +393,9 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then# the prefix with the root on the flash, downloads every package and index there, and without it each# download ended in "No such file or directory"printf 'd\t700\t501\t501\t/private/var/mobile/tmp\n' >> root/etc/var.skel+ # Metal's compiled shader stages (HakiosMetalCA.m: HM_CACHE_DIR): in /var/db so that var_store keeps them (it+ # leaves tmp out), open to every user like tmp, since the window server may not be root+ printf 'd\t1777\t0\t0\t/private/var/db/hakios-metal-cache\n' >> root/etc/var.skel# A key of ours for root, when there is one: ssh/authorized_keys, never in the repository (like# fw-*). dropbear from the package manager's prefix reads it and lets us in over the phone's# Wi-Fi, which saves rebuilding an image for every question one wants to ask a running Darwin. Itdiff --git a/userland/src/HakiosMetalCA.m b/userland/src/HakiosMetalCA.mindex 0392dea..a453ee5 100644--- a/userland/src/HakiosMetalCA.m+++ b/userland/src/HakiosMetalCA.m@@ -552,6 +552,8 @@ static void hm_fps_waited(double seconds); /* metal_fps: time in -waitUntilComstatic void hm_fps_drew(double seconds, unsigned cpu); /* metal_fps: our time in a draw call */static void hm_fps_core(double seconds); /* metal_fps: the core's time in a draw call */static BOOL hm_fps_on(void); /* metal_fps: command buffers timed pass by pass */+#define HM_CACHE_DIR "/private/var/db/hakios-metal-cache" /* compiled stages, kept across boots by var_store */+#define HM_CACHE_SEED "/redwood/share/hakios-metal-cache" /* the same, baked into the image (read-only) */static BOOL hm_gmem_on(void); /* metal_gmem=1: tiled rendering where a pass allows it */- (void)hakiosShadowYCbCr:(IOSurfaceRef)surface format:(MTLPixelFormat)format;- (void)hakiosRefreshShadow;@@ -1978,9 +1980,23 @@ ca_prof_start(void)hm_library_stand_ins();ca_prof_start();/* (hakios) the window server's compiled stages kept on disk (hakios-metal: compileStage's cache), so that a- * CoreAnimation shader compiled once is not compiled again by the next window server; and no listings */- mkdir("/var/tmp/hakios-metal-cache", 0755);- setenv("HAKIOS_METAL_CACHE", "/var/tmp/hakios-metal-cache", 0);+ * CoreAnimation shader compiled once is not compiled again by the next window server; and no listings.+ * In /var/db, not /var/tmp: var_store (var_store=1 ufs_write=1) keeps /private/var across boots but leaves tmp+ * out by design, so a cache in /var/tmp started empty at every boot and the first minute of every boot paid the+ * 66 to 103 compiles again. A cache in the image (HM_CACHE_SEED, read-only, put there by+ * tools/metal-cache-seed.sh from an earlier boot's) is looked in after it and never written. */+ /* (the skeleton makes HM_CACHE_DIR 1777, init.c from var.skel; an image without that line, or a process that may not+ * write there, falls back on /var/tmp as before: a cache for this boot only is better than none) */+ mkdir(HM_CACHE_DIR, 01777);+ setenv("HAKIOS_METAL_CACHE", access(HM_CACHE_DIR, W_OK | X_OK) == 0 ? HM_CACHE_DIR : (mkdir("/var/tmp/hakios-metal-cache", 0755),+ "/var/tmp/hakios-metal-cache"), 0);+ {+ struct stat st;++ if (stat(HM_CACHE_SEED, &st) == 0 && S_ISDIR(st.st_mode)) {+ setenv("HAKIOS_METAL_CACHE_SEED", HM_CACHE_SEED, 0);+ }+ }setenv("HAKIOS_METAL_LISTING", "0", 0);hm_ensure_spi([self class]);hm_functions_encode_from_metadata();@@ -5279,7 +5295,7 @@ static struct {unsigned passes, draws, dispatches, copies, gmemPasses, tiles;double shadowIn, shadowOut; /* seconds in surface copies: taken from the surface, written back into it */unsigned shadowIns, shadowOuts;- struct { unsigned kind, width, height, draws, tiles; double us; } slowest[4]; /* the slowest passes of the period */+ struct { unsigned kind, width, height, draws, tiles; double us; char why[96]; } slowest[4]; /* the slowest passes of the period (WHY: a sysmem pass's reason) */double passUs; /* all passes timed, together */char gmemWhy[160]; /* the last reason a pass stayed in sysmem */struct { char name[96]; unsigned count; double us; } programs[48]; /* metal_fps=draws: GPU time by program */@@ -5449,7 +5465,8 @@ hm_fps_note(BOOL present, double gpu, double latency, hakios_metal_cb *cb)fewShown += few ? 1 : 0;if ((slowUs > 20000 && !hm_fps.heavyShown) || few) {hm_fps.heavyShown = YES;- hm_print("fps: ... a heavy pass, %.1f ms; its draws:\n", slowUs / 1e3);+ hm_print("fps: ... a heavy pass, %.1f ms%s%s; its draws:\n", slowUs / 1e3,+ hakios_metal_cb_pass_sysmem_why(cb, slow)[0] != 0 ? ", in sysmem: " : "", hakios_metal_cb_pass_sysmem_why(cb, slow));for (unsigned i = 0;; i++) {const char *name = NULL;unsigned pass, area[4];@@ -5500,9 +5517,12 @@ hm_fps_note(BOOL present, double gpu, double latency, hakios_metal_cb *cb)if (all != nil) {unsigned attached = 0, touched = 0;hakios_metal_cb_pass_attachments(cb, i, &attached, &touched);+ const char *sysWhy = hakios_metal_cb_pass_sysmem_why(cb, i);+[all appendFormat:@" [%u %ux%u %u draws%@ %.2f ms]", kind, width, height, draws,hakios_metal_cb_pass_tiles(cb, i) > 0 ? [NSString stringWithFormat:@" %d tiles (attachments %#x, touched %#x)",- hakios_metal_cb_pass_tiles(cb, i), attached, touched] : @"",+ hakios_metal_cb_pass_tiles(cb, i), attached, touched] :+ kind == 0 && sysWhy[0] != 0 ? [NSString stringWithFormat:@" in sysmem: %s", sysWhy] : @"",us / 1e3];}hm_fps.passUs += us;@@ -5514,6 +5534,7 @@ hm_fps_note(BOOL present, double gpu, double latency, hakios_metal_cb *cb)hm_fps.slowest[k].height = height;hm_fps.slowest[k].draws = draws;hm_fps.slowest[k].tiles = (unsigned)MAX(hakios_metal_cb_pass_tiles(cb, i), 0);+ strlcpy(hm_fps.slowest[k].why, hakios_metal_cb_pass_sysmem_why(cb, i), sizeof(hm_fps.slowest[k].why));hm_fps.slowest[k].us = us;break;}@@ -5578,19 +5599,23 @@ hm_fps_note(BOOL present, double gpu, double latency, hakios_metal_cb *cb)}if (hm_fps.slowest[0].us > 0) {static const char *const kinds[] = {"draw", "copy", "clear", "dispatch"};- char line[400];+ char line[1024];size_t n = 0;- for (int k = 0; k < 4 && hm_fps.slowest[k].us > 0; k++) {- char tiles[24] = "";+ for (int k = 0; k < 4 && hm_fps.slowest[k].us > 0 && n < sizeof(line) - 1; k++) {+ char tiles[128] = "";if (hm_fps.slowest[k].tiles != 0) {snprintf(tiles, sizeof(tiles), " in %u tiles", hm_fps.slowest[k].tiles);+ } else if (hm_fps.slowest[k].kind == 0 && hm_fps.slowest[k].why[0] != 0) {+ /* (why it stayed in sysmem: SpringBoard's "home" pass, 1127x2438 and 33 draws, did with no reason seen) */+ snprintf(tiles, sizeof(tiles), " in sysmem (%s)", hm_fps.slowest[k].why);}n += (size_t)snprintf(line + n, sizeof(line) - n, "%s %s %ux%u%s%u%s %.2f ms", k ? ";" : "",kinds[hm_fps.slowest[k].kind & 3], hm_fps.slowest[k].width, hm_fps.slowest[k].height,hm_fps.slowest[k].kind == 0 ? " draws " : " ", hm_fps.slowest[k].kind == 0 ? hm_fps.slowest[k].draws : 0,tiles, hm_fps.slowest[k].us / 1e3);+ n = MIN(n, sizeof(line) - 1); /* (snprintf says what it would have written: never past the line) */}hm_print("fps: ... passes timed by the GPU: %.0f ms in all; the slowest:%s\n", hm_fps.passUs / 1e3, line);}