Skip to content

The agent that grows with you

perf(update-test): clone each top-level entry with one clonefileat(2) on macOS

ethernet341c02a6eeed
diff --git a/scripts/update-test/hermes-update-rehearsal.sh b/scripts/update-test/hermes-update-rehearsal.sh
index b5cbe68e66e..edd57ef5bc0 100755
--- a/scripts/update-test/hermes-update-rehearsal.sh
+++ b/scripts/update-test/hermes-update-rehearsal.sh
@@ -180,12 +180,33 @@ detect_clone_mode() {
clone_entry() {
case "$CLONE_MODE" in
- clonefile) cp -cpR "$1" "$2/" ;;
+ clonefile) clonefile_entry "$1" "$2" || {
+ warn "fast clone of $(basename "$1") failed; cloning it file by file"
+ rm -rf "$2/$(basename "$1")"; cp -cpR "$1" "$2/"; } ;;
reflink|copy-gnu) cp -a --reflink=auto "$1" "$2/" ;;
*) cp -pR "$1" "$2/" ;;
esac
}
+# APFS can clone a whole directory tree in ONE clonefileat(2) call -- ~9x faster
+# than cp -c, which clones file by file (2.3s vs 19.3s for 130k entries). No
+# compiler on a stock Mac, but /usr/bin/perl can make the raw syscall. The
+# kernel stamps cloned DIRECTORIES with the current time, so a second pass puts
+# every directory's atime/mtime back; file times come through the clone as-is.
+clonefile_entry() {
+ /usr/bin/perl -MFile::Find -e '
+ my ($src, $dst) = @ARGV;
+ # SYS_clonefileat = 462 (xnu syscalls.master), AT_FDCWD = -2,
+ # CLONE_NOFOLLOW = 1: a top-level symlink is cloned as a link.
+ syscall(462, -2, $src, -2, $dst, 1) == 0 or exit 1;
+ find({ no_chdir => 1, wanted => sub {
+ lstat($_); return unless -d _;
+ my @st = lstat(_);
+ utime($st[8], $st[9], $dst . substr($File::Find::name, length $src)) or exit 1;
+ } }, $src);
+ ' "$1" "$2/$(basename "$1")" 2>/dev/null
+}
+
# Clone every top-level entry of $1 into $2, except one named $3 (may be empty).
clone_tree() {
local src="$1" dst="$2" skip="$3" e
diff --git a/scripts/update-test/smoke-test.sh b/scripts/update-test/smoke-test.sh
index 06d951fc45d..3d7f21ed8c0 100755
--- a/scripts/update-test/smoke-test.sh
+++ b/scripts/update-test/smoke-test.sh
@@ -38,6 +38,8 @@ printf '{"lockfileVersion":3}\n' > "$H/photon/sidecar/package-lock.json"
printf '{"lockfileVersion":3}\n' > "$H/photon/sidecar/node_modules/.package-lock.json"
mkdir -p "$H/cache/scratch"
printf 'throwaway\n' > "$H/cache/scratch/keep.txt"
+# Old directory times, so a clone that stamps directories with "now" is caught.
+touch -t 202001010000 "$H/memories" "$H/skills/foo" "$H/skills"
python - "$H/state.db" <<'PY'
import sqlite3, sys
@@ -105,6 +107,10 @@ done
[ -f "$SNAP/userdata/Cache/data.bin" ]; check $? "whole userData: nothing filtered out of the clone"
grep -q 'clone_mode' "$SNAP/manifest.json"; check $? "manifest records the clone mode"
echo " clone mode: $(sed -n 's/.*"clone_mode": "\(.*\)",/\1/p' "$SNAP/manifest.json")"
+same_mtime() { ! [ "$1" -nt "$2" ] && ! [ "$2" -nt "$1" ]; }
+same_mtime "$SNAP/home/memories" "$H/memories" && same_mtime "$SNAP/home/skills/foo" "$H/skills/foo"
+check $? "clone keeps directory mtimes"
+! grep -q 'fast clone .* failed' "$ROOT/pre.log"; check $? "no clone fell back to the slow path"
echo
echo "--- pre points the install at the rehearsal copy ---"