| 1 |
#!/usr/bin/env bash |
| 2 |
# spell-checker:ignore (jargon) profdata profraw sysroot rustlib nullglob aeiou nocheck CGU Cprofile cygpath atexit |
| 3 |
# |
| 4 |
# Build uutils coreutils with Profile-Guided Optimization. |
| 5 |
# |
| 6 |
# 1. build an instrumented multicall binary (-Cprofile-generate) |
| 7 |
# 2. run representative workloads to collect raw profiles |
| 8 |
# 3. merge them with llvm-profdata |
| 9 |
# 4. build the optimized binary (-Cprofile-use), unless --train-only |
| 10 |
# |
| 11 |
# Runs on Linux, macOS and Windows (git-bash). |
| 12 |
# |
| 13 |
# Usage: |
| 14 |
# util/build-pgo.sh [--target TRIPLE] [--target-dir DIR] [--features LIST] |
| 15 |
# [--train-only] [--llvm-profdata PATH] |
| •16 |
# [--instrumented-binary PATH] |
| 17 |
# |
| 18 |
# --instrumented-binary skips step 1 and trains the binary given instead. Use it |
| 19 |
# when the caller already builds the binary with its own flags (a distribution |
| 20 |
# package, say): the profile is only usable by a build that matches the one it |
| 21 |
# was collected from, so the instrumented and final builds must be produced by |
| 22 |
# the same command, with the same features and profile settings. |
| 23 |
|
| 24 |
set -euo pipefail |
| 25 |
|
| 26 |
# On Windows the instrumented binary, rustc and the profiler runtime are all |
| 27 |
# native programs that do not understand git-bash's `/d/a/...` paths, while bash |
| 28 |
# itself does not understand `D:\a\...`. `cygpath -m` yields `D:/a/...`, which |
| 29 |
# both sides accept, so every path the script builds goes through it. |
| 30 |
norm_path() { |
| 31 |
if command -v cygpath >/dev/null 2>&1; then cygpath -m "$1"; else printf '%s\n' "$1"; fi |
| 32 |
} |
| 33 |
|
| 34 |
REPO_ROOT="$(norm_path "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)")" |
| 35 |
TARGET_DIR="${REPO_ROOT}/target/coreutils-pgo" |
| 36 |
FEATURES="unix" |
| 37 |
TARGET="" |
| 38 |
TRAIN_ONLY=0 |
| 39 |
LLVM_PROFDATA="" |
| •40 |
INSTRUMENTED_BINARY="" |
| 41 |
|
| 42 |
while [ $# -gt 0 ]; do |
| 43 |
case "$1" in |
| 44 |
--target) TARGET="$2"; shift 2 ;; |
| 45 |
--target-dir) TARGET_DIR="$2"; shift 2 ;; |
| 46 |
--features) FEATURES="$2"; shift 2 ;; |
| 47 |
--llvm-profdata) LLVM_PROFDATA="$2"; shift 2 ;; |
| •48 |
--instrumented-binary) INSTRUMENTED_BINARY="$2"; shift 2 ;; |
| 49 |
--train-only) TRAIN_ONLY=1; shift ;; |
| •50 |
-h|--help) sed -n '3,22p' "${BASH_SOURCE[0]}"; exit 0 ;; |
| 51 |
*) echo "unknown argument: $1" >&2; exit 2 ;; |
| 52 |
esac |
| 53 |
done |
| 54 |
|
| 55 |
mkdir -p "$TARGET_DIR" |
| 56 |
TARGET_DIR="$(norm_path "$(cd "$TARGET_DIR" && pwd)")" |
| 57 |
|
| 58 |
HOST="$(rustc --print host-tuple)" |
| 59 |
[ -n "$TARGET" ] || TARGET="$HOST" |
| 60 |
# Cargo puts the artifacts of an explicit --target under a per-target directory, |
| 61 |
# and Windows binaries carry a suffix. |
| 62 |
case "$TARGET" in *windows*) EXE=".exe" ;; *) EXE="" ;; esac |
| 63 |
echo "target: ${TARGET} (host: ${HOST})" |
| 64 |
|
| 65 |
SCRIPT_START=$SECONDS |
| 66 |
|
| 67 |
fmt_duration() { printf '%dm%02ds' $(($1 / 60)) $(($1 % 60)); } |
| 68 |
begin_step() { |
| 69 |
STEP_NAME="$1" |
| 70 |
STEP_START=$SECONDS |
| 71 |
echo |
| 72 |
echo "=== ${STEP_NAME} ===" |
| 73 |
} |
| 74 |
end_step() { echo "--- ${STEP_NAME}: $(fmt_duration $((SECONDS - STEP_START))) ---"; } |
| 75 |
|
| 76 |
PROFILE_DIR="${TARGET_DIR}/profiles" |
| 77 |
CORPUS_DIR="${TARGET_DIR}/corpus" |
| 78 |
MERGED="${TARGET_DIR}/coreutils.profdata" |
| 79 |
|
| 80 |
# llvm-profdata must come from the *active* toolchain: its version has to match |
| 81 |
# the rustc that instrumented the binary. |
| 82 |
if [ -z "$LLVM_PROFDATA" ]; then |
| 83 |
SYSROOT="$(norm_path "$(rustc --print sysroot)")" |
| 84 |
# llvm-profdata ships for the host, whatever we are cross-building for. |
| 85 |
case "$HOST" in *windows*) PROFDATA_EXE=".exe" ;; *) PROFDATA_EXE="" ;; esac |
| 86 |
LLVM_PROFDATA="${SYSROOT}/lib/rustlib/${HOST}/bin/llvm-profdata${PROFDATA_EXE}" |
| 87 |
fi |
| 88 |
if [ ! -x "$LLVM_PROFDATA" ]; then |
| 89 |
echo "llvm-profdata not found at ${LLVM_PROFDATA}" >&2 |
| 90 |
echo "Run: rustup component add llvm-tools" >&2 |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
echo "llvm-profdata: ${LLVM_PROFDATA}" |
| 94 |
|
| 95 |
cargo_build() { |
| 96 |
# $1: target dir, $2: extra rustflags |
| 97 |
local feature_args=() |
| 98 |
if [ -n "$FEATURES" ]; then feature_args=(--features="$FEATURES"); fi |
| 99 |
( |
| 100 |
cd "$REPO_ROOT" |
| 101 |
export CARGO_TARGET_DIR="$1" |
| 102 |
export CARGO_INCREMENTAL=0 |
| 103 |
export RUSTFLAGS="${RUSTFLAGS:+${RUSTFLAGS} }$2" |
| 104 |
# bash 3.2 (macOS) errors on an empty array expansion under `set -u`, |
| 105 |
# hence the `[@]+` guard on both expansions below. |
| 106 |
echo "Running: cargo build --release --target=${TARGET} ${feature_args[*]+${feature_args[*]}}" |
| 107 |
echo " RUSTFLAGS=${RUSTFLAGS}" |
| 108 |
cargo build --release --target="$TARGET" ${feature_args[@]+"${feature_args[@]}"} |
| 109 |
) |
| 110 |
} |
| 111 |
|
| 112 |
begin_step "Step 1: instrumented build" |
| 113 |
INSTR_DIR="${TARGET_DIR}/instrumented" |
| 114 |
rm -rf "$PROFILE_DIR" |
| 115 |
mkdir -p "$PROFILE_DIR" |
| 116 |
# The instrumented build must use the same LTO and codegen-unit settings as the |
| 117 |
# final one. Inlining happens before instrumentation, so building it with LTO |
| 118 |
# off yields counters for functions that no longer exist once the final build |
| 119 |
# runs whole-program codegen, and the profile is then largely wasted on it. |
| 120 |
# |
| 121 |
# `--cfg pgo_training` makes the binary flush its own counters before exiting: |
| 122 |
# it always leaves through `std::process::exit`, which on Windows is |
| 123 |
# `ExitProcess` and skips the `atexit` handler the profiling runtime would |
| 124 |
# otherwise write the profile from. |
| •125 |
if [ -n "$INSTRUMENTED_BINARY" ]; then |
| 126 |
echo "using the caller's instrumented binary: ${INSTRUMENTED_BINARY}" |
| 127 |
echo "(it must have been built with -Cprofile-generate=${PROFILE_DIR} --cfg pgo_training)" |
| 128 |
BIN="$INSTRUMENTED_BINARY" |
| 129 |
else |
| 130 |
cargo_build "$INSTR_DIR" "-Cprofile-generate=${PROFILE_DIR} --cfg pgo_training" |
| 131 |
BIN="${INSTR_DIR}/${TARGET}/release/coreutils${EXE}" |
| 132 |
fi |
| 133 |
[ -x "$BIN" ] || { echo "instrumented binary not found: ${BIN}" >&2; exit 1; } |
| 134 |
# Training runs the binary we just built, so a foreign target only works where |
| 135 |
# the host can execute it (x86_64 on arm64 macOS needs Rosetta, for instance). |
| 136 |
if ! "$BIN" true >/dev/null 2>&1; then |
| 137 |
echo "cannot run the instrumented ${TARGET} binary on this ${HOST} host" >&2 |
| 138 |
exit 1 |
| 139 |
fi |
| 140 |
end_step |
| 141 |
|
| 142 |
begin_step "Step 2: corpus" |
| 143 |
# Everything is generated from scratch: reading whatever /usr/share/dict/words |
| 144 |
# or /etc/passwd happens to hold would make the profile machine-dependent. |
| 145 |
export LLVM_PROFILE_FILE="${PROFILE_DIR}/coreutils-%p.profraw" |
| 146 |
rm -rf "$CORPUS_DIR" |
| 147 |
mkdir -p "$CORPUS_DIR" |
| 148 |
|
| 149 |
WORDS="${CORPUS_DIR}/words.txt" |
| 150 |
NUMBERS="${CORPUS_DIR}/numbers.txt" |
| 151 |
REPEATED="${CORPUS_DIR}/repeated.txt" |
| 152 |
PAIRS="${CORPUS_DIR}/pairs.txt" |
| 153 |
BLOB="${CORPUS_DIR}/blob.bin" |
| 154 |
COLUMNS="${CORPUS_DIR}/columns.txt" |
| 155 |
|
| 156 |
# Built with bash's own printf rather than awk: git-bash has no awk, and the |
| 157 |
# whole corpus is still generated in a couple of seconds. |
| 158 |
# Shuffled by a stride so sort/uniq get unordered input rather than a no-op. |
| 159 |
for ((i = 0; i < 200000; i++)); do printf 'w%06d\n' $(((i * 7919) % 200000)); done > "$WORDS" |
| 160 |
for ((i = 0; i < 100000; i++)); do printf 'line%04d\n' $((i % 1000)); done > "$REPEATED" |
| 161 |
for ((i = 0; i < 100000; i++)); do printf '%08d value%d\n' "$i" "$i"; done > "$PAIRS" |
| 162 |
for ((i = 0; i < 2000; i++)); do printf 'user%d:x:%d:%d:User %d:/home/user%d:/bin/sh\n' "$i" $((1000 + i)) $((1000 + i)) "$i" "$i"; done > "$COLUMNS" |
| 163 |
# seq/dd are part of what we want to profile, so use the instrumented binary. |
| 164 |
"$BIN" seq 500000 > "$NUMBERS" |
| 165 |
"$BIN" dd "if=${BIN}" "of=${BLOB}" bs=64K count=64 2>/dev/null |
| 166 |
end_step |
| 167 |
|
| 168 |
begin_step "Step 3: training workloads" |
| 169 |
# Kept inside the target dir rather than under TMPDIR: git-bash may hand back a |
| 170 |
# Windows-style TMPDIR that neither mktemp nor the workloads would agree on. |
| 171 |
WORK="${TARGET_DIR}/work" |
| 172 |
rm -rf "$WORK" |
| 173 |
mkdir -p "$WORK" |
| 174 |
trap 'rm -rf "$WORK"' EXIT |
| 175 |
|
| 176 |
# Individual workloads are allowed to fail (a util may be absent from the |
| 177 |
# selected feature set); a partial profile is still a usable profile. |
| 178 |
run() { "$BIN" "$@" >/dev/null 2>&1 || true; } |
| 179 |
|
| 180 |
# sort: the single hottest util, in its main modes |
| 181 |
run sort "$WORDS" -o "${WORK}/sorted.txt" |
| 182 |
run sort -n "$NUMBERS" -o "${WORK}/sorted-n.txt" |
| 183 |
run sort -u "$REPEATED" -o "${WORK}/sorted-u.txt" |
| 184 |
run sort -r "$WORDS" -o "${WORK}/sorted-r.txt" |
| 185 |
run sort -k2,2 -t: "$COLUMNS" -o "${WORK}/sorted-k.txt" |
| 186 |
|
| 187 |
# wc / cat / head / tail, to a regular file so the write path is not /dev/null |
| 188 |
run wc -l "$WORDS" |
| 189 |
run wc -w "$WORDS" |
| 190 |
run wc -c "$BLOB" |
| 191 |
run wc "$WORDS" "$NUMBERS" |
| 192 |
"$BIN" cat "$WORDS" > "${WORK}/cat.txt" 2>/dev/null || true |
| 193 |
"$BIN" cat -n "$WORDS" > "${WORK}/cat-n.txt" 2>/dev/null || true |
| 194 |
"$BIN" head -n 50000 "$WORDS" > "${WORK}/head.txt" 2>/dev/null || true |
| 195 |
"$BIN" tail -n 50000 "$WORDS" > "${WORK}/tail.txt" 2>/dev/null || true |
| 196 |
run head -c 1M "$BLOB" |
| 197 |
|
| 198 |
"$BIN" cat "$WORDS" | "$BIN" sort | "$BIN" uniq -c > "${WORK}/pipe1.txt" 2>/dev/null || true |
| 199 |
"$BIN" seq 200000 | "$BIN" wc -l > /dev/null 2>&1 || true |
| 200 |
"$BIN" cat "$BLOB" | "$BIN" sha256sum > /dev/null 2>&1 || true |
| 201 |
"$BIN" sort -n "$NUMBERS" | "$BIN" tail -n 1000 | "$BIN" cut -c1-4 > "${WORK}/pipe2.txt" 2>/dev/null || true |
| 202 |
|
| 203 |
# text utils |
| 204 |
run uniq "$REPEATED" |
| 205 |
run uniq -c "$REPEATED" |
| 206 |
run uniq -u "$REPEATED" |
| 207 |
run cut -d: -f1,3 "$COLUMNS" |
| 208 |
run cut -c1-10 "$WORDS" |
| 209 |
run nl "$WORDS" |
| 210 |
run fold -w 40 "$WORDS" |
| 211 |
run expand "$WORDS" |
| 212 |
run unexpand "$WORDS" |
| 213 |
run paste "$WORDS" "$WORDS" |
| 214 |
run join "$PAIRS" "$PAIRS" |
| 215 |
run join --nocheck-order "$PAIRS" "$PAIRS" |
| 216 |
run split -l 20000 "$WORDS" "${WORK}/split-" |
| 217 |
"$BIN" tr a-z A-Z < "$WORDS" > "${WORK}/tr.txt" 2>/dev/null || true |
| 218 |
"$BIN" tr -d aeiou < "$WORDS" > "${WORK}/tr-d.txt" 2>/dev/null || true |
| 219 |
"$BIN" tee "${WORK}/tee.txt" < "$NUMBERS" > /dev/null 2>&1 || true |
| 220 |
|
| 221 |
# numbers / encoding / hashing |
| 222 |
run seq 1000000 |
| 223 |
run seq 0 0.1 10000 |
| 224 |
"$BIN" base64 "$BLOB" > "${WORK}/encoded.b64" 2>/dev/null || true |
| 225 |
run base64 -d "${WORK}/encoded.b64" |
| 226 |
run cksum "$BLOB" |
| 227 |
run md5sum "$BLOB" |
| 228 |
run sha1sum "$BLOB" |
| 229 |
run sha256sum "$BLOB" |
| 230 |
|
| 231 |
# file management: cp/mv/rm/ls are as hot in practice as anything above |
| 232 |
run cp "$WORDS" "${WORK}/copy.txt" |
| 233 |
run cp -r "${REPO_ROOT}/src/uu/sort" "${WORK}/tree" |
| 234 |
run mv "${WORK}/copy.txt" "${WORK}/moved.txt" |
| 235 |
run mv "${WORK}/tree" "${WORK}/tree2" |
| 236 |
run ls -la "${REPO_ROOT}/src/uu" |
| 237 |
run ls -lR "${REPO_ROOT}/src/uu" |
| 238 |
run ls --color=always -la "${REPO_ROOT}/src" |
| 239 |
run du -sh "${REPO_ROOT}/src" |
| 240 |
run df -h |
| 241 |
run rm -rf "${WORK}/tree2" |
| 242 |
|
| 243 |
echo "Workloads complete." |
| 244 |
end_step |
| 245 |
|
| 246 |
begin_step "Step 4: merging profiles" |
| 247 |
shopt -s nullglob |
| 248 |
RAW=("${PROFILE_DIR}"/coreutils-*.profraw) |
| 249 |
shopt -u nullglob |
| 250 |
if [ ${#RAW[@]} -eq 0 ]; then |
| 251 |
echo "no .profraw files found in ${PROFILE_DIR}" >&2 |
| 252 |
exit 1 |
| 253 |
fi |
| 254 |
echo "Merging ${#RAW[@]} profile(s)..." |
| 255 |
"$LLVM_PROFDATA" merge -sparse "${RAW[@]}" -o "$MERGED" |
| 256 |
echo "Merged profile: ${MERGED}" |
| 257 |
|
| 258 |
# A profile that covers almost nothing still builds fine and silently produces a |
| 259 |
# barely-optimized binary, so fail loudly instead: every workload in step 3 is |
| 260 |
# allowed to fail individually, and without this a broken corpus would ship. |
| 261 |
"$LLVM_PROFDATA" show "$MERGED" | head -6 |
| 262 |
COVERED="$("$LLVM_PROFDATA" show "$MERGED" | sed -n 's/^Total functions: *//p')" |
| 263 |
MIN_FUNCTIONS=500 |
| 264 |
if [ -z "$COVERED" ] || [ "$COVERED" -lt "$MIN_FUNCTIONS" ]; then |
| 265 |
echo "profile covers only ${COVERED:-0} functions (expected >= ${MIN_FUNCTIONS})" >&2 |
| 266 |
echo "the training workloads probably did not run; refusing to ship this profile" >&2 |
| 267 |
# Distinguish "the workloads never ran" from "they ran but the counters were |
| 268 |
# never written": the first leaves no/short raw files, the second leaves |
| 269 |
# plenty of same-sized ones that merge down to zero functions. |
| 270 |
echo "--- raw profiles in ${PROFILE_DIR}:" >&2 |
| 271 |
ls -l "$PROFILE_DIR" | head -12 >&2 |
| 272 |
echo "--- first raw profile (${RAW[0]}):" >&2 |
| 273 |
"$LLVM_PROFDATA" show "${RAW[0]}" >&2 || true |
| 274 |
echo "--- instrumented binary: ${BIN}" >&2 |
| 275 |
ls -l "$BIN" >&2 |
| 276 |
exit 1 |
| 277 |
fi |
| 278 |
echo "Profile covers ${COVERED} functions." |
| 279 |
end_step |
| 280 |
|
| 281 |
# CI needs the absolute, natively-spelled path to put in RUSTFLAGS; it cannot |
| 282 |
# reconstruct it portably because of the Windows path rewriting above. |
| 283 |
printf '%s\n' "$MERGED" > "${TARGET_DIR}/profdata-path.txt" |
| 284 |
|
| 285 |
if [ "$TRAIN_ONLY" -eq 1 ]; then |
| 286 |
echo |
| 287 |
echo "To use the profile in a release build, add to RUSTFLAGS:" |
| 288 |
echo " -Cprofile-use=${MERGED}" |
| 289 |
echo "Total: $(fmt_duration $((SECONDS - SCRIPT_START)))" |
| 290 |
exit 0 |
| 291 |
fi |
| 292 |
|
| 293 |
begin_step "Step 5: optimized build" |
| 294 |
cargo_build "$TARGET_DIR" "-Cprofile-use=${MERGED}" |
| 295 |
end_step |
| 296 |
echo |
| 297 |
echo "Optimized binary: ${TARGET_DIR}/${TARGET}/release/coreutils${EXE}" |
| 298 |
echo "Total: $(fmt_duration $((SECONDS - SCRIPT_START)))" |