| 1 | #!/usr/bin/env bash |
| 2 | # drv-churn: which flake checks an edit gives a new derivation. |
| 3 | # |
| 4 | # A check whose derivation an edit leaves unchanged is a cache hit, and its |
| 5 | # evidence carries to the edited tree under the pure transfer rule. This |
| 6 | # script measures that. It exports a revision's tree the way attest does |
| 7 | # (git archive into a plain directory, so the flake has no rev), applies one |
| 8 | # representative edit at a time to a copy of it, evaluates every check's |
| 9 | # drvPath, and prints which checks changed. Nothing is built, except what |
| 10 | # the checks' evaluation imports from derivations. |
| 11 | # |
| 12 | # nix/tools/drv-churn.sh measure HEAD's tree and definitions |
| 13 | # nix/tools/drv-churn.sh origin/main measure another revision's |
| 14 | # nix/tools/drv-churn.sh "$(git stash create)" |
| 15 | # measure uncommitted, staged work |
| 16 | # |
| 17 | # Options: |
| 18 | # --git evaluate each edit as a commit of a git flake, so self.rev |
| 19 | # changes with every edit, as it does for nix build run in |
| 20 | # a checkout. The default mode is attest's. |
| 21 | # --history RANGE instead of the fixed edits, replay the paths each |
| 22 | # non-merge commit in RANGE touched (for example |
| 23 | # origin/main~20..origin/main): every path the commit |
| 24 | # changed that exists in the measured tree is edited at |
| 25 | # once. A commit's changes to the Nix definitions |
| 26 | # themselves are replayed as comments, so this measures |
| 27 | # the measured revision's definitions, not the commit's. |
| 28 | # --expect FILE compare the fixed edits' results with FILE, a table |
| 29 | # written by --write, and fail on any difference. |
| 30 | # --write FILE write the fixed edits' results to FILE. |
| 31 | # Both use the fixed edits in the default mode. |
| 32 | # |
| 33 | # Every edit appends a comment line in the file's own syntax, adds a new |
| 34 | # file, or deletes one. An edit to a file that holds no input of any check |
| 35 | # must change no derivation; an edit to a check's input must change that |
| 36 | # check's. |
| 37 | set -euo pipefail |
| 38 | |
| 39 | mode=path |
| 40 | history= |
| 41 | expect= |
| 42 | write= |
| 43 | rev=HEAD |
| 44 | while [ $# -gt 0 ]; do |
| 45 | case "$1" in |
| 46 | --git) mode=git ;; |
| 47 | --history) history="$2"; shift ;; |
| 48 | --expect) expect="$2"; shift ;; |
| 49 | --write) write="$2"; shift ;; |
| 50 | -h | --help) sed -n '2,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//'; exit 0 ;; |
| 51 | -*) echo "drv-churn: unknown option $1" >&2; exit 2 ;; |
| 52 | *) rev="$1" ;; |
| 53 | esac |
| 54 | shift |
| 55 | done |
| 56 | if { [ -n "$history" ] || [ "$mode" = git ]; } && { [ -n "$expect" ] || [ -n "$write" ]; }; then |
| 57 | echo "drv-churn: --expect and --write record the fixed edits in the default mode" >&2 |
| 58 | exit 2 |
| 59 | fi |
| 60 | |
| 61 | repo="$(git rev-parse --show-toplevel)" |
| 62 | commit="$(git -C "$repo" rev-parse --verify "$rev^{commit}")" |
| 63 | system="$(nix eval --impure --raw --expr builtins.currentSystem)" |
| 64 | work="$(mktemp -d "${KLAUS_SCRATCH:-${TMPDIR:-/tmp}}/drv-churn.XXXXXX")" |
| 65 | trap 'chmod -R u+w "$work" 2>/dev/null; rm -rf "$work"' EXIT |
| 66 | |
| 67 | base="$work/base" |
| 68 | mkdir "$base" |
| 69 | git -C "$repo" archive "$commit" | tar -x -C "$base" |
| 70 | |
| 71 | # One line per check: its name and its drvPath. |
| 72 | evaluate() { |
| 73 | local ref="$1" |
| 74 | # shellcheck disable=SC2016 # the expression is Nix, not shell |
| 75 | nix eval --raw "$ref#checks.$system" --apply ' |
| 76 | cs: builtins.concatStringsSep "" (map (n: n + " " + cs.${n}.drvPath + "\n") |
| 77 | (builtins.attrNames cs))' |
| 78 | } |
| 79 | |
| 80 | # The flake reference for a tree. In git mode the tree is committed first, |
| 81 | # with a fixed identity and date, so the same edit makes the same commit. |
| 82 | flakeOf() { |
| 83 | local dir="$1" label="$2" |
| 84 | if [ "$mode" = git ]; then |
| 85 | git -C "$dir" add -A |
| 86 | GIT_AUTHOR_DATE=@0 GIT_COMMITTER_DATE=@0 git -C "$dir" \ |
| 87 | -c user.name=drv-churn -c user.email=drv-churn@localhost \ |
| 88 | commit --quiet --allow-empty --no-verify -m "$label" |
| 89 | echo "git+file://$dir?rev=$(git -C "$dir" rev-parse HEAD)" |
| 90 | else |
| 91 | echo "path:$dir" |
| 92 | fi |
| 93 | } |
| 94 | |
| 95 | # The comment line that file takes, by its name. |
| 96 | comment() { |
| 97 | case "$1" in |
| 98 | *.go | *.cue) echo "// drv-churn" ;; |
| 99 | *.md | *.txt | *.json) echo "drv-churn" ;; |
| 100 | *) echo "# drv-churn" ;; |
| 101 | esac |
| 102 | } |
| 103 | |
| 104 | # Apply one edit to dir: append to, add or delete path. |
| 105 | apply() { |
| 106 | local dir="$1" op="$2" path="$3" |
| 107 | case "$op" in |
| 108 | append) |
| 109 | [ -f "$dir/$path" ] || { echo "drv-churn: no file $path to edit" >&2; exit 1; } |
| 110 | printf '\n%s\n' "$(comment "$path")" >> "$dir/$path" |
| 111 | ;; |
| 112 | add) |
| 113 | [ ! -e "$dir/$path" ] || { echo "drv-churn: $path already exists" >&2; exit 1; } |
| 114 | mkdir -p "$(dirname "$dir/$path")" |
| 115 | comment "$path" > "$dir/$path" |
| 116 | ;; |
| 117 | delete) |
| 118 | [ -f "$dir/$path" ] || { echo "drv-churn: no file $path to delete" >&2; exit 1; } |
| 119 | rm "$dir/$path" |
| 120 | ;; |
| 121 | commit) ;; |
| 122 | esac |
| 123 | } |
| 124 | |
| 125 | # The fixed edits, as label, operation and path. A Go module's edit is to |
| 126 | # its first source file that is not a test. |
| 127 | edits() { |
| 128 | cat <<'EOF' |
| 129 | commit only commit - |
| 130 | bin/valley append bin/valley |
| 131 | completions append completions/_valley |
| 132 | doc append docs/architecture.md |
| 133 | doc: README append README.md |
| 134 | doc: module README append sigverify/README.md |
| 135 | doc: archive append archive/design/roadmap.md |
| 136 | doc: research append research/README.md |
| 137 | doc: added add docs/churn-probe.md |
| 138 | schema: attestation append schema/attestation.cue |
| 139 | schema: events append schema/events.cue |
| 140 | schema: identity append schema/identity.cue |
| 141 | schema: node append schema/node.cue |
| 142 | schema: valley append schema/valley.cue |
| 143 | schema: verification append schema/verification.cue |
| 144 | check script append nix/checks/valley-status.sh |
| 145 | check script: shared append nix/checks/fail-nth.sh |
| 146 | check script: added add nix/checks/churn-probe.sh |
| 147 | nix: module append nix/valley-host.nix |
| 148 | nix: packages append nix/packages.nix |
| 149 | nix: knowledge lint append nix/knowledge-lint.py |
| 150 | flake.nix append flake.nix |
| 151 | policy append policy/project/policy.cue |
| 152 | fixture: host append examples/hosts/protected.cue |
| 153 | fixture: smoke policy append examples/policy/smoke/project/policy.cue |
| 154 | fixture: deriver policy append examples/policy/deriver/project.cue |
| 155 | fixture: registry append examples/identity/registry/registry.cue |
| 156 | fixture: graph append examples/graphs/consumer/README.md |
| 157 | fixture: graph deleted delete examples/graphs/consumer/docs/design.md |
| 158 | fixture: integrator e2e append integrator/e2e/run.sh |
| 159 | fixture: plan golden append status/testdata/plan/plan.txt |
| 160 | fixture: attest vectors append attest/conformance/README.md |
| 161 | go test: status append status/status_test.go |
| 162 | EOF |
| 163 | local mod file |
| 164 | while IFS= read -r mod; do |
| 165 | file="$(cd "$base/$mod" && find . -maxdepth 1 -name '*.go' ! -name '*_test.go' | LC_ALL=C sort | head -n1)" |
| 166 | [ -n "$file" ] || continue |
| 167 | printf 'go: %s\tappend\t%s\n' "$mod" "$mod/${file#./}" |
| 168 | done < <(cd "$base" && find . -name go.mod ! -path './archive/*' -printf '%h\n' | sed 's|^\./||' | LC_ALL=C sort) |
| 169 | } |
| 170 | |
| 171 | # The history's edits: one per non-merge commit in the range, oldest |
| 172 | # first, as label and the paths it touched. |
| 173 | historyEdits() { |
| 174 | local c |
| 175 | git -C "$repo" rev-list --reverse --no-merges "$history" | while IFS= read -r c; do |
| 176 | printf '%s %s\t' "$(git -C "$repo" rev-parse --short "$c")" \ |
| 177 | "$(git -C "$repo" log -1 --format=%s "$c" | cut -c1-60)" |
| 178 | git -C "$repo" diff-tree --no-commit-id --name-only -r "$c" | tr '\n' ' ' |
| 179 | echo |
| 180 | done |
| 181 | } |
| 182 | |
| 183 | # The checks whose drvPath differs between two evaluations. |
| 184 | changed() { |
| 185 | join -j1 <(LC_ALL=C sort "$1") <(LC_ALL=C sort "$2") | |
| 186 | awk '$2 != $3 { print $1 }' | tr '\n' ' ' | sed 's/ $//' |
| 187 | } |
| 188 | |
| 189 | if [ "$mode" = git ]; then |
| 190 | git -C "$base" init --quiet --initial-branch=main |
| 191 | fi |
| 192 | evaluate "$(flakeOf "$base" base)" > "$work/base.drvs" |
| 193 | total="$(wc -l < "$work/base.drvs")" |
| 194 | echo "# drv-churn: $(git -C "$repo" rev-parse --short "$commit") ($mode mode), $total checks" >&2 |
| 195 | |
| 196 | results="$work/results" |
| 197 | : > "$results" |
| 198 | run() { |
| 199 | local label="$1" dir="$work/edit" |
| 200 | shift |
| 201 | if [ "$mode" = git ]; then |
| 202 | dir="$base" |
| 203 | git -C "$dir" checkout --quiet --force --detach main |
| 204 | git -C "$dir" clean --quiet -fdx |
| 205 | else |
| 206 | rm -rf "$dir" |
| 207 | cp -a "$base" "$dir" |
| 208 | fi |
| 209 | while [ $# -gt 0 ]; do |
| 210 | apply "$dir" "$1" "$2" |
| 211 | shift 2 |
| 212 | done |
| 213 | evaluate "$(flakeOf "$dir" "$label")" > "$work/edit.drvs" |
| 214 | local names |
| 215 | names="$(changed "$work/base.drvs" "$work/edit.drvs")" |
| 216 | printf '%s\t%s\n' "$label" "$names" >> "$results" |
| 217 | printf '%-34s %3d %s\n' "$label" "$(wc -w <<< "$names")" "$names" |
| 218 | } |
| 219 | |
| 220 | if [ -n "$history" ]; then |
| 221 | sum=0 |
| 222 | count=0 |
| 223 | while IFS=$'\t' read -r label paths; do |
| 224 | args=() |
| 225 | for p in $paths; do |
| 226 | [ -f "$base/$p" ] && args+=(append "$p") |
| 227 | done |
| 228 | run "$label" "${args[@]}" |
| 229 | sum=$((sum + $(tail -n1 "$results" | cut -f2 | wc -w))) |
| 230 | count=$((count + 1)) |
| 231 | done < <(historyEdits) |
| 232 | awk -v s="$sum" -v c="$count" -v t="$total" \ |
| 233 | 'BEGIN { printf "mean: %.1f of %d checks changed per commit, over %d commits\n", s / c, t, c }' |
| 234 | else |
| 235 | while IFS=$'\t' read -r label op path; do |
| 236 | run "$label" "$op" "$path" |
| 237 | done < <(edits) |
| 238 | fi |
| 239 | |
| 240 | if [ -n "$write" ]; then |
| 241 | cp "$results" "$write" |
| 242 | fi |
| 243 | if [ -n "$expect" ]; then |
| 244 | diff -u "$expect" "$results" >&2 || { |
| 245 | echo "drv-churn: the edits changed other checks than $expect records" >&2 |
| 246 | exit 1 |
| 247 | } |
| 248 | fi |