gunk-dev/the-valley

drv-churn.sh

source58cae22c1ac6main's published tip history
8.9 KiB
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.
37set -euo pipefail
38
39mode=path
40history=
41expect=
42write=
43rev=HEAD
44while [ $# -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
55done
56if { [ -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
59fi
60
61repo="$(git rev-parse --show-toplevel)"
62commit="$(git -C "$repo" rev-parse --verify "$rev^{commit}")"
63system="$(nix eval --impure --raw --expr builtins.currentSystem)"
64work="$(mktemp -d "${KLAUS_SCRATCH:-${TMPDIR:-/tmp}}/drv-churn.XXXXXX")"
65trap 'chmod -R u+w "$work" 2>/dev/null; rm -rf "$work"' EXIT
66
67base="$work/base"
68mkdir "$base"
69git -C "$repo" archive "$commit" | tar -x -C "$base"
70
71# One line per check: its name and its drvPath.
72evaluate() {
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.
82flakeOf() {
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.
96comment() {
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.
105apply() {
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.
127edits() {
128 cat <<'EOF'
129commit only commit -
130bin/valley append bin/valley
131completions append completions/_valley
132doc append docs/architecture.md
133doc: README append README.md
134doc: module README append sigverify/README.md
135doc: archive append archive/design/roadmap.md
136doc: research append research/README.md
137doc: added add docs/churn-probe.md
138schema: attestation append schema/attestation.cue
139schema: events append schema/events.cue
140schema: identity append schema/identity.cue
141schema: node append schema/node.cue
142schema: valley append schema/valley.cue
143schema: verification append schema/verification.cue
144check script append nix/checks/valley-status.sh
145check script: shared append nix/checks/fail-nth.sh
146check script: added add nix/checks/churn-probe.sh
147nix: module append nix/valley-host.nix
148nix: packages append nix/packages.nix
149nix: knowledge lint append nix/knowledge-lint.py
150flake.nix append flake.nix
151policy append policy/project/policy.cue
152fixture: host append examples/hosts/protected.cue
153fixture: smoke policy append examples/policy/smoke/project/policy.cue
154fixture: deriver policy append examples/policy/deriver/project.cue
155fixture: registry append examples/identity/registry/registry.cue
156fixture: graph append examples/graphs/consumer/README.md
157fixture: graph deleted delete examples/graphs/consumer/docs/design.md
158fixture: integrator e2e append integrator/e2e/run.sh
159fixture: plan golden append status/testdata/plan/plan.txt
160fixture: attest vectors append attest/conformance/README.md
161go test: status append status/status_test.go
162EOF
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.
173historyEdits() {
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.
184changed() {
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
189if [ "$mode" = git ]; then
190 git -C "$base" init --quiet --initial-branch=main
191fi
192evaluate "$(flakeOf "$base" base)" > "$work/base.drvs"
193total="$(wc -l < "$work/base.drvs")"
194echo "# drv-churn: $(git -C "$repo" rev-parse --short "$commit") ($mode mode), $total checks" >&2
195
196results="$work/results"
197: > "$results"
198run() {
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
220if [ -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 }'
234else
235 while IFS=$'\t' read -r label op path; do
236 run "$label" "$op" "$path"
237 done < <(edits)
238fi
239
240if [ -n "$write" ]; then
241 cp "$results" "$write"
242fi
243if [ -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 }
248fi