OILS / regtest / aports-run.sh View on Github | oils.pub

758 lines, 351 significant
1#!/usr/bin/env bash
2#
3# Build Alpine Linux packages: baseline, OSH as /bin/sh, OSH as /bin/bash
4# See regtest/aports.md
5#
6# Usage:
7# regtest/aports-run.sh <function name>
8#
9# Common usage:
10#
11# export APORTS_EPOCH=2025-08-04-foo # optional override
12# $0 build-many-shards-overlayfs shard{0..16} # build all 17 shards in 2 configs
13#
14# Also useful:
15#
16# $0 fetch-packages fetch $pkg_filter $a_repo # alpine repo is 'main' or 'community'
17#
18# $0 fetch-packages fetch 100,300p # packages 100-300
19# $0 fetch-packages fetch '.*' # all packages
20#
21# Look for results in _tmp/aports-build/
22#
23# Build many packages:
24#
25# $0 build-packages-overlayfs osh-as-sh shard9 community
26# $0 build-packages-overlayfs osh-as-sh shardA # main is default $a_repo
27#
28# Build a single package:
29#
30# $0 build-package-overlayfs osh-as-sh userspace-rcu
31# $0 build-package-overlayfs osh-as-sh xterm community # community repo
32#
33# Drop into a shell:
34# INTERACTIVE=1 $0 build-package-overlayfs osh-as-sh userspace-rcu
35#
36# PKG_FILTER
37# shard[0-9]+ - shard3 is packages 301 to 400
38# [0-9]+ - 42 means build the first 42 packages
39# [0-9]+,[0-9]+p - 100,300p packages 100 to 300 (sed syntax)
40# ALL - all packages
41# .* - egrep pattern matching all packages
42# curl - egrep pattern matching 'curl'
43#
44# Preview packages:
45#
46# $0 package-dirs shard9 community
47
48: ${LIB_OSH=stdlib/osh}
49source $LIB_OSH/bash-strict.sh
50source $LIB_OSH/task-five.sh
51
52source regtest/aports-common.sh
53
54#
55# Config
56#
57
58show-config() {
59 enter-rootfs sh -c '
60 ls -l /bin/sh /bin/ash /bin/bash
61 '
62}
63
64save-default-config() {
65 enter-rootfs sh -c '
66 set -x
67 dest=/bin/bash.ORIG
68 cp /bin/bash $dest
69 '
70 show-config
71}
72
73
74set-baseline() {
75 # ensure we have the default config
76 enter-rootfs sh -c '
77 set -x
78 ln -s -f /bin/busybox /bin/sh
79 ln -s -f /bin/busybox /bin/ash
80 cp /bin/bash.ORIG /bin/bash
81 '
82 show-config
83}
84
85set-osh-as-X() {
86 local x=$1
87
88 enter-rootfs sh -c '
89 x=$1
90 set -x
91 if ! test -f /usr/local/bin/oils-for-unix; then
92 echo "Build Oils first"
93 exit
94 fi
95 ln -s -f /usr/local/bin/oils-for-unix /bin/$x
96 ' dummy0 "$x"
97 show-config
98}
99
100set-osh-as-sh() {
101 set-osh-as-X sh
102}
103
104set-osh-as-ash() {
105 set-osh-as-X ash
106}
107
108set-osh-as-bash() {
109 set-osh-as-X bash
110}
111
112#
113# Run
114#
115
116package-dirs() {
117 # lz gives 5 packages: some fail at baseline
118 # lzip: a single fast package
119 # mpfr4: OSH bug, and big log
120 # yash: make sure it doesn't hang
121 local package_filter=${1:-'lz|mpfr|yash'}
122 local a_repo=${2:-main} # or 'community'
123
124 local -a prefix
125
126 if [[ $package_filter = 'ALL' ]]; then
127 prefix=( cat )
128
129 # 100 means 0 to 100
130 elif [[ $package_filter =~ ^[0-9]+$ ]]; then
131 prefix=( head -n $package_filter )
132
133 # 100,300p means lines 100 to 300
134 elif [[ $package_filter =~ ^[0-9]+,[0-9]+p$ ]]; then
135 prefix=( sed -n $package_filter )
136
137 elif [[ $package_filter =~ ^shard([0-9]+)$ ]]; then
138 # shards of 100 packages
139
140 local shard_num=${BASH_REMATCH[1]}
141 #echo shard=$shard_num
142
143 local range
144 # shard 0 is 0-99
145 # shard 9 is 900 to 999
146 # shard 10 is 1000 to 1099
147 case $shard_num in
148 # sed doesn't like 000,099
149 0) range='1,100p' ;;
150 *) range="${shard_num}01,$(( shard_num + 1))00p" ;;
151 esac
152
153 prefix=( sed -n "$range" )
154
155 # shardA, shardB For testing the combined report
156 elif [[ $package_filter =~ ^shard([A-Z]+)$ ]]; then
157 local shard_name=${BASH_REMATCH[1]}
158 case $a_repo in
159 main)
160 case $shard_name in
161 A) package_filter='^gzip' ;; # failure
162 B) package_filter='^xz' ;; # failure
163 C) package_filter='^lz' ;; # 3 packages
164 D) package_filter='^jq$' ;; # produces autotools test-suite.log
165 E) package_filter='^py3-p' ;; # many packages in parallel
166 F) package_filter='^py3-pathspec' ;; # very fast package
167 P) package_filter='^xz$|^shorewall' ;; # patches
168 *) package_filter='^perl-http-daemon' ;; # test out perl
169 esac
170 ;;
171 community)
172 case $shard_name in
173 A) package_filter='^py3-zulip' ;; # one Python package
174 B) package_filter='^xterm' ;; # one C package
175 C) package_filter='^shfmt' ;; # one Go package
176 D) package_filter='^shellspec' ;; # OSH disagreement because of 'var'
177 *) package_filter='^shell' ;; # a bunch of packages
178 esac
179 ;;
180 *)
181 die "Invalid a_repo $a_repo"
182 ;;
183 esac
184
185 prefix=( egrep "$package_filter" )
186
187 elif [[ $package_filter =~ ^disagree-(.*)+$ ]]; then
188 local filename=${BASH_REMATCH[1]}
189 # A file of EXACT package names, not patterns
190 # See copy-disagree
191 local package_file="_tmp/$package_filter.txt"
192 comm -1 -2 <(sort $package_file) <(sort _tmp/apk-${a_repo}-manifest.txt)
193 return
194
195 else
196 prefix=( egrep "$package_filter" )
197
198 fi
199
200 "${prefix[@]}" _tmp/apk-${a_repo}-manifest.txt
201}
202
203copy-disagree() {
204 ### Determine what to run
205
206 local epoch=${1:-2025-09-18-bash}
207 cp -v \
208 _tmp/aports-report/$epoch/disagree-packages.txt \
209 _tmp/disagree-$epoch.txt
210}
211
212do-packages() {
213 ### Download sources - abuild puts it in /var/cahe/distfiles
214 local action=${1:-fetch}
215 local package_filter=${2:-}
216 local a_repo=${3:-main}
217 # flags to pass to the inner shell
218 local sh_flags=${4:-'-e -u'} # -u to disable -e
219
220 # 6 seconds for 10 packages
221 # There are ~1600 packages
222 # So if there are 20 shards, each shard could have 10?
223
224 local -a package_dirs
225 package_dirs=( $(package-dirs "$package_filter" "$a_repo") )
226
227 echo "${dirs[@]}"
228 #return
229
230 time enter-rootfs-user sh $sh_flags -c '
231
232 action=$1
233 a_repo=$2
234 shift 2
235 for dir in "$@"; do
236 time abuild -r -C aports/$a_repo/$dir "$action"
237 done
238 ' dummy0 "$action" "$a_repo" "${package_dirs[@]}"
239}
240
241fetch-packages() {
242 local package_filter=${1:-}
243 local a_repo=${2:-main}
244
245 # -u means we don't pass -e (and it's non-empty)
246 do-packages fetch "$package_filter" "$a_repo" '-u'
247}
248
249banner() {
250 echo
251 echo "=== $@"
252 echo
253}
254
255build-package-overlayfs() {
256 local config=${1:-baseline}
257 local pkg=${2:-lua5.4}
258 local a_repo=${3:-main}
259
260 # baseline stack:
261 # _chroot/aports-build
262 # _chroot/package-upper/baseline/gzip # upper dir / layer dir
263 #
264 # osh-as-sh stack:
265 # _chroot/aports-build
266 # _chroot/osh-as-sh.overlay/layer # this has the symlink
267 # _chroot/package-upper/osh-as-sh/gzip # upper dir / layer dir
268
269 # allow concurrency
270 local xargs_slot="${XARGS_SLOT:-99}"
271 local ov_base_dir=_chroot/package-slot${xargs_slot}.overlay
272
273 local merged=$ov_base_dir/merged
274 local work=$ov_base_dir/work
275
276 local layer_dir=_chroot/package-layers/$config/$pkg
277 mkdir -p $merged $work $layer_dir
278
279 local overlay_opts
280 case $config in
281 baseline)
282 overlay_opts="lowerdir=$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
283 ;;
284 osh-as-sh)
285 local osh_as_sh=_chroot/osh-as-sh.overlay/layer
286 overlay_opts="lowerdir=$osh_as_sh:$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
287 ;;
288 *)
289 die "Invalid config $config"
290 ;;
291 esac
292
293 sudo mount \
294 -t overlay \
295 aports-package \
296 -o "$overlay_opts" \
297 $merged
298
299 local -a prefix
300 if test -n "$XARGS_SLOT"; then
301 local x=$XARGS_SLOT
302
303 # run slot 0 on cores 0 and 1
304 # run slot 9 on cores 18 and 19
305 local cores="$(( x*2 )),$(( x*2 + 1 ))"
306
307 # oversubscribe
308 # run slot 0 on cores 0 and 1
309 # run slot 19 on cores 19 and 0
310 #local cores="$(( x )),$(( (x + 1) % NUM_CORES ))"
311 prefix=( taskset -c "$cores" )
312 fi
313
314 "${prefix[@]}" $merged/enter-chroot -u udu sh -c '
315 cd oils
316
317 # show the effect of the overlay
318 #ls -l /bin/sh
319
320 regtest/aports-guest.sh build-one-package "$@"
321 ' dummy0 "$pkg" "$a_repo" "$xargs_slot"
322
323 if test -n "$INTERACTIVE"; then
324 echo "Starting interactive shell in overlayfs environment for package $a_repo/$pkg"
325 echo "Rebuild: abuild -f -r -C ~/aports/$a_repo/$pkg"
326 echo " Help: abuild -h"
327 # If the last command in the child shell exited non-zero then ctrl-d/exit
328 # will report that error code to the parent. If we don't ignore that error
329 # we will exit early and leave the package overlay mounted.
330 set +o errexit
331 $merged/enter-chroot -u udu
332 set -o errexit
333 fi
334
335 unmount-loop $merged
336}
337
338build-pkg() {
339 ### trivial wrapper around build-package-overlayfs - change arg order for xargs
340 local config=${1:-baseline}
341 local a_repo=${2:-main}
342 local pkg=${3:-lua5.4}
343
344 build-package-overlayfs "$config" "$pkg" "$a_repo"
345
346 # TODO:
347 # - we should only do this after we've done BOTH configs, so it appears
348 # atomically
349 save-package-files $config $a_repo $pkg
350
351 # TODO: blow away the layer dir, since we saved the "tombstone".
352 # We're not doing this now because we're still reporting off DEPRECATED shard
353 # files.
354}
355
356LOG_SIZE_THRESHOLD=$(( 500 * 1000 )) # 500 KB
357#LOG_SIZE_THRESHOLD=$(( 1 * 1000 ))
358
359abridge-one-log() {
360 local src=$1
361 local dest=$2
362
363 local size
364 size=$(stat --format '%s' $src)
365 if test $size -lt $LOG_SIZE_THRESHOLD; then
366 cp --verbose $src $dest
367 else
368 # Bug fix: abridging to 1000 lines isn't sufficient. We got some logs
369 # that were hundreds of MB, with less than 1000 lines!
370 { echo "*** This log is abridged to its last $LOG_SIZE_THRESHOLD bytes"
371 echo
372 tail --bytes $LOG_SIZE_THRESHOLD $src
373 } > $dest
374 fi
375}
376
377# save-package-files creates a tree we can rsync
378# For EACH PACKAGE, without shards
379#
380# TODO: Both baseline and osh-as-sh should appear atomically?
381
382# Source tree:
383#
384# _chroot/package-layers/
385# baseline/
386# jq/
387# home/udu/
388# oils/_tmp/aports-guest/
389# jq.log.txt
390# jq.task.tsv
391# packages/main/x86_64
392# jq-*.apk
393# aports/main/jq/
394# src/jq-1.8.0/test-suite.log
395#
396# _tmp/aports-build/
397# 2025-11-12/
398# shard0/ # TODO: remove shards
399# baseline/
400# NEW:
401# apk/
402# jq.apk.txt # md5sum
403# layer/
404# jq.tombstone.txt # find '%s %P\n'
405# task/
406# jq.task.tsv
407# EXISTING:
408# apk.txt
409# tasks.tsv
410# log/
411# jq.log.txt
412# test-suite/
413# jq/ TODO: support multiple logs
414# test-suite.log
415
416save-package-files() {
417 ### Copy some files from _chroot/package-layers/ -> _tmp/aports-build
418
419 local config=${1:-baseline}
420 local a_repo=${2:-main}
421 local pkg=${3:-jq}
422
423 local layer_dir=_chroot/package-layers/$config/$pkg
424 local dest_dir=$BASE_DIR/$APORTS_EPOCH/$config
425
426 # 5 directories
427 mkdir -p $dest_dir/{apk,layer,task,log,test-suite}
428
429 cp --verbose \
430 $layer_dir/home/udu/oils/_tmp/aports-guest/$pkg.task.tsv \
431 $dest_dir/task
432
433 abridge-one-log \
434 $layer_dir/home/udu/oils/_tmp/aports-guest/$pkg.log.txt \
435 $dest_dir/log/$pkg.log.txt
436
437 # Abridge this log too
438 find $layer_dir/home/udu/aports/$a_repo/$pkg -name 'test-suite.log' |
439 while read -r log_src; do
440 local test_suite_dest_dir=$dest_dir/test-suite/$pkg
441 mkdir -p $test_suite_dest_dir
442 abridge-one-log \
443 $log_src \
444 $test_suite_dest_dir/test-suite.log.txt
445 done
446
447 md5sum $layer_dir/home/udu/packages/$a_repo/x86_64/*.apk \
448 > $dest_dir/apk/$pkg.apk.txt || true # allow failure if nothing built
449
450 find $layer_dir -printf '%s %P\n' \
451 > $dest_dir/layer/$pkg.tombstone.txt || true
452
453 #tree $dest_dir
454
455 # log.txt
456 # log.txt
457}
458
459NUM_CORES=$(( $(nproc) ))
460
461# 2 cores per package build
462NUM_PAR=$(( NUM_CORES / 2 ))
463
464# over-subscribe - allow 20 processes to see 2 cores each
465# Note: this causes more timeouts. TODO: get rid of shards to get rid of
466# stragglers, and then raise the timeout to 20 minutes or more.
467# NUM_PAR=$(( NUM_CORES ))
468
469# TODO: we ran into the env.sh race condition in the enter-chroot script
470# generated by alpine-chroot-install
471build-many-packages-overlayfs() {
472 local package_filter=${1:-}
473 local config=${2:-baseline}
474 local a_repo=${3:-main}
475 local parallel=${4:-T}
476
477 banner "Building packages (filter=$package_filter a_repo=$a_repo)"
478
479 local -a flags
480 if test -n "$parallel"; then
481 log "(with $NUM_PAR jobs in parallel)"
482 flags=( -P $NUM_PAR )
483 else
484 log '(serially)'
485 fi
486
487 package-dirs "$package_filter" $a_repo |
488 xargs "${flags[@]}" -n 1 --process-slot-var=XARGS_SLOT -- \
489 $0 build-pkg $config $a_repo
490}
491
492
493clean-host-and-guest() {
494 # host dir _tmp/aports-build
495 rm -r -f -v $BASE_DIR
496}
497
498clean-guest() {
499 # clean guest chroot
500 sudo rm -r -f -v $CHROOT_HOME_DIR/oils/_tmp
501}
502
503readonly -a CONFIGS=( baseline osh-as-sh )
504
505APORTS_EPOCH="${APORTS_EPOCH:-}"
506# default epoch
507if test -z "$APORTS_EPOCH"; then
508 APORTS_EPOCH=$(date '+%Y-%m-%d')
509fi
510
511_build-many-configs-overlayfs() {
512 local package_filter=${1:-}
513 local epoch=${2:-$APORTS_EPOCH}
514 local a_repo=${3:-main}
515
516 if test -z "$package_filter"; then
517 die "Package filter is required (e.g. shard3, ALL)"
518 fi
519
520 clean-guest
521
522 # See note about /etc/sudoers.d at top of file
523
524 local dest_dir="$BASE_DIR/$epoch/$package_filter" # e.g. shard10
525
526 for config in "${CONFIGS[@]}"; do
527 banner "$epoch: Using config $config"
528
529 build-many-packages-overlayfs "$package_filter" "$config" "$a_repo"
530 done
531}
532
533remove-shard-files() {
534 local shard_dir=${1:-_chroot/shardC}
535
536 # For all packages packages, for baseline and osh-as-sh, clean up the aports source dir
537 # For linux, clang, etc. it becomes MANY GIGABYTES
538 #
539 # 2025-09-12: ignore errors from rm; I think there was a race condition -
540 # processes could still be running and creating files
541 #
542 # rm: cannot remove '_chroot/shard6/baseline/llvm19/home/udu/aports/main/llvm19/src/llvm-project-19.1.7.src/build/lib': Directory not empty
543 # real 1041m46.464s
544
545 #log "Removing big files in shard $shard_dir"
546 #sudo rm -r -f $shard_dir/*/*/home/udu/aports/ || true
547
548 log "Removing all files in $shard_dir"
549 sudo rm -r -f $shard_dir || true
550}
551
552build-many-shards-overlayfs() {
553 sudo -k
554
555 local a_repo=${A_REPO:-main} # env var like $APORTS_EPOCH
556
557 # Clean up old runs
558 sudo rm -r -f _chroot/shard* _chroot/disagree*
559
560 banner "$APORTS_EPOCH $a_repo: building shards: $*"
561
562 time for shard_name in "$@"; do
563 _build-many-configs-overlayfs "$shard_name" "$APORTS_EPOCH" "$a_repo"
564
565 # Move layer files to _chroot/shard10/{baseline,osh}/...
566 mv -v --no-target-directory _chroot/package-layers _chroot/$shard_name
567
568 # Make it rsync-able in _tmp/aports-build ($BASE_DIR)
569 make-shard-tree $shard_name $a_repo
570
571 # Remove big files
572 remove-shard-files _chroot/$shard_name
573
574 # TODO: we should publish and clean up after every PACKAGE, rather than
575 # each shard
576 done
577}
578
579build-and-stat() {
580 # Measure resource utilization
581 local proc_dir="$BASE_DIR/$APORTS_EPOCH/proc-log"
582 mkdir -v -p $proc_dir
583 regtest/proc_log.py --out-dir $proc_dir --sleep-secs 5 &
584 local proc_log_pid=$!
585
586 sleep 0.05 # prevent overlapping sudo prompt
587
588 build-many-shards-overlayfs "$@"
589
590 kill -s TERM $proc_log_pid
591 wc -l $proc_dir/*.txt
592}
593
594make-shard-tree() {
595 ### Put outputs in rsync-able format, for a SINGLE shard
596
597 # The dir structure is like this:
598 #
599 # _tmp/aports-build/
600 # 2025-09-10-overlayfs/
601 # shard0/
602 # baseline/
603 # apk.txt
604 # tasks.tsv
605 # log/
606 # gzip.log.txt
607 # xz.log.txt
608 # test-suite/ # autotools dir
609 # gzip/
610 # test-suite.log.txt
611 # osh-as-sh/
612 # apk.txt
613 # tasks.tsv
614 # log/
615 # gzip.log.txt
616 # xz.log.txt
617 # test-suite/
618 # gzip/
619 # test-suite.log.txt
620 # shard1/
621 # ...
622 # shard16/
623 # ...
624
625 local shard_name=$1
626 local a_repo=${2:-main}
627 local epoch=${3:-$APORTS_EPOCH}
628
629 local shard_dir=_chroot/$shard_name
630
631 for config in baseline osh-as-sh; do
632 local dest_dir=$BASE_DIR/$epoch/$shard_name/$config
633 mkdir -p $dest_dir
634 #ls -l $shard_dir/$config
635
636 # Four outputs
637 # 1) log.txt for each package
638 # 2) Optional test-suite.txt for each package
639 # 3) merged tasks.tsv
640 # - comes from .task.tsv
641 # 4) merged apk.txt
642 #
643 # So 3 and 4 should not be merged yet
644 #
645 # _tmp/aports-build/
646 # 2025-11-12/
647 # shardP/
648 # baseline/
649 # log/
650 # test-suite/
651 # apk.txt
652 # tasks.tsv
653 #
654 # We want to
655
656 time python3 devtools/tsv_concat.py \
657 $shard_dir/$config/*/home/udu/oils/_tmp/aports-guest/*.task.tsv > $dest_dir/tasks.tsv
658
659 # Allowed to fail if zero .apk are built
660 time md5sum $shard_dir/$config/*/home/udu/packages/$a_repo/x86_64/*.apk > $dest_dir/apk.txt \
661 || true
662
663 abridge-logs $shard_dir/$config $dest_dir
664
665 done
666}
667
668abridge-logs() {
669 local config_src_dir=${1:-_chroot/shardD/osh-as-sh}
670 local dest_dir=${2:-$BASE_DIR/shardD/osh-as-sh}
671
672 local log_dest_dir=$dest_dir/log
673 local test_suite_dest_dir=$dest_dir/test-suite
674 mkdir -p $log_dest_dir $test_suite_dest_dir
675
676 local threshold=$(( 500 * 1000 )) # 500 KB
677
678 # this assumes the build process doesn't create *.log.txt
679 # test-suite.log is the name used by the autotools test runner - we want to save those too
680 # ignore permission errors with || true
681 { find $config_src_dir -name '*.log.txt' -a -printf '%s\t%P\n' || true; } |
682 while read -r size path; do
683 local src=$config_src_dir/$path
684 # Remove text until last slash (shortest match)
685 # like $(basename $path) but in bash, for speed
686 local filename=${path##*/}
687 local dest=$log_dest_dir/$filename
688
689 if test "$size" -lt "$threshold"; then
690 cp -v $src $dest
691 else
692 # Bug fix: abriding to 1000 lines isn't sufficient. We got some logs
693 # that were hundreds of MB, with less than 1000 lines!
694 { echo "*** This log is abridged to its last 500 KB:"
695 echo
696 tail --bytes 500000 $src
697 } > $dest
698 fi
699 done
700
701 { find $config_src_dir -name 'test-suite.log' -a -printf '%P\n' || true; } |
702 while read -r path; do
703 local src=$config_src_dir/$path
704
705 # Remove text after the first slash (shortest match)
706 local package_name=${path%%/*}
707 local dest=$test_suite_dest_dir/$package_name/test-suite.log.txt
708
709 mkdir -p "$(dirname $dest)"
710 cp -v --no-target-directory $src $dest
711 done
712
713 # 500K threshold: 76 MB
714 du --si -s $log_dest_dir
715}
716
717demo-build() {
718 local pkg=${1:-gzip} # in shardA, uses many cores
719 local do_pin=${2:-}
720
721 local -a prefix
722 if test -n "$do_pin"; then
723 echo "*** Pinning to CPU 0 ***"
724 prefix=( taskset -c 0 )
725 fi
726
727 "${prefix[@]}" $CHROOT_DIR/enter-chroot -u udu sh -c '
728 pkg=$1
729
730 echo "nproc = $(nproc)"
731
732 cd oils
733 set -x
734
735 # Note the user / real ratio! How many cores did we use?
736 time regtest/aports-guest.sh build-one-package $pkg
737 ' dummy0 $pkg
738}
739
740test-taskset() {
741 local pkg=${1:-gzip} # in shardA, uses many cores
742
743 demo-build $pkg ''
744 demo-build $pkg T
745}
746
747test-proc-log() {
748 local out_dir=_tmp/proc-log
749 mkdir -p $out_dir
750
751 regtest/proc_log.py --out-dir $out_dir &
752 local pid=$!
753 sleep 3.1 # should get 3 entries
754 kill $pid
755 wc -l $out_dir/*.txt
756}
757
758task-five "$@"