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
APFS: who waits for the volume's lock and on what, and five takes fewer
vcvkk3637ddb790fd
diff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.hindex 28dbb10..435b9d5 100644--- a/xnu/files/bsd/miscfs/apfs/apfs.h+++ b/xnu/files/bsd/miscfs/apfs/apfs.h@@ -95,7 +95,51 @@ struct apfs_mount {struct {thread_t th;uint64_t epoch;+ const char *site; /* the function that began it */} am_ops[APFS_OPS_MAX];+ /*+ * The lock by who takes it (the function that called apfs_lock; apfs_lib's taking it back in+ * the middle of an operation counts for the operation's): takes, waiting (in all, the longest,+ * how many over 1 ms), holding (the same). Under am_lock.+ */+#define APFS_LOCK_SITES 32+ struct apfs_lock_site {+ const char *name;+ uint64_t takes, relocks;+ uint64_t wait, wait_max, wait_long;+ uint64_t hold, hold_max, hold_long;+ } am_sites[APFS_LOCK_SITES];+ const char *am_lock_site; /* the holder's */+ uint32_t am_lock_waiting; /* threads waiting for it now (atomic) */+ uint64_t am_waiters_hist[5]; /* others waiting when it was taken: 0, 1, 2-3, 4-7, 8 or more */+ uint64_t am_lookup_negative; /* names not there, now known to the name cache */+ uint64_t am_lookup_negative_hits; /* lookups the name cache answered with "not there" */+ uint64_t am_inactive_free; /* vnop_inactive with nothing to give back: no lock taken */+ uint64_t am_lookup_positive_hits; /* lookups the name cache answered with the vnode */+ uint64_t am_node_found, am_node_made; /* apfs_node_vnode: a vnode there already, a new one */+ uint64_t am_node_named; /* of those there already, entered in the name cache by a lookup */+ /* waiting by the waiter's base priority: up to 4, 20, 31, 46, higher (all waits; those over 1 ms) */+ uint64_t am_prio_wait[5], am_prio_long[5], am_prio_long_wait[5];+ /*+ * The waits over 1 ms, by the same priorities, taken apart: blocked (the lock not given),+ * queued (runnable, no processor to run on), on a processor (spinning, the lock's own work).+ */+ uint64_t am_long_blocked[5], am_long_queued[5], am_long_oncpu[5];+ uint64_t am_long_cpu[8]; /* the processor each one ended on */+ /*+ * Who waits (waits over 10 us) and who reads (read(2), pagein), by process name; the+ * first APFS_LOCK_PROCS-1 names, the last entry takes the rest. Changed under am_lock,+ * entries only ever added; read without it. Allocated with the mount (the mount itself+ * has to stay within kalloc_type's 16 KB); NULL if that failed: then not kept.+ */+#define APFS_LOCK_PROCS 64+ struct apfs_lock_proc {+ char name[MAXCOMLEN + 1];+ uint64_t waits, wait, wait_max, wait_long;+ uint64_t read_calls, read_bytes, pagein_calls, pagein_bytes;+ uint64_t read_ticks, pagein_ticks; /* time in them, added without the lock (atomic) */+ } *am_procs;+ uint32_t am_nprocs;uint32_t am_nops;uint32_t am_untracked; /* operations the table had no room for: nothing is freed meanwhile *//* how long it was held: up to 10 us, 100 us, 1 ms, 10 ms, more; and the longest */@@ -107,6 +151,7 @@ struct apfs_mount {*/void *am_iobuf[APFS_IOBUF_KEEP];uint32_t am_iobuf_kept;+ lck_spin_t am_iobuf_spin; /* the two above: their own lock, not am_lock */uint32_t am_iobuf_size;/** Synthetic directories: empty directories that are not on the volume, for mount@@ -144,6 +189,17 @@ struct apfs_node {* decmpfs header on every call.*/struct apfs_inode an_ino;+ /*+ * A symbolic link's target, read once: every path through the link asks for it again+ * (/var, /tmp, /etc -- thousands of times a second after a boot), and it cannot change.+ * Put in once (atomically), read without the lock, freed with the vnode.+ */+ struct apfs_link *an_link;+};++struct apfs_link {+ size_t len;+ char text[];};#define APFS_MP(mp) ((struct apfs_mount *)vfs_fsprivate(mp))@@ -163,7 +219,8 @@ void apfs_node_detach(struct apfs_node *an);/* the breakdown of where the time went, straight to the log (apfs_vfsops.c) */void apfs_stats_print(void);-void apfs_lock(struct apfs_mount *am); /* an operation begins */+void apfs_lock_at(struct apfs_mount *am, const char *site); /* an operation begins */+#define apfs_lock(am) apfs_lock_at((am), __func__)void apfs_unlock(struct apfs_mount *am); /* and ends */void apfs_lock_inner(struct apfs_mount *am); /* the lock taken back in the middle of one (apfs_lib) */void apfs_unlock_inner(struct apfs_mount *am); /* let go of in the middle of one */diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex 8ce465c..6a01808 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -147,6 +147,83 @@ apfs_ticks_ms(uint64_t ticks)return ns / 1000000;}+/* The volume's lock by who takes it, and how many others waited, appended to buf */+static void+apfs_lock_report(struct apfs_mount *am, char *buf, size_t len)+{+ size_t at = strlen(buf);+ uint32_t i;++ at += scnprintf(buf + at, len - at, "lookups %llu of names not there, now in the name cache as such; %llu "+ "answered by it so\n", am->am_lookup_negative, am->am_lookup_negative_hits);+ at += scnprintf(buf + at, len - at, "inactive %llu with nothing to give back, without the lock\n",+ am->am_inactive_free);+ at += scnprintf(buf + at, len - at, "vnodes %llu found there already (%llu of them by a lookup, its name now in the "+ "name cache), %llu made; %llu lookups the name cache answered with the vnode\n", am->am_node_found,+ am->am_node_named, am->am_node_made, am->am_lookup_positive_hits);+ at += scnprintf(buf + at, len - at, "lock waited by priority (all ms; over 1 ms: how many, ms): background (<=4) %llu; "+ "%llu, %llu; utility (<=20) %llu; %llu, %llu; default (<=31) %llu; %llu, %llu; user-initiated (<=46) %llu; %llu, "+ "%llu; interactive %llu; %llu, %llu\n",+ apfs_ticks_ms(am->am_prio_wait[0]), am->am_prio_long[0], apfs_ticks_ms(am->am_prio_long_wait[0]),+ apfs_ticks_ms(am->am_prio_wait[1]), am->am_prio_long[1], apfs_ticks_ms(am->am_prio_long_wait[1]),+ apfs_ticks_ms(am->am_prio_wait[2]), am->am_prio_long[2], apfs_ticks_ms(am->am_prio_long_wait[2]),+ apfs_ticks_ms(am->am_prio_wait[3]), am->am_prio_long[3], apfs_ticks_ms(am->am_prio_long_wait[3]),+ apfs_ticks_ms(am->am_prio_wait[4]), am->am_prio_long[4], apfs_ticks_ms(am->am_prio_long_wait[4]));+ {+ static const char *const band[5] = { "background", "utility", "default", "user-initiated", "interactive" };++ at += scnprintf(buf + at, len - at, "lock waits over 1 ms, where the time went (ms: blocked, the lock not "+ "given / queued, runnable without a processor / on a processor):");+ for (i = 0; i < 5; i++) {+ at += scnprintf(buf + at, len - at, " %s %llu/%llu/%llu%s", band[i],+ (unsigned long long)apfs_ticks_ms(am->am_long_blocked[i]),+ (unsigned long long)apfs_ticks_ms(am->am_long_queued[i]),+ (unsigned long long)apfs_ticks_ms(am->am_long_oncpu[i]), i < 4 ? ";" : "\n");+ }+ at += scnprintf(buf + at, len - at, "lock waits over 1 ms, the processor each ended on:");+ for (i = 0; i < 8; i++) {+ at += scnprintf(buf + at, len - at, " cpu %u %llu%s", i, am->am_long_cpu[i], i < 7 ? "," : "\n");+ }+ }+ at += scnprintf(buf + at, len - at, "lock takes with others waiting: none %llu, 1 %llu, 2-3 %llu, 4-7 %llu, "+ "8 or more %llu\n", am->am_waiters_hist[0], am->am_waiters_hist[1], am->am_waiters_hist[2],+ am->am_waiters_hist[3], am->am_waiters_hist[4]);+ {+ uint64_t max_ns;++ absolutetime_to_nanoseconds(am->am_lock_hold_max, &max_ns);+ at += scnprintf(buf + at, len - at, "lock holds: %llu under 10 us, %llu to 100 us, %llu to 1 ms, %llu to "+ "10 ms, %llu longer; longest %llu us\n", am->am_lock_hold_hist[0], am->am_lock_hold_hist[1],+ am->am_lock_hold_hist[2], am->am_lock_hold_hist[3], am->am_lock_hold_hist[4], max_ns / 1000);+ }+ for (i = 0; i < APFS_LOCK_SITES && am->am_sites[i].name != NULL && at < len; i++) {+ const struct apfs_lock_site *ls = &am->am_sites[i];+ uint64_t wmax, hmax;++ absolutetime_to_nanoseconds(ls->wait_max, &wmax);+ absolutetime_to_nanoseconds(ls->hold_max, &hmax);+ at += scnprintf(buf + at, len - at, "lock by %-22s %7llu takes %7llu again; waited %6llu ms (longest %6llu us, "+ "%5llu over 1 ms); held %6llu ms (longest %6llu us, %5llu over 1 ms)\n", ls->name, ls->takes, ls->relocks,+ (unsigned long long)apfs_ticks_ms(ls->wait), wmax / 1000, ls->wait_long,+ (unsigned long long)apfs_ticks_ms(ls->hold), hmax / 1000, ls->hold_long);+ }+ for (i = 0; am->am_procs != NULL && i < APFS_LOCK_PROCS && i < am->am_nprocs && at < len; i++) {+ const struct apfs_lock_proc *lp = &am->am_procs[i];+ uint64_t wmax;++ /* (only who waited 1 ms or more in all, or read 1 MB or more) */+ if (apfs_ticks_ms(lp->wait) == 0 && lp->read_bytes + lp->pagein_bytes < (1 << 20)) {+ continue;+ }+ absolutetime_to_nanoseconds(lp->wait_max, &wmax);+ at += scnprintf(buf + at, len - at, "who %-16.16s waited %6llu times over 10 us, %6llu ms (longest %7llu us, "+ "%4llu over 1 ms); read(2) %6llu calls %10llu bytes %6llu ms; pagein %6llu calls %10llu bytes %6llu ms\n",+ lp->name, lp->waits, (unsigned long long)apfs_ticks_ms(lp->wait), wmax / 1000, lp->wait_long,+ lp->read_calls, lp->read_bytes, (unsigned long long)apfs_ticks_ms(lp->read_ticks), lp->pagein_calls,+ lp->pagein_bytes, (unsigned long long)apfs_ticks_ms(lp->pagein_ticks));+ }+}+/** kern.apfs_stats: reading it says where the time went, writing anything zeroes the* counters. The point is to tell apart, on one road through the driver, the time spent@@ -158,15 +235,19 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS{#pragma unused(oidp, arg1, arg2)struct apfs_mount *am = apfs_the_mount;- char text[1024];+ size_t cap = 16384;+ char *text = kalloc_data(cap, Z_WAITOK | Z_ZERO);int error;+ if (text == NULL) {+ return ENOMEM;+ }if (am == NULL) {- strlcpy(text, "apfs: nothing mounted\n", sizeof(text));+ strlcpy(text, "apfs: nothing mounted\n", cap);} else {struct apfs_nx *nx = &am->am_nx;- snprintf(text, sizeof(text),+ snprintf(text, cap,"pagein %llu calls %llu ms %llu bytes\n""read %llu calls %llu ms %llu bytes\n""device %llu calls %llu ms %llu bytes\n"@@ -200,8 +281,10 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGS(unsigned long long)apfs_ticks_ms(am->am_lock_wait_ticks),(unsigned long long)apfs_ticks_ms(am->am_lock_held_ticks),(unsigned long long)nx->unlocked_fills);+ apfs_lock_report(am, text, cap);}error = SYSCTL_OUT(req, text, strlen(text) + 1);+ kfree_data(text, cap);if (error || req->newptr == USER_ADDR_NULL) {return error;}@@ -219,6 +302,28 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGSam->am_lock_wait_max = 0;memset(am->am_lock_hold_hist, 0, sizeof(am->am_lock_hold_hist));am->am_lock_hold_max = 0;+ memset(am->am_waiters_hist, 0, sizeof(am->am_waiters_hist));+ am->am_lookup_negative = 0;+ am->am_lookup_negative_hits = 0;+ am->am_inactive_free = 0;+ am->am_lookup_positive_hits = am->am_node_found = am->am_node_made = am->am_node_named = 0;+ memset(am->am_prio_wait, 0, sizeof(am->am_prio_wait));+ memset(am->am_prio_long, 0, sizeof(am->am_prio_long));+ memset(am->am_prio_long_wait, 0, sizeof(am->am_prio_long_wait));+ memset(am->am_long_blocked, 0, sizeof(am->am_long_blocked));+ memset(am->am_long_queued, 0, sizeof(am->am_long_queued));+ memset(am->am_long_oncpu, 0, sizeof(am->am_long_oncpu));+ memset(am->am_long_cpu, 0, sizeof(am->am_long_cpu));+ if (am->am_procs != NULL) {+ memset(am->am_procs, 0, APFS_LOCK_PROCS * sizeof(am->am_procs[0]));+ }+ am->am_nprocs = 0;+ for (uint32_t i = 0; i < APFS_LOCK_SITES; i++) {+ const char *name = am->am_sites[i].name;++ memset(&am->am_sites[i], 0, sizeof(am->am_sites[i]));+ am->am_sites[i].name = name;+ }apfs_unlock(am);}return 0;@@ -290,6 +395,15 @@ apfs_stats_print(void)am->am_lock_hold_hist[3], am->am_lock_hold_hist[4], max_ns / 1000);printf("apfs: lookups %llu by the name's hash, %llu by reading the directory\n", nx->lookups_hashed,nx->lookups_scanned);+ {+ char *rep = kalloc_data(16384, Z_WAITOK | Z_ZERO);++ if (rep != NULL) {+ apfs_lock_report(am, rep, 16384);+ printf("%s", rep);+ kfree_data(rep, 16384);+ }+ }printf("apfs: metadata read without the lock: %llu times, %llu of them already in by another's read; ""retired blocks %llu now, %llu at most; operations going on now %u\n", nx->unlocked_fills, nx->fill_races,nx->retired_now, nx->retired_most, am->am_nops);@@ -330,6 +444,19 @@ apfs_node_vnode(struct apfs_mount *am, uint64_t obj_id, vnode_t dvp,apfs_unlock(am);if (vnode_getwithvid(vp, vid) == 0) {+ os_atomic_inc(&am->am_node_found, relaxed);+ /*+ * Found by a lookup: the name goes into the name cache, as vnode_create+ * puts it there for a new vnode -- else every later lookup of this+ * name comes back here for the lock. (The volume does not change;+ * a synthetic name is looked for only where there is no real one, and+ * adding one purges its directory's entries.) The name cache keeps+ * one name per vnode and leaves one that has a name already as it is.+ */+ if (dvp != NULLVP && cnp != NULL && (cnp->cn_flags & MAKEENTRY)) {+ cache_enter(dvp, vp, cnp);+ os_atomic_inc(&am->am_node_named, relaxed);+ }*vpp = vp;return 0;}@@ -431,6 +558,7 @@ apfs_node_vnode(struct apfs_mount *am, uint64_t obj_id, vnode_t dvp,am->am_hash[b] = an;an->an_attached = true;apfs_unlock(am);+ os_atomic_inc(&am->am_node_made, relaxed);*vpp = an->an_vp;return 0;@@ -569,6 +697,7 @@ apfs_sysctl_synthetic SYSCTL_HANDLER_ARGSapfs_unlock(am);/* the parent's entries changed: what the name cache has of it is stale */cache_purge(dvp);+ cache_purge_negatives(dvp); /* (the new name was known not to be there) */vnode_put(dvp);out:if (tvp != NULLVP) {@@ -780,6 +909,10 @@ apfs_free_mount(struct apfs_mount *am)kfree_data(am->am_iobuf[--am->am_iobuf_kept], am->am_iobuf_size);}apfs_nx_close(&am->am_nx);+ if (am->am_procs != NULL) {+ kfree_data(am->am_procs, APFS_LOCK_PROCS * sizeof(am->am_procs[0]));+ }+ lck_spin_destroy(&am->am_iobuf_spin, &apfs_lck_grp);lck_mtx_destroy(&am->am_lock, &apfs_lck_grp);kfree_type(struct apfs_mount, am);}@@ -806,6 +939,8 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)return ENOMEM;}lck_mtx_init(&am->am_lock, &apfs_lck_grp, LCK_ATTR_NULL);+ lck_spin_init(&am->am_iobuf_spin, &apfs_lck_grp, LCK_ATTR_NULL);+ am->am_procs = kalloc_data(APFS_LOCK_PROCS * sizeof(am->am_procs[0]), Z_WAITOK | Z_ZERO);am->am_mp = mp;am->am_devvp = devvp;am->am_devblock = devblock;diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c b/xnu/files/bsd/miscfs/apfs/apfs_vnops.cindex b4bf69f..e9f38d9 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vnops.c@@ -21,15 +21,69 @@#include <sys/unistd.h>#include <sys/vnode_internal.h>#include <sys/xattr.h>+#include <os/atomic_private.h>#include <vfs/vfs_support.h>/* How much is read out of a file at a time when copying into a uio. */-void-apfs_lock_inner(struct apfs_mount *am)+extern integer_t thread_kern_get_pri(thread_t thr); /* the thread's base priority */+extern void thread_runnable_and_run_self(uint64_t *runnable, uint64_t *run);++/* The statistics of the calling process (with the lock held) */+static struct apfs_lock_proc *+lock_proc(struct apfs_mount *am)+{+ static struct apfs_lock_proc nowhere; /* (no table: counted, under the lock, and never shown) */+ char name[MAXCOMLEN + 1];+ uint32_t i;++ if (am->am_procs == NULL) {+ return &nowhere;+ }+ proc_selfname(name, sizeof(name));+ for (i = 0; i < am->am_nprocs; i++) {+ if (strncmp(am->am_procs[i].name, name, sizeof(name)) == 0) {+ return &am->am_procs[i];+ }+ }+ if (am->am_nprocs < APFS_LOCK_PROCS - 1) {+ strlcpy(am->am_procs[am->am_nprocs].name, name, sizeof(name));+ return &am->am_procs[am->am_nprocs++];+ }+ if (am->am_nprocs == APFS_LOCK_PROCS - 1) {+ strlcpy(am->am_procs[am->am_nprocs].name, "(the rest)", sizeof(name));+ am->am_nprocs++;+ }+ return &am->am_procs[APFS_LOCK_PROCS - 1];+}++/* The statistics of a caller of apfs_lock (with the lock held) */+static struct apfs_lock_site *+lock_site(struct apfs_mount *am, const char *name)+{+ uint32_t i;++ for (i = 0; i < APFS_LOCK_SITES && am->am_sites[i].name != NULL; i++) {+ if (am->am_sites[i].name == name) {+ return &am->am_sites[i];+ }+ }+ if (i == APFS_LOCK_SITES) {+ i = APFS_LOCK_SITES - 1; /* (more callers than kept: the last one takes the rest) */+ } else {+ am->am_sites[i].name = name;+ }+ return &am->am_sites[i];+}++/* The lock taken, by site: how long it was waited for, how many others were waiting */+static void+lock_take(struct apfs_mount *am, const char *site, bool relock){static uint64_t limits[4];- uint64_t t0 = apfs_ticks(), waited;+ uint64_t t0 = apfs_ticks(), waited, long_wait, runnable0, run0;+ uint32_t others = os_atomic_inc_orig(&am->am_lock_waiting, relaxed);+ struct apfs_lock_site *ls;unsigned k;if (limits[3] == 0) {@@ -38,8 +92,11 @@ apfs_lock_inner(struct apfs_mount *am)nanoseconds_to_absolutetime(100000, &limits[2]);nanoseconds_to_absolutetime(1000000, &limits[3]);}+ thread_runnable_and_run_self(&runnable0, &run0);lck_mtx_lock(&am->am_lock);+ os_atomic_dec(&am->am_lock_waiting, relaxed);waited = apfs_ticks() - t0;+ long_wait = limits[3];/* (the holder's own: no atomics needed) */am->am_lock_wait_ticks += waited;am->am_lock_waits++;@@ -49,14 +106,77 @@ apfs_lock_inner(struct apfs_mount *am)if (waited > am->am_lock_wait_max) {am->am_lock_wait_max = waited;}+ am->am_waiters_hist[others == 0 ? 0 : others == 1 ? 1 : others <= 3 ? 2 : others <= 7 ? 3 : 4]++;+ {+ int pri = thread_kern_get_pri(current_thread());+ unsigned b = pri <= 4 ? 0 : pri <= 20 ? 1 : pri <= 31 ? 2 : pri <= 46 ? 3 : 4;++ am->am_prio_wait[b] += waited;+ if (waited >= limits[3]) {+ uint64_t runnable, run;++ am->am_prio_long[b]++;+ am->am_prio_long_wait[b] += waited;+ /* where the time went: runnable includes running, and the rest was blocked */+ thread_runnable_and_run_self(&runnable, &run);+ runnable -= runnable0;+ run -= run0;+ am->am_long_blocked[b] += waited > runnable ? waited - runnable : 0;+ am->am_long_queued[b] += runnable > run ? runnable - run : 0;+ am->am_long_oncpu[b] += run;+ am->am_long_cpu[cpu_number() & 7]++;+ }+ }+ if (waited >= limits[1]) {+ struct apfs_lock_proc *lp = lock_proc(am);++ lp->waits++;+ lp->wait += waited;+ if (waited > lp->wait_max) {+ lp->wait_max = waited;+ }+ lp->wait_long += waited >= limits[3];+ }+ if (relock) {+ /* apfs_lib taking it back in the middle of an operation: the operation's */+ thread_t self = current_thread();+ uint32_t i;++ site = "apfs_lib";+ for (i = 0; i < am->am_nops; i++) {+ if (am->am_ops[i].th == self) {+ site = am->am_ops[i].site;+ break;+ }+ }+ }+ ls = lock_site(am, site);+ if (relock) {+ ls->relocks++;+ } else {+ ls->takes++;+ }+ ls->wait += waited;+ if (waited > ls->wait_max) {+ ls->wait_max = waited;+ }+ ls->wait_long += waited >= long_wait;+ am->am_lock_site = site;am->am_lock_t0 = apfs_ticks();}+void+apfs_lock_inner(struct apfs_mount *am)+{+ lock_take(am, NULL, true);+}+voidapfs_unlock_inner(struct apfs_mount *am){static uint64_t limits[4];uint64_t held = apfs_ticks() - am->am_lock_t0;+ struct apfs_lock_site *ls;unsigned k;if (limits[3] == 0) {@@ -72,17 +192,26 @@ apfs_unlock_inner(struct apfs_mount *am)if (held > am->am_lock_hold_max) {am->am_lock_hold_max = held;}+ if (am->am_lock_site != NULL) {+ ls = lock_site(am, am->am_lock_site);+ ls->hold += held;+ if (held > ls->hold_max) {+ ls->hold_max = held;+ }+ ls->hold_long += held >= limits[2];+ }lck_mtx_unlock(&am->am_lock);}/* An operation begins: the lock, and the epoch it began at noted */void-apfs_lock(struct apfs_mount *am)+apfs_lock_at(struct apfs_mount *am, const char *site){- apfs_lock_inner(am);+ lock_take(am, site, false);if (am->am_nops < APFS_OPS_MAX) {am->am_ops[am->am_nops].th = current_thread();am->am_ops[am->am_nops].epoch = am->am_nx.epoch;+ am->am_ops[am->am_nops].site = site;am->am_nops++;} else {am->am_untracked++;@@ -156,6 +285,21 @@ apfs_vnop_lookup(struct vnop_lookup_args *ap)return apfs_node_vnode(am, dn->an_parent, NULLVP, NULL, ap->a_vpp);}+ /*+ * The name cache first, as a file system's lookup is to ask it (namei's own quick path+ * leaves the entries for names that are not there to us): -1, the vnode, with an iocount;+ * ENOENT, known not to be there (for a CREATE or RENAME it says 0 and forgets the entry).+ */+ error = cache_lookup(dvp, ap->a_vpp, cnp);+ if (error == -1) {+ os_atomic_inc(&am->am_lookup_positive_hits, relaxed);+ return 0;+ }+ if (error == ENOENT) {+ os_atomic_inc(&am->am_lookup_negative_hits, relaxed);+ return ENOENT;+ }+apfs_lock(am);if (dn->an_id >= APFS_SYNTH_BASE) {error = ENOENT; /* nothing of a synthetic directory is on the volume */@@ -183,6 +327,15 @@ apfs_vnop_lookup(struct vnop_lookup_args *ap)(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {return EROFS;}+ /*+ * A name that is not there stays not there (the volume cannot change; a synthetic+ * directory added later purges its parent's entries): the name cache says so from now on,+ * instead of asking again -- iOS asks for missing files thousands of times after a boot.+ */+ if (error == ENOENT && cnp->cn_nameiop == LOOKUP && (cnp->cn_flags & MAKEENTRY)) {+ cache_enter(dvp, NULLVP, cnp);+ os_atomic_inc(&am->am_lookup_negative, relaxed);+ }if (error) {return error;}@@ -251,6 +404,40 @@ apfs_vnop_getattr(struct vnop_getattr_args *ap)/* ------------------------------------------------------------------ reading */+/*+ * A buffer for one round of vnop_read: one of those kept, or a new one. The kept ones have+ * a lock of their own, so that a read takes am_lock only for the reading itself.+ */+static uint8_t *+apfs_iobuf_get(struct apfs_mount *am)+{+ uint8_t *buf = NULL;++ lck_spin_lock(&am->am_iobuf_spin);+ if (am->am_iobuf_kept > 0) {+ buf = am->am_iobuf[--am->am_iobuf_kept];+ }+ lck_spin_unlock(&am->am_iobuf_spin);+ if (buf == NULL) {+ buf = kalloc_data(am->am_iobuf_size, Z_WAITOK);+ }+ return buf;+}++static void+apfs_iobuf_put(struct apfs_mount *am, uint8_t *buf)+{+ lck_spin_lock(&am->am_iobuf_spin);+ if (am->am_iobuf_kept < APFS_IOBUF_KEEP) {+ am->am_iobuf[am->am_iobuf_kept++] = buf;+ buf = NULL;+ }+ lck_spin_unlock(&am->am_iobuf_spin);+ if (buf != NULL) {+ kfree_data(buf, am->am_iobuf_size);+ }+}+static intapfs_vnop_read(struct vnop_read_args *ap){@@ -261,6 +448,7 @@ apfs_vnop_read(struct vnop_read_args *ap)uint8_t *buf;uint64_t size = 0;uint64_t t0, read_bytes = 0;+ struct apfs_lock_proc *lp = NULL;int error = 0;if (vnode_isdir(vp)) {@@ -275,50 +463,56 @@ apfs_vnop_read(struct vnop_read_args *ap)ino = APFS_VP(vp)->an_ino;size = APFS_VP(vp)->an_size;t0 = apfs_ticks();- apfs_lock(am);- if (am->am_iobuf_kept > 0) {- buf = am->am_iobuf[--am->am_iobuf_kept];- } else {- apfs_unlock(am);- buf = kalloc_data(am->am_iobuf_size, Z_WAITOK);+ if (uio_resid(uio) > 0 && (uint64_t)uio_offset(uio) < size) {+ buf = apfs_iobuf_get(am);if (buf == NULL) {return ENOMEM;}+ /* (how many reads are going on: a hint to apfs_lib, kept without the lock) */+ os_atomic_inc(&am->am_nx.requests, relaxed);apfs_lock(am);- }- /* the lock is held at the top of every round and let go of only for uiomove */- am->am_nx.requests++;- while (uio_resid(uio) > 0 && (uint64_t)uio_offset(uio) < size) {- size_t round = (am->am_nx.requests <= 1)- ? am->am_iobuf_size : MIN(am->am_iobuf_size, APFS_ROUND_SHARED);- size_t want = (size_t)MIN((user_ssize_t)round, uio_resid(uio));- size_t got = 0;-- error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf,- (uint64_t)uio_offset(uio), buf, want, &got);- if (error || got == 0) {- break;+ {+ lp = lock_proc(am);+ lp->read_calls++;+ lp->read_bytes += (uint64_t)MIN((uint64_t)uio_resid(uio), size - (uint64_t)uio_offset(uio));}- apfs_unlock(am);- error = uiomove((caddr_t)buf, (int)got, uio);- apfs_lock(am);- if (error) {- break;+ /*+ * The lock is held for the reading of each round and let go of for its uiomove;+ * taken again only if another round follows. What comes after the last one (the+ * buffer given back, the counts) needs no am_lock.+ */+ for (;;) {+ size_t round = (os_atomic_load(&am->am_nx.requests, relaxed) <= 1)+ ? am->am_iobuf_size : MIN(am->am_iobuf_size, APFS_ROUND_SHARED);+ size_t want = (size_t)MIN((user_ssize_t)round, uio_resid(uio));+ size_t got = 0;++ error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf,+ (uint64_t)uio_offset(uio), buf, want, &got);+ apfs_unlock(am);+ if (error || got == 0) {+ break;+ }+ error = uiomove((caddr_t)buf, (int)got, uio);+ if (error) {+ break;+ }+ read_bytes += got;+ if (uio_resid(uio) <= 0 || (uint64_t)uio_offset(uio) >= size) {+ break;+ }+ apfs_lock(am);}- read_bytes += got;+ os_atomic_dec(&am->am_nx.requests, relaxed);+ apfs_iobuf_put(am, buf);}- am->am_nx.requests--;- if (am->am_iobuf_kept < APFS_IOBUF_KEEP) {- am->am_iobuf[am->am_iobuf_kept++] = buf;- buf = NULL;- }- am->am_read_ticks += apfs_ticks() - t0;- am->am_read_calls++;- am->am_read_bytes += read_bytes;- apfs_unlock(am);- if (buf != NULL) {- kfree_data(buf, am->am_iobuf_size);+ if (lp != NULL) {+ /* (the entry stays where it is; a zeroing meanwhile costs at most this one read's time) */+ os_atomic_add(&lp->read_ticks, apfs_ticks() - t0, relaxed);}+ os_atomic_add(&am->am_read_ticks, apfs_ticks() - t0, relaxed);+ os_atomic_inc(&am->am_read_calls, relaxed);+ os_atomic_add(&am->am_read_bytes, read_bytes, relaxed);return error;}@@ -339,6 +533,7 @@ apfs_vnop_pagein(struct vnop_pagein_args *ap)vm_offset_t address = 0;uint64_t size = 0;uint64_t t0 = apfs_ticks();+ struct apfs_lock_proc *lp = NULL;off_t at, end;int error = 0;@@ -387,12 +582,17 @@ apfs_vnop_pagein(struct vnop_pagein_args *ap)break;}want = (size_t)MIN(end - at, (off_t)(size - (uint64_t)at));+ os_atomic_inc(&am->am_nx.requests, relaxed);apfs_lock(am);- am->am_nx.requests++;+ if (at == offset) {+ lp = lock_proc(am);+ lp->pagein_calls++;+ lp->pagein_bytes += ap->a_size;+ }error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf, (uint64_t)at,dst, want, &got);- am->am_nx.requests--;apfs_unlock(am);+ os_atomic_dec(&am->am_nx.requests, relaxed);if (error) {break;}@@ -410,11 +610,13 @@ apfs_vnop_pagein(struct vnop_pagein_args *ap)}ubc_upl_unmap(upl);- apfs_lock(am);- am->am_pagein_ticks += apfs_ticks() - t0;- am->am_pagein_calls++;- am->am_pagein_bytes += ap->a_size;- apfs_unlock(am);+ /* (the counts: without the lock, which would be taken for nothing else) */+ if (lp != NULL) {+ os_atomic_add(&lp->pagein_ticks, apfs_ticks() - t0, relaxed);+ }+ os_atomic_add(&am->am_pagein_ticks, apfs_ticks() - t0, relaxed);+ os_atomic_inc(&am->am_pagein_calls, relaxed);+ os_atomic_add(&am->am_pagein_bytes, (uint64_t)ap->a_size, relaxed);if (!(ap->a_flags & UPL_NOCOMMIT)) {if (error) {ubc_upl_abort_range(upl, upl_offset, (upl_size_t)ap->a_size,@@ -548,6 +750,7 @@ apfs_vnop_readlink(struct vnop_readlink_args *ap)vnode_t vp = ap->a_vp;struct apfs_mount *am = APFS_VP_MOUNT(vp);struct apfs_node *an = APFS_VP(vp);+ struct apfs_link *link;char *buf;size_t n = 0;int error;@@ -555,18 +758,35 @@ apfs_vnop_readlink(struct vnop_readlink_args *ap)if (!vnode_islnk(vp)) {return EINVAL;}- buf = kalloc_data(MAXPATHLEN, Z_WAITOK);- if (buf == NULL) {- return ENOMEM;- }- apfs_lock(am);- error = apfs_readlink(&am->am_vol, an->an_id, buf, MAXPATHLEN, &n);- apfs_unlock(am);- if (error == 0) {- error = uiomove(buf, (int)n, ap->a_uio);+ link = os_atomic_load(&an->an_link, acquire);+ if (link == NULL) {+ /* the first time: from the tree, with the lock; kept for every time after */+ buf = kalloc_data(MAXPATHLEN, Z_WAITOK);+ if (buf == NULL) {+ return ENOMEM;+ }+ apfs_lock(am);+ error = apfs_readlink(&am->am_vol, an->an_id, buf, MAXPATHLEN, &n);+ apfs_unlock(am);+ if (error) {+ kfree_data(buf, MAXPATHLEN);+ return error;+ }+ link = kalloc_data(sizeof(*link) + n, Z_WAITOK);+ if (link == NULL) {+ error = uiomove(buf, (int)n, ap->a_uio);+ kfree_data(buf, MAXPATHLEN);+ return error;+ }+ link->len = n;+ memcpy(link->text, buf, n);+ kfree_data(buf, MAXPATHLEN);+ if (!os_atomic_cmpxchg(&an->an_link, NULL, link, release)) {+ kfree_data(link, sizeof(*link) + n); /* somebody else's is in */+ link = os_atomic_load(&an->an_link, acquire);+ }}- kfree_data(buf, MAXPATHLEN);- return error;+ return uiomove(link->text, (int)link->len, ap->a_uio);}/* ------------------------------------------------------------------ attributes */@@ -731,11 +951,24 @@ apfs_vnop_inactive(struct vnop_inactive_args *ap){struct apfs_node *an = APFS_VP(ap->a_vp);- if (an != NULL) {- apfs_lock(an->an_mount);- apfs_cfile_fini(&an->an_cf);- apfs_unlock(an->an_mount);+ if (an == NULL) {+ return 0;+ }+ /*+ * Most files hold nothing to give back (a directory, a link, a file not compressed): then+ * not even the lock. Looked at without it: a state being set up meanwhile is given back with+ * the vnode instead (apfs_vnop_reclaim), which is all a wrong "nothing" can cost.+ */+ if (os_atomic_load(&an->an_cf.piece_off, relaxed) == NULL &&+ os_atomic_load(&an->an_cf.piece_len, relaxed) == NULL &&+ os_atomic_load(&an->an_cf.held_data, relaxed) == NULL &&+ os_atomic_load(&an->an_cf.ra_data, relaxed) == NULL) {+ os_atomic_inc(&an->an_mount->am_inactive_free, relaxed);+ return 0;}+ apfs_lock(an->an_mount);+ apfs_cfile_fini(&an->an_cf);+ apfs_unlock(an->an_mount);return 0;}@@ -751,6 +984,9 @@ apfs_vnop_reclaim(struct vnop_reclaim_args *ap)apfs_unlock(an->an_mount);apfs_node_detach(an);vnode_clearfsnode(vp);+ if (an->an_link != NULL) {+ kfree_data(an->an_link, sizeof(*an->an_link) + an->an_link->len);+ }kfree_type(struct apfs_node, an);}return 0;diff --git a/xnu/redwood.patch b/xnu/redwood.patchindex 83412d0..d4ad229 100644--- a/xnu/redwood.patch+++ b/xnu/redwood.patch@@ -2870,6 +2870,37 @@ index bec97fbf7243..203e1f48783f 100644#endif#if CONFIG_DTRACE+diff --git a/osfmk/kern/thread.c b/osfmk/kern/thread.c+index 384ef23fe0e1..d5ab6db0edbc 100644+--- a/osfmk/kern/thread.c++++ b/osfmk/kern/thread.c+@@ -3065,6 +3065,26 @@ thread_get_tag(thread_t th)+ return thread_get_tag_internal(th);+ }+++/*++ * For apfs's lock statistics (bsd/miscfs/apfs/apfs_vnops.c): how long the current thread has++ * been runnable (running included) and how long it has run, in mach_absolute_time() units.++ * Taken before and after a wait for a lock, the two tell the wait apart: blocked, queued for++ * a processor, or spinning on one.++ */++void thread_runnable_and_run_self(uint64_t *runnable, uint64_t *run);++++void++thread_runnable_and_run_self(uint64_t *runnable, uint64_t *run)++{++ thread_t thread = current_thread();++ boolean_t istate = ml_set_interrupts_enabled(FALSE);++ uint64_t now = mach_absolute_time();++++ *runnable = thread->runnable_timer.all_bits + (now - thread->runnable_timer.tstamp);++ *run = recount_current_thread_time_mach();++ ml_set_interrupts_enabled(istate);++}+++ uint64_t+ thread_last_run_time(thread_t th)+ {diff --git a/osfmk/kern/work_interval.c b/osfmk/kern/work_interval.cindex f2b9ce24bd8d..3aed167fe32e 100644--- a/osfmk/kern/work_interval.c