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
userland: measuring a boot in stretches -- init's sleep and >>, vol_ls zero and cpuload, apfs_mmap_test -q
vcvkk75087a8512b8
diff --git a/userland/rc.jobs.flashlock b/userland/rc.jobs.flashlocknew file mode 100644index 0000000..d5d8f6c--- /dev/null+++ b/userland/rc.jobs.flashlock@@ -0,0 +1,22 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# The iOS root volume on the phone's flash as the root (flashboot.packfs resident, root_apfs=ufs_darwin+# ufs=2 ufs_native=1). The APFS driver's numbers (kern.apfs_stats: the volume's lock by who takes it) for+# three stretches: the boot, until 25 s after this list began (nothing of ours meanwhile, so that+# SpringBoard's start is not shared); the next 30 s, iOS left to itself (only how busy each processor+# was, sampled: vol_ls cpuload); then our reads -- /usr/sbin+# through read(2) and a mapping (sum 3095b9e298a4df17) and /usr/libexec twice each way, only the totals+# printed, each with the driver's numbers for itself. The results go to a file and are shown at the end,+# where the kernel's log keeps them (its middle is lost to launchd's own lines).+sleep 25+/redwood/bin/vol_ls sysctl kern.apfs_stats >> /private/var/results+/redwood/bin/vol_ls zero kern.apfs_stats+/redwood/bin/vol_ls cpuload 30 >> /private/var/results+/redwood/bin/vol_ls sysctl kern.apfs_stats >> /private/var/results+/redwood/bin/apfs_mmap_test -q read /usr/sbin 65536 >> /private/var/results+/redwood/bin/apfs_mmap_test -q mmap /usr/sbin >> /private/var/results+/redwood/bin/apfs_mmap_test -q read /usr/libexec 65536 >> /private/var/results+/redwood/bin/apfs_mmap_test -q read /usr/libexec 65536 >> /private/var/results+/redwood/bin/apfs_mmap_test -q mmap /usr/libexec >> /private/var/results+/redwood/bin/apfs_mmap_test -q mmap /usr/libexec >> /private/var/results+/bin/ps -A >> /private/var/results+/redwood/bin/vol_ls cat /private/var/results 262144diff --git a/userland/src/apfs_mmap_test.c b/userland/src/apfs_mmap_test.cindex ff30dea..31b0e6f 100644--- a/userland/src/apfs_mmap_test.c+++ b/userland/src/apfs_mmap_test.c@@ -14,8 +14,9 @@* The hash is FNV-1a over the whole file, which is what verify.py and the in-kernel walk* use, so a line here can be compared with a line from either.*- * usage: apfs_mmap_test mmap [PATH]- * apfs_mmap_test read [PATH] [CHUNK]+ * usage: apfs_mmap_test [-q] mmap [PATH]+ * apfs_mmap_test [-q] read [PATH] [CHUNK]+ * -q: no line for each file, only the files' count, bytes, line-hash sum and time.* PATH defaults to /private/var/apfs-check/usr, CHUNK to 4096. PATH may be anywhere on a* mounted volume: the lines name files from that volume's mount point (statfs), which is* what makes them the host's lines.@@ -45,6 +46,7 @@ static size_t mount_len = sizeof(MOUNT) - 1; /* the mount point's length in astatic const char *tag; /* what to print each line under */static int chunk_size = 4096;static int use_mmap;+static int quiet; /* -q: no line a file, only what they add up to */static unsigned long long total_sum;static unsigned long long total_bytes;@@ -80,7 +82,7 @@ stats_reset(void)static voidstats_print(void){- char buf[1024];+ static char buf[16384];size_t n = sizeof(buf);char *line, *next;@@ -180,7 +182,9 @@ do_file(const char *path, off_t size)}snprintf(line, sizeof(line), "%s\t%lld\t%016llx\n", path + mount_len,(long long)size, h);- printf("%s %s", tag, line);+ if (!quiet) {+ printf("%s %s", tag, line);+ }total_sum += fnv1a((const unsigned char *)line, strlen(line), FNV_INIT);total_files++;total_bytes += (unsigned long long)size;@@ -231,10 +235,17 @@ main(int argc, char **argv){static char path[PATH_MAX];const char *where = MOUNT "/usr";- const char *mode = argc > 1 ? argv[1] : "mmap";+ const char *mode;double t0, t1;int tries;+ if (argc > 1 && strcmp(argv[1], "-q") == 0) {+ quiet = 1;+ argv++;+ argc--;+ }+ mode = argc > 1 ? argv[1] : "mmap";+use_mmap = (strcmp(mode, "read") != 0);tag = use_mmap ? "apfs-mmap:" : "apfs-read:";if (argc > 2) {diff --git a/userland/src/init.c b/userland/src/init.cindex 8f0509b..d90f756 100644--- a/userland/src/init.c+++ b/userland/src/init.c@@ -750,6 +750,7 @@ run_command(char *line){char *argv[64];char *envp[] = { "PATH=/bin:/sbin:/usr/bin:/usr/sbin", "HOME=/", "TERM=dumb", NULL };+ const char *append_to = NULL;int argc = 0, status = 0, background = 0;pid_t pid;@@ -779,6 +780,25 @@ run_command(char *line)background = 1;argv[--argc] = NULL;}+ /* "... >> FILE": its output added to FILE (a list keeps results there and shows them at its end) */+ if (argc > 2 && strcmp(argv[argc - 2], ">>") == 0) {+ append_to = argv[argc - 1];+ argc -= 2;+ argv[argc] = NULL;+ }++ /* "sleep seconds": nothing for that long, without starting a program (a list's pause during a boot) */+ if (strcmp(argv[0], "sleep") == 0 && argc > 1) {+ int seconds = 0;++ for (const char *d = argv[1]; *d >= '0' && *d <= '9'; d++) {+ seconds = seconds * 10 + (*d - '0');+ }+ for (int left = seconds * 5; left > 0; left--) {+ usleep(200000);+ }+ return;+ }/* "exec program args": become that program, keeping process number 1 */if (strcmp(argv[0], "exec") == 0 && argc > 1) {@@ -792,6 +812,15 @@ run_command(char *line)pid = fork();if (pid == 0) {+ if (append_to != NULL) {+ int fd = open(append_to, O_WRONLY | O_CREAT | O_APPEND, 0644);++ if (fd >= 0) {+ dup2(fd, 1);+ dup2(fd, 2);+ close(fd);+ }+ }execve(argv[0], argv, envp);printf("%s: %s\n", argv[0], strerror(errno));_exit(127);diff --git a/userland/src/vol_ls.c b/userland/src/vol_ls.cindex 9773a4b..ced3f5a 100644--- a/userland/src/vol_ls.c+++ b/userland/src/vol_ls.c@@ -12,6 +12,8 @@* build machine for the same file* vol_ls sysctl NAME... a string the kernel keeps (kern.apfs_stats: where the APFS* driver's time went -- reads, page-ins, the lock)+ * vol_ls zero NAME... write to it (kern.apfs_stats: its counters zeroed)+ * vol_ls cpuload SECONDS how busy each processor is over the next SECONDS*/#include <dirent.h>#include <errno.h>@@ -24,6 +26,8 @@#include <sys/stat.h>#include <sys/sysctl.h>#include <unistd.h>+#include <mach/mach.h>+#include <mach/processor_info.h>#define FNV_INIT 0xcbf29ce484222325ULL#define FNV_PRIME 0x100000001b3ULL@@ -217,6 +221,70 @@ main(int argc, char **argv)if (strcmp(argv[1], "cat") == 0) {return do_cat(argv[2], argc > 3 ? atol(argv[3]) : 4096);}+ if (strcmp(argv[1], "cpuload") == 0) {+ /*+ * How busy every processor was over the next SECONDS: user, system, idle, and its+ * interrupts (IPIs among them). On the phone the slower cores' (4-6) idle count runs+ * ~56 times too fast -- the "all" line comes out at 1% user while each fast core shows+ * ~43% -- so their "99% idle" says nothing; their IPIs and the threads that run there+ * (kern.apfs_stats: where long waits ended) show they work.+ */+ processor_info_array_t a = NULL, b = NULL, sa = NULL, sb = NULL;+ mach_msg_type_number_t na = 0, nb = 0, nsa = 0, nsb = 0;+ natural_t ncpu = 0, n2 = 0, n3 = 0, n4 = 0;+ unsigned seconds = (unsigned)atoi(argv[2]);+ unsigned long long tu = 0, ts = 0, ti = 0;++ if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &ncpu, &a, &na) != KERN_SUCCESS) {+ printf("cpuload: host_processor_info failed\n");+ return 1;+ }+ (void)host_processor_info(mach_host_self(), PROCESSOR_CPU_STAT64, &n3, &sa, &nsa);+ sleep(seconds);+ (void)host_processor_info(mach_host_self(), PROCESSOR_CPU_STAT64, &n4, &sb, &nsb);+ if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &n2, &b, &nb) != KERN_SUCCESS || n2 != ncpu) {+ printf("cpuload: host_processor_info failed\n");+ return 1;+ }+ for (natural_t c = 0; c < ncpu; c++) {+ const unsigned *x = (const unsigned *)a + c * CPU_STATE_MAX, *y = (const unsigned *)b + c * CPU_STATE_MAX;+ unsigned u = y[CPU_STATE_USER] - x[CPU_STATE_USER] + y[CPU_STATE_NICE] - x[CPU_STATE_NICE];+ unsigned sy = y[CPU_STATE_SYSTEM] - x[CPU_STATE_SYSTEM], id = y[CPU_STATE_IDLE] - x[CPU_STATE_IDLE];+ unsigned all = u + sy + id;++ printf("cpuload: cpu %u over %u s: user %u%%, system %u%%, idle %u%%", c, seconds,+ all ? u * 100 / all : 0, all ? sy * 100 / all : 0, all ? id * 100 / all : 0);+ if (sa != NULL && sb != NULL && n3 == ncpu && n4 == ncpu) {+ /* (the 64-bit form: packed, 4-byte aligned, so read field by field) */+ const struct processor_cpu_stat64 *p = (const struct processor_cpu_stat64 *)sa + c;+ const struct processor_cpu_stat64 *q = (const struct processor_cpu_stat64 *)sb + c;++ printf("; interrupts %llu, of them IPIs %llu, timer %llu",+ (unsigned long long)(q->irq_ex_cnt - p->irq_ex_cnt), (unsigned long long)(q->ipi_cnt - p->ipi_cnt),+ (unsigned long long)(q->timer_cnt - p->timer_cnt));+ }+ printf("\n");+ tu += u;+ ts += sy;+ ti += id;+ }+ printf("cpuload: all %u processors over %u s: user %llu%%, system %llu%%, idle %llu%%\n", ncpu, seconds,+ (tu + ts + ti) ? tu * 100 / (tu + ts + ti) : 0, (tu + ts + ti) ? ts * 100 / (tu + ts + ti) : 0,+ (tu + ts + ti) ? ti * 100 / (tu + ts + ti) : 0);+ return 0;+ }+ if (strcmp(argv[1], "zero") == 0) {+ /* (kern.apfs_stats: writing anything zeroes the counters) */+ for (i = 2; i < argc; i++) {+ char zero = 0;++ if (sysctlbyname(argv[i], NULL, NULL, &zero, 1) != 0) {+ printf("%s: %s\n", argv[i], strerror(errno));+ bad++;+ }+ }+ return bad ? 1 : 0;+ }if (strcmp(argv[1], "sysctl") == 0) {for (i = 2; i < argc; i++) {static char text[16384];