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

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