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

Measuring the stutter: cputop (which processes take the processors, every 5 s), rc.jobs.steady and rc.jobs.bench (the same gestures after the start-up work is over, four rounds), frames.py says which core each frame was drawn on, tl: y for every refresh told to the window server

shimoku743aef7e3923fb
diff --git a/tools/frames.py b/tools/frames.py
index 6ec6ca7..5dd40c3 100755
--- a/tools/frames.py
+++ b/tools/frames.py
@@ -21,9 +21,13 @@ to the next one or 2.5 s later -- is a segment; for each:
The thresholds are as the coordinator asked for them; 18.0 rather than 16.7 because a frame of a steady
60 comes 16.6 to 16.8 ms after the one before.
"""
+import os
import sys
OVER_ONE, OVER_TWO, SEGMENT_US = 18000, 34500, 2500000
+# the first cpu-id of a small core: the loader lists the boot core, then the big ones, then the small (xnuloader/loader.c);
+# on the phone with cpus=7 the Cortex-A55s are 4, 5 and 6 (FRAMES_SMALL_FROM= for another layout)
+SMALL_FROM = int(os.environ.get('FRAMES_SMALL_FROM', '4'))
def lines_of(paths):
@@ -52,7 +56,8 @@ def main():
if w[1] == 'p' and len(w) >= 8 and int(w[2]) in (1, 4):
gestures.append((int(w[7]), int(w[2]), int(w[3]), int(w[4]), int(w[5])))
elif w[1] == 'g' and len(w) >= 8:
- frames.append((int(w[4]), int(w[6]), int(w[7])))
+ # (and the core it ended on, "cpu N", where eagl.c says it: -1 where it does not)
+ frames.append((int(w[4]), int(w[6]), int(w[7]), int(w[9]) if len(w) >= 10 and w[8] == 'cpu' else -1))
elif w[1] == 's' and len(w) >= 5:
swaps[int(w[2])] = int(w[4])
elif w[1] == 'v' and len(w) >= 9:
@@ -74,9 +79,9 @@ def main():
drawn = [f for f in frames if f[1] <= swap]
if not drawn:
continue
- began, ended, draws = drawn[-1]
+ began, ended, draws, cpu = drawn[-1]
ready, shown, done = shows[sid]
- timeline.append(dict(began=began, ended=ended, swap=swap, ready=ready, shown=shown, done=done))
+ timeline.append(dict(began=began, ended=ended, swap=swap, ready=ready, shown=shown, done=done, cpu=cpu))
timeline.sort(key=lambda f: f['done'])
all_counts = [0, 0, 0]
kinds, previous = {}, '' # the same gesture done more than once: its frames together
@@ -117,6 +122,9 @@ def main():
idle = mean([b['began'] - a['ready'] for _, a, b in over1])
copy = mean([b['done'] - b['shown'] for _, a, b in over1])
line += f'; the slow ones: draw {draw:4.1f}, gpu {gpu:4.1f}, idle before {idle:5.1f}, copy {copy:3.1f} ms'
+ known = [b for _, a, b in over1 if b['cpu'] >= 0]
+ if known:
+ line += f', {100 * sum(b["cpu"] >= SMALL_FROM for b in known) / len(known):.0f}% drawn on small cores'
stalls = [w for w in waits if start <= w[0] < end]
if stalls:
line += f'; {len(stalls)} waits for the GPU ({sum(w[2] for w in stalls) / 1000:.0f} ms)'
@@ -134,6 +142,15 @@ def main():
if all_counts[0]:
print(f'all gestures: {all_counts[0]} frame steps, over 16.7 ms {100 * all_counts[1] / all_counts[0]:.0f}%, '
f'over 33 ms {100 * all_counts[2] / all_counts[0]:.0f}%')
+ # the window server's drawing by the kind of core it ended on (eagl.c's "cpu N")
+ known = [f for f in timeline if f['cpu'] >= 0]
+ if known:
+ small = [f['ended'] - f['began'] for f in known if f['cpu'] >= SMALL_FROM]
+ big = [f['ended'] - f['began'] for f in known if f['cpu'] < SMALL_FROM]
+ def avg(values):
+ return sum(values) / len(values) / 1000 if values else 0
+ print(f'drawing: {len(big)} frames on big cores ({avg(big):.1f} ms each), {len(small)} on small ones '
+ f'({avg(small):.1f} ms each)')
if __name__ == '__main__':
diff --git a/userland/build.sh b/userland/build.sh
index f022c32..5fb61ad 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -192,6 +192,8 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then
{ ${LDID:-ldid} -Ssrc/record_test.entitlements obj/record_test 2>/dev/null ||
echo "build.sh: no ldid: record_test is unsigned, and the system may not let it record"; }
$DYN -o obj/cpu_test src/cpu_test.c
+ # which processes take the processors, every few seconds (src/cputop.c; rc.jobs.cputop)
+ $DYN -o obj/cputop src/cputop.c
$DYN -framework CoreFoundation -framework IOSurface -o obj/cow_test src/cow_test.c
$DYN -framework CoreFoundation -framework CoreGraphics -framework IOSurface -o obj/cg_test src/cg_test.c
# what Darwin here believes about Metal, and what CoreImage does without it (src/metal_test.m);
@@ -278,6 +280,7 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then
fi
cp obj/hello_dyn obj/svc_test obj/surf_test obj/rwtouch obj/mg_test root/bin/
[ -f obj/cpu_test ] && cp obj/cpu_test root/bin/ # (a prebuilt/ from before this program has none)
+ [ -f obj/cputop ] && cp obj/cputop root/bin/
[ -f obj/cow_test ] && cp obj/cow_test root/bin/
[ -f obj/cg_test ] && cp obj/cg_test root/bin/
[ -f obj/shm_test ] && cp obj/shm_test root/bin/
diff --git a/userland/rc.jobs.bench b/userland/rc.jobs.bench
new file mode 100644
index 0000000..2289a03
--- /dev/null
+++ b/userland/rc.jobs.bench
@@ -0,0 +1,12 @@
+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist).
+# The frame-rate benchmark (RC_JOBS=rc.jobs.bench, touch_lat=1, SECS=180; tools/frames.py): unlock, a minute for the
+# start-up work to be over (src/cputop.c beside it says whether it was; the home screen's layout is the same in every
+# boot once the widgets are up), then four times the same round -- Settings opened from its icon, its list scrolled,
+# the home button, a page swipe there and back -- so that each kind of motion is measured four times in the same state
+# of the system and runs can be compared.
+/redwood/bin/cputop 5 &
+/redwood/bin/rwtouch wait 30 swipe 0.5 0.985 0.5 0.3 200 wait 60
+/redwood/bin/rwtouch tap 0.615 0.71 wait 3 zigzag 0.5 0.75 0.35 500 3 wait 2 button home wait 3 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 3 zigzag 0.5 0.75 0.35 500 3 wait 2 button home wait 3 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 3 zigzag 0.5 0.75 0.35 500 3 wait 2 button home wait 3 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 3 zigzag 0.5 0.75 0.35 500 3 wait 2 button home wait 3 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
diff --git a/userland/rc.jobs.cputop b/userland/rc.jobs.cputop
new file mode 100644
index 0000000..9547f9c
--- /dev/null
+++ b/userland/rc.jobs.cputop
@@ -0,0 +1,8 @@
+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist).
+# rc.jobs.scroll with cputop beside it (src/cputop.c): which processes take the processors while the frames are
+# measured, every 5 s in the log (RC_JOBS=rc.jobs.cputop, touch_lat=1 cpuload=100; tools/frames.py for the frames).
+/redwood/bin/cputop 5 &
+/redwood/bin/rwtouch wait 30 swipe 0.5 0.985 0.5 0.3 200 wait 8
+/redwood/bin/rwtouch swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 5 zigzag 0.5 0.75 0.35 500 6 wait 2 swipe 0.5 0.8 0.5 0.3 150 wait 2 swipe 0.5 0.3 0.5 0.8 150 wait 2 button home wait 3
+/redwood/bin/fps_info
diff --git a/userland/rc.jobs.steady b/userland/rc.jobs.steady
new file mode 100644
index 0000000..e021f51
--- /dev/null
+++ b/userland/rc.jobs.steady
@@ -0,0 +1,10 @@
+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist).
+# rc.jobs.scroll once the system has settled: unlock, then a minute for the start-up work (the widgets' first
+# renderings take four to five cores for seconds: src/cputop.c), then the same gestures twice, so that what is measured
+# is the steady state; cputop beside it says whether it was (RC_JOBS=rc.jobs.steady, touch_lat=1, SECS=200).
+/redwood/bin/cputop 5 &
+/redwood/bin/rwtouch wait 30 swipe 0.5 0.985 0.5 0.3 200 wait 60
+/redwood/bin/rwtouch swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 5 zigzag 0.5 0.75 0.35 500 6 wait 2 swipe 0.5 0.8 0.5 0.3 150 wait 2 swipe 0.5 0.3 0.5 0.8 150 wait 2 button home wait 3
+/redwood/bin/rwtouch swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 1 swipe 0.85 0.5 0.15 0.5 300 wait 1 swipe 0.15 0.5 0.85 0.5 300 wait 2
+/redwood/bin/rwtouch tap 0.615 0.71 wait 5 zigzag 0.5 0.75 0.35 500 6 wait 2 swipe 0.5 0.8 0.5 0.3 150 wait 2 swipe 0.5 0.3 0.5 0.8 150 wait 2 button home wait 3
diff --git a/userland/src/cputop.c b/userland/src/cputop.c
new file mode 100644
index 0000000..0ee3960
--- /dev/null
+++ b/userland/src/cputop.c
@@ -0,0 +1,153 @@
+/*
+ * Which processes take the processors: every few seconds, the processor time each process was given since the
+ * last look (user and system together), the most first, as one line for the run's log:
+ *
+ * cputop: 25 s: 412% in all: backboardd[13] 96% SpringBoard[41] 88% WeatherWidget[131] 40% ...
+ *
+ * (100% is one core's whole time.) tools/frames.py says when the cores were all busy ("cores busy") while the
+ * window server's frames came late; this says with whom it shared them. A process born since the last look counts
+ * with all the time it has had.
+ *
+ * usage: cputop [SECONDS [LOOKS]] every SECONDS (default 5), LOOKS times (default: until the run ends)
+ */
+#include <mach/mach_time.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* libproc's, which the iPhoneOS SDK does not declare (XNU's bsd/sys/proc_info.h, libsyscall/wrappers/libproc) */
+#define PROC_ALL_PIDS 1
+#define PROC_PIDTASKINFO 4
+struct proc_taskinfo {
+ uint64_t pti_virtual_size;
+ uint64_t pti_resident_size;
+ uint64_t pti_total_user; /* in mach_absolute_time's units */
+ uint64_t pti_total_system;
+ uint64_t pti_threads_user;
+ uint64_t pti_threads_system;
+ int32_t pti_policy;
+ int32_t pti_faults;
+ int32_t pti_pageins;
+ int32_t pti_cow_faults;
+ int32_t pti_messages_sent;
+ int32_t pti_messages_received;
+ int32_t pti_syscalls_mach;
+ int32_t pti_syscalls_unix;
+ int32_t pti_csw;
+ int32_t pti_threadnum;
+ int32_t pti_numrunning;
+ int32_t pti_priority;
+};
+int proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize);
+int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize);
+int proc_name(int pid, void *buffer, uint32_t buffersize);
+
+#define MAX_PROCS 2048
+#define SHOWN 10
+
+struct sample {
+ int pid;
+ uint64_t time; /* user + system, mach_absolute_time's units */
+};
+
+static struct sample last[MAX_PROCS], now[MAX_PROCS];
+static int last_count;
+
+struct share {
+ int pid;
+ uint64_t time;
+};
+
+static int
+by_time(const void *a, const void *b)
+{
+ const struct share *x = a, *y = b;
+
+ return x->time < y->time ? 1 : x->time > y->time ? -1 : 0;
+}
+
+static uint64_t
+time_before(int pid, int *found)
+{
+ for (int i = 0; i < last_count; i++) {
+ if (last[i].pid == pid) {
+ *found = 1;
+ return last[i].time;
+ }
+ }
+ *found = 0;
+ return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+ unsigned seconds = argc > 1 ? (unsigned)atoi(argv[1]) : 5, looks = argc > 2 ? (unsigned)atoi(argv[2]) : 0;
+ static int pids[MAX_PROCS];
+ static struct share shares[MAX_PROCS];
+ mach_timebase_info_data_t timebase;
+ uint64_t start = mach_absolute_time(), before = start;
+
+ if (seconds == 0) {
+ seconds = 5;
+ }
+ mach_timebase_info(&timebase);
+ setvbuf(stdout, NULL, _IOLBF, 0);
+ for (unsigned look = 0; looks == 0 || look <= looks; look++) {
+ int bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids)), count = 0, shown = 0;
+ uint64_t at = mach_absolute_time(), all = 0;
+
+ for (int i = 0; i < bytes / (int)sizeof(int) && count < MAX_PROCS; i++) {
+ struct proc_taskinfo info;
+
+ if (pids[i] <= 0 || proc_pidinfo(pids[i], PROC_PIDTASKINFO, 0, &info, sizeof(info)) != sizeof(info)) {
+ continue;
+ }
+ now[count].pid = pids[i];
+ now[count].time = info.pti_total_user + info.pti_total_system;
+ count++;
+ }
+ if (look > 0) {
+ int n = 0;
+
+ for (int i = 0; i < count; i++) {
+ int found;
+ uint64_t then = time_before(now[i].pid, &found);
+
+ if (now[i].time > then) {
+ shares[n].pid = now[i].pid;
+ shares[n].time = now[i].time - then;
+ all += shares[n].time;
+ n++;
+ }
+ }
+ qsort(shares, (size_t)n, sizeof(shares[0]), by_time);
+ /* the shares as percent of one core over the time between the looks (both in mach_absolute_time's units) */
+ double span = (double)(at - before);
+ printf("cputop: %llu s: %.0f%% in all:", (unsigned long long)((at - start) * timebase.numer / timebase.denom / 1000000000ull),
+ span > 0 ? 100.0 * (double)all / span : 0);
+ for (int i = 0; i < n && shown < SHOWN; i++) {
+ char name[64] = "?";
+ double percent = span > 0 ? 100.0 * (double)shares[i].time / span : 0;
+
+ if (percent < 1) {
+ break;
+ }
+ proc_name(shares[i].pid, name, sizeof(name));
+ printf(" %s[%d] %.0f%%", name, shares[i].pid, percent);
+ shown++;
+ }
+ printf("\n");
+ }
+ memcpy(last, now, sizeof(now[0]) * (size_t)count);
+ last_count = count;
+ before = at;
+ if (looks != 0 && look == looks) {
+ break;
+ }
+ sleep(seconds);
+ }
+ return 0;
+}
diff --git a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
index 47af973..93d04b5 100644
--- a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
+++ b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
@@ -1496,7 +1496,12 @@ IOMobileFramebufferUserClient::vsync(void)
* only then lets go of the old one, so the port in `ref` cannot be released while it is sent to
* (the send does not wait: a full queue drops the message)
*/
- sendAsyncResult64WithOptions(ref, kIOReturnSuccess, args, 6, kIOUserNotifyOptionCanDrop);
+ IOReturn sent = sendAsyncResult64WithOptions(ref, kIOReturnSuccess, args, 6, kIOUserNotifyOptionCanDrop);
+ if (gTouchLat) {
+ /* "tl: y COUNT due US at US SENT": the refresh told to the window server, when it was due, when it went, and
+ * whether it went (0) or was dropped on a full queue -- for the window server's idle gaps (tools/frames.py) */
+ hakios_tl("tl: y %llu due %llu at %llu %#x\n", fVSyncCount, micros(now), micros(mach_absolute_time()), sent);
+ }
}
IOLockUnlock(fLock);
}